1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "array_tests.h" |
|
30 #include <ucx/utils.h> |
|
31 |
|
32 UCX_TEST(test_ucx_array_destroy) { |
|
33 UcxArray array; |
|
34 ucx_array_init(&array, 16, sizeof(int)); |
|
35 |
|
36 UCX_TEST_BEGIN |
|
37 ucx_array_destroy(&array); |
|
38 UCX_TEST_ASSERT(array.data == NULL, "data pointer not NULL after destroy"); |
|
39 UCX_TEST_ASSERT(array.size == 0, "size not zero after destroy"); |
|
40 UCX_TEST_ASSERT(array.capacity == 0, "capacity not zero after destroy"); |
|
41 UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(), |
|
42 "allocator corrupted during destroy"); |
|
43 UCX_TEST_END |
|
44 } |
|
45 |
|
46 UCX_TEST(test_ucx_array_new) { |
|
47 UcxArray* array = ucx_array_new(16, 47); |
|
48 |
|
49 UCX_TEST_BEGIN |
|
50 UCX_TEST_ASSERT(array->data, "no memory allocated"); |
|
51 UCX_TEST_ASSERT(array->size == 0, "size not initially zero"); |
|
52 UCX_TEST_ASSERT(array->capacity == 16, "capacity not as requested"); |
|
53 UCX_TEST_ASSERT(array->elemsize == 47, "element size not as requested"); |
|
54 UCX_TEST_ASSERT(array->allocator == ucx_default_allocator(), |
|
55 "array not using the default allocator"); |
|
56 UCX_TEST_END |
|
57 ucx_array_free(array); |
|
58 } |
|
59 |
|
60 UCX_TEST(test_ucx_array_append_from) { |
|
61 UcxArray *array = ucx_array_new(16, sizeof(int)); |
|
62 int *elements; |
|
63 |
|
64 int x = 42; |
|
65 ucx_array_append_from(array, &x, 1); |
|
66 UCX_TEST_BEGIN |
|
67 |
|
68 elements = array->data; |
|
69 UCX_TEST_ASSERT(elements[0] == 42, "failed"); |
|
70 |
|
71 int y[2] = {13, 37}; |
|
72 ucx_array_append_from(array, y, 2); |
|
73 |
|
74 elements = array->data; |
|
75 UCX_TEST_ASSERT(array->size == 3, "incorrect size after append"); |
|
76 UCX_TEST_ASSERT(elements[1] == 13, "failed"); |
|
77 UCX_TEST_ASSERT(elements[2] == 37, "failed"); |
|
78 UCX_TEST_ASSERT(elements[0] == 42, |
|
79 "append corrupted previously inserted data"); |
|
80 |
|
81 ucx_array_append_from(array, NULL, 2); |
|
82 |
|
83 elements = array->data; |
|
84 UCX_TEST_ASSERT(array->size == 5, "incorrect size after NULL append"); |
|
85 UCX_TEST_ASSERT(elements[3] == 0, "element is not zeroed"); |
|
86 UCX_TEST_ASSERT(elements[4] == 0, "element is not zeroed"); |
|
87 UCX_TEST_ASSERT(elements[0] == 42, |
|
88 "NULL append corrupted previously inserted data"); |
|
89 UCX_TEST_ASSERT(elements[1] == 13, |
|
90 "NULL append corrupted previously inserted data"); |
|
91 UCX_TEST_ASSERT(elements[2] == 37, |
|
92 "NULL append corrupted previously inserted data"); |
|
93 |
|
94 UCX_TEST_END |
|
95 |
|
96 ucx_array_free(array); |
|
97 } |
|
98 |
|
99 UCX_TEST(test_ucx_array_append_from_struct) { |
|
100 struct teststruct { |
|
101 unsigned long long x; |
|
102 unsigned long long y; |
|
103 unsigned long long z; |
|
104 }; |
|
105 |
|
106 UcxArray *array = ucx_array_new(16, sizeof(struct teststruct)); |
|
107 struct teststruct *elements; |
|
108 |
|
109 struct teststruct data; |
|
110 data.x = 13; data.y = 37; data.z = 47; |
|
111 |
|
112 ucx_array_append_from(array, &data, 1); |
|
113 UCX_TEST_BEGIN |
|
114 |
|
115 elements = array->data; |
|
116 UCX_TEST_ASSERT(elements[0].x == 13, "failed"); |
|
117 UCX_TEST_ASSERT(elements[0].y == 37, "failed"); |
|
118 UCX_TEST_ASSERT(elements[0].z == 47, "failed"); |
|
119 |
|
120 data.x = 0; data.y = 8; data.z = 15; |
|
121 ucx_array_append_from(array, &data, 1); |
|
122 |
|
123 elements = array->data; |
|
124 UCX_TEST_ASSERT(array->size == 2, "incorrect size after append"); |
|
125 UCX_TEST_ASSERT(elements[1].x == 0, "failed"); |
|
126 UCX_TEST_ASSERT(elements[1].y == 8, "failed"); |
|
127 UCX_TEST_ASSERT(elements[1].z == 15, "failed"); |
|
128 |
|
129 UCX_TEST_ASSERT(elements[0].x == 13, |
|
130 "append corrupted previously inserted data"); |
|
131 UCX_TEST_ASSERT(elements[0].y == 37, |
|
132 "append corrupted previously inserted data"); |
|
133 UCX_TEST_ASSERT(elements[0].z == 47, |
|
134 "append corrupted previously inserted data"); |
|
135 |
|
136 UCX_TEST_END |
|
137 |
|
138 ucx_array_destroy(array); |
|
139 } |
|
140 |
|
141 UCX_TEST(test_ucx_array_prepend_from) { |
|
142 int *elems; |
|
143 UcxArray *array = ucx_array_new(16, sizeof(int)); |
|
144 |
|
145 int x = 42; |
|
146 ucx_array_prepend_from(array, &x, 1); |
|
147 UCX_TEST_BEGIN |
|
148 |
|
149 elems = array->data; |
|
150 UCX_TEST_ASSERT(elems[0] == 42, "failed"); |
|
151 |
|
152 int y[2] = {13, 37}; |
|
153 ucx_array_prepend_from(array, y, 2); |
|
154 |
|
155 elems = array->data; |
|
156 UCX_TEST_ASSERT(array->size == 3, "incorrect size after prepend"); |
|
157 UCX_TEST_ASSERT(elems[0] == 13, "failed"); |
|
158 UCX_TEST_ASSERT(elems[1] == 37, "failed"); |
|
159 UCX_TEST_ASSERT(elems[2] == 42, |
|
160 "prepend corrupted previously inserted data"); |
|
161 |
|
162 ucx_array_prepend_from(array, NULL, 2); |
|
163 |
|
164 elems = array->data; |
|
165 UCX_TEST_ASSERT(array->size == 5, "incorrect size after NULL prepend"); |
|
166 UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed"); |
|
167 UCX_TEST_ASSERT(elems[1] == 0, "element is not zeroed"); |
|
168 UCX_TEST_ASSERT(elems[2] == 13, |
|
169 "NULL prepend corrupted previously inserted data"); |
|
170 UCX_TEST_ASSERT(elems[3] == 37, |
|
171 "NULL prepend corrupted previously inserted data"); |
|
172 UCX_TEST_ASSERT(elems[4] == 42, |
|
173 "NULL prepend corrupted previously inserted data"); |
|
174 |
|
175 UCX_TEST_END |
|
176 |
|
177 ucx_array_free(array); |
|
178 } |
|
179 |
|
180 UCX_TEST(test_ucx_array_set_from) { |
|
181 int *elems; |
|
182 UcxArray *array = ucx_array_new(16, sizeof(int)); |
|
183 |
|
184 int x = 42; |
|
185 |
|
186 UCX_TEST_BEGIN |
|
187 |
|
188 ucx_array_set_from(array, 7, &x, 1); |
|
189 |
|
190 elems = array->data; |
|
191 UCX_TEST_ASSERT(elems[7] == 42, "failed"); |
|
192 UCX_TEST_ASSERT(array->size >= 8, "array not resized on set"); |
|
193 UCX_TEST_ASSERT(array->capacity == 16, "capacity changed unnecessarily"); |
|
194 |
|
195 int y[2] = {13, 37}; |
|
196 ucx_array_set_from(array, 27, y, 2); |
|
197 |
|
198 elems = array->data; |
|
199 UCX_TEST_ASSERT(elems[27] == 13, "failed"); |
|
200 UCX_TEST_ASSERT(elems[28] == 37, "failed"); |
|
201 UCX_TEST_ASSERT(array->size == 29, "array not resized on set"); |
|
202 UCX_TEST_ASSERT(array->capacity == 32, "capacity not grown"); |
|
203 |
|
204 ucx_array_set_from(array, 7, NULL, 2); |
|
205 |
|
206 elems = array->data; |
|
207 UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set"); |
|
208 UCX_TEST_ASSERT(elems[8] == 0, "not zeroed on NULL set"); |
|
209 |
|
210 UCX_TEST_END |
|
211 |
|
212 ucx_array_free(array); |
|
213 } |
|
214 |
|
215 |
|
216 UCX_TEST(test_ucx_array_equals) { |
|
217 UcxArray *a1 = ucx_array_new(16, sizeof(int32_t)); |
|
218 UcxArray *a2 = ucx_array_new(16, sizeof(int32_t)); |
|
219 UcxArray *a3 = ucx_array_new(16, sizeof(int64_t)); |
|
220 UcxArray *a4 = ucx_array_new(16, sizeof(int32_t)); |
|
221 |
|
222 int32_t *intelems; |
|
223 int64_t *longintelems; |
|
224 |
|
225 a1->size = 5; |
|
226 intelems = a1->data; |
|
227 intelems[0] = 47; |
|
228 intelems[1] = 11; |
|
229 intelems[2] = 0; |
|
230 intelems[3] = 8; |
|
231 intelems[4] = 15; |
|
232 a2->size = 5; |
|
233 intelems = a2->data; |
|
234 intelems[0] = 47; |
|
235 intelems[1] = 11; |
|
236 intelems[2] = 0; |
|
237 intelems[3] = 8; |
|
238 intelems[4] = 15; |
|
239 a3->size = 5; |
|
240 longintelems = a3->data; |
|
241 longintelems[0] = 47; |
|
242 longintelems[1] = 11; |
|
243 longintelems[2] = 0; |
|
244 longintelems[3] = 8; |
|
245 longintelems[4] = 15; |
|
246 a4->size = 5; |
|
247 intelems = a4->data; |
|
248 intelems[0] = 47; |
|
249 intelems[1] = 11; |
|
250 intelems[2] = -6; |
|
251 intelems[3] = 8; |
|
252 intelems[4] = 15; |
|
253 |
|
254 UCX_TEST_BEGIN |
|
255 |
|
256 UCX_TEST_ASSERT(ucx_array_equals(a1, a2, ucx_cmp_int32, NULL), "failed"); |
|
257 UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, ucx_cmp_int32, NULL), "failed"); |
|
258 UCX_TEST_ASSERT(!ucx_array_equals(a4, a1, ucx_cmp_int32, NULL), "failed"); |
|
259 UCX_TEST_ASSERT(!ucx_array_equals(a1, a3, ucx_cmp_int64, NULL), |
|
260 "comparing arrays of different element size shall fail"); |
|
261 UCX_TEST_ASSERT(!ucx_array_equals(a3, a1, ucx_cmp_int64, NULL), |
|
262 "comparing arrays of different element size shall fail"); |
|
263 |
|
264 UCX_TEST_ASSERT(ucx_array_equals(a1, a2, NULL, NULL), |
|
265 "compare using memcmp() failed"); |
|
266 UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, NULL, NULL), |
|
267 "compare using memcmp() failed"); |
|
268 |
|
269 UCX_TEST_END |
|
270 ucx_array_free(a1); |
|
271 ucx_array_free(a2); |
|
272 ucx_array_free(a3); |
|
273 ucx_array_free(a4); |
|
274 } |
|
275 |
|
276 UCX_TEST(test_ucx_array_concat) { |
|
277 UcxArray *a1 = ucx_array_new(16, sizeof(int)); |
|
278 UcxArray *a2 = ucx_array_new(16, sizeof(int)); |
|
279 int *elems; |
|
280 |
|
281 a1->size = 2; |
|
282 elems = a1->data; |
|
283 elems[0] = 47; |
|
284 elems[1] = 11; |
|
285 a2->size = 3; |
|
286 elems = a2->data; |
|
287 elems[0] = 0; |
|
288 elems[1] = 8; |
|
289 elems[2] = 15; |
|
290 |
|
291 UCX_TEST_BEGIN |
|
292 |
|
293 UCX_TEST_ASSERT(!ucx_array_concat(a1, a2), "failed"); |
|
294 UCX_TEST_ASSERT(a1->size == 5, "failed"); |
|
295 elems = a1->data; |
|
296 UCX_TEST_ASSERT(elems[0] == 47, "failed"); |
|
297 UCX_TEST_ASSERT(elems[1] == 11, "failed"); |
|
298 UCX_TEST_ASSERT(elems[2] == 0, "failed"); |
|
299 UCX_TEST_ASSERT(elems[3] == 8, "failed"); |
|
300 UCX_TEST_ASSERT(elems[4] == 15, "failed"); |
|
301 |
|
302 a1->elemsize *= 2; |
|
303 UCX_TEST_ASSERT(ucx_array_concat(a1, a2), |
|
304 "arrays of different element size must not be concatenated"); |
|
305 UCX_TEST_ASSERT(a1->size == 5, |
|
306 "arrays of different element size must not be concatenated"); |
|
307 |
|
308 UCX_TEST_END |
|
309 ucx_array_free(a1); |
|
310 ucx_array_free(a2); |
|
311 } |
|
312 |
|
313 UCX_TEST(test_ucx_array_at) { |
|
314 UcxArray *array = ucx_array_new(16, sizeof(int)); |
|
315 |
|
316 int x[3] = {42, 13, 5}; |
|
317 ucx_array_append_from(array, x, 3); |
|
318 |
|
319 UCX_TEST_BEGIN |
|
320 |
|
321 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 1) == 13, "failed"); |
|
322 *(int*)ucx_array_at(array, 1) = 80; |
|
323 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 1) == 80, "assignment failed"); |
|
324 |
|
325 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 0) == 42, "corrupted data"); |
|
326 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 2) == 5, "corrupted data"); |
|
327 |
|
328 UCX_TEST_END |
|
329 |
|
330 ucx_array_free(array); |
|
331 } |
|
332 |
|
333 UCX_TEST(test_ucx_array_find) { |
|
334 UcxArray *array = ucx_array_new(16, sizeof(int)); |
|
335 int *elems; |
|
336 |
|
337 array->size = 5; |
|
338 elems = array->data; |
|
339 elems[0] = 47; |
|
340 elems[1] = 11; |
|
341 elems[2] = 0; |
|
342 elems[3] = 8; |
|
343 elems[4] = 15; |
|
344 |
|
345 int x = 8; |
|
346 int y = 90; |
|
347 |
|
348 UCX_TEST_BEGIN |
|
349 |
|
350 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,ucx_cmp_int,NULL) == 3, |
|
351 "doesn't find element"); |
|
352 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,ucx_cmp_int,NULL) == 5, |
|
353 "finds non-existing element"); |
|
354 |
|
355 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,NULL,NULL) == 3, |
|
356 "failed using memcmp()"); |
|
357 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,NULL,NULL) == 5, |
|
358 "failed using memcmp()"); |
|
359 |
|
360 UCX_TEST_END |
|
361 ucx_array_free(array); |
|
362 } |
|
363 |
|
364 UCX_TEST(test_ucx_array_contains) { |
|
365 UcxArray *array = ucx_array_new(16, sizeof(int)); |
|
366 int *elems; |
|
367 |
|
368 array->size = 5; |
|
369 elems = array->data; |
|
370 elems[0] = 47; |
|
371 elems[1] = 11; |
|
372 elems[2] = 0; |
|
373 elems[3] = 8; |
|
374 elems[4] = 15; |
|
375 |
|
376 int x = 8; |
|
377 int y = 90; |
|
378 |
|
379 UCX_TEST_BEGIN |
|
380 |
|
381 UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,ucx_cmp_int,NULL), |
|
382 "false negative"); |
|
383 UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,ucx_cmp_int,NULL), |
|
384 "false positive"); |
|
385 |
|
386 UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,NULL,NULL), |
|
387 "false negative using memcmp()"); |
|
388 UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL), |
|
389 "false positive using memcmp()"); |
|
390 |
|
391 UCX_TEST_END |
|
392 ucx_array_free(array); |
|
393 } |
|
394 |
|
395 UCX_TEST(test_ucx_array_remove) { |
|
396 UcxArray *array = ucx_array_new(16, sizeof(int)); |
|
397 int *elems; |
|
398 |
|
399 array->size = 5; |
|
400 elems = array->data; |
|
401 elems[0] = 47; |
|
402 elems[1] = 11; |
|
403 elems[2] = 0; |
|
404 elems[3] = 8; |
|
405 elems[4] = 15; |
|
406 |
|
407 UCX_TEST_BEGIN |
|
408 |
|
409 ucx_array_remove(array, 2); |
|
410 elems = array->data; |
|
411 UCX_TEST_ASSERT( |
|
412 elems[0] == 47 && |
|
413 elems[1] == 11 && |
|
414 elems[2] == 8 && |
|
415 elems[3] == 15, |
|
416 "wrong contents after remove"); |
|
417 UCX_TEST_ASSERT(array->size == 4, "wrong size after remove"); |
|
418 |
|
419 ucx_array_remove_fast(array, 1); |
|
420 elems = array->data; |
|
421 UCX_TEST_ASSERT( |
|
422 elems[0] == 47 && |
|
423 elems[1] == 15 && |
|
424 elems[2] == 8, |
|
425 "wrong contents after fast remove"); |
|
426 UCX_TEST_ASSERT(array->size == 3, "wrong size after fast remove"); |
|
427 |
|
428 UCX_TEST_END |
|
429 ucx_array_free(array); |
|
430 } |
|
431 |
|
432 UCX_TEST(test_ucx_array_clone) { |
|
433 UcxArray array; |
|
434 UcxArray copy; |
|
435 ucx_array_init(&array, 16, sizeof(int)); |
|
436 ucx_array_init(©, 4, 2*sizeof(double)); |
|
437 int *elems; |
|
438 |
|
439 array.size = 5; |
|
440 elems = array.data; |
|
441 elems[0] = 47; |
|
442 elems[1] = 11; |
|
443 elems[2] = 0; |
|
444 elems[3] = 8; |
|
445 elems[4] = 15; |
|
446 |
|
447 ucx_array_clone(©, &array); |
|
448 UCX_TEST_BEGIN |
|
449 |
|
450 UCX_TEST_ASSERT(array.data != copy.data, "no true copy"); |
|
451 UCX_TEST_ASSERT(array.size == copy.size, "size mismatch"); |
|
452 UCX_TEST_ASSERT(array.capacity == copy.capacity, "capacity mismatch"); |
|
453 UCX_TEST_ASSERT(array.elemsize == copy.elemsize, "element size mismatch"); |
|
454 UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch"); |
|
455 UCX_TEST_ASSERT(ucx_array_equals(&array, ©, ucx_cmp_int, NULL), |
|
456 "contents do not match after clone"); |
|
457 |
|
458 UCX_TEST_END |
|
459 |
|
460 ucx_array_destroy(&array); |
|
461 ucx_array_destroy(©); |
|
462 } |
|
463 |
|
464 static int ucx_cmp_int_reverse(const void* x, const void* y, void* data) { |
|
465 return -ucx_cmp_int(x,y,data); |
|
466 } |
|
467 |
|
468 UCX_TEST(test_ucx_array_sort) { |
|
469 int *elems; |
|
470 |
|
471 UcxArray *array = ucx_array_new(16, sizeof(int)); |
|
472 array->size = 5; |
|
473 elems = array->data; |
|
474 elems[0] = 47; |
|
475 elems[1] = 11; |
|
476 elems[2] = 0; |
|
477 elems[3] = 8; |
|
478 elems[4] = 15; |
|
479 |
|
480 UcxArray *expected = ucx_array_new(16, sizeof(int)); |
|
481 expected->size = 5; |
|
482 elems = expected->data; |
|
483 elems[0] = 0; |
|
484 elems[1] = 8; |
|
485 elems[2] = 11; |
|
486 elems[3] = 15; |
|
487 elems[4] = 47; |
|
488 |
|
489 UcxArray *expectedrev = ucx_array_new(16, sizeof(int)); |
|
490 expectedrev->size = 5; |
|
491 elems = expectedrev->data; |
|
492 elems[0] = 47; |
|
493 elems[1] = 15; |
|
494 elems[2] = 11; |
|
495 elems[3] = 8; |
|
496 elems[4] = 0; |
|
497 |
|
498 |
|
499 UCX_TEST_BEGIN |
|
500 void* original_ptr = array->data; |
|
501 ucx_array_sort(array, ucx_cmp_int, NULL); |
|
502 UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), "failed"); |
|
503 UCX_TEST_ASSERT(array->size == 5, "size corrupted"); |
|
504 UCX_TEST_ASSERT(array->data == original_ptr, "shall not reallocate"); |
|
505 |
|
506 ucx_array_sort(array, ucx_cmp_int_reverse, NULL); |
|
507 UCX_TEST_ASSERT(ucx_array_equals(array, expectedrev, NULL, NULL), "failed"); |
|
508 |
|
509 ucx_array_reserve(array, 32); |
|
510 ucx_array_reserve(expected, 32); |
|
511 array->size = expected->size = 32; |
|
512 for (size_t i = 0 ; i < 32 ; i++) { |
|
513 ((int*)array->data)[i]= ((i%2==0)?-1:1) * ((int) i); |
|
514 ((int*)expected->data)[i] = (-30+2*i) - (i > 15 ? 1 : 0); |
|
515 } |
|
516 |
|
517 /* dummy third argument to trigger a possible fallback for qsort_s */ |
|
518 ucx_array_sort(array, ucx_cmp_int, array->data); |
|
519 UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), |
|
520 "failed for bigger arrays"); |
|
521 UCX_TEST_END |
|
522 |
|
523 ucx_array_free(expectedrev); |
|
524 ucx_array_free(expected); |
|
525 ucx_array_free(array); |
|
526 } |
|
527 |
|
528 UCX_TEST(test_ucx_array_shrink) { |
|
529 UcxArray *array = ucx_array_new(16, sizeof(int)); |
|
530 array->size = 4; |
|
531 |
|
532 UCX_TEST_BEGIN |
|
533 UCX_TEST_ASSERT(!ucx_array_shrink(array), "failed"); |
|
534 UCX_TEST_ASSERT(array->capacity == 4, "incorrect capacity after shrink"); |
|
535 UCX_TEST_END |
|
536 ucx_array_free(array); |
|
537 } |
|
538 |
|
539 UCX_TEST(test_ucx_array_resize) { |
|
540 UcxArray *array = ucx_array_new(16, sizeof(int)); |
|
541 array->size = 8; |
|
542 |
|
543 UCX_TEST_BEGIN |
|
544 |
|
545 UCX_TEST_ASSERT(!ucx_array_resize(array, 32), "failed"); |
|
546 UCX_TEST_ASSERT(array->capacity == 32, "incorrect capacity after resize"); |
|
547 UCX_TEST_ASSERT(array->size == 8, "incorrect size after resize"); |
|
548 |
|
549 UCX_TEST_ASSERT(!ucx_array_resize(array, 4), "failed"); |
|
550 UCX_TEST_ASSERT(array->capacity == 4, "incorrect capacity after resize"); |
|
551 UCX_TEST_ASSERT(array->size == 4, "incorrect size after resize"); |
|
552 |
|
553 UCX_TEST_END |
|
554 ucx_array_free(array); |
|
555 } |
|
556 |
|
557 UCX_TEST(test_ucx_array_reserve) { |
|
558 UcxArray *array = ucx_array_new(16, sizeof(int)); |
|
559 |
|
560 UCX_TEST_BEGIN |
|
561 |
|
562 UCX_TEST_ASSERT(!ucx_array_reserve(array, 4), "failed"); |
|
563 UCX_TEST_ASSERT(array->capacity == 16, "reserve shall not shrink"); |
|
564 |
|
565 UCX_TEST_ASSERT(!ucx_array_resize(array, 32), "failed"); |
|
566 UCX_TEST_ASSERT(array->capacity == 32, "incorrect capacity after reserve"); |
|
567 |
|
568 UCX_TEST_END |
|
569 ucx_array_free(array); |
|
570 } |
|
571 |
|
572 UCX_TEST(test_ucx_array_grow) { |
|
573 UcxArray *array = ucx_array_new(16, sizeof(int)); |
|
574 array->size = 12; |
|
575 |
|
576 UCX_TEST_BEGIN |
|
577 |
|
578 UCX_TEST_ASSERT(!ucx_array_grow(array, 4), "failed"); |
|
579 UCX_TEST_ASSERT(array->capacity == 16, "shall be noop if contents fit"); |
|
580 /* subsequent calls shall also be noops */ |
|
581 UCX_TEST_ASSERT(!ucx_array_grow(array, 4), "failed"); |
|
582 UCX_TEST_ASSERT(array->capacity == 16, "shall be noop if contents fit"); |
|
583 |
|
584 UCX_TEST_ASSERT(!ucx_array_grow(array, 6), "failed"); |
|
585 UCX_TEST_ASSERT(array->capacity == 18, "incorrect capacity after grow"); |
|
586 |
|
587 UCX_TEST_END |
|
588 ucx_array_free(array); |
|
589 } |
|
590 |
|
591 UCX_TEST(test_ucx_array_util_set) { |
|
592 size_t capacity = 16; |
|
593 int* array = malloc(sizeof(int)*capacity); |
|
594 int x; |
|
595 |
|
596 UCX_TEST_BEGIN |
|
597 |
|
598 x = 42; |
|
599 ucx_array_util_set(&array, &capacity, sizeof(int), 7, &x); |
|
600 |
|
601 UCX_TEST_ASSERT(array[7] == 42, "failed"); |
|
602 UCX_TEST_ASSERT(capacity == 16, "capacity changed unnecessarily"); |
|
603 |
|
604 x = 13; |
|
605 ucx_array_util_set(&array, &capacity, sizeof(int), 37, &x); |
|
606 x = 37; |
|
607 ucx_array_util_set(&array, &capacity, sizeof(int), 38, &x); |
|
608 |
|
609 UCX_TEST_ASSERT(array[37] == 13, "failed"); |
|
610 UCX_TEST_ASSERT(array[38] == 37, "failed"); |
|
611 UCX_TEST_ASSERT(capacity == 64, "capacity not grown"); |
|
612 |
|
613 UCX_TEST_END |
|
614 |
|
615 free(array); |
|
616 } |
|
617 |
|
618 |
|
619 UCX_TEST(test_ucx_array_util_setptr) { |
|
620 size_t capacity = 16; |
|
621 double** array = malloc(sizeof(double*)*capacity); |
|
622 double x, y, z; |
|
623 |
|
624 UCX_TEST_BEGIN |
|
625 |
|
626 ucx_array_util_setptr(&array, &capacity, 7, &x); |
|
627 |
|
628 UCX_TEST_ASSERT(array[7] == &x, "failed"); |
|
629 UCX_TEST_ASSERT(capacity == 16, "capacity changed unnecessarily"); |
|
630 |
|
631 ucx_array_util_setptr(&array, &capacity, 37, &y); |
|
632 ucx_array_util_setptr(&array, &capacity, 38, &z); |
|
633 |
|
634 UCX_TEST_ASSERT(array[37] == &y, "failed"); |
|
635 UCX_TEST_ASSERT(array[38] == &z, "failed"); |
|
636 UCX_TEST_ASSERT(capacity == 64, "capacity not grown"); |
|
637 |
|
638 UCX_TEST_END |
|
639 |
|
640 free(array); |
|
641 } |
|
642 |
|