Tue, 09 Jan 2024 21:25:08 +0100
migrate cxEmptyList tests - relates to #342
tests/test_list.c | file | annotate | diff | comparison | revisions | |
tests/test_list.cpp | file | annotate | diff | comparison | revisions | |
tests/ucxtest.c | file | annotate | diff | comparison | revisions |
--- a/tests/test_list.c Tue Jan 09 00:09:11 2024 +0100 +++ b/tests/test_list.c Tue Jan 09 21:25:08 2024 +0100 @@ -555,6 +555,86 @@ destroy_nodes_test_data(expected); } + +CX_TEST(test_empty_list_size) { + CX_TEST_DO { + CX_TEST_ASSERT(cxEmptyList->size == 0); + CX_TEST_ASSERT(cxListSize(cxEmptyList) == 0); + } +} + +CX_TEST(test_empty_list_iterator) { + CxList *list = cxEmptyList; + + CxIterator it1 = cxListIterator(list); + CxIterator it2 = cxListBackwardsIterator(list); + CxMutIterator it3 = cxListMutIterator(list); + CxMutIterator it4 = cxListMutBackwardsIterator(list); + + CX_TEST_DO { + CX_TEST_ASSERT(!cxIteratorValid(it1)); + CX_TEST_ASSERT(!cxIteratorValid(it2)); + CX_TEST_ASSERT(!cxIteratorValid(it3)); + CX_TEST_ASSERT(!cxIteratorValid(it4)); + + int c = 0; + cx_foreach(void*, data, it1) c++; + cx_foreach(void*, data, it2) c++; + cx_foreach(void*, data, it3) c++; + cx_foreach(void*, data, it4) c++; + CX_TEST_ASSERT(c == 0); + } +} + +CX_TEST(test_empty_list_noops) { + CX_TEST_DO { + CxList copy = *cxEmptyList; + cxListSort(cxEmptyList); + cxListClear(cxEmptyList); + cxListDestroy(cxEmptyList); + CX_TEST_ASSERT(0 == memcmp(©, cxEmptyList, sizeof(CxList))); // NOLINT(*-suspicious-memory-comparison) + } +} + +CX_TEST(test_empty_list_at) { + CX_TEST_DO { + CX_TEST_ASSERT(cxListAt(cxEmptyList, 0) == NULL); + CX_TEST_ASSERT(cxListAt(cxEmptyList, 1) == NULL); + } +} + +CX_TEST(test_empty_list_find) { + int x = 42, y = 1337; + CX_TEST_DO { + CX_TEST_ASSERT(cxListFind(cxEmptyList, &x) < 0); + CX_TEST_ASSERT(cxListFind(cxEmptyList, &y) < 0); + } +} + +CX_TEST(test_empty_list_compare) { + CxList *empty = cxEmptyList; + CxList *ll = cxLinkedListCreateSimple(sizeof(int)); + CxList *al = cxArrayListCreateSimple(sizeof(int), 8); + int x = 5; + CX_TEST_DO { + CX_TEST_ASSERT(0 == cxListCompare(empty, cxEmptyList)); + CX_TEST_ASSERT(0 == cxListCompare(ll, cxEmptyList)); + CX_TEST_ASSERT(0 == cxListCompare(al, cxEmptyList)); + CX_TEST_ASSERT(0 == cxListCompare(cxEmptyList, ll)); + CX_TEST_ASSERT(0 == cxListCompare(cxEmptyList, al)); + + cxListAdd(ll, &x); + cxListAdd(al, &x); + + CX_TEST_ASSERT(0 < cxListCompare(ll, cxEmptyList)); + CX_TEST_ASSERT(0 < cxListCompare(al, cxEmptyList)); + CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, ll)); + CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, al)); + } + cxListDestroy(ll); + cxListDestroy(al); +} + CxTestSuite *cx_test_suite_array_list(void) { CxTestSuite *suite = cx_test_suite_new("array_list"); @@ -584,3 +664,15 @@ return suite; } +CxTestSuite *cx_test_suite_empty_list(void) { + CxTestSuite *suite = cx_test_suite_new("empty list dummy"); + + cx_test_register(suite, test_empty_list_size); + cx_test_register(suite, test_empty_list_iterator); + cx_test_register(suite, test_empty_list_noops); + cx_test_register(suite, test_empty_list_at); + cx_test_register(suite, test_empty_list_find); + cx_test_register(suite, test_empty_list_compare); + + return suite; +}
--- a/tests/test_list.cpp Tue Jan 09 00:09:11 2024 +0100 +++ b/tests/test_list.cpp Tue Jan 09 21:25:08 2024 +0100 @@ -481,93 +481,6 @@ class PointerArrayList : public HighLevelTest { }; -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(LinkedList, cxListAdd) { auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); @@ -935,6 +848,94 @@ 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); @@ -997,80 +998,3 @@ CX_TEST_ASSERT(testingAllocator.verify()); } -TEST(EmptyList, Size) { - auto list = cxEmptyList; - - EXPECT_EQ(list->size, 0); - EXPECT_EQ(cxListSize(list), 0); -} - -TEST(EmptyList, Iterator) { - auto list = cxEmptyList; - - auto it1 = cxListIterator(list); - auto it2 = cxListBackwardsIterator(list); - auto it3 = cxListMutIterator(list); - auto it4 = cxListMutBackwardsIterator(list); - - CX_TEST_ASSERT(!cxIteratorValid(it1)); - CX_TEST_ASSERT(!cxIteratorValid(it2)); - CX_TEST_ASSERT(!cxIteratorValid(it3)); - CX_TEST_ASSERT(!cxIteratorValid(it4)); - - int c = 0; - cx_foreach(void*, data, it1) c++; - cx_foreach(void*, data, it2) c++; - cx_foreach(void*, data, it3) c++; - cx_foreach(void*, data, it4) c++; - EXPECT_EQ(c, 0); -} - -TEST(EmptyList, NoOps) { - auto list = cxEmptyList; - - ASSERT_NO_FATAL_FAILURE(cxListSort(list)); - ASSERT_NO_FATAL_FAILURE(cxListClear(list)); - ASSERT_NO_FATAL_FAILURE(cxListDestroy(list)); -} - -TEST(EmptyList, At) { - auto list = cxEmptyList; - - EXPECT_EQ(cxListAt(list, 0), NULL); - EXPECT_EQ(cxListAt(list, 1), NULL); -} - -TEST(EmptyList, Find) { - auto list = cxEmptyList; - - int x = 42, y = 1337; - - EXPECT_LT(cxListFind(list, &x), 0); - EXPECT_LT(cxListFind(list, &y), 0); -} - -TEST(EmptyList, Compare) { - auto empty = cxEmptyList; - - auto ll = cxLinkedListCreateSimple(sizeof(int)); - auto al = cxArrayListCreateSimple(sizeof(int), 8); - - int x = 5; - - EXPECT_EQ(cxListCompare(empty, cxEmptyList), 0); - EXPECT_EQ(cxListCompare(ll, cxEmptyList), 0); - EXPECT_EQ(cxListCompare(al, cxEmptyList), 0); - EXPECT_EQ(cxListCompare(cxEmptyList, ll), 0); - EXPECT_EQ(cxListCompare(cxEmptyList, al), 0); - - cxListAdd(ll, &x); - cxListAdd(al, &x); - - EXPECT_GT(cxListCompare(ll, cxEmptyList), 0); - EXPECT_GT(cxListCompare(al, cxEmptyList), 0); - EXPECT_LT(cxListCompare(cxEmptyList, ll), 0); - EXPECT_LT(cxListCompare(cxEmptyList, al), 0); - - cxListDestroy(ll); - cxListDestroy(al); -}
--- a/tests/ucxtest.c Tue Jan 09 00:09:11 2024 +0100 +++ b/tests/ucxtest.c Tue Jan 09 21:25:08 2024 +0100 @@ -36,6 +36,7 @@ CxTestSuite *cx_test_suite_string(void); CxTestSuite *cx_test_suite_buffer(void); CxTestSuite *cx_test_suite_printf(void); +CxTestSuite *cx_test_suite_empty_list(void); CxTestSuite *cx_test_suite_array_list(void); CxTestSuite *cx_test_suite_linked_list(void); CxTestSuite *cx_test_suite_mempool(void); @@ -58,6 +59,7 @@ cx_test_suite_string(), cx_test_suite_buffer(), cx_test_suite_printf(), + cx_test_suite_empty_list(), cx_test_suite_array_list(), cx_test_suite_linked_list(), cx_test_suite_mempool(),