tests/test_list.c

changeset 998
bb196054f3fd
parent 993
b642eca4b956
child 999
84fc42b04d3b
--- a/tests/test_list.c	Thu Nov 28 20:59:11 2024 +0100
+++ b/tests/test_list.c	Mon Dec 02 20:58:17 2024 +0100
@@ -34,6 +34,7 @@
 #include "cx/linked_list.h"
 
 #include <stdarg.h>
+#include <errno.h>
 
 CX_TEST(test_array_add) {
     CX_ARRAY_DECLARE(int, arr);
@@ -73,6 +74,74 @@
     free(arr);
 }
 
+CX_TEST(test_array_add8) {
+    CX_ARRAY_DECLARE_SIZED(int, arr, uint8_t);
+    arr = calloc(5, sizeof(int));
+    arr[0] = 2;
+    arr[1] = 3;
+    arr[2] = 5;
+    arr[3] = 7;
+    arr[4] = 11;
+    arr_size = 3;
+    arr_capacity = 5;
+    int elem = 8, elem2 = 47;
+    int result;
+    CX_TEST_DO {
+        result = cx_array_simple_add(arr, elem);
+        CX_TEST_ASSERT(result == 0);
+        CX_TEST_ASSERT(arr[0] == 2);
+        CX_TEST_ASSERT(arr[1] == 3);
+        CX_TEST_ASSERT(arr[2] == 5);
+        CX_TEST_ASSERT(arr[3] == 8);
+        CX_TEST_ASSERT(arr[4] == 11);
+        CX_TEST_ASSERT(arr_size == 4);
+        CX_TEST_ASSERT(arr_capacity == 5);
+
+        arr_size = 5;
+        result = cx_array_simple_add(arr, elem2);
+        CX_TEST_ASSERT(result == 0);
+        CX_TEST_ASSERT(arr[0] == 2);
+        CX_TEST_ASSERT(arr[1] == 3);
+        CX_TEST_ASSERT(arr[2] == 5);
+        CX_TEST_ASSERT(arr[3] == 8);
+        CX_TEST_ASSERT(arr[4] == 11);
+        CX_TEST_ASSERT(arr[5] == 47);
+        CX_TEST_ASSERT(arr_size == 6);
+        CX_TEST_ASSERT(arr_capacity >= 6);
+
+        result = cx_array_simple_copy(arr, 260, &elem, 1);
+        CX_TEST_ASSERT(result != 0);
+        CX_TEST_ASSERT(errno == EOVERFLOW);
+        CX_TEST_ASSERT(arr_size == 6);
+        CX_TEST_ASSERT(arr_capacity < 128);
+    }
+    free(arr);
+}
+
+CX_TEST(test_array_copy_unsupported_width) {
+    CX_ARRAY_DECLARE_SIZED(int, arr, uint16_t);
+    cx_array_initialize(arr, 16);
+    int result;
+    CX_TEST_DO {
+        int elem = 5;
+        result = cx_array_copy(
+            (void **) &(arr),
+            &(arr_size),
+            &(arr_capacity),
+            12, // unsupported width
+            5,
+            &elem, sizeof(int),
+            1,
+            cx_array_default_reallocator
+        );
+        CX_TEST_ASSERT(result != 0);
+        CX_TEST_ASSERT(errno == EINVAL);
+        CX_TEST_ASSERT(arr_size == 0);
+        CX_TEST_ASSERT(arr_capacity == 16);
+    }
+    free(arr);
+}
+
 CX_TEST(test_array_insert_sorted) {
     int d1 = 50;
     int d2 = 80;
@@ -1808,6 +1877,8 @@
     CxTestSuite *suite = cx_test_suite_new("array_list");
 
     cx_test_register(suite, test_array_add);
+    cx_test_register(suite, test_array_add8);
+    cx_test_register(suite, test_array_copy_unsupported_width);
     cx_test_register(suite, test_array_insert_sorted);
     cx_test_register(suite, test_array_binary_search);
 

mercurial