simplifies some linked list tests

Tue, 28 Dec 2021 18:05:14 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Dec 2021 18:05:14 +0100
changeset 492
188942a7308b
parent 491
6d538177f746
child 493
e3469b497eff

simplifies some linked list tests

test/test_list.c file | annotate | diff | comparison | revisions
--- a/test/test_list.c	Tue Dec 28 17:49:52 2021 +0100
+++ b/test/test_list.c	Tue Dec 28 18:05:14 2021 +0100
@@ -30,7 +30,7 @@
 #include "test_config.h"
 #include "util_allocator.h"
 
-int cmp_int(
+int cmp_int_impl(
         int const *l,
         int const *r
 ) {
@@ -38,6 +38,8 @@
     return left == right ? 0 : (left < right ? -1 : 1);
 }
 
+#define cmp_int ((CxListComparator) cmp_int_impl)
+
 struct node {
     struct node *next;
     struct node *prev;
@@ -132,22 +134,22 @@
 
     s = 2;
     CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
-                                        false, (CxListComparator) cmp_int, &s), 0)
+                                        false, cmp_int, &s), 0)
     s = 4;
     CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
-                                        false, (CxListComparator) cmp_int, &s), 1)
+                                        false, cmp_int, &s), 1)
     s = 6;
     CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
-                                        false, (CxListComparator) cmp_int, &s), 2)
+                                        false, cmp_int, &s), 2)
     s = 8;
     CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
-                                        false, (CxListComparator) cmp_int, &s), 3)
+                                        false, cmp_int, &s), 3)
     s = 10;
     CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
-                                        false, (CxListComparator) cmp_int, &s), 4)
+                                        false, cmp_int, &s), 4)
     s = -2;
     CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
-                                        false, (CxListComparator) cmp_int, &s), 4)
+                                        false, cmp_int, &s), 4)
 }
 
 void test_linked_list_compare(void) {
@@ -160,19 +162,19 @@
     void *lc = create_test_data(4, c);
 
     CU_ASSERT_TRUE(0 < cx_linked_list_compare(la, lb, loc_next, loc_data,
-                                              false, (CxListComparator) cmp_int)
+                                              false, cmp_int)
     )
     CU_ASSERT_TRUE(0 > cx_linked_list_compare(lb, la, loc_next, loc_data,
-                                              false, (CxListComparator) cmp_int)
+                                              false, cmp_int)
     )
     CU_ASSERT_TRUE(0 < cx_linked_list_compare(lc, la, loc_next, loc_data,
-                                              false, (CxListComparator) cmp_int)
+                                              false, cmp_int)
     )
     CU_ASSERT_TRUE(0 > cx_linked_list_compare(la, lc, loc_next, loc_data,
-                                              false, (CxListComparator) cmp_int)
+                                              false, cmp_int)
     )
     CU_ASSERT_TRUE(0 == cx_linked_list_compare(la, la, loc_next, loc_data,
-                                               false, (CxListComparator) cmp_int)
+                                               false, cmp_int)
     )
 
     destroy_test_data(la);
@@ -512,7 +514,7 @@
     void *end = cx_linked_list_last(begin, loc_next);
 
     cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data,
-                        false, (CxListComparator) cmp_int);
+                        false, cmp_int);
 
     struct node *check = begin;
     struct node *check_last = NULL;
@@ -549,7 +551,7 @@
     CU_ASSERT_PTR_EQUAL(end, list)
     CU_ASSERT_PTR_EQUAL(begin, cx_linked_list_first(end, loc_prev))
     CU_ASSERT_TRUE(0 == cx_linked_list_compare(begin, expected, loc_next, loc_data,
-                                               0, (CxListComparator) cmp_int))
+                                               0, cmp_int))
 
     destroy_test_data(begin);
     destroy_test_data(expected);
@@ -558,7 +560,7 @@
 void test_hl_linked_list_create(void) {
     cxTestingAllocatorReset();
 
-    CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
+    CxList list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
 
     CU_ASSERT_EQUAL(list->size, 0)
     CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
@@ -575,10 +577,10 @@
 
     int data[] = {2, 4, 5, 7, 10, 15};
 
-    CxList expected = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
+    CxList expected = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
     for (int i = 0; i < 5; i++) cxListAdd(expected, &data[i]);
 
-    CxList list = cxLinkedListFromArray(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int), 5, data);
+    CxList list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 5, data);
 
     CU_ASSERT_TRUE(0 == cxListCompare(list, expected))
 
@@ -591,7 +593,7 @@
     cxTestingAllocatorReset();
 
     int data;
-    CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
+    CxList list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
 
     data = 5;
     CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
@@ -603,11 +605,12 @@
     CU_ASSERT_EQUAL(list->size, 3)
     CU_ASSERT_TRUE(list->capacity >= list->size)
 
