tests/test_list.cpp

changeset 677
b09aae58bba4
parent 672
55d8fdd38ca4
child 679
022fbd4bc057
equal deleted inserted replaced
676:d0680a23d850 677:b09aae58bba4
590 CxList *list, 590 CxList *list,
591 bool as_pointer 591 bool as_pointer
592 ) { 592 ) {
593 auto len = testdata_len; 593 auto len = testdata_len;
594 cx_for_n (i, len) EXPECT_EQ(cxListAdd(list, &testdata.data[i]), 0); 594 cx_for_n (i, len) EXPECT_EQ(cxListAdd(list, &testdata.data[i]), 0);
595 EXPECT_EQ(list->size, len); 595 EXPECT_EQ(cxListSize(list), len);
596 EXPECT_GE(list->capacity, list->size);
597 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]); 596 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
598 cx_for_n (i, len) ++testdata.data[i]; 597 cx_for_n (i, len) ++testdata.data[i];
599 if (as_pointer) { 598 if (as_pointer) {
600 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]); 599 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
601 } else { 600 } else {
605 604
606 static void verifyInsert(CxList *list) { 605 static void verifyInsert(CxList *list) {
607 int a = 5, b = 47, c = 13, d = 42; 606 int a = 5, b = 47, c = 13, d = 42;
608 607
609 EXPECT_NE(cxListInsert(list, 1, &a), 0); 608 EXPECT_NE(cxListInsert(list, 1, &a), 0);
610 EXPECT_EQ(list->size, 0); 609 EXPECT_EQ(cxListSize(list), 0);
611 EXPECT_EQ(cxListInsert(list, 0, &a), 0); 610 EXPECT_EQ(cxListInsert(list, 0, &a), 0);
612 EXPECT_EQ(list->size, 1); 611 EXPECT_EQ(cxListSize(list), 1);
613 EXPECT_EQ(cxListInsert(list, 0, &b), 0); 612 EXPECT_EQ(cxListInsert(list, 0, &b), 0);
614 EXPECT_EQ(list->size, 2); 613 EXPECT_EQ(cxListSize(list), 2);
615 EXPECT_EQ(cxListInsert(list, 1, &c), 0); 614 EXPECT_EQ(cxListInsert(list, 1, &c), 0);
616 EXPECT_EQ(list->size, 3); 615 EXPECT_EQ(cxListSize(list), 3);
617 EXPECT_EQ(cxListInsert(list, 3, &d), 0); 616 EXPECT_EQ(cxListInsert(list, 3, &d), 0);
618 617
619 ASSERT_EQ(list->size, 4); 618 ASSERT_EQ(cxListSize(list), 4);
620 EXPECT_GE(list->capacity, list->size);
621 619
622 EXPECT_EQ(*(int *) cxListAt(list, 0), 47); 620 EXPECT_EQ(*(int *) cxListAt(list, 0), 47);
623 EXPECT_EQ(*(int *) cxListAt(list, 1), 13); 621 EXPECT_EQ(*(int *) cxListAt(list, 1), 13);
624 EXPECT_EQ(*(int *) cxListAt(list, 2), 5); 622 EXPECT_EQ(*(int *) cxListAt(list, 2), 5);
625 EXPECT_EQ(*(int *) cxListAt(list, 3), 42); 623 EXPECT_EQ(*(int *) cxListAt(list, 3), 42);
670 } 668 }
671 669
672 void verifyRemove(CxList *list) const { 670 void verifyRemove(CxList *list) const {
673 EXPECT_EQ(cxListRemove(list, 2), 0); 671 EXPECT_EQ(cxListRemove(list, 2), 0);
674 EXPECT_EQ(cxListRemove(list, 4), 0); 672 EXPECT_EQ(cxListRemove(list, 4), 0);
675 EXPECT_EQ(list->size, testdata_len - 2); 673 EXPECT_EQ(cxListSize(list), testdata_len - 2);
676 EXPECT_GE(list->capacity, list->size);
677 EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[0]); 674 EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[0]);
678 EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[1]); 675 EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[1]);
679 EXPECT_EQ(*(int *) cxListAt(list, 2), testdata.data[3]); 676 EXPECT_EQ(*(int *) cxListAt(list, 2), testdata.data[3]);
680 EXPECT_EQ(*(int *) cxListAt(list, 3), testdata.data[4]); 677 EXPECT_EQ(*(int *) cxListAt(list, 3), testdata.data[4]);
681 EXPECT_EQ(*(int *) cxListAt(list, 4), testdata.data[6]); 678 EXPECT_EQ(*(int *) cxListAt(list, 4), testdata.data[6]);
682 679
683 EXPECT_EQ(cxListRemove(list, 0), 0); 680 EXPECT_EQ(cxListRemove(list, 0), 0);
684 EXPECT_EQ(list->size, testdata_len - 3); 681 EXPECT_EQ(cxListSize(list), testdata_len - 3);
685 EXPECT_GE(list->capacity, list->size);
686 EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[1]); 682 EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[1]);
687 EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[3]); 683 EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[3]);
688 684
689 EXPECT_NE(cxListRemove(list, testdata_len), 0); 685 EXPECT_NE(cxListRemove(list, testdata_len), 0);
690 } 686 }
691 687
692 static void verifyClear(CxList *list) { 688 static void verifyClear(CxList *list) {
693 cxListClear(list); 689 cxListClear(list);
694 EXPECT_EQ(0, list->size); 690 EXPECT_EQ(0, cxListSize(list));
695 } 691 }
696 692
697 static unsigned destr_test_ctr; 693 static unsigned destr_test_ctr;
698 static int destr_last_value; 694 static int destr_last_value;
699 695
715 int off = cxListIsStoringPointers(list) ? 1 : 0; 711 int off = cxListIsStoringPointers(list) ? 1 : 0;
716 712
717 cxListRemove(list, 15); 713 cxListRemove(list, 15);
718 EXPECT_EQ(1, destr_test_ctr); 714 EXPECT_EQ(1, destr_test_ctr);
719 EXPECT_EQ(testdata.data[15], destr_last_value + off); 715 EXPECT_EQ(testdata.data[15], destr_last_value + off);
720 EXPECT_EQ(testdata_len - destr_test_ctr, list->size); 716 EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
721 cxListRemove(list, 47); 717 cxListRemove(list, 47);
722 EXPECT_EQ(2, destr_test_ctr); 718 EXPECT_EQ(2, destr_test_ctr);
723 EXPECT_EQ(testdata.data[48], destr_last_value + off); 719 EXPECT_EQ(testdata.data[48], destr_last_value + off);
724 EXPECT_EQ(testdata_len - destr_test_ctr, list->size); 720 EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
725 721
726 auto iter = cxListMutIteratorAt(list, 7); 722 auto iter = cxListMutIteratorAt(list, 7);
727 cxIteratorNext(iter); 723 cxIteratorNext(iter);
728 EXPECT_EQ(2, destr_test_ctr); 724 EXPECT_EQ(2, destr_test_ctr);
729 EXPECT_EQ(testdata.data[48], destr_last_value + off); 725 EXPECT_EQ(testdata.data[48], destr_last_value + off);
730 EXPECT_EQ(testdata_len - destr_test_ctr, list->size); 726 EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
731 cxIteratorFlagRemoval(iter); 727 cxIteratorFlagRemoval(iter);
732 cxIteratorNext(iter); 728 cxIteratorNext(iter);
733 EXPECT_EQ(3, destr_test_ctr); 729 EXPECT_EQ(3, destr_test_ctr);
734 EXPECT_EQ(testdata.data[8], destr_last_value + off); 730 EXPECT_EQ(testdata.data[8], destr_last_value + off);
735 EXPECT_EQ(testdata_len - destr_test_ctr, list->size); 731 EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
736 732
737 iter = cxListMutBackwardsIteratorAt(list, 5); 733 iter = cxListMutBackwardsIteratorAt(list, 5);
738 cxIteratorNext(iter); 734 cxIteratorNext(iter);
739 EXPECT_EQ(3, destr_test_ctr); 735 EXPECT_EQ(3, destr_test_ctr);
740 EXPECT_EQ(testdata.data[8], destr_last_value + off); 736 EXPECT_EQ(testdata.data[8], destr_last_value + off);
741 EXPECT_EQ(testdata_len - destr_test_ctr, list->size); 737 EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
742 cxIteratorFlagRemoval(iter); 738 cxIteratorFlagRemoval(iter);
743 cxIteratorNext(iter); 739 cxIteratorNext(iter);
744 EXPECT_EQ(4, destr_test_ctr); 740 EXPECT_EQ(4, destr_test_ctr);
745 EXPECT_EQ(testdata.data[4], destr_last_value + off); 741 EXPECT_EQ(testdata.data[4], destr_last_value + off);
746 EXPECT_EQ(testdata_len - destr_test_ctr, list->size); 742 EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
747 743
748 cxListClear(list); 744 cxListClear(list);
749 EXPECT_EQ(testdata_len, destr_test_ctr); 745 EXPECT_EQ(testdata_len, destr_test_ctr);
750 EXPECT_EQ(testdata.data[testdata_len - 1], destr_last_value + off); 746 EXPECT_EQ(testdata.data[testdata_len - 1], destr_last_value + off);
751 747
752 748
753 } 749 }
754 750
755 void verifySimpleDestructor(CxList *list) { 751 void verifySimpleDestructor(CxList *list) {
756 destr_test_ctr = 0; 752 destr_test_ctr = 0;
757 list->content_destructor_type = CX_DESTRUCTOR_SIMPLE;
758 list->simple_destructor = simple_destr_test_fun; 753 list->simple_destructor = simple_destr_test_fun;
759 verifyAnyDestructor(list); 754 verifyAnyDestructor(list);
760 } 755 }
761 756
762 void verifyAdvancedDestructor(CxList *list) { 757 void verifyAdvancedDestructor(CxList *list) {
763 destr_test_ctr = 0; 758 destr_test_ctr = 0;
764 list->content_destructor_type = CX_DESTRUCTOR_ADVANCED; 759 list->advanced_destructor = advanced_destr_test_fun;
765 list->advanced_destructor.func = advanced_destr_test_fun;
766 verifyAnyDestructor(list); 760 verifyAnyDestructor(list);
767 } 761 }
768 762
769 static void verifySwap(CxList *list) { 763 static void verifySwap(CxList *list) {
770 ASSERT_EQ(list->size, 0); 764 ASSERT_EQ(cxListSize(list), 0);
771 765
772 int original[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; 766 int original[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
773 int swapped[16] = {8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13}; 767 int swapped[16] = {8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13};
774 768
775 // we have to add the items one by one, because it could be a pointer list 769 // we have to add the items one by one, because it could be a pointer list
814 } 808 }
815 } 809 }
816 810
817 void verifyAt(CxList *list) const { 811 void verifyAt(CxList *list) const {
818 auto len = testdata_len; 812 auto len = testdata_len;
819 EXPECT_EQ(list->size, len); 813 EXPECT_EQ(cxListSize(list), len);
820 cx_for_n (i, len) { 814 cx_for_n (i, len) {
821 EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]); 815 EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
822 } 816 }
823 EXPECT_EQ(cxListAt(list, list->size), nullptr); 817 EXPECT_EQ(cxListAt(list, cxListSize(list)), nullptr);
824 } 818 }
825 819
826 void verifyFind(CxList *list) const { 820 void verifyFind(CxList *list) const {
827 cx_for_n (attempt, 25) { 821 cx_for_n (attempt, 25) {
828 size_t exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp) 822 size_t exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
836 } 830 }
837 EXPECT_EQ(cxListFind(list, &val), exp); 831 EXPECT_EQ(cxListFind(list, &val), exp);
838 } 832 }
839 833
840 int notinlist = -1; 834 int notinlist = -1;
841 EXPECT_EQ(list->size, cxListFind(list, &notinlist)); 835 EXPECT_EQ(cxListSize(list), cxListFind(list, &notinlist));
842 } 836 }
843 837
844 void verifySort(CxList *list) const { 838 void verifySort(CxList *list) const {
845 std::array<int, testdata_len> expected{}; 839 std::array<int, testdata_len> expected{};
846 std::partial_sort_copy(testdata.data.begin(), testdata.data.end(), expected.begin(), expected.end()); 840 std::partial_sort_copy(testdata.data.begin(), testdata.data.end(), expected.begin(), expected.end());
854 cx_foreach(int*, x, iter) { 848 cx_foreach(int*, x, iter) {
855 ASSERT_EQ(i, iter.index); 849 ASSERT_EQ(i, iter.index);
856 EXPECT_EQ(*x, testdata.data[iter.index]); 850 EXPECT_EQ(*x, testdata.data[iter.index]);
857 i++; 851 i++;
858 } 852 }
859 ASSERT_EQ(i, list->size); 853 ASSERT_EQ(i, cxListSize(list));
860 iter = cxListBackwardsIterator(list); 854 iter = cxListBackwardsIterator(list);
861 cx_foreach(int*, x, iter) { 855 cx_foreach(int*, x, iter) {
862 ASSERT_EQ(i - 1, iter.index); 856 ASSERT_EQ(i - 1, iter.index);
863 EXPECT_EQ(*x, testdata.data[iter.index]); 857 EXPECT_EQ(*x, testdata.data[iter.index]);
864 i--; 858 i--;
885 if (i % 2 == 0) cxIteratorFlagRemoval(mut_iter); 879 if (i % 2 == 0) cxIteratorFlagRemoval(mut_iter);
886 i--; 880 i--;
887 j++; 881 j++;
888 } 882 }
889 ASSERT_EQ(i, 0); 883 ASSERT_EQ(i, 0);
890 ASSERT_EQ(list->size, len / 2); 884 ASSERT_EQ(cxListSize(list), len / 2);
891 cx_for_n(j, len / 2) ASSERT_EQ(*(int *) cxListAt(list, j), testdata.data[j * 2]); 885 cx_for_n(j, len / 2) ASSERT_EQ(*(int *) cxListAt(list, j), testdata.data[j * 2]);
892 } 886 }
893 887
894 static void verifyInsertViaIterator(CxList *list) { 888 static void verifyInsertViaIterator(CxList *list) {
895 int newdata[] = {10, 20, 30, 40, 50}; 889 int newdata[] = {10, 20, 30, 40, 50};
910 iter = cxListMutIterator(list); 904 iter = cxListMutIterator(list);
911 cxListInsertBefore(&iter, &newdata[2]); 905 cxListInsertBefore(&iter, &newdata[2]);
912 EXPECT_TRUE(cxIteratorValid(iter)); 906 EXPECT_TRUE(cxIteratorValid(iter));
913 EXPECT_EQ(iter.index, 1); 907 EXPECT_EQ(iter.index, 1);
914 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 0); 908 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 0);
915 iter = cxListMutIteratorAt(list, list->size); 909 iter = cxListMutIteratorAt(list, cxListSize(list));
916 cxListInsertBefore(&iter, &newdata[3]); 910 cxListInsertBefore(&iter, &newdata[3]);
917 EXPECT_FALSE(cxIteratorValid(iter)); 911 EXPECT_FALSE(cxIteratorValid(iter));
918 EXPECT_EQ(iter.index, 9); 912 EXPECT_EQ(iter.index, 9);
919 iter = cxListMutIteratorAt(list, list->size); 913 iter = cxListMutIteratorAt(list, cxListSize(list));
920 cxListInsertAfter(&iter, &newdata[4]); 914 cxListInsertAfter(&iter, &newdata[4]);
921 EXPECT_FALSE(cxIteratorValid(iter)); 915 EXPECT_FALSE(cxIteratorValid(iter));
922 EXPECT_EQ(iter.index, 10); 916 EXPECT_EQ(iter.index, 10);
923 917
924 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; 918 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};
937 CxList *right 931 CxList *right
938 ) { 932 ) {
939 EXPECT_EQ(cxListCompare(left, right), 0); 933 EXPECT_EQ(cxListCompare(left, right), 0);
940 int x = 42; 934 int x = 42;
941 cxListAdd(left, &x); 935 cxListAdd(left, &x);
942 ASSERT_GT(left->size, right->size); 936 ASSERT_GT(cxListSize(left), cxListSize(right));
943 EXPECT_GT(cxListCompare(left, right), 0); 937 EXPECT_GT(cxListCompare(left, right), 0);
944 EXPECT_LT(cxListCompare(right, left), 0); 938 EXPECT_LT(cxListCompare(right, left), 0);
945 cxListAdd(right, &x); 939 cxListAdd(right, &x);
946 ASSERT_EQ(left->size, right->size); 940 ASSERT_EQ(cxListSize(left), cxListSize(right));
947 EXPECT_EQ(cxListCompare(left, right), 0); 941 EXPECT_EQ(cxListCompare(left, right), 0);
948 int a = 5, b = 10; 942 int a = 5, b = 10;
949 cxListInsert(left, 15, &a); 943 cxListInsert(left, 15, &a);
950 cxListInsert(right, 15, &b); 944 cxListInsert(right, 15, &b);
951 ASSERT_EQ(left->size, right->size); 945 ASSERT_EQ(cxListSize(left), cxListSize(right));
952 EXPECT_LT(cxListCompare(left, right), 0); 946 EXPECT_LT(cxListCompare(left, right), 0);
953 EXPECT_GT(cxListCompare(right, left), 0); 947 EXPECT_GT(cxListCompare(right, left), 0);
954 *(int *) cxListAt(left, 15) = 10; 948 *(int *) cxListAt(left, 15) = 10;
955 EXPECT_EQ(cxListCompare(left, right), 0); 949 EXPECT_EQ(cxListCompare(left, right), 0);
956 } 950 }
970 964
971 TEST_F(PointerLinkedList, cxListStorePointers) { 965 TEST_F(PointerLinkedList, cxListStorePointers) {
972 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, 47)); 966 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, 47));
973 EXPECT_FALSE(cxListIsStoringPointers(list)); 967 EXPECT_FALSE(cxListIsStoringPointers(list));
974 cxListStorePointers(list); 968 cxListStorePointers(list);
975 EXPECT_EQ(list->itemsize, sizeof(void *)); 969 EXPECT_EQ(list->item_size, sizeof(void *));
976 EXPECT_NE(list->cl, nullptr); 970 EXPECT_NE(list->cl, nullptr);
977 EXPECT_NE(list->climpl, nullptr); 971 EXPECT_NE(list->climpl, nullptr);
978 EXPECT_TRUE(cxListIsStoringPointers(list)); 972 EXPECT_TRUE(cxListIsStoringPointers(list));
979 cxListStoreObjects(list); 973 cxListStoreObjects(list);
980 EXPECT_NE(list->cl, nullptr); 974 EXPECT_NE(list->cl, nullptr);
983 } 977 }
984 978
985 TEST_F(LinkedList, cxLinkedListCreate) { 979 TEST_F(LinkedList, cxLinkedListCreate) {
986 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 980 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
987 ASSERT_NE(list, nullptr); 981 ASSERT_NE(list, nullptr);
988 EXPECT_EQ(list->itemsize, sizeof(int)); 982 EXPECT_EQ(list->item_size, sizeof(int));
989 EXPECT_EQ(list->capacity, (size_t) -1); 983 EXPECT_EQ(list->simple_destructor, nullptr);
990 EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE); 984 EXPECT_EQ(list->advanced_destructor, nullptr);
991 EXPECT_EQ(list->size, 0); 985 EXPECT_EQ(list->destructor_data, nullptr);
986 EXPECT_EQ(cxListSize(list), 0);
992 EXPECT_EQ(list->allocator, &testingAllocator); 987 EXPECT_EQ(list->allocator, &testingAllocator);
993 EXPECT_EQ(list->cmpfunc, cx_cmp_int); 988 EXPECT_EQ(list->cmpfunc, cx_cmp_int);
994 EXPECT_FALSE(cxListIsStoringPointers(list)); 989 EXPECT_FALSE(cxListIsStoringPointers(list));
995 } 990 }
996 991
997 TEST_F(LinkedList, cxLinkedListCreateSimple) { 992 TEST_F(LinkedList, cxLinkedListCreateSimple) {
998 CxList *list = autofree(cxLinkedListCreateSimple(sizeof(int))); 993 CxList *list = autofree(cxLinkedListCreateSimple(sizeof(int)));
999 ASSERT_NE(list, nullptr); 994 ASSERT_NE(list, nullptr);
1000 EXPECT_EQ(list->itemsize, sizeof(int)); 995 EXPECT_EQ(list->item_size, sizeof(int));
1001 EXPECT_EQ(list->capacity, (size_t) -1);
1002 EXPECT_EQ(list->cmpfunc, nullptr); 996 EXPECT_EQ(list->cmpfunc, nullptr);
1003 EXPECT_EQ(list->allocator, cxDefaultAllocator); 997 EXPECT_EQ(list->allocator, cxDefaultAllocator);
998 EXPECT_EQ(list->simple_destructor, nullptr);
999 EXPECT_EQ(list->advanced_destructor, nullptr);
1000 EXPECT_EQ(list->destructor_data, nullptr);
1001 EXPECT_EQ(cxListSize(list), 0);
1004 EXPECT_FALSE(cxListIsStoringPointers(list)); 1002 EXPECT_FALSE(cxListIsStoringPointers(list));
1005 } 1003 }
1006 1004
1007 TEST_F(LinkedList, cxLinkedListCreateSimpleForPointers) { 1005 TEST_F(LinkedList, cxLinkedListCreateSimpleForPointers) {
1008 CxList *list = autofree(cxLinkedListCreateSimple(CX_STORE_POINTERS)); 1006 CxList *list = autofree(cxLinkedListCreateSimple(CX_STORE_POINTERS));
1009 ASSERT_NE(list, nullptr); 1007 ASSERT_NE(list, nullptr);
1010 EXPECT_EQ(list->itemsize, sizeof(void *)); 1008 EXPECT_EQ(list->item_size, sizeof(void *));
1011 EXPECT_EQ(list->capacity, (size_t) -1);
1012 EXPECT_EQ(list->cmpfunc, nullptr); 1009 EXPECT_EQ(list->cmpfunc, nullptr);
1013 EXPECT_EQ(list->allocator, cxDefaultAllocator); 1010 EXPECT_EQ(list->allocator, cxDefaultAllocator);
1011 EXPECT_EQ(list->simple_destructor, nullptr);
1012 EXPECT_EQ(list->advanced_destructor, nullptr);
1013 EXPECT_EQ(list->destructor_data, nullptr);
1014 EXPECT_EQ(cxListSize(list), 0);
1014 EXPECT_TRUE(cxListIsStoringPointers(list)); 1015 EXPECT_TRUE(cxListIsStoringPointers(list));
1015 } 1016 }
1016 1017
1017 TEST_F(ArrayList, cxArrayListCreate) { 1018 TEST_F(ArrayList, cxArrayListCreate) {
1018 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8)); 1019 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
1019 ASSERT_NE(list, nullptr); 1020 ASSERT_NE(list, nullptr);
1020 EXPECT_EQ(list->itemsize, sizeof(int)); 1021 EXPECT_EQ(list->item_size, sizeof(int));
1021 EXPECT_EQ(list->capacity, 8); 1022 EXPECT_EQ(list->simple_destructor, nullptr);
1022 EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE); 1023 EXPECT_EQ(list->advanced_destructor, nullptr);
1023 EXPECT_EQ(list->size, 0); 1024 EXPECT_EQ(list->destructor_data, nullptr);
1025 EXPECT_EQ(cxListSize(list), 0);
1024 EXPECT_EQ(list->allocator, &testingAllocator); 1026 EXPECT_EQ(list->allocator, &testingAllocator);
1025 EXPECT_EQ(list->cmpfunc, cx_cmp_int); 1027 EXPECT_EQ(list->cmpfunc, cx_cmp_int);
1026 EXPECT_FALSE(cxListIsStoringPointers(list)); 1028 EXPECT_FALSE(cxListIsStoringPointers(list));
1027 } 1029 }
1028 1030
1029 TEST_F(ArrayList, cxArrayListCreateSimple) { 1031 TEST_F(ArrayList, cxArrayListCreateSimple) {
1030 CxList *list = autofree(cxArrayListCreateSimple(sizeof(int), 8)); 1032 CxList *list = autofree(cxArrayListCreateSimple(sizeof(int), 8));
1031 ASSERT_NE(list, nullptr); 1033 ASSERT_NE(list, nullptr);
1032 EXPECT_EQ(list->cmpfunc, nullptr); 1034 EXPECT_EQ(list->cmpfunc, nullptr);
1033 EXPECT_EQ(list->allocator, cxDefaultAllocator); 1035 EXPECT_EQ(list->allocator, cxDefaultAllocator);
1034 EXPECT_EQ(list->itemsize, sizeof(int)); 1036 EXPECT_EQ(list->item_size, sizeof(int));
1035 EXPECT_EQ(list->capacity, 8); 1037 EXPECT_EQ(list->simple_destructor, nullptr);
1038 EXPECT_EQ(list->advanced_destructor, nullptr);
1039 EXPECT_EQ(list->destructor_data, nullptr);
1040 EXPECT_EQ(cxListSize(list), 0);
1036 EXPECT_FALSE(cxListIsStoringPointers(list)); 1041 EXPECT_FALSE(cxListIsStoringPointers(list));
1037 } 1042 }
1038 1043
1039 TEST_F(ArrayList, cxArrayListCreateSimpleForPointers) { 1044 TEST_F(ArrayList, cxArrayListCreateSimpleForPointers) {
1040 CxList *list = autofree(cxArrayListCreateSimple(CX_STORE_POINTERS, 8)); 1045 CxList *list = autofree(cxArrayListCreateSimple(CX_STORE_POINTERS, 8));
1041 ASSERT_NE(list, nullptr); 1046 ASSERT_NE(list, nullptr);
1042 EXPECT_EQ(list->cmpfunc, nullptr); 1047 EXPECT_EQ(list->cmpfunc, nullptr);
1043 EXPECT_EQ(list->allocator, cxDefaultAllocator); 1048 EXPECT_EQ(list->allocator, cxDefaultAllocator);
1044 EXPECT_EQ(list->itemsize, sizeof(void *)); 1049 EXPECT_EQ(list->item_size, sizeof(void *));
1045 EXPECT_EQ(list->capacity, 8);
1046 EXPECT_TRUE(cxListIsStoringPointers(list)); 1050 EXPECT_TRUE(cxListIsStoringPointers(list));
1047 } 1051 }
1048 1052
1049 TEST_F(LinkedList, cxListAdd) { 1053 TEST_F(LinkedList, cxListAdd) {
1050 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 1054 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
1311 } 1315 }
1312 1316
1313 TEST_F(PointerLinkedList, DestroySimpleDestructor) { 1317 TEST_F(PointerLinkedList, DestroySimpleDestructor) {
1314 int item = 0; 1318 int item = 0;
1315 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 1319 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
1316 list->content_destructor_type = CX_DESTRUCTOR_SIMPLE;
1317 list->simple_destructor = [](void *elem) { *(int *) elem = 42; }; 1320 list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
1318 cxListAdd(list, &item); 1321 cxListAdd(list, &item);
1319 cxListDestroy(list); 1322 cxListDestroy(list);
1320 EXPECT_EQ(item, 42); 1323 EXPECT_EQ(item, 42);
1321 } 1324 }
1322 1325
1323 TEST_F(PointerLinkedList, DestroyAdvancedDestructor) { 1326 TEST_F(PointerLinkedList, DestroyAdvancedDestructor) {
1324 void *item = cxMalloc(&testingAllocator, sizeof(int)); 1327 void *item = cxMalloc(&testingAllocator, sizeof(int));
1325 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 1328 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
1326 list->content_destructor_type = CX_DESTRUCTOR_ADVANCED; 1329 list->destructor_data = &testingAllocator;
1327 list->advanced_destructor.data = &testingAllocator; 1330 list->advanced_destructor = (cx_destructor_func2) cxFree;
1328 list->advanced_destructor.func = (cx_destructor_func2) cxFree;
1329 cxListAdd(list, item); 1331 cxListAdd(list, item);
1330 ASSERT_FALSE(testingAllocator.verify()); 1332 ASSERT_FALSE(testingAllocator.verify());
1331 cxListDestroy(list); 1333 cxListDestroy(list);
1332 EXPECT_TRUE(testingAllocator.verify()); 1334 EXPECT_TRUE(testingAllocator.verify());
1333 } 1335 }

mercurial