blob: a857506fd0996c696cb081e39499cd9cbc93b6b2 [file] [log] [blame]
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +00001/*
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.orgf916f9e2013-08-05 22:31:20 +00008#include "SkTDynamicHash.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "Test.h"
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000010
11namespace {
12
13struct Entry {
14 int key;
mtklein@google.com0d9f5f72013-08-05 23:08:19 +000015 double value;
commit-bot@chromium.org55bd9402014-04-02 19:17:00 +000016
17 static const int& GetKey(const Entry& entry) { return entry.key; }
18 static uint32_t Hash(const int& key) { return key; }
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000019};
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +000020
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000021
commit-bot@chromium.org55bd9402014-04-02 19:17:00 +000022class Hash : public SkTDynamicHash<Entry, int> {
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000023public:
24 Hash() : INHERITED() {}
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000025
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
30private:
commit-bot@chromium.org55bd9402014-04-02 19:17:00 +000031 typedef SkTDynamicHash<Entry, int> INHERITED;
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000032};
33
34} // namespace
35
36#define ASSERT(x) REPORTER_ASSERT(reporter, x)
37
commit-bot@chromium.org1f6cf692014-03-05 01:00:50 +000038DEF_TEST(DynamicHash_growth, reporter) {
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000039 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.org7a4b9fa2014-01-13 18:28:14 +000045 Hash hash;
46 ASSERT(hash.capacity() == 0);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000047
48 hash.add(&a);
mtklein@google.comc9ab2b72013-08-12 14:51:25 +000049 ASSERT(hash.capacity() == 4);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000050
51 hash.add(&b);
52 ASSERT(hash.capacity() == 4);
53
54 hash.add(&c);
mtklein@google.comc9ab2b72013-08-12 14:51:25 +000055 ASSERT(hash.capacity() == 4);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000056
57 hash.add(&d);
58 ASSERT(hash.capacity() == 8);
59
60 hash.add(&e);
mtklein@google.comc9ab2b72013-08-12 14:51:25 +000061 ASSERT(hash.capacity() == 8);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000062
63 ASSERT(hash.count() == 5);
64}
65
Florin Malitac274f8f2018-04-18 16:57:32 -040066DEF_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.org1f6cf692014-03-05 01:00:50 +000098DEF_TEST(DynamicHash_add, reporter) {
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000099 Hash hash;
100 Entry a = { 1, 2.0 };
101 Entry b = { 2, 3.0 };
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000102
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.orgf916f9e2013-08-05 22:31:20 +0000108}
109
commit-bot@chromium.org1f6cf692014-03-05 01:00:50 +0000110DEF_TEST(DynamicHash_lookup, reporter) {
commit-bot@chromium.org7a4b9fa2014-01-13 18:28:14 +0000111 Hash hash;
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000112
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?
halcanary96fcdcc2015-08-27 07:41:13 -0700135 ASSERT(hash.find(1) != nullptr);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000136 ASSERT(hash.find(1)->value == 2.0);
halcanary96fcdcc2015-08-27 07:41:13 -0700137 ASSERT(hash.find(5) != nullptr);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000138 ASSERT(hash.find(5)->value == 3.0);
139
140 // These aren't in the hash.
halcanary96fcdcc2015-08-27 07:41:13 -0700141 ASSERT(hash.find(2) == nullptr);
142 ASSERT(hash.find(9) == nullptr);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000143}
144
commit-bot@chromium.org1f6cf692014-03-05 01:00:50 +0000145DEF_TEST(DynamicHash_remove, reporter) {
commit-bot@chromium.org7a4b9fa2014-01-13 18:28:14 +0000146 Hash hash;
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000147
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
halcanary96fcdcc2015-08-27 07:41:13 -0700158 ASSERT(hash.find(1) == nullptr);
159 ASSERT(hash.find(5) != nullptr);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000160 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);
halcanary96fcdcc2015-08-27 07:41:13 -0700165 ASSERT(hash.find(9) != nullptr);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000166 ASSERT(hash.find(9)->value == 4.0);
halcanary96fcdcc2015-08-27 07:41:13 -0700167 ASSERT(hash.find(5) != nullptr);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000168 ASSERT(hash.find(5)->value == 3.0);
169}
170
robertphillips3d533ac2014-07-20 09:40:00 -0700171template<typename T> static void TestIter(skiatest::Reporter* reporter) {
commit-bot@chromium.org1f6cf692014-03-05 01:00:50 +0000172 Hash hash;
173
174 int count = 0;
175 // this should fall out of loop immediately
robertphillips3d533ac2014-07-20 09:40:00 -0700176 for (T iter(&hash); !iter.done(); ++iter) {
commit-bot@chromium.org1f6cf692014-03-05 01:00:50 +0000177 ++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};
robertphillips3d533ac2014-07-20 09:40:00 -0700193 for (T iter(&hash); !iter.done(); ++iter) {
commit-bot@chromium.org1f6cf692014-03-05 01:00:50 +0000194 int key = (*iter).key;
195 keys[count] = key;
halcanary96fcdcc2015-08-27 07:41:13 -0700196 ASSERT(hash.find(key) != nullptr);
commit-bot@chromium.org1f6cf692014-03-05 01:00:50 +0000197 ++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;
robertphillips3d533ac2014-07-20 09:40:00 -0700207 memset(keys, 0, sizeof(keys));
208 for (T iter(&hash); !iter.done(); ++iter) {
commit-bot@chromium.org1f6cf692014-03-05 01:00:50 +0000209 int key = (*iter).key;
210 keys[count] = key;
211 ASSERT(key != 1);
halcanary96fcdcc2015-08-27 07:41:13 -0700212 ASSERT(hash.find(key) != nullptr);
commit-bot@chromium.org1f6cf692014-03-05 01:00:50 +0000213 ++count;
214 }
215 ASSERT(2 == count);
216 ASSERT(keys[0] != keys[1]);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000217}
robertphillips3d533ac2014-07-20 09:40:00 -0700218
219DEF_TEST(DynamicHash_iterator, reporter) {
220 TestIter<Hash::Iter>(reporter);
221 TestIter<Hash::ConstIter>(reporter);
222}
223
224static 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
halcanary96fcdcc2015-08-27 07:41:13 -0700250 ASSERT(hash.find(1) != nullptr);
251 ASSERT(hash.find(2) != nullptr);
robertphillips3d533ac2014-07-20 09:40:00 -0700252}
253
254DEF_TEST(DynamicHash_reset, reporter) {
255 TestResetOrRewind(reporter, true);
256}
257
258DEF_TEST(DynamicHash_rewind, reporter) {
259 TestResetOrRewind(reporter, false);
260}