| 68 CX_TEST_ASSERT(arr.data[3] == 8); |
68 CX_TEST_ASSERT(arr.data[3] == 8); |
| 69 CX_TEST_ASSERT(arr.data[4] == 11); |
69 CX_TEST_ASSERT(arr.data[4] == 11); |
| 70 CX_TEST_ASSERT(arr.data[5] == 47); |
70 CX_TEST_ASSERT(arr.data[5] == 47); |
| 71 CX_TEST_ASSERT(arr.size == 6); |
71 CX_TEST_ASSERT(arr.size == 6); |
| 72 CX_TEST_ASSERT(arr.capacity >= 6); |
72 CX_TEST_ASSERT(arr.capacity >= 6); |
| |
73 } |
| |
74 cx_array_free(arr); |
| |
75 } |
| |
76 |
| |
77 CX_TEST(test_array_remove) { |
| |
78 CX_ARRAY(int, arr); |
| |
79 cx_array_init(arr, 5); |
| |
80 arr.data[0] = 2; |
| |
81 arr.data[1] = 3; |
| |
82 arr.data[2] = 5; |
| |
83 arr.data[3] = 7; |
| |
84 arr.data[4] = 11; |
| |
85 arr.size = 5; |
| |
86 int expected[5]; |
| |
87 const size_t len = 5*sizeof(int); |
| |
88 memcpy(expected, arr.data, len); |
| |
89 CX_TEST_DO { |
| |
90 // out-of-bounds |
| |
91 cx_array_remove(arr, 7); |
| |
92 CX_TEST_ASSERT(arr.capacity == 5); |
| |
93 CX_TEST_ASSERT(arr.size == 5); |
| |
94 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); |
| |
95 |
| |
96 // remove mid |
| |
97 cx_array_remove(arr, 2); |
| |
98 CX_TEST_ASSERT(arr.capacity == 5); |
| |
99 CX_TEST_ASSERT(arr.size == 4); |
| |
100 expected[2] = 7; |
| |
101 expected[3] = 11; |
| |
102 expected[4] = 11; // not cleared |
| |
103 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); |
| |
104 |
| |
105 // remove end |
| |
106 cx_array_remove(arr, 3); |
| |
107 CX_TEST_ASSERT(arr.capacity == 5); |
| |
108 CX_TEST_ASSERT(arr.size == 3); |
| |
109 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); |
| |
110 } |
| |
111 cx_array_free(arr); |
| |
112 } |
| |
113 |
| |
114 CX_TEST(test_array_remove_fast) { |
| |
115 CX_ARRAY(int, arr); |
| |
116 cx_array_init(arr, 5); |
| |
117 arr.data[0] = 2; |
| |
118 arr.data[1] = 3; |
| |
119 arr.data[2] = 5; |
| |
120 arr.data[3] = 7; |
| |
121 arr.data[4] = 11; |
| |
122 arr.size = 5; |
| |
123 int expected[5]; |
| |
124 const size_t len = 5*sizeof(int); |
| |
125 memcpy(expected, arr.data, len); |
| |
126 CX_TEST_DO { |
| |
127 // out-of-bounds |
| |
128 cx_array_remove_fast(arr, 7); |
| |
129 CX_TEST_ASSERT(arr.capacity == 5); |
| |
130 CX_TEST_ASSERT(arr.size == 5); |
| |
131 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); |
| |
132 |
| |
133 // remove mid |
| |
134 cx_array_remove_fast(arr, 2); |
| |
135 CX_TEST_ASSERT(arr.capacity == 5); |
| |
136 CX_TEST_ASSERT(arr.size == 4); |
| |
137 expected[2] = 11; |
| |
138 expected[3] = 7; |
| |
139 expected[4] = 11; // not cleared |
| |
140 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); |
| |
141 |
| |
142 // remove end |
| |
143 cx_array_remove_fast(arr, 3); |
| |
144 CX_TEST_ASSERT(arr.capacity == 5); |
| |
145 CX_TEST_ASSERT(arr.size == 3); |
| |
146 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); |
| |
147 } |
| |
148 cx_array_free(arr); |
| |
149 } |
| |
150 |
| |
151 CX_TEST(test_array_remove_array) { |
| |
152 CX_ARRAY(int, arr); |
| |
153 cx_array_init(arr, 10); |
| |
154 arr.data[0] = 2; |
| |
155 arr.data[1] = 3; |
| |
156 arr.data[2] = 5; |
| |
157 arr.data[3] = 7; |
| |
158 arr.data[4] = 11; |
| |
159 arr.data[5] = 47; |
| |
160 arr.data[6] = 80; |
| |
161 arr.data[7] = 104; |
| |
162 arr.data[8] = 250; |
| |
163 arr.data[9] = 999; |
| |
164 arr.size = 10; |
| |
165 int expected[10]; |
| |
166 const size_t len = 5*sizeof(int); |
| |
167 memcpy(expected, arr.data, len); |
| |
168 CX_TEST_DO { |
| |
169 // completely out-of-bounds |
| |
170 cx_array_remove_array(arr, 10, 3); |
| |
171 CX_TEST_ASSERT(arr.capacity == 10); |
| |
172 CX_TEST_ASSERT(arr.size == 10); |
| |
173 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); |
| |
174 |
| |
175 // somewhere in the middle |
| |
176 cx_array_remove_array(arr, 2, 3); |
| |
177 CX_TEST_ASSERT(arr.capacity == 10); |
| |
178 CX_TEST_ASSERT(arr.size == 7); |
| |
179 expected[2] = 47; |
| |
180 expected[3] = 80; |
| |
181 expected[4] = 104; |
| |
182 expected[5] = 250; |
| |
183 expected[6] = 999; |
| |
184 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); |
| |
185 |
| |
186 // partially out-of-bounds |
| |
187 cx_array_remove_array(arr, 5, 4); |
| |
188 CX_TEST_ASSERT(arr.capacity == 10); |
| |
189 CX_TEST_ASSERT(arr.size == 5); |
| |
190 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); |
| |
191 } |
| |
192 cx_array_free(arr); |
| |
193 } |
| |
194 |
| |
195 CX_TEST(test_array_remove_array_fast) { |
| |
196 CX_ARRAY(int, arr); |
| |
197 cx_array_init(arr, 10); |
| |
198 arr.data[0] = 2; |
| |
199 arr.data[1] = 3; |
| |
200 arr.data[2] = 5; |
| |
201 arr.data[3] = 7; |
| |
202 arr.data[4] = 11; |
| |
203 arr.data[5] = 47; |
| |
204 arr.data[6] = 80; |
| |
205 arr.data[7] = 104; |
| |
206 arr.data[8] = 250; |
| |
207 arr.data[9] = 999; |
| |
208 arr.size = 10; |
| |
209 int expected[10]; |
| |
210 const size_t len = 5*sizeof(int); |
| |
211 memcpy(expected, arr.data, len); |
| |
212 CX_TEST_DO { |
| |
213 // completely out-of-bounds |
| |
214 cx_array_remove_array_fast(arr, 10, 3); |
| |
215 CX_TEST_ASSERT(arr.capacity == 10); |
| |
216 CX_TEST_ASSERT(arr.size == 10); |
| |
217 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); |
| |
218 |
| |
219 // somewhere in the middle |
| |
220 cx_array_remove_array_fast(arr, 2, 3); |
| |
221 CX_TEST_ASSERT(arr.capacity == 10); |
| |
222 CX_TEST_ASSERT(arr.size == 7); |
| |
223 expected[2] = 104; |
| |
224 expected[3] = 250; |
| |
225 expected[4] = 999; |
| |
226 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); |
| |
227 |
| |
228 // partially out-of-bounds |
| |
229 cx_array_remove_array_fast(arr, 5, 4); |
| |
230 CX_TEST_ASSERT(arr.capacity == 10); |
| |
231 CX_TEST_ASSERT(arr.size == 5); |
| |
232 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); |
| 73 } |
233 } |
| 74 cx_array_free(arr); |
234 cx_array_free(arr); |
| 75 } |
235 } |
| 76 |
236 |
| 77 CX_TEST(test_array_reserve) { |
237 CX_TEST(test_array_reserve) { |
| 3275 cxListFree(defaulted_list); |
3435 cxListFree(defaulted_list); |
| 3276 cxListFree(linked_list); |
3436 cxListFree(linked_list); |
| 3277 cxListFree(array_list); |
3437 cxListFree(array_list); |
| 3278 } |
3438 } |
| 3279 |
3439 |
| 3280 CxTestSuite *cx_test_suite_array_list(void) { |
3440 CxTestSuite *cx_test_suite_array(void) { |
| 3281 CxTestSuite *suite = cx_test_suite_new("array_list"); |
3441 CxTestSuite *suite = cx_test_suite_new("array (low-level)"); |
| 3282 |
3442 |
| 3283 cx_test_register(suite, test_array_add); |
3443 cx_test_register(suite, test_array_add); |
| |
3444 cx_test_register(suite, test_array_remove); |
| |
3445 cx_test_register(suite, test_array_remove_fast); |
| |
3446 cx_test_register(suite, test_array_remove_array); |
| |
3447 cx_test_register(suite, test_array_remove_array_fast); |
| 3284 cx_test_register(suite, test_array_reserve); |
3448 cx_test_register(suite, test_array_reserve); |
| 3285 cx_test_register(suite, test_array_copy_to_new); |
3449 cx_test_register(suite, test_array_copy_to_new); |
| 3286 cx_test_register(suite, test_array_insert_sorted); |
3450 cx_test_register(suite, test_array_insert_sorted); |
| 3287 cx_test_register(suite, test_array_insert_unique); |
3451 cx_test_register(suite, test_array_insert_unique); |
| 3288 cx_test_register(suite, test_array_binary_search); |
3452 cx_test_register(suite, test_array_binary_search); |
| 3289 cx_test_register(suite, test_array_binary_search_with_duplicates); |
3453 cx_test_register(suite, test_array_binary_search_with_duplicates); |
| |
3454 |
| |
3455 return suite; |
| |
3456 } |
| |
3457 |
| |
3458 CxTestSuite *cx_test_suite_array_list(void) { |
| |
3459 CxTestSuite *suite = cx_test_suite_new("array_list"); |
| 3290 |
3460 |
| 3291 cx_test_register(suite, test_list_arl_create); |
3461 cx_test_register(suite, test_list_arl_create); |
| 3292 cx_test_register(suite, test_list_arl_create_simple); |
3462 cx_test_register(suite, test_list_arl_create_simple); |
| 3293 cx_test_register(suite, test_list_arl_create_simple_for_pointers); |
3463 cx_test_register(suite, test_list_arl_create_simple_for_pointers); |
| 3294 cx_test_register(suite, test_list_parl_destroy_no_destr); |
3464 cx_test_register(suite, test_list_parl_destroy_no_destr); |