reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 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.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 18 | #include "GrBinHashKey.h" |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 19 | #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. |
| 28 | static bool LT(const int& elem, int value) { |
| 29 | return elem < value; |
| 30 | } |
| 31 | static bool EQ(const int& elem, int value) { |
| 32 | return elem == value; |
| 33 | } |
| 34 | #include "GrTBSearch.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 35 | |
| 36 | static 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 | |
| 45 | static void test_tdarray() { |
| 46 | GrTDArray<int> array; |
bsalomon@google.com | 6034c50 | 2011-02-22 16:37:47 +0000 | [diff] [blame] | 47 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | *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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 66 | |
| 67 | static 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.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 75 | GrAssert(index == (int) i); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 76 | index = GrTBSearch<int, int>(array, n, -array[i]); |
| 77 | GrAssert(index < 0); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
bsalomon@google.com | beccee7 | 2011-04-01 14:51:07 +0000 | [diff] [blame] | 82 | // bogus empty class for GrBinHashKey |
| 83 | class BogusEntry {}; |
| 84 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 85 | static void test_binHashKey() |
| 86 | { |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 87 | const char* testStringA = "abcdABCD"; |
| 88 | const char* testStringB = "abcdBBCD"; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 89 | enum { |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 90 | kDataLenUsedForKey = 8 |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
bsalomon@google.com | beccee7 | 2011-04-01 14:51:07 +0000 | [diff] [blame] | 93 | typedef GrBinHashKey<BogusEntry, kDataLenUsedForKey> KeyType; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 94 | |
| 95 | KeyType keyA; |
| 96 | int passCnt = 0; |
| 97 | while (keyA.doPass()) { |
| 98 | ++passCnt; |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 99 | keyA.keyData(reinterpret_cast<const uint32_t*>(testStringA), kDataLenUsedForKey); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 100 | } |
| 101 | GrAssert(passCnt == 1); //We expect the static allocation to suffice |
bsalomon@google.com | beccee7 | 2011-04-01 14:51:07 +0000 | [diff] [blame] | 102 | GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 103 | passCnt = 0; |
| 104 | while (keyBust.doPass()) { |
| 105 | ++passCnt; |
| 106 | // Exceed static storage by 1 |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 107 | keyBust.keyData(reinterpret_cast<const uint32_t*>(testStringA), kDataLenUsedForKey); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 108 | } |
| 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.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 116 | keyA2.keyData(reinterpret_cast<const uint32_t*>(testStringA), 4); |
| 117 | keyA2.keyData(&reinterpret_cast<const uint32_t*>(testStringA)[4], kDataLenUsedForKey-4); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 118 | } |
| 119 | GrAssert(keyA.getHash() == keyA2.getHash()); |
| 120 | |
| 121 | KeyType keyB; |
| 122 | while (keyB.doPass()){ |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 123 | keyB.keyData(reinterpret_cast<const uint32_t*>(testStringB), kDataLenUsedForKey); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 124 | } |
| 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.com | beccee7 | 2011-04-01 14:51:07 +0000 | [diff] [blame] | 141 | GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust2; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 142 | 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.com | beccee7 | 2011-04-01 14:51:07 +0000 | [diff] [blame] | 147 | GrBinHashKey<BogusEntry, kDataLenUsedForKey-1> keyBust3; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 148 | 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.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 155 | static 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 274 | void gr_run_unittests() { |
| 275 | test_tdarray(); |
| 276 | test_bsearch(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 277 | test_binHashKey(); |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 278 | test_convex(); |
bsalomon@google.com | 6034c50 | 2011-02-22 16:37:47 +0000 | [diff] [blame] | 279 | GrRedBlackTree<int>::UnitTest(); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 280 | GrDrawTarget::VertexLayoutUnitTest(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 281 | } |