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) { | 24 static void test_string_null(skiatest::Reporter* reporter) { |
25 uint8_t storage[8]; | 25 uint8_t storage[8]; |
26 SkWriter32 writer(0, storage, sizeof(storage)); | 26 SkWriter32 writer(0, storage, sizeof(storage)); |
27 SkReader32 reader(storage, sizeof(storage)); | |
28 | |
29 const char* str; | |
30 size_t len; | |
31 | 27 |
32 // Can we write NULL? | 28 // Can we write NULL? |
33 writer.writeString(NULL); | 29 writer.writeString(NULL); |
34 const int32_t null[] = { 0xFFFF }; | 30 const int32_t expected[] = { 0x0, 0x0 }; |
35 check_contents(reporter, writer, null, sizeof(null)); | 31 check_contents(reporter, writer, expected, sizeof(expected)); |
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 } | 32 } |
51 | 33 |
52 static void test_rewind(skiatest::Reporter* reporter) { | 34 static void test_rewind(skiatest::Reporter* reporter) { |
53 SkSWriter32<32> writer(32); | 35 SkSWriter32<32> writer(32); |
54 int32_t array[3] = { 1, 2, 4 }; | 36 int32_t array[3] = { 1, 2, 4 }; |
55 | 37 |
56 REPORTER_ASSERT(reporter, 0 == writer.bytesWritten()); | 38 REPORTER_ASSERT(reporter, 0 == writer.bytesWritten()); |
57 for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) { | 39 for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) { |
58 writer.writeInt(array[i]); | 40 writer.writeInt(array[i]); |
59 } | 41 } |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 testWritePad(reporter, &writer); | 238 testWritePad(reporter, &writer); |
257 } | 239 } |
258 | 240 |
259 test_string_null(reporter); | 241 test_string_null(reporter); |
260 test_ptr(reporter); | 242 test_ptr(reporter); |
261 test_rewind(reporter); | 243 test_rewind(reporter); |
262 } | 244 } |
263 | 245 |
264 #include "TestClassDef.h" | 246 #include "TestClassDef.h" |
265 DEFINE_TESTCLASS("Writer32", Writer32Class, Tests) | 247 DEFINE_TESTCLASS("Writer32", Writer32Class, Tests) |
OLD | NEW |