test/test_list.c

changeset 509
0d3c6075f82c
parent 507
2e8878770de0
equal deleted inserted replaced
508:8aea65ae1eaf 509:0d3c6075f82c
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "cx/linked_list.h" 29 #include "cx/linked_list.h"
30 #include "cx/utils.h"
30 #include "test_config.h" 31 #include "test_config.h"
31 #include "util_allocator.h" 32 #include "util_allocator.h"
32 33
33 static int cmp_int_impl( 34 static int cmp_int_impl(
34 int const *l, 35 int const *l,
50 51
51 const ptrdiff_t loc_prev = offsetof(struct node, prev); 52 const ptrdiff_t loc_prev = offsetof(struct node, prev);
52 const ptrdiff_t loc_next = offsetof(struct node, next); 53 const ptrdiff_t loc_next = offsetof(struct node, next);
53 const ptrdiff_t loc_data = offsetof(struct node, data); 54 const ptrdiff_t loc_data = offsetof(struct node, data);
54 55
55 struct node *create_test_data( 56 static struct node *create_nodes_test_data(
56 size_t n, 57 size_t n,
57 int const data[] 58 int const data[]
58 ) { 59 ) {
59 if (n == 0) return NULL; 60 CU_ASSERT_NOT_EQUAL_FATAL(n, 0)
60 struct node *begin = calloc(1, sizeof(struct node)); 61 struct node *begin = calloc(1, sizeof(struct node));
61 struct node *prev = begin; 62 struct node *prev = begin;
62 if (data) begin->data = data[0]; 63 if (data) begin->data = data[0];
63 for (size_t i = 1; i < n; i++) { 64 for (size_t i = 1; i < n; i++) {
64 struct node *node = calloc(1, sizeof(struct node)); 65 struct node *node = calloc(1, sizeof(struct node));
67 prev = node; 68 prev = node;
68 } 69 }
69 return begin; 70 return begin;
70 } 71 }
71 72
72 void destroy_test_data(struct node *begin) { 73 static void destroy_nodes_test_data(struct node *begin) {
73 struct node *node = begin; 74 struct node *node = begin;
74 while (node) { 75 while (node) {
75 struct node *next = node->next; 76 struct node *next = node->next;
76 free(node); 77 free(node);
77 node = next; 78 node = next;
78 } 79 }
80 }
81
82 static int *create_ints_test_data(size_t len) {
83 int *data = malloc(sizeof(int) * len);
84 cx_for_n (i, len) data[i] = rand(); // NOLINT(cert-msc50-cpp)
85 return data;
79 } 86 }
80 87
81 void test_linked_list_link_unlink(void) { 88 void test_linked_list_link_unlink(void) {
82 89
83 struct node nd(a), nd(b), nd(c); 90 struct node nd(a), nd(b), nd(c);
127 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b) 134 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b)
128 } 135 }
129 136
130 void test_linked_list_find(void) { 137 void test_linked_list_find(void) {
131 int data[] = {2, 4, 6, 8}; 138 int data[] = {2, 4, 6, 8};
132 void *list = create_test_data(4, data); 139 void *list = create_nodes_test_data(4, data);
133 int s; 140 int s;
134 141
135 s = 2; 142 s = 2;
136 CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data, 143 CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
137 false, cmp_int, &s), 0) 144 false, cmp_int, &s), 0)
155 void test_linked_list_compare(void) { 162 void test_linked_list_compare(void) {
156 int a[] = {2, 4, 6, 8}; 163 int a[] = {2, 4, 6, 8};
157 int b[] = {2, 4, 6}; 164 int b[] = {2, 4, 6};
158 int c[] = {2, 4, 6, 9}; 165 int c[] = {2, 4, 6, 9};
159 166
160 void *la = create_test_data(4, a); 167 void *la = create_nodes_test_data(4, a);
161 void *lb = create_test_data(3, b); 168 void *lb = create_nodes_test_data(3, b);
162 void *lc = create_test_data(4, c); 169 void *lc = create_nodes_test_data(4, c);
163 170
164 CU_ASSERT_TRUE(0 < cx_linked_list_compare(la, lb, loc_next, loc_data, 171 CU_ASSERT_TRUE(0 < cx_linked_list_compare(la, lb, loc_next, loc_data,
165 false, cmp_int) 172 false, cmp_int)
166 ) 173 )
167 CU_ASSERT_TRUE(0 > cx_linked_list_compare(lb, la, loc_next, loc_data, 174 CU_ASSERT_TRUE(0 > cx_linked_list_compare(lb, la, loc_next, loc_data,
175 ) 182 )
176 CU_ASSERT_TRUE(0 == cx_linked_list_compare(la, la, loc_next, loc_data, 183 CU_ASSERT_TRUE(0 == cx_linked_list_compare(la, la, loc_next, loc_data,
177 false, cmp_int) 184 false, cmp_int)
178 ) 185 )
179 186
180 destroy_test_data(la); 187 destroy_nodes_test_data(la);
181 destroy_test_data(lb); 188 destroy_nodes_test_data(lb);
182 destroy_test_data(lc); 189 destroy_nodes_test_data(lc);
183 } 190 }
184 191
185 void test_linked_list_add(void) { 192 void test_linked_list_add(void) {
186 struct node nodes[4]; 193 struct node nodes[4];
187 void *begin, *end; 194 void *begin, *end;
419 CU_ASSERT_PTR_NULL(nodes[3].prev) 426 CU_ASSERT_PTR_NULL(nodes[3].prev)
420 CU_ASSERT_PTR_EQUAL(nodes[4].next, &nodes[0]) 427 CU_ASSERT_PTR_EQUAL(nodes[4].next, &nodes[0])
421 } 428 }
422 429
423 void test_linked_list_first(void) { 430 void test_linked_list_first(void) {
424 struct node *begin = create_test_data(3, NULL); 431 struct node *begin = create_nodes_test_data(3, NULL);
425 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin, loc_prev), begin) 432 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin, loc_prev), begin)
426 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next, loc_prev), begin) 433 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next, loc_prev), begin)
427 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next->next, loc_prev), begin) 434 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next->next, loc_prev), begin)
428 destroy_test_data(begin); 435 destroy_nodes_test_data(begin);
429 } 436 }
430 437
431 void test_linked_list_last(void) { 438 void test_linked_list_last(void) {
432 struct node *begin = create_test_data(3, NULL); 439 struct node *begin = create_nodes_test_data(3, NULL);
433 struct node *end = begin->next->next; 440 struct node *end = begin->next->next;
434 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin, loc_next), end) 441 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin, loc_next), end)
435 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next, loc_next), end) 442 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next, loc_next), end)
436 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next->next, loc_next), end) 443 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next->next, loc_next), end)
437 destroy_test_data(begin); 444 destroy_nodes_test_data(begin);
438 } 445 }
439 446
440 void test_linked_list_prev(void) { 447 void test_linked_list_prev(void) {
441 struct node *begin = create_test_data(3, NULL); 448 struct node *begin = create_nodes_test_data(3, NULL);
442 CU_ASSERT_PTR_NULL(cx_linked_list_prev(begin, loc_next, begin)) 449 CU_ASSERT_PTR_NULL(cx_linked_list_prev(begin, loc_next, begin))
443 CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next), begin) 450 CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next), begin)
444 CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next->next), begin->next) 451 CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next->next), begin->next)
445 destroy_test_data(begin); 452 destroy_nodes_test_data(begin);
446 } 453 }
447 454
448 void test_linked_list_remove(void) { 455 void test_linked_list_remove(void) {
449 void *begin, *end; 456 void *begin, *end;
450 457
451 int data[] = {2, 4, 6}; 458 int data[] = {2, 4, 6};
452 begin = create_test_data(3, data); 459 begin = create_nodes_test_data(3, data);
453 struct node *first = begin; 460 struct node *first = begin;
454 struct node *second = first->next; 461 struct node *second = first->next;
455 struct node *third = second->next; 462 struct node *third = second->next;
456 end = third; 463 end = third;
457 464
481 void test_linked_list_size(void) { 488 void test_linked_list_size(void) {
482 struct node *list; 489 struct node *list;
483 490
484 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc_next), 0) 491 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc_next), 0)
485 492
486 list = create_test_data(5, NULL); 493 list = create_nodes_test_data(5, NULL);
487 CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 5) 494 CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 5)
488 destroy_test_data(list); 495 destroy_nodes_test_data(list);
489 496
490 list = create_test_data(13, NULL); 497 list = create_nodes_test_data(13, NULL);
491 CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 13) 498 CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 13)
492 destroy_test_data(list); 499 destroy_nodes_test_data(list);
493 } 500 }
494 501
495 void test_linked_list_sort(void) { 502 void test_linked_list_sort(void) {
496 int expected[] = { 503 int expected[] = {
497 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, 504 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
508 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, 515 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
509 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, 516 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
510 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 517 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
511 }; 518 };
512 519
513 void *begin = create_test_data(100, scrambled); 520 void *begin = create_nodes_test_data(100, scrambled);
514 void *end = cx_linked_list_last(begin, loc_next); 521 void *end = cx_linked_list_last(begin, loc_next);
515 522
516 cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data, 523 cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data,
517 false, cmp_int); 524 false, cmp_int);
518 525
519 struct node *check = begin; 526 struct node *check = begin;
520 struct node *check_last = NULL; 527 struct node *check_last = NULL;
521 CU_ASSERT_PTR_NULL(check->prev) 528 CU_ASSERT_PTR_NULL(check->prev)
522 CU_ASSERT_EQUAL(check->data, expected[0]) 529 CU_ASSERT_EQUAL(check->data, expected[0])
523 for (int i = 0; i < 100; i++) { 530 cx_for_n (i, 100) {
524 CU_ASSERT_EQUAL(check->data, expected[i]) 531 CU_ASSERT_EQUAL(check->data, expected[i])
525 CU_ASSERT_PTR_EQUAL(check->prev, check_last) 532 CU_ASSERT_PTR_EQUAL(check->prev, check_last)
526 if (i < 99) { 533 if (i < 99) {
527 CU_ASSERT_PTR_NOT_NULL(check->next) 534 CU_ASSERT_PTR_NOT_NULL(check->next)
528 } 535 }
530 check = check->next; 537 check = check->next;
531 } 538 }
532 CU_ASSERT_PTR_NULL(check) 539 CU_ASSERT_PTR_NULL(check)
533 CU_ASSERT_PTR_EQUAL(end, check_last) 540 CU_ASSERT_PTR_EQUAL(end, check_last)
534 541
535 destroy_test_data(begin); 542 destroy_nodes_test_data(begin);
536 } 543 }
537 544
538 void test_linked_list_reverse(void) { 545 void test_linked_list_reverse(void) {
539 void *begin, *end; 546 void *begin, *end;
540 547
541 int data[] = {2, 4, 6, 8}; 548 int data[] = {2, 4, 6, 8};
542 int reversed[] = {8, 6, 4, 2}; 549 int reversed[] = {8, 6, 4, 2};
543 550
544 void *list = create_test_data(4, data); 551 void *list = create_nodes_test_data(4, data);
545 void *expected = create_test_data(4, reversed); 552 void *expected = create_nodes_test_data(4, reversed);
546 553
547 begin = list; 554 begin = list;
548 end = cx_linked_list_last(list, loc_next); 555 end = cx_linked_list_last(list, loc_next);
549 556
550 cx_linked_list_reverse(&begin, &end, loc_prev, loc_next); 557 cx_linked_list_reverse(&begin, &end, loc_prev, loc_next);
551 CU_ASSERT_PTR_EQUAL(end, list) 558 CU_ASSERT_PTR_EQUAL(end, list)
552 CU_ASSERT_PTR_EQUAL(begin, cx_linked_list_first(end, loc_prev)) 559 CU_ASSERT_PTR_EQUAL(begin, cx_linked_list_first(end, loc_prev))
553 CU_ASSERT_TRUE(0 == cx_linked_list_compare(begin, expected, loc_next, loc_data, 560 CU_ASSERT_TRUE(0 == cx_linked_list_compare(begin, expected, loc_next, loc_data,
554 0, cmp_int)) 561 0, cmp_int))
555 562
556 destroy_test_data(begin); 563 destroy_nodes_test_data(begin);
557 destroy_test_data(expected); 564 destroy_nodes_test_data(expected);
558 } 565 }
559 566
560 static void verify_linked_list_create(CxList *list) { 567 static void verify_linked_list_create(CxList *list) {
561 CU_ASSERT_EQUAL(list->size, 0) 568 CU_ASSERT_EQUAL(list->size, 0)
562 CU_ASSERT_EQUAL(list->capacity, (size_t) -1) 569 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
580 587
581 void test_hl_linked_list_from_array(void) { 588 void test_hl_linked_list_from_array(void) {
582 int data[] = {2, 4, 5, 7, 10, 15}; 589 int data[] = {2, 4, 5, 7, 10, 15};
583 590
584 CxList *expected = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 591 CxList *expected = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
585 for (int i = 0; i < 5; i++) cxListAdd(expected, &data[i]); 592 cx_for_n (i, 5) cxListAdd(expected, &data[i]);
586 593
587 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 5, data); 594 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 5, data);
588 595
589 CU_ASSERT_TRUE(0 == cxListCompare(list, expected)) 596 CU_ASSERT_TRUE(0 == cxListCompare(list, expected))
590 597
596 CxList *list, 603 CxList *list,
597 int data[], 604 int data[],
598 size_t len, 605 size_t len,
599 bool write_through 606 bool write_through
600 ) { 607 ) {
601 for (size_t i = 0; i < len; i++) { 608 cx_for_n (i, len) CU_ASSERT_EQUAL(cxListAdd(list, &data[i]), 0)
602 CU_ASSERT_EQUAL(cxListAdd(list, &data[i]), 0)
603 }
604 CU_ASSERT_EQUAL(list->size, len) 609 CU_ASSERT_EQUAL(list->size, len)
605 CU_ASSERT_TRUE(list->capacity >= list->size) 610 CU_ASSERT_TRUE(list->capacity >= list->size)
606 for (size_t i = 0; i < len; i++) { 611 cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
607 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i]) 612 cx_for_n (i, len) ++data[i];
608 }
609 for (size_t i = 0; i < len; i++) {
610 ++data[i];
611 }
612 if (write_through) { 613 if (write_through) {
613 for (size_t i = 0; i < len; i++) { 614 cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
614 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
615 }
616 } else { 615 } else {
617 for (size_t i = 0; i < len; i++) { 616 cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i] - 1)
618 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i] - 1)
619 }
620 } 617 }
621 cxListDestroy(list); 618 cxListDestroy(list);
622 } 619 }
623 620
624 void test_hl_linked_list_add(void) { 621 void test_hl_linked_list_add(void) {
627 verify_hl_linked_list_add(list, data, sizeof(data) / sizeof(int), false); 624 verify_hl_linked_list_add(list, data, sizeof(data) / sizeof(int), false);
628 } 625 }
629 626
630 void test_hl_ptr_linked_list_add(void) { 627 void test_hl_ptr_linked_list_add(void) {
631 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 628 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
632 int data[] = {5, 47, 13, 9, 18, 1, 42}; 629 int data[] = {5, 47, 84, 13, 9, 18, 90, 1, 42};
633 verify_hl_linked_list_add(list, data, sizeof(data) / sizeof(int), true); 630 verify_hl_linked_list_add(list, data, sizeof(data) / sizeof(int), true);
634 } 631 }
635 632
636 void test_hl_linked_list_insert(void) { 633 static void verify_hl_linked_list_insert(CxList *list) {
637 int data;
638 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
639
640 data = 5;
641 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0)
642 CU_ASSERT_EQUAL(list->size, 0)
643 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
644 CU_ASSERT_EQUAL(list->size, 1)
645 data = 47;
646 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
647 CU_ASSERT_EQUAL(list->size, 2)
648 data = 13;
649 CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0)
650 CU_ASSERT_EQUAL(list->size, 3)
651 data = 42;
652 CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0)
653
654 CU_ASSERT_EQUAL(list->size, 4)
655 CU_ASSERT_TRUE(list->capacity >= list->size)
656
657 int exp[] = {47, 13, 5, 42};
658 CxList *expected = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 4, exp);
659 CU_ASSERT_TRUE(0 == cxListCompare(list, expected))
660
661 cxListDestroy(list);
662 cxListDestroy(expected);
663 }
664
665 void test_hl_ptr_linked_list_insert(void) {
666 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
667
668 int a = 5, b = 47, c = 13, d = 42; 634 int a = 5, b = 47, c = 13, d = 42;
669 635
670 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0) 636 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0)
671 CU_ASSERT_EQUAL(list->size, 0) 637 CU_ASSERT_EQUAL(list->size, 0)
672 CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0) 638 CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0)
686 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) 652 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
687 653
688 cxListDestroy(list); 654 cxListDestroy(list);
689 } 655 }
690 656
691 void test_hl_linked_list_remove(void) { 657 void test_hl_linked_list_insert(void) {
692 int data[] = {5, 47, 42, 13}; 658 verify_hl_linked_list_insert(cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)));
693 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, 659 }
694 sizeof(int), 4, data); 660
695 661 void test_hl_ptr_linked_list_insert(void) {
662 verify_hl_linked_list_insert(cxPointerLinkedListCreate(cxTestingAllocator, cmp_int));
663 }
664
665 static void verify_hl_linked_list_remove(CxList *list) {
696 CU_ASSERT_EQUAL(list->size, 4) 666 CU_ASSERT_EQUAL(list->size, 4)
697 CU_ASSERT_TRUE(list->capacity >= list->size) 667 CU_ASSERT_TRUE(list->capacity >= list->size)
698 668
699 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) 669 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
700 670
723 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) 693 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
724 694
725 cxListDestroy(list); 695 cxListDestroy(list);
726 } 696 }
727 697
698 void test_hl_linked_list_remove(void) {
699 int data[] = {5, 47, 42, 13};
700 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
701 sizeof(int), 4, data);
702 verify_hl_linked_list_remove(list);
703 }
704
728 void test_hl_ptr_linked_list_remove(void) { 705 void test_hl_ptr_linked_list_remove(void) {
729 int a = 5, b = 47, c = 42, d = 13; 706 int a = 5, b = 47, c = 42, d = 13;
730 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 707 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
731
732 cxListAdd(list, &a); 708 cxListAdd(list, &a);
733 cxListAdd(list, &b); 709 cxListAdd(list, &b);
734 cxListAdd(list, &c); 710 cxListAdd(list, &c);
735 cxListAdd(list, &d); 711 cxListAdd(list, &d);
736 712 verify_hl_linked_list_remove(list);
737 CU_ASSERT_EQUAL(list->size, 4) 713 }
738 CU_ASSERT_TRUE(list->capacity >= list->size) 714
739 715 static void verify_hl_linked_list_at(
740 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) 716 CxList *list,
741 717 size_t len,
742 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) 718 int *data
743 CU_ASSERT_EQUAL(list->size, 3) 719 ) {
744 CU_ASSERT_TRUE(list->capacity >= list->size) 720 CU_ASSERT_EQUAL(list->size, len)
745 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) 721 cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
746 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) 722 CU_ASSERT_PTR_NULL(cxListAt(list, len))
747 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) 723 free(data);
748
749 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
750 CU_ASSERT_EQUAL(list->size, 2)
751 CU_ASSERT_TRUE(list->capacity >= list->size)
752 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
753 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
754
755 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
756 CU_ASSERT_EQUAL(list->size, 1)
757 CU_ASSERT_TRUE(list->capacity >= list->size)
758 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
759
760 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
761 CU_ASSERT_EQUAL(list->size, 0)
762 CU_ASSERT_TRUE(list->capacity >= list->size)
763
764 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
765
766 cxListDestroy(list); 724 cxListDestroy(list);
767 } 725 }
768 726
769 void test_hl_linked_list_at(void) { 727 void test_hl_linked_list_at(void) {
770 int data[] = {5, 47, 13}; 728 size_t len = 100;
729 int *data = create_ints_test_data(len);
771 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, 730 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
772 sizeof(int), 3, data); 731 sizeof(int), len, data);
773 732 verify_hl_linked_list_at(list, len, data);
774 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) 733 }
775 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) 734
776 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) 735 void test_hl_ptr_linked_list_at(void) {
777 CU_ASSERT_PTR_NULL(cxListAt(list, 3)) 736 size_t len = 250;
778 737 int *data = create_ints_test_data(len);
738 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
739 cx_for_n (i, len) cxListAdd(list, &data[i]);
740 verify_hl_linked_list_at(list, len, data);
741 }
742
743 static void verify_hl_linked_list_find(
744 CxList *list,
745 size_t len,
746 int *data
747 ) {
748 cx_for_n (attempt, 100) {
749 size_t exp = rand() % len; // NOLINT(cert-msc50-cpp)
750 int val = data[exp];
751 cx_for_n (i, exp) {
752 if (data[i] == val) {
753 exp = i;
754 break;
755 }
756 }
757 CU_ASSERT_EQUAL(cxListFind(list, &val), exp)
758 }
759 free(data);
779 cxListDestroy(list); 760 cxListDestroy(list);
780 } 761 }
781 762
782 void test_hl_ptr_linked_list_at(void) { 763 void test_hl_linked_list_find(void) {
764 size_t len = 100;
765 int *data = create_ints_test_data(len);
766 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), len, data);
767 verify_hl_linked_list_find(list, len, data);
768 }
769
770 void test_hl_ptr_linked_list_find(void) {
771 size_t len = 250;
772 int *data = create_ints_test_data(len);
783 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 773 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
784 774 cx_for_n (i, len) cxListAdd(list, &data[i]);
785 int a = 5, b = 47, c = 13; 775 verify_hl_linked_list_find(list, len, data);
786 cxListAdd(list, &a); 776 }
787 cxListAdd(list, &b); 777
788 cxListAdd(list, &c); 778 struct sort_test_data {
789 779 size_t len;
790 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) 780 int *data;
791 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) 781 int *sorted;
792 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) 782 };
793 CU_ASSERT_PTR_NULL(cxListAt(list, 3)) 783
794 784 static struct sort_test_data create_sort_test_data(void) {
785 size_t len = 1000;
786 int *data = create_ints_test_data(len);
787 int *sorted = malloc(sizeof(int) * len);
788 memcpy(sorted, data, sizeof(int) * len);
789 qsort(sorted, len, sizeof(int), cmp_int);
790 struct sort_test_data s = {len, data, sorted};
791 return s;
792 }
793
794 static void free_sort_test_data(struct sort_test_data s) {
795 free(s.data);
796 free(s.sorted);
797 }
798
799 static void verify_hl_linked_list_sort(
800 CxList *list,
801 struct sort_test_data td
802 ) {
803 cxListSort(list);
804 cx_for_n (i, td.len) CU_ASSERT_EQUAL_FATAL(*(int *) cxListAt(list, i), td.sorted[i])
805 free_sort_test_data(td);
795 cxListDestroy(list); 806 cxListDestroy(list);
796 } 807 }
797 808
798 void test_hl_linked_list_find(void) { 809 void test_hl_linked_list_sort(void) {
799 int data[] = {5, 47, 13}; 810 struct sort_test_data td = create_sort_test_data();
800 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, 811 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), td.len, td.data);
801 sizeof(int), 3, data); 812 verify_hl_linked_list_sort(list, td);
802 CU_ASSERT_EQUAL(list->size, 3) 813 }
803 CU_ASSERT_TRUE(list->capacity >= list->size) 814
804 815 void test_hl_ptr_linked_list_sort(void) {
805 int criteria; 816 struct sort_test_data td = create_sort_test_data();
806
807 criteria = 5;
808 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
809 criteria = 47;
810 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
811 criteria = 13;
812 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
813 criteria = 9000;
814 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
815 criteria = -5;
816 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
817
818 cxListDestroy(list);
819 }
820
821 void test_hl_ptr_linked_list_find(void) {
822 int a = 5, b = 47, c = 13, criteria;
823 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 817 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
824 818 cx_for_n (i, td.len) cxListAdd(list, &td.data[i]);
825 cxListAdd(list, &a); 819 verify_hl_linked_list_sort(list, td);
826 cxListAdd(list, &b); 820 }
827 cxListAdd(list, &c); 821
828 822 void verify_hl_linked_list_iterator(CxList *list) {
829 CU_ASSERT_EQUAL(list->size, 3)
830 CU_ASSERT_TRUE(list->capacity >= list->size)
831
832 criteria = 5;
833 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
834 criteria = 47;
835 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
836 criteria = 13;
837 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
838 criteria = 9000;
839 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
840 criteria = -5;
841 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
842 b = -5;
843 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
844
845 cxListDestroy(list);
846 }
847
848 void test_hl_linked_list_sort(void) {
849 int expected[] = {
850 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
851 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
852 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
853 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
854 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
855 4785, 4791, 4801, 4859, 4903, 4973
856 };
857 int scrambled[] = {
858 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
859 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
860 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
861 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
862 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
863 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
864 };
865 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, scrambled);
866 CxList *exp = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, expected);
867
868 cxListSort(list);
869 CU_ASSERT_TRUE(0 == cxListCompare(list, exp))
870
871 cxListDestroy(list);
872 cxListDestroy(exp);
873 }
874
875 void test_hl_ptr_linked_list_sort(void) {
876 int expected[] = {
877 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
878 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
879 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
880 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
881 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
882 4785, 4791, 4801, 4859, 4903, 4973
883 };
884 int scrambled[] = {
885 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
886 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
887 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
888 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
889 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
890 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
891 };
892
893 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
894
895 for (int i = 0; i < 100; i++) {
896 cxListAdd(list, &scrambled[i]);
897 }
898
899 cxListSort(list);
900
901 for (int i = 0; i < 100; i++) {
902 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
903 }
904
905 cxListDestroy(list);
906 }
907
908 void test_hl_linked_list_iterator_impl(CxList *list) {
909 int i = 0; 823 int i = 0;
910 CxIterator iter = cxListBegin(list); 824 CxIterator iter = cxListBegin(list);
911 cx_foreach(int*, x, iter) { 825 cx_foreach(int*, x, iter) {
912 CU_ASSERT_EQUAL(iter.index, (size_t) (i + 1) / 2) 826 CU_ASSERT_EQUAL(iter.index, (size_t) (i + 1) / 2)
913 CU_ASSERT_EQUAL(*x, i) 827 CU_ASSERT_EQUAL(*x, i)
914 if (*x % 2 == 1) iter.remove = true; 828 if (*x % 2 == 1) iter.remove = true;
915 i++; 829 i++;
916 } 830 }
917 CU_ASSERT_EQUAL(i, 10) 831 CU_ASSERT_EQUAL(i, 10)
918 CU_ASSERT_EQUAL_FATAL(list->size, 5) 832 CU_ASSERT_EQUAL_FATAL(list->size, 5)
919 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 0) 833 cx_for_n(j, 5) CU_ASSERT_EQUAL(*(int *) cxListAt(list, j), (int) j * 2)
920 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 2)
921 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 4)
922 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 6)
923 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 4), 8)
924 cxListDestroy(list); 834 cxListDestroy(list);
925 } 835 }
926 836
927 void test_hl_linked_list_iterator(void) { 837 void test_hl_linked_list_iterator(void) {
928 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 838 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
929 for (int i = 0; i < 10; i++) { 839 cx_for_n (i, 10) cxListAdd(list, &i);
930 cxListAdd(list, &i); 840 verify_hl_linked_list_iterator(list);
931 }
932 test_hl_linked_list_iterator_impl(list);
933 } 841 }
934 842
935 void test_hl_ptr_linked_list_iterator(void) { 843 void test_hl_ptr_linked_list_iterator(void) {
936 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 844 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
937 int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 845 int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
938 for (int i = 0; i < 10; i++) { 846 cx_for_n (i, 10) cxListAdd(list, &data[i]);
939 cxListAdd(list, &data[i]); 847 verify_hl_linked_list_iterator(list);
940 } 848 }
941 test_hl_linked_list_iterator_impl(list); 849
942 } 850 static void verify_hl_linked_list_insert_via_iterator(
943 851 CxList *list,
944 void test_hl_linked_list_insert_via_iterator(void) { 852 int *testdata
945 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 853 ) {
946 for (int i = 0; i < 5; i++) {
947 cxListAdd(list, &i);
948 }
949 CxIterator iter = cxListIterator(list, 2); 854 CxIterator iter = cxListIterator(list, 2);
950 CU_ASSERT_EQUAL(iter.index, 2) 855 CU_ASSERT_EQUAL(iter.index, 2)
951 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) 856 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
952 857 size_t i = 4;
953 int data = 10; 858
954 cxListInsertAfter(&iter, &data); 859 ++i;
860 cxListInsertAfter(&iter, &testdata[i]);
955 CU_ASSERT_EQUAL(iter.index, 2) 861 CU_ASSERT_EQUAL(iter.index, 2)
956 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) 862 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
957 data = 20; 863 ++i;
958 cxListInsertBefore(&iter, &data); 864 cxListInsertBefore(&iter, &testdata[i]);
959 CU_ASSERT_EQUAL(iter.index, 3) 865 CU_ASSERT_EQUAL(iter.index, 3)
960 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) 866 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
961 867
962 data = 30;
963 iter = cxListBegin(list); 868 iter = cxListBegin(list);
964 cxListInsertBefore(&iter, &data); 869 ++i;
870 cxListInsertBefore(&iter, &testdata[i]);
965 CU_ASSERT_EQUAL(iter.index, 1) 871 CU_ASSERT_EQUAL(iter.index, 1)
966 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0) 872 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0)
967 data = 40;
968 iter = cxListIterator(list, list->size); 873 iter = cxListIterator(list, list->size);
969 cxListInsertBefore(&iter, &data); 874 ++i;
875 cxListInsertBefore(&iter, &testdata[i]);
970 CU_ASSERT_EQUAL(iter.index, 9) 876 CU_ASSERT_EQUAL(iter.index, 9)
971 CU_ASSERT_FALSE(cxIteratorValid(&iter)) 877 CU_ASSERT_FALSE(cxIteratorValid(&iter))
972 data = 50;
973 iter = cxListIterator(list, list->size); 878 iter = cxListIterator(list, list->size);
974 cxListInsertAfter(&iter, &data); 879 ++i;
880 cxListInsertAfter(&iter, &testdata[i]);
975 CU_ASSERT_EQUAL(iter.index, 10) 881 CU_ASSERT_EQUAL(iter.index, 10)
976 CU_ASSERT_FALSE(cxIteratorValid(&iter)) 882 CU_ASSERT_FALSE(cxIteratorValid(&iter))
977 883
978 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; 884 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};
979 CxList *expected = cxLinkedListFromArray(cxTestingAllocator, 885 cx_for_n (j, 10) CU_ASSERT_EQUAL(*(int *) cxListAt(list, j), expdata[j])
980 cmp_int, sizeof(int), 10, expdata);
981
982 CU_ASSERT_EQUAL(0, cxListCompare(list, expected))
983 cxListDestroy(list); 886 cxListDestroy(list);
984 cxListDestroy(expected); 887 }
888
889 void test_hl_linked_list_insert_via_iterator(void) {
890 int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50};
891 // only add the first five elements, the remaining five will be inserted
892 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 5, testdata);
893 verify_hl_linked_list_insert_via_iterator(list, testdata);
985 } 894 }
986 895
987 void test_hl_ptr_linked_list_insert_via_iterator(void) { 896 void test_hl_ptr_linked_list_insert_via_iterator(void) {
988 int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50}; 897 int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50};
989 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 898 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
990 int i; 899 // only add the first five elements, the remaining five will be inserted
991 for (i = 0; i < 5; i++) { 900 cx_for_n (i, 5) cxListAdd(list, &testdata[i]);
992 cxListAdd(list, &testdata[i]); 901 verify_hl_linked_list_insert_via_iterator(list, testdata);
993 }
994 CxIterator iter = cxListIterator(list, 2);
995 CU_ASSERT_EQUAL(iter.index, 2)
996 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
997
998 cxListInsertAfter(&iter, &testdata[i++]);
999 CU_ASSERT_EQUAL(iter.index, 2)
1000 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
1001 cxListInsertBefore(&iter, &testdata[i++]);
1002 CU_ASSERT_EQUAL(iter.index, 3)
1003 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
1004
1005 iter = cxListBegin(list);
1006 cxListInsertBefore(&iter, &testdata[i++]);
1007 CU_ASSERT_EQUAL(iter.index, 1)
1008 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0)
1009 iter = cxListIterator(list, list->size);
1010 cxListInsertBefore(&iter, &testdata[i++]);
1011 CU_ASSERT_EQUAL(iter.index, 9)
1012 CU_ASSERT_FALSE(cxIteratorValid(&iter))
1013 iter = cxListIterator(list, list->size);
1014 cxListInsertAfter(&iter, &testdata[i++]);
1015 CU_ASSERT_EQUAL(iter.index, 10)
1016 CU_ASSERT_FALSE(cxIteratorValid(&iter))
1017
1018 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};
1019 for (i = 0; i < 10; i++) {
1020 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expdata[i])
1021 }
1022
1023 cxListDestroy(list);
1024 } 902 }
1025 903
1026 static void test_setup_allocator(void) { 904 static void test_setup_allocator(void) {
1027 cxTestingAllocatorReset(); 905 cxTestingAllocatorReset();
1028 } 906 }

mercurial