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 "SkTDynamicHash.h" | 8 #include "SkTDynamicHash.h" |
9 #include "Test.h" | 9 #include "Test.h" |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... | |
28 int countCollisions(const int& key) const { return this->INHERITED::countCol lisions(key); } | 28 int countCollisions(const int& key) const { return this->INHERITED::countCol lisions(key); } |
29 | 29 |
30 private: | 30 private: |
31 typedef SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> INHERITED; | 31 typedef SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> INHERITED; |
32 }; | 32 }; |
33 | 33 |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 #define ASSERT(x) REPORTER_ASSERT(reporter, x) | 36 #define ASSERT(x) REPORTER_ASSERT(reporter, x) |
37 | 37 |
38 static void test_growth(skiatest::Reporter* reporter) { | 38 DEF_TEST(DynamicHash_growth, reporter) { |
39 Entry a = { 1, 2.0 }; | 39 Entry a = { 1, 2.0 }; |
40 Entry b = { 2, 3.0 }; | 40 Entry b = { 2, 3.0 }; |
41 Entry c = { 3, 4.0 }; | 41 Entry c = { 3, 4.0 }; |
42 Entry d = { 4, 5.0 }; | 42 Entry d = { 4, 5.0 }; |
43 Entry e = { 5, 6.0 }; | 43 Entry e = { 5, 6.0 }; |
44 | 44 |
45 Hash hash; | 45 Hash hash; |
46 ASSERT(hash.capacity() == 0); | 46 ASSERT(hash.capacity() == 0); |
47 | 47 |
48 hash.add(&a); | 48 hash.add(&a); |
49 ASSERT(hash.capacity() == 4); | 49 ASSERT(hash.capacity() == 4); |
50 | 50 |
51 hash.add(&b); | 51 hash.add(&b); |
52 ASSERT(hash.capacity() == 4); | 52 ASSERT(hash.capacity() == 4); |
53 | 53 |
54 hash.add(&c); | 54 hash.add(&c); |
55 ASSERT(hash.capacity() == 4); | 55 ASSERT(hash.capacity() == 4); |
56 | 56 |
57 hash.add(&d); | 57 hash.add(&d); |
58 ASSERT(hash.capacity() == 8); | 58 ASSERT(hash.capacity() == 8); |
59 | 59 |
60 hash.add(&e); | 60 hash.add(&e); |
61 ASSERT(hash.capacity() == 8); | 61 ASSERT(hash.capacity() == 8); |
62 | 62 |
63 ASSERT(hash.count() == 5); | 63 ASSERT(hash.count() == 5); |
64 } | 64 } |
65 | 65 |
66 static void test_add(skiatest::Reporter* reporter) { | 66 DEF_TEST(DynamicHash_add, reporter) { |
67 Hash hash; | 67 Hash hash; |
68 Entry a = { 1, 2.0 }; | 68 Entry a = { 1, 2.0 }; |
69 Entry b = { 2, 3.0 }; | 69 Entry b = { 2, 3.0 }; |
70 | 70 |
71 ASSERT(hash.count() == 0); | 71 ASSERT(hash.count() == 0); |
72 hash.add(&a); | 72 hash.add(&a); |
73 ASSERT(hash.count() == 1); | 73 ASSERT(hash.count() == 1); |
74 hash.add(&b); | 74 hash.add(&b); |
75 ASSERT(hash.count() == 2); | 75 ASSERT(hash.count() == 2); |
76 } | 76 } |
77 | 77 |
78 static void test_lookup(skiatest::Reporter* reporter) { | 78 DEF_TEST(DynamicHash_lookup, reporter) { |
79 Hash hash; | 79 Hash hash; |
80 | 80 |
81 // These collide. | 81 // These collide. |
82 Entry a = { 1, 2.0 }; | 82 Entry a = { 1, 2.0 }; |
83 Entry b = { 5, 3.0 }; | 83 Entry b = { 5, 3.0 }; |
84 | 84 |
85 // Before we insert anything, nothing can collide. | 85 // Before we insert anything, nothing can collide. |
86 ASSERT(hash.countCollisions(1) == 0); | 86 ASSERT(hash.countCollisions(1) == 0); |
87 ASSERT(hash.countCollisions(5) == 0); | 87 ASSERT(hash.countCollisions(5) == 0); |
88 ASSERT(hash.countCollisions(9) == 0); | 88 ASSERT(hash.countCollisions(9) == 0); |
(...skipping 14 matching lines...) Expand all Loading... | |
103 ASSERT(hash.find(1) != NULL); | 103 ASSERT(hash.find(1) != NULL); |
104 ASSERT(hash.find(1)->value == 2.0); | 104 ASSERT(hash.find(1)->value == 2.0); |
105 ASSERT(hash.find(5) != NULL); | 105 ASSERT(hash.find(5) != NULL); |
106 ASSERT(hash.find(5)->value == 3.0); | 106 ASSERT(hash.find(5)->value == 3.0); |
107 | 107 |
108 // These aren't in the hash. | 108 // These aren't in the hash. |
109 ASSERT(hash.find(2) == NULL); | 109 ASSERT(hash.find(2) == NULL); |
110 ASSERT(hash.find(9) == NULL); | 110 ASSERT(hash.find(9) == NULL); |
111 } | 111 } |
112 | 112 |
113 static void test_remove(skiatest::Reporter* reporter) { | 113 DEF_TEST(DynamicHash_remove, reporter) { |
114 Hash hash; | 114 Hash hash; |
115 | 115 |
116 // These collide. | 116 // These collide. |
117 Entry a = { 1, 2.0 }; | 117 Entry a = { 1, 2.0 }; |
118 Entry b = { 5, 3.0 }; | 118 Entry b = { 5, 3.0 }; |
119 Entry c = { 9, 4.0 }; | 119 Entry c = { 9, 4.0 }; |
120 | 120 |
121 hash.add(&a); | 121 hash.add(&a); |
122 hash.add(&b); | 122 hash.add(&b); |
123 hash.remove(1); | 123 hash.remove(1); |
124 // a should be marked deleted, and b should still be findable. | 124 // a should be marked deleted, and b should still be findable. |
125 | 125 |
126 ASSERT(hash.find(1) == NULL); | 126 ASSERT(hash.find(1) == NULL); |
127 ASSERT(hash.find(5) != NULL); | 127 ASSERT(hash.find(5) != NULL); |
128 ASSERT(hash.find(5)->value == 3.0); | 128 ASSERT(hash.find(5)->value == 3.0); |
129 | 129 |
130 // This will go in the same slot as 'a' did before. | 130 // This will go in the same slot as 'a' did before. |
131 ASSERT(hash.countCollisions(9) == 0); | 131 ASSERT(hash.countCollisions(9) == 0); |
132 hash.add(&c); | 132 hash.add(&c); |
133 ASSERT(hash.find(9) != NULL); | 133 ASSERT(hash.find(9) != NULL); |
134 ASSERT(hash.find(9)->value == 4.0); | 134 ASSERT(hash.find(9)->value == 4.0); |
135 ASSERT(hash.find(5) != NULL); | 135 ASSERT(hash.find(5) != NULL); |
136 ASSERT(hash.find(5)->value == 3.0); | 136 ASSERT(hash.find(5)->value == 3.0); |
137 } | 137 } |
138 | 138 |
139 DEF_TEST(DynamicHash, reporter) { | 139 DEF_TEST(DynamicHash_iterator, reporter) { |
140 test_growth(reporter); | 140 Hash hash; |
141 test_add(reporter); | 141 |
142 test_lookup(reporter); | 142 int count = 0; |
143 test_remove(reporter); | 143 // this should fall out of loop immediately |
144 for (Hash::Iter iter(&hash); !iter.done(); ++iter) { | |
egdaniel
2014/03/04 23:56:34
Part of me feels like this done() function name is
mtklein
2014/03/05 00:13:05
Yeah, I feel you. I think .done() is actually the
| |
145 ++count; | |
146 } | |
147 ASSERT(0 == count); | |
148 | |
149 // These collide. | |
150 Entry a = { 1, 2.0 }; | |
151 Entry b = { 5, 3.0 }; | |
152 Entry c = { 9, 4.0 }; | |
153 | |
154 hash.add(&a); | |
155 hash.add(&b); | |
156 hash.add(&c); | |
157 | |
158 // should see all 3 unique keys when iterating over hash | |
159 count = 0; | |
160 int keys[3] = {0, 0, 0}; | |
161 for (Hash::Iter iter(&hash); !iter.done(); ++iter) { | |
162 int key = (*iter).key; | |
163 keys[count] = key; | |
164 ASSERT(hash.find(key) != NULL); | |
165 ++count; | |
166 } | |
167 ASSERT(3 == count); | |
168 ASSERT(keys[0] != keys[1]); | |
169 ASSERT(keys[0] != keys[2]); | |
170 ASSERT(keys[1] != keys[2]); | |
171 | |
172 // should see 2 unique keys when iterating over hash that aren't 1 | |
173 hash.remove(1); | |
174 count = 0; | |
175 memset(keys,0,sizeof(keys)); | |
176 for (Hash::Iter iter(&hash); !iter.done(); ++iter) { | |
177 int key = (*iter).key; | |
178 keys[count] = key; | |
179 ASSERT(key != 1); | |
180 ASSERT(hash.find(key) != NULL); | |
181 ++count; | |
182 } | |
183 ASSERT(2 == count); | |
184 ASSERT(keys[0] != keys[1]); | |
144 } | 185 } |
186 | |
OLD | NEW |