add complete test coverage for the new array features

Fri, 19 Dec 2025 18:12:20 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 19 Dec 2025 18:12:20 +0100
changeset 1637
34c2e1436945
parent 1636
5ed91801f79d
child 1638
14ae6a039af7

add complete test coverage for the new array features

tests/test_list.c file | annotate | diff | comparison | revisions
--- a/tests/test_list.c	Fri Dec 19 17:54:15 2025 +0100
+++ b/tests/test_list.c	Fri Dec 19 18:12:20 2025 +0100
@@ -37,6 +37,13 @@
 #include <stdarg.h>
 #include <errno.h>
 
+static int test_ccmp_int(const void *l, const void *r, void *c) {
+    int *z = c;
+    // return bullshit to make the test fail when c was not passed correctly
+    if (z == NULL || *z != 1337) return -1;
+    return cx_cmp_int(l, r);
+}
+
 CX_TEST(test_array_add) {
     CX_ARRAY(int, arr);
     cx_array_init(arr, 5);
@@ -297,6 +304,43 @@
     cx_array_free(arr);
 }
 
+CX_TEST(test_array_remove_array_fast_ints) {
+    char d_char[5] = {1, 2, 3, 4, 5};
+    short d_short[5] = {1, 2, 3, 4, 5};
+    long d_long[5] = {1, 2, 3, 4, 5};
+    long long d_llong[5] = {1, 2, 3, 4, 5};
+    CX_ARRAY(char, a_char);
+    cx_array_init_fixed(a_char, d_char, 5);
+    CX_ARRAY(short, a_short);
+    cx_array_init_fixed(a_short, d_short, 5);
+    CX_ARRAY(long, a_long);
+    cx_array_init_fixed(a_long, d_long, 5);
+    CX_ARRAY(long long, a_llong);
+    cx_array_init_fixed(a_llong, d_llong, 5);
+    CX_TEST_DO {
+        cx_array_remove_fast(a_char, 2);
+        cx_array_remove_fast(a_short, 2);
+        cx_array_remove_fast(a_long, 2);
+        cx_array_remove_fast(a_llong, 2);
+        CX_TEST_ASSERT(a_char.data[0] == 1);
+        CX_TEST_ASSERT(a_short.data[0] == 1);
+        CX_TEST_ASSERT(a_long.data[0] == 1);
+        CX_TEST_ASSERT(a_llong.data[0] == 1);
+        CX_TEST_ASSERT(a_char.data[1] == 2);
+        CX_TEST_ASSERT(a_short.data[1] == 2);
+        CX_TEST_ASSERT(a_long.data[1] == 2);
+        CX_TEST_ASSERT(a_llong.data[1] == 2);
+        CX_TEST_ASSERT(a_char.data[2] == 5);
+        CX_TEST_ASSERT(a_short.data[2] == 5);
+        CX_TEST_ASSERT(a_long.data[2] == 5);
+        CX_TEST_ASSERT(a_llong.data[2] == 5);
+        CX_TEST_ASSERT(a_char.data[3] == 4);
+        CX_TEST_ASSERT(a_short.data[3] == 4);
+        CX_TEST_ASSERT(a_long.data[3] == 4);
+        CX_TEST_ASSERT(a_llong.data[3] == 4);
+    }
+}
+
 CX_TEST(test_array_reserve) {
     CX_ARRAY(int, arr);
     cx_array_init(arr, 5);
@@ -355,6 +399,79 @@
     cx_array_free(arr);
 }
 
