tests/test_list.c

changeset 803
0711d869ce4d
parent 801
04aa3913c0e3
child 804
5136f2fc32ec
     1.1 --- a/tests/test_list.c	Fri Jan 12 20:13:13 2024 +0100
     1.2 +++ b/tests/test_list.c	Fri Jan 12 20:24:29 2024 +0100
     1.3 @@ -29,6 +29,7 @@
     1.4  #include "cx/test.h"
     1.5  #include "util_allocator.h"
     1.6  #include "cx/compare.h"
     1.7 +#include "cx/utils.h"
     1.8  
     1.9  #include "cx/array_list.h"
    1.10  #include "cx/linked_list.h"
    1.11 @@ -854,6 +855,491 @@
    1.12      cx_testing_allocator_destroy(&talloc);
    1.13  }
    1.14  
    1.15 +#define set_up_combo \
    1.16 +    CxTestingAllocator talloc; \
    1.17 +    cx_testing_allocator_init(&talloc); \
    1.18 +    CxAllocator *alloc = &talloc.base; \
    1.19 +    CX_TEST_DO {
    1.20 +#define tear_down_combo \
    1.21 +        cxListDestroy(list); \
    1.22 +        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));\
    1.23 +    } \
    1.24 +    cx_testing_allocator_destroy(&talloc);
    1.25 +#define roll_out_test_combos(name, body) \
    1.26 +static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \
    1.27 +    __attribute__((__unused__)) bool isptrlist, \
    1.28 +    __attribute__((__unused__)) bool islinkedlist) body \
    1.29 +CX_TEST(test_list_ll_##name) { \
    1.30 +    set_up_combo \
    1.31 +        CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); \
    1.32 +        CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false, true); \
    1.33 +    tear_down_combo \
    1.34 +} \
    1.35 +CX_TEST(test_list_arl_##name) { \
    1.36 +    set_up_combo \
    1.37 +        CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); \
    1.38 +        CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false, false); \
    1.39 +    tear_down_combo \
    1.40 +} \
    1.41 +CX_TEST(test_list_pll_##name) { \
    1.42 +    set_up_combo \
    1.43 +        CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS); \
    1.44 +        CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true, true); \
    1.45 +    tear_down_combo \
    1.46 +} \
    1.47 +CX_TEST(test_list_parl_##name) { \
    1.48 +    set_up_combo \
    1.49 +        CxList *list = cxArrayListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS, 8); \
    1.50 +        CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true, false); \
    1.51 +    tear_down_combo \
    1.52 +}
    1.53 +#define array_init(...) {__VA_ARGS__}
    1.54 +
    1.55 +static inline int *int_test_data_added_to_list(CxList *list, bool isptrlist, size_t len) {
    1.56 +    int *testdata = int_test_data(len);
    1.57 +    if (isptrlist) {
    1.58 +        cx_for_n(i, len) {
    1.59 +            cxListAdd(list, &testdata[i]);
    1.60 +        }
    1.61 +    } else {
    1.62 +        cxListAddArray(list, testdata, len);
    1.63 +    }
    1.64 +    return testdata;
    1.65 +}
    1.66 +
    1.67 +roll_out_test_combos(add, {
    1.68 +    const size_t len = 250;
    1.69 +    int *testdata = int_test_data(len);
    1.70 +    cx_for_n (i, len) CX_TEST_ASSERT(cxListAdd(list, &testdata[i]) == 0);
    1.71 +    CX_TEST_ASSERT(cxListSize(list) == len);
    1.72 +    cx_for_n (i, len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
    1.73 +    cx_for_n (i, len) ++testdata[i];
    1.74 +    if (isptrlist) {
    1.75 +        cx_for_n (i, len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
    1.76 +    } else {
    1.77 +        cx_for_n (i, len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i] - 1);
    1.78 +    }
    1.79 +    free(testdata);
    1.80 +})
    1.81 +
    1.82 +roll_out_test_combos(insert, {
    1.83 +    int a = 5;
    1.84 +    int b = 47;
    1.85 +    int c = 13;
    1.86 +    int d = 42;
    1.87 +    CX_TEST_ASSERT(cxListInsert(list, 1, &a) != 0);
    1.88 +    CX_TEST_ASSERT(cxListSize(list) == 0);
    1.89 +    CX_TEST_ASSERT(cxListInsert(list, 0, &a) == 0);
    1.90 +    CX_TEST_ASSERT(cxListSize(list) == 1);
    1.91 +    CX_TEST_ASSERT(cxListInsert(list, 0, &b) == 0);
    1.92 +    CX_TEST_ASSERT(cxListSize(list) == 2);
    1.93 +    CX_TEST_ASSERT(cxListInsert(list, 1, &c) == 0);
    1.94 +    CX_TEST_ASSERT(cxListSize(list) == 3);
    1.95 +    CX_TEST_ASSERT(cxListInsert(list, 3, &d) == 0);
    1.96 +    CX_TEST_ASSERT(cxListSize(list) == 4);
    1.97 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == 47);
    1.98 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 13);
    1.99 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 5);
   1.100 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 42);
   1.101 +})
   1.102 +
   1.103 +roll_out_test_combos(insert_array, {
   1.104 +    int a[5] = array_init(5, 47, 11, 13, 42);
   1.105 +    int b[5] = array_init(9, 18, 72, 50, 7);
   1.106 +    int *aptr[5];
   1.107 +    int *bptr[5];
   1.108 +    cx_for_n(i, 5) {
   1.109 +        aptr[i] = &a[i];
   1.110 +        bptr[i] = &b[i];
   1.111 +    }
   1.112 +
   1.113 +    size_t inserted;
   1.114 +
   1.115 +    if (isptrlist) {
   1.116 +        inserted = cxListInsertArray(list, 0, aptr, 5);
   1.117 +    } else {
   1.118 +        inserted = cxListInsertArray(list, 0, a, 5);
   1.119 +    }
   1.120 +    CX_TEST_ASSERT(inserted == 5);
   1.121 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == 5);
   1.122 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 47);
   1.123 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 11);
   1.124 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 13);
   1.125 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 4) == 42);
   1.126 +    if (isptrlist) {
   1.127 +        inserted = cxListInsertArray(list, 3, bptr, 5);
   1.128 +    } else {
   1.129 +        inserted = cxListInsertArray(list, 3, b, 5);
   1.130 +    }
   1.131 +    CX_TEST_ASSERT(inserted == 5);
   1.132 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == 5);
   1.133 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 47);
   1.134 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 11);
   1.135 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 9);
   1.136 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 4) == 18);
   1.137 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 5) == 72);
   1.138 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 6) == 50);
   1.139 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 7) == 7);
   1.140 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 8) == 13);
   1.141 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 9) == 42);
   1.142 +})
   1.143 +
   1.144 +roll_out_test_combos(remove, {
   1.145 +    const size_t testdata_len = 32;
   1.146 +    int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
   1.147 +
   1.148 +    CX_TEST_ASSERT(cxListRemove(list, 2) == 0);
   1.149 +    CX_TEST_ASSERT(cxListRemove(list, 4) == 0);
   1.150 +    CX_TEST_ASSERT(cxListSize(list) == testdata_len - 2);
   1.151 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == testdata[0]);
   1.152 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == testdata[1]);
   1.153 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == testdata[3]);
   1.154 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == testdata[4]);
   1.155 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 4) == testdata[6]);
   1.156 +    CX_TEST_ASSERT(cxListRemove(list, 0) == 0);
   1.157 +    CX_TEST_ASSERT(cxListSize(list) == testdata_len - 3);
   1.158 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == testdata[1]);
   1.159 +    CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == testdata[3]);
   1.160 +    CX_TEST_ASSERT(cxListRemove(list, testdata_len) != 0);
   1.161 +    free(testdata);
   1.162 +})
   1.163 +
   1.164 +roll_out_test_combos(find_remove, {
   1.165 +    const size_t testdata_len = 250;
   1.166 +    int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
   1.167 +
   1.168 +    int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
   1.169 +    int val = testdata[exp];
   1.170 +    // randomly picked number could occur earlier in list - find first position
   1.171 +    for (int i = 0 ; i < exp ; i++) {
   1.172 +        if (testdata[i] == val) {
   1.173 +            exp = i;
   1.174 +            break;
   1.175 +        }
   1.176 +    }
   1.177 +    CX_TEST_ASSERT(cxListSize(list) == testdata_len);
   1.178 +    CX_TEST_ASSERT(cxListFind(list, &val) == exp);
   1.179 +    CX_TEST_ASSERT(cxListFindRemove(list, &val) == exp);
   1.180 +    CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1);
   1.181 +    CX_TEST_ASSERT(cxListFind(list, &val) != exp);
   1.182 +
   1.183 +    int notinlist = -1;
   1.184 +    CX_TEST_ASSERT(cxListFindRemove(list, &notinlist) < 0);
   1.185 +    CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1);
   1.186 +
   1.187 +    free(testdata);
   1.188 +})
   1.189 +
   1.190 +roll_out_test_combos(clear, {
   1.191 +    int *testdata = int_test_data_added_to_list(list, isptrlist, 8);
   1.192 +    CX_TEST_ASSERT(cxListSize(list) > 0);
   1.193 +    cxListClear(list);
   1.194 +    CX_TEST_ASSERT(cxListSize(list) == 0);
   1.195 +    free(testdata);
   1.196 +})
   1.197 +
   1.198 +roll_out_test_combos(at, {
   1.199 +    size_t len = 128;
   1.200 +    int *testdata = int_test_data_added_to_list(list, isptrlist, 128);
   1.201 +    CX_TEST_ASSERT(cxListSize(list) == len);
   1.202 +    cx_for_n (i, len) {
   1.203 +        CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
   1.204 +    }
   1.205 +    CX_TEST_ASSERT(cxListAt(list, cxListSize(list)) == NULL);
   1.206 +    free(testdata);
   1.207 +})
   1.208 +
   1.209 +roll_out_test_combos(swap, {
   1.210 +    int original[16] = array_init(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
   1.211 +    int swapped[16] = array_init(8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13);
   1.212 +
   1.213 +    cx_for_n(i, 16) {
   1.214 +        cxListAdd(list, &original[i]);
   1.215 +    }
   1.216 +
   1.217 +    CX_TEST_ASSERT(0 == cxListSwap(list, 1, 4));
   1.218 +    CX_TEST_ASSERT(0 == cxListSwap(list, 2, 14));
   1.219 +    CX_TEST_ASSERT(0 == cxListSwap(list, 9, 6));
   1.220 +    CX_TEST_ASSERT(0 == cxListSwap(list, 3, 3));
   1.221 +    CX_TEST_ASSERT(0 == cxListSwap(list, 10, 11));
   1.222 +    CX_TEST_ASSERT(0 == cxListSwap(list, 8, 0));
   1.223 +    CX_TEST_ASSERT(0 == cxListSwap(list, 7, 12));
   1.224 +    CX_TEST_ASSERT(0 == cxListSwap(list, 13, 15));
   1.225 +
   1.226 +    CX_TEST_ASSERT(0 != cxListSwap(list, 5, 16));
   1.227 +    CX_TEST_ASSERT(0 != cxListSwap(list, 16, 6));
   1.228 +    CX_TEST_ASSERT(0 != cxListSwap(list, 16, 17));
   1.229 +
   1.230 +    CxIterator iter = cxListIterator(list);
   1.231 +    cx_foreach(int*, e, iter) {
   1.232 +        CX_TEST_ASSERT(*e == swapped[iter.index]);
   1.233 +    }
   1.234 +    iter = cxListBackwardsIterator(list);
   1.235 +    cx_foreach(int*, e, iter) {
   1.236 +        CX_TEST_ASSERT(*e == swapped[iter.index]);
   1.237 +    }
   1.238 +})
   1.239 +
   1.240 +roll_out_test_combos(swap_no_sbo, {
   1.241 +    if (islinkedlist) {
   1.242 +        CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
   1.243 +        CX_TEST_CALL_SUBROUTINE(test_list_verify_swap, list, isptrlist, true);
   1.244 +        CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
   1.245 +    } else {
   1.246 +        // TODO: currently, SBO for swap operation cannot be disabled for array lists
   1.247 +        CX_TEST_ASSERT(true);
   1.248 +    }
   1.249 +})
   1.250 +
   1.251 +roll_out_test_combos(find, {
   1.252 +    const size_t testdata_len = 500;
   1.253 +    int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
   1.254 +
   1.255 +    cx_for_n (attempt, 25) {
   1.256 +        int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
   1.257 +        int val = testdata[exp];
   1.258 +        // randomly picked number could occur earlier in list - find first position
   1.259 +        for (int i = 0 ; i < exp ; i++) {
   1.260 +            if (testdata[i] == val) {
   1.261 +                exp = i;
   1.262 +                break;
   1.263 +            }
   1.264 +        }
   1.265 +        CX_TEST_ASSERT(cxListFind(list, &val) == exp);
   1.266 +    }
   1.267 +
   1.268 +    int notinlist = -1;
   1.269 +    CX_TEST_ASSERT(cxListFind(list, &notinlist) < 0);
   1.270 +
   1.271 +    free(testdata);
   1.272 +})
   1.273 +
   1.274 +roll_out_test_combos(sort, {
   1.275 +    const size_t testdata_len = 250;
   1.276 +    int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
   1.277 +    int *expected = malloc(testdata_len*sizeof(int));
   1.278 +    memcpy(expected, testdata, testdata_len*sizeof(int));
   1.279 +    qsort(expected, testdata_len, sizeof(int), cx_cmp_int);
   1.280 +
   1.281 +    cxListSort(list);
   1.282 +    cx_for_n (i, testdata_len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]);
   1.283 +
   1.284 +    free(expected);
   1.285 +    free(testdata);
   1.286 +})
   1.287 +
   1.288 +roll_out_test_combos(reverse, {
   1.289 +    const size_t testdata_len = 50;
   1.290 +    int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
   1.291 +    cxListReverse(list);
   1.292 +    cx_for_n(i, testdata_len) {
   1.293 +        CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[testdata_len - 1 - i]);
   1.294 +    }
   1.295 +    free(testdata);
   1.296 +})
   1.297 +
   1.298 +roll_out_test_combos(iterator, {
   1.299 +    const size_t len = 50;
   1.300 +    int *testdata = int_test_data_added_to_list(list, isptrlist, len);
   1.301 +
   1.302 +    CxIterator iter = cxListIterator(list);
   1.303 +    size_t i = 0;
   1.304 +    cx_foreach(int*, x, iter) {
   1.305 +        CX_TEST_ASSERT(i == iter.index);
   1.306 +        CX_TEST_ASSERT(*x == testdata[iter.index]);
   1.307 +        i++;
   1.308 +    }
   1.309 +    CX_TEST_ASSERT(i == cxListSize(list));
   1.310 +    iter = cxListBackwardsIterator(list);
   1.311 +    cx_foreach(int*, x, iter) {
   1.312 +        CX_TEST_ASSERT(i - 1 == iter.index);
   1.313 +        CX_TEST_ASSERT(*x == testdata[iter.index]);
   1.314 +        i--;
   1.315 +    }
   1.316 +    CX_TEST_ASSERT(i == 0);
   1.317 +    i = len / 2;
   1.318 +    CxMutIterator mut_iter = cxListMutIteratorAt(list, i);
   1.319 +    size_t j = 0;
   1.320 +    cx_foreach(int*, x, mut_iter) {
   1.321 +        CX_TEST_ASSERT(mut_iter.index == len / 2 + j / 2);
   1.322 +        CX_TEST_ASSERT(*x == testdata[i]);
   1.323 +        if (i % 2 == 1) cxIteratorFlagRemoval(mut_iter);
   1.324 +        i++;
   1.325 +        j++;
   1.326 +    }
   1.327 +    CX_TEST_ASSERT(i == len);
   1.328 +    i = len / 2;
   1.329 +    j = 0;
   1.330 +    mut_iter = cxListMutBackwardsIteratorAt(list, i - 1);
   1.331 +    cx_foreach(int*, x, mut_iter) {
   1.332 +        CX_TEST_ASSERT(mut_iter.index == len / 2 - 1 - j);
   1.333 +        CX_TEST_ASSERT(*x == testdata[i - 1]);
   1.334 +        if (i % 2 == 0) cxIteratorFlagRemoval(mut_iter);
   1.335 +        i--;
   1.336 +        j++;
   1.337 +    }
   1.338 +    CX_TEST_ASSERT(i == 0);
   1.339 +    CX_TEST_ASSERT(cxListSize(list) == len / 2);
   1.340 +    cx_for_n(k, len / 2) CX_TEST_ASSERT(*(int *) cxListAt(list, k) == testdata[k * 2]);
   1.341 +
   1.342 +    free(testdata);
   1.343 +})
   1.344 +
   1.345 +roll_out_test_combos(insert_with_iterator, {
   1.346 +    int fivenums[] = array_init(0, 1, 2, 3, 4);
   1.347 +    cx_for_n(i, 5) cxListAdd(list, &fivenums[i]);
   1.348 +    int newdata[] = array_init(10, 20, 30, 40, 50);
   1.349 +
   1.350 +    CxMutIterator iter = cxListMutIteratorAt(list, 2);
   1.351 +    CX_TEST_ASSERT(cxIteratorValid(iter));
   1.352 +    CX_TEST_ASSERT(iter.index == 2);
   1.353 +    CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2);
   1.354 +    cxListInsertAfter(&iter, &newdata[0]);
   1.355 +    CX_TEST_ASSERT(cxIteratorValid(iter));
   1.356 +    CX_TEST_ASSERT(iter.index == 2);
   1.357 +    CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2);
   1.358 +    cxListInsertBefore(&iter, &newdata[1]);
   1.359 +    CX_TEST_ASSERT(cxIteratorValid(iter));
   1.360 +    CX_TEST_ASSERT(iter.index == 3);
   1.361 +    CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2);
   1.362 +
   1.363 +    iter = cxListMutIterator(list);
   1.364 +    cxListInsertBefore(&iter, &newdata[2]);
   1.365 +    CX_TEST_ASSERT(cxIteratorValid(iter));
   1.366 +    CX_TEST_ASSERT(iter.index == 1);
   1.367 +    CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 0);
   1.368 +    iter = cxListMutIteratorAt(list, cxListSize(list));
   1.369 +    cxListInsertBefore(&iter, &newdata[3]);
   1.370 +    CX_TEST_ASSERT(!cxIteratorValid(iter));
   1.371 +    CX_TEST_ASSERT(iter.index == 9);
   1.372 +    iter = cxListMutIteratorAt(list, cxListSize(list));
   1.373 +    cxListInsertAfter(&iter, &newdata[4]);
   1.374 +    CX_TEST_ASSERT(!cxIteratorValid(iter));
   1.375 +    CX_TEST_ASSERT(iter.index == 10);
   1.376 +
   1.377 +    int expdata[] = array_init(30, 0, 1, 20, 2, 10, 3, 4, 40, 50);
   1.378 +    cx_for_n (j, 10) CX_TEST_ASSERT(*(int *) cxListAt(list, j) == expdata[j]);
   1.379 +})
   1.380 +
   1.381 +static CX_TEST_SUBROUTINE(test_list_verify_compare, CxList *left, CxList *right) {
   1.382 +    CX_TEST_ASSERTM(cxListCompare(left, right) == 0, "lists don't start identical");
   1.383 +    int x = 42;
   1.384 +    cxListAdd(left, &x);
   1.385 +    CX_TEST_ASSERT(cxListSize(left) > cxListSize(right));
   1.386 +    CX_TEST_ASSERT(cxListCompare(left, right) > 0);
   1.387 +    CX_TEST_ASSERT(cxListCompare(right, left) < 0);
   1.388 +    cxListAdd(right, &x);
   1.389 +    CX_TEST_ASSERT(cxListSize(left) == cxListSize(right));
   1.390 +    CX_TEST_ASSERT(cxListCompare(left, right) == 0);
   1.391 +    int a = 5, b = 10;
   1.392 +    cxListInsert(left, 15, &a);
   1.393 +    cxListInsert(right, 15, &b);
   1.394 +    CX_TEST_ASSERT(cxListSize(left) == cxListSize(right));
   1.395 +    CX_TEST_ASSERT(cxListCompare(left, right) < 0);
   1.396 +    CX_TEST_ASSERT(cxListCompare(right, left) > 0);
   1.397 +    *(int *) cxListAt(left, 15) = 10;
   1.398 +    CX_TEST_ASSERT(cxListCompare(left, right) == 0);
   1.399 +}
   1.400 +
   1.401 +#define roll_out_compare_tests(suffix, otherctr) \
   1.402 +roll_out_test_combos(compare_##suffix, { \
   1.403 +    const size_t len = 47; \
   1.404 +    int *testdata = int_test_data_added_to_list(list, isptrlist, len); \
   1.405 +    CxList *other = otherctr; \
   1.406 +    cx_for_n(i, len) cxListAdd(other, &testdata[i]); \
   1.407 +    CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \
   1.408 +    cxListDestroy(other); \
   1.409 +    free(testdata); \
   1.410 +})
   1.411 +
   1.412 +roll_out_compare_tests(
   1.413 +        ll, cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int))
   1.414 +)
   1.415 +
   1.416 +roll_out_compare_tests(
   1.417 +        pll, cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS)
   1.418 +)
   1.419 +
   1.420 +roll_out_compare_tests(
   1.421 +        arl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50)
   1.422 +)
   1.423 +
   1.424 +roll_out_compare_tests(
   1.425 +        parl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 50)
   1.426 +)
   1.427 +
   1.428 +static unsigned destr_test_ctr;
   1.429 +static int destr_last_value;
   1.430 +
   1.431 +static void simple_destr_test_fun(void *data) {
   1.432 +    int *ptr = data;
   1.433 +    destr_last_value = *ptr;
   1.434 +    *ptr = destr_last_value + 1;
   1.435 +    destr_test_ctr++;
   1.436 +}
   1.437 +
   1.438 +static void advanced_destr_test_fun(__attribute__((__unused__)) void *u, void *data) {
   1.439 +    simple_destr_test_fun(data);
   1.440 +}
   1.441 +
   1.442 +static CX_TEST_SUBROUTINE(test_list_verify_destructor, CxList *list,
   1.443 +                          int const *testdata, size_t testdata_len) {
   1.444 +    destr_test_ctr = 0;
   1.445 +    
   1.446 +    int off = cxListIsStoringPointers(list) ? 1 : 0;
   1.447 +
   1.448 +    cxListRemove(list, 15);
   1.449 +    CX_TEST_ASSERT(1 == destr_test_ctr);
   1.450 +    CX_TEST_ASSERT(testdata[15] == destr_last_value + off);
   1.451 +    CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list));
   1.452 +    cxListRemove(list, 47);
   1.453 +    CX_TEST_ASSERT(2 == destr_test_ctr);
   1.454 +    CX_TEST_ASSERT(testdata[48] == destr_last_value + off);
   1.455 +    CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list));
   1.456 +
   1.457 +    CxMutIterator iter = cxListMutIteratorAt(list, 7);
   1.458 +    cxIteratorNext(iter);
   1.459 +    CX_TEST_ASSERT(2 == destr_test_ctr);
   1.460 +    CX_TEST_ASSERT(testdata[48] == destr_last_value + off);
   1.461 +    CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list));
   1.462 +    cxIteratorFlagRemoval(iter);
   1.463 +    cxIteratorNext(iter);
   1.464 +    CX_TEST_ASSERT(3 == destr_test_ctr);
   1.465 +    CX_TEST_ASSERT(testdata[8] == destr_last_value + off);
   1.466 +    CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list));
   1.467 +
   1.468 +    iter = cxListMutBackwardsIteratorAt(list, 5);
   1.469 +    cxIteratorNext(iter);
   1.470 +    CX_TEST_ASSERT(3 == destr_test_ctr);
   1.471 +    CX_TEST_ASSERT(testdata[8] == destr_last_value + off);
   1.472 +    CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list));
   1.473 +    cxIteratorFlagRemoval(iter);
   1.474 +    cxIteratorNext(iter);
   1.475 +    CX_TEST_ASSERT(4 == destr_test_ctr);
   1.476 +    CX_TEST_ASSERT(testdata[4] == destr_last_value + off);
   1.477 +    CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list));
   1.478 +
   1.479 +    cxListClear(list);
   1.480 +    CX_TEST_ASSERT(testdata_len == destr_test_ctr);
   1.481 +    CX_TEST_ASSERT(testdata[testdata_len - 1] == destr_last_value + off);
   1.482 +}
   1.483 +
   1.484 +roll_out_test_combos(simple_destr, {
   1.485 +    const size_t len = 60;
   1.486 +    int *testdata = int_test_data_added_to_list(list, isptrlist, len);
   1.487 +    list->simple_destructor = simple_destr_test_fun;
   1.488 +    CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len);
   1.489 +    free(testdata);
   1.490 +})
   1.491 +
   1.492 +roll_out_test_combos(advanced_destr, {
   1.493 +    const size_t len = 75;
   1.494 +    int *testdata = int_test_data_added_to_list(list, isptrlist, len);
   1.495 +    list->advanced_destructor = advanced_destr_test_fun;
   1.496 +    CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len);
   1.497 +    free(testdata);
   1.498 +})
   1.499 +
   1.500  CxTestSuite *cx_test_suite_array_list(void) {
   1.501      CxTestSuite *suite = cx_test_suite_new("array_list");
   1.502  
   1.503 @@ -864,6 +1350,47 @@
   1.504      cx_test_register(suite, test_list_parl_destroy_simple_destr);
   1.505      cx_test_register(suite, test_list_parl_destroy_adv_destr);
   1.506  
   1.507 +    cx_test_register(suite, test_list_arl_add);
   1.508 +    cx_test_register(suite, test_list_parl_add);
   1.509 +    cx_test_register(suite, test_list_arl_insert);
   1.510 +    cx_test_register(suite, test_list_parl_insert);
   1.511 +    cx_test_register(suite, test_list_arl_insert_array);
   1.512 +    cx_test_register(suite, test_list_parl_insert_array);
   1.513 +    cx_test_register(suite, test_list_arl_remove);
   1.514 +    cx_test_register(suite, test_list_parl_remove);
   1.515 +    cx_test_register(suite, test_list_arl_find_remove);
   1.516 +    cx_test_register(suite, test_list_parl_find_remove);
   1.517 +    cx_test_register(suite, test_list_arl_clear);
   1.518 +    cx_test_register(suite, test_list_parl_clear);
   1.519 +    cx_test_register(suite, test_list_arl_at);
   1.520 +    cx_test_register(suite, test_list_parl_at);
   1.521 +    cx_test_register(suite, test_list_arl_swap);
   1.522 +    cx_test_register(suite, test_list_parl_swap);
   1.523 +    cx_test_register(suite, test_list_arl_swap_no_sbo);
   1.524 +    cx_test_register(suite, test_list_parl_swap_no_sbo);
   1.525 +    cx_test_register(suite, test_list_arl_find);
   1.526 +    cx_test_register(suite, test_list_parl_find);
   1.527 +    cx_test_register(suite, test_list_arl_sort);
   1.528 +    cx_test_register(suite, test_list_parl_sort);
   1.529 +    cx_test_register(suite, test_list_arl_reverse);
   1.530 +    cx_test_register(suite, test_list_parl_reverse);
   1.531 +    cx_test_register(suite, test_list_arl_iterator);
   1.532 +    cx_test_register(suite, test_list_parl_iterator);
   1.533 +    cx_test_register(suite, test_list_arl_insert_with_iterator);
   1.534 +    cx_test_register(suite, test_list_parl_insert_with_iterator);
   1.535 +    cx_test_register(suite, test_list_arl_compare_ll);
   1.536 +    cx_test_register(suite, test_list_arl_compare_arl);
   1.537 +    cx_test_register(suite, test_list_arl_compare_pll);
   1.538 +    cx_test_register(suite, test_list_arl_compare_parl);
   1.539 +    cx_test_register(suite, test_list_parl_compare_ll);
   1.540 +    cx_test_register(suite, test_list_parl_compare_arl);
   1.541 +    cx_test_register(suite, test_list_parl_compare_pll);
   1.542 +    cx_test_register(suite, test_list_parl_compare_parl);
   1.543 +    cx_test_register(suite, test_list_arl_simple_destr);
   1.544 +    cx_test_register(suite, test_list_parl_simple_destr);
   1.545 +    cx_test_register(suite, test_list_arl_advanced_destr);
   1.546 +    cx_test_register(suite, test_list_parl_advanced_destr);
   1.547 +
   1.548      return suite;
   1.549  }
   1.550  
   1.551 @@ -895,6 +1422,47 @@
   1.552      cx_test_register(suite, test_list_pll_destroy_simple_destr);
   1.553      cx_test_register(suite, test_list_pll_destroy_adv_destr);
   1.554  
   1.555 +    cx_test_register(suite, test_list_ll_add);
   1.556 +    cx_test_register(suite, test_list_pll_add);
   1.557 +    cx_test_register(suite, test_list_ll_insert);
   1.558 +    cx_test_register(suite, test_list_pll_insert);
   1.559 +    cx_test_register(suite, test_list_ll_insert_array);
   1.560 +    cx_test_register(suite, test_list_pll_insert_array);
   1.561 +    cx_test_register(suite, test_list_ll_remove);
   1.562 +    cx_test_register(suite, test_list_pll_remove);
   1.563 +    cx_test_register(suite, test_list_ll_find_remove);
   1.564 +    cx_test_register(suite, test_list_pll_find_remove);
   1.565 +    cx_test_register(suite, test_list_ll_clear);
   1.566 +    cx_test_register(suite, test_list_pll_clear);
   1.567 +    cx_test_register(suite, test_list_ll_at);
   1.568 +    cx_test_register(suite, test_list_pll_at);
   1.569 +    cx_test_register(suite, test_list_ll_swap);
   1.570 +    cx_test_register(suite, test_list_pll_swap);
   1.571 +    cx_test_register(suite, test_list_ll_swap_no_sbo);
   1.572 +    cx_test_register(suite, test_list_pll_swap_no_sbo);
   1.573 +    cx_test_register(suite, test_list_ll_find);
   1.574 +    cx_test_register(suite, test_list_pll_find);
   1.575 +    cx_test_register(suite, test_list_ll_sort);
   1.576 +    cx_test_register(suite, test_list_pll_sort);
   1.577 +    cx_test_register(suite, test_list_ll_reverse);
   1.578 +    cx_test_register(suite, test_list_pll_reverse);
   1.579 +    cx_test_register(suite, test_list_ll_iterator);
   1.580 +    cx_test_register(suite, test_list_pll_iterator);
   1.581 +    cx_test_register(suite, test_list_ll_insert_with_iterator);
   1.582 +    cx_test_register(suite, test_list_pll_insert_with_iterator);
   1.583 +    cx_test_register(suite, test_list_ll_compare_ll);
   1.584 +    cx_test_register(suite, test_list_ll_compare_arl);
   1.585 +    cx_test_register(suite, test_list_ll_compare_pll);
   1.586 +    cx_test_register(suite, test_list_ll_compare_parl);
   1.587 +    cx_test_register(suite, test_list_pll_compare_ll);
   1.588 +    cx_test_register(suite, test_list_pll_compare_arl);
   1.589 +    cx_test_register(suite, test_list_pll_compare_pll);
   1.590 +    cx_test_register(suite, test_list_pll_compare_parl);
   1.591 +    cx_test_register(suite, test_list_ll_simple_destr);
   1.592 +    cx_test_register(suite, test_list_pll_simple_destr);
   1.593 +    cx_test_register(suite, test_list_ll_advanced_destr);
   1.594 +    cx_test_register(suite, test_list_pll_advanced_destr);
   1.595 +
   1.596      return suite;
   1.597  }
   1.598  

mercurial