test/test_list.c

changeset 503
a89857072ace
parent 500
eb9e7bd40a8e
child 506
18782bbe3607
equal deleted inserted replaced
502:33e7b6ebf403 503:a89857072ace
566 CU_ASSERT_EQUAL(list->capacity, (size_t) -1) 566 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
567 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) 567 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
568 CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) 568 CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
569 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) 569 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
570 570
571 cxLinkedListDestroy(list); 571 cxListDestroy(list);
572 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 572 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
573 } 573 }
574 574
575 void test_hl_ptr_linked_list_create(void) { 575 void test_hl_ptr_linked_list_create(void) {
576 cxTestingAllocatorReset(); 576 cxTestingAllocatorReset();
581 CU_ASSERT_EQUAL(list->capacity, (size_t) -1) 581 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
582 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) 582 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
583 CU_ASSERT_EQUAL(list->itemsize, sizeof(void *)) 583 CU_ASSERT_EQUAL(list->itemsize, sizeof(void *))
584 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) 584 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
585 585
586 cxLinkedListDestroy(list); 586 cxListDestroy(list);
587 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 587 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
588 } 588 }
589 589
590 void test_hl_linked_list_from_array(void) { 590 void test_hl_linked_list_from_array(void) {
591 cxTestingAllocatorReset(); 591 cxTestingAllocatorReset();
597 597
598 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 5, data); 598 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 5, data);
599 599
600 CU_ASSERT_TRUE(0 == cxListCompare(list, expected)) 600 CU_ASSERT_TRUE(0 == cxListCompare(list, expected))
601 601
602 cxLinkedListDestroy(list); 602 cxListDestroy(list);
603 cxLinkedListDestroy(expected); 603 cxListDestroy(expected);
604 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 604 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
605 } 605 }
606 606
607 void test_hl_linked_list_add(void) { 607 void test_hl_linked_list_add(void) {
608 cxTestingAllocatorReset(); 608 cxTestingAllocatorReset();
622 622
623 int exp[] = {5, 47, 13}; 623 int exp[] = {5, 47, 13};
624 CxList *expected = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 3, exp); 624 CxList *expected = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 3, exp);
625 CU_ASSERT_TRUE(0 == cxListCompare(list, expected)) 625 CU_ASSERT_TRUE(0 == cxListCompare(list, expected))
626 626
627 cxLinkedListDestroy(list); 627 cxListDestroy(list);
628 cxLinkedListDestroy(expected); 628 cxListDestroy(expected);
629 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 629 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
630 } 630 }
631 631
632 void test_hl_ptr_linked_list_add(void) { 632 void test_hl_ptr_linked_list_add(void) {
633 cxTestingAllocatorReset(); 633 cxTestingAllocatorReset();
653 653
654 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9) 654 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9)
655 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10) 655 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10)
656 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11) 656 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11)
657 657
658 cxLinkedListDestroy(list); 658 cxListDestroy(list);
659 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 659 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
660 } 660 }
661 661
662 void test_hl_linked_list_insert(void) { 662 void test_hl_linked_list_insert(void) {
663 cxTestingAllocatorReset(); 663 cxTestingAllocatorReset();
684 684
685 int exp[] = {47, 13, 5, 42}; 685 int exp[] = {47, 13, 5, 42};
686 CxList *expected = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 4, exp); 686 CxList *expected = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 4, exp);
687 CU_ASSERT_TRUE(0 == cxListCompare(list, expected)) 687 CU_ASSERT_TRUE(0 == cxListCompare(list, expected))
688 688
689 cxLinkedListDestroy(list); 689 cxListDestroy(list);
690 cxLinkedListDestroy(expected); 690 cxListDestroy(expected);
691 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 691 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
692 } 692 }
693 693
694 void test_hl_ptr_linked_list_insert(void) { 694 void test_hl_ptr_linked_list_insert(void) {
695 cxTestingAllocatorReset(); 695 cxTestingAllocatorReset();
714 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) 714 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
715 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) 715 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
716 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5) 716 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
717 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) 717 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
718 718
719 cxLinkedListDestroy(list); 719 cxListDestroy(list);
720 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 720 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
721 } 721 }
722 722
723 void test_hl_linked_list_remove(void) { 723 void test_hl_linked_list_remove(void) {
724 cxTestingAllocatorReset(); 724 cxTestingAllocatorReset();
754 CU_ASSERT_EQUAL(list->size, 0) 754 CU_ASSERT_EQUAL(list->size, 0)
755 CU_ASSERT_TRUE(list->capacity >= list->size) 755 CU_ASSERT_TRUE(list->capacity >= list->size)
756 756
757 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) 757 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
758 758
759 cxLinkedListDestroy(list); 759 cxListDestroy(list);
760 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 760 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
761 } 761 }
762 762
763 void test_hl_ptr_linked_list_remove(void) { 763 void test_hl_ptr_linked_list_remove(void) {
764 cxTestingAllocatorReset(); 764 cxTestingAllocatorReset();
798 CU_ASSERT_EQUAL(list->size, 0) 798 CU_ASSERT_EQUAL(list->size, 0)
799 CU_ASSERT_TRUE(list->capacity >= list->size) 799 CU_ASSERT_TRUE(list->capacity >= list->size)
800 800
801 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) 801 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
802 802
803 cxLinkedListDestroy(list); 803 cxListDestroy(list);
804 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 804 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
805 } 805 }
806 806
807 void test_hl_linked_list_at(void) { 807 void test_hl_linked_list_at(void) {
808 cxTestingAllocatorReset(); 808 cxTestingAllocatorReset();
814 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) 814 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
815 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) 815 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
816 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) 816 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
817 CU_ASSERT_PTR_NULL(cxListAt(list, 3)) 817 CU_ASSERT_PTR_NULL(cxListAt(list, 3))
818 818
819 cxLinkedListDestroy(list); 819 cxListDestroy(list);
820 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 820 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
821 } 821 }
822 822
823 void test_hl_ptr_linked_list_at(void) { 823 void test_hl_ptr_linked_list_at(void) {
824 cxTestingAllocatorReset(); 824 cxTestingAllocatorReset();
833 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) 833 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
834 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) 834 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
835 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) 835 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
836 CU_ASSERT_PTR_NULL(cxListAt(list, 3)) 836 CU_ASSERT_PTR_NULL(cxListAt(list, 3))
837 837
838 cxLinkedListDestroy(list); 838 cxListDestroy(list);
839 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 839 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
840 } 840 }
841 841
842 void test_hl_linked_list_find(void) { 842 void test_hl_linked_list_find(void) {
843 cxTestingAllocatorReset(); 843 cxTestingAllocatorReset();
859 criteria = 9000; 859 criteria = 9000;
860 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) 860 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
861 criteria = -5; 861 criteria = -5;
862 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) 862 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
863 863
864 cxLinkedListDestroy(list); 864 cxListDestroy(list);
865 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 865 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
866 } 866 }
867 867
868 void test_hl_ptr_linked_list_find(void) { 868 void test_hl_ptr_linked_list_find(void) {
869 cxTestingAllocatorReset(); 869 cxTestingAllocatorReset();
889 criteria = -5; 889 criteria = -5;
890 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) 890 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
891 b = -5; 891 b = -5;
892 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) 892 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
893 893
894 cxLinkedListDestroy(list); 894 cxListDestroy(list);
895 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 895 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
896 } 896 }
897 897
898 void test_hl_linked_list_sort(void) { 898 void test_hl_linked_list_sort(void) {
899 int expected[] = { 899 int expected[] = {
919 CxList *exp = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, expected); 919 CxList *exp = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, expected);
920 920
921 cxListSort(list); 921 cxListSort(list);
922 CU_ASSERT_TRUE(0 == cxListCompare(list, exp)) 922 CU_ASSERT_TRUE(0 == cxListCompare(list, exp))
923 923
924 cxLinkedListDestroy(list); 924 cxListDestroy(list);
925 cxLinkedListDestroy(exp); 925 cxListDestroy(exp);
926 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 926 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
927 } 927 }
928 928
929 void test_hl_ptr_linked_list_sort(void) { 929 void test_hl_ptr_linked_list_sort(void) {
930 int expected[] = { 930 int expected[] = {
956 956
957 for (int i = 0; i < 100; i++) { 957 for (int i = 0; i < 100; i++) {
958 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i]) 958 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
959 } 959 }
960 960
961 cxLinkedListDestroy(list); 961 cxListDestroy(list);
962 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 962 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
963 } 963 }
964 964
965 void test_hl_linked_list_iterator_impl(CxList *list) { 965 void test_hl_linked_list_iterator_impl(CxList *list) {
966 int i = 0; 966 int i = 0;
976 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 0) 976 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 0)
977 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 2) 977 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 2)
978 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 4) 978 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 4)
979 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 6) 979 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 6)
980 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 4), 8) 980 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 4), 8)
981 cxLinkedListDestroy(list); 981 cxListDestroy(list);
982 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 982 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
983 } 983 }
984 984
985 void test_hl_linked_list_iterator(void) { 985 void test_hl_linked_list_iterator(void) {
986 cxTestingAllocatorReset(); 986 cxTestingAllocatorReset();
1039 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; 1039 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};
1040 CxList *expected = cxLinkedListFromArray(cxTestingAllocator, 1040 CxList *expected = cxLinkedListFromArray(cxTestingAllocator,
1041 cmp_int, sizeof(int), 10, expdata); 1041 cmp_int, sizeof(int), 10, expdata);
1042 1042
1043 CU_ASSERT_EQUAL(0, cxListCompare(list, expected)) 1043 CU_ASSERT_EQUAL(0, cxListCompare(list, expected))
1044 cxLinkedListDestroy(list); 1044 cxListDestroy(list);
1045 cxLinkedListDestroy(expected); 1045 cxListDestroy(expected);
1046 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1046 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
1047 } 1047 }
1048 1048
1049 void test_hl_ptr_linked_list_insert_via_iterator(void) { 1049 void test_hl_ptr_linked_list_insert_via_iterator(void) {
1050 int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50}; 1050 int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50};
1081 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; 1081 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};
1082 for (i = 0; i < 10; i++) { 1082 for (i = 0; i < 10; i++) {
1083 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expdata[i]) 1083 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expdata[i])
1084 } 1084 }
1085 1085
1086 cxLinkedListDestroy(list); 1086 cxListDestroy(list);
1087 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1087 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
1088 } 1088 }
1089 1089
1090 int main() { 1090 int main() {
1091 CU_pSuite suite = NULL; 1091 CU_pSuite suite = NULL;

mercurial