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" |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 14 | #include "GrRedBlackTree.h" |
| 15 | #include "GrTDArray.h" |
| 16 | |
caryclark@google.com | cf6285b | 2012-06-06 12:09:01 +0000 | [diff] [blame^] | 17 | // FIXME: needs to be in a header |
| 18 | void gr_run_unittests(); |
| 19 | |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 20 | // If we aren't inheriting these as #defines from elsewhere, |
| 21 | // clang demands they be declared before we #include the template |
| 22 | // that relies on them. |
| 23 | static bool LT(const int& elem, int value) { |
| 24 | return elem < value; |
| 25 | } |
| 26 | static bool EQ(const int& elem, int value) { |
| 27 | return elem == value; |
| 28 | } |
| 29 | #include "GrTBSearch.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 30 | |
| 31 | static void dump(const GrTDArray<int>& array) { |
| 32 | #if 0 |
| 33 | for (int i = 0; i < array.count(); i++) { |
| 34 | printf(" %d", array[i]); |
| 35 | } |
| 36 | printf("\n"); |
| 37 | #endif |
| 38 | } |
| 39 | |
| 40 | static void test_tdarray() { |
| 41 | GrTDArray<int> array; |
bsalomon@google.com | 6034c50 | 2011-02-22 16:37:47 +0000 | [diff] [blame] | 42 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | *array.append() = 0; dump(array); |
| 44 | *array.append() = 2; dump(array); |
| 45 | *array.append() = 4; dump(array); |
| 46 | *array.append() = 6; dump(array); |
| 47 | GrAssert(array.count() == 4); |
| 48 | |
| 49 | *array.insert(0) = -1; dump(array); |
| 50 | *array.insert(2) = 1; dump(array); |
| 51 | *array.insert(4) = 3; dump(array); |
| 52 | *array.insert(7) = 7; dump(array); |
| 53 | GrAssert(array.count() == 8); |
| 54 | array.remove(3); dump(array); |
| 55 | array.remove(0); dump(array); |
| 56 | array.removeShuffle(4); dump(array); |
| 57 | array.removeShuffle(1); dump(array); |
| 58 | GrAssert(array.count() == 4); |
| 59 | } |
| 60 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 61 | |
| 62 | static void test_bsearch() { |
| 63 | const int array[] = { |
| 64 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99 |
| 65 | }; |
| 66 | |
| 67 | for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) { |
| 68 | for (size_t i = 0; i < n; i++) { |
| 69 | int index = GrTBSearch<int, int>(array, n, array[i]); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 70 | GrAssert(index == (int) i); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | index = GrTBSearch<int, int>(array, n, -array[i]); |
| 72 | GrAssert(index < 0); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
bsalomon@google.com | beccee7 | 2011-04-01 14:51:07 +0000 | [diff] [blame] | 77 | // bogus empty class for GrBinHashKey |
| 78 | class BogusEntry {}; |
| 79 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 80 | static void test_binHashKey() |
| 81 | { |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 82 | const char* testStringA_ = "abcdABCD"; |
| 83 | const char* testStringB_ = "abcdBBCD"; |
| 84 | const uint32_t* testStringA = reinterpret_cast<const uint32_t*>(testStringA_); |
| 85 | const uint32_t* testStringB = reinterpret_cast<const uint32_t*>(testStringB_); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 86 | enum { |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 87 | kDataLenUsedForKey = 8 |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
bsalomon@google.com | 0a17bec | 2011-08-18 20:23:09 +0000 | [diff] [blame] | 90 | GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyA; |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 91 | keyA.setKeyData(testStringA); |
| 92 | // test copy constructor and comparison |
bsalomon@google.com | 0a17bec | 2011-08-18 20:23:09 +0000 | [diff] [blame] | 93 | GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyA2(keyA); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 94 | GrAssert(keyA.compare(keyA2) == 0); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 95 | GrAssert(keyA.getHash() == keyA2.getHash()); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 96 | // test re-init |
| 97 | keyA2.setKeyData(testStringA); |
| 98 | GrAssert(keyA.compare(keyA2) == 0); |
| 99 | GrAssert(keyA.getHash() == keyA2.getHash()); |
| 100 | // test sorting |
bsalomon@google.com | 0a17bec | 2011-08-18 20:23:09 +0000 | [diff] [blame] | 101 | GrBinHashKey<BogusEntry, kDataLenUsedForKey> keyB; |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 102 | keyB.setKeyData(testStringB); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 103 | GrAssert(keyA.compare(keyB) < 0); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 104 | GrAssert(keyA.getHash() != keyB.getHash()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 105 | } |
| 106 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 107 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | void gr_run_unittests() { |
| 109 | test_tdarray(); |
| 110 | test_bsearch(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 111 | test_binHashKey(); |
bsalomon@google.com | 6034c50 | 2011-02-22 16:37:47 +0000 | [diff] [blame] | 112 | GrRedBlackTree<int>::UnitTest(); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 113 | GrDrawTarget::VertexLayoutUnitTest(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | } |