commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 8 | #include "SkTDynamicHash.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "Test.h" |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 10 | |
| 11 | namespace { |
| 12 | |
| 13 | struct Entry { |
| 14 | int key; |
mtklein@google.com | 0d9f5f7 | 2013-08-05 23:08:19 +0000 | [diff] [blame] | 15 | double value; |
commit-bot@chromium.org | 55bd940 | 2014-04-02 19:17:00 +0000 | [diff] [blame] | 16 | |
| 17 | static const int& GetKey(const Entry& entry) { return entry.key; } |
| 18 | static uint32_t Hash(const int& key) { return key; } |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 19 | }; |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 20 | |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 21 | |
commit-bot@chromium.org | 55bd940 | 2014-04-02 19:17:00 +0000 | [diff] [blame] | 22 | class Hash : public SkTDynamicHash<Entry, int> { |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 23 | public: |
| 24 | Hash() : INHERITED() {} |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 25 | |
| 26 | // Promote protected methods to public for this test. |
| 27 | int capacity() const { return this->INHERITED::capacity(); } |
| 28 | int countCollisions(const int& key) const { return this->INHERITED::countCollisions(key); } |
| 29 | |
| 30 | private: |
commit-bot@chromium.org | 55bd940 | 2014-04-02 19:17:00 +0000 | [diff] [blame] | 31 | typedef SkTDynamicHash<Entry, int> INHERITED; |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | #define ASSERT(x) REPORTER_ASSERT(reporter, x) |
| 37 | |
commit-bot@chromium.org | 1f6cf69 | 2014-03-05 01:00:50 +0000 | [diff] [blame] | 38 | DEF_TEST(DynamicHash_growth, reporter) { |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 39 | Entry a = { 1, 2.0 }; |
| 40 | Entry b = { 2, 3.0 }; |
| 41 | Entry c = { 3, 4.0 }; |
| 42 | Entry d = { 4, 5.0 }; |
| 43 | Entry e = { 5, 6.0 }; |
| 44 | |
commit-bot@chromium.org | 7a4b9fa | 2014-01-13 18:28:14 +0000 | [diff] [blame] | 45 | Hash hash; |
| 46 | ASSERT(hash.capacity() == 0); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 47 | |
| 48 | hash.add(&a); |
mtklein@google.com | c9ab2b7 | 2013-08-12 14:51:25 +0000 | [diff] [blame] | 49 | ASSERT(hash.capacity() == 4); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 50 | |
| 51 | hash.add(&b); |
| 52 | ASSERT(hash.capacity() == 4); |
| 53 | |
| 54 | hash.add(&c); |
mtklein@google.com | c9ab2b7 | 2013-08-12 14:51:25 +0000 | [diff] [blame] | 55 | ASSERT(hash.capacity() == 4); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 56 | |
| 57 | hash.add(&d); |
| 58 | ASSERT(hash.capacity() == 8); |
| 59 | |
| 60 | hash.add(&e); |
mtklein@google.com | c9ab2b7 | 2013-08-12 14:51:25 +0000 | [diff] [blame] | 61 | ASSERT(hash.capacity() == 8); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 62 | |
| 63 | ASSERT(hash.count() == 5); |
| 64 | } |
| 65 | |
Florin Malita | c274f8f | 2018-04-18 16:57:32 -0400 | [diff] [blame] | 66 | DEF_TEST(DynamicHash_growth_bounded, reporter) { |
| 67 | Entry a = { 1, 2.0 }; |
| 68 | Entry b = { 2, 3.0 }; |
| 69 | Entry c = { 3, 4.0 }; |
| 70 | Entry d = { 4, 5.0 }; |
| 71 | Entry e = { 5, 6.0 }; |
| 72 | |
| 73 | Hash hash; |
| 74 | ASSERT(hash.capacity() == 0); |
| 75 | |
| 76 | hash.add(&a); |
| 77 | ASSERT(hash.capacity() == 4); |
| 78 | |
| 79 | hash.remove(a.key); |
| 80 | hash.add(&b); |
| 81 | ASSERT(hash.capacity() == 4); |
| 82 | |
| 83 | hash.remove(b.key); |
| 84 | hash.add(&c); |
| 85 | ASSERT(hash.capacity() == 4); |
| 86 | |
| 87 | hash.remove(c.key); |
| 88 | hash.add(&d); |
| 89 | ASSERT(hash.capacity() == 4); |
| 90 | |
| 91 | hash.remove(d.key); |
| 92 | hash.add(&e); |
| 93 | ASSERT(hash.capacity() == 4); |
| 94 | |
| 95 | ASSERT(hash.count() == 1); |
| 96 | } |
| 97 | |
commit-bot@chromium.org | 1f6cf69 | 2014-03-05 01:00:50 +0000 | [diff] [blame] | 98 | DEF_TEST(DynamicHash_add, reporter) { |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 99 | Hash hash; |
| 100 | Entry a = { 1, 2.0 }; |
| 101 | Entry b = { 2, 3.0 }; |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 102 | |
| 103 | ASSERT(hash.count() == 0); |
| 104 | hash.add(&a); |
| 105 | ASSERT(hash.count() == 1); |
| 106 | hash.add(&b); |
| 107 | ASSERT(hash.count() == 2); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 108 | } |
| 109 | |
commit-bot@chromium.org | 1f6cf69 | 2014-03-05 01:00:50 +0000 | [diff] [blame] | 110 | DEF_TEST(DynamicHash_lookup, reporter) { |
commit-bot@chromium.org | 7a4b9fa | 2014-01-13 18:28:14 +0000 | [diff] [blame] | 111 | Hash hash; |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 112 | |
| 113 | // These collide. |
| 114 | Entry a = { 1, 2.0 }; |
| 115 | Entry b = { 5, 3.0 }; |
| 116 | |
| 117 | // Before we insert anything, nothing can collide. |
| 118 | ASSERT(hash.countCollisions(1) == 0); |
| 119 | ASSERT(hash.countCollisions(5) == 0); |
| 120 | ASSERT(hash.countCollisions(9) == 0); |
| 121 | |
| 122 | // First is easy. |
| 123 | hash.add(&a); |
| 124 | ASSERT(hash.countCollisions(1) == 0); |
| 125 | ASSERT(hash.countCollisions(5) == 1); |
| 126 | ASSERT(hash.countCollisions(9) == 1); |
| 127 | |
| 128 | // Second is one step away. |
| 129 | hash.add(&b); |
| 130 | ASSERT(hash.countCollisions(1) == 0); |
| 131 | ASSERT(hash.countCollisions(5) == 1); |
| 132 | ASSERT(hash.countCollisions(9) == 2); |
| 133 | |
| 134 | // We can find our data right? |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 135 | ASSERT(hash.find(1) != nullptr); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 136 | ASSERT(hash.find(1)->value == 2.0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 137 | ASSERT(hash.find(5) != nullptr); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 138 | ASSERT(hash.find(5)->value == 3.0); |
| 139 | |
| 140 | // These aren't in the hash. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 141 | ASSERT(hash.find(2) == nullptr); |
| 142 | ASSERT(hash.find(9) == nullptr); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 143 | } |
| 144 | |
commit-bot@chromium.org | 1f6cf69 | 2014-03-05 01:00:50 +0000 | [diff] [blame] | 145 | DEF_TEST(DynamicHash_remove, reporter) { |
commit-bot@chromium.org | 7a4b9fa | 2014-01-13 18:28:14 +0000 | [diff] [blame] | 146 | Hash hash; |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 147 | |
| 148 | // These collide. |
| 149 | Entry a = { 1, 2.0 }; |
| 150 | Entry b = { 5, 3.0 }; |
| 151 | Entry c = { 9, 4.0 }; |
| 152 | |
| 153 | hash.add(&a); |
| 154 | hash.add(&b); |
| 155 | hash.remove(1); |
| 156 | // a should be marked deleted, and b should still be findable. |
| 157 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 158 | ASSERT(hash.find(1) == nullptr); |
| 159 | ASSERT(hash.find(5) != nullptr); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 160 | ASSERT(hash.find(5)->value == 3.0); |
| 161 | |
| 162 | // This will go in the same slot as 'a' did before. |
| 163 | ASSERT(hash.countCollisions(9) == 0); |
| 164 | hash.add(&c); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 165 | ASSERT(hash.find(9) != nullptr); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 166 | ASSERT(hash.find(9)->value == 4.0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 167 | ASSERT(hash.find(5) != nullptr); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 168 | ASSERT(hash.find(5)->value == 3.0); |
| 169 | } |
| 170 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 171 | template<typename T> static void TestIter(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 1f6cf69 | 2014-03-05 01:00:50 +0000 | [diff] [blame] | 172 | Hash hash; |
| 173 | |
| 174 | int count = 0; |
| 175 | // this should fall out of loop immediately |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 176 | for (T iter(&hash); !iter.done(); ++iter) { |
commit-bot@chromium.org | 1f6cf69 | 2014-03-05 01:00:50 +0000 | [diff] [blame] | 177 | ++count; |
| 178 | } |
| 179 | ASSERT(0 == count); |
| 180 | |
| 181 | // These collide. |
| 182 | Entry a = { 1, 2.0 }; |
| 183 | Entry b = { 5, 3.0 }; |
| 184 | Entry c = { 9, 4.0 }; |
| 185 | |
| 186 | hash.add(&a); |
| 187 | hash.add(&b); |
| 188 | hash.add(&c); |
| 189 | |
| 190 | // should see all 3 unique keys when iterating over hash |
| 191 | count = 0; |
| 192 | int keys[3] = {0, 0, 0}; |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 193 | for (T iter(&hash); !iter.done(); ++iter) { |
commit-bot@chromium.org | 1f6cf69 | 2014-03-05 01:00:50 +0000 | [diff] [blame] | 194 | int key = (*iter).key; |
| 195 | keys[count] = key; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 196 | ASSERT(hash.find(key) != nullptr); |
commit-bot@chromium.org | 1f6cf69 | 2014-03-05 01:00:50 +0000 | [diff] [blame] | 197 | ++count; |
| 198 | } |
| 199 | ASSERT(3 == count); |
| 200 | ASSERT(keys[0] != keys[1]); |
| 201 | ASSERT(keys[0] != keys[2]); |
| 202 | ASSERT(keys[1] != keys[2]); |
| 203 | |
| 204 | // should see 2 unique keys when iterating over hash that aren't 1 |
| 205 | hash.remove(1); |
| 206 | count = 0; |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 207 | memset(keys, 0, sizeof(keys)); |
| 208 | for (T iter(&hash); !iter.done(); ++iter) { |
commit-bot@chromium.org | 1f6cf69 | 2014-03-05 01:00:50 +0000 | [diff] [blame] | 209 | int key = (*iter).key; |
| 210 | keys[count] = key; |
| 211 | ASSERT(key != 1); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 212 | ASSERT(hash.find(key) != nullptr); |
commit-bot@chromium.org | 1f6cf69 | 2014-03-05 01:00:50 +0000 | [diff] [blame] | 213 | ++count; |
| 214 | } |
| 215 | ASSERT(2 == count); |
| 216 | ASSERT(keys[0] != keys[1]); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 217 | } |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 218 | |
| 219 | DEF_TEST(DynamicHash_iterator, reporter) { |
| 220 | TestIter<Hash::Iter>(reporter); |
| 221 | TestIter<Hash::ConstIter>(reporter); |
| 222 | } |
| 223 | |
| 224 | static void TestResetOrRewind(skiatest::Reporter* reporter, bool testReset) { |
| 225 | Hash hash; |
| 226 | Entry a = { 1, 2.0 }; |
| 227 | Entry b = { 2, 3.0 }; |
| 228 | |
| 229 | ASSERT(hash.capacity() == 0); |
| 230 | hash.add(&a); |
| 231 | hash.add(&b); |
| 232 | ASSERT(hash.count() == 2); |
| 233 | ASSERT(hash.capacity() == 4); |
| 234 | |
| 235 | if (testReset) { |
| 236 | hash.reset(); |
| 237 | ASSERT(hash.capacity() == 0); |
| 238 | } else { |
| 239 | hash.rewind(); |
| 240 | ASSERT(hash.capacity() == 4); |
| 241 | } |
| 242 | ASSERT(hash.count() == 0); |
| 243 | |
| 244 | // make sure things still work |
| 245 | hash.add(&a); |
| 246 | hash.add(&b); |
| 247 | ASSERT(hash.count() == 2); |
| 248 | ASSERT(hash.capacity() == 4); |
| 249 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 250 | ASSERT(hash.find(1) != nullptr); |
| 251 | ASSERT(hash.find(2) != nullptr); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | DEF_TEST(DynamicHash_reset, reporter) { |
| 255 | TestResetOrRewind(reporter, true); |
| 256 | } |
| 257 | |
| 258 | DEF_TEST(DynamicHash_rewind, reporter) { |
| 259 | TestResetOrRewind(reporter, false); |
| 260 | } |