epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 11 | #include "SkRandom.h" |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 12 | #include "SkReader32.h" |
| 13 | #include "SkWriter32.h" |
| 14 | #include "Test.h" |
| 15 | |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 16 | static void check_contents(skiatest::Reporter* reporter, const SkWriter32& writer, |
| 17 | const void* expected, size_t size) { |
| 18 | SkAutoSMalloc<256> storage(size); |
| 19 | REPORTER_ASSERT(reporter, writer.bytesWritten() == size); |
| 20 | writer.flatten(storage.get()); |
| 21 | REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size)); |
| 22 | } |
| 23 | |
| 24 | static void test_rewind(skiatest::Reporter* reporter) { |
| 25 | SkSWriter32<32> writer(32); |
| 26 | int32_t array[3] = { 1, 2, 4 }; |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 27 | |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 28 | REPORTER_ASSERT(reporter, 0 == writer.bytesWritten()); |
| 29 | for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) { |
| 30 | writer.writeInt(array[i]); |
| 31 | } |
| 32 | check_contents(reporter, writer, array, sizeof(array)); |
| 33 | |
| 34 | writer.rewindToOffset(2*sizeof(int32_t)); |
| 35 | REPORTER_ASSERT(reporter, sizeof(array) - 4 == writer.bytesWritten()); |
| 36 | writer.writeInt(3); |
| 37 | REPORTER_ASSERT(reporter, sizeof(array) == writer.bytesWritten()); |
| 38 | array[2] = 3; |
| 39 | check_contents(reporter, writer, array, sizeof(array)); |
reed@google.com | d7e2782 | 2012-08-31 20:17:56 +0000 | [diff] [blame] | 40 | |
| 41 | // test rewinding past allocated chunks. This used to crash because we |
| 42 | // didn't truncate our link-list after freeing trailing blocks |
| 43 | { |
| 44 | SkWriter32 writer(64); |
| 45 | for (int i = 0; i < 100; ++i) { |
| 46 | writer.writeInt(i); |
| 47 | } |
| 48 | REPORTER_ASSERT(reporter, 100*4 == writer.bytesWritten()); |
| 49 | for (int j = 100*4; j >= 0; j -= 16) { |
| 50 | writer.rewindToOffset(j); |
| 51 | } |
| 52 | REPORTER_ASSERT(reporter, writer.bytesWritten() < 16); |
| 53 | } |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 54 | } |
| 55 | |
reed@google.com | 51c62a6 | 2012-06-12 20:47:53 +0000 | [diff] [blame] | 56 | static void test_ptr(skiatest::Reporter* reporter) { |
| 57 | SkSWriter32<32> writer(32); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 58 | |
reed@google.com | 51c62a6 | 2012-06-12 20:47:53 +0000 | [diff] [blame] | 59 | void* p0 = reporter; |
| 60 | void* p1 = &writer; |
| 61 | |
| 62 | // try writing ptrs where at least one of them may be at a non-multiple of |
| 63 | // 8 boundary, to confirm this works on 64bit machines. |
| 64 | |
| 65 | writer.writePtr(p0); |
| 66 | writer.write8(0x33); |
| 67 | writer.writePtr(p1); |
| 68 | writer.write8(0x66); |
| 69 | |
| 70 | size_t size = writer.size(); |
| 71 | REPORTER_ASSERT(reporter, 2 * sizeof(void*) + 2 * sizeof(int32_t)); |
| 72 | |
| 73 | char buffer[32]; |
| 74 | SkASSERT(sizeof(buffer) >= size); |
| 75 | writer.flatten(buffer); |
| 76 | |
| 77 | SkReader32 reader(buffer, size); |
| 78 | REPORTER_ASSERT(reporter, reader.readPtr() == p0); |
| 79 | REPORTER_ASSERT(reporter, reader.readInt() == 0x33); |
| 80 | REPORTER_ASSERT(reporter, reader.readPtr() == p1); |
| 81 | REPORTER_ASSERT(reporter, reader.readInt() == 0x66); |
| 82 | } |
| 83 | |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 84 | static void test1(skiatest::Reporter* reporter, SkWriter32* writer) { |
| 85 | const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 86 | for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) { |
| 87 | REPORTER_ASSERT(reporter, i*4 == writer->size()); |
| 88 | writer->write32(data[i]); |
| 89 | uint32_t* addr = writer->peek32(i * 4); |
| 90 | REPORTER_ASSERT(reporter, data[i] == *addr); |
| 91 | } |
| 92 | |
| 93 | char buffer[sizeof(data)]; |
| 94 | REPORTER_ASSERT(reporter, sizeof(buffer) == writer->size()); |
| 95 | writer->flatten(buffer); |
| 96 | REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer))); |
| 97 | } |
| 98 | |
| 99 | static void test2(skiatest::Reporter* reporter, SkWriter32* writer) { |
| 100 | static const char gStr[] = "abcdefghimjklmnopqrstuvwxyz"; |
| 101 | size_t i; |
| 102 | |
| 103 | size_t len = 0; |
| 104 | for (i = 0; i <= 26; ++i) { |
| 105 | len += SkWriter32::WriteStringSize(gStr, i); |
| 106 | writer->writeString(gStr, i); |
| 107 | } |
| 108 | REPORTER_ASSERT(reporter, writer->size() == len); |
| 109 | |
| 110 | SkAutoMalloc storage(len); |
| 111 | writer->flatten(storage.get()); |
| 112 | |
| 113 | SkReader32 reader; |
| 114 | reader.setMemory(storage.get(), len); |
| 115 | for (i = 0; i <= 26; ++i) { |
| 116 | REPORTER_ASSERT(reporter, !reader.eof()); |
| 117 | const char* str = reader.readString(&len); |
| 118 | REPORTER_ASSERT(reporter, i == len); |
| 119 | REPORTER_ASSERT(reporter, strlen(str) == len); |
| 120 | REPORTER_ASSERT(reporter, !memcmp(str, gStr, len)); |
scroggo@google.com | e9617eb | 2012-07-23 13:44:10 +0000 | [diff] [blame] | 121 | // Ensure that the align4 of the string is padded with zeroes. |
| 122 | size_t alignedSize = SkAlign4(len + 1); |
| 123 | for (size_t j = len; j < alignedSize; j++) { |
| 124 | REPORTER_ASSERT(reporter, 0 == str[j]); |
| 125 | } |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 126 | } |
| 127 | REPORTER_ASSERT(reporter, reader.eof()); |
| 128 | } |
| 129 | |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 130 | static void testWritePad(skiatest::Reporter* reporter, SkWriter32* writer) { |
| 131 | // Create some random data to write. |
| 132 | const size_t dataSize = 10<<2; |
| 133 | SkASSERT(SkIsAlign4(dataSize)); |
| 134 | |
| 135 | SkAutoMalloc originalData(dataSize); |
| 136 | { |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 137 | SkMWCRandom rand(0); |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 138 | uint32_t* ptr = static_cast<uint32_t*>(originalData.get()); |
| 139 | uint32_t* stop = ptr + (dataSize>>2); |
| 140 | while (ptr < stop) { |
| 141 | *ptr++ = rand.nextU(); |
| 142 | } |
| 143 | |
| 144 | // Write the random data to the writer at different lengths for |
| 145 | // different alignments. |
| 146 | for (size_t len = 0; len < dataSize; len++) { |
| 147 | writer->writePad(originalData.get(), len); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | uint32_t totalBytes = writer->size(); |
| 152 | |
| 153 | SkAutoMalloc readStorage(totalBytes); |
| 154 | writer->flatten(readStorage.get()); |
| 155 | |
| 156 | SkReader32 reader; |
| 157 | reader.setMemory(readStorage.get(), totalBytes); |
| 158 | |
| 159 | for (size_t len = 0; len < dataSize; len++) { |
| 160 | const char* readPtr = static_cast<const char*>(reader.skip(len)); |
| 161 | // Ensure that the data read is the same as what was written. |
| 162 | REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0); |
| 163 | // Ensure that the rest is padded with zeroes. |
| 164 | const char* stop = readPtr + SkAlign4(len); |
| 165 | readPtr += len; |
| 166 | while (readPtr < stop) { |
| 167 | REPORTER_ASSERT(reporter, *readPtr++ == 0); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 172 | static void Tests(skiatest::Reporter* reporter) { |
| 173 | // dynamic allocator |
| 174 | { |
| 175 | SkWriter32 writer(256 * 4); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 176 | test1(reporter, &writer); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 177 | |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 178 | writer.reset(); |
| 179 | test2(reporter, &writer); |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 180 | |
| 181 | writer.reset(); |
| 182 | testWritePad(reporter, &writer); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 183 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 184 | |
reed@google.com | 5595af1 | 2012-12-21 15:36:33 +0000 | [diff] [blame] | 185 | // storage-block |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 186 | { |
| 187 | SkWriter32 writer(0); |
| 188 | uint32_t storage[256]; |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 189 | writer.reset(storage, sizeof(storage)); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 190 | test1(reporter, &writer); |
| 191 | |
| 192 | writer.reset(storage, sizeof(storage)); |
| 193 | test2(reporter, &writer); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 194 | |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 195 | writer.reset(storage, sizeof(storage)); |
| 196 | testWritePad(reporter, &writer); |
skia.committer@gmail.com | b89a03c | 2012-12-22 02:02:33 +0000 | [diff] [blame] | 197 | |
reed@google.com | 5595af1 | 2012-12-21 15:36:33 +0000 | [diff] [blame] | 198 | // try overflowing the storage-block |
| 199 | uint32_t smallStorage[8]; |
| 200 | writer.reset(smallStorage, sizeof(smallStorage)); |
| 201 | test2(reporter, &writer); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 202 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 203 | |
reed@google.com | e49aca9 | 2012-04-24 21:12:39 +0000 | [diff] [blame] | 204 | // small storage |
| 205 | { |
reed@google.com | 51c62a6 | 2012-06-12 20:47:53 +0000 | [diff] [blame] | 206 | SkSWriter32<8 * sizeof(intptr_t)> writer(100); |
reed@google.com | e49aca9 | 2012-04-24 21:12:39 +0000 | [diff] [blame] | 207 | test1(reporter, &writer); |
| 208 | writer.reset(); // should just rewind our storage |
| 209 | test2(reporter, &writer); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 210 | |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 211 | writer.reset(); |
| 212 | testWritePad(reporter, &writer); |
reed@google.com | e49aca9 | 2012-04-24 21:12:39 +0000 | [diff] [blame] | 213 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 214 | |
reed@google.com | e49aca9 | 2012-04-24 21:12:39 +0000 | [diff] [blame] | 215 | // large storage |
| 216 | { |
reed@google.com | 51c62a6 | 2012-06-12 20:47:53 +0000 | [diff] [blame] | 217 | SkSWriter32<1024 * sizeof(intptr_t)> writer(100); |
reed@google.com | e49aca9 | 2012-04-24 21:12:39 +0000 | [diff] [blame] | 218 | test1(reporter, &writer); |
| 219 | writer.reset(); // should just rewind our storage |
| 220 | test2(reporter, &writer); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 221 | |
scroggo@google.com | dd39488 | 2012-07-24 20:47:55 +0000 | [diff] [blame] | 222 | writer.reset(); |
| 223 | testWritePad(reporter, &writer); |
reed@google.com | e49aca9 | 2012-04-24 21:12:39 +0000 | [diff] [blame] | 224 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 225 | |
reed@google.com | 51c62a6 | 2012-06-12 20:47:53 +0000 | [diff] [blame] | 226 | test_ptr(reporter); |
reed@google.com | 1cf58d0 | 2012-08-30 14:03:21 +0000 | [diff] [blame] | 227 | test_rewind(reporter); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | #include "TestClassDef.h" |
| 231 | DEFINE_TESTCLASS("Writer32", Writer32Class, Tests) |