epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 11 | #include "GrBinHashKey.h" |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 12 | #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. |
| 21 | static bool LT(const int& elem, int value) { |
| 22 | return elem < value; |
| 23 | } |
| 24 | static bool EQ(const int& elem, int value) { |
| 25 | return elem == value; |
| 26 | } |
| 27 | #include "GrTBSearch.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 28 | |
| 29 | static 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 | |
| 38 | static void test_tdarray() { |
| 39 | GrTDArray<int> array; |
bsalomon@google.com | 6034c50 | 2011-02-22 16:37:47 +0000 | [diff] [blame] | 40 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | *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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | |
| 60 | static 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.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 68 | GrAssert(index == (int) i); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 69 | index = GrTBSearch<int, int>(array, n, -array[i]); |
| 70 | GrAssert(index < 0); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
bsalomon@google.com | beccee7 | 2011-04-01 14:51:07 +0000 | [diff] [blame] | 75 | // bogus empty class for GrBinHashKey |
| 76 | class BogusEntry {}; |
| 77 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 78 | static void test_binHashKey() |
| 79 | { |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 80 | const char* testStringA = "abcdABCD"; |
| 81 | const char* testStringB = "abcdBBCD"; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 82 | enum { |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 83 | kDataLenUsedForKey = 8 |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
bsalomon@google.com | beccee7 | 2011-04-01 14:51:07 +0000 | [diff] [blame] | 86 | typedef GrBinHashKey<BogusEntry, kDataLenUsedForKey> KeyType; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 87 | |
| 88 | KeyType keyA; |
| 89 | int passCnt = 0; |
| 90 | while (keyA.doPass()) { |
| 91 | ++passCnt; |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 92 | keyA.keyData(reinterpret_cast<const uint32_t*>(testStringA), kDataLenUsedForKey); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 93 | } |
| 94 | GrAssert(passCnt == 1); //We expect the static allocation to suffice |
bsalomon@google.com | beccee7 | 2011-04-01 14:51:07 +0000 | [diff] [blame] | 95 | GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 96 | passCnt = 0; |
| 97 | while (keyBust.doPass()) { |
| 98 | ++passCnt; |
| 99 | // Exceed static storage by 1 |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 100 | keyBust.keyData(reinterpret_cast<const uint32_t*>(testStringA), kDataLenUsedForKey); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 101 | } |
| 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.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 109 | keyA2.keyData(reinterpret_cast<const uint32_t*>(testStringA), 4); |
| 110 | keyA2.keyData(&reinterpret_cast<const uint32_t*>(testStringA)[4], kDataLenUsedForKey-4); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 111 | } |
| 112 | GrAssert(keyA.getHash() == keyA2.getHash()); |
| 113 | |
| 114 | KeyType keyB; |
| 115 | while (keyB.doPass()){ |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 116 | keyB.keyData(reinterpret_cast<const uint32_t*>(testStringB), kDataLenUsedForKey); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 117 | } |
| 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.com | beccee7 | 2011-04-01 14:51:07 +0000 | [diff] [blame] | 134 | GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust2; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 135 | 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.com | beccee7 | 2011-04-01 14:51:07 +0000 | [diff] [blame] | 140 | GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust3; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 141 | 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.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 148 | static 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 267 | void gr_run_unittests() { |
| 268 | test_tdarray(); |
| 269 | test_bsearch(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 270 | test_binHashKey(); |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 271 | test_convex(); |
bsalomon@google.com | 6034c50 | 2011-02-22 16:37:47 +0000 | [diff] [blame] | 272 | GrRedBlackTree<int>::UnitTest(); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 273 | GrDrawTarget::VertexLayoutUnitTest(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 274 | } |