| 511 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
511 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 512 } |
512 } |
| 513 cx_testing_allocator_destroy(&talloc); |
513 cx_testing_allocator_destroy(&talloc); |
| 514 } |
514 } |
| 515 |
515 |
| |
516 static unsigned test_hash_map_clone_func_max_clones; |
| 516 static void *test_hash_map_clone_func(void *dst, const void *src, |
517 static void *test_hash_map_clone_func(void *dst, const void *src, |
| 517 const CxAllocator *al, void *data) { |
518 const CxAllocator *al, void *data) { |
| |
519 if (test_hash_map_clone_func_max_clones == 0) return NULL; |
| |
520 test_hash_map_clone_func_max_clones--; |
| 518 if (dst == NULL) { |
521 if (dst == NULL) { |
| 519 dst = cxMalloc(al, sizeof(int)); |
522 dst = cxMalloc(al, sizeof(int)); |
| 520 } |
523 } |
| 521 *((int*)dst) = *((int*)src) + *((int*)data); |
524 *((int*)dst) = *((int*)src) + *((int*)data); |
| 522 return dst; |
525 return dst; |
| 533 cxMapPut(dst, exist_keys[i], &exists[i]); |
536 cxMapPut(dst, exist_keys[i], &exists[i]); |
| 534 cxMapPut(src, source_keys[i], &source[i]); |
537 cxMapPut(src, source_keys[i], &source[i]); |
| 535 } |
538 } |
| 536 CX_TEST_DO { |
539 CX_TEST_DO { |
| 537 int c = 4; |
540 int c = 4; |
| |
541 test_hash_map_clone_func_max_clones = 100; // no limit |
| 538 size_t cloned = cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c); |
542 size_t cloned = cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c); |
| 539 CX_TEST_ASSERT(cloned == 3); |
543 CX_TEST_ASSERT(cloned == 3); |
| 540 CX_TEST_ASSERT(cxMapSize(dst) == 5); |
544 CX_TEST_ASSERT(cxMapSize(dst) == 5); |
| 541 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1); |
545 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1); |
| 542 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13); |
546 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13); |
| 550 } |
554 } |
| 551 cxMapFree(dst); |
555 cxMapFree(dst); |
| 552 cxMapFree(src); |
556 cxMapFree(src); |
| 553 } |
557 } |
| 554 |
558 |
| |
559 CX_TEST(test_hash_map_clone_alloc_fail) { |
| |
560 CxMap *dst = cxHashMapCreateSimple(sizeof(int)); |
| |
561 CxMap *src = cxHashMapCreateSimple(sizeof(int)); |
| |
562 const char *exist_keys[] = {"k1", "k2", "k3"}; |
| |
563 int exists[] = {1, 3, 4}; |
| |
564 const char *source_keys[] = {"k4", "k2", "k5"}; |
| |
565 int source[] = {7, 9, 15}; |
| |
566 for (unsigned int i = 0 ; i < 3 ; i++) { |
| |
567 cxMapPut(dst, exist_keys[i], &exists[i]); |
| |
568 cxMapPut(src, source_keys[i], &source[i]); |
| |
569 } |
| |
570 CX_TEST_DO { |
| |
571 int c = 4; |
| |
572 test_hash_map_clone_func_max_clones = 2; |
| |
573 size_t cloned = cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c); |
| |
574 CX_TEST_ASSERT(cloned == 2); |
| |
575 CX_TEST_ASSERT(cxMapSize(dst) == 4); |
| |
576 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1); |
| |
577 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13); |
| |
578 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 4); |
| |
579 // the concrete element which is affected might change when the hash function changes |
| |
580 CX_TEST_ASSERT(cxMapGet(dst, "k4") == NULL); |
| |
581 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k5")) == 19); |
| |
582 CX_TEST_ASSERT(cxMapSize(src) == 3); |
| |
583 CX_TEST_ASSERT(*((int*)cxMapGet(src, "k4")) == 7); |
| |
584 CX_TEST_ASSERT(*((int*)cxMapGet(src, "k2")) == 9); |
| |
585 CX_TEST_ASSERT(*((int*)cxMapGet(src, "k5")) == 15); |
| |
586 } |
| |
587 cxMapFree(dst); |
| |
588 cxMapFree(src); |
| |
589 } |
| |
590 |
| 555 CX_TEST(test_hash_map_clone_ptr) { |
591 CX_TEST(test_hash_map_clone_ptr) { |
| 556 CxTestingAllocator talloc; |
592 CxTestingAllocator talloc; |
| 557 cx_testing_allocator_init(&talloc); |
593 cx_testing_allocator_init(&talloc); |
| 558 CxAllocator *allocator = &talloc.base; |
594 CxAllocator *allocator = &talloc.base; |
| 559 CxMap *dst = cxHashMapCreateSimple(CX_STORE_POINTERS); |
595 CxMap *dst = cxHashMapCreateSimple(CX_STORE_POINTERS); |
| 569 cxMapPut(dst, exist_keys[i], y); |
605 cxMapPut(dst, exist_keys[i], y); |
| 570 cxMapPut(src, source_keys[i], &source[i]); |
606 cxMapPut(src, source_keys[i], &source[i]); |
| 571 } |
607 } |
| 572 CX_TEST_DO { |
608 CX_TEST_DO { |
| 573 int c = 4; |
609 int c = 4; |
| |
610 test_hash_map_clone_func_max_clones = 100; // no limit |
| 574 size_t cloned = cxMapClone(dst, src, test_hash_map_clone_func, allocator, &c); |
611 size_t cloned = cxMapClone(dst, src, test_hash_map_clone_func, allocator, &c); |
| 575 CX_TEST_ASSERT(cloned == 3); |
612 CX_TEST_ASSERT(cloned == 3); |
| 576 CX_TEST_ASSERT(cxMapSize(dst) == 5); |
613 CX_TEST_ASSERT(cxMapSize(dst) == 5); |
| 577 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1); |
614 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1); |
| 578 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13); |
615 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13); |
| 936 cx_test_register(suite, test_hash_map_simple_destructor_objects); |
973 cx_test_register(suite, test_hash_map_simple_destructor_objects); |
| 937 cx_test_register(suite, test_hash_map_advanced_destructor_objects); |
974 cx_test_register(suite, test_hash_map_advanced_destructor_objects); |
| 938 cx_test_register(suite, test_hash_map_simple_destructor_pointers); |
975 cx_test_register(suite, test_hash_map_simple_destructor_pointers); |
| 939 cx_test_register(suite, test_hash_map_advanced_destructor_pointers); |
976 cx_test_register(suite, test_hash_map_advanced_destructor_pointers); |
| 940 cx_test_register(suite, test_hash_map_clone); |
977 cx_test_register(suite, test_hash_map_clone); |
| |
978 cx_test_register(suite, test_hash_map_clone_alloc_fail); |
| 941 cx_test_register(suite, test_hash_map_clone_ptr); |
979 cx_test_register(suite, test_hash_map_clone_ptr); |
| 942 cx_test_register(suite, test_empty_map_no_ops); |
980 cx_test_register(suite, test_empty_map_no_ops); |
| 943 cx_test_register(suite, test_empty_map_size); |
981 cx_test_register(suite, test_empty_map_size); |
| 944 cx_test_register(suite, test_empty_map_get); |
982 cx_test_register(suite, test_empty_map_get); |
| 945 cx_test_register(suite, test_empty_map_iterator); |
983 cx_test_register(suite, test_empty_map_iterator); |