test/test_list.c

changeset 507
2e8878770de0
parent 506
18782bbe3607
child 509
0d3c6075f82c
equal deleted inserted replaced
506:18782bbe3607 507:2e8878770de0
28 28
29 #include "cx/linked_list.h" 29 #include "cx/linked_list.h"
30 #include "test_config.h" 30 #include "test_config.h"
31 #include "util_allocator.h" 31 #include "util_allocator.h"
32 32
33 int cmp_int_impl( 33 static int cmp_int_impl(
34 int const *l, 34 int const *l,
35 int const *r 35 int const *r
36 ) { 36 ) {
37 int left = *l, right = *r; 37 int left = *l, right = *r;
38 return left == right ? 0 : (left < right ? -1 : 1); 38 return left == right ? 0 : (left < right ? -1 : 1);
555 555
556 destroy_test_data(begin); 556 destroy_test_data(begin);
557 destroy_test_data(expected); 557 destroy_test_data(expected);
558 } 558 }
559 559
560 static void test_linked_list_create(CxList *list) { 560 static void verify_linked_list_create(CxList *list) {
561 CU_ASSERT_EQUAL(list->size, 0) 561 CU_ASSERT_EQUAL(list->size, 0)
562 CU_ASSERT_EQUAL(list->capacity, (size_t) -1) 562 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
563 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) 563 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
564 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) 564 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
565 565
567 } 567 }
568 568
569 void test_hl_linked_list_create(void) { 569 void test_hl_linked_list_create(void) {
570 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 570 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
571 CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) 571 CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
572 test_linked_list_create(list); 572 verify_linked_list_create(list);
573 } 573 }
574 574
575 void test_hl_ptr_linked_list_create(void) { 575 void test_hl_ptr_linked_list_create(void) {
576 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 576 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
577 CU_ASSERT_EQUAL(list->itemsize, sizeof(void *)) 577 CU_ASSERT_EQUAL(list->itemsize, sizeof(void *))
578 test_linked_list_create(list); 578 verify_linked_list_create(list);
579 } 579 }
580 580
581 void test_hl_linked_list_from_array(void) { 581 void test_hl_linked_list_from_array(void) {
582 int data[] = {2, 4, 5, 7, 10, 15}; 582 int data[] = {2, 4, 5, 7, 10, 15};
583 583
590 590
591 cxListDestroy(list); 591 cxListDestroy(list);
592 cxListDestroy(expected); 592 cxListDestroy(expected);
593 } 593 }
594 594
595 static void verify_hl_linked_list_add(
596 CxList *list,
597 int data[],
598 size_t len,
599 bool write_through
600 ) {
601 for (size_t i = 0; i < len; i++) {
602 CU_ASSERT_EQUAL(cxListAdd(list, &data[i]), 0)
603 }
604 CU_ASSERT_EQUAL(list->size, len)
605 CU_ASSERT_TRUE(list->capacity >= list->size)
606 for (size_t i = 0; i < len; i++) {
607 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
608 }
609 for (size_t i = 0; i < len; i++) {
610 ++data[i];
611 }
612 if (write_through) {
613 for (size_t i = 0; i < len; i++) {
614 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
615 }
616 } else {
617 for (size_t i = 0; i < len; i++) {
618 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i] - 1)
619 }
620 }
621 cxListDestroy(list);
622 }
623
595 void test_hl_linked_list_add(void) { 624 void test_hl_linked_list_add(void) {
596 int data;
597 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 625 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
598 626 int data[] = {5, 47, 13, 9, 18, 1, 42};
599 data = 5; 627 verify_hl_linked_list_add(list, data, sizeof(data) / sizeof(int), false);
600 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
601 data = 47;
602 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
603 data = 13;
604 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
605
606 CU_ASSERT_EQUAL(list->size, 3)
607 CU_ASSERT_TRUE(list->capacity >= list->size)
608
609 int exp[] = {5, 47, 13};
610 CxList *expected = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 3, exp);
611 CU_ASSERT_TRUE(0 == cxListCompare(list, expected))
612
613 cxListDestroy(list);
614 cxListDestroy(expected);
615 } 628 }
616 629
617 void test_hl_ptr_linked_list_add(void) { 630 void test_hl_ptr_linked_list_add(void) {
618 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 631 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
619 632 int data[] = {5, 47, 13, 9, 18, 1, 42};
620 int a = 5, b = 47, c = 13; 633 verify_hl_linked_list_add(list, data, sizeof(data) / sizeof(int), true);
621
622 CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
623 CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
624 CU_ASSERT_EQUAL(cxListAdd(list, &c), 0)
625
626 CU_ASSERT_EQUAL(list->size, 3)
627 CU_ASSERT_TRUE(list->capacity >= list->size)
628
629 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
630 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
631 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
632
633 a = 9;
634 b = 10;
635 c = 11;
636
637 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9)
638 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10)
639 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11)
640
641 cxListDestroy(list);
642 } 634 }
643 635
644 void test_hl_linked_list_insert(void) { 636 void test_hl_linked_list_insert(void) {
645 int data; 637 int data;
646 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 638 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));

mercurial