2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkTDynamicHash.h"
17 static const int& GetKey(const Entry& entry) { return entry.key; }
18 static uint32_t Hash(const int& key) { return key; }
22 class Hash : public SkTDynamicHash<Entry, int> {
24 Hash() : INHERITED() {}
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); }
31 typedef SkTDynamicHash<Entry, int> INHERITED;
36 #define ASSERT(x) REPORTER_ASSERT(reporter, x)
38 DEF_TEST(DynamicHash_growth, reporter) {
46 ASSERT(hash.capacity() == 0);
49 ASSERT(hash.capacity() == 4);
52 ASSERT(hash.capacity() == 4);
55 ASSERT(hash.capacity() == 4);
58 ASSERT(hash.capacity() == 8);
61 ASSERT(hash.capacity() == 8);
63 ASSERT(hash.count() == 5);
66 DEF_TEST(DynamicHash_add, reporter) {
71 ASSERT(hash.count() == 0);
73 ASSERT(hash.count() == 1);
75 ASSERT(hash.count() == 2);
78 DEF_TEST(DynamicHash_lookup, reporter) {
85 // Before we insert anything, nothing can collide.
86 ASSERT(hash.countCollisions(1) == 0);
87 ASSERT(hash.countCollisions(5) == 0);
88 ASSERT(hash.countCollisions(9) == 0);
92 ASSERT(hash.countCollisions(1) == 0);
93 ASSERT(hash.countCollisions(5) == 1);
94 ASSERT(hash.countCollisions(9) == 1);
96 // Second is one step away.
98 ASSERT(hash.countCollisions(1) == 0);
99 ASSERT(hash.countCollisions(5) == 1);
100 ASSERT(hash.countCollisions(9) == 2);
102 // We can find our data right?
103 ASSERT(hash.find(1) != NULL);
104 ASSERT(hash.find(1)->value == 2.0);
105 ASSERT(hash.find(5) != NULL);
106 ASSERT(hash.find(5)->value == 3.0);
108 // These aren't in the hash.
109 ASSERT(hash.find(2) == NULL);
110 ASSERT(hash.find(9) == NULL);
113 DEF_TEST(DynamicHash_remove, reporter) {
117 Entry a = { 1, 2.0 };
118 Entry b = { 5, 3.0 };
119 Entry c = { 9, 4.0 };
124 // a should be marked deleted, and b should still be findable.
126 ASSERT(hash.find(1) == NULL);
127 ASSERT(hash.find(5) != NULL);
128 ASSERT(hash.find(5)->value == 3.0);
130 // This will go in the same slot as 'a' did before.
131 ASSERT(hash.countCollisions(9) == 0);
133 ASSERT(hash.find(9) != NULL);
134 ASSERT(hash.find(9)->value == 4.0);
135 ASSERT(hash.find(5) != NULL);
136 ASSERT(hash.find(5)->value == 3.0);
139 template<typename T> static void TestIter(skiatest::Reporter* reporter) {
143 // this should fall out of loop immediately
144 for (T iter(&hash); !iter.done(); ++iter) {
150 Entry a = { 1, 2.0 };
151 Entry b = { 5, 3.0 };
152 Entry c = { 9, 4.0 };
158 // should see all 3 unique keys when iterating over hash
160 int keys[3] = {0, 0, 0};
161 for (T iter(&hash); !iter.done(); ++iter) {
162 int key = (*iter).key;
164 ASSERT(hash.find(key) != NULL);
168 ASSERT(keys[0] != keys[1]);
169 ASSERT(keys[0] != keys[2]);
170 ASSERT(keys[1] != keys[2]);
172 // should see 2 unique keys when iterating over hash that aren't 1
175 memset(keys, 0, sizeof(keys));
176 for (T iter(&hash); !iter.done(); ++iter) {
177 int key = (*iter).key;
180 ASSERT(hash.find(key) != NULL);
184 ASSERT(keys[0] != keys[1]);
187 DEF_TEST(DynamicHash_iterator, reporter) {
188 TestIter<Hash::Iter>(reporter);
189 TestIter<Hash::ConstIter>(reporter);
192 static void TestResetOrRewind(skiatest::Reporter* reporter, bool testReset) {
194 Entry a = { 1, 2.0 };
195 Entry b = { 2, 3.0 };
197 ASSERT(hash.capacity() == 0);
200 ASSERT(hash.count() == 2);
201 ASSERT(hash.capacity() == 4);
205 ASSERT(hash.capacity() == 0);
208 ASSERT(hash.capacity() == 4);
210 ASSERT(hash.count() == 0);
212 // make sure things still work
215 ASSERT(hash.count() == 2);
216 ASSERT(hash.capacity() == 4);
218 ASSERT(hash.find(1) != NULL);
219 ASSERT(hash.find(2) != NULL);
222 DEF_TEST(DynamicHash_reset, reporter) {
223 TestResetOrRewind(reporter, true);
226 DEF_TEST(DynamicHash_rewind, reporter) {
227 TestResetOrRewind(reporter, false);