# HG changeset patch # User Mike Becker # Date 1680342708 -7200 # Node ID 55d8fdd38ca4c01bde93ab196ca94cc7ed2bc8d1 # Parent d7a67375a7ac24cab65a46f11f58c293c43da37d increase test coverage - fixes #250 diff -r d7a67375a7ac -r 55d8fdd38ca4 tests/test_list.cpp --- a/tests/test_list.cpp Sat Apr 01 11:00:59 2023 +0200 +++ b/tests/test_list.cpp Sat Apr 01 11:51:48 2023 +0200 @@ -38,19 +38,6 @@ #include #include -struct testdatastruct { - int x; - void *ptr; -}; - -static void free_testdatastruct( - void *a, - void *s -) { - auto al = reinterpret_cast(a); - cxFree(al, reinterpret_cast(s)->ptr); -} - struct node { node *next = nullptr; node *prev = nullptr; @@ -599,13 +586,6 @@ return list; } - void verifyCreate(CxList *list) const { - EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE); - EXPECT_EQ(list->size, 0); - EXPECT_EQ(list->allocator, &testingAllocator); - EXPECT_EQ(list->cmpfunc, cx_cmp_int); - } - void verifyAdd( CxList *list, bool as_pointer @@ -709,19 +689,81 @@ EXPECT_NE(cxListRemove(list, testdata_len), 0); } - void verifyClear(CxList *list) { - // use the testing allocator for testing the destructor function - list->content_destructor_type = CX_DESTRUCTOR_ADVANCED; - list->advanced_destructor.func = free_testdatastruct; - list->advanced_destructor.data = &testingAllocator; + static void verifyClear(CxList *list) { + cxListClear(list); + EXPECT_EQ(0, list->size); + } + + static unsigned destr_test_ctr; + static int destr_last_value; + + static void simple_destr_test_fun(void *data) { + auto ptr = (int *) data; + destr_last_value = *ptr; + *ptr = destr_last_value + 1; + destr_test_ctr++; + } + + static void advanced_destr_test_fun( + [[maybe_unused]] void *u, + void *data + ) { + simple_destr_test_fun(data); + } + + void verifyAnyDestructor(CxList *list) { + int off = cxListIsStoringPointers(list) ? 1 : 0; - testdatastruct s[10]; - for (auto &t: s) { - t.ptr = cxMalloc(&testingAllocator, 16); - cxListAdd(list, &t); - } + cxListRemove(list, 15); + EXPECT_EQ(1, destr_test_ctr); + EXPECT_EQ(testdata.data[15], destr_last_value + off); + EXPECT_EQ(testdata_len - destr_test_ctr, list->size); + cxListRemove(list, 47); + EXPECT_EQ(2, destr_test_ctr); + EXPECT_EQ(testdata.data[48], destr_last_value + off); + EXPECT_EQ(testdata_len - destr_test_ctr, list->size); + + auto iter = cxListMutIteratorAt(list, 7); + cxIteratorNext(iter); + EXPECT_EQ(2, destr_test_ctr); + EXPECT_EQ(testdata.data[48], destr_last_value + off); + EXPECT_EQ(testdata_len - destr_test_ctr, list->size); + cxIteratorFlagRemoval(iter); + cxIteratorNext(iter); + EXPECT_EQ(3, destr_test_ctr); + EXPECT_EQ(testdata.data[8], destr_last_value + off); + EXPECT_EQ(testdata_len - destr_test_ctr, list->size); + + iter = cxListMutBackwardsIteratorAt(list, 5); + cxIteratorNext(iter); + EXPECT_EQ(3, destr_test_ctr); + EXPECT_EQ(testdata.data[8], destr_last_value + off); + EXPECT_EQ(testdata_len - destr_test_ctr, list->size); + cxIteratorFlagRemoval(iter); + cxIteratorNext(iter); + EXPECT_EQ(4, destr_test_ctr); + EXPECT_EQ(testdata.data[4], destr_last_value + off); + EXPECT_EQ(testdata_len - destr_test_ctr, list->size); cxListClear(list); + EXPECT_EQ(testdata_len, destr_test_ctr); + EXPECT_EQ(testdata.data[testdata_len - 1], destr_last_value + off); + + + } + + void verifySimpleDestructor(CxList *list) { + destr_test_ctr = 0; + list->content_destructor_type = CX_DESTRUCTOR_SIMPLE; + list->simple_destructor = simple_destr_test_fun; + verifyAnyDestructor(list); + } + + void verifyAdvancedDestructor(CxList *list) { + destr_test_ctr = 0; + list->content_destructor_type = CX_DESTRUCTOR_ADVANCED; + list->advanced_destructor.func = advanced_destr_test_fun; + verifyAnyDestructor(list); } static void verifySwap(CxList *list) { @@ -794,6 +836,9 @@ } EXPECT_EQ(cxListFind(list, &val), exp); } + + int notinlist = -1; + EXPECT_EQ(list->size, cxListFind(list, ¬inlist)); } void verifySort(CxList *list) const { @@ -911,6 +956,9 @@ } }; +unsigned HighLevelTest::destr_test_ctr = 0; +int HighLevelTest::destr_last_value = 0; + class LinkedList : public HighLevelTest { }; @@ -932,12 +980,6 @@ EXPECT_NE(list->cl, nullptr); EXPECT_EQ(list->climpl, nullptr); EXPECT_FALSE(cxListIsStoringPointers(list)); - - list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)); - EXPECT_EQ(list->itemsize, sizeof(void *)); - EXPECT_NE(list->cl, nullptr); - EXPECT_NE(list->climpl, nullptr); - EXPECT_TRUE(cxListIsStoringPointers(list)); } TEST_F(LinkedList, cxLinkedListCreate) { @@ -945,7 +987,11 @@ ASSERT_NE(list, nullptr); EXPECT_EQ(list->itemsize, sizeof(int)); EXPECT_EQ(list->capacity, (size_t) -1); - verifyCreate(list); + EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE); + EXPECT_EQ(list->size, 0); + EXPECT_EQ(list->allocator, &testingAllocator); + EXPECT_EQ(list->cmpfunc, cx_cmp_int); + EXPECT_FALSE(cxListIsStoringPointers(list)); } TEST_F(LinkedList, cxLinkedListCreateSimple) { @@ -955,6 +1001,17 @@ EXPECT_EQ(list->capacity, (size_t) -1); EXPECT_EQ(list->cmpfunc, nullptr); EXPECT_EQ(list->allocator, cxDefaultAllocator); + EXPECT_FALSE(cxListIsStoringPointers(list)); +} + +TEST_F(LinkedList, cxLinkedListCreateSimpleForPointers) { + CxList *list = autofree(cxLinkedListCreateSimple(CX_STORE_POINTERS)); + ASSERT_NE(list, nullptr); + EXPECT_EQ(list->itemsize, sizeof(void *)); + EXPECT_EQ(list->capacity, (size_t) -1); + EXPECT_EQ(list->cmpfunc, nullptr); + EXPECT_EQ(list->allocator, cxDefaultAllocator); + EXPECT_TRUE(cxListIsStoringPointers(list)); } TEST_F(ArrayList, cxArrayListCreate) { @@ -962,7 +1019,11 @@ ASSERT_NE(list, nullptr); EXPECT_EQ(list->itemsize, sizeof(int)); EXPECT_EQ(list->capacity, 8); - verifyCreate(list); + EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE); + EXPECT_EQ(list->size, 0); + EXPECT_EQ(list->allocator, &testingAllocator); + EXPECT_EQ(list->cmpfunc, cx_cmp_int); + EXPECT_FALSE(cxListIsStoringPointers(list)); } TEST_F(ArrayList, cxArrayListCreateSimple) { @@ -972,6 +1033,17 @@ EXPECT_EQ(list->allocator, cxDefaultAllocator); EXPECT_EQ(list->itemsize, sizeof(int)); EXPECT_EQ(list->capacity, 8); + EXPECT_FALSE(cxListIsStoringPointers(list)); +} + +TEST_F(ArrayList, cxArrayListCreateSimpleForPointers) { + CxList *list = autofree(cxArrayListCreateSimple(CX_STORE_POINTERS, 8)); + ASSERT_NE(list, nullptr); + EXPECT_EQ(list->cmpfunc, nullptr); + EXPECT_EQ(list->allocator, cxDefaultAllocator); + EXPECT_EQ(list->itemsize, sizeof(void *)); + EXPECT_EQ(list->capacity, 8); + EXPECT_TRUE(cxListIsStoringPointers(list)); } TEST_F(LinkedList, cxListAdd) { @@ -1026,15 +1098,15 @@ } TEST_F(LinkedList, cxListClear) { - verifyClear(autofree(cxLinkedListCreateSimple(sizeof(testdatastruct)))); + verifyClear(linkedListFromTestData()); } TEST_F(PointerLinkedList, cxListClear) { - verifyClear(cxLinkedListCreateSimple(CX_STORE_POINTERS)); + verifyClear(pointerLinkedListFromTestData()); } TEST_F(ArrayList, cxListClear) { - verifyClear(autofree(cxArrayListCreateSimple(sizeof(testdatastruct), 8))); + verifyClear(arrayListFromTestData()); } TEST_F(LinkedList, cxListSwap) { @@ -1203,7 +1275,31 @@ verifyCompare(left, right); } -TEST_F(PointerLinkedList, NoDestructor) { +TEST_F(LinkedList, SimpleDestructor) { + verifySimpleDestructor(linkedListFromTestData()); +} + +TEST_F(PointerLinkedList, SimpleDestructor) { + verifySimpleDestructor(pointerLinkedListFromTestData()); +} + +TEST_F(ArrayList, SimpleDestructor) { + verifySimpleDestructor(arrayListFromTestData()); +} + +TEST_F(LinkedList, AdvancedDestructor) { + verifyAdvancedDestructor(linkedListFromTestData()); +} + +TEST_F(PointerLinkedList, AdvancedDestructor) { + verifyAdvancedDestructor(pointerLinkedListFromTestData()); +} + +TEST_F(ArrayList, AdvancedDestructor) { + verifyAdvancedDestructor(arrayListFromTestData()); +} + +TEST_F(PointerLinkedList, DestroyNoDestructor) { void *item = cxMalloc(&testingAllocator, sizeof(int)); auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); cxListAdd(list, item); @@ -1214,7 +1310,7 @@ EXPECT_TRUE(testingAllocator.verify()); } -TEST_F(PointerLinkedList, SimpleDestructor) { +TEST_F(PointerLinkedList, DestroySimpleDestructor) { int item = 0; auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); list->content_destructor_type = CX_DESTRUCTOR_SIMPLE; @@ -1224,7 +1320,7 @@ EXPECT_EQ(item, 42); } -TEST_F(PointerLinkedList, AdvancedDestructor) { +TEST_F(PointerLinkedList, DestroyAdvancedDestructor) { void *item = cxMalloc(&testingAllocator, sizeof(int)); auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;