test/test_list.cpp

changeset 647
2e6e9d9f2159
parent 641
d402fead3386
equal deleted inserted replaced
646:dfd0403ff8b6 647:2e6e9d9f2159
695 EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[3]); 695 EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[3]);
696 696
697 EXPECT_NE(cxListRemove(list, testdata_len), 0); 697 EXPECT_NE(cxListRemove(list, testdata_len), 0);
698 } 698 }
699 699
700 static void verifySwap(CxList *list) {
701 ASSERT_EQ(list->size, 0);
702
703 int original[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
704 int swapped[16] = {8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13};
705
706 // we have to add the items one by one, because it could be a pointer list
707 cx_for_n(i, 16) {
708 cxListAdd(list, &original[i]);
709 }
710
711 int result;
712
713 // execute the test two times with different item sizes
714 result = cxListSwap(list, 1, 4);
715 EXPECT_EQ(0, result);
716 result = cxListSwap(list, 2, 14);
717 EXPECT_EQ(0, result);
718 result = cxListSwap(list, 9, 6);
719 EXPECT_EQ(0, result);
720 result = cxListSwap(list, 3, 3);
721 EXPECT_EQ(0, result);
722 result = cxListSwap(list, 10, 11);
723 EXPECT_EQ(0, result);
724 result = cxListSwap(list, 8, 0);
725 EXPECT_EQ(0, result);
726 result = cxListSwap(list, 7, 12);
727 EXPECT_EQ(0, result);
728 result = cxListSwap(list, 13, 15);
729 EXPECT_EQ(0, result);
730
731 result = cxListSwap(list, 5, 16);
732 EXPECT_NE(0, result);
733 result = cxListSwap(list, 16, 6);
734 EXPECT_NE(0, result);
735 result = cxListSwap(list, 16, 17);
736 EXPECT_NE(0, result);
737
738 auto iter = cxListBegin(list);
739 cx_foreach(int*, e, iter) {
740 EXPECT_EQ(*e, swapped[iter.index]);
741 }
742 // TODO: replace with backward iterator
743 cx_for_n(i, 16) {
744 EXPECT_EQ(*((int *) cxListAt(list, i)), swapped[i]);
745 }
746 }
747
700 void verifyAt(CxList *list) const { 748 void verifyAt(CxList *list) const {
701 auto len = testdata_len; 749 auto len = testdata_len;
702 EXPECT_EQ(list->size, len); 750 EXPECT_EQ(list->size, len);
703 cx_for_n (i, len) { 751 cx_for_n (i, len) {
704 EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]); 752 EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
901 949
902 TEST_F(ArrayList, cxListRemove) { 950 TEST_F(ArrayList, cxListRemove) {
903 verifyRemove(arrayListFromTestData()); 951 verifyRemove(arrayListFromTestData());
904 } 952 }
905 953
954 TEST_F(LinkedList, cxListSwap) {
955 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
956 }
957
958 TEST_F(PointerLinkedList, cxListSwap) {
959 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
960 cxListStorePointers(list);
961 verifySwap(list);
962 }
963
964 TEST_F(ArrayList, cxListSwap) {
965 verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 16)));
966 }
967
968 TEST_F(LinkedList, cxListSwapNoSBO) {
969 CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
970 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
971 CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
972 }
973
974 TEST_F(PointerLinkedList, cxListSwapNoSBO) {
975 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
976 cxListStorePointers(list);
977 CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
978 verifySwap(list);
979 CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
980 }
981
982 TEST_F(ArrayList, cxListSwapNoSBO) {
983 CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
984 verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 16)));
985 CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
986 }
987
906 TEST_F(LinkedList, cxListAt) { 988 TEST_F(LinkedList, cxListAt) {
907 verifyAt(linkedListFromTestData()); 989 verifyAt(linkedListFromTestData());
908 } 990 }
909 991
910 TEST_F(PointerLinkedList, cxListAt) { 992 TEST_F(PointerLinkedList, cxListAt) {

mercurial