blob: 17614215e8a48aadd0d1da6dcad7cabacece38e1 [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{
junov@google.comf7c00f62011-08-18 18:15:16 +000080 const char* testStringA_ = "abcdABCD";
81 const char* testStringB_ = "abcdBBCD";
82 const uint32_t* testStringA = reinterpret_cast<const uint32_t*>(testStringA_);
83 const uint32_t* testStringB = reinterpret_cast<const uint32_t*>(testStringB_);
junov@google.comf93e7172011-03-31 21:26:24 +000084 enum {
tomhudson@google.com78e7d2c2011-06-01 20:43:05 +000085 kDataLenUsedForKey = 8
junov@google.comf93e7172011-03-31 21:26:24 +000086 };
87
junov@google.comf7c00f62011-08-18 18:15:16 +000088 class Entry {};
junov@google.comf93e7172011-03-31 21:26:24 +000089
junov@google.comf7c00f62011-08-18 18:15:16 +000090 GrBinHashKey<Entry, kDataLenUsedForKey> keyA;
91 keyA.setKeyData(testStringA);
92 // test copy constructor and comparison
93 GrBinHashKey<Entry, kDataLenUsedForKey> keyA2(keyA);
94 GrAssert(keyA.compare(keyA2) == 0);
junov@google.comf93e7172011-03-31 21:26:24 +000095 GrAssert(keyA.getHash() == keyA2.getHash());
junov@google.comf7c00f62011-08-18 18:15:16 +000096 // test re-init
97 keyA2.setKeyData(testStringA);
98 GrAssert(keyA.compare(keyA2) == 0);
99 GrAssert(keyA.getHash() == keyA2.getHash());
100 // test sorting
101 GrBinHashKey<Entry, kDataLenUsedForKey> keyB;
102 keyB.setKeyData(testStringB);
junov@google.comf93e7172011-03-31 21:26:24 +0000103 GrAssert(keyA.compare(keyB) < 0);
junov@google.comf7c00f62011-08-18 18:15:16 +0000104 GrAssert(keyA.getHash() != keyB.getHash());
junov@google.comf93e7172011-03-31 21:26:24 +0000105}
106
reed@google.com07f3ee12011-05-16 17:21:57 +0000107static void test_convex() {
108#if 0
109 GrPath testPath;
110 GrPath::Iter testIter;
111
112 GrPath pt;
113 pt.moveTo(0, 0);
114 pt.close();
115
116 testIter.reset(pt);
117 testPath.resetFromIter(&testIter);
118 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
119
120 GrPath line;
121 line.moveTo(GrIntToScalar(12), GrIntToScalar(20));
122 line.lineTo(GrIntToScalar(-12), GrIntToScalar(-20));
123 line.close();
124
125 testIter.reset(line);
126 testPath.resetFromIter(&testIter);
127 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
128
129 GrPath triLeft;
130 triLeft.moveTo(0, 0);
131 triLeft.lineTo(1, 0);
132 triLeft.lineTo(1, 1);
133 triLeft.close();
134
135 testIter.reset(triLeft);
136 testPath.resetFromIter(&testIter);
137 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
138
139 GrPath triRight;
140 triRight.moveTo(0, 0);
141 triRight.lineTo(-1, 0);
142 triRight.lineTo(1, 1);
143 triRight.close();
144
145 testIter.reset(triRight);
146 testPath.resetFromIter(&testIter);
147 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
148
149 GrPath square;
150 square.moveTo(0, 0);
151 square.lineTo(1, 0);
152 square.lineTo(1, 1);
153 square.lineTo(0, 1);
154 square.close();
155
156 testIter.reset(square);
157 testPath.resetFromIter(&testIter);
158 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
159
160 GrPath redundantSquare;
161 square.moveTo(0, 0);
162 square.lineTo(0, 0);
163 square.lineTo(0, 0);
164 square.lineTo(1, 0);
165 square.lineTo(1, 0);
166 square.lineTo(1, 0);
167 square.lineTo(1, 1);
168 square.lineTo(1, 1);
169 square.lineTo(1, 1);
170 square.lineTo(0, 1);
171 square.lineTo(0, 1);
172 square.lineTo(0, 1);
173 square.close();
174
175 testIter.reset(redundantSquare);
176 testPath.resetFromIter(&testIter);
177 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
178
179 GrPath bowTie;
180 bowTie.moveTo(0, 0);
181 bowTie.lineTo(0, 0);
182 bowTie.lineTo(0, 0);
183 bowTie.lineTo(1, 1);
184 bowTie.lineTo(1, 1);
185 bowTie.lineTo(1, 1);
186 bowTie.lineTo(1, 0);
187 bowTie.lineTo(1, 0);
188 bowTie.lineTo(1, 0);
189 bowTie.lineTo(0, 1);
190 bowTie.lineTo(0, 1);
191 bowTie.lineTo(0, 1);
192 bowTie.close();
193
194 testIter.reset(bowTie);
195 testPath.resetFromIter(&testIter);
196 GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
197
198 GrPath spiral;
199 spiral.moveTo(0, 0);
200 spiral.lineTo(1, 0);
201 spiral.lineTo(1, 1);
202 spiral.lineTo(0, 1);
203 spiral.lineTo(0,.5);
204 spiral.lineTo(.5,.5);
205 spiral.lineTo(.5,.75);
206 spiral.close();
207
208 testIter.reset(spiral);
209 testPath.resetFromIter(&testIter);
210 GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
211
212 GrPath dent;
213 dent.moveTo(0, 0);
214 dent.lineTo(1, 1);
215 dent.lineTo(0, 1);
216 dent.lineTo(-.5,2);
217 dent.lineTo(-2, 1);
218 dent.close();
219
220 testIter.reset(dent);
221 testPath.resetFromIter(&testIter);
222 GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
223#endif
224}
225
reed@google.comac10a2d2010-12-22 21:39:39 +0000226void gr_run_unittests() {
227 test_tdarray();
228 test_bsearch();
junov@google.comf93e7172011-03-31 21:26:24 +0000229 test_binHashKey();
reed@google.com07f3ee12011-05-16 17:21:57 +0000230 test_convex();
bsalomon@google.com6034c502011-02-22 16:37:47 +0000231 GrRedBlackTree<int>::UnitTest();
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000232 GrDrawTarget::VertexLayoutUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +0000233}