test/test_list.cpp

changeset 606
314e9288af2f
parent 602
3b071ea0e9cf
child 610
de5d3ee6435f
equal deleted inserted replaced
605:be5a4902d405 606:314e9288af2f
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/array_list.h"
30 #include "cx/utils.h" 31 #include "cx/utils.h"
31 #include "cx/compare.h" 32 #include "cx/compare.h"
32 #include "util_allocator.h" 33 #include "util_allocator.h"
33 34
34 #include <gtest/gtest.h> 35 #include <gtest/gtest.h>
561 562
562 static constexpr size_t testdata_len = 250; 563 static constexpr size_t testdata_len = 250;
563 int_test_data<testdata_len> testdata; 564 int_test_data<testdata_len> testdata;
564 565
565 auto autofree(CxList *list) const -> CxList * { 566 auto autofree(CxList *list) const -> CxList * {
566 lists.insert(list); 567 if (list != nullptr) lists.insert(list);
567 return list; 568 return list;
568 } 569 }
569 570
570 auto linkedListFromTestData() const -> CxList * { 571 auto linkedListFromTestData() const -> CxList * {
572 // TODO: replace with cxListAddArray
571 return autofree( 573 return autofree(
572 cxLinkedListFromArray( 574 cxLinkedListFromArray(
573 &testingAllocator, 575 &testingAllocator,
574 cx_cmp_int, 576 cx_cmp_int,
575 sizeof(int), 577 sizeof(int),
583 auto list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)); 585 auto list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
584 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]); 586 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
585 return list; 587 return list;
586 } 588 }
587 589
590 auto arrayListFromTestData() const -> CxList * {
591 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), testdata_len));
592 // TODO: replace with cxListAddArray
593 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
594 return list;
595 }
596
588 void verifyCreate(CxList *list) const { 597 void verifyCreate(CxList *list) const {
589 EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE); 598 EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE);
590 EXPECT_EQ(list->size, 0); 599 EXPECT_EQ(list->size, 0);
591 EXPECT_EQ(list->capacity, (size_t) -1);
592 EXPECT_EQ(list->allocator, &testingAllocator); 600 EXPECT_EQ(list->allocator, &testingAllocator);
593 EXPECT_EQ(list->cmpfunc, cx_cmp_int); 601 EXPECT_EQ(list->cmpfunc, cx_cmp_int);
594 } 602 }
595 603
596 void verifyAdd( 604 void verifyAdd(
597 CxList *list, 605 CxList *list,
598 bool write_through 606 bool as_pointer
599 ) { 607 ) {
600 auto len = testdata_len; 608 auto len = testdata_len;
601 cx_for_n (i, len) EXPECT_EQ(cxListAdd(list, &testdata.data[i]), 0); 609 cx_for_n (i, len) EXPECT_EQ(cxListAdd(list, &testdata.data[i]), 0);
602 EXPECT_EQ(list->size, len); 610 EXPECT_EQ(list->size, len);
603 EXPECT_GE(list->capacity, list->size); 611 EXPECT_GE(list->capacity, list->size);
604 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]); 612 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
605 cx_for_n (i, len) ++testdata.data[i]; 613 cx_for_n (i, len) ++testdata.data[i];
606 if (write_through) { 614 if (as_pointer) {
607 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]); 615 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
608 } else { 616 } else {
609 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i] - 1); 617 cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i] - 1);
610 } 618 }
611 } 619 }
767 }; 775 };
768 776
769 class PointerLinkedList : public HighLevelTest { 777 class PointerLinkedList : public HighLevelTest {
770 }; 778 };
771 779
780 class ArrayList : public HighLevelTest {
781 };
782
772 TEST_F(LinkedList, cxLinkedListCreate) { 783 TEST_F(LinkedList, cxLinkedListCreate) {
773 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 784 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
785 ASSERT_NE(list, nullptr);
774 EXPECT_EQ(list->itemsize, sizeof(int)); 786 EXPECT_EQ(list->itemsize, sizeof(int));
787 EXPECT_EQ(list->capacity, (size_t) -1);
775 verifyCreate(list); 788 verifyCreate(list);
776 } 789 }
777 790
778 TEST_F(PointerLinkedList, cxPointerLinkedListCreate) { 791 TEST_F(PointerLinkedList, cxPointerLinkedListCreate) {
779 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)); 792 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
793 ASSERT_NE(list, nullptr);
780 EXPECT_EQ(list->itemsize, sizeof(void *)); 794 EXPECT_EQ(list->itemsize, sizeof(void *));
795 EXPECT_EQ(list->capacity, (size_t) -1);
781 verifyCreate(list); 796 verifyCreate(list);
782 } 797 }
783 798
784 TEST_F(LinkedList, cxLinkedListFromArray) { 799 TEST_F(LinkedList, cxLinkedListFromArray) {
785 CxList *expected = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 800 CxList *expected = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
786 cx_for_n (i, testdata_len) cxListAdd(expected, &testdata.data[i]); 801 cx_for_n (i, testdata_len) cxListAdd(expected, &testdata.data[i]);
787 CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cx_cmp_int, sizeof(int), 802 CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cx_cmp_int, sizeof(int),
788 testdata_len, testdata.data.data())); 803 testdata_len, testdata.data.data()));
804 ASSERT_NE(list, nullptr);
805 EXPECT_EQ(list->itemsize, sizeof(int));
806 EXPECT_EQ(list->capacity, (size_t) -1);
807 EXPECT_EQ(list->size, testdata_len);
789 EXPECT_EQ(cxListCompare(list, expected), 0); 808 EXPECT_EQ(cxListCompare(list, expected), 0);
809 }
810
811 TEST_F(ArrayList, cxArrayListCreate) {
812 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
813 ASSERT_NE(list, nullptr);
814 EXPECT_EQ(list->itemsize, sizeof(int));
815 EXPECT_EQ(list->capacity, 8);
816 verifyCreate(list);
790 } 817 }
791 818
792 TEST_F(LinkedList, cxListAdd) { 819 TEST_F(LinkedList, cxListAdd) {
793 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 820 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
794 verifyAdd(list, false); 821 verifyAdd(list, false);
797 TEST_F(PointerLinkedList, cxListAdd) { 824 TEST_F(PointerLinkedList, cxListAdd) {
798 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)); 825 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
799 verifyAdd(list, true); 826 verifyAdd(list, true);
800 } 827 }
801 828
829 TEST_F(ArrayList, cxListAdd) {
830 ASSERT_EQ(1,0); // TODO: remove when implemented
831 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
832 verifyAdd(list, false);
833 }
834
802 TEST_F(LinkedList, cxListInsert) { 835 TEST_F(LinkedList, cxListInsert) {
803 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); 836 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
804 } 837 }
805 838
806 TEST_F(PointerLinkedList, cxListInsert) { 839 TEST_F(PointerLinkedList, cxListInsert) {
807 verifyInsert(autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int))); 840 verifyInsert(autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)));
808 } 841 }
809 842
843 TEST_F(ArrayList, cxListInsert) {
844 ASSERT_EQ(1,0); // TODO: remove when implemented
845 verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2)));
846 }
847
810 TEST_F(LinkedList, cxListRemove) { 848 TEST_F(LinkedList, cxListRemove) {
811 verifyRemove(linkedListFromTestData()); 849 verifyRemove(linkedListFromTestData());
812 } 850 }
813 851
814 TEST_F(PointerLinkedList, cxListRemove) { 852 TEST_F(PointerLinkedList, cxListRemove) {
815 verifyRemove(pointerLinkedListFromTestData()); 853 verifyRemove(pointerLinkedListFromTestData());
816 } 854 }
817 855
856 TEST_F(ArrayList, cxListRemove) {
857 ASSERT_EQ(1,0); // TODO: remove when implemented
858 verifyRemove(arrayListFromTestData());
859 }
860
818 TEST_F(LinkedList, cxListAt) { 861 TEST_F(LinkedList, cxListAt) {
819 verifyAt(linkedListFromTestData()); 862 verifyAt(linkedListFromTestData());
820 } 863 }
821 864
822 TEST_F(PointerLinkedList, cxListAt) { 865 TEST_F(PointerLinkedList, cxListAt) {
823 verifyAt(pointerLinkedListFromTestData()); 866 verifyAt(pointerLinkedListFromTestData());
824 } 867 }
825 868
869 TEST_F(ArrayList, cxListAt) {
870 ASSERT_EQ(1,0); // TODO: remove when implemented
871 verifyAt(arrayListFromTestData());
872 }
873
826 TEST_F(LinkedList, cxListFind) { 874 TEST_F(LinkedList, cxListFind) {
827 verifyFind(linkedListFromTestData()); 875 verifyFind(linkedListFromTestData());
828 } 876 }
829 877
830 TEST_F(PointerLinkedList, cxListFind) { 878 TEST_F(PointerLinkedList, cxListFind) {
831 verifyFind(pointerLinkedListFromTestData()); 879 verifyFind(pointerLinkedListFromTestData());
832 } 880 }
833 881
882 TEST_F(ArrayList, cxListFind) {
883 ASSERT_EQ(1,0); // TODO: remove when implemented
884 verifyFind(arrayListFromTestData());
885 }
886
834 TEST_F(LinkedList, cxListSort) { 887 TEST_F(LinkedList, cxListSort) {
835 verifySort(linkedListFromTestData()); 888 verifySort(linkedListFromTestData());
836 } 889 }
837 890
838 TEST_F(PointerLinkedList, cxListSort) { 891 TEST_F(PointerLinkedList, cxListSort) {
839 verifySort(pointerLinkedListFromTestData()); 892 verifySort(pointerLinkedListFromTestData());
840 } 893 }
841 894
895 TEST_F(ArrayList, cxListSort) {
896 ASSERT_EQ(1,0); // TODO: remove when implemented
897 verifySort(arrayListFromTestData());
898 }
899
842 TEST_F(LinkedList, Iterator) { 900 TEST_F(LinkedList, Iterator) {
843 verifyIterator(linkedListFromTestData()); 901 verifyIterator(linkedListFromTestData());
844 } 902 }
845 903
846 TEST_F(PointerLinkedList, Iterator) { 904 TEST_F(PointerLinkedList, Iterator) {
847 verifyIterator(pointerLinkedListFromTestData()); 905 verifyIterator(pointerLinkedListFromTestData());
906 }
907
908 TEST_F(ArrayList, Iterator) {
909 ASSERT_EQ(1,0); // TODO: remove when implemented
910 verifyIterator(arrayListFromTestData());
848 } 911 }
849 912
850 TEST_F(LinkedList, InsertViaIterator) { 913 TEST_F(LinkedList, InsertViaIterator) {
851 int fivenums[] = {0, 1, 2, 3, 4, 5}; 914 int fivenums[] = {0, 1, 2, 3, 4, 5};
852 CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cx_cmp_int, sizeof(int), 5, fivenums)); 915 CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cx_cmp_int, sizeof(int), 5, fivenums));
858 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)); 921 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
859 cx_for_n (i, 5) cxListAdd(list, &fivenums[i]); 922 cx_for_n (i, 5) cxListAdd(list, &fivenums[i]);
860 verifyInsertViaIterator(list); 923 verifyInsertViaIterator(list);
861 } 924 }
862 925
926 TEST_F(ArrayList, InsertViaIterator) {
927 ASSERT_EQ(1,0); // TODO: remove when implemented
928 int fivenums[] = {0, 1, 2, 3, 4, 5};
929 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4));
930 // TODO: replace with cxListAddArray
931 cx_for_n (i, 5) cxListAdd(list, &fivenums[i]);
932 verifyInsertViaIterator(list);
933 }
934
863 TEST_F(LinkedList, cxListReverse) { 935 TEST_F(LinkedList, cxListReverse) {
864 verifyReverse(linkedListFromTestData()); 936 verifyReverse(linkedListFromTestData());
865 } 937 }
866 938
867 TEST_F(PointerLinkedList, cxListReverse) { 939 TEST_F(PointerLinkedList, cxListReverse) {
868 verifyReverse(pointerLinkedListFromTestData()); 940 verifyReverse(pointerLinkedListFromTestData());
941 }
942
943 TEST_F(ArrayList, cxListReverse) {
944 ASSERT_EQ(1,0); // TODO: remove when implemented
945 verifyReverse(arrayListFromTestData());
869 } 946 }
870 947
871 TEST_F(LinkedList, cxListCompare) { 948 TEST_F(LinkedList, cxListCompare) {
872 auto left = linkedListFromTestData(); 949 auto left = linkedListFromTestData();
873 auto right = linkedListFromTestData(); 950 auto right = linkedListFromTestData();
878 auto left = linkedListFromTestData(); 955 auto left = linkedListFromTestData();
879 auto right = pointerLinkedListFromTestData(); 956 auto right = pointerLinkedListFromTestData();
880 verifyCompare(left, right); 957 verifyCompare(left, right);
881 } 958 }
882 959
960 TEST_F(LinkedList, cxListCompareWithArrayList) {
961 ASSERT_EQ(1,0); // TODO: remove when implemented
962 auto left = linkedListFromTestData();
963 auto right = arrayListFromTestData();
964 verifyCompare(left, right);
965 }
966
883 TEST_F(PointerLinkedList, cxListCompare) { 967 TEST_F(PointerLinkedList, cxListCompare) {
884 auto left = pointerLinkedListFromTestData(); 968 auto left = pointerLinkedListFromTestData();
885 auto right = pointerLinkedListFromTestData(); 969 auto right = pointerLinkedListFromTestData();
886 verifyCompare(left, right); 970 verifyCompare(left, right);
887 } 971 }
888 972
889 TEST_F(PointerLinkedList, cxListCompareWithNormalList) { 973 TEST_F(PointerLinkedList, cxListCompareWithNormalList) {
890 auto left = pointerLinkedListFromTestData(); 974 auto left = pointerLinkedListFromTestData();
975 auto right = linkedListFromTestData();
976 verifyCompare(left, right);
977 }
978
979 TEST_F(PointerLinkedList, cxListCompareWithArrayList) {
980 ASSERT_EQ(1,0); // TODO: remove when implemented
981 auto left = pointerLinkedListFromTestData();
982 auto right = arrayListFromTestData();
983 verifyCompare(left, right);
984 }
985
986 TEST_F(ArrayList, cxListCompare) {
987 ASSERT_EQ(1,0); // TODO: remove when implemented
988 auto left = arrayListFromTestData();
989 auto right = arrayListFromTestData();
990 verifyCompare(left, right);
991 }
992
993 TEST_F(ArrayList, cxListCompareWithPtrList) {
994 ASSERT_EQ(1,0); // TODO: remove when implemented
995 auto left = arrayListFromTestData();
996 auto right = pointerLinkedListFromTestData();
997 verifyCompare(left, right);
998 }
999
1000 TEST_F(ArrayList, cxListCompareWithNormalList) {
1001 ASSERT_EQ(1,0); // TODO: remove when implemented
1002 auto left = arrayListFromTestData();
891 auto right = linkedListFromTestData(); 1003 auto right = linkedListFromTestData();
892 verifyCompare(left, right); 1004 verifyCompare(left, right);
893 } 1005 }
894 1006
895 TEST_F(PointerLinkedList, NoDestructor) { 1007 TEST_F(PointerLinkedList, NoDestructor) {

mercurial