tests/test_hash_map.c

changeset 1466
a58c65d31342
parent 1450
09a73312d5ec
equal deleted inserted replaced
1465:dc886f1a6155 1466:a58c65d31342
513 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 513 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
514 } 514 }
515 cx_testing_allocator_destroy(&talloc); 515 cx_testing_allocator_destroy(&talloc);
516 } 516 }
517 517
518 static bool test_hash_map_clone_func_max_enabled = false;
518 static unsigned test_hash_map_clone_func_max_clones; 519 static unsigned test_hash_map_clone_func_max_clones;
519 static void *test_hash_map_clone_func(void *dst, const void *src, 520 static void *test_hash_map_clone_func(void *dst, const void *src,
520 const CxAllocator *al, void *data) { 521 const CxAllocator *al, void *data) {
521 if (test_hash_map_clone_func_max_clones == 0) return NULL; 522 if (test_hash_map_clone_func_max_enabled) {
522 test_hash_map_clone_func_max_clones--; 523 if (test_hash_map_clone_func_max_clones == 0) return NULL;
524 test_hash_map_clone_func_max_clones--;
525 }
523 if (dst == NULL) { 526 if (dst == NULL) {
524 dst = cxMalloc(al, sizeof(int)); 527 dst = cxMalloc(al, sizeof(int));
525 } 528 }
526 *((int*)dst) = *((int*)src) + *((int*)data); 529 *((int*)dst) = *((int*)src) + *((int*)data);
527 return dst; 530 return dst;
538 cxMapPut(dst, exist_keys[i], &exists[i]); 541 cxMapPut(dst, exist_keys[i], &exists[i]);
539 cxMapPut(src, source_keys[i], &source[i]); 542 cxMapPut(src, source_keys[i], &source[i]);
540 } 543 }
541 CX_TEST_DO { 544 CX_TEST_DO {
542 int c = 4; 545 int c = 4;
543 test_hash_map_clone_func_max_clones = 100; // no limit
544 CX_TEST_ASSERT(0 == cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c)); 546 CX_TEST_ASSERT(0 == cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c));
545 CX_TEST_ASSERT(cxMapSize(dst) == 5); 547 CX_TEST_ASSERT(cxMapSize(dst) == 5);
546 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1); 548 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1);
547 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13); 549 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13);
548 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 4); 550 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 4);
568 cxMapPut(dst, exist_keys[i], &exists[i]); 570 cxMapPut(dst, exist_keys[i], &exists[i]);
569 cxMapPut(src, source_keys[i], &source[i]); 571 cxMapPut(src, source_keys[i], &source[i]);
570 } 572 }
571 CX_TEST_DO { 573 CX_TEST_DO {
572 int c = 4; 574 int c = 4;
575 test_hash_map_clone_func_max_enabled = true;
573 test_hash_map_clone_func_max_clones = 2; 576 test_hash_map_clone_func_max_clones = 2;
574 CX_TEST_ASSERT(0 != cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c)); 577 CX_TEST_ASSERT(0 != cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c));
578 test_hash_map_clone_func_max_enabled = false;
575 CX_TEST_ASSERT(cxMapSize(dst) == 4); 579 CX_TEST_ASSERT(cxMapSize(dst) == 4);
576 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1); 580 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1);
577 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13); 581 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13);
578 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 4); 582 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 4);
579 // the concrete element which is affected might change when the hash function changes 583 // the concrete element which is affected might change when the hash function changes
605 cxMapPut(dst, exist_keys[i], y); 609 cxMapPut(dst, exist_keys[i], y);
606 cxMapPut(src, source_keys[i], &source[i]); 610 cxMapPut(src, source_keys[i], &source[i]);
607 } 611 }
608 CX_TEST_DO { 612 CX_TEST_DO {
609 int c = 4; 613 int c = 4;
610 test_hash_map_clone_func_max_clones = 100; // no limit
611 CX_TEST_ASSERT(0 == cxMapClone(dst, src, test_hash_map_clone_func, allocator, &c)); 614 CX_TEST_ASSERT(0 == cxMapClone(dst, src, test_hash_map_clone_func, allocator, &c));
612 CX_TEST_ASSERT(cxMapSize(dst) == 5); 615 CX_TEST_ASSERT(cxMapSize(dst) == 5);
613 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1); 616 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1);
614 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13); 617 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13);
615 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 4); 618 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 4);
641 cxMapPut(s1, s1_keys[i], &s1_values[i]); 644 cxMapPut(s1, s1_keys[i], &s1_values[i]);
642 cxMapPut(s2, s2_keys[i], &s2_values[i]); 645 cxMapPut(s2, s2_keys[i], &s2_values[i]);
643 } 646 }
644 CX_TEST_DO { 647 CX_TEST_DO {
645 int c = 4; 648 int c = 4;
646 test_hash_map_clone_func_max_clones = 100; // no limit
647 CX_TEST_ASSERT(0 == cxMapDifference(dst, s1, s2, test_hash_map_clone_func, NULL, &c)); 649 CX_TEST_ASSERT(0 == cxMapDifference(dst, s1, s2, test_hash_map_clone_func, NULL, &c));
648 CX_TEST_ASSERT(cxMapSize(dst) == 2); 650 CX_TEST_ASSERT(cxMapSize(dst) == 2);
649 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 5); 651 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 5);
650 CX_TEST_ASSERT(cxMapGet(dst, "k2") == NULL); 652 CX_TEST_ASSERT(cxMapGet(dst, "k2") == NULL);
651 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8); 653 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8);
668 cxMapPut(s1, s1_keys[i], &s1_values[i]); 670 cxMapPut(s1, s1_keys[i], &s1_values[i]);
669 cxMapPut(s2, s2_keys[i], &s2_values[i]); 671 cxMapPut(s2, s2_keys[i], &s2_values[i]);
670 } 672 }
671 CX_TEST_DO { 673 CX_TEST_DO {
672 int c = 4; 674 int c = 4;
675 test_hash_map_clone_func_max_enabled = true;
673 test_hash_map_clone_func_max_clones = 1; 676 test_hash_map_clone_func_max_clones = 1;
674 CX_TEST_ASSERT(1 == cxMapDifference(dst, s1, s2, test_hash_map_clone_func, NULL, &c)); 677 CX_TEST_ASSERT(1 == cxMapDifference(dst, s1, s2, test_hash_map_clone_func, NULL, &c));
678 test_hash_map_clone_func_max_enabled = false;
675 CX_TEST_ASSERT(cxMapSize(dst) == 1); 679 CX_TEST_ASSERT(cxMapSize(dst) == 1);
676 // the concrete element which is affected might change when the hash function changes 680 // the concrete element which is affected might change when the hash function changes
677 CX_TEST_ASSERT(cxMapGet(dst, "k1") == NULL); 681 CX_TEST_ASSERT(cxMapGet(dst, "k1") == NULL);
678 CX_TEST_ASSERT(cxMapGet(dst, "k2") == NULL); 682 CX_TEST_ASSERT(cxMapGet(dst, "k2") == NULL);
679 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8); 683 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8);
698 CxHashKey key = CX_HASH_KEY(k[i]); 702 CxHashKey key = CX_HASH_KEY(k[i]);
699 cxListAdd(keys, &key); 703 cxListAdd(keys, &key);
700 } 704 }
701 CX_TEST_DO { 705 CX_TEST_DO {
702 int c = 4; 706 int c = 4;
703 test_hash_map_clone_func_max_clones = 100; // no limit
704 CX_TEST_ASSERT(0 == cxMapListDifference(dst, src, keys, test_hash_map_clone_func, NULL, &c)); 707 CX_TEST_ASSERT(0 == cxMapListDifference(dst, src, keys, test_hash_map_clone_func, NULL, &c));
705 CX_TEST_ASSERT(cxMapSize(dst) == 2); 708 CX_TEST_ASSERT(cxMapSize(dst) == 2);
706 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 5); 709 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 5);
707 CX_TEST_ASSERT(cxMapGet(dst, "k2") == NULL); 710 CX_TEST_ASSERT(cxMapGet(dst, "k2") == NULL);
708 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8); 711 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8);
727 CxHashKey key = CX_HASH_KEY(k[i]); 730 CxHashKey key = CX_HASH_KEY(k[i]);
728 cxListAdd(keys, &key); 731 cxListAdd(keys, &key);
729 } 732 }
730 CX_TEST_DO { 733 CX_TEST_DO {
731 int c = 4; 734 int c = 4;
735 test_hash_map_clone_func_max_enabled = true;
732 test_hash_map_clone_func_max_clones = 1; 736 test_hash_map_clone_func_max_clones = 1;
733 CX_TEST_ASSERT(1 == cxMapListDifference(dst, src, keys, test_hash_map_clone_func, NULL, &c)); 737 CX_TEST_ASSERT(1 == cxMapListDifference(dst, src, keys, test_hash_map_clone_func, NULL, &c));
738 test_hash_map_clone_func_max_enabled = false;
734 CX_TEST_ASSERT(cxMapSize(dst) == 1); 739 CX_TEST_ASSERT(cxMapSize(dst) == 1);
735 // the concrete element which is affected might change when the hash function changes 740 // the concrete element which is affected might change when the hash function changes
736 CX_TEST_ASSERT(cxMapGet(dst, "k1") == NULL); 741 CX_TEST_ASSERT(cxMapGet(dst, "k1") == NULL);
737 CX_TEST_ASSERT(cxMapGet(dst, "k2") == NULL); 742 CX_TEST_ASSERT(cxMapGet(dst, "k2") == NULL);
738 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8); 743 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8);
755 for (unsigned int i = 0 ; i < 3 ; i++) { 760 for (unsigned int i = 0 ; i < 3 ; i++) {
756 cxMapPut(s1, s1_keys[i], &s1_values[i]); 761 cxMapPut(s1, s1_keys[i], &s1_values[i]);
757 cxMapPut(s2, s2_keys[i], &s2_values[i]); 762 cxMapPut(s2, s2_keys[i], &s2_values[i]);
758 } 763 }
759 764
760 // add k5 to dst which is not in src, but shall also be not in the difference 765 // add k5 to dst which is not in src, and also not in the difference
761 int *k5 = cxMallocDefault(sizeof(int)); 766 int *k5 = cxMallocDefault(sizeof(int));
762 *k5 = 1337; 767 *k5 = 1337;
763 cxMapPut(dst, "k5",k5); 768 cxMapPut(dst, "k5",k5);
764 769
765 CX_TEST_DO { 770 CX_TEST_DO {
766 int c = 4; 771 int c = 4;
767 test_hash_map_clone_func_max_clones = 100; // no limit
768 CX_TEST_ASSERT(0 == cxMapDifference(dst, s1, s2, test_hash_map_clone_func, NULL, &c)); 772 CX_TEST_ASSERT(0 == cxMapDifference(dst, s1, s2, test_hash_map_clone_func, NULL, &c));
769 CX_TEST_ASSERT(cxMapSize(dst) == 2); 773 CX_TEST_ASSERT(cxMapSize(dst) == 3);
770 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 5); 774 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 5);
771 CX_TEST_ASSERT(cxMapGet(dst, "k2") == NULL); 775 CX_TEST_ASSERT(cxMapGet(dst, "k2") == NULL);
772 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8); 776 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8);
773 CX_TEST_ASSERT(cxMapGet(dst, "k5") == NULL); 777 CX_TEST_ASSERT(*(int*)cxMapGet(dst, "k5") == 1337);
774 } 778 }
775 cxMapFree(dst); 779 cxMapFree(dst);
776 cxMapFree(s1); 780 cxMapFree(s1);
777 cxMapFree(s2); 781 cxMapFree(s2);
778 } 782 }
792 for (unsigned int i = 0 ; i < 3 ; i++) { 796 for (unsigned int i = 0 ; i < 3 ; i++) {
793 CxHashKey key = CX_HASH_KEY(k[i]); 797 CxHashKey key = CX_HASH_KEY(k[i]);
794 cxListAdd(keys, &key); 798 cxListAdd(keys, &key);
795 } 799 }
796 800
797 // add k5 to dst which is not in src, but shall also be not in the difference 801 // add k5 to dst which is not in src, and also not in the difference
798 int *k5 = cxMallocDefault(sizeof(int)); 802 int *k5 = cxMallocDefault(sizeof(int));
799 *k5 = 1337; 803 *k5 = 1337;
800 cxMapPut(dst, "k5",k5); 804 cxMapPut(dst, "k5",k5);
801 805
802 CX_TEST_DO { 806 CX_TEST_DO {
803 int c = 4; 807 int c = 4;
804 test_hash_map_clone_func_max_clones = 100; // no limit
805 CX_TEST_ASSERT(0 == cxMapListDifference(dst, src, keys, test_hash_map_clone_func, NULL, &c)); 808 CX_TEST_ASSERT(0 == cxMapListDifference(dst, src, keys, test_hash_map_clone_func, NULL, &c));
806 CX_TEST_ASSERT(cxMapSize(dst) == 2); 809 CX_TEST_ASSERT(cxMapSize(dst) == 3);
807 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 5); 810 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 5);
808 CX_TEST_ASSERT(cxMapGet(dst, "k2") == NULL); 811 CX_TEST_ASSERT(cxMapGet(dst, "k2") == NULL);
809 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8); 812 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8);
810 CX_TEST_ASSERT(cxMapGet(dst, "k5") == NULL); 813 CX_TEST_ASSERT(*(int*)cxMapGet(dst, "k5") == 1337);
811 } 814 }
812 cxMapFree(dst); 815 cxMapFree(dst);
813 cxMapFree(src); 816 cxMapFree(src);
814 cxListFree(keys); 817 cxListFree(keys);
815 } 818 }

mercurial