tests/test_list.cpp

changeset 666
b5dd654deb3b
parent 662
d0d95740071b
child 667
2f88a7c13a28
equal deleted inserted replaced
665:c4041b07165e 666:b5dd654deb3b
36 #include <array> 36 #include <array>
37 #include <vector> 37 #include <vector>
38 #include <unordered_set> 38 #include <unordered_set>
39 #include <algorithm> 39 #include <algorithm>
40 40
41 struct testdatastruct {
42 int x;
43 void *ptr;
44 };
45
46 static void free_testdatastruct(
47 void *a,
48 void *s
49 ) {
50 auto al = reinterpret_cast<CxTestingAllocator *>(a);
51 cxFree(al, reinterpret_cast<testdatastruct *>(s)->ptr);
52 }
53
41 struct node { 54 struct node {
42 node *next = nullptr; 55 node *next = nullptr;
43 node *prev = nullptr; 56 node *prev = nullptr;
44 int data = 0; 57 int data = 0;
45 }; 58 };
693 EXPECT_GE(list->capacity, list->size); 706 EXPECT_GE(list->capacity, list->size);
694 EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[1]); 707 EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[1]);
695 EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[3]); 708 EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[3]);
696 709
697 EXPECT_NE(cxListRemove(list, testdata_len), 0); 710 EXPECT_NE(cxListRemove(list, testdata_len), 0);
711 }
712
713 void verifyClear(CxList *list) {
714 // use the testing allocator for testing the destructor function
715 list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;
716 list->advanced_destructor.func = free_testdatastruct;
717 list->advanced_destructor.data = &testingAllocator;
718
719 testdatastruct s[10];
720 for (auto &t: s) {
721 t.ptr = cxMalloc(&testingAllocator, 16);
722 cxListAdd(list, &t);
723 }
724
725 cxListClear(list);
698 } 726 }
699 727
700 static void verifySwap(CxList *list) { 728 static void verifySwap(CxList *list) {
701 ASSERT_EQ(list->size, 0); 729 ASSERT_EQ(list->size, 0);
702 730
995 1023
996 TEST_F(ArrayList, cxListRemove) { 1024 TEST_F(ArrayList, cxListRemove) {
997 verifyRemove(arrayListFromTestData()); 1025 verifyRemove(arrayListFromTestData());
998 } 1026 }
999 1027
1028 TEST_F(LinkedList, cxListClear) {
1029 verifyClear(autofree(cxLinkedListCreateSimple(sizeof(testdatastruct))));
1030 }
1031
1032 TEST_F(PointerLinkedList, cxListClear) {
1033 auto l = cxLinkedListCreateSimple(sizeof(testdatastruct));
1034 cxListStorePointers(l);
1035 verifyClear(autofree(l));
1036 }
1037
1038 TEST_F(ArrayList, cxListClear) {
1039 verifyClear(autofree(cxArrayListCreateSimple(sizeof(testdatastruct), 8)));
1040 }
1041
1000 TEST_F(LinkedList, cxListSwap) { 1042 TEST_F(LinkedList, cxListSwap) {
1001 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); 1043 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
1002 } 1044 }
1003 1045
1004 TEST_F(PointerLinkedList, cxListSwap) { 1046 TEST_F(PointerLinkedList, cxListSwap) {

mercurial