add tests for read/writePackedUInt, and fix a bug there.

git-svn-id: http://skia.googlecode.com/svn/trunk@2482 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2011-10-18 11:49:52 +00:00
parent b3b465567d
commit 19f286b6f4
2 changed files with 56 additions and 20 deletions

View File

@ -64,28 +64,22 @@ SkScalar SkStream::readScalar() {
return value;
}
#define SK_MAX_BYTE_FOR_U8 0xFD
#define SK_BYTE_SENTINEL_FOR_U16 0xFE
#define SK_BYTE_SENTINEL_FOR_U32 0xFF
size_t SkStream::readPackedUInt() {
uint8_t byte;
if (!this->read(&byte, 1)) {
return 0;
}
if (byte != 0xFF) {
if (SK_BYTE_SENTINEL_FOR_U16 == byte) {
return this->readU16();
} else if (SK_BYTE_SENTINEL_FOR_U32 == byte) {
return this->readU32();
} else {
return byte;
}
uint16_t word;
if (!this->read(&word, 2)) {
return 0;
}
if (word != 0xFFFF) {
return word;
}
uint32_t quad;
if (!this->read(&quad, 4)) {
return 0;
}
return quad;
}
//////////////////////////////////////////////////////////////////////////////////////
@ -156,13 +150,23 @@ bool SkWStream::writeScalar(SkScalar value) {
}
bool SkWStream::writePackedUInt(size_t value) {
if (value < 0xFF) {
return this->write8(value);
} else if (value < 0xFFFF) {
return this->write8(0xFF) && this->write16(value);
uint8_t data[5];
size_t len = 1;
if (value <= SK_MAX_BYTE_FOR_U8) {
data[0] = value;
len = 1;
} else if (value <= 0xFFFF) {
uint16_t value16 = value;
data[0] = SK_BYTE_SENTINEL_FOR_U16;
memcpy(&data[1], &value16, 2);
len = 3;
} else {
return this->write16(0xFFFF) && this->write32(value);
uint32_t value32 = value;
data[0] = SK_BYTE_SENTINEL_FOR_U32;
memcpy(&data[1], &value32, 4);
len = 5;
}
return this->write(data, len);
}
bool SkWStream::writeStream(SkStream* stream, size_t length) {

View File

@ -99,9 +99,41 @@ static void TestWStream(skiatest::Reporter* reporter) {
delete[] dst;
}
static void TestPackedUInt(skiatest::Reporter* reporter) {
// we know that packeduint tries to write 1, 2 or 4 bytes for the length,
// so we test values around each of those transitions (and a few others)
const size_t sizes[] = {
0, 1, 2, 0xFC, 0xFD, 0xFE, 0xFF, 0x100, 0x101, 32767, 32768, 32769,
0xFFFD, 0xFFFE, 0xFFFF, 0x10000, 0x10001,
0xFFFFFD, 0xFFFFFE, 0xFFFFFF, 0x1000000, 0x1000001,
0x7FFFFFFE, 0x7FFFFFFF, 0x80000000, 0x80000001, 0xFFFFFFFE, 0xFFFFFFFF
};
size_t i;
char buffer[sizeof(sizes) * 4];
SkMemoryWStream wstream(buffer, sizeof(buffer));
for (i = 0; i < SK_ARRAY_COUNT(sizes); ++i) {
bool success = wstream.writePackedUInt(sizes[i]);
REPORTER_ASSERT(reporter, success);
}
wstream.flush();
SkMemoryStream rstream(buffer, sizeof(buffer));
for (i = 0; i < SK_ARRAY_COUNT(sizes); ++i) {
size_t n = rstream.readPackedUInt();
if (sizes[i] != n) {
SkDebugf("-- %d: sizes:%x n:%x\n", i, sizes[i], n);
}
REPORTER_ASSERT(reporter, sizes[i] == n);
}
}
static void TestStreams(skiatest::Reporter* reporter) {
TestRStream(reporter);
TestWStream(reporter);
TestPackedUInt(reporter);
}
#include "TestClassDef.h"