| 142 } |
142 } |
| 143 cxFreeDefault(arr); |
143 cxFreeDefault(arr); |
| 144 } |
144 } |
| 145 |
145 |
| 146 CX_TEST(test_array_reserve) { |
146 CX_TEST(test_array_reserve) { |
| |
147 { |
| |
148 // default size |
| |
149 CX_ARRAY_DECLARE(int, arr); |
| |
150 cx_array_initialize(arr, 16); |
| |
151 int result; |
| |
152 CX_TEST_DO { |
| |
153 result = cx_array_reserve( |
| |
154 (void **) &(arr), |
| |
155 &(arr_size), |
| |
156 &(arr_capacity), |
| |
157 0, // default width |
| |
158 sizeof(int), |
| |
159 30, |
| |
160 cx_array_default_reallocator |
| |
161 ); |
| |
162 CX_TEST_ASSERT(result == 0); |
| |
163 CX_TEST_ASSERT(arr_size == 0); |
| |
164 CX_TEST_ASSERT(arr_capacity == 30); |
| |
165 } |
| |
166 cxFreeDefault(arr); |
| |
167 } |
| |
168 { |
| |
169 // 16-bit size |
| |
170 CX_ARRAY_DECLARE_SIZED(char, arr, uint16_t); |
| |
171 cx_array_initialize(arr, 16); |
| |
172 arr_size = 5; |
| |
173 int result; |
| |
174 CX_TEST_DO { |
| |
175 errno = 0; |
| |
176 result = cx_array_simple_reserve(arr, 3); |
| |
177 CX_TEST_ASSERT(result == 0); |
| |
178 CX_TEST_ASSERT(errno == 0); |
| |
179 CX_TEST_ASSERT(arr_size == 5); |
| |
180 CX_TEST_ASSERT(arr_capacity == 16); |
| |
181 |
| |
182 result = cx_array_simple_reserve(arr, 20); |
| |
183 CX_TEST_ASSERT(result == 0); |
| |
184 CX_TEST_ASSERT(errno == 0); |
| |
185 CX_TEST_ASSERT(arr_size == 5); |
| |
186 CX_TEST_ASSERT(arr_capacity == 25); |
| |
187 |
| |
188 result = cx_array_simple_reserve(arr, 2000); |
| |
189 CX_TEST_ASSERT(result == 0); |
| |
190 CX_TEST_ASSERT(errno == 0); |
| |
191 CX_TEST_ASSERT(arr_size == 5); |
| |
192 CX_TEST_ASSERT(arr_capacity == 2005); |
| |
193 |
| |
194 // this does not fit into an array with a 16-bit size |
| |
195 result = cx_array_simple_reserve(arr, 70000); |
| |
196 CX_TEST_ASSERT(result == 1); |
| |
197 CX_TEST_ASSERT(errno == EOVERFLOW); |
| |
198 CX_TEST_ASSERT(arr_size == 5); |
| |
199 CX_TEST_ASSERT(arr_capacity == 2005); |
| |
200 } |
| |
201 cxFreeDefault(arr); |
| |
202 } |
| |
203 { |
| |
204 // 8-bit size |
| |
205 CX_ARRAY_DECLARE_SIZED(char, arr, uint8_t); |
| |
206 cx_array_initialize(arr, 16); |
| |
207 arr_size = 5; |
| |
208 int result; |
| |
209 CX_TEST_DO { |
| |
210 errno = 0; |
| |
211 result = cx_array_simple_reserve(arr, 3); |
| |
212 CX_TEST_ASSERT(result == 0); |
| |
213 CX_TEST_ASSERT(errno == 0); |
| |
214 CX_TEST_ASSERT(arr_size == 5); |
| |
215 CX_TEST_ASSERT(arr_capacity == 16); |
| |
216 |
| |
217 result = cx_array_simple_reserve(arr, 20); |
| |
218 CX_TEST_ASSERT(result == 0); |
| |
219 CX_TEST_ASSERT(errno == 0); |
| |
220 CX_TEST_ASSERT(arr_size == 5); |
| |
221 CX_TEST_ASSERT(arr_capacity == 25); |
| |
222 |
| |
223 // this does not fit into an array with an 8-bit size |
| |
224 arr_size = 10; |
| |
225 result = cx_array_simple_reserve(arr, 250); |
| |
226 CX_TEST_ASSERT(result == 1); |
| |
227 CX_TEST_ASSERT(errno == EOVERFLOW); |
| |
228 CX_TEST_ASSERT(arr_size == 10); |
| |
229 CX_TEST_ASSERT(arr_capacity == 25); |
| |
230 } |
| |
231 cxFreeDefault(arr); |
| |
232 } |
| |
233 } |
| |
234 |
| |
235 CX_TEST(test_array_reserve_unsupported_width) { |
| 147 CX_ARRAY_DECLARE_SIZED(int, arr, uint16_t); |
236 CX_ARRAY_DECLARE_SIZED(int, arr, uint16_t); |
| 148 cx_array_initialize(arr, 16); |
237 cx_array_initialize(arr, 16); |
| 149 arr_size = 5; |
|
| 150 int result; |
238 int result; |
| 151 CX_TEST_DO { |
239 CX_TEST_DO { |
| 152 result = cx_array_simple_reserve(arr, 3); |
240 result = cx_array_reserve( |
| 153 CX_TEST_ASSERT(result == 0); |
241 (void **) &(arr), |
| 154 CX_TEST_ASSERT(arr_size == 5); |
242 &(arr_size), |
| |
243 &(arr_capacity), |
| |
244 12, // unsupported width |
| |
245 sizeof(int), |
| |
246 30, |
| |
247 cx_array_default_reallocator |
| |
248 ); |
| |
249 CX_TEST_ASSERT(result != 0); |
| |
250 CX_TEST_ASSERT(errno == EINVAL); |
| |
251 CX_TEST_ASSERT(arr_size == 0); |
| 155 CX_TEST_ASSERT(arr_capacity == 16); |
252 CX_TEST_ASSERT(arr_capacity == 16); |
| 156 |
|
| 157 result = cx_array_simple_reserve(arr, 20); |
|
| 158 CX_TEST_ASSERT(result == 0); |
|
| 159 CX_TEST_ASSERT(arr_size == 5); |
|
| 160 CX_TEST_ASSERT(arr_capacity == 25); |
|
| 161 } |
253 } |
| 162 cxFreeDefault(arr); |
254 cxFreeDefault(arr); |
| 163 } |
255 } |
| 164 |
256 |
| 165 CX_TEST(test_array_insert_sorted) { |
257 CX_TEST(test_array_insert_sorted) { |
| 3194 |
3286 |
| 3195 cx_test_register(suite, test_array_add); |
3287 cx_test_register(suite, test_array_add); |
| 3196 cx_test_register(suite, test_array_add8); |
3288 cx_test_register(suite, test_array_add8); |
| 3197 cx_test_register(suite, test_array_copy_unsupported_width); |
3289 cx_test_register(suite, test_array_copy_unsupported_width); |
| 3198 cx_test_register(suite, test_array_reserve); |
3290 cx_test_register(suite, test_array_reserve); |
| |
3291 cx_test_register(suite, test_array_reserve_unsupported_width); |
| 3199 cx_test_register(suite, test_array_insert_sorted); |
3292 cx_test_register(suite, test_array_insert_sorted); |
| 3200 cx_test_register(suite, test_array_insert_unique); |
3293 cx_test_register(suite, test_array_insert_unique); |
| 3201 cx_test_register(suite, test_array_binary_search); |
3294 cx_test_register(suite, test_array_binary_search); |
| 3202 |
3295 |
| 3203 cx_test_register(suite, test_list_arl_create); |
3296 cx_test_register(suite, test_list_arl_create); |