| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Test.h" | 8 #include "Test.h" |
| 9 #include "TestClassDef.h" | 9 #include "TestClassDef.h" |
| 10 #include "SkRandom.h" | 10 #include "SkRandom.h" |
| 11 #include "SkReader32.h" | 11 #include "SkReader32.h" |
| 12 #include "SkWriter32.h" | 12 #include "SkWriter32.h" |
| 13 | 13 |
| 14 static void check_contents(skiatest::Reporter* reporter, const SkWriter32& write
r, | 14 static void check_contents(skiatest::Reporter* reporter, const SkWriter32& write
r, |
| 15 const void* expected, size_t size) { | 15 const void* expected, size_t size) { |
| 16 SkAutoSMalloc<256> storage(size); | 16 SkAutoSMalloc<256> storage(size); |
| 17 REPORTER_ASSERT(reporter, writer.bytesWritten() == size); | 17 REPORTER_ASSERT(reporter, writer.bytesWritten() == size); |
| 18 writer.flatten(storage.get()); | 18 writer.flatten(storage.get()); |
| 19 REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size)); | 19 REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size)); |
| 20 } | 20 } |
| 21 | 21 |
| 22 | 22 |
| 23 static void test_reserve(skiatest::Reporter* reporter) { | 23 static void test_reserve(skiatest::Reporter* reporter) { |
| 24 // There used to be a bug where we'd assert your first reservation had to | 24 // There used to be a bug where we'd assert your first reservation had to |
| 25 // fit in external storage if you used it. This would crash in debug mode. | 25 // fit in external storage if you used it. This would crash in debug mode. |
| 26 uint8_t storage[4]; | 26 uint8_t storage[4]; |
| 27 SkWriter32 writer(0, storage, sizeof(storage)); | 27 SkWriter32 writer(storage, sizeof(storage)); |
| 28 writer.reserve(40); | 28 writer.reserve(40); |
| 29 } | 29 } |
| 30 | 30 |
| 31 static void test_string_null(skiatest::Reporter* reporter) { | 31 static void test_string_null(skiatest::Reporter* reporter) { |
| 32 uint8_t storage[8]; | 32 uint8_t storage[8]; |
| 33 SkWriter32 writer(0, storage, sizeof(storage)); | 33 SkWriter32 writer(storage, sizeof(storage)); |
| 34 | 34 |
| 35 // Can we write NULL? | 35 // Can we write NULL? |
| 36 writer.writeString(NULL); | 36 writer.writeString(NULL); |
| 37 const int32_t expected[] = { 0x0, 0x0 }; | 37 const int32_t expected[] = { 0x0, 0x0 }; |
| 38 check_contents(reporter, writer, expected, sizeof(expected)); | 38 check_contents(reporter, writer, expected, sizeof(expected)); |
| 39 } | 39 } |
| 40 | 40 |
| 41 static void test_rewind(skiatest::Reporter* reporter) { | 41 static void test_rewind(skiatest::Reporter* reporter) { |
| 42 SkSWriter32<32> writer(32); | 42 SkSWriter32<32> writer; |
| 43 int32_t array[3] = { 1, 2, 4 }; | 43 int32_t array[3] = { 1, 2, 4 }; |
| 44 | 44 |
| 45 REPORTER_ASSERT(reporter, 0 == writer.bytesWritten()); | 45 REPORTER_ASSERT(reporter, 0 == writer.bytesWritten()); |
| 46 for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) { | 46 for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) { |
| 47 writer.writeInt(array[i]); | 47 writer.writeInt(array[i]); |
| 48 } | 48 } |
| 49 check_contents(reporter, writer, array, sizeof(array)); | 49 check_contents(reporter, writer, array, sizeof(array)); |
| 50 | 50 |
| 51 writer.rewindToOffset(2*sizeof(int32_t)); | 51 writer.rewindToOffset(2*sizeof(int32_t)); |
| 52 REPORTER_ASSERT(reporter, sizeof(array) - 4 == writer.bytesWritten()); | 52 REPORTER_ASSERT(reporter, sizeof(array) - 4 == writer.bytesWritten()); |
| 53 writer.writeInt(3); | 53 writer.writeInt(3); |
| 54 REPORTER_ASSERT(reporter, sizeof(array) == writer.bytesWritten()); | 54 REPORTER_ASSERT(reporter, sizeof(array) == writer.bytesWritten()); |
| 55 array[2] = 3; | 55 array[2] = 3; |
| 56 check_contents(reporter, writer, array, sizeof(array)); | 56 check_contents(reporter, writer, array, sizeof(array)); |
| 57 | 57 |
| 58 // test rewinding past allocated chunks. This used to crash because we | 58 // test rewinding past allocated chunks. This used to crash because we |
| 59 // didn't truncate our link-list after freeing trailing blocks | 59 // didn't truncate our link-list after freeing trailing blocks |
| 60 { | 60 { |
| 61 SkWriter32 writer(64); | 61 SkWriter32 writer; |
| 62 for (int i = 0; i < 100; ++i) { | 62 for (int i = 0; i < 100; ++i) { |
| 63 writer.writeInt(i); | 63 writer.writeInt(i); |
| 64 } | 64 } |
| 65 REPORTER_ASSERT(reporter, 100*4 == writer.bytesWritten()); | 65 REPORTER_ASSERT(reporter, 100*4 == writer.bytesWritten()); |
| 66 for (int j = 100*4; j >= 0; j -= 16) { | 66 for (int j = 100*4; j >= 0; j -= 16) { |
| 67 writer.rewindToOffset(j); | 67 writer.rewindToOffset(j); |
| 68 } | 68 } |
| 69 REPORTER_ASSERT(reporter, writer.bytesWritten() < 16); | 69 REPORTER_ASSERT(reporter, writer.bytesWritten() < 16); |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 | 72 |
| 73 static void test_ptr(skiatest::Reporter* reporter) { | 73 static void test_ptr(skiatest::Reporter* reporter) { |
| 74 SkSWriter32<32> writer(32); | 74 SkSWriter32<32> writer; |
| 75 | 75 |
| 76 void* p0 = reporter; | 76 void* p0 = reporter; |
| 77 void* p1 = &writer; | 77 void* p1 = &writer; |
| 78 | 78 |
| 79 // try writing ptrs where at least one of them may be at a non-multiple of | 79 // try writing ptrs where at least one of them may be at a non-multiple of |
| 80 // 8 boundary, to confirm this works on 64bit machines. | 80 // 8 boundary, to confirm this works on 64bit machines. |
| 81 | 81 |
| 82 writer.writePtr(p0); | 82 writer.writePtr(p0); |
| 83 writer.write8(0x33); | 83 writer.write8(0x33); |
| 84 writer.writePtr(p1); | 84 writer.writePtr(p1); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0)
; | 179 REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0)
; |
| 180 // Ensure that the rest is padded with zeroes. | 180 // Ensure that the rest is padded with zeroes. |
| 181 const char* stop = readPtr + SkAlign4(len); | 181 const char* stop = readPtr + SkAlign4(len); |
| 182 readPtr += len; | 182 readPtr += len; |
| 183 while (readPtr < stop) { | 183 while (readPtr < stop) { |
| 184 REPORTER_ASSERT(reporter, *readPtr++ == 0); | 184 REPORTER_ASSERT(reporter, *readPtr++ == 0); |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 } | 187 } |
| 188 | 188 |
| 189 DEF_TEST(Writer32, reporter) { | 189 DEF_TEST(Writer32_dynamic, reporter) { |
| 190 // dynamic allocator | 190 SkWriter32 writer; |
| 191 { | 191 test1(reporter, &writer); |
| 192 SkWriter32 writer(256 * 4); | |
| 193 test1(reporter, &writer); | |
| 194 | 192 |
| 195 writer.reset(); | 193 writer.reset(); |
| 196 test2(reporter, &writer); | 194 test2(reporter, &writer); |
| 197 | 195 |
| 198 writer.reset(); | 196 writer.reset(); |
| 199 testWritePad(reporter, &writer); | 197 testWritePad(reporter, &writer); |
| 200 } | 198 } |
| 201 | 199 |
| 202 // storage-block | 200 DEF_TEST(Writer32_contiguous, reporter) { |
| 203 { | 201 uint32_t storage[256]; |
| 204 SkWriter32 writer(0); | 202 SkWriter32 writer; |
| 205 uint32_t storage[256]; | 203 writer.reset(storage, sizeof(storage)); |
| 206 writer.reset(storage, sizeof(storage)); | 204 // This write is small enough to fit in storage, so it's contiguous. |
| 207 // These three writes are small enough to fit in storage. | 205 test1(reporter, &writer); |
| 208 test1(reporter, &writer); | 206 REPORTER_ASSERT(reporter, writer.contiguousArray() != NULL); |
| 209 REPORTER_ASSERT(reporter, writer.wroteOnlyToStorage()); | |
| 210 | 207 |
| 211 writer.reset(storage, sizeof(storage)); | 208 // This write is too big for the 32 byte storage block we provide. |
| 212 test2(reporter, &writer); | 209 writer.reset(storage, 32); |
| 213 REPORTER_ASSERT(reporter, writer.wroteOnlyToStorage()); | 210 test2(reporter, &writer); |
| 211 // Some data is in storage, some in writer's internal storage. |
| 212 REPORTER_ASSERT(reporter, writer.contiguousArray() == NULL); |
| 214 | 213 |
| 215 writer.reset(storage, sizeof(storage)); | 214 writer.reset(); |
| 216 testWritePad(reporter, &writer); | 215 test2(reporter, &writer); |
| 217 REPORTER_ASSERT(reporter, writer.wroteOnlyToStorage()); | 216 // There is no external storage. All the data is in internal storage, |
| 217 // so we can always read it contiguously. |
| 218 REPORTER_ASSERT(reporter, writer.contiguousArray() != NULL); |
| 219 } |
| 218 | 220 |
| 219 // Try overflowing the storage-block. | 221 DEF_TEST(Writer32_small, reporter) { |
| 220 uint32_t smallStorage[8]; | 222 SkSWriter32<8 * sizeof(intptr_t)> writer; |
| 221 writer.reset(smallStorage, sizeof(smallStorage)); | 223 test1(reporter, &writer); |
| 222 test2(reporter, &writer); | 224 writer.reset(); // should just rewind our storage |
| 223 REPORTER_ASSERT(reporter, !writer.wroteOnlyToStorage()); | 225 test2(reporter, &writer); |
| 224 } | |
| 225 | 226 |
| 226 // small storage | 227 writer.reset(); |
| 227 { | 228 testWritePad(reporter, &writer); |
| 228 SkSWriter32<8 * sizeof(intptr_t)> writer(100); | 229 } |
| 229 test1(reporter, &writer); | |
| 230 writer.reset(); // should just rewind our storage | |
| 231 test2(reporter, &writer); | |
| 232 | 230 |
| 233 writer.reset(); | 231 DEF_TEST(Writer32_large, reporter) { |
| 234 testWritePad(reporter, &writer); | 232 SkSWriter32<1024 * sizeof(intptr_t)> writer; |
| 235 } | 233 test1(reporter, &writer); |
| 234 writer.reset(); // should just rewind our storage |
| 235 test2(reporter, &writer); |
| 236 | 236 |
| 237 // large storage | 237 writer.reset(); |
| 238 { | 238 testWritePad(reporter, &writer); |
| 239 SkSWriter32<1024 * sizeof(intptr_t)> writer(100); | 239 } |
| 240 test1(reporter, &writer); | |
| 241 writer.reset(); // should just rewind our storage | |
| 242 test2(reporter, &writer); | |
| 243 | 240 |
| 244 writer.reset(); | 241 DEF_TEST(Writer32_misc, reporter) { |
| 245 testWritePad(reporter, &writer); | |
| 246 } | |
| 247 | |
| 248 test_reserve(reporter); | 242 test_reserve(reporter); |
| 249 test_string_null(reporter); | 243 test_string_null(reporter); |
| 250 test_ptr(reporter); | 244 test_ptr(reporter); |
| 251 test_rewind(reporter); | 245 test_rewind(reporter); |
| 252 } | 246 } |
| OLD | NEW |