tests/test_list.cpp

changeset 655
7340c4255f1f
parent 654
c9d008861178
child 662
d0d95740071b
equal deleted inserted replaced
654:c9d008861178 655:7340c4255f1f
733 result = cxListSwap(list, 16, 6); 733 result = cxListSwap(list, 16, 6);
734 EXPECT_NE(0, result); 734 EXPECT_NE(0, result);
735 result = cxListSwap(list, 16, 17); 735 result = cxListSwap(list, 16, 17);
736 EXPECT_NE(0, result); 736 EXPECT_NE(0, result);
737 737
738 auto iter = cxListBegin(list); 738 auto iter = cxListIterator(list);
739 cx_foreach(int*, e, iter) { 739 cx_foreach(int*, e, iter) {
740 EXPECT_EQ(*e, swapped[iter.index]); 740 EXPECT_EQ(*e, swapped[iter.index]);
741 } 741 }
742 // TODO: replace with backward iterator 742 iter = cxListBackwardsIterator(list);
743 cx_for_n(i, 16) { 743 cx_foreach(int*, e, iter) {
744 EXPECT_EQ(*((int *) cxListAt(list, i)), swapped[i]); 744 EXPECT_EQ(*e, swapped[iter.index]);
745 } 745 }
746 } 746 }
747 747
748 void verifyAt(CxList *list) const { 748 void verifyAt(CxList *list) const {
749 auto len = testdata_len; 749 auto len = testdata_len;
775 cxListSort(list); 775 cxListSort(list);
776 cx_for_n (i, testdata_len) ASSERT_EQ(*(int *) cxListAt(list, i), expected[i]); 776 cx_for_n (i, testdata_len) ASSERT_EQ(*(int *) cxListAt(list, i), expected[i]);
777 } 777 }
778 778
779 void verifyIterator(CxList *list) const { 779 void verifyIterator(CxList *list) const {
780 int i = 0; 780 auto iter = cxListIterator(list);
781 auto iter = cxListBeginMut(list); 781 size_t i = 0;
782 cx_foreach(int*, x, iter) { 782 cx_foreach(int*, x, iter) {
783 ASSERT_EQ(iter.index, (size_t) (i + 1) / 2); 783 ASSERT_EQ(i, iter.index);
784 EXPECT_EQ(*x, testdata.data[iter.index]);
785 i++;
786 }
787 ASSERT_EQ(i, list->size);
788 iter = cxListBackwardsIterator(list);
789 cx_foreach(int*, x, iter) {
790 ASSERT_EQ(i - 1, iter.index);
791 EXPECT_EQ(*x, testdata.data[iter.index]);
792 i--;
793 }
794 ASSERT_EQ(i, 0);
795 auto len = testdata_len;
796 i = len / 2;
797 auto mut_iter = cxListMutIteratorAt(list, i);
798 size_t j = 0;
799 cx_foreach(int*, x, mut_iter) {
800 ASSERT_EQ(mut_iter.index, len / 2 + j / 2);
784 ASSERT_EQ(*x, testdata.data[i]); 801 ASSERT_EQ(*x, testdata.data[i]);
785 if (i % 2 == 1) cxIteratorFlagRemoval(iter); 802 if (i % 2 == 1) cxIteratorFlagRemoval(mut_iter);
786 i++; 803 i++;
787 } 804 j++;
788 auto len = testdata_len; 805 }
789 EXPECT_EQ(i, len); 806 ASSERT_EQ(i, len);
807 i = len / 2;
808 j = 0;
809 mut_iter = cxListMutBackwardsIteratorAt(list, i - 1);
810 cx_foreach(int*, x, mut_iter) {
811 ASSERT_EQ(mut_iter.index, len / 2 - 1 - j);
812 ASSERT_EQ(*x, testdata.data[i - 1]);
813 if (i % 2 == 0) cxIteratorFlagRemoval(mut_iter);
814 i--;
815 j++;
816 }
817 ASSERT_EQ(i, 0);
790 ASSERT_EQ(list->size, len / 2); 818 ASSERT_EQ(list->size, len / 2);
791 cx_for_n(j, len / 2) ASSERT_EQ(*(int *) cxListAt(list, j), testdata.data[j * 2]); 819 cx_for_n(j, len / 2) ASSERT_EQ(*(int *) cxListAt(list, j), testdata.data[j * 2]);
792 } 820 }
793 821
794 static void verifyInsertViaIterator(CxList *list) { 822 static void verifyInsertViaIterator(CxList *list) {
795 int newdata[] = {10, 20, 30, 40, 50}; 823 int newdata[] = {10, 20, 30, 40, 50};
796 824
797 auto iter = cxListMutIterator(list, 2); 825 auto iter = cxListMutIteratorAt(list, 2);
798 EXPECT_TRUE(cxIteratorValid(iter)); 826 EXPECT_TRUE(cxIteratorValid(iter));
799 EXPECT_EQ(iter.index, 2); 827 EXPECT_EQ(iter.index, 2);
800 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2); 828 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2);
801 cxListInsertAfter(&iter, &newdata[0]); 829 cxListInsertAfter(&iter, &newdata[0]);
802 EXPECT_TRUE(cxIteratorValid(iter)); 830 EXPECT_TRUE(cxIteratorValid(iter));
805 cxListInsertBefore(&iter, &newdata[1]); 833 cxListInsertBefore(&iter, &newdata[1]);
806 EXPECT_TRUE(cxIteratorValid(iter)); 834 EXPECT_TRUE(cxIteratorValid(iter));
807 EXPECT_EQ(iter.index, 3); 835 EXPECT_EQ(iter.index, 3);
808 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2); 836 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2);
809 837
810 iter = cxListBeginMut(list); 838 iter = cxListMutIterator(list);
811 cxListInsertBefore(&iter, &newdata[2]); 839 cxListInsertBefore(&iter, &newdata[2]);
812 EXPECT_TRUE(cxIteratorValid(iter)); 840 EXPECT_TRUE(cxIteratorValid(iter));
813 EXPECT_EQ(iter.index, 1); 841 EXPECT_EQ(iter.index, 1);
814 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 0); 842 EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 0);
815 iter = cxListMutIterator(list, list->size); 843 iter = cxListMutIteratorAt(list, list->size);
816 cxListInsertBefore(&iter, &newdata[3]); 844 cxListInsertBefore(&iter, &newdata[3]);
817 EXPECT_FALSE(cxIteratorValid(iter)); 845 EXPECT_FALSE(cxIteratorValid(iter));
818 EXPECT_EQ(iter.index, 9); 846 EXPECT_EQ(iter.index, 9);
819 iter = cxListMutIterator(list, list->size); 847 iter = cxListMutIteratorAt(list, list->size);
820 cxListInsertAfter(&iter, &newdata[4]); 848 cxListInsertAfter(&iter, &newdata[4]);
821 EXPECT_FALSE(cxIteratorValid(iter)); 849 EXPECT_FALSE(cxIteratorValid(iter));
822 EXPECT_EQ(iter.index, 10); 850 EXPECT_EQ(iter.index, 10);
823 851
824 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; 852 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};

mercurial