| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/templates/a_hash_map.h" |
| 34 |
|
| 35 |
#include "tests/test_macros.h" |
| 36 |
|
| 37 |
namespace TestAHashMap { |
| 38 |
|
| 39 |
TEST_CASE("[AHashMap] List initialization") { |
| 40 |
AHashMap<int, String> map{ { 0, "A" }, { 1, "B" }, { 2, "C" }, { 3, "D" }, { 4, "E" } }; |
| 41 |
|
| 42 |
CHECK(map.size() == 5); |
| 43 |
CHECK(map[0] == "A"); |
| 44 |
CHECK(map[1] == "B"); |
| 45 |
CHECK(map[2] == "C"); |
| 46 |
CHECK(map[3] == "D"); |
| 47 |
CHECK(map[4] == "E"); |
| 48 |
} |
| 49 |
|
| 50 |
TEST_CASE("[AHashMap] List initialization with existing elements") { |
| 51 |
AHashMap<int, String> map{ { 0, "A" }, { 0, "B" }, { 0, "C" }, { 0, "D" }, { 0, "E" } }; |
| 52 |
|
| 53 |
CHECK(map.size() == 1); |
| 54 |
CHECK(map[0] == "E"); |
| 55 |
} |
| 56 |
|
| 57 |
TEST_CASE("[AHashMap] Insert element") { |
| 58 |
AHashMap<int, int> map; |
| 59 |
AHashMap<int, int>::Iterator e = map.insert(42, 84); |
| 60 |
|
| 61 |
CHECK(e); |
| 62 |
CHECK(e->key == 42); |
| 63 |
CHECK(e->value == 84); |
| 64 |
CHECK(map[42] == 84); |
| 65 |
CHECK(map.has(42)); |
| 66 |
CHECK(map.find(42)); |
| 67 |
} |
| 68 |
|
| 69 |
TEST_CASE("[AHashMap] Overwrite element") { |
| 70 |
AHashMap<int, int> map; |
| 71 |
map.insert(42, 84); |
| 72 |
map.insert(42, 1234); |
| 73 |
|
| 74 |
CHECK(map[42] == 1234); |
| 75 |
} |
| 76 |
|
| 77 |
TEST_CASE("[AHashMap] Erase via element") { |
| 78 |
AHashMap<int, int> map; |
| 79 |
AHashMap<int, int>::Iterator e = map.insert(42, 84); |
| 80 |
map.remove(e); |
| 81 |
CHECK(!map.has(42)); |
| 82 |
CHECK(!map.find(42)); |
| 83 |
} |
| 84 |
|
| 85 |
TEST_CASE("[AHashMap] Erase via key") { |
| 86 |
AHashMap<int, int> map; |
| 87 |
map.insert(42, 84); |
| 88 |
map.erase(42); |
| 89 |
CHECK(!map.has(42)); |
| 90 |
CHECK(!map.find(42)); |
| 91 |
} |
| 92 |
|
| 93 |
TEST_CASE("[AHashMap] Size") { |
| 94 |
AHashMap<int, int> map; |
| 95 |
map.insert(42, 84); |
| 96 |
map.insert(123, 84); |
| 97 |
map.insert(123, 84); |
| 98 |
map.insert(0, 84); |
| 99 |
map.insert(123485, 84); |
| 100 |
|
| 101 |
CHECK(map.size() == 4); |
| 102 |
} |
| 103 |
|
| 104 |
TEST_CASE("[AHashMap] Iteration") { |
| 105 |
AHashMap<int, int> map; |
| 106 |
|
| 107 |
map.insert(42, 84); |
| 108 |
map.insert(123, 12385); |
| 109 |
map.insert(0, 12934); |
| 110 |
map.insert(123485, 1238888); |
| 111 |
map.insert(123, 111111); |
| 112 |
|
| 113 |
Vector<Pair<int, int>> expected; |
| 114 |
expected.push_back(Pair<int, int>(42, 84)); |
| 115 |
expected.push_back(Pair<int, int>(123, 111111)); |
| 116 |
expected.push_back(Pair<int, int>(0, 12934)); |
| 117 |
expected.push_back(Pair<int, int>(123485, 1238888)); |
| 118 |
|
| 119 |
int idx = 0; |
| 120 |
for (const KeyValue<int, int> &E : map) { |
| 121 |
CHECK(expected[idx] == Pair<int, int>(E.key, E.value)); |
| 122 |
idx++; |
| 123 |
} |
| 124 |
|
| 125 |
idx--; |
| 126 |
for (AHashMap<int, int>::Iterator it = map.last(); it; --it) { |
| 127 |
CHECK(expected[idx] == Pair<int, int>(it->key, it->value)); |
| 128 |
idx--; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
TEST_CASE("[AHashMap] Const iteration") { |
| 133 |
AHashMap<int, int> map; |
| 134 |
map.insert(42, 84); |
| 135 |
map.insert(123, 12385); |
| 136 |
map.insert(0, 12934); |
| 137 |
map.insert(123485, 1238888); |
| 138 |
map.insert(123, 111111); |
| 139 |
|
| 140 |
const AHashMap<int, int> const_map = map; |
| 141 |
|
| 142 |
Vector<Pair<int, int>> expected; |
| 143 |
expected.push_back(Pair<int, int>(42, 84)); |
| 144 |
expected.push_back(Pair<int, int>(123, 111111)); |
| 145 |
expected.push_back(Pair<int, int>(0, 12934)); |
| 146 |
expected.push_back(Pair<int, int>(123485, 1238888)); |
| 147 |
expected.push_back(Pair<int, int>(123, 111111)); |
| 148 |
|
| 149 |
int idx = 0; |
| 150 |
for (const KeyValue<int, int> &E : const_map) { |
| 151 |
CHECK(expected[idx] == Pair<int, int>(E.key, E.value)); |
| 152 |
idx++; |
| 153 |
} |
| 154 |
|
| 155 |
idx--; |
| 156 |
for (AHashMap<int, int>::ConstIterator it = const_map.last(); it; --it) { |
| 157 |
CHECK(expected[idx] == Pair<int, int>(it->key, it->value)); |
| 158 |
idx--; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
TEST_CASE("[AHashMap] Replace key") { |
| 163 |
AHashMap<int, int> map; |
| 164 |
map.insert(42, 84); |
| 165 |
map.insert(0, 12934); |
| 166 |
CHECK(map.replace_key(0, 1)); |
| 167 |
CHECK(map.has(1)); |
| 168 |
CHECK(map[1] == 12934); |
| 169 |
} |
| 170 |
|
| 171 |
TEST_CASE("[AHashMap] Clear") { |
| 172 |
AHashMap<int, int> map; |
| 173 |
map.insert(42, 84); |
| 174 |
map.insert(123, 12385); |
| 175 |
map.insert(0, 12934); |
| 176 |
|
| 177 |
map.clear(); |
| 178 |
CHECK(!map.has(42)); |
| 179 |
CHECK(map.size() == 0); |
| 180 |
CHECK(map.is_empty()); |
| 181 |
} |
| 182 |
|
| 183 |
TEST_CASE("[AHashMap] Get") { |
| 184 |
AHashMap<int, int> map; |
| 185 |
map.insert(42, 84); |
| 186 |
map.insert(123, 12385); |
| 187 |
map.insert(0, 12934); |
| 188 |
|
| 189 |
CHECK(map.get(123) == 12385); |
| 190 |
map.get(123) = 10; |
| 191 |
CHECK(map.get(123) == 10); |
| 192 |
|
| 193 |
CHECK(*map.getptr(0) == 12934); |
| 194 |
*map.getptr(0) = 1; |
| 195 |
CHECK(*map.getptr(0) == 1); |
| 196 |
|
| 197 |
CHECK(map.get(42) == 84); |
| 198 |
CHECK(map.getptr(-10) == nullptr); |
| 199 |
} |
| 200 |
|
| 201 |
TEST_CASE("[AHashMap] Insert, iterate and remove many elements") { |
| 202 |
const int elem_max = 1234; |
| 203 |
AHashMap<int, int> map; |
| 204 |
for (int i = 0; i < elem_max; i++) { |
| 205 |
map.insert(i, i); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
int idx = 0; |
| 210 |
for (const KeyValue<int, int> &K : map) { |
| 211 |
CHECK(idx == K.key); |
| 212 |
CHECK(idx == K.value); |
| 213 |
CHECK(map.has(idx)); |
| 214 |
idx++; |
| 215 |
} |
| 216 |
|
| 217 |
Vector<int> elems_still_valid; |
| 218 |
|
| 219 |
for (int i = 0; i < elem_max; i++) { |
| 220 |
if ((i % 5) == 0) { |
| 221 |
map.erase(i); |
| 222 |
} else { |
| 223 |
elems_still_valid.push_back(i); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
CHECK(elems_still_valid.size() == map.size()); |
| 228 |
|
| 229 |
for (int i = 0; i < elems_still_valid.size(); i++) { |
| 230 |
CHECK(map.has(elems_still_valid[i])); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
TEST_CASE("[AHashMap] Insert, iterate and remove many strings") { |
| 235 |
const int elem_max = 432; |
| 236 |
AHashMap<String, String> map; |
| 237 |
|
| 238 |
|
| 239 |
ERR_PRINT_OFF; |
| 240 |
for (int i = 0; i < elem_max; i++) { |
| 241 |
map.insert(itos(i), itos(i)); |
| 242 |
} |
| 243 |
ERR_PRINT_ON; |
| 244 |
|
| 245 |
|
| 246 |
int idx = 0; |
| 247 |
for (auto &K : map) { |
| 248 |
CHECK(itos(idx) == K.key); |
| 249 |
CHECK(itos(idx) == K.value); |
| 250 |
CHECK(map.has(itos(idx))); |
| 251 |
idx++; |
| 252 |
} |
| 253 |
|
| 254 |
Vector<String> elems_still_valid; |
| 255 |
|
| 256 |
for (int i = 0; i < elem_max; i++) { |
| 257 |
if ((i % 5) == 0) { |
| 258 |
map.erase(itos(i)); |
| 259 |
} else { |
| 260 |
elems_still_valid.push_back(itos(i)); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
CHECK(elems_still_valid.size() == map.size()); |
| 265 |
|
| 266 |
for (int i = 0; i < elems_still_valid.size(); i++) { |
| 267 |
CHECK(map.has(elems_still_valid[i])); |
| 268 |
} |
| 269 |
|
| 270 |
elems_still_valid.clear(); |
| 271 |
} |
| 272 |
|
| 273 |
TEST_CASE("[AHashMap] Copy constructor") { |
| 274 |
AHashMap<int, int> map0; |
| 275 |
const uint32_t count = 5; |
| 276 |
for (uint32_t i = 0; i < count; i++) { |
| 277 |
map0.insert(i, i); |
| 278 |
} |
| 279 |
AHashMap<int, int> map1(map0); |
| 280 |
CHECK(map0.size() == map1.size()); |
| 281 |
CHECK(map0.get_capacity() == map1.get_capacity()); |
| 282 |
CHECK(*map0.getptr(0) == *map1.getptr(0)); |
| 283 |
} |
| 284 |
|
| 285 |
TEST_CASE("[AHashMap] Operator =") { |
| 286 |
AHashMap<int, int> map0; |
| 287 |
AHashMap<int, int> map1; |
| 288 |
const uint32_t count = 5; |
| 289 |
map1.insert(1234, 1234); |
| 290 |
for (uint32_t i = 0; i < count; i++) { |
| 291 |
map0.insert(i, i); |
| 292 |
} |
| 293 |
map1 = map0; |
| 294 |
CHECK(map0.size() == map1.size()); |
| 295 |
CHECK(map0.get_capacity() == map1.get_capacity()); |
| 296 |
CHECK(*map0.getptr(0) == *map1.getptr(0)); |
| 297 |
} |
| 298 |
|
| 299 |
TEST_CASE("[AHashMap] Array methods") { |
| 300 |
AHashMap<int, int> map; |
| 301 |
for (int i = 0; i < 100; i++) { |
| 302 |
map.insert(100 - i, i); |
| 303 |
} |
| 304 |
for (int i = 0; i < 100; i++) { |
| 305 |
CHECK(map.get_by_index(i).value == i); |
| 306 |
} |
| 307 |
int index = map.get_index(1); |
| 308 |
CHECK(map.get_by_index(index).value == 99); |
| 309 |
CHECK(map.erase_by_index(index)); |
| 310 |
CHECK(!map.erase_by_index(index)); |
| 311 |
CHECK(map.get_index(1) == -1); |
| 312 |
} |
| 313 |
|
| 314 |
} |