tests/test_list.c

changeset 1637
34c2e1436945
parent 1636
5ed91801f79d
equal deleted inserted replaced
1636:5ed91801f79d 1637:34c2e1436945
35 #include "cx/kv_list.h" 35 #include "cx/kv_list.h"
36 36
37 #include <stdarg.h> 37 #include <stdarg.h>
38 #include <errno.h> 38 #include <errno.h>
39 39
40 static int test_ccmp_int(const void *l, const void *r, void *c) {
41 int *z = c;
42 // return bullshit to make the test fail when c was not passed correctly
43 if (z == NULL || *z != 1337) return -1;
44 return cx_cmp_int(l, r);
45 }
46
40 CX_TEST(test_array_add) { 47 CX_TEST(test_array_add) {
41 CX_ARRAY(int, arr); 48 CX_ARRAY(int, arr);
42 cx_array_init(arr, 5); 49 cx_array_init(arr, 5);
43 arr.data[0] = 2; 50 arr.data[0] = 2;
44 arr.data[1] = 3; 51 arr.data[1] = 3;
295 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len)); 302 CX_TEST_ASSERT(0 == memcmp(arr.data, expected, len));
296 } 303 }
297 cx_array_free(arr); 304 cx_array_free(arr);
298 } 305 }
299 306
307 CX_TEST(test_array_remove_array_fast_ints) {
308 char d_char[5] = {1, 2, 3, 4, 5};
309 short d_short[5] = {1, 2, 3, 4, 5};
310 long d_long[5] = {1, 2, 3, 4, 5};
311 long long d_llong[5] = {1, 2, 3, 4, 5};
312 CX_ARRAY(char, a_char);
313 cx_array_init_fixed(a_char, d_char, 5);
314 CX_ARRAY(short, a_short);
315 cx_array_init_fixed(a_short, d_short, 5);
316 CX_ARRAY(long, a_long);
317 cx_array_init_fixed(a_long, d_long, 5);
318 CX_ARRAY(long long, a_llong);
319 cx_array_init_fixed(a_llong, d_llong, 5);
320 CX_TEST_DO {
321 cx_array_remove_fast(a_char, 2);
322 cx_array_remove_fast(a_short, 2);
323 cx_array_remove_fast(a_long, 2);
324 cx_array_remove_fast(a_llong, 2);
325 CX_TEST_ASSERT(a_char.data[0] == 1);
326 CX_TEST_ASSERT(a_short.data[0] == 1);
327 CX_TEST_ASSERT(a_long.data[0] == 1);
328 CX_TEST_ASSERT(a_llong.data[0] == 1);
329 CX_TEST_ASSERT(a_char.data[1] == 2);
330 CX_TEST_ASSERT(a_short.data[1] == 2);
331 CX_TEST_ASSERT(a_long.data[1] == 2);
332 CX_TEST_ASSERT(a_llong.data[1] == 2);
333 CX_TEST_ASSERT(a_char.data[2] == 5);
334 CX_TEST_ASSERT(a_short.data[2] == 5);
335 CX_TEST_ASSERT(a_long.data[2] == 5);
336 CX_TEST_ASSERT(a_llong.data[2] == 5);
337 CX_TEST_ASSERT(a_char.data[3] == 4);
338 CX_TEST_ASSERT(a_short.data[3] == 4);
339 CX_TEST_ASSERT(a_long.data[3] == 4);
340 CX_TEST_ASSERT(a_llong.data[3] == 4);
341 }
342 }
343
300 CX_TEST(test_array_reserve) { 344 CX_TEST(test_array_reserve) {
301 CX_ARRAY(int, arr); 345 CX_ARRAY(int, arr);
302 cx_array_init(arr, 5); 346 cx_array_init(arr, 5);
303 arr.data[0] = 2; 347 arr.data[0] = 2;
304 arr.data[1] = 3; 348 arr.data[1] = 3;
351 for (int x = 1 ; x <= 9 ; x++) { 395 for (int x = 1 ; x <= 9 ; x++) {
352 CX_TEST_ASSERT(arr.data[x-1] == 2*x); 396 CX_TEST_ASSERT(arr.data[x-1] == 2*x);
353 } 397 }
354 } 398 }
355 cx_array_free(arr); 399 cx_array_free(arr);
400 }
401
402 CX_TEST(test_array_sort) {
403 int data[31] = {
404 67, 90, 120, 77, 64, 75, 56, 130, 70, 59, 51, 71, 10, 78, 75, 65,
405 54, 56, 80, 62, 52, 72, 71, 58, 40, 90, 75, 57, 58, 60, 50
406 };
407 int expected[31] = {
408 10, 40, 50, 51, 52, 54, 56, 56, 57, 58, 58, 59, 60, 62, 64, 65, 67,
409 70, 71, 71, 72, 75, 75, 75, 77, 78, 80, 90, 90, 120, 130
410 };
411 CX_ARRAY(int, arr);
412 cx_array_init_fixed(arr, data, 31);
413 CX_TEST_DO {
414 cx_array_sort(arr, cx_cmp_int);
415 CX_TEST_ASSERT(memcmp(arr.data, expected, 31*sizeof(int)) == 0);
416 }
417 }
418
419 CX_TEST(test_array_sort_c) {
420 int data[31] = {
421 67, 90, 120, 77, 64, 75, 56, 130, 70, 59, 51, 71, 10, 78, 75, 65,
422 54, 56, 80, 62, 52, 72, 71, 58, 40, 90, 75, 57, 58, 60, 50
423 };
424 int expected[31] = {
425 10, 40, 50, 51, 52, 54, 56, 56, 57, 58, 58, 59, 60, 62, 64, 65, 67,
426 70, 71, 71, 72, 75, 75, 75, 77, 78, 80, 90, 90, 120, 130
427 };
428 CX_ARRAY(int, arr);
429 cx_array_init_fixed(arr, data, 31);
430 CX_TEST_DO {
431 int z = 1337;
432 cx_array_sort_c(arr, test_ccmp_int, &z);
433 CX_TEST_ASSERT(memcmp(arr.data, expected, 31*sizeof(int)) == 0);
434 }
435 }
436
437 CX_TEST(test_array_iterator) {
438 int data[31] = {
439 67, 90, 120, 77, 64, 75, 56, 130, 70, 59, 51, 71, 10, 78, 75, 65,
440 54, 56, 80, 62, 52, 72, 71, 58, 40, 90, 75, 57, 58, 60, 50
441 };
442 int copy[31] = {0};
443 CX_ARRAY(int, arr);
444 cx_array_init_fixed(arr, data, 31);
445 CX_TEST_DO {
446 CxIterator iter = cx_array_iterator(arr);
447 cx_foreach(int*, x, iter) {
448 copy[iter.index] = *x;
449 }
450 CX_TEST_ASSERT(memcmp(copy, data, 31*sizeof(int)) == 0);
451 }
452 }
453
454 CX_TEST(test_array_iterator_ptr) {
455 int data[31] = {
456 67, 90, 120, 77, 64, 75, 56, 130, 70, 59, 51, 71, 10, 78, 75, 65,
457 54, 56, 80, 62, 52, 72, 71, 58, 40, 90, 75, 57, 58, 60, 50
458 };
459 int *data_ptr[31];
460 for (unsigned i = 0 ; i < 31 ; i++) {
461 data_ptr[i] = &data[i];
462 }
463 int copy[31] = {0};
464 CX_ARRAY(int, arr);
465 cx_array_init_fixed(arr, data_ptr, 31);
466 CX_TEST_DO {
467 CxIterator iter = cx_array_iterator_ptr(arr);
468 cx_foreach(int*, x, iter) {
469 copy[iter.index] = *x;
470 }
471 CX_TEST_ASSERT(memcmp(copy, data, 31*sizeof(int)) == 0);
472 }
356 } 473 }
357 474
358 CX_TEST(test_array_insert_sorted) { 475 CX_TEST(test_array_insert_sorted) {
359 int d1 = 50; 476 int d1 = 50;
360 int d2 = 80; 477 int d2 = 80;
598 s = 75; 715 s = 75;
599 CX_TEST_ASSERT(15 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int)); 716 CX_TEST_ASSERT(15 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int));
600 s = 70; // exact match, we want the smallest index 717 s = 70; // exact match, we want the smallest index
601 CX_TEST_ASSERT(12 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int)); 718 CX_TEST_ASSERT(12 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int));
602 } 719 }
603 }
604
605 static int test_ccmp_int(const void *l, const void *r, void *c) {
606 int *z = c;
607 // return bullshit to make the test fail when c was not passed correctly
608 if (z == NULL || *z != 1337) return -1;
609 return cx_cmp_int(l, r);
610 } 720 }
611 721
612 typedef struct node { 722 typedef struct node {
613 struct node *next; 723 struct node *next;
614 struct node *prev; 724 struct node *prev;
3504 cx_test_register(suite, test_array_add_capacity_grow_strategy); 3614 cx_test_register(suite, test_array_add_capacity_grow_strategy);
3505 cx_test_register(suite, test_array_remove); 3615 cx_test_register(suite, test_array_remove);
3506 cx_test_register(suite, test_array_remove_fast); 3616 cx_test_register(suite, test_array_remove_fast);
3507 cx_test_register(suite, test_array_remove_array); 3617 cx_test_register(suite, test_array_remove_array);
3508 cx_test_register(suite, test_array_remove_array_fast); 3618 cx_test_register(suite, test_array_remove_array_fast);
3619 cx_test_register(suite, test_array_remove_array_fast_ints);
3509 cx_test_register(suite, test_array_reserve); 3620 cx_test_register(suite, test_array_reserve);
3510 cx_test_register(suite, test_array_copy_to_new); 3621 cx_test_register(suite, test_array_copy_to_new);
3622 cx_test_register(suite, test_array_sort);
3623 cx_test_register(suite, test_array_sort_c);
3624 cx_test_register(suite, test_array_iterator);
3625 cx_test_register(suite, test_array_iterator_ptr);
3511 cx_test_register(suite, test_array_insert_sorted); 3626 cx_test_register(suite, test_array_insert_sorted);
3512 cx_test_register(suite, test_array_insert_unique); 3627 cx_test_register(suite, test_array_insert_unique);
3513 cx_test_register(suite, test_array_binary_search); 3628 cx_test_register(suite, test_array_binary_search);
3514 cx_test_register(suite, test_array_binary_search_with_duplicates); 3629 cx_test_register(suite, test_array_binary_search_with_duplicates);
3515 3630

mercurial