tests/test_map.cpp

Tue, 18 Apr 2023 19:15:50 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 18 Apr 2023 19:15:50 +0200
changeset 687
612ed521b1c5
parent 686
64919f63f059
child 690
2c2304622981
permissions
-rw-r--r--

tweak rehash test to achieve missing coverage

universe@556 1 /*
universe@556 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@556 3 *
universe@556 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@556 5 *
universe@556 6 * Redistribution and use in source and binary forms, with or without
universe@556 7 * modification, are permitted provided that the following conditions are met:
universe@556 8 *
universe@556 9 * 1. Redistributions of source code must retain the above copyright
universe@556 10 * notice, this list of conditions and the following disclaimer.
universe@556 11 *
universe@556 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@556 13 * notice, this list of conditions and the following disclaimer in the
universe@556 14 * documentation and/or other materials provided with the distribution.
universe@556 15 *
universe@556 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@556 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@556 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@556 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@556 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@556 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@556 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@556 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@556 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@556 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@556 26 * POSSIBILITY OF SUCH DAMAGE.
universe@556 27 */
universe@556 28
universe@556 29 #include "cx/hash_map.h"
universe@556 30 #include "cx/utils.h"
universe@658 31 #include "cx/string.h"
universe@556 32 #include "util_allocator.h"
universe@556 33
universe@556 34 #include <gtest/gtest.h>
universe@556 35 #include <unordered_map>
universe@556 36 #include <unordered_set>
universe@556 37
universe@556 38 struct map_operation {
universe@556 39 enum {
universe@556 40 put, rm
universe@556 41 } op;
universe@556 42 char const *key;
universe@556 43 char const *value;
universe@556 44 };
universe@556 45
universe@556 46 auto generate_map_operations() -> std::vector<map_operation> {
universe@556 47 return {
universe@556 48 {map_operation::put, "key 1", "test"},
universe@556 49 {map_operation::put, "key 2", "blub"},
universe@556 50 {map_operation::put, "key 3", "hallo"},
universe@556 51 {map_operation::put, "key 2", "foobar"},
universe@556 52 {map_operation::put, "key 4", "value 4"},
universe@556 53 {map_operation::put, "key 5", "value 5"},
universe@556 54 {map_operation::put, "key 6", "value 6"},
universe@556 55 {map_operation::rm, "key 4", nullptr},
universe@556 56 {map_operation::put, "key 7", "value 7"},
universe@556 57 {map_operation::put, "key 8", "value 8"},
universe@556 58 {map_operation::rm, "does not exist", nullptr},
universe@556 59 {map_operation::put, "key 9", "value 9"},
universe@556 60 {map_operation::put, "key 6", "other value"},
universe@556 61 {map_operation::put, "key 7", "something else"},
universe@556 62 {map_operation::rm, "key 8", nullptr},
universe@556 63 {map_operation::rm, "key 2", nullptr},
universe@556 64 {map_operation::put, "key 8", "new value"},
universe@556 65 };
universe@556 66 }
universe@556 67
universe@556 68 static void verify_map_contents(
universe@556 69 CxMap *map,
universe@556 70 std::unordered_map<std::string, std::string> const &refmap
universe@556 71 ) {
universe@556 72 // verify key iterator
universe@556 73 {
universe@556 74 auto keyiter = cxMapIteratorKeys(map);
universe@556 75 std::unordered_set<std::string> keys;
universe@563 76 cx_foreach(CxHashKey*, elem, keyiter) {
universe@604 77 keys.insert(std::string(elem->data.cstr, elem->len));
universe@556 78 }
universe@561 79 EXPECT_EQ(keyiter.index, map->size);
universe@556 80 ASSERT_EQ(keys.size(), map->size);
universe@556 81 for (auto &&k: keys) {
universe@556 82 EXPECT_NE(refmap.find(k), refmap.end());
universe@556 83 }
universe@556 84 }
universe@556 85
universe@556 86 // verify value iterator
universe@556 87 {
universe@556 88 auto valiter = cxMapIteratorValues(map);
universe@556 89 std::unordered_set<std::string> values; // we use that the values in our test data are unique strings
universe@556 90 cx_foreach(char const*, elem, valiter) {
universe@556 91 values.insert(std::string(elem));
universe@556 92 }
universe@561 93 EXPECT_EQ(valiter.index, map->size);
universe@556 94 ASSERT_EQ(values.size(), map->size);
universe@556 95 for (auto &&v: values) {
universe@556 96 EXPECT_NE(std::find_if(refmap.begin(), refmap.end(),
universe@556 97 [v](auto const &e) { return e.second == v; }), refmap.end());
universe@556 98 }
universe@556 99 }
universe@556 100
universe@556 101 // verify pair iterator
universe@556 102 {
universe@556 103 auto pairiter = cxMapIterator(map);
universe@556 104 std::unordered_map<std::string, std::string> pairs;
universe@556 105 cx_foreach(CxMapEntry*, entry, pairiter) {
universe@604 106 pairs[std::string(entry->key->data.cstr, entry->key->len)] = std::string((char *) entry->value);
universe@556 107 }
universe@561 108 EXPECT_EQ(pairiter.index, map->size);
universe@556 109 ASSERT_EQ(pairs.size(), refmap.size());
universe@556 110 for (auto &&p: pairs) {
universe@556 111 ASSERT_EQ(p.second, refmap.at(p.first));
universe@556 112 }
universe@556 113 }
universe@556 114 }
universe@556 115
universe@556 116 TEST(CxHashMap, Create) {
universe@556 117 CxTestingAllocator allocator;
universe@658 118 auto map = cxHashMapCreate(&allocator, 1, 0);
universe@556 119 auto hmap = reinterpret_cast<struct cx_hash_map_s *>(map);
universe@556 120 EXPECT_GT(hmap->bucket_count, 0);
universe@556 121 cx_for_n(i, hmap->bucket_count) {
universe@556 122 EXPECT_EQ(hmap->buckets[i], nullptr);
universe@556 123 }
universe@677 124 EXPECT_EQ(map->item_size, 1);
universe@556 125 EXPECT_EQ(map->size, 0);
universe@556 126 EXPECT_EQ(map->allocator, &allocator);
universe@685 127 EXPECT_FALSE(map->store_pointer);
universe@685 128 EXPECT_EQ(map->cmpfunc, nullptr);
universe@685 129 EXPECT_EQ(map->simple_destructor, nullptr);
universe@685 130 EXPECT_EQ(map->advanced_destructor, nullptr);
universe@685 131 EXPECT_EQ(map->destructor_data, nullptr);
universe@658 132 cxMapStorePointers(map);
universe@685 133 EXPECT_TRUE(map->store_pointer);
universe@677 134 EXPECT_EQ(map->item_size, sizeof(void *));
universe@658 135 cxMapStoreObjects(map);
universe@685 136 EXPECT_FALSE(map->store_pointer);
universe@556 137
universe@556 138 cxMapDestroy(map);
universe@556 139 EXPECT_TRUE(allocator.verify());
universe@556 140 }
universe@556 141
universe@668 142 TEST(CxHashMap, CreateForStoringPointers) {
universe@668 143 CxTestingAllocator allocator;
universe@668 144 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0);
universe@668 145 auto hmap = reinterpret_cast<struct cx_hash_map_s *>(map);
universe@668 146 EXPECT_GT(hmap->bucket_count, 0);
universe@668 147 cx_for_n(i, hmap->bucket_count) {
universe@668 148 EXPECT_EQ(hmap->buckets[i], nullptr);
universe@668 149 }
universe@668 150 EXPECT_EQ(map->size, 0);
universe@668 151 EXPECT_EQ(map->allocator, &allocator);
universe@685 152 EXPECT_TRUE(map->store_pointer);
universe@677 153 EXPECT_EQ(map->item_size, sizeof(void *));
universe@668 154
universe@668 155 cxMapDestroy(map);
universe@668 156 EXPECT_TRUE(allocator.verify());
universe@668 157 }
universe@668 158
universe@556 159 TEST(CxHashMap, BasicOperations) {
universe@556 160 // create the map
universe@556 161 CxTestingAllocator allocator;
universe@669 162 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8);
universe@556 163
universe@556 164 // create a reference map
universe@556 165 std::unordered_map<std::string, std::string> refmap;
universe@556 166
universe@556 167 // generate operations
universe@556 168 auto ops = generate_map_operations();
universe@556 169
universe@556 170 // verify iterators for empty map
universe@556 171 verify_map_contents(map, refmap);
universe@556 172
universe@556 173 // execute operations and verify results
universe@556 174 for (auto &&op: ops) {
universe@563 175 CxHashKey key = cx_hash_key_str(op.key);
universe@563 176 key.hash = 0; // force the hash map to compute the hash
universe@556 177 if (op.op == map_operation::put) {
universe@556 178 // execute a put operation and verify that the exact value can be read back
universe@556 179 refmap[std::string(op.key)] = std::string(op.value);
universe@556 180 int result = cxMapPut(map, key, (void *) op.value);
universe@556 181 EXPECT_EQ(result, 0);
universe@556 182 auto added = cxMapGet(map, key);
universe@556 183 EXPECT_EQ(memcmp(op.value, added, strlen(op.value)), 0);
universe@556 184 } else {
universe@556 185 // execute a remove and verify that the removed element was returned (or nullptr)
universe@556 186 auto found = refmap.find(op.key);
universe@659 187 auto removed = cxMapRemoveAndGet(map, key);
universe@556 188 if (found == refmap.end()) {
universe@556 189 EXPECT_EQ(removed, nullptr);
universe@556 190 } else {
universe@556 191 EXPECT_EQ(std::string((char *) removed), found->second);
universe@556 192 refmap.erase(found);
universe@556 193 }
universe@556 194 }
universe@556 195 // compare the current map state with the reference map
universe@556 196 verify_map_contents(map, refmap);
universe@556 197 }
universe@556 198
universe@556 199 // destroy the map and verify the memory (de)allocations
universe@556 200 cxMapDestroy(map);
universe@556 201 EXPECT_TRUE(allocator.verify());
universe@556 202 }
universe@561 203
universe@561 204 TEST(CxHashMap, RemoveViaIterator) {
universe@561 205 CxTestingAllocator allocator;
universe@669 206 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 4);
universe@561 207
universe@563 208 cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1");
universe@563 209 cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2");
universe@563 210 cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3");
universe@563 211 cxMapPut(map, cx_hash_key_str("key 4"), (void *) "val 4");
universe@563 212 cxMapPut(map, cx_hash_key_str("key 5"), (void *) "val 5");
universe@563 213 cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6");
universe@561 214
universe@630 215 auto iter = cxMapMutIterator(map);
universe@561 216 cx_foreach(CxMapEntry*, entry, iter) {
universe@630 217 if (entry->key->data.cstr[4] % 2 == 1) cxIteratorFlagRemoval(iter);
universe@561 218 }
universe@561 219 EXPECT_EQ(map->size, 3);
universe@561 220 EXPECT_EQ(iter.index, map->size);
universe@561 221
universe@563 222 EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 1")), nullptr);
universe@563 223 EXPECT_NE(cxMapGet(map, cx_hash_key_str("key 2")), nullptr);
universe@563 224 EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 3")), nullptr);
universe@563 225 EXPECT_NE(cxMapGet(map, cx_hash_key_str("key 4")), nullptr);
universe@563 226 EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 5")), nullptr);
universe@563 227 EXPECT_NE(cxMapGet(map, cx_hash_key_str("key 6")), nullptr);
universe@561 228
universe@561 229 cxMapDestroy(map);
universe@561 230 EXPECT_TRUE(allocator.verify());
universe@561 231 }
universe@562 232
universe@562 233 TEST(CxHashMap, RehashNotRequired) {
universe@562 234 CxTestingAllocator allocator;
universe@669 235 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8);
universe@562 236
universe@563 237 cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1");
universe@563 238 cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2");
universe@563 239 cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3");
universe@563 240 cxMapPut(map, cx_hash_key_str("key 4"), (void *) "val 4");
universe@563 241 cxMapPut(map, cx_hash_key_str("key 5"), (void *) "val 5");
universe@563 242 cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6");
universe@562 243
universe@562 244 // 6/8 does not exceed 0.75, therefore the function should not rehash
universe@562 245 int result = cxMapRehash(map);
universe@562 246 EXPECT_EQ(result, 0);
universe@562 247 EXPECT_EQ(reinterpret_cast<struct cx_hash_map_s *>(map)->bucket_count, 8);
universe@562 248
universe@562 249 cxMapDestroy(map);
universe@562 250 EXPECT_TRUE(allocator.verify());
universe@562 251 }
universe@562 252
universe@562 253 TEST(CxHashMap, Rehash) {
universe@562 254 CxTestingAllocator allocator;
universe@687 255 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 7);
universe@562 256
universe@563 257 cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1");
universe@563 258 cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2");
universe@563 259 cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3");
universe@687 260 cxMapPut(map, cx_hash_key_str("foo 4"), (void *) "val 4");
universe@563 261 cxMapPut(map, cx_hash_key_str("key 5"), (void *) "val 5");
universe@563 262 cxMapPut(map, cx_hash_key_str("key 6"), (void *) "val 6");
universe@687 263 cxMapPut(map, cx_hash_key_str("bar 7"), (void *) "val 7");
universe@687 264 cxMapPut(map, cx_hash_key_str("key 8"), (void *) "val 8");
universe@687 265 cxMapPut(map, cx_hash_key_str("key 9"), (void *) "val 9");
universe@687 266 cxMapPut(map, cx_hash_key_str("key 10"), (void *) "val 10");
universe@562 267
universe@562 268 int result = cxMapRehash(map);
universe@562 269 EXPECT_EQ(result, 0);
universe@687 270 EXPECT_EQ(reinterpret_cast<struct cx_hash_map_s *>(map)->bucket_count, 25);
universe@687 271 EXPECT_EQ(map->size, 10);
universe@562 272
universe@563 273 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 1")), "val 1"), 0);
universe@563 274 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 2")), "val 2"), 0);
universe@563 275 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 3")), "val 3"), 0);
universe@687 276 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("foo 4")), "val 4"), 0);
universe@563 277 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 5")), "val 5"), 0);
universe@563 278 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 6")), "val 6"), 0);
universe@687 279 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("bar 7")), "val 7"), 0);
universe@687 280 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 8")), "val 8"), 0);
universe@687 281 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 9")), "val 9"), 0);
universe@687 282 EXPECT_EQ(strcmp((char *) cxMapGet(map, cx_hash_key_str("key 10")), "val 10"), 0);
universe@562 283
universe@562 284 cxMapDestroy(map);
universe@562 285 EXPECT_TRUE(allocator.verify());
universe@594 286 }
universe@594 287
universe@594 288 TEST(CxHashMap, Clear) {
universe@594 289 CxTestingAllocator allocator;
universe@669 290 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0);
universe@686 291
universe@594 292 cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1");
universe@594 293 cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2");
universe@594 294 cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3");
universe@594 295
universe@594 296 EXPECT_EQ(map->size, 3);
universe@594 297
universe@594 298 cxMapClear(map);
universe@594 299
universe@594 300 EXPECT_EQ(map->size, 0);
universe@594 301 EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 1")), nullptr);
universe@594 302 EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 2")), nullptr);
universe@594 303 EXPECT_EQ(cxMapGet(map, cx_hash_key_str("key 3")), nullptr);
universe@594 304
universe@594 305 cxMapDestroy(map);
universe@594 306 EXPECT_TRUE(allocator.verify());
universe@658 307 }
universe@658 308
universe@658 309 TEST(CxHashMap, StoreUcxStrings) {
universe@658 310 // create the map
universe@658 311 CxTestingAllocator allocator;
universe@658 312 auto map = cxHashMapCreate(&allocator, sizeof(cxstring), 8);
universe@658 313
universe@658 314 // define some strings
universe@685 315 auto s1 = CX_STR("this");
universe@685 316 auto s2 = CX_STR("is");
universe@685 317 auto s3 = CX_STR("a");
universe@685 318 auto s4 = CX_STR("test");
universe@685 319 auto s5 = CX_STR("setup");
universe@658 320
universe@658 321 // put them into the map
universe@658 322 cxMapPut(map, cx_hash_key_str("s1"), &s1);
universe@658 323 cxMapPut(map, cx_hash_key_str("s2"), &s2);
universe@658 324 cxMapPut(map, cx_hash_key_str("s3"), &s3);
universe@658 325 cxMapPut(map, cx_hash_key_str("s4"), &s4);
universe@658 326
universe@658 327 // overwrite a value
universe@658 328 cxMapPut(map, cx_hash_key_str("s1"), &s5);
universe@658 329
universe@658 330 // look up a string
universe@658 331 auto s3p = reinterpret_cast<cxstring *>(cxMapGet(map, cx_hash_key_str("s3")));
universe@658 332 EXPECT_EQ(s3p->length, s3.length);
universe@658 333 EXPECT_EQ(s3p->ptr, s3.ptr);
universe@658 334 EXPECT_NE(s3p, &s3);
universe@658 335
universe@658 336 // remove a string
universe@659 337 cxMapRemove(map, cx_hash_key_str("s2"));
universe@658 338
universe@658 339 // iterate
universe@658 340 auto ref = std::vector{s5.ptr, s3.ptr, s4.ptr};
universe@658 341 auto iter = cxMapIteratorValues(map);
universe@658 342 cx_foreach(cxstring*, s, iter) {
universe@658 343 auto found = std::find(ref.begin(), ref.end(), s->ptr);
universe@658 344 ASSERT_NE(found, ref.end());
universe@658 345 ref.erase(found);
universe@658 346 }
universe@658 347 EXPECT_EQ(ref.size(), 0);
universe@658 348
universe@658 349 cxMapDestroy(map);
universe@658 350 EXPECT_TRUE(allocator.verify());
universe@658 351 }
universe@685 352
universe@686 353 static void test_simple_destructor(void *data) {
universe@686 354 strcpy((char *) data, "OK");
universe@686 355 }
universe@686 356
universe@686 357 static void test_advanced_destructor(
universe@686 358 [[maybe_unused]] void *unused,
universe@686 359 void *data
universe@686 360 ) {
universe@686 361 strcpy((char *) data, "OK");
universe@686 362 }
universe@686 363
universe@686 364 static void verify_any_destructor(CxMap *map) {
universe@686 365 auto k1 = cx_hash_key_str("key 1");
universe@686 366 auto k2 = cx_hash_key_str("key 2");
universe@686 367 auto k3 = cx_hash_key_str("key 3");
universe@686 368 auto k4 = cx_hash_key_str("key 4");
universe@686 369 auto k5 = cx_hash_key_str("key 5");
universe@686 370
universe@686 371 char v1[] = "val 1";
universe@686 372 char v2[] = "val 2";
universe@686 373 char v3[] = "val 3";
universe@686 374 char v4[] = "val 4";
universe@686 375 char v5[] = "val 5";
universe@686 376
universe@686 377 cxMapPut(map, k1, (void *) v1);
universe@686 378 cxMapPut(map, k2, (void *) v2);
universe@686 379 cxMapPut(map, k3, (void *) v3);
universe@686 380 cxMapPut(map, k4, (void *) v4);
universe@686 381
universe@686 382 cxMapRemove(map, k2);
universe@686 383 auto r = cxMapRemoveAndGet(map, k3);
universe@686 384 cxMapDetach(map, k1);
universe@686 385
universe@686 386 EXPECT_STREQ(v1, "val 1");
universe@686 387 EXPECT_STREQ(v2, "OK");
universe@686 388 EXPECT_STREQ(v3, "val 3");
universe@686 389 EXPECT_STREQ(v4, "val 4");
universe@686 390 EXPECT_STREQ(v5, "val 5");
universe@686 391 EXPECT_EQ(r, v3);
universe@686 392
universe@686 393 cxMapClear(map);
universe@686 394
universe@686 395 EXPECT_STREQ(v1, "val 1");
universe@686 396 EXPECT_STREQ(v2, "OK");
universe@686 397 EXPECT_STREQ(v3, "val 3");
universe@686 398 EXPECT_STREQ(v4, "OK");
universe@686 399 EXPECT_STREQ(v5, "val 5");
universe@686 400
universe@686 401 cxMapPut(map, k1, (void *) v1);
universe@686 402 cxMapPut(map, k3, (void *) v3);
universe@686 403 cxMapPut(map, k5, (void *) v5);
universe@686 404
universe@686 405 {
universe@686 406 auto iter = cxMapMutIteratorKeys(map);
universe@686 407 cx_foreach(CxHashKey*, key, iter) {
universe@686 408 if (key->data.cstr[4] == '1') cxIteratorFlagRemoval(iter);
universe@686 409 }
universe@686 410 }
universe@686 411 {
universe@686 412 auto iter = cxMapMutIteratorValues(map);
universe@686 413 cx_foreach(char*, v, iter) {
universe@686 414 if (v[4] == '5') cxIteratorFlagRemoval(iter);
universe@686 415 }
universe@686 416 }
universe@686 417
universe@686 418 EXPECT_STREQ(v1, "OK");
universe@686 419 EXPECT_STREQ(v2, "OK");
universe@686 420 EXPECT_STREQ(v3, "val 3");
universe@686 421 EXPECT_STREQ(v4, "OK");
universe@686 422 EXPECT_STREQ(v5, "OK");
universe@686 423
universe@686 424 v1[0] = v2[0] = v4[0] = v5[0] = 'c';
universe@686 425
universe@686 426 cxMapDestroy(map);
universe@686 427
universe@686 428 EXPECT_STREQ(v1, "cK");
universe@686 429 EXPECT_STREQ(v2, "cK");
universe@686 430 EXPECT_STREQ(v3, "OK");
universe@686 431 EXPECT_STREQ(v4, "cK");
universe@686 432 EXPECT_STREQ(v5, "cK");
universe@686 433 }
universe@686 434
universe@686 435 TEST(CxHashMap, SimpleDestructor) {
universe@686 436 CxTestingAllocator allocator;
universe@686 437 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0);
universe@686 438 map->simple_destructor = test_simple_destructor;
universe@686 439 verify_any_destructor(map);
universe@686 440 EXPECT_TRUE(allocator.verify());
universe@686 441 }
universe@686 442
universe@686 443 TEST(CxHashMap, AdvancedDestructor) {
universe@686 444 CxTestingAllocator allocator;
universe@686 445 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0);
universe@686 446 map->advanced_destructor = test_advanced_destructor;
universe@686 447 verify_any_destructor(map);
universe@686 448 EXPECT_TRUE(allocator.verify());
universe@686 449 }

mercurial