| 175 auto testdata = create_nodes_test_data({2, 4, 6, 8}); |
166 auto testdata = create_nodes_test_data({2, 4, 6, 8}); |
| 176 auto list = testdata.begin; |
167 auto list = testdata.begin; |
| 177 int s; |
168 int s; |
| 178 |
169 |
| 179 s = 2; |
170 s = 2; |
| 180 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cmp_int, &s), 0); |
171 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 0); |
| 181 s = 4; |
172 s = 4; |
| 182 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cmp_int, &s), 1); |
173 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 1); |
| 183 s = 6; |
174 s = 6; |
| 184 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cmp_int, &s), 2); |
175 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 2); |
| 185 s = 8; |
176 s = 8; |
| 186 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cmp_int, &s), 3); |
177 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 3); |
| 187 s = 10; |
178 s = 10; |
| 188 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cmp_int, &s), 4); |
179 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 4); |
| 189 s = -2; |
180 s = -2; |
| 190 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cmp_int, &s), 4); |
181 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 4); |
| 191 } |
182 } |
| 192 |
183 |
| 193 TEST(LinkedList_LowLevel, cx_linked_list_compare) { |
184 TEST(LinkedList_LowLevel, cx_linked_list_compare) { |
| 194 auto ta = create_nodes_test_data({2, 4, 6, 8}); |
185 auto ta = create_nodes_test_data({2, 4, 6, 8}); |
| 195 auto tb = create_nodes_test_data({2, 4, 6}); |
186 auto tb = create_nodes_test_data({2, 4, 6}); |
| 196 auto tc = create_nodes_test_data({2, 4, 6, 9}); |
187 auto tc = create_nodes_test_data({2, 4, 6, 9}); |
| 197 auto la = ta.begin, lb = tb.begin, lc = tc.begin; |
188 auto la = ta.begin, lb = tb.begin, lc = tc.begin; |
| 198 |
189 |
| 199 EXPECT_GT(cx_linked_list_compare(la, lb, loc_next, loc_data, false, false, cmp_int), 0); |
190 EXPECT_GT(cx_linked_list_compare(la, lb, loc_next, loc_data, false, false, cx_cmp_int), 0); |
| 200 EXPECT_LT(cx_linked_list_compare(lb, la, loc_next, loc_data, false, false, cmp_int), 0); |
191 EXPECT_LT(cx_linked_list_compare(lb, la, loc_next, loc_data, false, false, cx_cmp_int), 0); |
| 201 EXPECT_GT(cx_linked_list_compare(lc, la, loc_next, loc_data, false, false, cmp_int), 0); |
192 EXPECT_GT(cx_linked_list_compare(lc, la, loc_next, loc_data, false, false, cx_cmp_int), 0); |
| 202 EXPECT_LT(cx_linked_list_compare(la, lc, loc_next, loc_data, false, false, cmp_int), 0); |
193 EXPECT_LT(cx_linked_list_compare(la, lc, loc_next, loc_data, false, false, cx_cmp_int), 0); |
| 203 EXPECT_EQ(cx_linked_list_compare(la, la, loc_next, loc_data, false, false, cmp_int), 0); |
194 EXPECT_EQ(cx_linked_list_compare(la, la, loc_next, loc_data, false, false, cx_cmp_int), 0); |
| 204 } |
195 } |
| 205 |
196 |
| 206 TEST(LinkedList_LowLevel, cx_linked_list_add) { |
197 TEST(LinkedList_LowLevel, cx_linked_list_add) { |
| 207 // test with begin, end / prev, next |
198 // test with begin, end / prev, next |
| 208 { |
199 { |
| 525 auto scrambled = create_nodes_test_data(testdata.data.begin(), testdata.data.end()); |
516 auto scrambled = create_nodes_test_data(testdata.data.begin(), testdata.data.end()); |
| 526 void *begin = scrambled.begin; |
517 void *begin = scrambled.begin; |
| 527 void *end = cx_linked_list_last(begin, loc_next); |
518 void *end = cx_linked_list_last(begin, loc_next); |
| 528 |
519 |
| 529 cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data, |
520 cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data, |
| 530 false, cmp_int); |
521 false, cx_cmp_int); |
| 531 |
522 |
| 532 node *check = reinterpret_cast<node *>(begin); |
523 node *check = reinterpret_cast<node *>(begin); |
| 533 node *check_last = nullptr; |
524 node *check_last = nullptr; |
| 534 cx_for_n (i, sorted.size()) { |
525 cx_for_n (i, sorted.size()) { |
| 535 EXPECT_EQ(check->data, sorted[i]); |
526 EXPECT_EQ(check->data, sorted[i]); |
| 553 auto orig_begin = begin, orig_end = end; |
544 auto orig_begin = begin, orig_end = end; |
| 554 |
545 |
| 555 cx_linked_list_reverse(&begin, &end, loc_prev, loc_next); |
546 cx_linked_list_reverse(&begin, &end, loc_prev, loc_next); |
| 556 EXPECT_EQ(end, orig_begin); |
547 EXPECT_EQ(end, orig_begin); |
| 557 EXPECT_EQ(begin, orig_end); |
548 EXPECT_EQ(begin, orig_end); |
| 558 EXPECT_EQ(cx_linked_list_compare(begin, expected.begin, loc_next, loc_data, false, false, cmp_int), 0); |
549 EXPECT_EQ(cx_linked_list_compare(begin, expected.begin, loc_next, loc_data, false, false, cx_cmp_int), 0); |
| 559 } |
550 } |
| 560 |
551 |
| 561 class HighLevelTest : public ::testing::Test { |
552 class HighLevelTest : public ::testing::Test { |
| 562 mutable std::unordered_set<CxList *> lists; |
553 mutable std::unordered_set<CxList *> lists; |
| 563 protected: |
554 protected: |
| 578 |
569 |
| 579 auto linkedListFromTestData() const -> CxList * { |
570 auto linkedListFromTestData() const -> CxList * { |
| 580 return autofree( |
571 return autofree( |
| 581 cxLinkedListFromArray( |
572 cxLinkedListFromArray( |
| 582 &testingAllocator, |
573 &testingAllocator, |
| 583 cmp_int, |
574 cx_cmp_int, |
| 584 sizeof(int), |
575 sizeof(int), |
| 585 testdata_len, |
576 testdata_len, |
| 586 testdata.data.data() |
577 testdata.data.data() |
| 587 ) |
578 ) |
| 588 ); |
579 ); |
| 589 } |
580 } |
| 590 |
581 |
| 591 auto pointerLinkedListFromTestData() const -> CxList * { |
582 auto pointerLinkedListFromTestData() const -> CxList * { |
| 592 auto list = autofree(cxPointerLinkedListCreate(&testingAllocator, cmp_int)); |
583 auto list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)); |
| 593 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]); |
584 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]); |
| 594 return list; |
585 return list; |
| 595 } |
586 } |
| 596 |
587 |
| 597 void verifyCreate(CxList *list) const { |
588 void verifyCreate(CxList *list) const { |
| 598 EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE); |
589 EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE); |
| 599 EXPECT_EQ(list->size, 0); |
590 EXPECT_EQ(list->size, 0); |
| 600 EXPECT_EQ(list->capacity, (size_t) -1); |
591 EXPECT_EQ(list->capacity, (size_t) -1); |
| 601 EXPECT_EQ(list->allocator, &testingAllocator); |
592 EXPECT_EQ(list->allocator, &testingAllocator); |
| 602 EXPECT_EQ(list->cmpfunc, cmp_int); |
593 EXPECT_EQ(list->cmpfunc, cx_cmp_int); |
| 603 } |
594 } |
| 604 |
595 |
| 605 void verifyAdd( |
596 void verifyAdd( |
| 606 CxList *list, |
597 CxList *list, |
| 607 bool write_through |
598 bool write_through |
| 777 |
768 |
| 778 class PointerLinkedList : public HighLevelTest { |
769 class PointerLinkedList : public HighLevelTest { |
| 779 }; |
770 }; |
| 780 |
771 |
| 781 TEST_F(LinkedList, cxLinkedListCreate) { |
772 TEST_F(LinkedList, cxLinkedListCreate) { |
| 782 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cmp_int, sizeof(int))); |
773 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); |
| 783 EXPECT_EQ(list->itemsize, sizeof(int)); |
774 EXPECT_EQ(list->itemsize, sizeof(int)); |
| 784 verifyCreate(list); |
775 verifyCreate(list); |
| 785 } |
776 } |
| 786 |
777 |
| 787 TEST_F(PointerLinkedList, cxPointerLinkedListCreate) { |
778 TEST_F(PointerLinkedList, cxPointerLinkedListCreate) { |
| 788 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cmp_int)); |
779 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)); |
| 789 EXPECT_EQ(list->itemsize, sizeof(void *)); |
780 EXPECT_EQ(list->itemsize, sizeof(void *)); |
| 790 verifyCreate(list); |
781 verifyCreate(list); |
| 791 } |
782 } |
| 792 |
783 |
| 793 TEST_F(LinkedList, cxLinkedListFromArray) { |
784 TEST_F(LinkedList, cxLinkedListFromArray) { |
| 794 CxList *expected = autofree(cxLinkedListCreate(&testingAllocator, cmp_int, sizeof(int))); |
785 CxList *expected = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); |
| 795 cx_for_n (i, testdata_len) cxListAdd(expected, &testdata.data[i]); |
786 cx_for_n (i, testdata_len) cxListAdd(expected, &testdata.data[i]); |
| 796 CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cmp_int, sizeof(int), |
787 CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cx_cmp_int, sizeof(int), |
| 797 testdata_len, testdata.data.data())); |
788 testdata_len, testdata.data.data())); |
| 798 EXPECT_EQ(cxListCompare(list, expected), 0); |
789 EXPECT_EQ(cxListCompare(list, expected), 0); |
| 799 } |
790 } |
| 800 |
791 |
| 801 TEST_F(LinkedList, cxListAdd) { |
792 TEST_F(LinkedList, cxListAdd) { |
| 802 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cmp_int, sizeof(int))); |
793 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); |
| 803 verifyAdd(list, false); |
794 verifyAdd(list, false); |
| 804 } |
795 } |
| 805 |
796 |
| 806 TEST_F(PointerLinkedList, cxListAdd) { |
797 TEST_F(PointerLinkedList, cxListAdd) { |
| 807 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cmp_int)); |
798 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)); |
| 808 verifyAdd(list, true); |
799 verifyAdd(list, true); |
| 809 } |
800 } |
| 810 |
801 |
| 811 TEST_F(LinkedList, cxListInsert) { |
802 TEST_F(LinkedList, cxListInsert) { |
| 812 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cmp_int, sizeof(int)))); |
803 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); |
| 813 } |
804 } |
| 814 |
805 |
| 815 TEST_F(PointerLinkedList, cxListInsert) { |
806 TEST_F(PointerLinkedList, cxListInsert) { |
| 816 verifyInsert(autofree(cxPointerLinkedListCreate(&testingAllocator, cmp_int))); |
807 verifyInsert(autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int))); |
| 817 } |
808 } |
| 818 |
809 |
| 819 TEST_F(LinkedList, cxListRemove) { |
810 TEST_F(LinkedList, cxListRemove) { |
| 820 verifyRemove(linkedListFromTestData()); |
811 verifyRemove(linkedListFromTestData()); |
| 821 } |
812 } |
| 856 verifyIterator(pointerLinkedListFromTestData()); |
847 verifyIterator(pointerLinkedListFromTestData()); |
| 857 } |
848 } |
| 858 |
849 |
| 859 TEST_F(LinkedList, InsertViaIterator) { |
850 TEST_F(LinkedList, InsertViaIterator) { |
| 860 int fivenums[] = {0, 1, 2, 3, 4, 5}; |
851 int fivenums[] = {0, 1, 2, 3, 4, 5}; |
| 861 CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cmp_int, sizeof(int), 5, fivenums)); |
852 CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cx_cmp_int, sizeof(int), 5, fivenums)); |
| 862 verifyInsertViaIterator(list); |
853 verifyInsertViaIterator(list); |
| 863 } |
854 } |
| 864 |
855 |
| 865 TEST_F(PointerLinkedList, InsertViaIterator) { |
856 TEST_F(PointerLinkedList, InsertViaIterator) { |
| 866 int fivenums[] = {0, 1, 2, 3, 4, 5}; |
857 int fivenums[] = {0, 1, 2, 3, 4, 5}; |
| 867 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cmp_int)); |
858 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)); |
| 868 cx_for_n (i, 5) cxListAdd(list, &fivenums[i]); |
859 cx_for_n (i, 5) cxListAdd(list, &fivenums[i]); |
| 869 verifyInsertViaIterator(list); |
860 verifyInsertViaIterator(list); |
| 870 } |
861 } |
| 871 |
862 |
| 872 TEST_F(LinkedList, cxListReverse) { |
863 TEST_F(LinkedList, cxListReverse) { |
| 901 verifyCompare(left, right); |
892 verifyCompare(left, right); |
| 902 } |
893 } |
| 903 |
894 |
| 904 TEST_F(PointerLinkedList, NoDestructor) { |
895 TEST_F(PointerLinkedList, NoDestructor) { |
| 905 void *item = cxMalloc(&testingAllocator, sizeof(int)); |
896 void *item = cxMalloc(&testingAllocator, sizeof(int)); |
| 906 auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cmp_int); |
897 auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_int); |
| 907 cxListAdd(list, item); |
898 cxListAdd(list, item); |
| 908 ASSERT_FALSE(testingAllocator.verify()); |
899 ASSERT_FALSE(testingAllocator.verify()); |
| 909 cxListDestroy(list); |
900 cxListDestroy(list); |
| 910 EXPECT_FALSE(testingAllocator.verify()); |
901 EXPECT_FALSE(testingAllocator.verify()); |
| 911 cxFree(&testingAllocator, item); |
902 cxFree(&testingAllocator, item); |
| 912 EXPECT_TRUE(testingAllocator.verify()); |
903 EXPECT_TRUE(testingAllocator.verify()); |
| 913 } |
904 } |
| 914 |
905 |
| 915 TEST_F(PointerLinkedList, SimpleDestructor) { |
906 TEST_F(PointerLinkedList, SimpleDestructor) { |
| 916 int item = 0; |
907 int item = 0; |
| 917 auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cmp_int); |
908 auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_int); |
| 918 list->content_destructor_type = CX_DESTRUCTOR_SIMPLE; |
909 list->content_destructor_type = CX_DESTRUCTOR_SIMPLE; |
| 919 list->simple_destructor = [](void *elem) { *(int *) elem = 42; }; |
910 list->simple_destructor = [](void *elem) { *(int *) elem = 42; }; |
| 920 cxListAdd(list, &item); |
911 cxListAdd(list, &item); |
| 921 cxListDestroy(list); |
912 cxListDestroy(list); |
| 922 EXPECT_EQ(item, 42); |
913 EXPECT_EQ(item, 42); |
| 923 } |
914 } |
| 924 |
915 |
| 925 TEST_F(PointerLinkedList, AdvancedDestructor) { |
916 TEST_F(PointerLinkedList, AdvancedDestructor) { |
| 926 void *item = cxMalloc(&testingAllocator, sizeof(int)); |
917 void *item = cxMalloc(&testingAllocator, sizeof(int)); |
| 927 auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cmp_int); |
918 auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_int); |
| 928 list->content_destructor_type = CX_DESTRUCTOR_ADVANCED; |
919 list->content_destructor_type = CX_DESTRUCTOR_ADVANCED; |
| 929 list->advanced_destructor.data = &testingAllocator; |
920 list->advanced_destructor.data = &testingAllocator; |
| 930 list->advanced_destructor.func = (cx_destructor_func2) cxFree; |
921 list->advanced_destructor.func = (cx_destructor_func2) cxFree; |
| 931 cxListAdd(list, item); |
922 cxListAdd(list, item); |
| 932 ASSERT_FALSE(testingAllocator.verify()); |
923 ASSERT_FALSE(testingAllocator.verify()); |