OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Test.h" | 8 #include "Test.h" |
9 #include "SkTDynamicHash.h" | 9 #include "SkTDynamicHash.h" |
10 #include "SkRandom.h" | |
10 | 11 |
11 namespace { | 12 namespace { |
12 | 13 |
13 struct Entry { | 14 struct Entry { |
14 int key; | 15 int key; |
15 double value; | 16 double value; |
16 }; | 17 }; |
17 | 18 |
18 const int& GetKey(const Entry& entry) { return entry.key; } | 19 const int& GetKey(const Entry& entry) { return entry.key; } |
19 uint32_t GetHash(const int& key) { return key; } | 20 uint32_t GetHash(const int& key) { return key; } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 | 133 |
133 // This will go in the same slot as 'a' did before. | 134 // This will go in the same slot as 'a' did before. |
134 ASSERT(hash.countCollisions(9) == 0); | 135 ASSERT(hash.countCollisions(9) == 0); |
135 hash.add(&c); | 136 hash.add(&c); |
136 ASSERT(hash.find(9) != NULL); | 137 ASSERT(hash.find(9) != NULL); |
137 ASSERT(hash.find(9)->value == 4.0); | 138 ASSERT(hash.find(9)->value == 4.0); |
138 ASSERT(hash.find(5) != NULL); | 139 ASSERT(hash.find(5) != NULL); |
139 ASSERT(hash.find(5)->value == 3.0); | 140 ASSERT(hash.find(5)->value == 3.0); |
140 } | 141 } |
141 | 142 |
143 static void test_validate(skiatest::Reporter* reporter) { | |
144 // Test validate after construction for different initial capacitys. | |
145 { | |
146 Entry a = { 1, 2.0 }; | |
147 Entry b = { 2, 3.0 }; | |
148 Entry c = { 9, 4.0 }; | |
149 | |
150 for (int i = 0; i < 77; ++i) { | |
151 Hash hash(i); | |
152 hash.validate(); | |
153 | |
154 hash.add(&a); | |
155 hash.validate(); | |
156 hash.add(&b); | |
157 hash.validate(); | |
158 hash.add(&c); | |
159 hash.validate(); | |
160 | |
161 ASSERT(hash.find(1) != NULL); | |
162 ASSERT(hash.find(9) != NULL); | |
163 hash.remove(1); | |
164 hash.validate(); | |
165 ASSERT(hash.find(2) == &b); | |
166 hash.remove(2); | |
167 hash.validate(); | |
168 hash.add(&a); | |
169 hash.validate(); | |
170 ASSERT(hash.find(1) == &a); | |
171 hash.remove(1); | |
172 hash.remove(9); | |
173 hash.validate(); | |
174 } | |
175 } | |
176 | |
177 { | |
178 SkRandom rand; | |
179 Entry entries[3333]; | |
mtklein
2013/12/03 19:02:02
Can you just mark 3333 and 237 as arbitrary someho
| |
180 Hash hash; | |
181 | |
182 for (size_t i = 0; i < SK_ARRAY_COUNT(entries); ++i) { | |
183 entries[i].key = rand.nextU(); | |
mtklein
2013/12/03 19:02:02
It's unlikely, but this could trigger a uniqueness
| |
184 entries[i].value = rand.nextF(); | |
185 hash.add(&entries[i]); | |
186 } | |
187 | |
188 hash.validate(); | |
189 | |
190 for (size_t i = 0; i < SK_ARRAY_COUNT(entries); i += 237) { | |
191 ASSERT(hash.find(entries[i].key) != NULL); | |
192 hash.remove(entries[i].key); | |
193 hash.validate(); | |
194 } | |
195 } | |
196 } | |
197 | |
142 static void test_dynamic_hash(skiatest::Reporter* reporter) { | 198 static void test_dynamic_hash(skiatest::Reporter* reporter) { |
143 test_growth(reporter); | 199 test_growth(reporter); |
144 test_add(reporter); | 200 test_add(reporter); |
145 test_lookup(reporter); | 201 test_lookup(reporter); |
146 test_remove(reporter); | 202 test_remove(reporter); |
203 test_validate(reporter); | |
147 } | 204 } |
148 | 205 |
149 #include "TestClassDef.h" | 206 #include "TestClassDef.h" |
150 DEFINE_TESTCLASS("DynamicHash", DynamicHashTestClass, test_dynamic_hash); | 207 DEFINE_TESTCLASS("DynamicHash", DynamicHashTestClass, test_dynamic_hash); |
OLD | NEW |