blob: 6e51e19a7c448c892da0a547560d0c27b5c14d45 [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
bsalomon@google.com0a17bec2011-08-18 20:23:09 +000088 GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyA;
junov@google.comf7c00f62011-08-18 18:15:16 +000089 keyA.setKeyData(testStringA);
90 // test copy constructor and comparison
bsalomon@google.com0a17bec2011-08-18 20:23:09 +000091 GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyA2(keyA);
junov@google.comf7c00f62011-08-18 18:15:16 +000092 GrAssert(keyA.compare(keyA2) == 0);
junov@google.comf93e7172011-03-31 21:26:24 +000093 GrAssert(keyA.getHash() == keyA2.getHash());
junov@google.comf7c00f62011-08-18 18:15:16 +000094 // test re-init
95 keyA2.setKeyData(testStringA);
96 GrAssert(keyA.compare(keyA2) == 0);
97 GrAssert(keyA.getHash() == keyA2.getHash());
98 // test sorting
bsalomon@google.com0a17bec2011-08-18 20:23:09 +000099 GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyB;
junov@google.comf7c00f62011-08-18 18:15:16 +0000100 keyB.setKeyData(testStringB);
junov@google.comf93e7172011-03-31 21:26:24 +0000101 GrAssert(keyA.compare(keyB) < 0);
junov@google.comf7c00f62011-08-18 18:15:16 +0000102 GrAssert(keyA.getHash() != keyB.getHash());
junov@google.comf93e7172011-03-31 21:26:24 +0000103}
104
reed@google.com07f3ee12011-05-16 17:21:57 +0000105static void test_convex() {
106#if 0
107 GrPath testPath;
108 GrPath::Iter testIter;
109
110 GrPath pt;
111 pt.moveTo(0, 0);
112 pt.close();
113
114 testIter.reset(pt);
115 testPath.resetFromIter(&testIter);
116 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
117
118 GrPath line;
119 line.moveTo(GrIntToScalar(12), GrIntToScalar(20));
120 line.lineTo(GrIntToScalar(-12), GrIntToScalar(-20));
121 line.close();
122
123 testIter.reset(line);
124 testPath.resetFromIter(&testIter);
125 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
126
127 GrPath triLeft;
128 triLeft.moveTo(0, 0);
129 triLeft.lineTo(1, 0);
130 triLeft.lineTo(1, 1);
131 triLeft.close();
132
133 testIter.reset(triLeft);
134 testPath.resetFromIter(&testIter);
135 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
136
137 GrPath triRight;
138 triRight.moveTo(0, 0);
139 triRight.lineTo(-1, 0);
140 triRight.lineTo(1, 1);
141 triRight.close();
142
143 testIter.reset(triRight);
144 testPath.resetFromIter(&testIter);
145 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
146
147 GrPath square;
148 square.moveTo(0, 0);
149 square.lineTo(1, 0);
150 square.lineTo(1, 1);
151 square.lineTo(0, 1);
152 square.close();
153
154 testIter.reset(square);
155 testPath.resetFromIter(&testIter);
156 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
157
158 GrPath redundantSquare;
159 square.moveTo(0, 0);
160 square.lineTo(0, 0);
161 square.lineTo(0, 0);
162 square.lineTo(1, 0);
163 square.lineTo(1, 0);
164 square.lineTo(1, 0);
165 square.lineTo(1, 1);
166 square.lineTo(1, 1);
167 square.lineTo(1, 1);
168 square.lineTo(0, 1);
169 square.lineTo(0, 1);
170 square.lineTo(0, 1);
171 square.close();
172
173 testIter.reset(redundantSquare);
174 testPath.resetFromIter(&testIter);
175 GrAssert(kConvex_ConvexHint == testPath.getConvexHint());
176
177 GrPath bowTie;
178 bowTie.moveTo(0, 0);
179 bowTie.lineTo(0, 0);
180 bowTie.lineTo(0, 0);
181 bowTie.lineTo(1, 1);
182 bowTie.lineTo(1, 1);
183 bowTie.lineTo(1, 1);
184 bowTie.lineTo(1, 0);
185 bowTie.lineTo(1, 0);
186 bowTie.lineTo(1, 0);
187 bowTie.lineTo(0, 1);
188 bowTie.lineTo(0, 1);
189 bowTie.lineTo(0, 1);
190 bowTie.close();
191
192 testIter.reset(bowTie);
193 testPath.resetFromIter(&testIter);
194 GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
195
196 GrPath spiral;
197 spiral.moveTo(0, 0);
198 spiral.lineTo(1, 0);
199 spiral.lineTo(1, 1);
200 spiral.lineTo(0, 1);
201 spiral.lineTo(0,.5);
202 spiral.lineTo(.5,.5);
203 spiral.lineTo(.5,.75);
204 spiral.close();
205
206 testIter.reset(spiral);
207 testPath.resetFromIter(&testIter);
208 GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
209
210 GrPath dent;
211 dent.moveTo(0, 0);
212 dent.lineTo(1, 1);
213 dent.lineTo(0, 1);
214 dent.lineTo(-.5,2);
215 dent.lineTo(-2, 1);
216 dent.close();
217
218 testIter.reset(dent);
219 testPath.resetFromIter(&testIter);
220 GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
221#endif
222}
223
reed@google.comac10a2d2010-12-22 21:39:39 +0000224void gr_run_unittests() {
225 test_tdarray();
226 test_bsearch();
junov@google.comf93e7172011-03-31 21:26:24 +0000227 test_binHashKey();
reed@google.com07f3ee12011-05-16 17:21:57 +0000228 test_convex();
bsalomon@google.com6034c502011-02-22 16:37:47 +0000229 GrRedBlackTree<int>::UnitTest();
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000230 GrDrawTarget::VertexLayoutUnitTest();
reed@google.comac10a2d2010-12-22 21:39:39 +0000231}