OLD | NEW |
1 #include "Test.h" | 1 #include "Test.h" |
2 #include "SkTDynamicHash.h" | 2 #include "SkTDynamicHash.h" |
3 | 3 |
4 namespace { | 4 namespace { |
5 | 5 |
6 struct Entry { | 6 struct Entry { |
7 int key; | 7 int key; |
8 double value; | 8 double value; |
9 }; | 9 }; |
10 const int& GetKey(const Entry& entry) { return entry.key; } | 10 const int& GetKey(const Entry& entry) { return entry.key; } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 ASSERT(hash.find(1) != NULL); | 106 ASSERT(hash.find(1) != NULL); |
107 ASSERT(hash.find(1)->value == 2.0); | 107 ASSERT(hash.find(1)->value == 2.0); |
108 ASSERT(hash.find(5) != NULL); | 108 ASSERT(hash.find(5) != NULL); |
109 ASSERT(hash.find(5)->value == 3.0); | 109 ASSERT(hash.find(5)->value == 3.0); |
110 | 110 |
111 // These aren't in the hash. | 111 // These aren't in the hash. |
112 ASSERT(hash.find(2) == NULL); | 112 ASSERT(hash.find(2) == NULL); |
113 ASSERT(hash.find(9) == NULL); | 113 ASSERT(hash.find(9) == NULL); |
114 } | 114 } |
115 | 115 |
116 static void test_remove(skiatest::Reporter* reporter) { | 116 static void test_easy_remove(skiatest::Reporter* reporter) { |
117 Hash hash(4); | 117 Hash hash(4); |
118 ASSERT(hash.capacity() == 4); | 118 ASSERT(hash.capacity() == 4); |
119 | 119 |
120 // These collide. | 120 // These collide. |
121 Entry a = { 1, 2.0 }; | 121 Entry a = { 1, 2.0 }; |
122 Entry b = { 5, 3.0 }; | 122 Entry b = { 5, 3.0 }; |
123 Entry c = { 9, 4.0 }; | |
124 | 123 |
125 hash.add(&a); | 124 hash.add(&a); |
| 125 ASSERT(hash.count() == 1); |
| 126 ASSERT(hash.countCollisions(1) == 0); |
| 127 |
126 hash.add(&b); | 128 hash.add(&b); |
127 hash.remove(1); | 129 ASSERT(hash.count() == 2); |
128 // a should be marked deleted, and b should still be findable. | 130 ASSERT(hash.countCollisions(1) == 0); |
| 131 ASSERT(hash.countCollisions(5) == 1); |
129 | 132 |
130 ASSERT(hash.find(1) == NULL); | 133 hash.remove(5); |
131 ASSERT(hash.find(5) != NULL); | 134 ASSERT(hash.count() == 1); |
132 ASSERT(hash.find(5)->value == 3.0); | 135 ASSERT(hash.countCollisions(1) == 0); |
| 136 } |
133 | 137 |
134 // This will go in the same slot as 'a' did before. | 138 static void test_interesting_remove(skiatest::Reporter* reporter) { |
135 ASSERT(hash.countCollisions(9) == 0); | 139 Hash hash(8); |
| 140 ASSERT(hash.capacity() == 8); |
| 141 |
| 142 // These collide. |
| 143 Entry a = { 1, 2.0 }; |
| 144 Entry b = { 9, 3.0 }; |
| 145 Entry c = { 17, 4.0 }; |
| 146 Entry d = { 25, 5.0 }; |
| 147 |
| 148 // This will trigger interesting behavior in remove. |
| 149 hash.add(&a); |
| 150 hash.add(&b); |
136 hash.add(&c); | 151 hash.add(&c); |
137 ASSERT(hash.find(9) != NULL); | 152 hash.remove(9); |
138 ASSERT(hash.find(9)->value == 4.0); | 153 |
139 ASSERT(hash.find(5) != NULL); | 154 // a shouldn't have moved. |
140 ASSERT(hash.find(5)->value == 3.0); | 155 ASSERT(hash.find(1) != NULL); |
| 156 ASSERT(hash.find(1)->value == 2.0); |
| 157 ASSERT(hash.countCollisions(1) == 0); |
| 158 // c should have moved into b's position. |
| 159 ASSERT(hash.find(17) != NULL); |
| 160 ASSERT(hash.find(17)->value == 4.0); |
| 161 ASSERT(hash.countCollisions(17) == 1); |
| 162 |
| 163 hash.add(&d); |
| 164 // a shouldn't have moved. |
| 165 ASSERT(hash.find(1) != NULL); |
| 166 ASSERT(hash.find(1)->value == 2.0); |
| 167 ASSERT(hash.countCollisions(1) == 0); |
| 168 // c shouldn't have moved. |
| 169 ASSERT(hash.find(17) != NULL); |
| 170 ASSERT(hash.find(17)->value == 4.0); |
| 171 ASSERT(hash.countCollisions(17) == 1); |
| 172 // d's after both of them. |
| 173 ASSERT(hash.find(25) != NULL); |
| 174 ASSERT(hash.find(25)->value == 5.0); |
| 175 ASSERT(hash.countCollisions(25) == 2); |
141 } | 176 } |
142 | 177 |
143 static void test_dynamic_hash(skiatest::Reporter* reporter) { | 178 static void test_dynamic_hash(skiatest::Reporter* reporter) { |
144 test_growth(reporter); | 179 test_growth(reporter); |
145 test_add(reporter); | 180 test_add(reporter); |
146 test_lookup(reporter); | 181 test_lookup(reporter); |
147 test_remove(reporter); | 182 test_easy_remove(reporter); |
| 183 test_interesting_remove(reporter); |
148 } | 184 } |
149 | 185 |
150 #include "TestClassDef.h" | 186 #include "TestClassDef.h" |
151 DEFINE_TESTCLASS("DynamicHash", DynamicHashTestClass, test_dynamic_hash); | 187 DEFINE_TESTCLASS("DynamicHash", DynamicHashTestClass, test_dynamic_hash); |
OLD | NEW |