tests/test_list.c

changeset 1480
83146195a1db
parent 1477
9170a7dff573
child 1481
1dda1eed1899
equal deleted inserted replaced
1479:ac1baaed2fd7 1480:83146195a1db
2972 2972
2973 CX_TEST(test_list_union_sorted_alloc_fail) { 2973 CX_TEST(test_list_union_sorted_alloc_fail) {
2974 CX_TEST_DO { 2974 CX_TEST_DO {
2975 CX_TEST_CALL_SUBROUTINE(verify_union, true, true); 2975 CX_TEST_CALL_SUBROUTINE(verify_union, true, true);
2976 } 2976 }
2977 }
2978
2979 CX_TEST(test_list_simple_clones) {
2980
2981 int a[] = {1, 2, 5, 8, 10};
2982 int b[] = {1, 3, 5, 7, 9, 11};
2983 int c[] = {2, 4, 6, 8, 10, 12};
2984 int d[] = {4, 8, 12};
2985
2986 CxList *la = cxArrayListCreateSimple(sizeof(int), 8);
2987 cxCollectionCompareFunc(la, cx_cmp_int);
2988 cxListInsertSortedArray(la, a, cx_nmemb(a));
2989 CxList *lb = cxArrayListCreateSimple(sizeof(int), 8);
2990 cxCollectionCompareFunc(lb, cx_cmp_int);
2991 cxListInsertSortedArray(lb, b, cx_nmemb(b));
2992 CxList *lc = cxArrayListCreateSimple(sizeof(int), 8);
2993 cxCollectionCompareFunc(lc, cx_cmp_int);
2994 cxListInsertSortedArray(lc, c, cx_nmemb(c));
2995 CxList *ld = cxArrayListCreateSimple(sizeof(int), 8);
2996 cxCollectionCompareFunc(ld, cx_cmp_int);
2997 cxListInsertSortedArray(ld, d, cx_nmemb(d));
2998
2999 CxList *d1 = cxArrayListCreateSimple(CX_STORE_POINTERS, 8);
3000 cxCollectionCompareFunc(d1, cx_cmp_int);
3001 cxDefineAdvancedDestructor(d1, cxFree, (void*) cxDefaultAllocator);
3002 CxList *d2 = cxArrayListCreateSimple(CX_STORE_POINTERS, 8);
3003 cxCollectionCompareFunc(d2, cx_cmp_int);
3004 cxDefineAdvancedDestructor(d2, cxFree, (void*) cxDefaultAllocator);
3005
3006 CX_TEST_DO {
3007 // clone a into d1
3008 CX_TEST_ASSERT(0 == cxListCloneSimple(d1, la));
3009 CX_TEST_ASSERT(0 == cxListCompare(d1, la));
3010 CX_TEST_ASSERT(cxCollectionSorted(d1));
3011
3012 // union of a (in d1) and b
3013 CX_TEST_ASSERT(0 == cxListUnionSimple(d2, d1, lb));
3014 CX_TEST_ASSERT(cxCollectionSorted(d2));
3015 CxList *expected_union = cxArrayListCreateSimple(sizeof(int), 8);
3016 {
3017 int expected[] = {1, 2, 3, 5, 7, 8, 9, 10, 11};
3018 cxListInsertSortedArray(expected_union, expected, cx_nmemb(expected));
3019 }
3020 CX_TEST_ASSERT(0 == cxListCompare(d2, expected_union));
3021 cxListFree(expected_union);
3022
3023 // intersection of (a union b) and c
3024 cxListClear(d1);
3025 CX_TEST_ASSERT(0 == cxListIntersectionSimple(d1, d2, lc));
3026 CX_TEST_ASSERT(cxCollectionSorted(d1));
3027 CxList *expected_intersection = cxArrayListCreateSimple(sizeof(int), 8);
3028 {
3029 int expected[] = {2, 8, 10};
3030 cxListInsertSortedArray(expected_intersection, expected, cx_nmemb(expected));
3031 }
3032 CX_TEST_ASSERT(0 == cxListCompare(d1, expected_intersection));
3033 cxListFree(expected_intersection);
3034
3035 // difference of ((a union b) intersect c) minus d
3036 cxListClear(d2);
3037 CX_TEST_ASSERT(0 == cxListDifferenceSimple(d2, d1, ld));
3038 CX_TEST_ASSERT(cxCollectionSorted(d2));
3039 CxList *expected_difference = cxArrayListCreateSimple(sizeof(int), 8);
3040 {
3041 int expected[] = {2, 10};
3042 cxListInsertSortedArray(expected_difference, expected, cx_nmemb(expected));
3043 }
3044 CX_TEST_ASSERT(0 == cxListCompare(d2, expected_difference));
3045 cxListFree(expected_difference);
3046 }
3047
3048 cxListFree(d1);
3049 cxListFree(d2);
3050 cxListFree(la);
3051 cxListFree(lb);
3052 cxListFree(lc);
3053 cxListFree(ld);
2977 } 3054 }
2978 3055
2979 CX_TEST(test_list_pointer_list_supports_null) { 3056 CX_TEST(test_list_pointer_list_supports_null) {
2980 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 3057 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
2981 int x = 47; 3058 int x = 47;
3441 cx_test_register(suite, test_list_difference_sorted_alloc_fail); 3518 cx_test_register(suite, test_list_difference_sorted_alloc_fail);
3442 cx_test_register(suite, test_list_intersection_unsorted); 3519 cx_test_register(suite, test_list_intersection_unsorted);
3443 cx_test_register(suite, test_list_intersection_sorted); 3520 cx_test_register(suite, test_list_intersection_sorted);
3444 cx_test_register(suite, test_list_intersection_unsorted_alloc_fail); 3521 cx_test_register(suite, test_list_intersection_unsorted_alloc_fail);
3445 cx_test_register(suite, test_list_intersection_sorted_alloc_fail); 3522 cx_test_register(suite, test_list_intersection_sorted_alloc_fail);
3523 cx_test_register(suite, test_list_simple_clones);
3446 3524
3447 return suite; 3525 return suite;
3448 } 3526 }
3449 3527
3450 CxTestSuite *cx_test_suite_list_corner_cases(void) { 3528 CxTestSuite *cx_test_suite_list_corner_cases(void) {

mercurial