blob: de69edcf8f3b763e7362c80fc38bbfb21daa99a3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
junov@google.comf93e7172011-03-31 21:26:24 +000011#include "GrBinHashKey.h"
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000012#include "GrDrawTarget.h"
13#include "GrMatrix.h"
14#include "GrPath.h"
15#include "GrRedBlackTree.h"
16#include "GrTDArray.h"
17
18// If we aren't inheriting these as #defines from elsewhere,
19// clang demands they be declared before we #include the template
20// that relies on them.
21static bool LT(const int& elem, int value) {
22 return elem < value;
23}
24static bool EQ(const int& elem, int value) {
25 return elem == value;
26}
27#include "GrTBSearch.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000028
29static void dump(const GrTDArray<int>& array) {
30#if 0
31 for (int i = 0; i < array.count(); i++) {
32 printf(" %d", array[i]);
33 }
34 printf("\n");
35#endif
36}
37
38static void test_tdarray() {
39 GrTDArray<int> array;
bsalomon@google.com6034c502011-02-22 16:37:47 +000040
reed@google.comac10a2d2010-12-22 21:39:39 +000041 *array.append() = 0; dump(array);
42 *array.append() = 2; dump(array);
43 *array.append() = 4; dump(array);
44 *array.append() = 6; dump(array);
45 GrAssert(array.count() == 4);
46
47 *array.insert(0) = -1; dump(array);
48 *array.insert(2) = 1; dump(array);
49 *array.insert(4) = 3; dump(array);
50 *array.insert(7) = 7; dump(array);
51 GrAssert(array.count() == 8);
52 array.remove(3); dump(array);
53 array.remove(0); dump(array);
54 array.removeShuffle(4); dump(array);
55 array.removeShuffle(1); dump(array);
56 GrAssert(array.count() == 4);
57}
58
reed@google.comac10a2d2010-12-22 21:39:39 +000059
60static void test_bsearch() {
61 const int array[] = {
62 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99
63 };
64
65 for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) {
66 for (size_t i = 0; i < n; i++) {
67 int index = GrTBSearch<int, int>(array, n, array[i]);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000068 GrAssert(index == (int) i);
reed@google.comac10a2d2010-12-22 21:39:39 +000069 index = GrTBSearch<int, int>(array, n, -array[i]);
70 GrAssert(index < 0);
71 }
72 }
73}
74
bsalomon@google.combeccee72011-04-01 14:51:07 +000075// bogus empty class for GrBinHashKey
76class BogusEntry {};
77
junov@google.comf93e7172011-03-31 21:26:24 +000078static void test_binHashKey()
79{
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +000080 const char* testStringA = "abcdABCD";
81 const char* testStringB = "abcdBBCD";
junov@google.comf93e7172011-03-31 21:26:24 +000082 enum {
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +000083 kDataLenUsedForKey = 8
junov@google.comf93e7172011-03-31 21:26:24 +000084 };
85
bsalomon@google.combeccee72011-04-01 14:51:07 +000086 typedef GrBinHashKey<BogusEntry, kDataLenUsedForKey> KeyType;
junov@google.comf93e7172011-03-31 21:26:24 +000087
88 KeyType keyA;
89 int passCnt = 0;
90 while (keyA.doPass()) {
91 ++passCnt;
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +000092 keyA.keyData(reinterpret_cast<const uint32_t*>(testStringA), kDataLenUsedForKey);
junov@google.comf93e7172011-03-31 21:26:24 +000093 }
94 GrAssert(passCnt == 1); //We expect the static allocation to suffice
bsalomon@google.combeccee72011-04-01 14:51:07 +000095 GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust;
junov@google.comf93e7172011-03-31 21:26:24 +000096 passCnt = 0;
97 while (keyBust.doPass()) {
98 ++passCnt;
99 // Exceed static storage by 1
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +0000100 keyBust.keyData(reinterpret_cast<const uint32_t*>(testStringA), kDataLenUsedForKey);
junov@google.comf93e7172011-03-31 21:26:24 +0000101 }
102 GrAssert(passCnt == 2); //We expect dynamic allocation to be necessary
103 GrAssert(keyA.getHash() == keyBust.getHash());
104
105 // Test that adding keyData in chunks gives
106 // the same hash as with one chunk
107 KeyType keyA2;
108 while (keyA2.doPass()) {
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +0000109 keyA2.keyData(reinterpret_cast<const uint32_t*>(testStringA), 4);
110 keyA2.keyData(&reinterpret_cast<const uint32_t*>(testStringA)[4], kDataLenUsedForKey-4);
junov@google.comf93e7172011-03-31 21:26:24 +0000111 }
112 GrAssert(keyA.getHash() == keyA2.getHash());
113
114 KeyType keyB;
115 while (keyB.doPass()){
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +0000116 keyB.keyData(reinterpret_cast<const uint32_t*>(testStringB), kDataLenUsedForKey);
junov@google.comf93e7172011-03-31 21:26:24 +0000117 }
118 GrAssert(keyA.compare(keyB) < 0);
119 GrAssert(keyA.compare(keyA2) == 0);
120
121 //Test ownership tranfer and copying
122 keyB.copyAndTakeOwnership(keyA);
123 GrAssert(keyA.fIsValid == false);
124 GrAssert(keyB.fIsValid);
125 GrAssert(keyB.getHash() == keyA2.getHash());
126 GrAssert(keyB.compare(keyA2) == 0);
127 keyA.deepCopyFrom(keyB);
128 GrAssert(keyA.fIsValid);
129 GrAssert(keyB.fIsValid);
130 GrAssert(keyA.getHash() == keyA2.getHash());
131 GrAssert(keyA.compare(keyA2) == 0);
132
133 //Test ownership tranfer and copying with key on heap
bsalomon@google.combeccee72011-04-01 14:51:07 +0000134 GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust2;
junov@google.comf93e7172011-03-31 21:26:24 +0000135 keyBust2.deepCopyFrom(keyBust);
136 GrAssert(keyBust.fIsValid);
137 GrAssert(keyBust2.fIsValid);
138 GrAssert(keyBust.getHash() == keyBust2.getHash());
139 GrAssert(keyBust.compare(keyBust2) == 0);
bsalomon@google.combeccee72011-04-01 14:51:07 +0000140 GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust3;
junov@google.comf93e7172011-03-31 21:26:24 +0000141 keyBust3.deepCopyFrom(keyBust);
142 GrAssert(keyBust.fIsValid == false);
143 GrAssert(keyBust3.fIsValid);
144 GrAssert(keyBust3.getHash() == keyBust2.getHash());
145 GrAssert(keyBust3.compare(keyBust2) == 0);
146}
147
reed@google.com07f3ee12011-05-16 17:21:57 +0000148static void test_convex() {
149#if 0
150 GrPath testPath;
151 GrPath::Iter testIter;
152
153 GrPath pt;
154 pt.moveTo(0, 0);
155 pt.close();
156
157 testIter.reset(pt);
158 testPath.resetFromIter(&testIter);
159 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
160
161 GrPath line;
162 line.moveTo(GrIntToScalar(12), GrIntToScalar(20));
163 line.lineTo(GrIntToScalar(-12), GrIntToScalar(-20));
164 line.close();
165
166 testIter.reset(line);
167 testPath.resetFromIter(&testIter);
168 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
169
170 GrPath triLeft;
171 triLeft.moveTo(0, 0);
172 triLeft.lineTo(1, 0);
173 triLeft.lineTo(1, 1);
174 triLeft.close();
175
176 testIter.reset(triLeft);
177 testPath.resetFromIter(&testIter);
178 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
179
180 GrPath triRight;
181 triRight.moveTo(0, 0);
182 triRight.lineTo(-1, 0);
183 triRight.lineTo(1, 1);
184 triRight.close();
185
186 testIter.reset(triRight);
187 testPath.resetFromIter(&testIter);
188 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
189
190 GrPath square;
191 square.moveTo(0, 0);
192 square.lineTo(1, 0);
193 square.lineTo(1, 1);
194 square.lineTo(0, 1);
195 square.close();
196
197 testIter.reset(square);
198 testPath.resetFromIter(&testIter);
199 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
200
201 GrPath redundantSquare;
202 square.moveTo(0, 0);
203 square.lineTo(0, 0);
204 square.lineTo(0, 0);
205 square.lineTo(1, 0);
206 square.lineTo(1, 0);
207 square.lineTo(1, 0);
208 square.lineTo(1, 1);
209 square.lineTo(1, 1);
210 square.lineTo(1, 1);
211 square.lineTo(0, 1);
212 square.lineTo(0, 1);
213 square.lineTo(0, 1);
214 square.close();
215
216 testIter.reset(redundantSquare);
217 testPath.resetFromIter(&testIter);
218 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
219
220 GrPath bowTie;
221 bowTie.moveTo(0, 0);
222 bowTie.lineTo(0, 0);
223 bowTie.lineTo(0, 0);
224 bowTie.lineTo(1, 1);
225 bowTie.lineTo(1, 1);
226 bowTie.lineTo(1, 1);
227 bowTie.lineTo(1, 0);
228 bowTie.lineTo(1, 0);
229 bowTie.lineTo(1, 0);
230 bowTie.lineTo(0, 1);
231 bowTie.lineTo(0, 1);
232 bowTie.lineTo(0, 1);
233 bowTie.close();
234
235 testIter.reset(bowTie);
236 testPath.resetFromIter(&testIter);
237 GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
238
239 GrPath spiral;
240 spiral.moveTo(0, 0);
241 spiral.lineTo(1, 0);
242 spiral.lineTo(1, 1);
243 spiral.lineTo(0, 1);
244 spiral.lineTo(0,.5);
245 spiral.lineTo(.5,.5);
246 spiral.lineTo(.5,.75);
247 spiral.close();
248
249 testIter.reset(spiral);
250 testPath.resetFromIter(&testIter);
251 GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
252
253 GrPath dent;
254 dent.moveTo(0, 0);
255 dent.lineTo(1, 1);
256 dent.lineTo(0, 1);
257 dent.lineTo(-.5,2);
258 dent.lineTo(-2, 1);
259 dent.close();
260
261 testIter.reset(dent);
262 testPath.resetFromIter(&testIter);
263 GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
264#endif
265}
266
reed@google.comac10a2d2010-12-22 21:39:39 +0000267void gr_run_unittests() {
268 test_tdarray();
269 test_bsearch();
junov@google.comf93e7172011-03-31 21:26:24 +0000270 test_binHashKey();
reed@google.com07f3ee12011-05-16 17:21:57 +0000271 test_convex();
bsalomon@google.com6034c502011-02-22 16:37:47 +0000272 GrRedBlackTree<int>::UnitTest();
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000273 GrDrawTarget::VertexLayoutUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +0000274}