tests/test_list.c

changeset 1422
8bfccb342895
parent 1419
e46406fd1b3c
child 1423
9a72258446cd
equal deleted inserted replaced
1421:809eb30cd621 1422:8bfccb342895
1141 1141
1142 CxIterator it1 = cxListIterator(list); 1142 CxIterator it1 = cxListIterator(list);
1143 CxIterator it2 = cxListBackwardsIterator(list); 1143 CxIterator it2 = cxListBackwardsIterator(list);
1144 CxIterator it3 = cxListMutIterator(list); 1144 CxIterator it3 = cxListMutIterator(list);
1145 CxIterator it4 = cxListMutBackwardsIterator(list); 1145 CxIterator it4 = cxListMutBackwardsIterator(list);
1146 CxIterator it5 = cxListMutIteratorAt(list, 0);
1147 CxIterator it6 = cxListMutBackwardsIteratorAt(list, 0);
1146 1148
1147 CX_TEST_DO { 1149 CX_TEST_DO {
1148 CX_TEST_ASSERT(!cxIteratorValid(it1)); 1150 CX_TEST_ASSERT(!cxIteratorValid(it1));
1149 CX_TEST_ASSERT(!cxIteratorValid(it2)); 1151 CX_TEST_ASSERT(!cxIteratorValid(it2));
1150 CX_TEST_ASSERT(!cxIteratorValid(it3)); 1152 CX_TEST_ASSERT(!cxIteratorValid(it3));
1151 CX_TEST_ASSERT(!cxIteratorValid(it4)); 1153 CX_TEST_ASSERT(!cxIteratorValid(it4));
1154 CX_TEST_ASSERT(!cxIteratorValid(it5));
1155 CX_TEST_ASSERT(!cxIteratorValid(it6));
1152 1156
1153 int c = 0; 1157 int c = 0;
1154 cx_foreach(void*, data, it1) c++; 1158 cx_foreach(void*, data, it1) c++;
1155 cx_foreach(void*, data, it2) c++; 1159 cx_foreach(void*, data, it2) c++;
1156 cx_foreach(void*, data, it3) c++; 1160 cx_foreach(void*, data, it3) c++;
1157 cx_foreach(void*, data, it4) c++; 1161 cx_foreach(void*, data, it4) c++;
1162 cx_foreach(void*, data, it5) c++;
1163 cx_foreach(void*, data, it6) c++;
1158 CX_TEST_ASSERT(c == 0); 1164 CX_TEST_ASSERT(c == 0);
1159 } 1165 }
1160 } 1166 }
1161 1167
1162 CX_TEST(test_empty_list_noops) { 1168 CX_TEST(test_empty_list_noops) {
1216 CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, ll)); 1222 CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, ll));
1217 CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, al)); 1223 CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, al));
1218 } 1224 }
1219 cxListFree(ll); 1225 cxListFree(ll);
1220 cxListFree(al); 1226 cxListFree(al);
1227 }
1228
1229 CX_TEST(test_null_list_free) {
1230 CX_TEST_DO {
1231 // cannot really verify, but asan or valgrind would complain
1232 cxListFree(NULL);
1233 }
1221 } 1234 }
1222 1235
1223 CX_TEST(test_list_ll_create) { 1236 CX_TEST(test_list_ll_create) {
1224 CxTestingAllocator talloc; 1237 CxTestingAllocator talloc;
1225 cx_testing_allocator_init(&talloc); 1238 cx_testing_allocator_init(&talloc);
2412 int *testdata = int_test_data_added_to_list(list, isptrlist, len); 2425 int *testdata = int_test_data_added_to_list(list, isptrlist, len);
2413 cxDefineAdvancedDestructor(list, advanced_destr_test_fun, NULL); 2426 cxDefineAdvancedDestructor(list, advanced_destr_test_fun, NULL);
2414 CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len); 2427 CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len);
2415 free(testdata); 2428 free(testdata);
2416 }) 2429 })
2430
2431 CX_TEST(test_list_pointer_list_supports_null) {
2432 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
2433 int x = 47;
2434 int y = 11;
2435 int z = 1337;
2436 int *nptr = NULL;
2437 cxListAdd(list, &x);
2438 cxListAdd(list, &y);
2439 cxListAdd(list, nptr);
2440 cxListAdd(list, &z);
2441 CX_TEST_DO {
2442 CX_TEST_ASSERT(cxListSize(list) == 4);
2443 CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == 47);
2444 CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 11);
2445 CX_TEST_ASSERT((int *) cxListAt(list, 2) == NULL);
2446 CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 1337);
2447 CX_TEST_ASSERT(cxListFind(list, nptr) == 2);
2448
2449 // when we sort the list, NULL is supposed to be smaller than any value
2450 cxListSort(list);
2451 CX_TEST_ASSERT((int *) cxListAt(list, 0) == NULL);
2452 CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 11);
2453 CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 47);
2454 CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 1337);
2455 }
2456 cxListFree(list);
2457 }
2417 2458
2418 CxTestSuite *cx_test_suite_array_list(void) { 2459 CxTestSuite *cx_test_suite_array_list(void) {
2419 CxTestSuite *suite = cx_test_suite_new("array_list"); 2460 CxTestSuite *suite = cx_test_suite_new("array_list");
2420 2461
2421 cx_test_register(suite, test_array_add); 2462 cx_test_register(suite, test_array_add);
2695 cx_test_register(suite, test_null_list_iterator); 2736 cx_test_register(suite, test_null_list_iterator);
2696 cx_test_register(suite, test_empty_list_noops); 2737 cx_test_register(suite, test_empty_list_noops);
2697 cx_test_register(suite, test_empty_list_at); 2738 cx_test_register(suite, test_empty_list_at);
2698 cx_test_register(suite, test_empty_list_find); 2739 cx_test_register(suite, test_empty_list_find);
2699 cx_test_register(suite, test_empty_list_compare); 2740 cx_test_register(suite, test_empty_list_compare);
2741 cx_test_register(suite, test_null_list_free);
2700 2742
2701 return suite; 2743 return suite;
2702 } 2744 }
2745
2746 CxTestSuite *cx_test_suite_list_corner_cases(void) {
2747 CxTestSuite *suite = cx_test_suite_new("list corner cases");
2748
2749 cx_test_register(suite, test_list_pointer_list_supports_null);
2750
2751 return suite;
2752 }

mercurial