blob: 2af0def1f5d301f53095453370426de4c35988f5 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
junov@google.comf93e7172011-03-31 21:26:24 +000018#include "GrBinHashKey.h"
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000019#include "GrDrawTarget.h"
20#include "GrMatrix.h"
21#include "GrPath.h"
22#include "GrRedBlackTree.h"
23#include "GrTDArray.h"
24
25// If we aren't inheriting these as #defines from elsewhere,
26// clang demands they be declared before we #include the template
27// that relies on them.
28static bool LT(const int& elem, int value) {
29 return elem < value;
30}
31static bool EQ(const int& elem, int value) {
32 return elem == value;
33}
34#include "GrTBSearch.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000035
36static void dump(const GrTDArray<int>& array) {
37#if 0
38 for (int i = 0; i < array.count(); i++) {
39 printf(" %d", array[i]);
40 }
41 printf("\n");
42#endif
43}
44
45static void test_tdarray() {
46 GrTDArray<int> array;
bsalomon@google.com6034c502011-02-22 16:37:47 +000047
reed@google.comac10a2d2010-12-22 21:39:39 +000048 *array.append() = 0; dump(array);
49 *array.append() = 2; dump(array);
50 *array.append() = 4; dump(array);
51 *array.append() = 6; dump(array);
52 GrAssert(array.count() == 4);
53
54 *array.insert(0) = -1; dump(array);
55 *array.insert(2) = 1; dump(array);
56 *array.insert(4) = 3; dump(array);
57 *array.insert(7) = 7; dump(array);
58 GrAssert(array.count() == 8);
59 array.remove(3); dump(array);
60 array.remove(0); dump(array);
61 array.removeShuffle(4); dump(array);
62 array.removeShuffle(1); dump(array);
63 GrAssert(array.count() == 4);
64}
65
reed@google.comac10a2d2010-12-22 21:39:39 +000066
67static void test_bsearch() {
68 const int array[] = {
69 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99
70 };
71
72 for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) {
73 for (size_t i = 0; i < n; i++) {
74 int index = GrTBSearch<int, int>(array, n, array[i]);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000075 GrAssert(index == (int) i);
reed@google.comac10a2d2010-12-22 21:39:39 +000076 index = GrTBSearch<int, int>(array, n, -array[i]);
77 GrAssert(index < 0);
78 }
79 }
80}
81
bsalomon@google.combeccee72011-04-01 14:51:07 +000082// bogus empty class for GrBinHashKey
83class BogusEntry {};
84
junov@google.comf93e7172011-03-31 21:26:24 +000085static void test_binHashKey()
86{
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +000087 const char* testStringA = "abcdABCD";
88 const char* testStringB = "abcdBBCD";
junov@google.comf93e7172011-03-31 21:26:24 +000089 enum {
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +000090 kDataLenUsedForKey = 8
junov@google.comf93e7172011-03-31 21:26:24 +000091 };
92
bsalomon@google.combeccee72011-04-01 14:51:07 +000093 typedef GrBinHashKey<BogusEntry, kDataLenUsedForKey> KeyType;
junov@google.comf93e7172011-03-31 21:26:24 +000094
95 KeyType keyA;
96 int passCnt = 0;
97 while (keyA.doPass()) {
98 ++passCnt;
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +000099 keyA.keyData(reinterpret_cast<const uint32_t*>(testStringA), kDataLenUsedForKey);
junov@google.comf93e7172011-03-31 21:26:24 +0000100 }
101 GrAssert(passCnt == 1); //We expect the static allocation to suffice
bsalomon@google.combeccee72011-04-01 14:51:07 +0000102 GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust;
junov@google.comf93e7172011-03-31 21:26:24 +0000103 passCnt = 0;
104 while (keyBust.doPass()) {
105 ++passCnt;
106 // Exceed static storage by 1
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +0000107 keyBust.keyData(reinterpret_cast<const uint32_t*>(testStringA), kDataLenUsedForKey);
junov@google.comf93e7172011-03-31 21:26:24 +0000108 }
109 GrAssert(passCnt == 2); //We expect dynamic allocation to be necessary
110 GrAssert(keyA.getHash() == keyBust.getHash());
111
112 // Test that adding keyData in chunks gives
113 // the same hash as with one chunk
114 KeyType keyA2;
115 while (keyA2.doPass()) {
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +0000116 keyA2.keyData(reinterpret_cast<const uint32_t*>(testStringA), 4);
117 keyA2.keyData(&reinterpret_cast<const uint32_t*>(testStringA)[4], kDataLenUsedForKey-4);
junov@google.comf93e7172011-03-31 21:26:24 +0000118 }
119 GrAssert(keyA.getHash() == keyA2.getHash());
120
121 KeyType keyB;
122 while (keyB.doPass()){
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +0000123 keyB.keyData(reinterpret_cast<const uint32_t*>(testStringB), kDataLenUsedForKey);
junov@google.comf93e7172011-03-31 21:26:24 +0000124 }
125 GrAssert(keyA.compare(keyB) < 0);
126 GrAssert(keyA.compare(keyA2) == 0);
127
128 //Test ownership tranfer and copying
129 keyB.copyAndTakeOwnership(keyA);
130 GrAssert(keyA.fIsValid == false);
131 GrAssert(keyB.fIsValid);
132 GrAssert(keyB.getHash() == keyA2.getHash());
133 GrAssert(keyB.compare(keyA2) == 0);
134 keyA.deepCopyFrom(keyB);
135 GrAssert(keyA.fIsValid);
136 GrAssert(keyB.fIsValid);
137 GrAssert(keyA.getHash() == keyA2.getHash());
138 GrAssert(keyA.compare(keyA2) == 0);
139
140 //Test ownership tranfer and copying with key on heap
bsalomon@google.combeccee72011-04-01 14:51:07 +0000141 GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust2;
junov@google.comf93e7172011-03-31 21:26:24 +0000142 keyBust2.deepCopyFrom(keyBust);
143 GrAssert(keyBust.fIsValid);
144 GrAssert(keyBust2.fIsValid);
145 GrAssert(keyBust.getHash() == keyBust2.getHash());
146 GrAssert(keyBust.compare(keyBust2) == 0);
bsalomon@google.combeccee72011-04-01 14:51:07 +0000147 GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust3;
junov@google.comf93e7172011-03-31 21:26:24 +0000148 keyBust3.deepCopyFrom(keyBust);
149 GrAssert(keyBust.fIsValid == false);
150 GrAssert(keyBust3.fIsValid);
151 GrAssert(keyBust3.getHash() == keyBust2.getHash());
152 GrAssert(keyBust3.compare(keyBust2) == 0);
153}
154
reed@google.com07f3ee12011-05-16 17:21:57 +0000155static void test_convex() {
156#if 0
157 GrPath testPath;
158 GrPath::Iter testIter;
159
160 GrPath pt;
161 pt.moveTo(0, 0);
162 pt.close();
163
164 testIter.reset(pt);
165 testPath.resetFromIter(&testIter);
166 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
167
168 GrPath line;
169 line.moveTo(GrIntToScalar(12), GrIntToScalar(20));
170 line.lineTo(GrIntToScalar(-12), GrIntToScalar(-20));
171 line.close();
172
173 testIter.reset(line);
174 testPath.resetFromIter(&testIter);
175 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
176
177 GrPath triLeft;
178 triLeft.moveTo(0, 0);
179 triLeft.lineTo(1, 0);
180 triLeft.lineTo(1, 1);
181 triLeft.close();
182
183 testIter.reset(triLeft);
184 testPath.resetFromIter(&testIter);
185 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
186
187 GrPath triRight;
188 triRight.moveTo(0, 0);
189 triRight.lineTo(-1, 0);
190 triRight.lineTo(1, 1);
191 triRight.close();
192
193 testIter.reset(triRight);
194 testPath.resetFromIter(&testIter);
195 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
196
197 GrPath square;
198 square.moveTo(0, 0);
199 square.lineTo(1, 0);
200 square.lineTo(1, 1);
201 square.lineTo(0, 1);
202 square.close();
203
204 testIter.reset(square);
205 testPath.resetFromIter(&testIter);
206 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
207
208 GrPath redundantSquare;
209 square.moveTo(0, 0);
210 square.lineTo(0, 0);
211 square.lineTo(0, 0);
212 square.lineTo(1, 0);
213 square.lineTo(1, 0);
214 square.lineTo(1, 0);
215 square.lineTo(1, 1);
216 square.lineTo(1, 1);
217 square.lineTo(1, 1);
218 square.lineTo(0, 1);
219 square.lineTo(0, 1);
220 square.lineTo(0, 1);
221 square.close();
222
223 testIter.reset(redundantSquare);
224 testPath.resetFromIter(&testIter);
225 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
226
227 GrPath bowTie;
228 bowTie.moveTo(0, 0);
229 bowTie.lineTo(0, 0);
230 bowTie.lineTo(0, 0);
231 bowTie.lineTo(1, 1);
232 bowTie.lineTo(1, 1);
233 bowTie.lineTo(1, 1);
234 bowTie.lineTo(1, 0);
235 bowTie.lineTo(1, 0);
236 bowTie.lineTo(1, 0);
237 bowTie.lineTo(0, 1);
238 bowTie.lineTo(0, 1);
239 bowTie.lineTo(0, 1);
240 bowTie.close();
241
242 testIter.reset(bowTie);
243 testPath.resetFromIter(&testIter);
244 GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
245
246 GrPath spiral;
247 spiral.moveTo(0, 0);
248 spiral.lineTo(1, 0);
249 spiral.lineTo(1, 1);
250 spiral.lineTo(0, 1);
251 spiral.lineTo(0,.5);
252 spiral.lineTo(.5,.5);
253 spiral.lineTo(.5,.75);
254 spiral.close();
255
256 testIter.reset(spiral);
257 testPath.resetFromIter(&testIter);
258 GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
259
260 GrPath dent;
261 dent.moveTo(0, 0);
262 dent.lineTo(1, 1);
263 dent.lineTo(0, 1);
264 dent.lineTo(-.5,2);
265 dent.lineTo(-2, 1);
266 dent.close();
267
268 testIter.reset(dent);
269 testPath.resetFromIter(&testIter);
270 GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
271#endif
272}
273
reed@google.comac10a2d2010-12-22 21:39:39 +0000274void gr_run_unittests() {
275 test_tdarray();
276 test_bsearch();
junov@google.comf93e7172011-03-31 21:26:24 +0000277 test_binHashKey();
reed@google.com07f3ee12011-05-16 17:21:57 +0000278 test_convex();
bsalomon@google.com6034c502011-02-22 16:37:47 +0000279 GrRedBlackTree<int>::UnitTest();
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000280 GrDrawTarget::VertexLayoutUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +0000281}