test/test_list.cpp

changeset 629
6c81ee4f11ad
parent 623
21082350a590
child 630
ac5e7f789048
equal deleted inserted replaced
628:1e2be40f0cb5 629:6c81ee4f11ad
567 if (list != nullptr) lists.insert(list); 567 if (list != nullptr) lists.insert(list);
568 return list; 568 return list;
569 } 569 }
570 570
571 auto linkedListFromTestData() const -> CxList * { 571 auto linkedListFromTestData() const -> CxList * {
572 // TODO: replace with cxListAddArray 572 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
573 return autofree( 573 cxListAddArray(list, testdata.data.data(), testdata_len);
574 cxLinkedListFromArray( 574 return list;
575 &testingAllocator,
576 cx_cmp_int,
577 sizeof(int),
578 testdata_len,
579 testdata.data.data()
580 )
581 );
582 } 575 }
583 576
584 auto pointerLinkedListFromTestData() const -> CxList * { 577 auto pointerLinkedListFromTestData() const -> CxList * {
585 auto list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)); 578 auto list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
579 // note: cannot use cxListAddArray() because we don't have a list of pointers
586 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]); 580 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
587 return list; 581 return list;
588 } 582 }
589 583
590 auto arrayListFromTestData() const -> CxList * { 584 auto arrayListFromTestData() const -> CxList * {
591 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));
592 // TODO: replace with cxListAddArray 586 cxListAddArray(list, testdata.data.data(), testdata_len);
593 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
594 return list; 587 return list;
595 } 588 }
596 589
597 void verifyCreate(CxList *list) const { 590 void verifyCreate(CxList *list) const {
598 EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE); 591 EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE);
794 EXPECT_EQ(list->itemsize, sizeof(void *)); 787 EXPECT_EQ(list->itemsize, sizeof(void *));
795 EXPECT_EQ(list->capacity, (size_t) -1); 788 EXPECT_EQ(list->capacity, (size_t) -1);
796 verifyCreate(list); 789 verifyCreate(list);
797 } 790 }
798 791
799 TEST_F(LinkedList, cxLinkedListFromArray) {
800 CxList *expected = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
801 cx_for_n (i, testdata_len) cxListAdd(expected, &testdata.data[i]);
802 CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cx_cmp_int, sizeof(int),
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);
808 EXPECT_EQ(cxListCompare(list, expected), 0);
809 }
810
811 TEST_F(ArrayList, cxArrayListCreate) { 792 TEST_F(ArrayList, cxArrayListCreate) {
812 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8)); 793 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
813 ASSERT_NE(list, nullptr); 794 ASSERT_NE(list, nullptr);
814 EXPECT_EQ(list->itemsize, sizeof(int)); 795 EXPECT_EQ(list->itemsize, sizeof(int));
815 EXPECT_EQ(list->capacity, 8); 796 EXPECT_EQ(list->capacity, 8);
903 verifyIterator(arrayListFromTestData()); 884 verifyIterator(arrayListFromTestData());
904 } 885 }
905 886
906 TEST_F(LinkedList, InsertViaIterator) { 887 TEST_F(LinkedList, InsertViaIterator) {
907 int fivenums[] = {0, 1, 2, 3, 4, 5}; 888 int fivenums[] = {0, 1, 2, 3, 4, 5};
908 CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cx_cmp_int, sizeof(int), 5, fivenums)); 889 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
890 cxListAddArray(list, fivenums, 5);
909 verifyInsertViaIterator(list); 891 verifyInsertViaIterator(list);
910 } 892 }
911 893
912 TEST_F(PointerLinkedList, InsertViaIterator) { 894 TEST_F(PointerLinkedList, InsertViaIterator) {
913 int fivenums[] = {0, 1, 2, 3, 4, 5}; 895 int fivenums[] = {0, 1, 2, 3, 4, 5};
914 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)); 896 CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
897 // note: don't use cxListAddArray() here, because we don't have a list of pointers
915 cx_for_n (i, 5) cxListAdd(list, &fivenums[i]); 898 cx_for_n (i, 5) cxListAdd(list, &fivenums[i]);
916 verifyInsertViaIterator(list); 899 verifyInsertViaIterator(list);
917 } 900 }
918 901
919 TEST_F(ArrayList, InsertViaIterator) { 902 TEST_F(ArrayList, InsertViaIterator) {
920 int fivenums[] = {0, 1, 2, 3, 4, 5}; 903 int fivenums[] = {0, 1, 2, 3, 4, 5};
921 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)); 904 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4));
922 // TODO: replace with cxListAddArray 905 cxListAddArray(list, fivenums, 5);
923 cx_for_n (i, 5) cxListAdd(list, &fivenums[i]);
924 verifyInsertViaIterator(list); 906 verifyInsertViaIterator(list);
925 } 907 }
926 908
927 TEST_F(LinkedList, cxListReverse) { 909 TEST_F(LinkedList, cxListReverse) {
928 verifyReverse(linkedListFromTestData()); 910 verifyReverse(linkedListFromTestData());

mercurial