+CX_TEST(test_array_sort) {
+    int data[31] = {
+        67, 90, 120, 77, 64, 75, 56, 130, 70, 59, 51, 71, 10, 78, 75, 65,
+        54, 56, 80, 62, 52, 72, 71, 58, 40, 90, 75, 57, 58, 60, 50
+    };
+    int expected[31] = {
+        10, 40, 50, 51, 52, 54, 56, 56, 57, 58, 58, 59, 60, 62, 64, 65, 67,
+        70, 71, 71, 72, 75, 75, 75, 77, 78, 80, 90, 90, 120, 130
+    };
+    CX_ARRAY(int, arr);
+    cx_array_init_fixed(arr, data, 31);
+    CX_TEST_DO {
+        cx_array_sort(arr, cx_cmp_int);
+        CX_TEST_ASSERT(memcmp(arr.data, expected, 31*sizeof(int)) == 0);
+    }
+}
+
+CX_TEST(test_array_sort_c) {
+    int data[31] = {
+        67, 90, 120, 77, 64, 75, 56, 130, 70, 59, 51, 71, 10, 78, 75, 65,
+        54, 56, 80, 62, 52, 72, 71, 58, 40, 90, 75, 57, 58, 60, 50
+    };
+    int expected[31] = {
+        10, 40, 50, 51, 52, 54, 56, 56, 57, 58, 58, 59, 60, 62, 64, 65, 67,
+        70, 71, 71, 72, 75, 75, 75, 77, 78, 80, 90, 90, 120, 130
+    };
+    CX_ARRAY(int, arr);
+    cx_array_init_fixed(arr, data, 31);
+    CX_TEST_DO {
+        int z = 1337;
+        cx_array_sort_c(arr, test_ccmp_int, &z);
+        CX_TEST_ASSERT(memcmp(arr.data, expected, 31*sizeof(int)) == 0);
+    }
+}
+
+CX_TEST(test_array_iterator) {
+    int data[31] = {
+        67, 90, 120, 77, 64, 75, 56, 130, 70, 59, 51, 71, 10, 78, 75, 65,
+        54, 56, 80, 62, 52, 72, 71, 58, 40, 90, 75, 57, 58, 60, 50
+    };
+    int copy[31] = {0};
+    CX_ARRAY(int, arr);
+    cx_array_init_fixed(arr, data, 31);
+    CX_TEST_DO {
+        CxIterator iter = cx_array_iterator(arr);
+        cx_foreach(int*, x, iter) {
+            copy[iter.index] = *x;
+        }
+        CX_TEST_ASSERT(memcmp(copy, data, 31*sizeof(int)) == 0);
+    }
+}
+
+CX_TEST(test_array_iterator_ptr) {
+    int data[31] = {
+        67, 90, 120, 77, 64, 75, 56, 130, 70, 59, 51, 71, 10, 78, 75, 65,
+        54, 56, 80, 62, 52, 72, 71, 58, 40, 90, 75, 57, 58, 60, 50
+    };
+    int *data_ptr[31];
+    for (unsigned i = 0 ; i < 31 ; i++) {
+        data_ptr[i] = &data[i];
+    }
+    int copy[31] = {0};
+    CX_ARRAY(int, arr);
+    cx_array_init_fixed(arr, data_ptr, 31);
+    CX_TEST_DO {
+        CxIterator iter = cx_array_iterator_ptr(arr);
+        cx_foreach(int*, x, iter) {
+            copy[iter.index] = *x;
+        }
+        CX_TEST_ASSERT(memcmp(copy, data, 31*sizeof(int)) == 0);
+    }
+}
+
 CX_TEST(test_array_insert_sorted) {
     int d1 = 50;
     int d2 = 80;
@@ -602,13 +719,6 @@
     }
 }
 
-static int test_ccmp_int(const void *l, const void *r, void *c) {
-    int *z = c;
-    // return bullshit to make the test fail when c was not passed correctly
-    if (z == NULL || *z != 1337) return -1;
-    return cx_cmp_int(l, r);
-}
-
 typedef struct node {
     struct node *next;
     struct node *prev;
@@ -3506,8 +3616,13 @@
     cx_test_register(suite, test_array_remove_fast);
     cx_test_register(suite, test_array_remove_array);
     cx_test_register(suite, test_array_remove_array_fast);
+    cx_test_register(suite, test_array_remove_array_fast_ints);
     cx_test_register(suite, test_array_reserve);
     cx_test_register(suite, test_array_copy_to_new);
+    cx_test_register(suite, test_array_sort);
+    cx_test_register(suite, test_array_sort_c);
+    cx_test_register(suite, test_array_iterator);
+    cx_test_register(suite, test_array_iterator_ptr);
     cx_test_register(suite, test_array_insert_sorted);
     cx_test_register(suite, test_array_insert_unique);
     cx_test_register(suite, test_array_binary_search);

mercurial