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 "SkPackBits.h" | 8 #include "SkPackBits.h" |
9 #include "Test.h" | 9 #include "Test.h" |
10 | 10 |
11 static const uint16_t gTest0[] = { 0, 0, 1, 1 }; | |
12 static const uint16_t gTest1[] = { 1, 2, 3, 4, 5, 6 }; | |
13 static const uint16_t gTest2[] = { 0, 0, 0, 1, 2, 3, 3, 3 }; | |
14 static const uint16_t gTest3[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 0, 0, 1 }; | |
15 | |
16 #include "SkRandom.h" | 11 #include "SkRandom.h" |
17 static SkRandom gRand; | 12 static SkRandom gRand; |
18 static void rand_fill(uint16_t buffer[], int count) { | |
19 for (int i = 0; i < count; i++) | |
20 buffer[i] = (uint16_t)gRand.nextU(); | |
21 } | |
22 | |
23 static void test_pack16(skiatest::Reporter* reporter) { | |
24 static const struct { | |
25 const uint16_t* fSrc; | |
26 int fCount; | |
27 } gTests[] = { | |
28 { gTest0, SK_ARRAY_COUNT(gTest0) }, | |
29 { gTest1, SK_ARRAY_COUNT(gTest1) }, | |
30 { gTest2, SK_ARRAY_COUNT(gTest2) }, | |
31 { gTest3, SK_ARRAY_COUNT(gTest3) } | |
32 }; | |
33 | |
34 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); i++) { | |
35 uint8_t dst[100]; | |
36 size_t dstSize = SkPackBits::Pack16(gTests[i].fSrc, | |
37 gTests[i].fCount, dst); | |
38 uint16_t src[100]; | |
39 int srcCount = SkPackBits::Unpack16(dst, dstSize, src); | |
40 bool match = gTests[i].fCount == srcCount && memcmp(gTests[i].fSrc, src, | |
41 gTests[i].fCount * sizeof(uint16_t)) == 0; | |
42 REPORTER_ASSERT(reporter, match); | |
43 } | |
44 | |
45 for (int n = 1000; n; n--) { | |
46 int size = 50; | |
47 uint16_t src[100], src2[100]; | |
48 uint8_t dst[200]; | |
49 rand_fill(src, size); | |
50 | |
51 size_t dstSize = SkPackBits::Pack16(src, size, dst); | |
52 size_t maxSize = SkPackBits::ComputeMaxSize16(size); | |
53 REPORTER_ASSERT(reporter, maxSize >= dstSize); | |
54 | |
55 int srcCount = SkPackBits::Unpack16(dst, dstSize, src2); | |
56 REPORTER_ASSERT(reporter, size == srcCount); | |
57 bool match = memcmp(src, src2, size * sizeof(uint16_t)) == 0; | |
58 REPORTER_ASSERT(reporter, match); | |
59 } | |
60 } | |
61 | |
62 static const uint8_t gTest80[] = { 0, 0, 1, 1 }; | 13 static const uint8_t gTest80[] = { 0, 0, 1, 1 }; |
63 static const uint8_t gTest81[] = { 1, 2, 3, 4, 5, 6 }; | 14 static const uint8_t gTest81[] = { 1, 2, 3, 4, 5, 6 }; |
64 static const uint8_t gTest82[] = { 0, 0, 0, 1, 2, 3, 3, 3 }; | 15 static const uint8_t gTest82[] = { 0, 0, 0, 1, 2, 3, 3, 3 }; |
65 static const uint8_t gTest83[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 0, 0, 1 }; | 16 static const uint8_t gTest83[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 0, 0, 1 }; |
66 static const uint8_t gTest84[] = { 1, 0, 3, 0, 0, 0, 2, 1, 1, 2 }; | 17 static const uint8_t gTest84[] = { 1, 0, 3, 0, 0, 0, 2, 1, 1, 2 }; |
67 | 18 |
68 static void rand_fill(uint8_t buffer[], int count) { | 19 static void rand_fill(uint8_t buffer[], int count) { |
69 for (int i = 0; i < count; i++) | 20 for (int i = 0; i < count; i++) |
70 buffer[i] = (uint8_t)((gRand.nextU() >> 8) & 0x3); | 21 buffer[i] = (uint8_t)((gRand.nextU() >> 8) & 0x3); |
71 } | 22 } |
72 | 23 |
73 static void test_pack8(skiatest::Reporter* reporter) { | 24 static void test_pack8(skiatest::Reporter* reporter) { |
74 static const struct { | 25 static const struct { |
75 const uint8_t* fSrc; | 26 const uint8_t* fSrc; |
76 int fCount; | 27 int fCount; |
77 } gTests[] = { | 28 } gTests[] = { |
78 { gTest80, SK_ARRAY_COUNT(gTest80) }, | 29 { gTest80, SK_ARRAY_COUNT(gTest80) }, |
79 { gTest81, SK_ARRAY_COUNT(gTest81) }, | 30 { gTest81, SK_ARRAY_COUNT(gTest81) }, |
80 { gTest82, SK_ARRAY_COUNT(gTest82) }, | 31 { gTest82, SK_ARRAY_COUNT(gTest82) }, |
81 { gTest83, SK_ARRAY_COUNT(gTest83) }, | 32 { gTest83, SK_ARRAY_COUNT(gTest83) }, |
82 { gTest84, SK_ARRAY_COUNT(gTest84) } | 33 { gTest84, SK_ARRAY_COUNT(gTest84) } |
83 }; | 34 }; |
84 | 35 |
85 for (size_t i = 4; i < SK_ARRAY_COUNT(gTests); i++) { | 36 for (size_t i = 4; i < SK_ARRAY_COUNT(gTests); i++) { |
86 uint8_t dst[100]; | 37 uint8_t dst[100]; |
87 size_t maxSize = SkPackBits::ComputeMaxSize8(gTests[i].fCount); | 38 size_t maxSize = SkPackBits::ComputeMaxSize8(gTests[i].fCount); |
88 size_t dstSize = SkPackBits::Pack8(gTests[i].fSrc, | 39 size_t dstSize = SkPackBits::Pack8(gTests[i].fSrc, |
89 gTests[i].fCount, dst); | 40 gTests[i].fCount, dst, maxSize - 1); |
| 41 REPORTER_ASSERT(reporter, dstSize == 0); |
| 42 dstSize = SkPackBits::Pack8(gTests[i].fSrc, |
| 43 gTests[i].fCount, dst, sizeof(dst)); |
90 REPORTER_ASSERT(reporter, dstSize <= maxSize); | 44 REPORTER_ASSERT(reporter, dstSize <= maxSize); |
91 uint8_t src[100]; | 45 uint8_t src[100]; |
92 int srcCount = SkPackBits::Unpack8(dst, dstSize, src); | 46 int srcCount = SkPackBits::Unpack8(dst, dstSize, src, gTests[i].fCount -
1); |
| 47 REPORTER_ASSERT(reporter, srcCount == 0); |
| 48 srcCount = SkPackBits::Unpack8(dst, dstSize, src, sizeof(src)); |
93 bool match = gTests[i].fCount == srcCount && | 49 bool match = gTests[i].fCount == srcCount && |
94 memcmp(gTests[i].fSrc, src, | 50 memcmp(gTests[i].fSrc, src, |
95 gTests[i].fCount * sizeof(uint8_t)) == 0; | 51 gTests[i].fCount * sizeof(uint8_t)) == 0; |
96 REPORTER_ASSERT(reporter, match); | 52 REPORTER_ASSERT(reporter, match); |
97 } | 53 } |
98 | 54 |
99 for (uint32_t size = 1; size <= 512; size += 1) { | 55 for (uint32_t size = 1; size <= 512; size += 1) { |
100 for (int n = 100; n; n--) { | 56 for (int n = 100; n; n--) { |
101 uint8_t src[600], src2[600]; | 57 uint8_t src[600], src2[600]; |
102 uint8_t dst[600]; | 58 uint8_t dst[600]; |
103 rand_fill(src, size); | 59 rand_fill(src, size); |
104 | 60 |
105 size_t dstSize = SkPackBits::Pack8(src, size, dst); | 61 size_t dstSize = SkPackBits::Pack8(src, size, dst, sizeof(dst)); |
106 size_t maxSize = SkPackBits::ComputeMaxSize8(size); | 62 size_t maxSize = SkPackBits::ComputeMaxSize8(size); |
107 REPORTER_ASSERT(reporter, maxSize >= dstSize); | 63 REPORTER_ASSERT(reporter, maxSize >= dstSize); |
108 | 64 |
109 size_t srcCount = SkPackBits::Unpack8(dst, dstSize, src2); | 65 size_t srcCount = SkPackBits::Unpack8(dst, dstSize, src2, size); |
110 REPORTER_ASSERT(reporter, size == srcCount); | 66 REPORTER_ASSERT(reporter, size == srcCount); |
111 bool match = memcmp(src, src2, size * sizeof(uint8_t)) == 0; | 67 bool match = memcmp(src, src2, size * sizeof(uint8_t)) == 0; |
112 REPORTER_ASSERT(reporter, match); | 68 REPORTER_ASSERT(reporter, match); |
113 | |
114 for (int j = 0; j < 100; j++) { | |
115 uint32_t skip = gRand.nextU() % size; | |
116 uint32_t write = gRand.nextU() % size; | |
117 if (skip + write > size) { | |
118 write = size - skip; | |
119 } | |
120 SkPackBits::Unpack8(src, skip, write, dst); | |
121 bool match = memcmp(src, src2 + skip, write) == 0; | |
122 REPORTER_ASSERT(reporter, match); | |
123 } | |
124 } | 69 } |
125 } | 70 } |
126 } | 71 } |
127 | 72 |
128 DEF_TEST(PackBits, reporter) { | 73 DEF_TEST(PackBits, reporter) { |
129 test_pack8(reporter); | 74 test_pack8(reporter); |
130 test_pack16(reporter); | |
131 } | 75 } |
OLD | NEW |