test/test_list.c

changeset 500
eb9e7bd40a8e
parent 499
3dc9075df822
child 503
a89857072ace
equal deleted inserted replaced
499:3dc9075df822 500:eb9e7bd40a8e
558 } 558 }
559 559
560 void test_hl_linked_list_create(void) { 560 void test_hl_linked_list_create(void) {
561 cxTestingAllocatorReset(); 561 cxTestingAllocatorReset();
562 562
563 CxList list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 563 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
564 564
565 CU_ASSERT_EQUAL(list->size, 0) 565 CU_ASSERT_EQUAL(list->size, 0)
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))
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();
577 577
578 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 578 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
579 579
580 CU_ASSERT_EQUAL(list->size, 0) 580 CU_ASSERT_EQUAL(list->size, 0)
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 *))
590 void test_hl_linked_list_from_array(void) { 590 void test_hl_linked_list_from_array(void) {
591 cxTestingAllocatorReset(); 591 cxTestingAllocatorReset();
592 592
593 int data[] = {2, 4, 5, 7, 10, 15}; 593 int data[] = {2, 4, 5, 7, 10, 15};
594 594
595 CxList expected = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 595 CxList *expected = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
596 for (int i = 0; i < 5; i++) cxListAdd(expected, &data[i]); 596 for (int i = 0; i < 5; i++) cxListAdd(expected, &data[i]);
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 cxLinkedListDestroy(list);
603 cxLinkedListDestroy(expected); 603 cxLinkedListDestroy(expected);
606 606
607 void test_hl_linked_list_add(void) { 607 void test_hl_linked_list_add(void) {
608 cxTestingAllocatorReset(); 608 cxTestingAllocatorReset();
609 609
610 int data; 610 int data;
611 CxList list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 611 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
612 612
613 data = 5; 613 data = 5;
614 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) 614 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
615 data = 47; 615 data = 47;
616 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) 616 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
619 619
620 CU_ASSERT_EQUAL(list->size, 3) 620 CU_ASSERT_EQUAL(list->size, 3)
621 CU_ASSERT_TRUE(list->capacity >= list->size) 621 CU_ASSERT_TRUE(list->capacity >= list->size)
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 cxLinkedListDestroy(list);
628 cxLinkedListDestroy(expected); 628 cxLinkedListDestroy(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();
634 634
635 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 635 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
636 636
637 int a = 5, b = 47, c = 13; 637 int a = 5, b = 47, c = 13;
638 638
639 CU_ASSERT_EQUAL(cxListAdd(list, &a), 0) 639 CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
640 CU_ASSERT_EQUAL(cxListAdd(list, &b), 0) 640 CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
661 661
662 void test_hl_linked_list_insert(void) { 662 void test_hl_linked_list_insert(void) {
663 cxTestingAllocatorReset(); 663 cxTestingAllocatorReset();
664 664
665 int data; 665 int data;
666 CxList list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 666 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
667 667
668 data = 5; 668 data = 5;
669 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0) 669 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0)
670 CU_ASSERT_EQUAL(list->size, 0) 670 CU_ASSERT_EQUAL(list->size, 0)
671 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0) 671 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
681 681
682 CU_ASSERT_EQUAL(list->size, 4) 682 CU_ASSERT_EQUAL(list->size, 4)
683 CU_ASSERT_TRUE(list->capacity >= list->size) 683 CU_ASSERT_TRUE(list->capacity >= list->size)
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 cxLinkedListDestroy(list);
690 cxLinkedListDestroy(expected); 690 cxLinkedListDestroy(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();
696 696
697 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 697 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
698 698
699 int a = 5, b = 47, c = 13, d = 42; 699 int a = 5, b = 47, c = 13, d = 42;
700 700
701 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0) 701 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0)
702 CU_ASSERT_EQUAL(list->size, 0) 702 CU_ASSERT_EQUAL(list->size, 0)
722 722
723 void test_hl_linked_list_remove(void) { 723 void test_hl_linked_list_remove(void) {
724 cxTestingAllocatorReset(); 724 cxTestingAllocatorReset();
725 725
726 int data[] = {5, 47, 42, 13}; 726 int data[] = {5, 47, 42, 13};
727 CxList list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, 727 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
728 sizeof(int), 4, data); 728 sizeof(int), 4, data);
729 729
730 CU_ASSERT_EQUAL(list->size, 4) 730 CU_ASSERT_EQUAL(list->size, 4)
731 CU_ASSERT_TRUE(list->capacity >= list->size) 731 CU_ASSERT_TRUE(list->capacity >= list->size)
732 732
733 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) 733 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
762 762
763 void test_hl_ptr_linked_list_remove(void) { 763 void test_hl_ptr_linked_list_remove(void) {
764 cxTestingAllocatorReset(); 764 cxTestingAllocatorReset();
765 765
766 int a = 5, b = 47, c = 42, d = 13; 766 int a = 5, b = 47, c = 42, d = 13;
767 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 767 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
768 768
769 cxListAdd(list, &a); 769 cxListAdd(list, &a);
770 cxListAdd(list, &b); 770 cxListAdd(list, &b);
771 cxListAdd(list, &c); 771 cxListAdd(list, &c);
772 cxListAdd(list, &d); 772 cxListAdd(list, &d);
806 806
807 void test_hl_linked_list_at(void) { 807 void test_hl_linked_list_at(void) {
808 cxTestingAllocatorReset(); 808 cxTestingAllocatorReset();
809 809
810 int data[] = {5, 47, 13}; 810 int data[] = {5, 47, 13};
811 CxList list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, 811 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
812 sizeof(int), 3, data); 812 sizeof(int), 3, data);
813 813
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))
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();
825 825
826 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 826 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
827 827
828 int a = 5, b = 47, c = 13; 828 int a = 5, b = 47, c = 13;
829 cxListAdd(list, &a); 829 cxListAdd(list, &a);
830 cxListAdd(list, &b); 830 cxListAdd(list, &b);
831 cxListAdd(list, &c); 831 cxListAdd(list, &c);
841 841
842 void test_hl_linked_list_find(void) { 842 void test_hl_linked_list_find(void) {
843 cxTestingAllocatorReset(); 843 cxTestingAllocatorReset();
844 844
845 int data[] = {5, 47, 13}; 845 int data[] = {5, 47, 13};
846 CxList list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, 846 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
847 sizeof(int), 3, data); 847 sizeof(int), 3, data);
848 CU_ASSERT_EQUAL(list->size, 3) 848 CU_ASSERT_EQUAL(list->size, 3)
849 CU_ASSERT_TRUE(list->capacity >= list->size) 849 CU_ASSERT_TRUE(list->capacity >= list->size)
850 850
851 int criteria; 851 int criteria;
852 852
867 867
868 void test_hl_ptr_linked_list_find(void) { 868 void test_hl_ptr_linked_list_find(void) {
869 cxTestingAllocatorReset(); 869 cxTestingAllocatorReset();
870 870
871 int a = 5, b = 47, c = 13, criteria; 871 int a = 5, b = 47, c = 13, criteria;
872 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 872 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
873 873
874 cxListAdd(list, &a); 874 cxListAdd(list, &a);
875 cxListAdd(list, &b); 875 cxListAdd(list, &b);
876 cxListAdd(list, &c); 876 cxListAdd(list, &c);
877 877
913 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 913 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
914 }; 914 };
915 915
916 cxTestingAllocatorReset(); 916 cxTestingAllocatorReset();
917 917
918 CxList list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, scrambled); 918 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, scrambled);
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 cxLinkedListDestroy(list);
944 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 944 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
945 }; 945 };
946 946
947 cxTestingAllocatorReset(); 947 cxTestingAllocatorReset();
948 948
949 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 949 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
950 950
951 for (int i = 0; i < 100; i++) { 951 for (int i = 0; i < 100; i++) {
952 cxListAdd(list, &scrambled[i]); 952 cxListAdd(list, &scrambled[i]);
953 } 953 }
954 954
960 960
961 cxLinkedListDestroy(list); 961 cxLinkedListDestroy(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;
967 CxIterator iter = cxListBegin(list); 967 CxIterator iter = cxListBegin(list);
968 cx_foreach(int*, x, iter) { 968 cx_foreach(int*, x, iter) {
969 CU_ASSERT_EQUAL(iter.index, (size_t) (i + 1) / 2) 969 CU_ASSERT_EQUAL(iter.index, (size_t) (i + 1) / 2)
970 CU_ASSERT_EQUAL(*x, i) 970 CU_ASSERT_EQUAL(*x, i)
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();
987 CxList list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 987 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
988 for (int i = 0; i < 10; i++) { 988 for (int i = 0; i < 10; i++) {
989 cxListAdd(list, &i); 989 cxListAdd(list, &i);
990 } 990 }
991 test_hl_linked_list_iterator_impl(list); 991 test_hl_linked_list_iterator_impl(list);
992 } 992 }
993 993
994 void test_hl_ptr_linked_list_iterator(void) { 994 void test_hl_ptr_linked_list_iterator(void) {
995 cxTestingAllocatorReset(); 995 cxTestingAllocatorReset();
996 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 996 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
997 int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 997 int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
998 for (int i = 0; i < 10; i++) { 998 for (int i = 0; i < 10; i++) {
999 cxListAdd(list, &data[i]); 999 cxListAdd(list, &data[i]);
1000 } 1000 }
1001 test_hl_linked_list_iterator_impl(list); 1001 test_hl_linked_list_iterator_impl(list);
1002 } 1002 }
1003 1003
1004 void test_hl_linked_list_insert_via_iterator(void) { 1004 void test_hl_linked_list_insert_via_iterator(void) {
1005 cxTestingAllocatorReset(); 1005 cxTestingAllocatorReset();
1006 CxList list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 1006 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
1007 for (int i = 0; i < 5; i++) { 1007 for (int i = 0; i < 5; i++) {
1008 cxListAdd(list, &i); 1008 cxListAdd(list, &i);
1009 } 1009 }
1010 CxIterator iter = cxListIterator(list, 2); 1010 CxIterator iter = cxListIterator(list, 2);
1011 CU_ASSERT_EQUAL(iter.index, 2) 1011 CU_ASSERT_EQUAL(iter.index, 2)
1035 cxListInsertAfter(&iter, &data); 1035 cxListInsertAfter(&iter, &data);
1036 CU_ASSERT_EQUAL(iter.index, 10) 1036 CU_ASSERT_EQUAL(iter.index, 10)
1037 CU_ASSERT_FALSE(cxIteratorValid(&iter)) 1037 CU_ASSERT_FALSE(cxIteratorValid(&iter))
1038 1038
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 cxLinkedListDestroy(list);
1045 cxLinkedListDestroy(expected); 1045 cxLinkedListDestroy(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};
1051 cxTestingAllocatorReset(); 1051 cxTestingAllocatorReset();
1052 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1052 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
1053 int i; 1053 int i;
1054 for (i = 0; i < 5; i++) { 1054 for (i = 0; i < 5; i++) {
1055 cxListAdd(list, &testdata[i]); 1055 cxListAdd(list, &testdata[i]);
1056 } 1056 }
1057 CxIterator iter = cxListIterator(list, 2); 1057 CxIterator iter = cxListIterator(list, 2);

mercurial