32 |
32 |
33 #include "cx/array_list.h" |
33 #include "cx/array_list.h" |
34 #include "cx/linked_list.h" |
34 #include "cx/linked_list.h" |
35 |
35 |
36 #include <stdarg.h> |
36 #include <stdarg.h> |
|
37 #include <errno.h> |
37 |
38 |
38 CX_TEST(test_array_add) { |
39 CX_TEST(test_array_add) { |
39 CX_ARRAY_DECLARE(int, arr); |
40 CX_ARRAY_DECLARE(int, arr); |
40 arr = calloc(5, sizeof(int)); |
41 arr = calloc(5, sizeof(int)); |
41 arr[0] = 2; |
42 arr[0] = 2; |
67 CX_TEST_ASSERT(arr[3] == 8); |
68 CX_TEST_ASSERT(arr[3] == 8); |
68 CX_TEST_ASSERT(arr[4] == 11); |
69 CX_TEST_ASSERT(arr[4] == 11); |
69 CX_TEST_ASSERT(arr[5] == 47); |
70 CX_TEST_ASSERT(arr[5] == 47); |
70 CX_TEST_ASSERT(arr_size == 6); |
71 CX_TEST_ASSERT(arr_size == 6); |
71 CX_TEST_ASSERT(arr_capacity >= 6); |
72 CX_TEST_ASSERT(arr_capacity >= 6); |
|
73 } |
|
74 free(arr); |
|
75 } |
|
76 |
|
77 CX_TEST(test_array_add8) { |
|
78 CX_ARRAY_DECLARE_SIZED(int, arr, uint8_t); |
|
79 arr = calloc(5, sizeof(int)); |
|
80 arr[0] = 2; |
|
81 arr[1] = 3; |
|
82 arr[2] = 5; |
|
83 arr[3] = 7; |
|
84 arr[4] = 11; |
|
85 arr_size = 3; |
|
86 arr_capacity = 5; |
|
87 int elem = 8, elem2 = 47; |
|
88 int result; |
|
89 CX_TEST_DO { |
|
90 result = cx_array_simple_add(arr, elem); |
|
91 CX_TEST_ASSERT(result == 0); |
|
92 CX_TEST_ASSERT(arr[0] == 2); |
|
93 CX_TEST_ASSERT(arr[1] == 3); |
|
94 CX_TEST_ASSERT(arr[2] == 5); |
|
95 CX_TEST_ASSERT(arr[3] == 8); |
|
96 CX_TEST_ASSERT(arr[4] == 11); |
|
97 CX_TEST_ASSERT(arr_size == 4); |
|
98 CX_TEST_ASSERT(arr_capacity == 5); |
|
99 |
|
100 arr_size = 5; |
|
101 result = cx_array_simple_add(arr, elem2); |
|
102 CX_TEST_ASSERT(result == 0); |
|
103 CX_TEST_ASSERT(arr[0] == 2); |
|
104 CX_TEST_ASSERT(arr[1] == 3); |
|
105 CX_TEST_ASSERT(arr[2] == 5); |
|
106 CX_TEST_ASSERT(arr[3] == 8); |
|
107 CX_TEST_ASSERT(arr[4] == 11); |
|
108 CX_TEST_ASSERT(arr[5] == 47); |
|
109 CX_TEST_ASSERT(arr_size == 6); |
|
110 CX_TEST_ASSERT(arr_capacity >= 6); |
|
111 |
|
112 result = cx_array_simple_copy(arr, 260, &elem, 1); |
|
113 CX_TEST_ASSERT(result != 0); |
|
114 CX_TEST_ASSERT(errno == EOVERFLOW); |
|
115 CX_TEST_ASSERT(arr_size == 6); |
|
116 CX_TEST_ASSERT(arr_capacity < 128); |
|
117 } |
|
118 free(arr); |
|
119 } |
|
120 |
|
121 CX_TEST(test_array_copy_unsupported_width) { |
|
122 CX_ARRAY_DECLARE_SIZED(int, arr, uint16_t); |
|
123 cx_array_initialize(arr, 16); |
|
124 int result; |
|
125 CX_TEST_DO { |
|
126 int elem = 5; |
|
127 result = cx_array_copy( |
|
128 (void **) &(arr), |
|
129 &(arr_size), |
|
130 &(arr_capacity), |
|
131 12, // unsupported width |
|
132 5, |
|
133 &elem, sizeof(int), |
|
134 1, |
|
135 cx_array_default_reallocator |
|
136 ); |
|
137 CX_TEST_ASSERT(result != 0); |
|
138 CX_TEST_ASSERT(errno == EINVAL); |
|
139 CX_TEST_ASSERT(arr_size == 0); |
|
140 CX_TEST_ASSERT(arr_capacity == 16); |
72 } |
141 } |
73 free(arr); |
142 free(arr); |
74 } |
143 } |
75 |
144 |
76 CX_TEST(test_array_insert_sorted) { |
145 CX_TEST(test_array_insert_sorted) { |
1806 |
1875 |
1807 CxTestSuite *cx_test_suite_array_list(void) { |
1876 CxTestSuite *cx_test_suite_array_list(void) { |
1808 CxTestSuite *suite = cx_test_suite_new("array_list"); |
1877 CxTestSuite *suite = cx_test_suite_new("array_list"); |
1809 |
1878 |
1810 cx_test_register(suite, test_array_add); |
1879 cx_test_register(suite, test_array_add); |
|
1880 cx_test_register(suite, test_array_add8); |
|
1881 cx_test_register(suite, test_array_copy_unsupported_width); |
1811 cx_test_register(suite, test_array_insert_sorted); |
1882 cx_test_register(suite, test_array_insert_sorted); |
1812 cx_test_register(suite, test_array_binary_search); |
1883 cx_test_register(suite, test_array_binary_search); |
1813 |
1884 |
1814 cx_test_register(suite, test_list_arl_create); |
1885 cx_test_register(suite, test_list_arl_create); |
1815 cx_test_register(suite, test_list_arl_create_simple); |
1886 cx_test_register(suite, test_list_arl_create_simple); |