| 205 CX_TEST_ASSERT(s3p->length == s3.length); |
205 CX_TEST_ASSERT(s3p->length == s3.length); |
| 206 CX_TEST_ASSERT(s3p->ptr == s3.ptr); |
206 CX_TEST_ASSERT(s3p->ptr == s3.ptr); |
| 207 CX_TEST_ASSERT(s3p != &s3); |
207 CX_TEST_ASSERT(s3p != &s3); |
| 208 |
208 |
| 209 // remove a string |
209 // remove a string |
| 210 cxMapRemove(map, "s2"); |
210 cxstring ret = {0}; |
| |
211 CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, "s2", &ret)); |
| 211 CX_TEST_ASSERT(map->collection.size == 3); |
212 CX_TEST_ASSERT(map->collection.size == 3); |
| |
213 CX_TEST_ASSERT(0 == cx_strcmp(ret, cx_str("is"))); |
| 212 |
214 |
| 213 // iterate |
215 // iterate |
| 214 bool s3found = false, s4found = false, s5found = false; |
216 bool s3found = false, s4found = false, s5found = false; |
| 215 CxIterator iter = cxMapIteratorValues(map); |
217 CxIterator iter = cxMapIteratorValues(map); |
| 216 cx_foreach(cxstring*, s, iter) { |
218 cx_foreach(cxstring*, s, iter) { |
| 287 cxMapPut(map, k1, v1); |
289 cxMapPut(map, k1, v1); |
| 288 cxMapPut(map, k2, v2); |
290 cxMapPut(map, k2, v2); |
| 289 cxMapPut(map, k3, v3); |
291 cxMapPut(map, k3, v3); |
| 290 cxMapPut(map, k4, v4); |
292 cxMapPut(map, k4, v4); |
| 291 |
293 |
| 292 cxMapRemove(map, k2); |
294 CX_TEST_ASSERT(0 == cxMapRemove(map, k2)); |
| 293 char *r = cxMapRemoveAndGet(map, k3); |
295 char *r; |
| 294 cxMapDetach(map, k1); |
296 CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, k3, &r)); |
| 295 |
297 |
| 296 CX_TEST_ASSERT(0 == strcmp(v1, "val 1")); |
298 CX_TEST_ASSERT(0 == strcmp(v1, "val 1")); |
| 297 CX_TEST_ASSERT(0 == strcmp(v2, "OK")); |
299 CX_TEST_ASSERT(0 == strcmp(v2, "OK")); |
| 298 CX_TEST_ASSERT(0 == strcmp(v3, "val 3")); |
300 CX_TEST_ASSERT(0 == strcmp(v3, "val 3")); |
| 299 CX_TEST_ASSERT(0 == strcmp(v4, "val 4")); |
301 CX_TEST_ASSERT(0 == strcmp(v4, "val 4")); |
| 424 CX_TEST(test_hash_map_generics) { |
426 CX_TEST(test_hash_map_generics) { |
| 425 CxTestingAllocator talloc; |
427 CxTestingAllocator talloc; |
| 426 cx_testing_allocator_init(&talloc); |
428 cx_testing_allocator_init(&talloc); |
| 427 CxAllocator *allocator = &talloc.base; |
429 CxAllocator *allocator = &talloc.base; |
| 428 CX_TEST_DO { |
430 CX_TEST_DO { |
| 429 CxMap *map = cxHashMapCreate(allocator, sizeof(cxstring), 0); |
431 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0); |
| 430 cxMapPut(map, "test", "test"); |
432 cxMapPut(map, "test", "test"); |
| 431 cxMapPut(map, cx_mutstr("foo"), "bar"); |
433 cxMapPut(map, cx_mutstr("foo"), "bar"); |
| 432 cxMapPut(map, cx_str("hallo"), "welt"); |
434 cxMapPut(map, cx_str("hallo"), "welt"); |
| 433 |
435 |
| 434 CX_TEST_ASSERT(map->collection.size == 3); |
436 CX_TEST_ASSERT(map->collection.size == 3); |
| 435 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "test"), "test")); |
437 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "test"), "test")); |
| 436 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar")); |
438 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar")); |
| 437 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "hallo"), "welt")); |
439 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "hallo"), "welt")); |
| 438 |
440 |
| 439 // note: we don't have a destructor here, so remove and detach are the same |
441 CX_TEST_ASSERT(0 == cxMapRemove(map, cx_str("test"))); |
| 440 cxMapRemove(map, cx_str("test")); |
|
| 441 const char *hallo = "hallo"; |
442 const char *hallo = "hallo"; |
| 442 cxMapDetach(map, hallo); |
443 CX_TEST_ASSERT(0 == cxMapRemove(map, hallo)); |
| 443 cxMapPut(map, cx_hash_key_str("key"), "value"); |
444 cxMapPut(map, cx_hash_key_str("key"), "value"); |
| 444 |
445 |
| 445 CX_TEST_ASSERT(map->collection.size == 2); |
446 CX_TEST_ASSERT(map->collection.size == 2); |
| 446 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key"), "value")); |
447 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key"), "value")); |
| 447 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar")); |
448 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar")); |
| 448 |
449 |
| 449 void *r; |
450 const char *r1, *r2; |
| 450 r = cxMapRemoveAndGet(map, "key"); |
451 CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, "key", &r1)); |
| 451 r = cxMapRemoveAndGet(map, cx_str("foo")); |
452 CX_TEST_ASSERT(0 == strcmp(r1, "value")); |
| 452 if (r != NULL) map->collection.size = 47; |
453 CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, cx_str("foo"), &r2)); |
| |
454 CX_TEST_ASSERT(0 == strcmp(r2, "bar")); |
| |
455 r2 = "nope"; |
| |
456 CX_TEST_ASSERT(0 != cxMapRemoveAndGet(map, cx_hash_key("notfound",9), &r2)); |
| |
457 CX_TEST_ASSERT(0 == strcmp(r2, "nope")); |
| 453 |
458 |
| 454 CX_TEST_ASSERT(map->collection.size == 0); |
459 CX_TEST_ASSERT(map->collection.size == 0); |
| 455 |
460 |
| 456 cxMapFree(map); |
461 cxMapFree(map); |
| 457 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
462 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 639 void *added = cxMapGet(map, key); |
644 void *added = cxMapGet(map, key); |
| 640 CX_TEST_ASSERT(0 == memcmp(kv.value, added, strlen(kv.value))); |
645 CX_TEST_ASSERT(0 == memcmp(kv.value, added, strlen(kv.value))); |
| 641 } else { |
646 } else { |
| 642 // execute a remove and verify that the removed element was returned (or NULL) |
647 // execute a remove and verify that the removed element was returned (or NULL) |
| 643 const char *found = test_map_reference_remove(kv.key); |
648 const char *found = test_map_reference_remove(kv.key); |
| 644 void *removed = cxMapRemoveAndGet(map, key); |
649 void *removed = (void*) 0x1337; |
| 645 CX_TEST_ASSERT(found == removed); |
650 int result = cxMapRemoveAndGet(map, key, &removed); |
| |
651 if (found == NULL) { |
| |
652 CX_TEST_ASSERT(0 != result); |
| |
653 CX_TEST_ASSERT(removed == (void*) 0x1337); |
| |
654 } else { |
| |
655 CX_TEST_ASSERT(0 == result); |
| |
656 CX_TEST_ASSERT(removed == found); |
| |
657 } |
| |
658 |
| 646 } |
659 } |
| 647 // compare the current map state with the reference map |
660 // compare the current map state with the reference map |
| 648 CX_TEST_CALL_SUBROUTINE(verify_map_contents, map); |
661 CX_TEST_CALL_SUBROUTINE(verify_map_contents, map); |
| 649 } |
662 } |
| 650 |
663 |