robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | |
| 10 | // This is a GR test |
| 11 | #if SK_SUPPORT_GPU |
mtklein@google.com | 4c2af74 | 2013-10-21 21:04:06 +0000 | [diff] [blame] | 12 | #include "GrTHashTable.h" |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 13 | |
| 14 | struct HashElement { |
| 15 | int fKey; |
| 16 | int fValue; |
| 17 | }; |
| 18 | |
| 19 | class GrFindPositivesFunctor { |
| 20 | public: |
| 21 | // only return elements with positive values |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 22 | bool operator()(const HashElement* elem) const { |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 23 | return elem->fValue > 0; |
| 24 | } |
| 25 | }; |
| 26 | |
| 27 | class GrFindNegativesFunctor { |
| 28 | public: |
| 29 | // only return elements with negative values |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 30 | bool operator()(const HashElement* elem) const { |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 31 | return elem->fValue < 0; |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | class HashKey { |
| 36 | public: |
| 37 | HashKey(int key) : fKey(key) {} |
| 38 | |
| 39 | uint32_t getHash() const { return fKey; } |
| 40 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 41 | static bool LessThan(const HashElement& entry, const HashKey& key) { |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 42 | return entry.fKey < key.fKey; |
| 43 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 44 | static bool Equals(const HashElement& entry, const HashKey& key) { |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 45 | return entry.fKey == key.fKey; |
| 46 | } |
| 47 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 48 | #ifdef SK_DEBUG |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 49 | static bool LessThan(const HashElement& a, const HashElement& b) { |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 50 | return a.fKey < b.fKey; |
| 51 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 52 | static bool Equals(const HashElement& a, const HashElement& b) { |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 53 | return a.fKey == b.fKey; |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | protected: |
| 58 | int fKey; |
| 59 | }; |
| 60 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 61 | DEF_TEST(HashCache, reporter) { |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 62 | GrTHashTable<HashElement, HashKey, 4> cache; |
| 63 | |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 64 | HashElement negHashElements[10] = { |
| 65 | { 0, 0 }, |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 66 | { 1, -1 }, |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 67 | { 2, -2 }, |
| 68 | { 3, -3 }, |
| 69 | { 4, -4 }, |
| 70 | { 5, -5 }, |
| 71 | { 6, -6 }, |
| 72 | { 7, -7 }, |
| 73 | { 8, -8 }, |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 74 | { 9, -9 } |
| 75 | }; |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 76 | HashElement posHashElements[10] = { |
| 77 | { 0, 0 }, |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 78 | { 1, 1 }, |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 79 | { 2, 2 }, |
| 80 | { 3, 3 }, |
| 81 | { 4, 4 }, |
| 82 | { 5, 5 }, |
| 83 | { 6, 6 }, |
| 84 | { 7, 7 }, |
| 85 | { 8, 8 }, |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 86 | { 9, 9 } |
| 87 | }; |
| 88 | |
| 89 | // add i: -i pairs |
| 90 | for (int i = 0; i < 10; ++i) { |
| 91 | cache.insert(HashKey(i), &negHashElements[i]); |
| 92 | } |
| 93 | |
| 94 | REPORTER_ASSERT(reporter, 10 == cache.count()); |
| 95 | |
| 96 | // look for all i's and assert we found the -i's |
| 97 | for (int i = 0; i < 10; ++i) { |
| 98 | HashElement* found = cache.find(i); |
| 99 | REPORTER_ASSERT(reporter, NULL != found && -i == found->fValue); |
| 100 | } |
| 101 | |
| 102 | // look for something not in the cache |
| 103 | { |
| 104 | HashElement* found = cache.find(10); |
| 105 | REPORTER_ASSERT(reporter, NULL == found); |
| 106 | } |
| 107 | |
| 108 | // add i:i duplicates (so each i will have a positive & negative entry) |
| 109 | for (int i = 0; i < 10; ++i) { |
| 110 | cache.insert(i, &posHashElements[i]); |
| 111 | } |
| 112 | |
| 113 | REPORTER_ASSERT(reporter, 20 == cache.count()); |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 114 | |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 115 | // test out the find functor to find all the positive values |
| 116 | { |
| 117 | GrFindPositivesFunctor findPos; |
| 118 | |
| 119 | HashElement* found = cache.find(0, findPos); |
| 120 | REPORTER_ASSERT(reporter, NULL == found); |
| 121 | |
| 122 | for (int i = 1; i < 10; ++i) { |
| 123 | found = cache.find(i, findPos); |
| 124 | |
| 125 | REPORTER_ASSERT(reporter, NULL != found && found->fValue > 0); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // make sure finding the positives wasn't a fluke - find the negatives |
| 130 | { |
| 131 | GrFindNegativesFunctor findNeg; |
| 132 | |
| 133 | HashElement* found = cache.find(0, findNeg); |
| 134 | REPORTER_ASSERT(reporter, NULL == found); |
| 135 | |
| 136 | for (int i = 1; i < 10; ++i) { |
| 137 | found = cache.find(i, findNeg); |
| 138 | |
| 139 | REPORTER_ASSERT(reporter, NULL != found && found->fValue < 0); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // remove the 0:0 entries |
| 144 | { |
| 145 | cache.remove(0, &negHashElements[0]); |
| 146 | cache.remove(0, &posHashElements[0]); |
| 147 | REPORTER_ASSERT(reporter, 18 == cache.count()); |
| 148 | |
| 149 | HashElement* found = cache.find(0); |
| 150 | REPORTER_ASSERT(reporter, NULL == found); |
| 151 | } |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 152 | } |
| 153 | |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 154 | #endif |