34 #include "cx/linked_list.h" |
34 #include "cx/linked_list.h" |
35 |
35 |
36 #include <stdarg.h> |
36 #include <stdarg.h> |
37 |
37 |
38 CX_TEST(test_array_add) { |
38 CX_TEST(test_array_add) { |
39 int stackspace[5] = {1,1,2,3,5}; |
39 CX_ARRAY_DECLARE(int, arr); |
40 int *stackarray = stackspace; |
40 arr = calloc(5, sizeof(int)); |
41 size_t stackarray_size = 3; |
41 arr[0] = 2; |
42 size_t stackarray_capacity = 5; |
42 arr[1] = 3; |
43 CX_ARRAY_DECLARE(int, heaparray); |
43 arr[2] = 5; |
44 heaparray = calloc(5, sizeof(int)); |
44 arr[3] = 7; |
45 heaparray[0] = 2; |
45 arr[4] = 11; |
46 heaparray[1] = 3; |
46 arr_size = 3; |
47 heaparray[2] = 5; |
47 arr_capacity = 5; |
48 heaparray[3] = 7; |
|
49 heaparray[4] = 11; |
|
50 heaparray_size = 3; |
|
51 heaparray_capacity = 5; |
|
52 int elem = 8, elem2 = 47; |
48 int elem = 8, elem2 = 47; |
53 enum cx_array_result result; |
49 int result; |
54 CX_TEST_DO { |
50 CX_TEST_DO { |
55 result = cx_array_add(&stackarray, &stackarray_size, &stackarray_capacity, sizeof(int), &elem, NULL); |
51 result = cx_array_simple_add(arr, elem); |
56 CX_TEST_ASSERT(result == CX_ARRAY_SUCCESS); |
52 CX_TEST_ASSERT(result == 0); |
57 CX_TEST_ASSERT(stackarray[0] == 1); |
53 CX_TEST_ASSERT(arr[0] == 2); |
58 CX_TEST_ASSERT(stackarray[1] == 1); |
54 CX_TEST_ASSERT(arr[1] == 3); |
59 CX_TEST_ASSERT(stackarray[2] == 2); |
55 CX_TEST_ASSERT(arr[2] == 5); |
60 CX_TEST_ASSERT(stackarray[3] == 8); |
56 CX_TEST_ASSERT(arr[3] == 8); |
61 CX_TEST_ASSERT(stackarray[4] == 5); |
57 CX_TEST_ASSERT(arr[4] == 11); |
62 CX_TEST_ASSERT(stackarray_size == 4); |
58 CX_TEST_ASSERT(arr_size == 4); |
63 CX_TEST_ASSERT(stackarray_capacity == 5); |
59 CX_TEST_ASSERT(arr_capacity == 5); |
64 |
60 |
65 stackarray_size = 5; |
61 arr_size = 5; |
66 result = cx_array_add(&stackarray, &stackarray_size, &stackarray_capacity, sizeof(int), &elem2, NULL); |
62 result = cx_array_simple_add(arr, elem2); |
67 CX_TEST_ASSERT(result == CX_ARRAY_REALLOC_NOT_SUPPORTED); |
63 CX_TEST_ASSERT(result == 0); |
68 CX_TEST_ASSERT(stackarray[0] == 1); |
64 CX_TEST_ASSERT(arr[0] == 2); |
69 CX_TEST_ASSERT(stackarray[1] == 1); |
65 CX_TEST_ASSERT(arr[1] == 3); |
70 CX_TEST_ASSERT(stackarray[2] == 2); |
66 CX_TEST_ASSERT(arr[2] == 5); |
71 CX_TEST_ASSERT(stackarray[3] == 8); |
67 CX_TEST_ASSERT(arr[3] == 8); |
72 CX_TEST_ASSERT(stackarray[4] == 5); |
68 CX_TEST_ASSERT(arr[4] == 11); |
73 CX_TEST_ASSERT(stackarray_size == 5); |
69 CX_TEST_ASSERT(arr[5] == 47); |
74 CX_TEST_ASSERT(stackarray_capacity == 5); |
70 CX_TEST_ASSERT(arr_size == 6); |
75 |
71 CX_TEST_ASSERT(arr_capacity >= 6); |
76 result = cx_array_simple_add(heaparray, elem); |
72 } |
77 CX_TEST_ASSERT(result == CX_ARRAY_SUCCESS); |
73 free(arr); |
78 CX_TEST_ASSERT(heaparray[0] == 2); |
|
79 CX_TEST_ASSERT(heaparray[1] == 3); |
|
80 CX_TEST_ASSERT(heaparray[2] == 5); |
|
81 CX_TEST_ASSERT(heaparray[3] == 8); |
|
82 CX_TEST_ASSERT(heaparray[4] == 11); |
|
83 CX_TEST_ASSERT(heaparray_size == 4); |
|
84 CX_TEST_ASSERT(heaparray_capacity == 5); |
|
85 |
|
86 heaparray_size = 5; |
|
87 result = cx_array_simple_add(heaparray, elem2); |
|
88 CX_TEST_ASSERT(result == CX_ARRAY_SUCCESS); |
|
89 CX_TEST_ASSERT(heaparray[0] == 2); |
|
90 CX_TEST_ASSERT(heaparray[1] == 3); |
|
91 CX_TEST_ASSERT(heaparray[2] == 5); |
|
92 CX_TEST_ASSERT(heaparray[3] == 8); |
|
93 CX_TEST_ASSERT(heaparray[4] == 11); |
|
94 CX_TEST_ASSERT(heaparray[5] == 47); |
|
95 CX_TEST_ASSERT(heaparray_size == 6); |
|
96 CX_TEST_ASSERT(heaparray_capacity >= 6); |
|
97 } |
|
98 free(heaparray); |
|
99 } |
74 } |
100 |
75 |
101 CX_TEST(test_array_insert_sorted) { |
76 CX_TEST(test_array_insert_sorted) { |
102 int d1 = 50; |
77 int d1 = 50; |
103 int d2 = 80; |
78 int d2 = 80; |