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 | { |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 80 | 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.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 84 | enum { |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 85 | kDataLenUsedForKey = 8 |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
bsalomon@google.com | 0a17bec | 2011-08-18 20:23:09 +0000 | [diff] [blame] | 88 | GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyA; |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 89 | keyA.setKeyData(testStringA); |
| 90 | // test copy constructor and comparison |
bsalomon@google.com | 0a17bec | 2011-08-18 20:23:09 +0000 | [diff] [blame] | 91 | GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyA2(keyA); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 92 | GrAssert(keyA.compare(keyA2) == 0); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 93 | GrAssert(keyA.getHash() == keyA2.getHash()); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 94 | // test re-init |
| 95 | keyA2.setKeyData(testStringA); |
| 96 | GrAssert(keyA.compare(keyA2) == 0); |
| 97 | GrAssert(keyA.getHash() == keyA2.getHash()); |
| 98 | // test sorting |
bsalomon@google.com | 0a17bec | 2011-08-18 20:23:09 +0000 | [diff] [blame] | 99 | GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyB; |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 100 | keyB.setKeyData(testStringB); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 101 | GrAssert(keyA.compare(keyB) < 0); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 102 | GrAssert(keyA.getHash() != keyB.getHash()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 103 | } |
| 104 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 105 | static 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 224 | void gr_run_unittests() { |
| 225 | test_tdarray(); |
| 226 | test_bsearch(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 227 | test_binHashKey(); |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 228 | test_convex(); |
bsalomon@google.com | 6034c50 | 2011-02-22 16:37:47 +0000 | [diff] [blame] | 229 | GrRedBlackTree<int>::UnitTest(); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 230 | GrDrawTarget::VertexLayoutUnitTest(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | } |