test/test_list.cpp

changeset 641
d402fead3386
parent 639
309e8b08c60e
child 647
2e6e9d9f2159
equal deleted inserted replaced
640:55cc3b373c5e 641:d402fead3386
571 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 571 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
572 cxListAddArray(list, testdata.data.data(), testdata_len); 572 cxListAddArray(list, testdata.data.data(), testdata_len);
573 return list; 573 return list;
574 } 574 }
575 575
576 auto pointerLinkedListFromTestData() const -> CxList * {
577 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
578 cxListStorePointers(list);
579 // note: cannot use cxListAddArray() because we don't have a list of pointers
580 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
581 return list;
582 }
583
576 auto arrayListFromTestData() const -> CxList * { 584 auto arrayListFromTestData() const -> CxList * {
577 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), testdata_len)); 585 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), testdata_len));
578 cxListAddArray(list, testdata.data.data(), testdata_len); 586 cxListAddArray(list, testdata.data.data(), testdata_len);
579 return list; 587 return list;
580 } 588 }
623 EXPECT_EQ(*(int *) cxListAt(list, 1), 13); 631 EXPECT_EQ(*(int *) cxListAt(list, 1), 13);
624 EXPECT_EQ(*(int *) cxListAt(list, 2), 5); 632 EXPECT_EQ(*(int *) cxListAt(list, 2), 5);
625 EXPECT_EQ(*(int *) cxListAt(list, 3), 42); 633 EXPECT_EQ(*(int *) cxListAt(list, 3), 42);
626 } 634 }
627 635
628 static void verifyInsertArray(CxList *list) { 636 static void verifyInsertArray(
637 CxList *list,
638 bool pointers = false
639 ) {
629 int a[5] = {5, 47, 11, 13, 42}; 640 int a[5] = {5, 47, 11, 13, 42};
630 int b[5] = {9, 18, 72, 50, 7}; 641 int b[5] = {9, 18, 72, 50, 7};
642 int *aptr[5];
643 int *bptr[5];
644 cx_for_n(i, 5) {
645 aptr[i] = &a[i];
646 bptr[i] = &b[i];
647 }
631 648
632 size_t inserted; 649 size_t inserted;
633 650
634 inserted = cxListInsertArray(list, 0, a, 5); 651 if (pointers) {
652 inserted = cxListInsertArray(list, 0, aptr, 5);
653 } else {
654 inserted = cxListInsertArray(list, 0, a, 5);
655 }
635 EXPECT_EQ(inserted, 5); 656 EXPECT_EQ(inserted, 5);
636 EXPECT_EQ(*(int *) cxListAt(list, 0), 5); 657 EXPECT_EQ(*(int *) cxListAt(list, 0), 5);
637 EXPECT_EQ(*(int *) cxListAt(list, 1), 47); 658 EXPECT_EQ(*(int *) cxListAt(list, 1), 47);
638 EXPECT_EQ(*(int *) cxListAt(list, 2), 11); 659 EXPECT_EQ(*(int *) cxListAt(list, 2), 11);
639 EXPECT_EQ(*(int *) cxListAt(list, 3), 13); 660 EXPECT_EQ(*(int *) cxListAt(list, 3), 13);
640 EXPECT_EQ(*(int *) cxListAt(list, 4), 42); 661 EXPECT_EQ(*(int *) cxListAt(list, 4), 42);
641 662 if (pointers) {
642 inserted = cxListInsertArray(list, 3, b, 5); 663 inserted = cxListInsertArray(list, 3, bptr, 5);
664 } else {
665 inserted = cxListInsertArray(list, 3, b, 5);
666 }
643 EXPECT_EQ(inserted, 5); 667 EXPECT_EQ(inserted, 5);
644 EXPECT_EQ(*(int *) cxListAt(list, 0), 5); 668 EXPECT_EQ(*(int *) cxListAt(list, 0), 5);
645 EXPECT_EQ(*(int *) cxListAt(list, 1), 47); 669 EXPECT_EQ(*(int *) cxListAt(list, 1), 47);
646 EXPECT_EQ(*(int *) cxListAt(list, 2), 11); 670 EXPECT_EQ(*(int *) cxListAt(list, 2), 11);
647 EXPECT_EQ(*(int *) cxListAt(list, 3), 9); 671 EXPECT_EQ(*(int *) cxListAt(list, 3), 9);
785 }; 809 };
786 810
787 class LinkedList : public HighLevelTest { 811 class LinkedList : public HighLevelTest {
788 }; 812 };
789 813
814 class PointerLinkedList : public HighLevelTest {
815 };
816
790 class ArrayList : public HighLevelTest { 817 class ArrayList : public HighLevelTest {
791 }; 818 };
819
820 TEST_F(PointerLinkedList, cxListStorePointers) {
821 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, 47));
822 EXPECT_FALSE(cxListIsStoringPointers(list));
823 cxListStorePointers(list);
824 EXPECT_EQ(list->itemsize, sizeof(void *));
825 EXPECT_NE(list->cl, nullptr);
826 EXPECT_NE(list->climpl, nullptr);
827 EXPECT_TRUE(cxListIsStoringPointers(list));
828 cxListStoreObjects(list);
829 EXPECT_NE(list->cl, nullptr);
830 EXPECT_EQ(list->climpl, nullptr);
831 EXPECT_FALSE(cxListIsStoringPointers(list));
832 }
792 833
793 TEST_F(LinkedList, cxLinkedListCreate) { 834 TEST_F(LinkedList, cxLinkedListCreate) {
794 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 835 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
795 ASSERT_NE(list, nullptr); 836 ASSERT_NE(list, nullptr);
796 EXPECT_EQ(list->itemsize, sizeof(int)); 837 EXPECT_EQ(list->itemsize, sizeof(int));
805 EXPECT_EQ(list->capacity, 8); 846 EXPECT_EQ(list->capacity, 8);
806 verifyCreate(list); 847 verifyCreate(list);
807 } 848 }
808 849
809 TEST_F(LinkedList, cxListAdd) { 850 TEST_F(LinkedList, cxListAdd) {
810 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 851 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
811 verifyAdd(list, false); 852 verifyAdd(list, false);
812 } 853 }
813 854
855 TEST_F(PointerLinkedList, cxListAdd) {
856 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
857 cxListStorePointers(list);
858 verifyAdd(list, true);
859 }
860
814 TEST_F(ArrayList, cxListAdd) { 861 TEST_F(ArrayList, cxListAdd) {
815 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8)); 862 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
816 verifyAdd(list, false); 863 verifyAdd(list, false);
817 } 864 }
818 865
819 TEST_F(LinkedList, cxListInsert) { 866 TEST_F(LinkedList, cxListInsert) {
820 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); 867 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
821 } 868 }
822 869
870 TEST_F(PointerLinkedList, cxListInsert) {
871 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
872 cxListStorePointers(list);
873 verifyInsert(list);
874 }
875
823 TEST_F(ArrayList, cxListInsert) { 876 TEST_F(ArrayList, cxListInsert) {
824 verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2))); 877 verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2)));
825 } 878 }
826 879
827 TEST_F(LinkedList, cxListInsertArray) { 880 TEST_F(LinkedList, cxListInsertArray) {
828 verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); 881 verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
829 } 882 }
830 883
884 TEST_F(PointerLinkedList, cxListInsertArray) {
885 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
886 cxListStorePointers(list);
887 verifyInsertArray(list, true);
888 }
889
831 TEST_F(ArrayList, cxListInsertArray) { 890 TEST_F(ArrayList, cxListInsertArray) {
832 verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4))); 891 verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)));
833 } 892 }
834 893
835 TEST_F(LinkedList, cxListRemove) { 894 TEST_F(LinkedList, cxListRemove) {
836 verifyRemove(linkedListFromTestData()); 895 verifyRemove(linkedListFromTestData());
837 } 896 }
838 897
898 TEST_F(PointerLinkedList, cxListRemove) {
899 verifyRemove(pointerLinkedListFromTestData());
900 }
901
839 TEST_F(ArrayList, cxListRemove) { 902 TEST_F(ArrayList, cxListRemove) {
840 verifyRemove(arrayListFromTestData()); 903 verifyRemove(arrayListFromTestData());
841 } 904 }
842 905
843 TEST_F(LinkedList, cxListAt) { 906 TEST_F(LinkedList, cxListAt) {
844 verifyAt(linkedListFromTestData()); 907 verifyAt(linkedListFromTestData());
845 } 908 }
846 909
910 TEST_F(PointerLinkedList, cxListAt) {
911 verifyAt(pointerLinkedListFromTestData());
912 }
913
847 TEST_F(ArrayList, cxListAt) { 914 TEST_F(ArrayList, cxListAt) {
848 verifyAt(arrayListFromTestData()); 915 verifyAt(arrayListFromTestData());
849 } 916 }
850 917
851 TEST_F(LinkedList, cxListFind) { 918 TEST_F(LinkedList, cxListFind) {
852 verifyFind(linkedListFromTestData()); 919 verifyFind(linkedListFromTestData());
853 } 920 }
854 921
922 TEST_F(PointerLinkedList, cxListFind) {
923 verifyFind(pointerLinkedListFromTestData());
924 }
925
855 TEST_F(ArrayList, cxListFind) { 926 TEST_F(ArrayList, cxListFind) {
856 verifyFind(arrayListFromTestData()); 927 verifyFind(arrayListFromTestData());
857 } 928 }
858 929
859 TEST_F(LinkedList, cxListSort) { 930 TEST_F(LinkedList, cxListSort) {
860 verifySort(linkedListFromTestData()); 931 verifySort(linkedListFromTestData());
861 } 932 }
862 933
934 TEST_F(PointerLinkedList, cxListSort) {
935 verifySort(pointerLinkedListFromTestData());
936 }
937
863 TEST_F(ArrayList, cxListSort) { 938 TEST_F(ArrayList, cxListSort) {
864 verifySort(arrayListFromTestData()); 939 verifySort(arrayListFromTestData());
865 } 940 }
866 941
867 TEST_F(LinkedList, Iterator) { 942 TEST_F(LinkedList, Iterator) {
868 verifyIterator(linkedListFromTestData()); 943 verifyIterator(linkedListFromTestData());
944 }
945
946 TEST_F(PointerLinkedList, Iterator) {
947 verifyIterator(pointerLinkedListFromTestData());
869 } 948 }
870 949
871 TEST_F(ArrayList, Iterator) { 950 TEST_F(ArrayList, Iterator) {
872 verifyIterator(arrayListFromTestData()); 951 verifyIterator(arrayListFromTestData());
873 } 952 }
877 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 956 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
878 cxListAddArray(list, fivenums, 5); 957 cxListAddArray(list, fivenums, 5);
879 verifyInsertViaIterator(list); 958 verifyInsertViaIterator(list);
880 } 959 }
881 960
961 TEST_F(PointerLinkedList, InsertViaIterator) {
962 int fivenums[] = {0, 1, 2, 3, 4, 5};
963 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
964 cxListStorePointers(list);
965 // note: cannot use cxListAddArray() because we don't have a list of pointers
966 cx_for_n(i, 5) cxListAdd(list, &fivenums[i]);
967 verifyInsertViaIterator(list);
968 }
969
882 TEST_F(ArrayList, InsertViaIterator) { 970 TEST_F(ArrayList, InsertViaIterator) {
883 int fivenums[] = {0, 1, 2, 3, 4, 5}; 971 int fivenums[] = {0, 1, 2, 3, 4, 5};
884 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)); 972 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4));
885 cxListAddArray(list, fivenums, 5); 973 cxListAddArray(list, fivenums, 5);
886 verifyInsertViaIterator(list); 974 verifyInsertViaIterator(list);
888 976
889 TEST_F(LinkedList, cxListReverse) { 977 TEST_F(LinkedList, cxListReverse) {
890 verifyReverse(linkedListFromTestData()); 978 verifyReverse(linkedListFromTestData());
891 } 979 }
892 980
981 TEST_F(PointerLinkedList, cxListReverse) {
982 verifyReverse(pointerLinkedListFromTestData());
983 }
984
893 TEST_F(ArrayList, cxListReverse) { 985 TEST_F(ArrayList, cxListReverse) {
894 verifyReverse(arrayListFromTestData()); 986 verifyReverse(arrayListFromTestData());
895 } 987 }
896 988
897 TEST_F(LinkedList, cxListCompare) { 989 TEST_F(LinkedList, cxListCompare) {
898 auto left = linkedListFromTestData(); 990 auto left = linkedListFromTestData();
899 auto right = linkedListFromTestData(); 991 auto right = linkedListFromTestData();
900 verifyCompare(left, right); 992 verifyCompare(left, right);
901 } 993 }
902 994
995 TEST_F(LinkedList, cxListCompareWithPtrList) {
996 auto left = linkedListFromTestData();
997 auto right = pointerLinkedListFromTestData();
998 verifyCompare(left, right);
999 }
1000
903 TEST_F(LinkedList, cxListCompareWithArrayList) { 1001 TEST_F(LinkedList, cxListCompareWithArrayList) {
904 auto left = linkedListFromTestData(); 1002 auto left = linkedListFromTestData();
905 auto right = arrayListFromTestData(); 1003 auto right = arrayListFromTestData();
906 verifyCompare(left, right); 1004 verifyCompare(left, right);
907 } 1005 }
908 1006
1007 TEST_F(PointerLinkedList, cxListCompare) {
1008 auto left = pointerLinkedListFromTestData();
1009 auto right = pointerLinkedListFromTestData();
1010 verifyCompare(left, right);
1011 }
1012
1013 TEST_F(PointerLinkedList, cxListCompareWithNormalList) {
1014 auto left = pointerLinkedListFromTestData();
1015 auto right = linkedListFromTestData();
1016 verifyCompare(left, right);
1017 }
1018
1019 TEST_F(PointerLinkedList, cxListCompareWithArrayList) {
1020 auto left = pointerLinkedListFromTestData();
1021 auto right = arrayListFromTestData();
1022 verifyCompare(left, right);
1023 }
1024
909 TEST_F(ArrayList, cxListCompare) { 1025 TEST_F(ArrayList, cxListCompare) {
910 auto left = arrayListFromTestData(); 1026 auto left = arrayListFromTestData();
911 auto right = arrayListFromTestData(); 1027 auto right = arrayListFromTestData();
912 verifyCompare(left, right); 1028 verifyCompare(left, right);
913 } 1029 }
914 1030
915 TEST_F(ArrayList, cxListCompareWithLinkedList) { 1031 TEST_F(ArrayList, cxListCompareWithPtrList) {
1032 auto left = arrayListFromTestData();
1033 auto right = pointerLinkedListFromTestData();
1034 verifyCompare(left, right);
1035 }
1036
1037 TEST_F(ArrayList, cxListCompareWithNormalList) {
916 auto left = arrayListFromTestData(); 1038 auto left = arrayListFromTestData();
917 auto right = linkedListFromTestData(); 1039 auto right = linkedListFromTestData();
918 verifyCompare(left, right); 1040 verifyCompare(left, right);
919 } 1041 }
1042
1043 TEST_F(PointerLinkedList, NoDestructor) {
1044 void *item = cxMalloc(&testingAllocator, sizeof(int));
1045 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *));
1046 cxListStorePointers(list);
1047 cxListAdd(list, item);
1048 ASSERT_FALSE(testingAllocator.verify());
1049 cxListDestroy(list);
1050 EXPECT_FALSE(testingAllocator.verify());
1051 cxFree(&testingAllocator, item);
1052 EXPECT_TRUE(testingAllocator.verify());
1053 }
1054
1055 TEST_F(PointerLinkedList, SimpleDestructor) {
1056 int item = 0;
1057 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *));
1058 cxListStorePointers(list);
1059 list->content_destructor_type = CX_DESTRUCTOR_SIMPLE;
1060 list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
1061 cxListAdd(list, &item);
1062 cxListDestroy(list);
1063 EXPECT_EQ(item, 42);
1064 }
1065
1066 TEST_F(PointerLinkedList, AdvancedDestructor) {
1067 void *item = cxMalloc(&testingAllocator, sizeof(int));
1068 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *));
1069 cxListStorePointers(list);
1070 list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;
1071 list->advanced_destructor.data = &testingAllocator;
1072 list->advanced_destructor.func = (cx_destructor_func2) cxFree;
1073 cxListAdd(list, item);
1074 ASSERT_FALSE(testingAllocator.verify());
1075 cxListDestroy(list);
1076 EXPECT_TRUE(testingAllocator.verify());
1077 }

mercurial