#129 - remove test code duplication

Sat, 09 Apr 2022 18:02:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 09 Apr 2022 18:02:53 +0200
changeset 509
0d3c6075f82c
parent 508
8aea65ae1eaf
child 510
133ac0f8f3fc

#129 - remove test code duplication

src/cx/utils.h file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
test/test_list.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/cx/utils.h	Sat Apr 09 16:37:43 2022 +0200
     1.2 +++ b/src/cx/utils.h	Sat Apr 09 18:02:53 2022 +0200
     1.3 @@ -46,6 +46,8 @@
     1.4  extern "C" {
     1.5  #endif
     1.6  
     1.7 +#define cx_for_n(varname, n) for (size_t varname = 0 ; (varname) < (n) ; (varname)++)
     1.8 +
     1.9  /* ----------------------
    1.10   * cx_szmul() definition.
    1.11   * ---------------------- */
     2.1 --- a/src/linked_list.c	Sat Apr 09 16:37:43 2022 +0200
     2.2 +++ b/src/linked_list.c	Sat Apr 09 18:02:53 2022 +0200
     2.3 @@ -27,6 +27,7 @@
     2.4   */
     2.5  
     2.6  #include "cx/linked_list.h"
     2.7 +#include "cx/utils.h"
     2.8  #include <stdint.h>
     2.9  #include <string.h>
    2.10  #include <assert.h>
    2.11 @@ -323,7 +324,7 @@
    2.12  
    2.13      // Update pointer
    2.14      if (loc_prev >= 0) ll_prev(sorted[0]) = NULL;
    2.15 -    for (size_t i = 0; i < length - 1; i++) {
    2.16 +    cx_for_n (i, length - 1) {
    2.17          cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next);
    2.18      }
    2.19      ll_next(sorted[length - 1]) = NULL;
    2.20 @@ -818,7 +819,7 @@
    2.21  ) {
    2.22      CxList *list = cxLinkedListCreate(allocator, comparator, item_size);
    2.23      if (list == NULL) return NULL;
    2.24 -    for (size_t i = 0; i < num_items; i++) {
    2.25 +    cx_for_n (i, num_items) {
    2.26          if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) {
    2.27              return cx_ll_default_destructor(list);
    2.28          }
     3.1 --- a/test/test_list.c	Sat Apr 09 16:37:43 2022 +0200
     3.2 +++ b/test/test_list.c	Sat Apr 09 18:02:53 2022 +0200
     3.3 @@ -27,6 +27,7 @@
     3.4   */
     3.5  
     3.6  #include "cx/linked_list.h"
     3.7 +#include "cx/utils.h"
     3.8  #include "test_config.h"
     3.9  #include "util_allocator.h"
    3.10  
    3.11 @@ -52,11 +53,11 @@
    3.12  const ptrdiff_t loc_next = offsetof(struct node, next);
    3.13  const ptrdiff_t loc_data = offsetof(struct node, data);
    3.14  
    3.15 -struct node *create_test_data(
    3.16 +static struct node *create_nodes_test_data(
    3.17          size_t n,
    3.18          int const data[]
    3.19  ) {
    3.20 -    if (n == 0) return NULL;
    3.21 +    CU_ASSERT_NOT_EQUAL_FATAL(n, 0)
    3.22      struct node *begin = calloc(1, sizeof(struct node));
    3.23      struct node *prev = begin;
    3.24      if (data) begin->data = data[0];
    3.25 @@ -69,7 +70,7 @@
    3.26      return begin;
    3.27  }
    3.28  
    3.29 -void destroy_test_data(struct node *begin) {
    3.30 +static void destroy_nodes_test_data(struct node *begin) {
    3.31      struct node *node = begin;
    3.32      while (node) {
    3.33          struct node *next = node->next;
    3.34 @@ -78,6 +79,12 @@
    3.35      }
    3.36  }
    3.37  
    3.38 +static int *create_ints_test_data(size_t len) {
    3.39 +    int *data = malloc(sizeof(int) * len);
    3.40 +    cx_for_n (i, len) data[i] = rand(); // NOLINT(cert-msc50-cpp)
    3.41 +    return data;
    3.42 +}
    3.43 +
    3.44  void test_linked_list_link_unlink(void) {
    3.45  
    3.46      struct node nd(a), nd(b), nd(c);
    3.47 @@ -129,7 +136,7 @@
    3.48  
    3.49  void test_linked_list_find(void) {
    3.50      int data[] = {2, 4, 6, 8};
    3.51 -    void *list = create_test_data(4, data);
    3.52 +    void *list = create_nodes_test_data(4, data);
    3.53      int s;
    3.54  
    3.55      s = 2;
    3.56 @@ -157,9 +164,9 @@
    3.57      int b[] = {2, 4, 6};
    3.58      int c[] = {2, 4, 6, 9};
    3.59  
    3.60 -    void *la = create_test_data(4, a);
    3.61 -    void *lb = create_test_data(3, b);
    3.62 -    void *lc = create_test_data(4, c);
    3.63 +    void *la = create_nodes_test_data(4, a);
    3.64 +    void *lb = create_nodes_test_data(3, b);
    3.65 +    void *lc = create_nodes_test_data(4, c);
    3.66  
    3.67      CU_ASSERT_TRUE(0 < cx_linked_list_compare(la, lb, loc_next, loc_data,
    3.68                                                false, cmp_int)
    3.69 @@ -177,9 +184,9 @@
    3.70                                                 false, cmp_int)
    3.71      )
    3.72  
    3.73 -    destroy_test_data(la);
    3.74 -    destroy_test_data(lb);
    3.75 -    destroy_test_data(lc);
    3.76 +    destroy_nodes_test_data(la);
    3.77 +    destroy_nodes_test_data(lb);
    3.78 +    destroy_nodes_test_data(lc);
    3.79  }
    3.80  
    3.81  void test_linked_list_add(void) {
    3.82 @@ -421,35 +428,35 @@
    3.83  }
    3.84  
    3.85  void test_linked_list_first(void) {
    3.86 -    struct node *begin = create_test_data(3, NULL);
    3.87 +    struct node *begin = create_nodes_test_data(3, NULL);
    3.88      CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin, loc_prev), begin)
    3.89      CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next, loc_prev), begin)
    3.90      CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next->next, loc_prev), begin)
    3.91 -    destroy_test_data(begin);
    3.92 +    destroy_nodes_test_data(begin);
    3.93  }
    3.94  
    3.95  void test_linked_list_last(void) {
    3.96 -    struct node *begin = create_test_data(3, NULL);
    3.97 +    struct node *begin = create_nodes_test_data(3, NULL);
    3.98      struct node *end = begin->next->next;
    3.99      CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin, loc_next), end)
   3.100      CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next, loc_next), end)
   3.101      CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next->next, loc_next), end)
   3.102 -    destroy_test_data(begin);
   3.103 +    destroy_nodes_test_data(begin);
   3.104  }
   3.105  
   3.106  void test_linked_list_prev(void) {
   3.107 -    struct node *begin = create_test_data(3, NULL);
   3.108 +    struct node *begin = create_nodes_test_data(3, NULL);
   3.109      CU_ASSERT_PTR_NULL(cx_linked_list_prev(begin, loc_next, begin))
   3.110      CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next), begin)
   3.111      CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next->next), begin->next)
   3.112 -    destroy_test_data(begin);
   3.113 +    destroy_nodes_test_data(begin);
   3.114  }
   3.115  
   3.116  void test_linked_list_remove(void) {
   3.117      void *begin, *end;
   3.118  
   3.119      int data[] = {2, 4, 6};
   3.120 -    begin = create_test_data(3, data);
   3.121 +    begin = create_nodes_test_data(3, data);
   3.122      struct node *first = begin;
   3.123      struct node *second = first->next;
   3.124      struct node *third = second->next;
   3.125 @@ -483,13 +490,13 @@
   3.126  
   3.127      CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc_next), 0)
   3.128  
   3.129 -    list = create_test_data(5, NULL);
   3.130 +    list = create_nodes_test_data(5, NULL);
   3.131      CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 5)
   3.132 -    destroy_test_data(list);
   3.133 +    destroy_nodes_test_data(list);
   3.134  
   3.135 -    list = create_test_data(13, NULL);
   3.136 +    list = create_nodes_test_data(13, NULL);
   3.137      CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 13)
   3.138 -    destroy_test_data(list);
   3.139 +    destroy_nodes_test_data(list);
   3.140  }
   3.141  
   3.142  void test_linked_list_sort(void) {
   3.143 @@ -510,7 +517,7 @@
   3.144              4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
   3.145      };
   3.146  
   3.147 -    void *begin = create_test_data(100, scrambled);
   3.148 +    void *begin = create_nodes_test_data(100, scrambled);
   3.149      void *end = cx_linked_list_last(begin, loc_next);
   3.150  
   3.151      cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data,
   3.152 @@ -520,7 +527,7 @@
   3.153      struct node *check_last = NULL;
   3.154      CU_ASSERT_PTR_NULL(check->prev)
   3.155      CU_ASSERT_EQUAL(check->data, expected[0])
   3.156 -    for (int i = 0; i < 100; i++) {
   3.157 +    cx_for_n (i, 100) {
   3.158          CU_ASSERT_EQUAL(check->data, expected[i])
   3.159          CU_ASSERT_PTR_EQUAL(check->prev, check_last)
   3.160          if (i < 99) {
   3.161 @@ -532,7 +539,7 @@
   3.162      CU_ASSERT_PTR_NULL(check)
   3.163      CU_ASSERT_PTR_EQUAL(end, check_last)
   3.164  
   3.165 -    destroy_test_data(begin);
   3.166 +    destroy_nodes_test_data(begin);
   3.167  }
   3.168  
   3.169  void test_linked_list_reverse(void) {
   3.170 @@ -541,8 +548,8 @@
   3.171      int data[] = {2, 4, 6, 8};
   3.172      int reversed[] = {8, 6, 4, 2};
   3.173  
   3.174 -    void *list = create_test_data(4, data);
   3.175 -    void *expected = create_test_data(4, reversed);
   3.176 +    void *list = create_nodes_test_data(4, data);
   3.177 +    void *expected = create_nodes_test_data(4, reversed);
   3.178  
   3.179      begin = list;
   3.180      end = cx_linked_list_last(list, loc_next);
   3.181 @@ -553,8 +560,8 @@
   3.182      CU_ASSERT_TRUE(0 == cx_linked_list_compare(begin, expected, loc_next, loc_data,
   3.183                                                 0, cmp_int))
   3.184  
   3.185 -    destroy_test_data(begin);
   3.186 -    destroy_test_data(expected);
   3.187 +    destroy_nodes_test_data(begin);
   3.188 +    destroy_nodes_test_data(expected);
   3.189  }
   3.190  
   3.191  static void verify_linked_list_create(CxList *list) {
   3.192 @@ -582,7 +589,7 @@
   3.193      int data[] = {2, 4, 5, 7, 10, 15};
   3.194  
   3.195      CxList *expected = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
   3.196 -    for (int i = 0; i < 5; i++) cxListAdd(expected, &data[i]);
   3.197 +    cx_for_n (i, 5) cxListAdd(expected, &data[i]);
   3.198  
   3.199      CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 5, data);
   3.200  
   3.201 @@ -598,25 +605,15 @@
   3.202          size_t len,
   3.203          bool write_through
   3.204  ) {
   3.205 -    for (size_t i = 0; i < len; i++) {
   3.206 -        CU_ASSERT_EQUAL(cxListAdd(list, &data[i]), 0)
   3.207 -    }
   3.208 +    cx_for_n (i, len) CU_ASSERT_EQUAL(cxListAdd(list, &data[i]), 0)
   3.209      CU_ASSERT_EQUAL(list->size, len)
   3.210      CU_ASSERT_TRUE(list->capacity >= list->size)
   3.211 -    for (size_t i = 0; i < len; i++) {
   3.212 -        CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
   3.213 -    }
   3.214 -    for (size_t i = 0; i < len; i++) {
   3.215 -        ++data[i];
   3.216 -    }
   3.217 +    cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
   3.218 +    cx_for_n (i, len) ++data[i];
   3.219      if (write_through) {
   3.220 -        for (size_t i = 0; i < len; i++) {
   3.221 -            CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
   3.222 -        }
   3.223 +        cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
   3.224      } else {
   3.225 -        for (size_t i = 0; i < len; i++) {
   3.226 -            CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i] - 1)
   3.227 -        }
   3.228 +        cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i] - 1)
   3.229      }
   3.230      cxListDestroy(list);
   3.231  }
   3.232 @@ -629,42 +626,11 @@
   3.233  
   3.234  void test_hl_ptr_linked_list_add(void) {
   3.235      CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
   3.236 -    int data[] = {5, 47, 13, 9, 18, 1, 42};
   3.237 +    int data[] = {5, 47, 84, 13, 9, 18, 90, 1, 42};
   3.238      verify_hl_linked_list_add(list, data, sizeof(data) / sizeof(int), true);
   3.239  }
   3.240  
   3.241 -void test_hl_linked_list_insert(void) {
   3.242 -    int data;
   3.243 -    CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
   3.244 -
   3.245 -    data = 5;
   3.246 -    CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0)
   3.247 -    CU_ASSERT_EQUAL(list->size, 0)
   3.248 -    CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
   3.249 -    CU_ASSERT_EQUAL(list->size, 1)
   3.250 -    data = 47;
   3.251 -    CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
   3.252 -    CU_ASSERT_EQUAL(list->size, 2)
   3.253 -    data = 13;
   3.254 -    CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0)
   3.255 -    CU_ASSERT_EQUAL(list->size, 3)
   3.256 -    data = 42;
   3.257 -    CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0)
   3.258 -
   3.259 -    CU_ASSERT_EQUAL(list->size, 4)
   3.260 -    CU_ASSERT_TRUE(list->capacity >= list->size)
   3.261 -
   3.262 -    int exp[] = {47, 13, 5, 42};
   3.263 -    CxList *expected = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 4, exp);
   3.264 -    CU_ASSERT_TRUE(0 == cxListCompare(list, expected))
   3.265 -
   3.266 -    cxListDestroy(list);
   3.267 -    cxListDestroy(expected);
   3.268 -}
   3.269 -
   3.270 -void test_hl_ptr_linked_list_insert(void) {
   3.271 -    CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
   3.272 -
   3.273 +static void verify_hl_linked_list_insert(CxList *list) {
   3.274      int a = 5, b = 47, c = 13, d = 42;
   3.275  
   3.276      CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0)
   3.277 @@ -688,11 +654,15 @@
   3.278      cxListDestroy(list);
   3.279  }
   3.280  
   3.281 -void test_hl_linked_list_remove(void) {
   3.282 -    int data[] = {5, 47, 42, 13};
   3.283 -    CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
   3.284 -                                         sizeof(int), 4, data);
   3.285 +void test_hl_linked_list_insert(void) {
   3.286 +    verify_hl_linked_list_insert(cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)));
   3.287 +}
   3.288  
   3.289 +void test_hl_ptr_linked_list_insert(void) {
   3.290 +    verify_hl_linked_list_insert(cxPointerLinkedListCreate(cxTestingAllocator, cmp_int));
   3.291 +}
   3.292 +
   3.293 +static void verify_hl_linked_list_remove(CxList *list) {
   3.294      CU_ASSERT_EQUAL(list->size, 4)
   3.295      CU_ASSERT_TRUE(list->capacity >= list->size)
   3.296  
   3.297 @@ -725,187 +695,131 @@
   3.298      cxListDestroy(list);
   3.299  }
   3.300  
   3.301 +void test_hl_linked_list_remove(void) {
   3.302 +    int data[] = {5, 47, 42, 13};
   3.303 +    CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
   3.304 +                                         sizeof(int), 4, data);
   3.305 +    verify_hl_linked_list_remove(list);
   3.306 +}
   3.307 +
   3.308  void test_hl_ptr_linked_list_remove(void) {
   3.309      int a = 5, b = 47, c = 42, d = 13;
   3.310      CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
   3.311 -
   3.312      cxListAdd(list, &a);
   3.313      cxListAdd(list, &b);
   3.314      cxListAdd(list, &c);
   3.315      cxListAdd(list, &d);
   3.316 +    verify_hl_linked_list_remove(list);
   3.317 +}
   3.318  
   3.319 -    CU_ASSERT_EQUAL(list->size, 4)
   3.320 -    CU_ASSERT_TRUE(list->capacity >= list->size)
   3.321 -
   3.322 -    CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
   3.323 -
   3.324 -    CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
   3.325 -    CU_ASSERT_EQUAL(list->size, 3)
   3.326 -    CU_ASSERT_TRUE(list->capacity >= list->size)
   3.327 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
   3.328 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
   3.329 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
   3.330 -
   3.331 -    CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
   3.332 -    CU_ASSERT_EQUAL(list->size, 2)
   3.333 -    CU_ASSERT_TRUE(list->capacity >= list->size)
   3.334 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   3.335 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
   3.336 -
   3.337 -    CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
   3.338 -    CU_ASSERT_EQUAL(list->size, 1)
   3.339 -    CU_ASSERT_TRUE(list->capacity >= list->size)
   3.340 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   3.341 -
   3.342 -    CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
   3.343 -    CU_ASSERT_EQUAL(list->size, 0)
   3.344 -    CU_ASSERT_TRUE(list->capacity >= list->size)
   3.345 -
   3.346 -    CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
   3.347 -
   3.348 +static void verify_hl_linked_list_at(
   3.349 +        CxList *list,
   3.350 +        size_t len,
   3.351 +        int *data
   3.352 +) {
   3.353 +    CU_ASSERT_EQUAL(list->size, len)
   3.354 +    cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i])
   3.355 +    CU_ASSERT_PTR_NULL(cxListAt(list, len))
   3.356 +    free(data);
   3.357      cxListDestroy(list);
   3.358  }
   3.359  
   3.360  void test_hl_linked_list_at(void) {
   3.361 -    int data[] = {5, 47, 13};
   3.362 +    size_t len = 100;
   3.363 +    int *data = create_ints_test_data(len);
   3.364      CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
   3.365 -                                         sizeof(int), 3, data);
   3.366 -
   3.367 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
   3.368 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
   3.369 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
   3.370 -    CU_ASSERT_PTR_NULL(cxListAt(list, 3))
   3.371 -
   3.372 -    cxListDestroy(list);
   3.373 +                                         sizeof(int), len, data);
   3.374 +    verify_hl_linked_list_at(list, len, data);
   3.375  }
   3.376  
   3.377  void test_hl_ptr_linked_list_at(void) {
   3.378 +    size_t len = 250;
   3.379 +    int *data = create_ints_test_data(len);
   3.380      CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
   3.381 +    cx_for_n (i, len) cxListAdd(list, &data[i]);
   3.382 +    verify_hl_linked_list_at(list, len, data);
   3.383 +}
   3.384  
   3.385 -    int a = 5, b = 47, c = 13;
   3.386 -    cxListAdd(list, &a);
   3.387 -    cxListAdd(list, &b);
   3.388 -    cxListAdd(list, &c);
   3.389 -
   3.390 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
   3.391 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
   3.392 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
   3.393 -    CU_ASSERT_PTR_NULL(cxListAt(list, 3))
   3.394 -
   3.395 +static void verify_hl_linked_list_find(
   3.396 +        CxList *list,
   3.397 +        size_t len,
   3.398 +        int *data
   3.399 +) {
   3.400 +    cx_for_n (attempt, 100) {
   3.401 +        size_t exp = rand() % len; // NOLINT(cert-msc50-cpp)
   3.402 +        int val = data[exp];
   3.403 +        cx_for_n (i, exp) {
   3.404 +            if (data[i] == val) {
   3.405 +                exp = i;
   3.406 +                break;
   3.407 +            }
   3.408 +        }
   3.409 +        CU_ASSERT_EQUAL(cxListFind(list, &val), exp)
   3.410 +    }
   3.411 +    free(data);
   3.412      cxListDestroy(list);
   3.413  }
   3.414  
   3.415  void test_hl_linked_list_find(void) {
   3.416 -    int data[] = {5, 47, 13};
   3.417 -    CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
   3.418 -                                         sizeof(int), 3, data);
   3.419 -    CU_ASSERT_EQUAL(list->size, 3)
   3.420 -    CU_ASSERT_TRUE(list->capacity >= list->size)
   3.421 -
   3.422 -    int criteria;
   3.423 -
   3.424 -    criteria = 5;
   3.425 -    CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
   3.426 -    criteria = 47;
   3.427 -    CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
   3.428 -    criteria = 13;
   3.429 -    CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
   3.430 -    criteria = 9000;
   3.431 -    CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
   3.432 -    criteria = -5;
   3.433 -    CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
   3.434 -
   3.435 -    cxListDestroy(list);
   3.436 +    size_t len = 100;
   3.437 +    int *data = create_ints_test_data(len);
   3.438 +    CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), len, data);
   3.439 +    verify_hl_linked_list_find(list, len, data);
   3.440  }
   3.441  
   3.442  void test_hl_ptr_linked_list_find(void) {
   3.443 -    int a = 5, b = 47, c = 13, criteria;
   3.444 +    size_t len = 250;
   3.445 +    int *data = create_ints_test_data(len);
   3.446      CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
   3.447 +    cx_for_n (i, len) cxListAdd(list, &data[i]);
   3.448 +    verify_hl_linked_list_find(list, len, data);
   3.449 +}
   3.450  
   3.451 -    cxListAdd(list, &a);
   3.452 -    cxListAdd(list, &b);
   3.453 -    cxListAdd(list, &c);
   3.454 +struct sort_test_data {
   3.455 +    size_t len;
   3.456 +    int *data;
   3.457 +    int *sorted;
   3.458 +};
   3.459  
   3.460 -    CU_ASSERT_EQUAL(list->size, 3)
   3.461 -    CU_ASSERT_TRUE(list->capacity >= list->size)
   3.462 +static struct sort_test_data create_sort_test_data(void) {
   3.463 +    size_t len = 1000;
   3.464 +    int *data = create_ints_test_data(len);
   3.465 +    int *sorted = malloc(sizeof(int) * len);
   3.466 +    memcpy(sorted, data, sizeof(int) * len);
   3.467 +    qsort(sorted, len, sizeof(int), cmp_int);
   3.468 +    struct sort_test_data s = {len, data, sorted};
   3.469 +    return s;
   3.470 +}
   3.471  
   3.472 -    criteria = 5;
   3.473 -    CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
   3.474 -    criteria = 47;
   3.475 -    CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
   3.476 -    criteria = 13;
   3.477 -    CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
   3.478 -    criteria = 9000;
   3.479 -    CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
   3.480 -    criteria = -5;
   3.481 -    CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
   3.482 -    b = -5;
   3.483 -    CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
   3.484 +static void free_sort_test_data(struct sort_test_data s) {
   3.485 +    free(s.data);
   3.486 +    free(s.sorted);
   3.487 +}
   3.488  
   3.489 +static void verify_hl_linked_list_sort(
   3.490 +        CxList *list,
   3.491 +        struct sort_test_data td
   3.492 +) {
   3.493 +    cxListSort(list);
   3.494 +    cx_for_n (i, td.len) CU_ASSERT_EQUAL_FATAL(*(int *) cxListAt(list, i), td.sorted[i])
   3.495 +    free_sort_test_data(td);
   3.496      cxListDestroy(list);
   3.497  }
   3.498  
   3.499  void test_hl_linked_list_sort(void) {
   3.500 -    int expected[] = {
   3.501 -            14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
   3.502 -            894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
   3.503 -            1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
   3.504 -            2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
   3.505 -            3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
   3.506 -            4785, 4791, 4801, 4859, 4903, 4973
   3.507 -    };
   3.508 -    int scrambled[] = {
   3.509 -            759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
   3.510 -            2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
   3.511 -            2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
   3.512 -            894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
   3.513 -            3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
   3.514 -            4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
   3.515 -    };
   3.516 -    CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, scrambled);
   3.517 -    CxList *exp = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, expected);
   3.518 -
   3.519 -    cxListSort(list);
   3.520 -    CU_ASSERT_TRUE(0 == cxListCompare(list, exp))
   3.521 -
   3.522 -    cxListDestroy(list);
   3.523 -    cxListDestroy(exp);
   3.524 +    struct sort_test_data td = create_sort_test_data();
   3.525 +    CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), td.len, td.data);
   3.526 +    verify_hl_linked_list_sort(list, td);
   3.527  }
   3.528  
   3.529  void test_hl_ptr_linked_list_sort(void) {
   3.530 -    int expected[] = {
   3.531 -            14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
   3.532 -            894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
   3.533 -            1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
   3.534 -            2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
   3.535 -            3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
   3.536 -            4785, 4791, 4801, 4859, 4903, 4973
   3.537 -    };
   3.538 -    int scrambled[] = {
   3.539 -            759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
   3.540 -            2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
   3.541 -            2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
   3.542 -            894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
   3.543 -            3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
   3.544 -            4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
   3.545 -    };
   3.546 -
   3.547 +    struct sort_test_data td = create_sort_test_data();
   3.548      CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
   3.549 -
   3.550 -    for (int i = 0; i < 100; i++) {
   3.551 -        cxListAdd(list, &scrambled[i]);
   3.552 -    }
   3.553 -
   3.554 -    cxListSort(list);
   3.555 -
   3.556 -    for (int i = 0; i < 100; i++) {
   3.557 -        CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
   3.558 -    }
   3.559 -
   3.560 -    cxListDestroy(list);
   3.561 +    cx_for_n (i, td.len) cxListAdd(list, &td.data[i]);
   3.562 +    verify_hl_linked_list_sort(list, td);
   3.563  }
   3.564  
   3.565 -void test_hl_linked_list_iterator_impl(CxList *list) {
   3.566 +void verify_hl_linked_list_iterator(CxList *list) {
   3.567      int i = 0;
   3.568      CxIterator iter = cxListBegin(list);
   3.569      cx_foreach(int*, x, iter) {
   3.570 @@ -916,111 +830,75 @@
   3.571      }
   3.572      CU_ASSERT_EQUAL(i, 10)
   3.573      CU_ASSERT_EQUAL_FATAL(list->size, 5)
   3.574 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 0)
   3.575 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 2)
   3.576 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 4)
   3.577 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 6)
   3.578 -    CU_ASSERT_EQUAL(*(int *) cxListAt(list, 4), 8)
   3.579 +    cx_for_n(j, 5) CU_ASSERT_EQUAL(*(int *) cxListAt(list, j), (int) j * 2)
   3.580      cxListDestroy(list);
   3.581  }
   3.582  
   3.583  void test_hl_linked_list_iterator(void) {
   3.584      CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
   3.585 -    for (int i = 0; i < 10; i++) {
   3.586 -        cxListAdd(list, &i);
   3.587 -    }
   3.588 -    test_hl_linked_list_iterator_impl(list);
   3.589 +    cx_for_n (i, 10) cxListAdd(list, &i);
   3.590 +    verify_hl_linked_list_iterator(list);
   3.591  }
   3.592  
   3.593  void test_hl_ptr_linked_list_iterator(void) {
   3.594      CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
   3.595      int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
   3.596 -    for (int i = 0; i < 10; i++) {
   3.597 -        cxListAdd(list, &data[i]);
   3.598 -    }
   3.599 -    test_hl_linked_list_iterator_impl(list);
   3.600 +    cx_for_n (i, 10) cxListAdd(list, &data[i]);
   3.601 +    verify_hl_linked_list_iterator(list);
   3.602  }
   3.603  
   3.604 -void test_hl_linked_list_insert_via_iterator(void) {
   3.605 -    CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
   3.606 -    for (int i = 0; i < 5; i++) {
   3.607 -        cxListAdd(list, &i);
   3.608 -    }
   3.609 +static void verify_hl_linked_list_insert_via_iterator(
   3.610 +        CxList *list,
   3.611 +        int *testdata
   3.612 +) {
   3.613      CxIterator iter = cxListIterator(list, 2);
   3.614      CU_ASSERT_EQUAL(iter.index, 2)
   3.615      CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
   3.616 +    size_t i = 4;
   3.617  
   3.618 -    int data = 10;
   3.619 -    cxListInsertAfter(&iter, &data);
   3.620 +    ++i;
   3.621 +    cxListInsertAfter(&iter, &testdata[i]);
   3.622      CU_ASSERT_EQUAL(iter.index, 2)
   3.623      CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
   3.624 -    data = 20;
   3.625 -    cxListInsertBefore(&iter, &data);
   3.626 +    ++i;
   3.627 +    cxListInsertBefore(&iter, &testdata[i]);
   3.628      CU_ASSERT_EQUAL(iter.index, 3)
   3.629      CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
   3.630  
   3.631 -    data = 30;
   3.632      iter = cxListBegin(list);
   3.633 -    cxListInsertBefore(&iter, &data);
   3.634 +    ++i;
   3.635 +    cxListInsertBefore(&iter, &testdata[i]);
   3.636      CU_ASSERT_EQUAL(iter.index, 1)
   3.637      CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0)
   3.638 -    data = 40;
   3.639      iter = cxListIterator(list, list->size);
   3.640 -    cxListInsertBefore(&iter, &data);
   3.641 +    ++i;
   3.642 +    cxListInsertBefore(&iter, &testdata[i]);
   3.643      CU_ASSERT_EQUAL(iter.index, 9)
   3.644      CU_ASSERT_FALSE(cxIteratorValid(&iter))
   3.645 -    data = 50;
   3.646      iter = cxListIterator(list, list->size);
   3.647 -    cxListInsertAfter(&iter, &data);
   3.648 +    ++i;
   3.649 +    cxListInsertAfter(&iter, &testdata[i]);
   3.650      CU_ASSERT_EQUAL(iter.index, 10)
   3.651      CU_ASSERT_FALSE(cxIteratorValid(&iter))
   3.652  
   3.653      int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};
   3.654 -    CxList *expected = cxLinkedListFromArray(cxTestingAllocator,
   3.655 -                                             cmp_int, sizeof(int), 10, expdata);
   3.656 +    cx_for_n (j, 10) CU_ASSERT_EQUAL(*(int *) cxListAt(list, j), expdata[j])
   3.657 +    cxListDestroy(list);
   3.658 +}
   3.659  
   3.660 -    CU_ASSERT_EQUAL(0, cxListCompare(list, expected))
   3.661 -    cxListDestroy(list);
   3.662 -    cxListDestroy(expected);
   3.663 +void test_hl_linked_list_insert_via_iterator(void) {
   3.664 +    int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50};
   3.665 +    // only add the first five elements, the remaining five will be inserted
   3.666 +    CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 5, testdata);
   3.667 +    verify_hl_linked_list_insert_via_iterator(list, testdata);
   3.668  }
   3.669  
   3.670  void test_hl_ptr_linked_list_insert_via_iterator(void) {
   3.671      int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50};
   3.672      CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
   3.673 -    int i;
   3.674 -    for (i = 0; i < 5; i++) {
   3.675 -        cxListAdd(list, &testdata[i]);
   3.676 -    }
   3.677 -    CxIterator iter = cxListIterator(list, 2);
   3.678 -    CU_ASSERT_EQUAL(iter.index, 2)
   3.679 -    CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
   3.680 -
   3.681 -    cxListInsertAfter(&iter, &testdata[i++]);
   3.682 -    CU_ASSERT_EQUAL(iter.index, 2)
   3.683 -    CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
   3.684 -    cxListInsertBefore(&iter, &testdata[i++]);
   3.685 -    CU_ASSERT_EQUAL(iter.index, 3)
   3.686 -    CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
   3.687 -
   3.688 -    iter = cxListBegin(list);
   3.689 -    cxListInsertBefore(&iter, &testdata[i++]);
   3.690 -    CU_ASSERT_EQUAL(iter.index, 1)
   3.691 -    CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0)
   3.692 -    iter = cxListIterator(list, list->size);
   3.693 -    cxListInsertBefore(&iter, &testdata[i++]);
   3.694 -    CU_ASSERT_EQUAL(iter.index, 9)
   3.695 -    CU_ASSERT_FALSE(cxIteratorValid(&iter))
   3.696 -    iter = cxListIterator(list, list->size);
   3.697 -    cxListInsertAfter(&iter, &testdata[i++]);
   3.698 -    CU_ASSERT_EQUAL(iter.index, 10)
   3.699 -    CU_ASSERT_FALSE(cxIteratorValid(&iter))
   3.700 -
   3.701 -    int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};
   3.702 -    for (i = 0; i < 10; i++) {
   3.703 -        CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expdata[i])
   3.704 -    }
   3.705 -
   3.706 -    cxListDestroy(list);
   3.707 +    // only add the first five elements, the remaining five will be inserted
   3.708 +    cx_for_n (i, 5) cxListAdd(list, &testdata[i]);
   3.709 +    verify_hl_linked_list_insert_via_iterator(list, testdata);
   3.710  }
   3.711  
   3.712  static void test_setup_allocator(void) {

mercurial