migrate list create and destroy tests - relates to #342

Wed, 10 Jan 2024 22:13:23 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Jan 2024 22:13:23 +0100
changeset 801
04aa3913c0e3
parent 800
1274e46b3013
child 802
30473af255b4

migrate list create and destroy tests - relates to #342

tests/test_list.c file | annotate | diff | comparison | revisions
tests/test_list.cpp file | annotate | diff | comparison | revisions
--- a/tests/test_list.c	Tue Jan 09 21:25:08 2024 +0100
+++ b/tests/test_list.c	Wed Jan 10 22:13:23 2024 +0100
@@ -635,9 +635,235 @@
     cxListDestroy(al);
 }
 
+CX_TEST(test_list_ll_create) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    CxAllocator *alloc = &talloc.base;
+    CX_TEST_DO {
+        CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int));
+        CX_TEST_ASSERT(list != NULL);
+        CX_TEST_ASSERT(list->item_size == sizeof(int));
+        CX_TEST_ASSERT(list->simple_destructor == NULL);
+        CX_TEST_ASSERT(list->advanced_destructor == NULL);
+        CX_TEST_ASSERT(list->destructor_data == NULL);
+        CX_TEST_ASSERT(cxListSize(list) == 0);
+        CX_TEST_ASSERT(list->allocator == alloc);
+        CX_TEST_ASSERT(list->cmpfunc == cx_cmp_int);
+        CX_TEST_ASSERT(!cxListIsStoringPointers(list));
+        cxListDestroy(list);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
+CX_TEST(test_list_ll_create_simple) {
+    CxList *list = cxLinkedListCreateSimple(sizeof(int));
+    CX_TEST_DO {
+        CX_TEST_ASSERT(list != NULL);
+        CX_TEST_ASSERT(list->item_size == sizeof(int));
+        CX_TEST_ASSERT(list->simple_destructor == NULL);
+        CX_TEST_ASSERT(list->advanced_destructor == NULL);
+        CX_TEST_ASSERT(list->destructor_data == NULL);
+        CX_TEST_ASSERT(cxListSize(list) == 0);
+        CX_TEST_ASSERT(list->allocator == cxDefaultAllocator);
+        CX_TEST_ASSERT(list->cmpfunc == NULL);
+        CX_TEST_ASSERT(!cxListIsStoringPointers(list));
+    }
+    cxListDestroy(list);
+}
+
+CX_TEST(test_list_ll_store_pointers) {
+    CxList *list = cxLinkedListCreateSimple(47);
+    CX_TEST_DO {
+        CX_TEST_ASSERT(!cxListIsStoringPointers(list));
+        cxListStorePointers(list);
+        CX_TEST_ASSERT(list->item_size == sizeof(void *));
+        CX_TEST_ASSERT(list->cl != NULL);
+        CX_TEST_ASSERT(list->climpl != NULL);
+        CX_TEST_ASSERT(cxListIsStoringPointers(list));
+        cxListStoreObjects(list);
+        CX_TEST_ASSERT(list->cl != NULL);
+        CX_TEST_ASSERT(list->climpl == NULL);
+        CX_TEST_ASSERT(!cxListIsStoringPointers(list));
+    }
+    cxListDestroy(list);
+}
+
+CX_TEST(test_list_ll_create_simple_for_pointers) {
+    CxList *list = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+    CX_TEST_DO {
+        CX_TEST_ASSERT(list != NULL);
+        CX_TEST_ASSERT(list->item_size == sizeof(void*));
+        CX_TEST_ASSERT(list->simple_destructor == NULL);
+        CX_TEST_ASSERT(list->advanced_destructor == NULL);
+        CX_TEST_ASSERT(list->destructor_data == NULL);
+        CX_TEST_ASSERT(cxListSize(list) == 0);
+        CX_TEST_ASSERT(list->allocator == cxDefaultAllocator);
+        CX_TEST_ASSERT(list->cmpfunc == cx_cmp_ptr);
+        CX_TEST_ASSERT(cxListIsStoringPointers(list));
+    }
+    cxListDestroy(list);
+}
+
+CX_TEST(test_list_arl_create) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    CxAllocator *alloc = &talloc.base;
+    CX_TEST_DO {
+        CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8);
+        CX_TEST_ASSERT(list != NULL);
+        CX_TEST_ASSERT(list->item_size == sizeof(int));
+        CX_TEST_ASSERT(list->simple_destructor == NULL);
+        CX_TEST_ASSERT(list->advanced_destructor == NULL);
+        CX_TEST_ASSERT(list->destructor_data == NULL);
+        CX_TEST_ASSERT(cxListSize(list) == 0);
+        CX_TEST_ASSERT(list->allocator == alloc);
+        CX_TEST_ASSERT(list->cmpfunc == cx_cmp_int);
+        CX_TEST_ASSERT(!cxListIsStoringPointers(list));
+        cxListDestroy(list);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
+CX_TEST(test_list_arl_create_simple) {
+    CxList *list = cxArrayListCreateSimple(sizeof(int), 8);
+    CX_TEST_DO {
+        CX_TEST_ASSERT(list != NULL);
+        CX_TEST_ASSERT(list->item_size == sizeof(int));
+        CX_TEST_ASSERT(list->simple_destructor == NULL);
+        CX_TEST_ASSERT(list->advanced_destructor == NULL);
+        CX_TEST_ASSERT(list->destructor_data == NULL);
+        CX_TEST_ASSERT(cxListSize(list) == 0);
+        CX_TEST_ASSERT(list->allocator == cxDefaultAllocator);
+        CX_TEST_ASSERT(list->cmpfunc == NULL);
+        CX_TEST_ASSERT(!cxListIsStoringPointers(list));
+    }
+    cxListDestroy(list);
+}
+
+CX_TEST(test_list_arl_create_simple_for_pointers) {
+    CxList *list = cxArrayListCreateSimple(CX_STORE_POINTERS, 8);
+    CX_TEST_DO {
+        CX_TEST_ASSERT(list != NULL);
+        CX_TEST_ASSERT(list->item_size == sizeof(void*));
+        CX_TEST_ASSERT(list->simple_destructor == NULL);
+        CX_TEST_ASSERT(list->advanced_destructor == NULL);
+        CX_TEST_ASSERT(list->destructor_data == NULL);
+        CX_TEST_ASSERT(cxListSize(list) == 0);
+        CX_TEST_ASSERT(list->allocator == cxDefaultAllocator);
+        CX_TEST_ASSERT(list->cmpfunc == cx_cmp_ptr);
+        CX_TEST_ASSERT(cxListIsStoringPointers(list));
+    }
+    cxListDestroy(list);
+}
+
+static void test_fake_simple_int_destr(void *elem) {
+    *(int *) elem = 42;
+}
+
+CX_TEST(test_list_pll_destroy_no_destr) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    CxAllocator *alloc = &talloc.base;
+    CX_TEST_DO {
+        void *item = cxMalloc(alloc, sizeof(int));
+        CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
+        cxListAdd(list, item);
+        CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
+        cxListDestroy(list);
+        // item is not yet freed
+        CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
+        cxFree(alloc, item);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
+CX_TEST(test_list_pll_destroy_simple_destr) {
+    CX_TEST_DO {
+        int item = 0;
+        CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
+        list->simple_destructor = test_fake_simple_int_destr;
+        cxListAdd(list, &item);
+        cxListDestroy(list);
+        CX_TEST_ASSERT(item == 42);
+    }
+}
+
+CX_TEST(test_list_pll_destroy_adv_destr) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    CxAllocator *alloc = &talloc.base;
+    CX_TEST_DO {
+        void *item = cxMalloc(alloc, sizeof(int));
+        CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
+        list->destructor_data = alloc;
+        list->advanced_destructor = (cx_destructor_func2) cxFree;
+        cxListAdd(list, item);
+        CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
+        cxListDestroy(list);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
+CX_TEST(test_list_parl_destroy_no_destr) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    CxAllocator *alloc = &talloc.base;
+    CX_TEST_DO {
+        void *item = cxMalloc(alloc, sizeof(int));
+        CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
+        cxListAdd(list, item);
+        CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
+        cxListDestroy(list);
+        // item is not yet freed
+        CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
+        cxFree(alloc, item);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
+CX_TEST(test_list_parl_destroy_simple_destr) {
+    CX_TEST_DO {
+        int item = 0;
+        CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
+        list->simple_destructor = test_fake_simple_int_destr;
+        cxListAdd(list, &item);
+        cxListDestroy(list);
+        CX_TEST_ASSERT(item == 42);
+    }
+}
+
+CX_TEST(test_list_parl_destroy_adv_destr) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    CxAllocator *alloc = &talloc.base;
+    CX_TEST_DO {
+        void *item = cxMalloc(alloc, sizeof(int));
+        CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
+        list->destructor_data = alloc;
+        list->advanced_destructor = (cx_destructor_func2) cxFree;
+        cxListAdd(list, item);
+        CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
+        cxListDestroy(list);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
 CxTestSuite *cx_test_suite_array_list(void) {
     CxTestSuite *suite = cx_test_suite_new("array_list");
 
+    cx_test_register(suite, test_list_arl_create);
+    cx_test_register(suite, test_list_arl_create_simple);
+    cx_test_register(suite, test_list_arl_create_simple_for_pointers);
+    cx_test_register(suite, test_list_parl_destroy_no_destr);
+    cx_test_register(suite, test_list_parl_destroy_simple_destr);
+    cx_test_register(suite, test_list_parl_destroy_adv_destr);
+
     return suite;
 }
 
@@ -661,6 +887,14 @@
     cx_test_register(suite, test_linked_list_sort);
     cx_test_register(suite, test_linked_list_reverse);
 
+    cx_test_register(suite, test_list_ll_create);
+    cx_test_register(suite, test_list_ll_create_simple);
+    cx_test_register(suite, test_list_ll_store_pointers);
+    cx_test_register(suite, test_list_ll_create_simple_for_pointers);
+    cx_test_register(suite, test_list_pll_destroy_no_destr);
+    cx_test_register(suite, test_list_pll_destroy_simple_destr);
+    cx_test_register(suite, test_list_pll_destroy_adv_destr);
+
     return suite;
 }
 
--- a/tests/test_list.cpp	Tue Jan 09 21:25:08 2024 +0100
+++ b/tests/test_list.cpp	Wed Jan 10 22:13:23 2024 +0100
@@ -848,153 +848,5 @@
     verifyAdvancedDestructor(pointerArrayListFromTestData());
 }
 
-TEST_F(PointerLinkedList, cxListStorePointers) {
-    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, 47));
-    CX_TEST_ASSERT(!cxListIsStoringPointers(list));
-    cxListStorePointers(list);
-    EXPECT_EQ(list->item_size, sizeof(void *));
-    CX_TEST_ASSERT(list->cl != NULL);
-    CX_TEST_ASSERT(list->climpl != NULL);
-    CX_TEST_ASSERT(cxListIsStoringPointers(list));
-    cxListStoreObjects(list);
-    CX_TEST_ASSERT(list->cl != NULL);
-    EXPECT_EQ(list->climpl, NULL);
-    CX_TEST_ASSERT(!cxListIsStoringPointers(list));
-}
-
-TEST_F(LinkedList, cxLinkedListCreate) {
-    CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
-    ASSERT_NE(list, NULL);
-    EXPECT_EQ(list->item_size, sizeof(int));
-    EXPECT_EQ(list->simple_destructor, NULL);
-    EXPECT_EQ(list->advanced_destructor, NULL);
-    EXPECT_EQ(list->destructor_data, NULL);
-    EXPECT_EQ(cxListSize(list), 0);
-    EXPECT_EQ(list->allocator, &testingAllocator);
-    EXPECT_EQ(list->cmpfunc, cx_cmp_int);
-    CX_TEST_ASSERT(!cxListIsStoringPointers(list));
-}
-
-TEST_F(LinkedList, cxLinkedListCreateSimple) {
-    CxList *list = autofree(cxLinkedListCreateSimple(sizeof(int)));
-    ASSERT_NE(list, NULL);
-    EXPECT_EQ(list->item_size, sizeof(int));
-    EXPECT_EQ(list->cmpfunc, NULL);
-    EXPECT_EQ(list->allocator, cxDefaultAllocator);
-    EXPECT_EQ(list->simple_destructor, NULL);
-    EXPECT_EQ(list->advanced_destructor, NULL);
-    EXPECT_EQ(list->destructor_data, NULL);
-    EXPECT_EQ(cxListSize(list), 0);
-    CX_TEST_ASSERT(!cxListIsStoringPointers(list));
-}
-
-TEST_F(PointerLinkedList, cxLinkedListCreateSimpleForPointers) {
-    CxList *list = autofree(cxLinkedListCreateSimple(CX_STORE_POINTERS));
-    ASSERT_NE(list, NULL);
-    EXPECT_EQ(list->item_size, sizeof(void *));
-    EXPECT_EQ(list->cmpfunc, cx_cmp_ptr);
-    EXPECT_EQ(list->allocator, cxDefaultAllocator);
-    EXPECT_EQ(list->simple_destructor, NULL);
-    EXPECT_EQ(list->advanced_destructor, NULL);
-    EXPECT_EQ(list->destructor_data, NULL);
-    EXPECT_EQ(cxListSize(list), 0);
-    CX_TEST_ASSERT(cxListIsStoringPointers(list));
-}
-
-TEST_F(ArrayList, cxArrayListCreate) {
-    CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
-    ASSERT_NE(list, NULL);
-    EXPECT_EQ(list->item_size, sizeof(int));
-    EXPECT_EQ(list->simple_destructor, NULL);
-    EXPECT_EQ(list->advanced_destructor, NULL);
-    EXPECT_EQ(list->destructor_data, NULL);
-    EXPECT_EQ(cxListSize(list), 0);
-    EXPECT_EQ(list->allocator, &testingAllocator);
-    EXPECT_EQ(list->cmpfunc, cx_cmp_int);
-    CX_TEST_ASSERT(!cxListIsStoringPointers(list));
-}
 
