1.1 --- a/tests/test_list.c Wed Jan 24 22:19:05 2024 +0100 1.2 +++ b/tests/test_list.c Thu Jan 25 22:01:12 2024 +0100 1.3 @@ -36,6 +36,68 @@ 1.4 1.5 #include <stdarg.h> 1.6 1.7 +CX_TEST(test_array_add) { 1.8 + int stackspace[5] = {1,1,2,3,5}; 1.9 + int *stackarray = stackspace; 1.10 + size_t stackarray_size = 3; 1.11 + size_t stackarray_capacity = 5; 1.12 + int *heaparray = calloc(5, sizeof(int)); 1.13 + heaparray[0] = 2; 1.14 + heaparray[1] = 3; 1.15 + heaparray[2] = 5; 1.16 + heaparray[3] = 7; 1.17 + heaparray[4] = 11; 1.18 + size_t heaparray_size = 3; 1.19 + size_t heaparray_capacity = 5; 1.20 + int elem = 8, elem2 = 47; 1.21 + enum cx_array_copy_result result; 1.22 + CX_TEST_DO { 1.23 + result = cx_array_add(&stackarray, &stackarray_size, &stackarray_capacity, sizeof(int), &elem, NULL); 1.24 + CX_TEST_ASSERT(result == CX_ARRAY_COPY_SUCCESS); 1.25 + CX_TEST_ASSERT(stackarray[0] == 1); 1.26 + CX_TEST_ASSERT(stackarray[1] == 1); 1.27 + CX_TEST_ASSERT(stackarray[2] == 2); 1.28 + CX_TEST_ASSERT(stackarray[3] == 8); 1.29 + CX_TEST_ASSERT(stackarray[4] == 5); 1.30 + CX_TEST_ASSERT(stackarray_size == 4); 1.31 + CX_TEST_ASSERT(stackarray_capacity == 5); 1.32 + 1.33 + stackarray_size = 5; 1.34 + result = cx_array_add(&stackarray, &stackarray_size, &stackarray_capacity, sizeof(int), &elem2, NULL); 1.35 + CX_TEST_ASSERT(result == CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED); 1.36 + CX_TEST_ASSERT(stackarray[0] == 1); 1.37 + CX_TEST_ASSERT(stackarray[1] == 1); 1.38 + CX_TEST_ASSERT(stackarray[2] == 2); 1.39 + CX_TEST_ASSERT(stackarray[3] == 8); 1.40 + CX_TEST_ASSERT(stackarray[4] == 5); 1.41 + CX_TEST_ASSERT(stackarray_size == 5); 1.42 + CX_TEST_ASSERT(stackarray_capacity == 5); 1.43 + 1.44 + result = cx_array_add(&heaparray, &heaparray_size, &heaparray_capacity, sizeof(int), &elem, cx_array_default_reallocator); 1.45 + CX_TEST_ASSERT(result == CX_ARRAY_COPY_SUCCESS); 1.46 + CX_TEST_ASSERT(heaparray[0] == 2); 1.47 + CX_TEST_ASSERT(heaparray[1] == 3); 1.48 + CX_TEST_ASSERT(heaparray[2] == 5); 1.49 + CX_TEST_ASSERT(heaparray[3] == 8); 1.50 + CX_TEST_ASSERT(heaparray[4] == 11); 1.51 + CX_TEST_ASSERT(heaparray_size == 4); 1.52 + CX_TEST_ASSERT(heaparray_capacity == 5); 1.53 + 1.54 + heaparray_size = 5; 1.55 + result = cx_array_add(&heaparray, &heaparray_size, &heaparray_capacity, sizeof(int), &elem2, cx_array_default_reallocator); 1.56 + CX_TEST_ASSERT(result == CX_ARRAY_COPY_SUCCESS); 1.57 + CX_TEST_ASSERT(heaparray[0] == 2); 1.58 + CX_TEST_ASSERT(heaparray[1] == 3); 1.59 + CX_TEST_ASSERT(heaparray[2] == 5); 1.60 + CX_TEST_ASSERT(heaparray[3] == 8); 1.61 + CX_TEST_ASSERT(heaparray[4] == 11); 1.62 + CX_TEST_ASSERT(heaparray[5] == 47); 1.63 + CX_TEST_ASSERT(heaparray_size == 6); 1.64 + CX_TEST_ASSERT(heaparray_capacity >= 6); 1.65 + } 1.66 + free(heaparray); 1.67 +} 1.68 + 1.69 typedef struct node { 1.70 struct node *next; 1.71 struct node *prev; 1.72 @@ -1344,6 +1406,8 @@ 1.73 CxTestSuite *cx_test_suite_array_list(void) { 1.74 CxTestSuite *suite = cx_test_suite_new("array_list"); 1.75 1.76 + cx_test_register(suite, test_array_add); 1.77 + 1.78 cx_test_register(suite, test_list_arl_create); 1.79 cx_test_register(suite, test_list_arl_create_simple); 1.80 cx_test_register(suite, test_list_arl_create_simple_for_pointers);