tests/test_list.cpp

changeset 667
2f88a7c13a28
parent 666
b5dd654deb3b
child 672
55d8fdd38ca4
equal deleted inserted replaced
666:b5dd654deb3b 667:2f88a7c13a28
585 cxListAddArray(list, testdata.data.data(), testdata_len); 585 cxListAddArray(list, testdata.data.data(), testdata_len);
586 return list; 586 return list;
587 } 587 }
588 588
589 auto pointerLinkedListFromTestData() const -> CxList * { 589 auto pointerLinkedListFromTestData() const -> CxList * {
590 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 590 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS));
591 cxListStorePointers(list);
592 // note: cannot use cxListAddArray() because we don't have a list of pointers 591 // note: cannot use cxListAddArray() because we don't have a list of pointers
593 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]); 592 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
594 return list; 593 return list;
595 } 594 }
596 595
931 EXPECT_TRUE(cxListIsStoringPointers(list)); 930 EXPECT_TRUE(cxListIsStoringPointers(list));
932 cxListStoreObjects(list); 931 cxListStoreObjects(list);
933 EXPECT_NE(list->cl, nullptr); 932 EXPECT_NE(list->cl, nullptr);
934 EXPECT_EQ(list->climpl, nullptr); 933 EXPECT_EQ(list->climpl, nullptr);
935 EXPECT_FALSE(cxListIsStoringPointers(list)); 934 EXPECT_FALSE(cxListIsStoringPointers(list));
935
936 list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS));
937 EXPECT_EQ(list->itemsize, sizeof(void *));
938 EXPECT_NE(list->cl, nullptr);
939 EXPECT_NE(list->climpl, nullptr);
940 EXPECT_TRUE(cxListIsStoringPointers(list));
936 } 941 }
937 942
938 TEST_F(LinkedList, cxLinkedListCreate) { 943 TEST_F(LinkedList, cxLinkedListCreate) {
939 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 944 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
940 ASSERT_NE(list, nullptr); 945 ASSERT_NE(list, nullptr);
973 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))); 978 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
974 verifyAdd(list, false); 979 verifyAdd(list, false);
975 } 980 }
976 981
977 TEST_F(PointerLinkedList, cxListAdd) { 982 TEST_F(PointerLinkedList, cxListAdd) {
978 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 983 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS));
979 cxListStorePointers(list);
980 verifyAdd(list, true); 984 verifyAdd(list, true);
981 } 985 }
982 986
983 TEST_F(ArrayList, cxListAdd) { 987 TEST_F(ArrayList, cxListAdd) {
984 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8)); 988 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
988 TEST_F(LinkedList, cxListInsert) { 992 TEST_F(LinkedList, cxListInsert) {
989 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); 993 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
990 } 994 }
991 995
992 TEST_F(PointerLinkedList, cxListInsert) { 996 TEST_F(PointerLinkedList, cxListInsert) {
993 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 997 verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)));
994 cxListStorePointers(list);
995 verifyInsert(list);
996 } 998 }
997 999
998 TEST_F(ArrayList, cxListInsert) { 1000 TEST_F(ArrayList, cxListInsert) {
999 verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2))); 1001 verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2)));
1000 } 1002 }
1002 TEST_F(LinkedList, cxListInsertArray) { 1004 TEST_F(LinkedList, cxListInsertArray) {
1003 verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); 1005 verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
1004 } 1006 }
1005 1007
1006 TEST_F(PointerLinkedList, cxListInsertArray) { 1008 TEST_F(PointerLinkedList, cxListInsertArray) {
1007 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 1009 verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)), true);
1008 cxListStorePointers(list);
1009 verifyInsertArray(list, true);
1010 } 1010 }
1011 1011
1012 TEST_F(ArrayList, cxListInsertArray) { 1012 TEST_F(ArrayList, cxListInsertArray) {
1013 verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4))); 1013 verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)));
1014 } 1014 }
1028 TEST_F(LinkedList, cxListClear) { 1028 TEST_F(LinkedList, cxListClear) {
1029 verifyClear(autofree(cxLinkedListCreateSimple(sizeof(testdatastruct)))); 1029 verifyClear(autofree(cxLinkedListCreateSimple(sizeof(testdatastruct))));
1030 } 1030 }
1031 1031
1032 TEST_F(PointerLinkedList, cxListClear) { 1032 TEST_F(PointerLinkedList, cxListClear) {
1033 auto l = cxLinkedListCreateSimple(sizeof(testdatastruct)); 1033 verifyClear(cxLinkedListCreateSimple(CX_STORE_POINTERS));
1034 cxListStorePointers(l);
1035 verifyClear(autofree(l));
1036 } 1034 }
1037 1035
1038 TEST_F(ArrayList, cxListClear) { 1036 TEST_F(ArrayList, cxListClear) {
1039 verifyClear(autofree(cxArrayListCreateSimple(sizeof(testdatastruct), 8))); 1037 verifyClear(autofree(cxArrayListCreateSimple(sizeof(testdatastruct), 8)));
1040 } 1038 }
1042 TEST_F(LinkedList, cxListSwap) { 1040 TEST_F(LinkedList, cxListSwap) {
1043 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); 1041 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
1044 } 1042 }
1045 1043
1046 TEST_F(PointerLinkedList, cxListSwap) { 1044 TEST_F(PointerLinkedList, cxListSwap) {
1047 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 1045 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)));
1048 cxListStorePointers(list);
1049 verifySwap(list);
1050 } 1046 }
1051 1047
1052 TEST_F(ArrayList, cxListSwap) { 1048 TEST_F(ArrayList, cxListSwap) {
1053 verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 16))); 1049 verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 16)));
1054 } 1050 }
1058 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)))); 1054 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
1059 CX_DISABLE_LINKED_LIST_SWAP_SBO = false; 1055 CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
1060 } 1056 }
1061 1057
1062 TEST_F(PointerLinkedList, cxListSwapNoSBO) { 1058 TEST_F(PointerLinkedList, cxListSwapNoSBO) {
1063 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
1064 cxListStorePointers(list);
1065 CX_DISABLE_LINKED_LIST_SWAP_SBO = true; 1059 CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
1066 verifySwap(list); 1060 verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)));
1067 CX_DISABLE_LINKED_LIST_SWAP_SBO = false; 1061 CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
1068 } 1062 }
1069 1063
1070 TEST_F(ArrayList, cxListSwapNoSBO) { 1064 TEST_F(ArrayList, cxListSwapNoSBO) {
1071 CX_DISABLE_LINKED_LIST_SWAP_SBO = true; 1065 CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
1128 verifyInsertViaIterator(list); 1122 verifyInsertViaIterator(list);
1129 } 1123 }
1130 1124
1131 TEST_F(PointerLinkedList, InsertViaIterator) { 1125 TEST_F(PointerLinkedList, InsertViaIterator) {
1132 int fivenums[] = {0, 1, 2, 3, 4, 5}; 1126 int fivenums[] = {0, 1, 2, 3, 4, 5};
1133 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 1127 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS));
1134 cxListStorePointers(list);
1135 // note: cannot use cxListAddArray() because we don't have a list of pointers 1128 // note: cannot use cxListAddArray() because we don't have a list of pointers
1136 cx_for_n(i, 5) cxListAdd(list, &fivenums[i]); 1129 cx_for_n(i, 5) cxListAdd(list, &fivenums[i]);
1137 verifyInsertViaIterator(list); 1130 verifyInsertViaIterator(list);
1138 } 1131 }
1139 1132
1140 TEST_F(ArrayList, InsertViaIterator) { 1133 TEST_F(ArrayList, InsertViaIterator) {
1141 int fivenums[] = {0, 1, 2, 3, 4, 5}; 1134 int fivenums[] = {0, 1, 2, 3, 4, 5};
1142 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)); 1135 auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4));
1143 cxListAddArray(list, fivenums, 5); 1136 cxListAddArray(list, fivenums, 5);
1144 verifyInsertViaIterator(list); 1137 verifyInsertViaIterator(list);
1145 } 1138 }
1146 1139
1147 TEST_F(LinkedList, cxListReverse) { 1140 TEST_F(LinkedList, cxListReverse) {
1210 verifyCompare(left, right); 1203 verifyCompare(left, right);
1211 } 1204 }
1212 1205
1213 TEST_F(PointerLinkedList, NoDestructor) { 1206 TEST_F(PointerLinkedList, NoDestructor) {
1214 void *item = cxMalloc(&testingAllocator, sizeof(int)); 1207 void *item = cxMalloc(&testingAllocator, sizeof(int));
1215 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *)); 1208 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
1216 cxListStorePointers(list);
1217 cxListAdd(list, item); 1209 cxListAdd(list, item);
1218 ASSERT_FALSE(testingAllocator.verify()); 1210 ASSERT_FALSE(testingAllocator.verify());
1219 cxListDestroy(list); 1211 cxListDestroy(list);
1220 EXPECT_FALSE(testingAllocator.verify()); 1212 EXPECT_FALSE(testingAllocator.verify());
1221 cxFree(&testingAllocator, item); 1213 cxFree(&testingAllocator, item);
1222 EXPECT_TRUE(testingAllocator.verify()); 1214 EXPECT_TRUE(testingAllocator.verify());
1223 } 1215 }
1224 1216
1225 TEST_F(PointerLinkedList, SimpleDestructor) { 1217 TEST_F(PointerLinkedList, SimpleDestructor) {
1226 int item = 0; 1218 int item = 0;
1227 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *)); 1219 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
1228 cxListStorePointers(list);
1229 list->content_destructor_type = CX_DESTRUCTOR_SIMPLE; 1220 list->content_destructor_type = CX_DESTRUCTOR_SIMPLE;
1230 list->simple_destructor = [](void *elem) { *(int *) elem = 42; }; 1221 list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
1231 cxListAdd(list, &item); 1222 cxListAdd(list, &item);
1232 cxListDestroy(list); 1223 cxListDestroy(list);
1233 EXPECT_EQ(item, 42); 1224 EXPECT_EQ(item, 42);
1234 } 1225 }
1235 1226
1236 TEST_F(PointerLinkedList, AdvancedDestructor) { 1227 TEST_F(PointerLinkedList, AdvancedDestructor) {
1237 void *item = cxMalloc(&testingAllocator, sizeof(int)); 1228 void *item = cxMalloc(&testingAllocator, sizeof(int));
1238 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *)); 1229 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
1239 cxListStorePointers(list);
1240 list->content_destructor_type = CX_DESTRUCTOR_ADVANCED; 1230 list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;
1241 list->advanced_destructor.data = &testingAllocator; 1231 list->advanced_destructor.data = &testingAllocator;
1242 list->advanced_destructor.func = (cx_destructor_func2) cxFree; 1232 list->advanced_destructor.func = (cx_destructor_func2) cxFree;
1243 cxListAdd(list, item); 1233 cxListAdd(list, item);
1244 ASSERT_FALSE(testingAllocator.verify()); 1234 ASSERT_FALSE(testingAllocator.verify());

mercurial