-TEST_F(ArrayList, cxArrayListCreateSimple) {
-    CxList *list = autofree(cxArrayListCreateSimple(sizeof(int), 8));
-    ASSERT_NE(list, NULL);
-    EXPECT_EQ(list->cmpfunc, NULL);
-    EXPECT_EQ(list->allocator, cxDefaultAllocator);
-    EXPECT_EQ(list->item_size, sizeof(int));
-    EXPECT_EQ(list->simple_destructor, NULL);
-    EXPECT_EQ(list->advanced_destructor, NULL);
-    EXPECT_EQ(list->destructor_data, NULL);
-    EXPECT_EQ(cxListSize(list), 0);
-    CX_TEST_ASSERT(!cxListIsStoringPointers(list));
-}
 
-TEST_F(PointerArrayList, cxArrayListCreateSimpleForPointers) {
-    CxList *list = autofree(cxArrayListCreateSimple(CX_STORE_POINTERS, 8));
-    ASSERT_NE(list, NULL);
-    EXPECT_EQ(list->cmpfunc, cx_cmp_ptr);
-    EXPECT_EQ(list->allocator, cxDefaultAllocator);
-    EXPECT_EQ(list->item_size, sizeof(void *));
-    CX_TEST_ASSERT(cxListIsStoringPointers(list));
-}
-
-TEST_F(PointerLinkedList, DestroyNoDestructor) {
-    void *item = cxMalloc(&testingAllocator, sizeof(int));
-    auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
-    cxListAdd(list, item);
-    CX_TEST_ASSERT(!testingAllocator.verify());
-    cxListDestroy(list);
-    CX_TEST_ASSERT(!testingAllocator.verify());
-    cxFree(&testingAllocator, item);
-    CX_TEST_ASSERT(testingAllocator.verify());
-}
-
-TEST_F(PointerLinkedList, DestroySimpleDestructor) {
-    int item = 0;
-    auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
-    list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
-    cxListAdd(list, &item);
-    cxListDestroy(list);
-    EXPECT_EQ(item, 42);
-}
-
-TEST_F(PointerLinkedList, DestroyAdvancedDestructor) {
-    void *item = cxMalloc(&testingAllocator, sizeof(int));
-    auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
-    list->destructor_data = &testingAllocator;
-    list->advanced_destructor = (cx_destructor_func2) cxFree;
-    cxListAdd(list, item);
-    CX_TEST_ASSERT(!testingAllocator.verify());
-    cxListDestroy(list);
-    CX_TEST_ASSERT(testingAllocator.verify());
-}
-
-TEST_F(PointerArrayList, DestroyNoDestructor) {
-    void *item = cxMalloc(&testingAllocator, sizeof(int));
-    auto list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
-    cxListAdd(list, item);
-    CX_TEST_ASSERT(!testingAllocator.verify());
-    cxListDestroy(list);
-    CX_TEST_ASSERT(!testingAllocator.verify());
-    cxFree(&testingAllocator, item);
-    CX_TEST_ASSERT(testingAllocator.verify());
-}
-
-TEST_F(PointerArrayList, DestroySimpleDestructor) {
-    int item = 0;
-    auto list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
-    list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
-    cxListAdd(list, &item);
-    cxListDestroy(list);
-    EXPECT_EQ(item, 42);
-}
-
-TEST_F(PointerArrayList, DestroyAdvancedDestructor) {
-    void *item = cxMalloc(&testingAllocator, sizeof(int));
-    auto list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
-    list->destructor_data = &testingAllocator;
-    list->advanced_destructor = (cx_destructor_func2) cxFree;
-    cxListAdd(list, item);
-    CX_TEST_ASSERT(!testingAllocator.verify());
-    cxListDestroy(list);
-    CX_TEST_ASSERT(testingAllocator.verify());
-}
-

mercurial