-    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
-    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
-    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
+    int exp[] = {5, 47, 13};
+    CxList expected = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 3, exp);
+    CU_ASSERT_TRUE(0 == cxListCompare(list, expected))
 
     cxLinkedListDestroy(list);
+    cxLinkedListDestroy(expected);
     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
@@ -615,7 +618,7 @@
     cxTestingAllocatorReset();
 
     int data;
-    CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
+    CxList list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
 
     data = 5;
     CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0)
@@ -634,29 +637,21 @@
     CU_ASSERT_EQUAL(list->size, 4)
     CU_ASSERT_TRUE(list->capacity >= list->size)
 
-    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
-    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
-    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
-    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
+    int exp[] = {47, 13, 5, 42};
+    CxList expected = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 4, exp);
+    CU_ASSERT_TRUE(0 == cxListCompare(list, expected))
 
     cxLinkedListDestroy(list);
+    cxLinkedListDestroy(expected);
     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_linked_list_remove(void) {
     cxTestingAllocatorReset();
 
-    int data;
-    CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
-
-    data = 5;
-    cxListAdd(list, &data);
-    data = 47;
-    cxListAdd(list, &data);
-    data = 42;
-    cxListAdd(list, &data);
-    data = 13;
-    cxListAdd(list, &data);
+    int data[] = {5, 47, 42, 13};
+    CxList list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
+                                        sizeof(int), 4, data);
 
     CU_ASSERT_EQUAL(list->size, 4)
     CU_ASSERT_TRUE(list->capacity >= list->size)
@@ -694,19 +689,13 @@
 void test_hl_linked_list_at(void) {
     cxTestingAllocatorReset();
 
-    CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
+    int data[] = {5, 47, 13};
+    CxList list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
+                                        sizeof(int), 3, data);
 
-    int data;
-    data = 5;
-    cxListAdd(list, &data);
-    data = 47;
-    cxListAdd(list, &data);
-    data = 13;
-    cxListAdd(list, &data);
-
-    CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5)
-    CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47)
-    CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13)
+    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
+    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
+    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
     CU_ASSERT_PTR_NULL(cxListAt(list, 3))
 
     cxLinkedListDestroy(list);
@@ -716,19 +705,14 @@
 void test_hl_linked_list_find(void) {
     cxTestingAllocatorReset();
 
-    int data, criteria;
-    CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
-
-    data = 5;
-    cxListAdd(list, &data);
-    data = 47;
-    cxListAdd(list, &data);
-    data = 13;
-    cxListAdd(list, &data);
-
+    int data[] = {5, 47, 13};
+    CxList list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
+                                        sizeof(int), 3, data);
     CU_ASSERT_EQUAL(list->size, 3)
     CU_ASSERT_TRUE(list->capacity >= list->size)
 
+    int criteria;
+
     criteria = 5;
     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
     criteria = 47;
@@ -764,26 +748,21 @@
 
     cxTestingAllocatorReset();
 
-    CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
-
-    for (int i = 0; i < 100; i++) {
-        cxListAdd(list, &scrambled[i]);
-    }
+    CxList list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, scrambled);
+    CxList exp = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, expected);
 
     cxListSort(list);
-
-    for (int i = 0; i < 100; i++) {
-        CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
-    }
+    CU_ASSERT_TRUE(0 == cxListCompare(list, exp))
 
     cxLinkedListDestroy(list);
+    cxLinkedListDestroy(exp);
     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_ptr_linked_list_create(void) {
     cxTestingAllocatorReset();
 
-    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
+    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
     CU_ASSERT_EQUAL(list->size, 0)
     CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
@@ -798,7 +777,7 @@
 void test_hl_ptr_linked_list_add(void) {
     cxTestingAllocatorReset();
 
-    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
+    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
     int a = 5, b = 47, c = 13;
 
@@ -828,7 +807,7 @@
 void test_hl_ptr_linked_list_insert(void) {
     cxTestingAllocatorReset();
 
-    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
+    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
     int a = 5, b = 47, c = 13, d = 42;
 
@@ -858,7 +837,7 @@
     cxTestingAllocatorReset();
 
     int a = 5, b = 47, c = 42, d = 13;
-    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
+    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
     cxListAdd(list, &a);
     cxListAdd(list, &b);
@@ -901,7 +880,7 @@
 void test_hl_ptr_linked_list_at(void) {
     cxTestingAllocatorReset();
 
-    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
+    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
     int a = 5, b = 47, c = 13;
     cxListAdd(list, &a);
@@ -921,7 +900,7 @@
     cxTestingAllocatorReset();
 
     int a = 5, b = 47, c = 13, criteria;
-    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
+    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
     cxListAdd(list, &a);
     cxListAdd(list, &b);
@@ -967,7 +946,7 @@
 
     cxTestingAllocatorReset();
 
-    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
+    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
     for (int i = 0; i < 100; i++) {
         cxListAdd(list, &scrambled[i]);

mercurial