remove test code duplication for cxListAdd

Sun, 06 Mar 2022 13:57:36 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 06 Mar 2022 13:57:36 +0100
changeset 507
2e8878770de0
parent 506
18782bbe3607
child 508
8aea65ae1eaf

remove test code duplication for cxListAdd

test/test_list.c file | annotate | diff | comparison | revisions
--- a/test/test_list.c	Fri Feb 25 14:35:18 2022 +0100
+++ b/test/test_list.c	Sun Mar 06 13:57:36 2022 +0100
@@ -30,7 +30,7 @@
 #include "test_config.h"
 #include "util_allocator.h"
 
-int cmp_int_impl(
+static int cmp_int_impl(
         int const *l,
         int const *r
 ) {
@@ -557,7 +557,7 @@
     destroy_test_data(expected);
 }
 
-static void test_linked_list_create(CxList *list) {
+static void verify_linked_list_create(CxList *list) {
     CU_ASSERT_EQUAL(list->size, 0)
     CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
     CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
@@ -569,13 +569,13 @@
 void test_hl_linked_list_create(void) {
     CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
     CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
-    test_linked_list_create(list);
+    verify_linked_list_create(list);
 }
 
 void test_hl_ptr_linked_list_create(void) {
     CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
     CU_ASSERT_EQUAL(list->itemsize, sizeof(void *))
-    test_linked_list_create(list);
+    verify_linked_list_create(list);
 }
 
 void test_hl_linked_list_from_array(void) {
@@ -592,53 +592,45 @@
     cxListDestroy(expected);
 }
 
-void test_hl_linked_list_add(void) {
-    int data;
-    CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
+static void verify_hl_linked_list_add(
+        CxList *list,
+        int data[],
+        size_t len,
+        bool write_through
+) {
+    for (size_t i = 0; i < len; i++) {
+        CU_ASSERT_EQUAL(cxListAdd(list, &data[i]), 0)
+    }
+    CU_ASSERT_EQUAL(list->size, len)
+    CU_ASSERT_TRUE(list->capacity >= list->size)
+    for (size_t i = 0; i < len; i++) {
+        CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
+    }
+    for (size_t i = 0; i < len; i++) {
+        ++data[i];
+    }
+    if (write_through) {
+        for (size_t i = 0; i < len; i++) {
+            CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
+        }
+    } else {
+        for (size_t i = 0; i < len; i++) {
+            CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i] - 1)
+        }
+    }
+    cxListDestroy(list);
+}
 
-    data = 5;
-    CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
-    data = 47;
-    CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
-    data = 13;
-    CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
-
-    CU_ASSERT_EQUAL(list->size, 3)
-    CU_ASSERT_TRUE(list->capacity >= list->size)
-
-    int exp[] = {5, 47, 13};
-    CxList *expected = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 3, exp);
-    CU_ASSERT_TRUE(0 == cxListCompare(list, expected))
-
-    cxListDestroy(list);
-    cxListDestroy(expected);
+void test_hl_linked_list_add(void) {
+    CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
+    int data[] = {5, 47, 13, 9, 18, 1, 42};
+    verify_hl_linked_list_add(list, data, sizeof(data) / sizeof(int), false);
 }
 
 void test_hl_ptr_linked_list_add(void) {
     CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
-
-    int a = 5, b = 47, c = 13;
-
-    CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
-    CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
-    CU_ASSERT_EQUAL(cxListAdd(list, &c), 0)
-
-    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)
-
-    a = 9;
-    b = 10;
-    c = 11;
-
-    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9)
-    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10)
-    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11)
-
-    cxListDestroy(list);
+    int data[] = {5, 47, 13, 9, 18, 1, 42};
+    verify_hl_linked_list_add(list, data, sizeof(data) / sizeof(int), true);
 }
 
 void test_hl_linked_list_insert(void) {

mercurial