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 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 9 | #include "GrBinHashKey.h" |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 10 | #include "GrDrawTarget.h" |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 11 | #include "SkMatrix.h" |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 12 | #include "GrRedBlackTree.h" |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 13 | |
caryclark@google.com | cf6285b | 2012-06-06 12:09:01 +0000 | [diff] [blame] | 14 | // FIXME: needs to be in a header |
| 15 | void gr_run_unittests(); |
| 16 | |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 17 | // If we aren't inheriting these as #defines from elsewhere, |
| 18 | // clang demands they be declared before we #include the template |
| 19 | // that relies on them. |
sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame^] | 20 | #if GR_DEBUG |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 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 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | static void test_bsearch() { |
| 30 | const int array[] = { |
| 31 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99 |
| 32 | }; |
| 33 | |
| 34 | for (size_t n = 0; n < GR_ARRAY_COUNT(array); n++) { |
| 35 | for (size_t i = 0; i < n; i++) { |
| 36 | int index = GrTBSearch<int, int>(array, n, array[i]); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 37 | GrAssert(index == (int) i); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 38 | index = GrTBSearch<int, int>(array, n, -array[i]); |
| 39 | GrAssert(index < 0); |
| 40 | } |
| 41 | } |
| 42 | } |
sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame^] | 43 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | |
bsalomon@google.com | beccee7 | 2011-04-01 14:51:07 +0000 | [diff] [blame] | 45 | // bogus empty class for GrBinHashKey |
| 46 | class BogusEntry {}; |
| 47 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 48 | static void test_binHashKey() |
| 49 | { |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 50 | const char* testStringA_ = "abcdABCD"; |
| 51 | const char* testStringB_ = "abcdBBCD"; |
| 52 | const uint32_t* testStringA = reinterpret_cast<const uint32_t*>(testStringA_); |
| 53 | const uint32_t* testStringB = reinterpret_cast<const uint32_t*>(testStringB_); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 54 | enum { |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 55 | kDataLenUsedForKey = 8 |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 58 | GrTBinHashKey<BogusEntry, kDataLenUsedForKey> keyA; |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 59 | keyA.setKeyData(testStringA); |
| 60 | // test copy constructor and comparison |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 61 | GrTBinHashKey<BogusEntry, kDataLenUsedForKey> keyA2(keyA); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 62 | GrAssert(keyA.compare(keyA2) == 0); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 63 | GrAssert(keyA.getHash() == keyA2.getHash()); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 64 | // test re-init |
| 65 | keyA2.setKeyData(testStringA); |
| 66 | GrAssert(keyA.compare(keyA2) == 0); |
| 67 | GrAssert(keyA.getHash() == keyA2.getHash()); |
| 68 | // test sorting |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 69 | GrTBinHashKey<BogusEntry, kDataLenUsedForKey> keyB; |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 70 | keyB.setKeyData(testStringB); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 71 | GrAssert(keyA.compare(keyB) < 0); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 72 | GrAssert(keyA.getHash() != keyB.getHash()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 73 | } |
| 74 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 75 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 76 | void gr_run_unittests() { |
sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame^] | 77 | GR_DEBUGCODE(test_bsearch();) |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 78 | test_binHashKey(); |
bsalomon@google.com | 6034c50 | 2011-02-22 16:37:47 +0000 | [diff] [blame] | 79 | GrRedBlackTree<int>::UnitTest(); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 80 | GrDrawState::VertexAttributesUnitTest(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 81 | } |