test/array_tests.c

branch
feature/array
changeset 353
135ce35d5108
parent 350
82a88d938108
child 354
7fd13b9f8f60
equal deleted inserted replaced
352:83888029778a 353:135ce35d5108
31 31
32 UCX_TEST(test_ucx_array_free) { 32 UCX_TEST(test_ucx_array_free) {
33 UcxArray array = ucx_array_new(16, sizeof(int)); 33 UcxArray array = ucx_array_new(16, sizeof(int));
34 34
35 UCX_TEST_BEGIN 35 UCX_TEST_BEGIN
36 ucx_array_free(&array); 36 ucx_array_destroy(&array);
37 UCX_TEST_ASSERT(array.data == NULL, "data pointer not NULL after free"); 37 UCX_TEST_ASSERT(array.data == NULL, "data pointer not NULL after free");
38 UCX_TEST_ASSERT(array.size == 0, "size not zero after free"); 38 UCX_TEST_ASSERT(array.size == 0, "size not zero after free");
39 UCX_TEST_ASSERT(array.capacity == 0, "capacity not zero after free"); 39 UCX_TEST_ASSERT(array.capacity == 0, "capacity not zero after free");
40 UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(), 40 UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(),
41 "allocator corrupted during free"); 41 "allocator corrupted during free");
51 UCX_TEST_ASSERT(array.capacity == 16, "capacity not as requested"); 51 UCX_TEST_ASSERT(array.capacity == 16, "capacity not as requested");
52 UCX_TEST_ASSERT(array.elemsize == 47, "element size not as requested"); 52 UCX_TEST_ASSERT(array.elemsize == 47, "element size not as requested");
53 UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(), 53 UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(),
54 "array not using the default allocator"); 54 "array not using the default allocator");
55 UCX_TEST_END 55 UCX_TEST_END
56 ucx_array_free(&array); 56 ucx_array_destroy(&array);
57 } 57 }
58 58
59 UCX_TEST(test_ucx_array_append) { 59 UCX_TEST(test_ucx_array_append) {
60 UcxArray array = ucx_array_new(16, sizeof(int)); 60 UcxArray array = ucx_array_new(16, sizeof(int));
61 int *elements; 61 int *elements;
86 UCX_TEST_ASSERT(elements[1] == 13, 86 UCX_TEST_ASSERT(elements[1] == 13,
87 "NULL append corrupted previously inserted data"); 87 "NULL append corrupted previously inserted data");
88 88
89 UCX_TEST_END 89 UCX_TEST_END
90 90
91 ucx_array_free(&array); 91 ucx_array_destroy(&array);
92 } 92 }
93 93
94 UCX_TEST(test_ucx_array_prepend) { 94 UCX_TEST(test_ucx_array_prepend) {
95 int *elems; 95 int *elems;
96 UcxArray array = ucx_array_new(16, sizeof(int)); 96 UcxArray array = ucx_array_new(16, sizeof(int));
121 UCX_TEST_ASSERT(elems[2] == 42, 121 UCX_TEST_ASSERT(elems[2] == 42,
122 "NULL prepend corrupted previously inserted data"); 122 "NULL prepend corrupted previously inserted data");
123 123
124 UCX_TEST_END 124 UCX_TEST_END
125 125
126 ucx_array_free(&array); 126 ucx_array_destroy(&array);
127 } 127 }
128 128
129 UCX_TEST(test_ucx_array_set) { 129 UCX_TEST(test_ucx_array_set) {
130 int *elems; 130 int *elems;
131 UcxArray array = ucx_array_new(16, sizeof(int)); 131 UcxArray array = ucx_array_new(16, sizeof(int));
155 elems = array.data; 155 elems = array.data;
156 UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set"); 156 UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set");
157 157
158 UCX_TEST_END 158 UCX_TEST_END
159 159
160 ucx_array_free(&array); 160 ucx_array_destroy(&array);
161 } 161 }
162 162
163 UCX_TEST(test_ucx_array_equals) { 163 UCX_TEST(test_ucx_array_equals) {
164 UcxArray a1 = ucx_array_new(16, sizeof(int32_t)); 164 UcxArray a1 = ucx_array_new(16, sizeof(int32_t));
165 UcxArray a2 = ucx_array_new(16, sizeof(int32_t)); 165 UcxArray a2 = ucx_array_new(16, sizeof(int32_t));
212 "compare using memcmp() failed"); 212 "compare using memcmp() failed");
213 UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, NULL, NULL), 213 UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, NULL, NULL),
214 "compare using memcmp() failed"); 214 "compare using memcmp() failed");
215 215
216 UCX_TEST_END 216 UCX_TEST_END
217 ucx_array_free(&a1); 217 ucx_array_destroy(&a1);
218 ucx_array_free(&a2); 218 ucx_array_destroy(&a2);
219 ucx_array_free(&a3); 219 ucx_array_destroy(&a3);
220 ucx_array_free(&a4); 220 ucx_array_destroy(&a4);
221 } 221 }
222 222
223 UCX_TEST(test_ucx_array_concat) { 223 UCX_TEST(test_ucx_array_concat) {
224 UcxArray a1 = ucx_array_new(16, sizeof(int)); 224 UcxArray a1 = ucx_array_new(16, sizeof(int));
225 UcxArray a2 = ucx_array_new(16, sizeof(int)); 225 UcxArray a2 = ucx_array_new(16, sizeof(int));
251 "arrays of different element size must not be concatenated"); 251 "arrays of different element size must not be concatenated");
252 UCX_TEST_ASSERT(a1.size == 5, 252 UCX_TEST_ASSERT(a1.size == 5,
253 "arrays of different element size must not be concatenated"); 253 "arrays of different element size must not be concatenated");
254 254
255 UCX_TEST_END 255 UCX_TEST_END
256 ucx_array_free(&a1); 256 ucx_array_destroy(&a1);
257 ucx_array_free(&a2); 257 ucx_array_destroy(&a2);
258 } 258 }
259 259
260 UCX_TEST(test_ucx_array_at) { 260 UCX_TEST(test_ucx_array_at) {
261 UcxArray array = ucx_array_new(16, sizeof(int)); 261 UcxArray array = ucx_array_new(16, sizeof(int));
262 262
276 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 0) == 42, "corrupted data"); 276 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 0) == 42, "corrupted data");
277 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 2) == 5, "corrupted data"); 277 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 2) == 5, "corrupted data");
278 278
279 UCX_TEST_END 279 UCX_TEST_END
280 280
281 ucx_array_free(&array); 281 ucx_array_destroy(&array);
282 } 282 }
283 283
284 UCX_TEST(test_ucx_array_find) { 284 UCX_TEST(test_ucx_array_find) {
285 UcxArray array = ucx_array_new(16, sizeof(int)); 285 UcxArray array = ucx_array_new(16, sizeof(int));
286 int *elems; 286 int *elems;
307 "failed using memcmp()"); 307 "failed using memcmp()");
308 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,NULL,NULL) == 5, 308 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,NULL,NULL) == 5,
309 "failed using memcmp()"); 309 "failed using memcmp()");
310 310
311 UCX_TEST_END 311 UCX_TEST_END
312 ucx_array_free(&array); 312 ucx_array_destroy(&array);
313 } 313 }
314 314
315 UCX_TEST(test_ucx_array_contains) { 315 UCX_TEST(test_ucx_array_contains) {
316 UcxArray array = ucx_array_new(16, sizeof(int)); 316 UcxArray array = ucx_array_new(16, sizeof(int));
317 int *elems; 317 int *elems;
338 "false negative using memcmp()"); 338 "false negative using memcmp()");
339 UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL), 339 UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL),
340 "false positive using memcmp()"); 340 "false positive using memcmp()");
341 341
342 UCX_TEST_END 342 UCX_TEST_END
343 ucx_array_free(&array); 343 ucx_array_destroy(&array);
344 } 344 }
345 345
346 UCX_TEST(test_ucx_array_remove) { 346 UCX_TEST(test_ucx_array_remove) {
347 UcxArray array = ucx_array_new(16, sizeof(int)); 347 UcxArray array = ucx_array_new(16, sizeof(int));
348 int *elems; 348 int *elems;
375 elems[2] == 8, 375 elems[2] == 8,
376 "wrong contents after fast remove"); 376 "wrong contents after fast remove");
377 UCX_TEST_ASSERT(array.size == 3, "wrong size after fast remove"); 377 UCX_TEST_ASSERT(array.size == 3, "wrong size after fast remove");
378 378
379 UCX_TEST_END 379 UCX_TEST_END
380 ucx_array_free(&array); 380 ucx_array_destroy(&array);
381 } 381 }
382 382
383 UCX_TEST(test_ucx_array_clone) { 383 UCX_TEST(test_ucx_array_clone) {
384 UcxArray array = ucx_array_new(16, sizeof(int)); 384 UcxArray array = ucx_array_new(16, sizeof(int));
385 int *elems; 385 int *elems;
402 UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch"); 402 UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch");
403 UCX_TEST_ASSERT(ucx_array_equals(array, copy, ucx_cmp_int, NULL), "failed"); 403 UCX_TEST_ASSERT(ucx_array_equals(array, copy, ucx_cmp_int, NULL), "failed");
404 404
405 UCX_TEST_END 405 UCX_TEST_END
406 406
407 ucx_array_free(&array); 407 ucx_array_destroy(&array);
408 ucx_array_free(&copy); 408 ucx_array_destroy(&copy);
409 } 409 }
410 410
411 static int ucx_cmp_int_reverse(const void* x, const void* y, void* data) { 411 static int ucx_cmp_int_reverse(const void* x, const void* y, void* data) {
412 return -ucx_cmp_int(x,y,data); 412 return -ucx_cmp_int(x,y,data);
413 } 413 }
465 ucx_array_sort(array, ucx_cmp_int, array.data); 465 ucx_array_sort(array, ucx_cmp_int, array.data);
466 UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), 466 UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL),
467 "failed for bigger arrays"); 467 "failed for bigger arrays");
468 UCX_TEST_END 468 UCX_TEST_END
469 469
470 ucx_array_free(&expected); 470 ucx_array_destroy(&expected);
471 ucx_array_free(&array); 471 ucx_array_destroy(&array);
472 } 472 }
473 473
474 UCX_TEST(test_ucx_array_autogrow) { 474 UCX_TEST(test_ucx_array_autogrow) {
475 int *elems; 475 int *elems;
476 UcxArray array = ucx_array_new(4, sizeof(int)); 476 UcxArray array = ucx_array_new(4, sizeof(int));
492 UCX_TEST_ASSERT(array.capacity == 8, "array did not grow"); 492 UCX_TEST_ASSERT(array.capacity == 8, "array did not grow");
493 UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow"); 493 UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow");
494 UCX_TEST_ASSERT(elems[3] == 5 && elems[4] == 5, "corrupt data"); 494 UCX_TEST_ASSERT(elems[3] == 5 && elems[4] == 5, "corrupt data");
495 495
496 UCX_TEST_END 496 UCX_TEST_END
497 ucx_array_free(&array); 497 ucx_array_destroy(&array);
498 } 498 }
499 499
500 UCX_TEST(test_ucx_array_shrink) { 500 UCX_TEST(test_ucx_array_shrink) {
501 UcxArray array = ucx_array_new(16, sizeof(int)); 501 UcxArray array = ucx_array_new(16, sizeof(int));
502 array.size = 4; 502 array.size = 4;
503 503
504 UCX_TEST_BEGIN 504 UCX_TEST_BEGIN
505 UCX_TEST_ASSERT(!ucx_array_shrink(&array), "failed"); 505 UCX_TEST_ASSERT(!ucx_array_shrink(&array), "failed");
506 UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after shrink"); 506 UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after shrink");
507 UCX_TEST_END 507 UCX_TEST_END
508 ucx_array_free(&array); 508 ucx_array_destroy(&array);
509 } 509 }
510 510
511 UCX_TEST(test_ucx_array_resize) { 511 UCX_TEST(test_ucx_array_resize) {
512 UcxArray array = ucx_array_new(16, sizeof(int)); 512 UcxArray array = ucx_array_new(16, sizeof(int));
513 array.size = 8; 513 array.size = 8;
521 UCX_TEST_ASSERT(!ucx_array_resize(&array, 4), "failed"); 521 UCX_TEST_ASSERT(!ucx_array_resize(&array, 4), "failed");
522 UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after resize"); 522 UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after resize");
523 UCX_TEST_ASSERT(array.size == 4, "incorrect size after resize"); 523 UCX_TEST_ASSERT(array.size == 4, "incorrect size after resize");
524 524
525 UCX_TEST_END 525 UCX_TEST_END
526 ucx_array_free(&array); 526 ucx_array_destroy(&array);
527 } 527 }
528 528
529 UCX_TEST(test_ucx_array_reserve) { 529 UCX_TEST(test_ucx_array_reserve) {
530 UcxArray array = ucx_array_new(16, sizeof(int)); 530 UcxArray array = ucx_array_new(16, sizeof(int));
531 531
536 536
537 UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed"); 537 UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed");
538 UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after reserve"); 538 UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after reserve");
539 539
540 UCX_TEST_END 540 UCX_TEST_END
541 ucx_array_free(&array); 541 ucx_array_destroy(&array);
542 } 542 }

mercurial