1.1 --- a/tests/test_list.cpp Mon Mar 20 18:05:12 2023 +0100 1.2 +++ b/tests/test_list.cpp Mon Mar 20 19:09:08 2023 +0100 1.3 @@ -38,6 +38,19 @@ 1.4 #include <unordered_set> 1.5 #include <algorithm> 1.6 1.7 +struct testdatastruct { 1.8 + int x; 1.9 + void *ptr; 1.10 +}; 1.11 + 1.12 +static void free_testdatastruct( 1.13 + void *a, 1.14 + void *s 1.15 +) { 1.16 + auto al = reinterpret_cast<CxTestingAllocator *>(a); 1.17 + cxFree(al, reinterpret_cast<testdatastruct *>(s)->ptr); 1.18 +} 1.19 + 1.20 struct node { 1.21 node *next = nullptr; 1.22 node *prev = nullptr; 1.23 @@ -697,6 +710,21 @@ 1.24 EXPECT_NE(cxListRemove(list, testdata_len), 0); 1.25 } 1.26 1.27 + void verifyClear(CxList *list) { 1.28 + // use the testing allocator for testing the destructor function 1.29 + list->content_destructor_type = CX_DESTRUCTOR_ADVANCED; 1.30 + list->advanced_destructor.func = free_testdatastruct; 1.31 + list->advanced_destructor.data = &testingAllocator; 1.32 + 1.33 + testdatastruct s[10]; 1.34 + for (auto &t: s) { 1.35 + t.ptr = cxMalloc(&testingAllocator, 16); 1.36 + cxListAdd(list, &t); 1.37 + } 1.38 + 1.39 + cxListClear(list); 1.40 + } 1.41 + 1.42 static void verifySwap(CxList *list) { 1.43 ASSERT_EQ(list->size, 0); 1.44 1.45 @@ -997,6 +1025,20 @@ 1.46 verifyRemove(arrayListFromTestData()); 1.47 } 1.48 1.49 +TEST_F(LinkedList, cxListClear) { 1.50 + verifyClear(autofree(cxLinkedListCreateSimple(sizeof(testdatastruct)))); 1.51 +} 1.52 + 1.53 +TEST_F(PointerLinkedList, cxListClear) { 1.54 + auto l = cxLinkedListCreateSimple(sizeof(testdatastruct)); 1.55 + cxListStorePointers(l); 1.56 + verifyClear(autofree(l)); 1.57 +} 1.58 + 1.59 +TEST_F(ArrayList, cxListClear) { 1.60 + verifyClear(autofree(cxArrayListCreateSimple(sizeof(testdatastruct), 8))); 1.61 +} 1.62 + 1.63 TEST_F(LinkedList, cxListSwap) { 1.64 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); 1.65 }