Avoid a loop in writeString and writePad by zeroing padding first.
Also add a benchmark to time the new improved writeString. Before
my change the bench took ~1.23ms and afterwards it takes ~.95ms.
Add some testing to ensure that writePad works properly.
TEST=Writer32Test, WriterBench
Review URL: https://codereview.appspot.com/6438045
git-svn-id: http://skia.googlecode.com/svn/trunk@4742 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tests/Writer32Test.cpp b/tests/Writer32Test.cpp
index 5c9d7ea..8b048a7 100644
--- a/tests/Writer32Test.cpp
+++ b/tests/Writer32Test.cpp
@@ -8,6 +8,7 @@
+#include "SkRandom.h"
#include "SkReader32.h"
#include "SkWriter32.h"
#include "Test.h"
@@ -86,6 +87,48 @@
REPORTER_ASSERT(reporter, reader.eof());
}
+static void testWritePad(skiatest::Reporter* reporter, SkWriter32* writer) {
+ // Create some random data to write.
+ const size_t dataSize = 10<<2;
+ SkASSERT(SkIsAlign4(dataSize));
+
+ SkAutoMalloc originalData(dataSize);
+ {
+ SkRandom rand(0);
+ uint32_t* ptr = static_cast<uint32_t*>(originalData.get());
+ uint32_t* stop = ptr + (dataSize>>2);
+ while (ptr < stop) {
+ *ptr++ = rand.nextU();
+ }
+
+ // Write the random data to the writer at different lengths for
+ // different alignments.
+ for (size_t len = 0; len < dataSize; len++) {
+ writer->writePad(originalData.get(), len);
+ }
+ }
+
+ uint32_t totalBytes = writer->size();
+
+ SkAutoMalloc readStorage(totalBytes);
+ writer->flatten(readStorage.get());
+
+ SkReader32 reader;
+ reader.setMemory(readStorage.get(), totalBytes);
+
+ for (size_t len = 0; len < dataSize; len++) {
+ const char* readPtr = static_cast<const char*>(reader.skip(len));
+ // Ensure that the data read is the same as what was written.
+ REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0);
+ // Ensure that the rest is padded with zeroes.
+ const char* stop = readPtr + SkAlign4(len);
+ readPtr += len;
+ while (readPtr < stop) {
+ REPORTER_ASSERT(reporter, *readPtr++ == 0);
+ }
+ }
+}
+
static void Tests(skiatest::Reporter* reporter) {
// dynamic allocator
{
@@ -95,6 +138,9 @@
writer.reset();
test2(reporter, &writer);
+
+ writer.reset();
+ testWritePad(reporter, &writer);
}
// single-block
@@ -108,6 +154,9 @@
writer.reset(storage, sizeof(storage));
test2(reporter, &writer);
+
+ writer.reset(storage, sizeof(storage));
+ testWritePad(reporter, &writer);
}
// small storage
@@ -116,6 +165,9 @@
test1(reporter, &writer);
writer.reset(); // should just rewind our storage
test2(reporter, &writer);
+
+ writer.reset();
+ testWritePad(reporter, &writer);
}
// large storage
@@ -124,6 +176,9 @@
test1(reporter, &writer);
writer.reset(); // should just rewind our storage
test2(reporter, &writer);
+
+ writer.reset();
+ testWritePad(reporter, &writer);
}
test_ptr(reporter);