test/test_list.cpp

changeset 639
309e8b08c60e
parent 638
eafb45eefc51
child 641
d402fead3386
equal deleted inserted replaced
638:eafb45eefc51 639:309e8b08c60e
167 auto testdata = create_nodes_test_data({2, 4, 6, 8}); 167 auto testdata = create_nodes_test_data({2, 4, 6, 8});
168 auto list = testdata.begin; 168 auto list = testdata.begin;
169 int s; 169 int s;
170 170
171 s = 2; 171 s = 2;
172 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 0); 172 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 0);
173 s = 4; 173 s = 4;
174 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 1); 174 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 1);
175 s = 6; 175 s = 6;
176 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 2); 176 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 2);
177 s = 8; 177 s = 8;
178 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 3); 178 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 3);
179 s = 10; 179 s = 10;
180 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 4); 180 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 4);
181 s = -2; 181 s = -2;
182 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 4); 182 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 4);
183 } 183 }
184 184
185 TEST(LinkedList_LowLevel, cx_linked_list_compare) { 185 TEST(LinkedList_LowLevel, cx_linked_list_compare) {
186 auto ta = create_nodes_test_data({2, 4, 6, 8}); 186 auto ta = create_nodes_test_data({2, 4, 6, 8});
187 auto tb = create_nodes_test_data({2, 4, 6}); 187 auto tb = create_nodes_test_data({2, 4, 6});
188 auto tc = create_nodes_test_data({2, 4, 6, 9}); 188 auto tc = create_nodes_test_data({2, 4, 6, 9});
189 auto la = ta.begin, lb = tb.begin, lc = tc.begin; 189 auto la = ta.begin, lb = tb.begin, lc = tc.begin;
190 190
191 EXPECT_GT(cx_linked_list_compare(la, lb, loc_next, loc_data, false, false, cx_cmp_int), 0); 191 EXPECT_GT(cx_linked_list_compare(la, lb, loc_next, loc_data, cx_cmp_int), 0);
192 EXPECT_LT(cx_linked_list_compare(lb, la, loc_next, loc_data, false, false, cx_cmp_int), 0); 192 EXPECT_LT(cx_linked_list_compare(lb, la, loc_next, loc_data, cx_cmp_int), 0);
193 EXPECT_GT(cx_linked_list_compare(lc, la, loc_next, loc_data, false, false, cx_cmp_int), 0); 193 EXPECT_GT(cx_linked_list_compare(lc, la, loc_next, loc_data, cx_cmp_int), 0);
194 EXPECT_LT(cx_linked_list_compare(la, lc, loc_next, loc_data, false, false, cx_cmp_int), 0); 194 EXPECT_LT(cx_linked_list_compare(la, lc, loc_next, loc_data, cx_cmp_int), 0);
195 EXPECT_EQ(cx_linked_list_compare(la, la, loc_next, loc_data, false, false, cx_cmp_int), 0); 195 EXPECT_EQ(cx_linked_list_compare(la, la, loc_next, loc_data, cx_cmp_int), 0);
196 } 196 }
197 197
198 TEST(LinkedList_LowLevel, cx_linked_list_add) { 198 TEST(LinkedList_LowLevel, cx_linked_list_add) {
199 // test with begin, end / prev, next 199 // test with begin, end / prev, next
200 { 200 {
516 516
517 auto scrambled = create_nodes_test_data(testdata.data.begin(), testdata.data.end()); 517 auto scrambled = create_nodes_test_data(testdata.data.begin(), testdata.data.end());
518 void *begin = scrambled.begin; 518 void *begin = scrambled.begin;
519 void *end = cx_linked_list_last(begin, loc_next); 519 void *end = cx_linked_list_last(begin, loc_next);
520 520
521 cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data, 521 cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data, cx_cmp_int);
522 false, cx_cmp_int);
523 522
524 node *check = reinterpret_cast<node *>(begin); 523 node *check = reinterpret_cast<node *>(begin);
525 node *check_last = nullptr; 524 node *check_last = nullptr;
526 cx_for_n (i, sorted.size()) { 525 cx_for_n (i, sorted.size()) {
527 EXPECT_EQ(check->data, sorted[i]); 526 EXPECT_EQ(check->data, sorted[i]);
545 auto orig_begin = begin, orig_end = end; 544 auto orig_begin = begin, orig_end = end;
546 545
547 cx_linked_list_reverse(&begin, &end, loc_prev, loc_next); 546 cx_linked_list_reverse(&begin, &end, loc_prev, loc_next);
548 EXPECT_EQ(end, orig_begin); 547 EXPECT_EQ(end, orig_begin);
549 EXPECT_EQ(begin, orig_end); 548 EXPECT_EQ(begin, orig_end);
550 EXPECT_EQ(cx_linked_list_compare(begin, expected.begin, loc_next, loc_data, false, false, cx_cmp_int), 0); 549 EXPECT_EQ(cx_linked_list_compare(begin, expected.begin, loc_next, loc_data, cx_cmp_int), 0);
551 } 550 }
552 551
553 class HighLevelTest : public ::testing::Test { 552 class HighLevelTest : public ::testing::Test {
554 mutable std::unordered_set<CxList *> lists; 553 mutable std::unordered_set<CxList *> lists;
555 protected: 554 protected:
569 } 568 }
570 569
571 auto linkedListFromTestData() const -> CxList * { 570 auto linkedListFromTestData() const -> CxList * {
572 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 571 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
573 cxListAddArray(list, testdata.data.data(), testdata_len); 572 cxListAddArray(list, testdata.data.data(), testdata_len);
574 return list;
575 }
576
577 auto pointerLinkedListFromTestData() const -> CxList * {
578 auto list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
579 // note: cannot use cxListAddArray() because we don't have a list of pointers
580 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
581 return list; 573 return list;
582 } 574 }
583 575
584 auto arrayListFromTestData() const -> CxList * { 576 auto arrayListFromTestData() const -> CxList * {
585 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), testdata_len)); 577 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), testdata_len));
793 }; 785 };
794 786
795 class LinkedList : public HighLevelTest { 787 class LinkedList : public HighLevelTest {
796 }; 788 };
797 789
798 class PointerLinkedList : public HighLevelTest {
799 };
800
801 class ArrayList : public HighLevelTest { 790 class ArrayList : public HighLevelTest {
802 }; 791 };
803 792
804 TEST_F(LinkedList, cxLinkedListCreate) { 793 TEST_F(LinkedList, cxLinkedListCreate) {
805 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 794 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
807 EXPECT_EQ(list->itemsize, sizeof(int)); 796 EXPECT_EQ(list->itemsize, sizeof(int));
808 EXPECT_EQ(list->capacity, (size_t) -1); 797 EXPECT_EQ(list->capacity, (size_t) -1);
809 verifyCreate(list); 798 verifyCreate(list);
810 } 799 }
811 800
812 TEST_F(PointerLinkedList, cxPointerLinkedListCreate) {
813 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
814 ASSERT_NE(list, nullptr);
815 EXPECT_EQ(list->itemsize, sizeof(void *));
816 EXPECT_EQ(list->capacity, (size_t) -1);
817 verifyCreate(list);
818 }
819
820 TEST_F(ArrayList, cxArrayListCreate) { 801 TEST_F(ArrayList, cxArrayListCreate) {
821 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8)); 802 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
822 ASSERT_NE(list, nullptr); 803 ASSERT_NE(list, nullptr);
823 EXPECT_EQ(list->itemsize, sizeof(int)); 804 EXPECT_EQ(list->itemsize, sizeof(int));
824 EXPECT_EQ(list->capacity, 8); 805 EXPECT_EQ(list->capacity, 8);
828 TEST_F(LinkedList, cxListAdd) { 809 TEST_F(LinkedList, cxListAdd) {
829 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 810 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
830 verifyAdd(list, false); 811 verifyAdd(list, false);
831 } 812 }
832 813
833 TEST_F(PointerLinkedList, cxListAdd) {
834 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
835 verifyAdd(list, true);
836 }
837
838 TEST_F(ArrayList, cxListAdd) { 814 TEST_F(ArrayList, cxListAdd) {
839 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8)); 815 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
840 verifyAdd(list, false); 816 verifyAdd(list, false);
841 } 817 }
842 818
843 TEST_F(LinkedList, cxListInsert) { 819 TEST_F(LinkedList, cxListInsert) {
844 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); 820 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
845 } 821 }
846 822
847 TEST_F(PointerLinkedList, cxListInsert) {
848 verifyInsert(autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)));
849 }
850
851 TEST_F(ArrayList, cxListInsert) { 823 TEST_F(ArrayList, cxListInsert) {
852 verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2))); 824 verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2)));
853 } 825 }
854 826
855 TEST_F(LinkedList, cxListInsertArray) { 827 TEST_F(LinkedList, cxListInsertArray) {
856 verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); 828 verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
857 } 829 }
858 830
859 TEST_F(PointerLinkedList, cxListInsertArray) {
860 //TODO: this is unfixably broken - solve with issue #234
861 //verifyInsertArray(autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)));
862 }
863
864 TEST_F(ArrayList, cxListInsertArray) { 831 TEST_F(ArrayList, cxListInsertArray) {
865 verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4))); 832 verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)));
866 } 833 }
867 834
868 TEST_F(LinkedList, cxListRemove) { 835 TEST_F(LinkedList, cxListRemove) {
869 verifyRemove(linkedListFromTestData()); 836 verifyRemove(linkedListFromTestData());
870 } 837 }
871 838
872 TEST_F(PointerLinkedList, cxListRemove) {
873 verifyRemove(pointerLinkedListFromTestData());
874 }
875
876 TEST_F(ArrayList, cxListRemove) { 839 TEST_F(ArrayList, cxListRemove) {
877 verifyRemove(arrayListFromTestData()); 840 verifyRemove(arrayListFromTestData());
878 } 841 }
879 842
880 TEST_F(LinkedList, cxListAt) { 843 TEST_F(LinkedList, cxListAt) {
881 verifyAt(linkedListFromTestData()); 844 verifyAt(linkedListFromTestData());
882 } 845 }
883 846
884 TEST_F(PointerLinkedList, cxListAt) {
885 verifyAt(pointerLinkedListFromTestData());
886 }
887
888 TEST_F(ArrayList, cxListAt) { 847 TEST_F(ArrayList, cxListAt) {
889 verifyAt(arrayListFromTestData()); 848 verifyAt(arrayListFromTestData());
890 } 849 }
891 850
892 TEST_F(LinkedList, cxListFind) { 851 TEST_F(LinkedList, cxListFind) {
893 verifyFind(linkedListFromTestData()); 852 verifyFind(linkedListFromTestData());
894 } 853 }
895 854
896 TEST_F(PointerLinkedList, cxListFind) {
897 verifyFind(pointerLinkedListFromTestData());
898 }
899
900 TEST_F(ArrayList, cxListFind) { 855 TEST_F(ArrayList, cxListFind) {
901 verifyFind(arrayListFromTestData()); 856 verifyFind(arrayListFromTestData());
902 } 857 }
903 858
904 TEST_F(LinkedList, cxListSort) { 859 TEST_F(LinkedList, cxListSort) {
905 verifySort(linkedListFromTestData()); 860 verifySort(linkedListFromTestData());
906 } 861 }
907 862
908 TEST_F(PointerLinkedList, cxListSort) {
909 verifySort(pointerLinkedListFromTestData());
910 }
911
912 TEST_F(ArrayList, cxListSort) { 863 TEST_F(ArrayList, cxListSort) {
913 verifySort(arrayListFromTestData()); 864 verifySort(arrayListFromTestData());
914 } 865 }
915 866
916 TEST_F(LinkedList, Iterator) { 867 TEST_F(LinkedList, Iterator) {
917 verifyIterator(linkedListFromTestData()); 868 verifyIterator(linkedListFromTestData());
918 }
919
920 TEST_F(PointerLinkedList, Iterator) {
921 verifyIterator(pointerLinkedListFromTestData());
922 } 869 }
923 870
924 TEST_F(ArrayList, Iterator) { 871 TEST_F(ArrayList, Iterator) {
925 verifyIterator(arrayListFromTestData()); 872 verifyIterator(arrayListFromTestData());
926 } 873 }
930 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 877 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
931 cxListAddArray(list, fivenums, 5); 878 cxListAddArray(list, fivenums, 5);
932 verifyInsertViaIterator(list); 879 verifyInsertViaIterator(list);
933 } 880 }
934 881
935 TEST_F(PointerLinkedList, InsertViaIterator) {
936 int fivenums[] = {0, 1, 2, 3, 4, 5};
937 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
938 // note: don't use cxListAddArray() here, because we don't have a list of pointers
939 cx_for_n (i, 5) cxListAdd(list, &fivenums[i]);
940 verifyInsertViaIterator(list);
941 }
942
943 TEST_F(ArrayList, InsertViaIterator) { 882 TEST_F(ArrayList, InsertViaIterator) {
944 int fivenums[] = {0, 1, 2, 3, 4, 5}; 883 int fivenums[] = {0, 1, 2, 3, 4, 5};
945 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)); 884 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4));
946 cxListAddArray(list, fivenums, 5); 885 cxListAddArray(list, fivenums, 5);
947 verifyInsertViaIterator(list); 886 verifyInsertViaIterator(list);
949 888
950 TEST_F(LinkedList, cxListReverse) { 889 TEST_F(LinkedList, cxListReverse) {
951 verifyReverse(linkedListFromTestData()); 890 verifyReverse(linkedListFromTestData());
952 } 891 }
953 892
954 TEST_F(PointerLinkedList, cxListReverse) {
955 verifyReverse(pointerLinkedListFromTestData());
956 }
957
958 TEST_F(ArrayList, cxListReverse) { 893 TEST_F(ArrayList, cxListReverse) {
959 verifyReverse(arrayListFromTestData()); 894 verifyReverse(arrayListFromTestData());
960 } 895 }
961 896
962 TEST_F(LinkedList, cxListCompare) { 897 TEST_F(LinkedList, cxListCompare) {
963 auto left = linkedListFromTestData(); 898 auto left = linkedListFromTestData();
964 auto right = linkedListFromTestData(); 899 auto right = linkedListFromTestData();
965 verifyCompare(left, right); 900 verifyCompare(left, right);
966 } 901 }
967 902
968 TEST_F(LinkedList, cxListCompareWithPtrList) {
969 auto left = linkedListFromTestData();
970 auto right = pointerLinkedListFromTestData();
971 verifyCompare(left, right);
972 }
973
974 TEST_F(LinkedList, cxListCompareWithArrayList) { 903 TEST_F(LinkedList, cxListCompareWithArrayList) {
975 auto left = linkedListFromTestData(); 904 auto left = linkedListFromTestData();
976 auto right = arrayListFromTestData(); 905 auto right = arrayListFromTestData();
977 verifyCompare(left, right); 906 verifyCompare(left, right);
978 } 907 }
979 908
980 TEST_F(PointerLinkedList, cxListCompare) {
981 auto left = pointerLinkedListFromTestData();
982 auto right = pointerLinkedListFromTestData();
983 verifyCompare(left, right);
984 }
985
986 TEST_F(PointerLinkedList, cxListCompareWithNormalList) {
987 auto left = pointerLinkedListFromTestData();
988 auto right = linkedListFromTestData();
989 verifyCompare(left, right);
990 }
991
992 TEST_F(PointerLinkedList, cxListCompareWithArrayList) {
993 auto left = pointerLinkedListFromTestData();
994 auto right = arrayListFromTestData();
995 verifyCompare(left, right);
996 }
997
998 TEST_F(ArrayList, cxListCompare) { 909 TEST_F(ArrayList, cxListCompare) {
999 auto left = arrayListFromTestData(); 910 auto left = arrayListFromTestData();
1000 auto right = arrayListFromTestData(); 911 auto right = arrayListFromTestData();
1001 verifyCompare(left, right); 912 verifyCompare(left, right);
1002 } 913 }
1003 914
1004 TEST_F(ArrayList, cxListCompareWithPtrList) { 915 TEST_F(ArrayList, cxListCompareWithLinkedList) {
1005 auto left = arrayListFromTestData();
1006 auto right = pointerLinkedListFromTestData();
1007 verifyCompare(left, right);
1008 }
1009
1010 TEST_F(ArrayList, cxListCompareWithNormalList) {
1011 auto left = arrayListFromTestData(); 916 auto left = arrayListFromTestData();
1012 auto right = linkedListFromTestData(); 917 auto right = linkedListFromTestData();
1013 verifyCompare(left, right); 918 verifyCompare(left, right);
1014 } 919 }
1015
1016 TEST_F(PointerLinkedList, NoDestructor) {
1017 void *item = cxMalloc(&testingAllocator, sizeof(int));
1018 auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_int);
1019 cxListAdd(list, item);
1020 ASSERT_FALSE(testingAllocator.verify());
1021 cxListDestroy(list);
1022 EXPECT_FALSE(testingAllocator.verify());
1023 cxFree(&testingAllocator, item);
1024 EXPECT_TRUE(testingAllocator.verify());
1025 }
1026
1027 TEST_F(PointerLinkedList, SimpleDestructor) {
1028 int item = 0;
1029 auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_int);
1030 list->content_destructor_type = CX_DESTRUCTOR_SIMPLE;
1031 list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
1032 cxListAdd(list, &item);
1033 cxListDestroy(list);
1034 EXPECT_EQ(item, 42);
1035 }
1036
1037 TEST_F(PointerLinkedList, AdvancedDestructor) {
1038 void *item = cxMalloc(&testingAllocator, sizeof(int));
1039 auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_int);
1040 list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;
1041 list->advanced_destructor.data = &testingAllocator;
1042 list->advanced_destructor.func = (cx_destructor_func2) cxFree;
1043 cxListAdd(list, item);
1044 ASSERT_FALSE(testingAllocator.verify());
1045 cxListDestroy(list);
1046 EXPECT_TRUE(testingAllocator.verify());
1047 }

mercurial