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