OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 | 10 |
11 #include "SkRandom.h" | 11 #include "SkRandom.h" |
12 #include "SkReader32.h" | 12 #include "SkReader32.h" |
13 #include "SkWriter32.h" | 13 #include "SkWriter32.h" |
14 #include "Test.h" | 14 #include "Test.h" |
15 | 15 |
16 static void check_contents(skiatest::Reporter* reporter, const SkWriter32& write
r, | 16 static void check_contents(skiatest::Reporter* reporter, const SkWriter32& write
r, |
17 const void* expected, size_t size) { | 17 const void* expected, size_t size) { |
18 SkAutoSMalloc<256> storage(size); | 18 SkAutoSMalloc<256> storage(size); |
19 REPORTER_ASSERT(reporter, writer.bytesWritten() == size); | 19 REPORTER_ASSERT(reporter, writer.bytesWritten() == size); |
20 writer.flatten(storage.get()); | 20 writer.flatten(storage.get()); |
21 REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size)); | 21 REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size)); |
22 } | 22 } |
23 | 23 |
| 24 static void test_string_null(skiatest::Reporter* reporter) { |
| 25 uint8_t storage[8]; |
| 26 SkWriter32 writer(0, storage, sizeof(storage)); |
| 27 SkReader32 reader(storage, sizeof(storage)); |
| 28 |
| 29 const char* str; |
| 30 size_t len; |
| 31 |
| 32 // Can we write NULL? |
| 33 writer.writeString(NULL); |
| 34 const int32_t null[] = { 0xFFFF }; |
| 35 check_contents(reporter, writer, null, sizeof(null)); |
| 36 str = reader.readString(&len); |
| 37 REPORTER_ASSERT(reporter, NULL == str); |
| 38 REPORTER_ASSERT(reporter, 0 == len); |
| 39 |
| 40 writer.reset(storage, sizeof(storage)); |
| 41 reader.rewind(); |
| 42 |
| 43 // Is NULL distinct from ""? |
| 44 writer.writeString(""); |
| 45 const int32_t empty[] = { 0x0, 0x0 }; |
| 46 check_contents(reporter, writer, empty, sizeof(empty)); |
| 47 str = reader.readString(&len); |
| 48 REPORTER_ASSERT(reporter, 0 == strcmp("", str)); |
| 49 REPORTER_ASSERT(reporter, 0 == len); |
| 50 } |
| 51 |
24 static void test_rewind(skiatest::Reporter* reporter) { | 52 static void test_rewind(skiatest::Reporter* reporter) { |
25 SkSWriter32<32> writer(32); | 53 SkSWriter32<32> writer(32); |
26 int32_t array[3] = { 1, 2, 4 }; | 54 int32_t array[3] = { 1, 2, 4 }; |
27 | 55 |
28 REPORTER_ASSERT(reporter, 0 == writer.bytesWritten()); | 56 REPORTER_ASSERT(reporter, 0 == writer.bytesWritten()); |
29 for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) { | 57 for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) { |
30 writer.writeInt(array[i]); | 58 writer.writeInt(array[i]); |
31 } | 59 } |
32 check_contents(reporter, writer, array, sizeof(array)); | 60 check_contents(reporter, writer, array, sizeof(array)); |
33 | 61 |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 { | 249 { |
222 SkSWriter32<1024 * sizeof(intptr_t)> writer(100); | 250 SkSWriter32<1024 * sizeof(intptr_t)> writer(100); |
223 test1(reporter, &writer); | 251 test1(reporter, &writer); |
224 writer.reset(); // should just rewind our storage | 252 writer.reset(); // should just rewind our storage |
225 test2(reporter, &writer); | 253 test2(reporter, &writer); |
226 | 254 |
227 writer.reset(); | 255 writer.reset(); |
228 testWritePad(reporter, &writer); | 256 testWritePad(reporter, &writer); |
229 } | 257 } |
230 | 258 |
| 259 test_string_null(reporter); |
231 test_ptr(reporter); | 260 test_ptr(reporter); |
232 test_rewind(reporter); | 261 test_rewind(reporter); |
233 } | 262 } |
234 | 263 |
235 #include "TestClassDef.h" | 264 #include "TestClassDef.h" |
236 DEFINE_TESTCLASS("Writer32", Writer32Class, Tests) | 265 DEFINE_TESTCLASS("Writer32", Writer32Class, Tests) |
OLD | NEW |