test/test_list.c

Tue, 28 Dec 2021 14:25:05 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Dec 2021 14:25:05 +0100
changeset 487
4bd19279778c
parent 486
d7ca126eab7f
child 488
9138acaa494b
permissions
-rw-r--r--

use c99 bool + add test for low level find

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "cx/linked_list.h"
    30 #include "test_config.h"
    31 #include "util_allocator.h"
    33 int cmp_int(
    34         int const *l,
    35         int const *r
    36 ) {
    37     int left = *l, right = *r;
    38     return left == right ? 0 : (left < right ? -1 : 1);
    39 }
    41 struct node {
    42     struct node *next;
    43     struct node *prev;
    44     int data;
    45 };
    47 #define nd(name) name = {0}
    49 const ptrdiff_t loc_prev = offsetof(struct node, prev);
    50 const ptrdiff_t loc_next = offsetof(struct node, next);
    51 const ptrdiff_t loc_data = offsetof(struct node, data);
    53 struct node *create_test_data(
    54         size_t n,
    55         const int data[]
    56 ) {
    57     if (n == 0) return NULL;
    58     struct node *begin = calloc(1, sizeof(struct node));
    59     struct node *prev = begin;
    60     if (data) begin->data = data[0];
    61     for (size_t i = 1; i < n; i++) {
    62         struct node *node = calloc(1, sizeof(struct node));
    63         if (data) node->data = data[i];
    64         cx_linked_list_link(prev, node, loc_prev, loc_next);
    65         prev = node;
    66     }
    67     return begin;
    68 }
    70 void destroy_test_data(struct node *begin) {
    71     struct node *node = begin;
    72     while (node) {
    73         struct node *next = node->next;
    74         free(node);
    75         node = next;
    76     }
    77 }
    79 void test_linked_list_link_unlink(void) {
    81     struct node nd(a), nd(b), nd(c);
    83     cx_linked_list_link(&a, &b, loc_prev, loc_next);
    84     CU_ASSERT_PTR_NULL(a.prev)
    85     CU_ASSERT_PTR_EQUAL(a.next, &b)
    86     CU_ASSERT_PTR_EQUAL(b.prev, &a)
    87     CU_ASSERT_PTR_NULL(b.next)
    89     cx_linked_list_unlink(&a, &b, loc_prev, loc_next);
    90     CU_ASSERT_PTR_NULL(a.prev)
    91     CU_ASSERT_PTR_NULL(a.next)
    92     CU_ASSERT_PTR_NULL(b.prev)
    93     CU_ASSERT_PTR_NULL(b.next)
    95     cx_linked_list_link(&b, &c, loc_prev, loc_next);
    96     cx_linked_list_link(&a, &b, loc_prev, loc_next);
    97     cx_linked_list_unlink(&b, &c, loc_prev, loc_next);
    98     CU_ASSERT_PTR_NULL(a.prev)
    99     CU_ASSERT_PTR_EQUAL(a.next, &b)
   100     CU_ASSERT_PTR_EQUAL(b.prev, &a)
   101     CU_ASSERT_PTR_NULL(b.next)
   102     CU_ASSERT_PTR_NULL(c.prev)
   103     CU_ASSERT_PTR_NULL(c.next)
   104 }
   106 void test_linked_list_at(void) {
   107     struct node nd(a), nd(b), nd(c), nd(d);
   108     cx_linked_list_link(&a, &b, loc_prev, loc_next);
   109     cx_linked_list_link(&b, &c, loc_prev, loc_next);
   110     cx_linked_list_link(&c, &d, loc_prev, loc_next);
   112     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 0), &a)
   113     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 1), &b)
   114     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 2), &c)
   115     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 3), &d)
   116     CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4))
   118     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_prev, 0), &a)
   119     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 1), &b)
   120     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 2), &c)
   121     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 3), &d)
   122     CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4))
   124     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 0), &a)
   125     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b)
   126 }
   128 void test_linked_list_find(void) {
   129     int data[] = {2, 4, 6, 8};
   130     void *list = create_test_data(4, data);
   131     int s;
   133     s = 2;
   134     CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
   135                                         false, (CxListComparator) cmp_int, &s), 0)
   136     s = 4;
   137     CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
   138                                         false, (CxListComparator) cmp_int, &s), 1)
   139     s = 6;
   140     CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
   141                                         false, (CxListComparator) cmp_int, &s), 2)
   142     s = 8;
   143     CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
   144                                         false, (CxListComparator) cmp_int, &s), 3)
   145     s = 10;
   146     CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
   147                                         false, (CxListComparator) cmp_int, &s), 4)
   148     s = -2;
   149     CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
   150                                         false, (CxListComparator) cmp_int, &s), 4)
   151 }
   153 void test_linked_list_compare(void) {
   154     int a[] = {2, 4, 6, 8};
   155     int b[] = {2, 4, 6};
   156     int c[] = {2, 4, 6, 9};
   158     void *la = create_test_data(4, a);
   159     void *lb = create_test_data(3, b);
   160     void *lc = create_test_data(4, c);
   162     CU_ASSERT_TRUE(0 < cx_linked_list_compare(la, lb, loc_next, loc_data,
   163                                               false, (CxListComparator) cmp_int)
   164     )
   165     CU_ASSERT_TRUE(0 > cx_linked_list_compare(lb, la, loc_next, loc_data,
   166                                               false, (CxListComparator) cmp_int)
   167     )
   168     CU_ASSERT_TRUE(0 < cx_linked_list_compare(lc, la, loc_next, loc_data,
   169                                               false, (CxListComparator) cmp_int)
   170     )
   171     CU_ASSERT_TRUE(0 > cx_linked_list_compare(la, lc, loc_next, loc_data,
   172                                               false, (CxListComparator) cmp_int)
   173     )
   174     CU_ASSERT_TRUE(0 == cx_linked_list_compare(la, la, loc_next, loc_data,
   175                                                false, (CxListComparator) cmp_int)
   176     )
   178     destroy_test_data(la);
   179     destroy_test_data(lb);
   180     destroy_test_data(lc);
   181 }
   183 void test_linked_list_add(void) {
   184     struct node nodes[4];
   185     void *begin, *end;
   187     // test with begin, end / prev, next
   188     memset(nodes, 0, 4 * sizeof(struct node));
   189     begin = end = NULL;
   191     cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
   192     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   193     CU_ASSERT_PTR_EQUAL(end, &nodes[0])
   194     CU_ASSERT_PTR_NULL(nodes[0].prev)
   195     CU_ASSERT_PTR_NULL(nodes[0].next)
   197     cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
   198     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   199     CU_ASSERT_PTR_EQUAL(end, &nodes[1])
   200     CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
   201     CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
   203     // test with begin only / prev, next
   204     memset(nodes, 0, 4 * sizeof(struct node));
   205     begin = end = NULL;
   207     cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]);
   208     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   209     cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]);
   210     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   211     CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
   212     CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
   214     cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]);
   215     CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
   216     CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
   218     // test with end only / prev, next
   219     memset(nodes, 0, 4 * sizeof(struct node));
   220     begin = end = NULL;
   222     cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[0]);
   223     CU_ASSERT_PTR_EQUAL(end, &nodes[0])
   224     cx_linked_list_add(NULL, &end,  loc_prev, loc_next, &nodes[1]);
   225     CU_ASSERT_PTR_EQUAL(end, &nodes[1])
   226     CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
   227     CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
   229     cx_linked_list_add(NULL, &end,  loc_prev, loc_next, &nodes[2]);
   230     CU_ASSERT_PTR_EQUAL(end, &nodes[2])
   231     CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
   232     CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
   234     // test with begin, end / next
   235     memset(nodes, 0, 4 * sizeof(struct node));
   236     begin = end = NULL;
   238     cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
   239     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   240     CU_ASSERT_PTR_EQUAL(end, &nodes[0])
   241     cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
   242     CU_ASSERT_PTR_EQUAL(end, &nodes[1])
   243     CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
   244     CU_ASSERT_PTR_NULL(nodes[1].prev)
   245 }
   247 void test_linked_list_prepend(void) {
   248     struct node nodes[4];
   249     void *begin, *end;
   251     // test with begin, end / prev, next
   252     memset(nodes, 0, 4 * sizeof(struct node));
   253     begin = end = NULL;
   255     cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[0]);
   256     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   257     CU_ASSERT_PTR_EQUAL(end, &nodes[0])
   258     CU_ASSERT_PTR_NULL(nodes[0].prev)
   259     CU_ASSERT_PTR_NULL(nodes[0].next)
   261     cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[1]);
   262     CU_ASSERT_PTR_EQUAL(begin, &nodes[1])
   263     CU_ASSERT_PTR_EQUAL(end, &nodes[0])
   264     CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
   265     CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
   267     // test with begin only / prev, next
   268     memset(nodes, 0, 4 * sizeof(struct node));
   269     begin = end = NULL;
   271     cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[0]);
   272     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   273     cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[1]);
   274     CU_ASSERT_PTR_EQUAL(begin, &nodes[1])
   275     CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
   276     CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
   278     cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[2]);
   279     CU_ASSERT_PTR_EQUAL(begin, &nodes[2])
   280     CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
   281     CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2])
   283     // test with end only / prev, next
   284     memset(nodes, 0, 4 * sizeof(struct node));
   285     begin = end = NULL;
   287     cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[0]);
   288     CU_ASSERT_PTR_EQUAL(end, &nodes[0])
   289     cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[1]);
   290     CU_ASSERT_PTR_EQUAL(end, &nodes[0])
   291     CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
   292     CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
   294     cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[2]);
   295     CU_ASSERT_PTR_EQUAL(end, &nodes[0])
   296     CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
   297     CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2])
   299     // test with begin, end / next
   300     memset(nodes, 0, 4 * sizeof(struct node));
   301     begin = end = NULL;
   303     cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[0]);
   304     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   305     CU_ASSERT_PTR_EQUAL(end, &nodes[0])
   306     cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[1]);
   307     cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[2]);
   308     CU_ASSERT_PTR_EQUAL(begin, &nodes[2])
   309     CU_ASSERT_PTR_EQUAL(end, &nodes[0])
   310     CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
   311     CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
   312     CU_ASSERT_PTR_NULL(nodes[1].prev)
   313     CU_ASSERT_PTR_NULL(nodes[0].prev)
   314 }
   316 void test_linked_list_insert(void) {
   317     struct node nodes[4];
   318     void *begin, *end;
   320     // insert mid list
   321     memset(nodes, 0, 4 * sizeof(struct node));
   322     begin = &nodes[0];
   323     end = &nodes[2];
   325     cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
   326     cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
   328     cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3]);
   329     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   330     CU_ASSERT_PTR_EQUAL(end, &nodes[2])
   331     CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[3])
   332     CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[3])
   333     CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[1])
   334     CU_ASSERT_PTR_EQUAL(nodes[3].next, &nodes[2])
   336     // insert end
   337     memset(nodes, 0, 4 * sizeof(struct node));
   338     begin = &nodes[0];
   339     end = &nodes[2];
   341     cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
   342     cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
   344     cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3]);
   345     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   346     CU_ASSERT_PTR_EQUAL(end, &nodes[3])
   347     CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[3])
   348     CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[2])
   349     CU_ASSERT_PTR_NULL(nodes[3].next)
   351     // insert begin
   352     memset(nodes, 0, 4 * sizeof(struct node));
   353     begin = &nodes[0];
   354     end = &nodes[2];
   356     cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
   357     cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
   359     cx_linked_list_insert(&begin, &end, loc_prev, loc_next, NULL, &nodes[3]);
   360     CU_ASSERT_PTR_EQUAL(begin, &nodes[3])
   361     CU_ASSERT_PTR_EQUAL(end, &nodes[2])
   362     CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[3])
   363     CU_ASSERT_PTR_NULL(nodes[3].prev)
   364     CU_ASSERT_PTR_EQUAL(nodes[3].next, &nodes[0])
   365 }
   367 void test_linked_list_insert_chain(void) {
   368     struct node nodes[5];
   369     void *begin, *end;
   371     // insert mid list
   372     memset(nodes, 0, 5 * sizeof(struct node));
   373     begin = &nodes[0];
   374     end = &nodes[2];
   376     cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
   377     cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
   378     cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
   380     cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3], NULL);
   381     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   382     CU_ASSERT_PTR_EQUAL(end, &nodes[2])
   383     CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[3])
   384     CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[4])
   385     CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[1])
   386     CU_ASSERT_PTR_EQUAL(nodes[4].next, &nodes[2])
   388     // insert end
   389     memset(nodes, 0, 5 * sizeof(struct node));
   390     begin = &nodes[0];
   391     end = &nodes[2];
   393     cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
   394     cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
   395     cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
   397     cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3], NULL);
   398     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   399     CU_ASSERT_PTR_EQUAL(end, &nodes[4])
   400     CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[3])
   401     CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[2])
   402     CU_ASSERT_PTR_NULL(nodes[4].next)
   404     // insert begin
   405     memset(nodes, 0, 5 * sizeof(struct node));
   406     begin = &nodes[0];
   407     end = &nodes[2];
   409     cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
   410     cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
   411     cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
   413     cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, NULL, &nodes[3], NULL);
   414     CU_ASSERT_PTR_EQUAL(begin, &nodes[3])
   415     CU_ASSERT_PTR_EQUAL(end, &nodes[2])
   416     CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[4])
   417     CU_ASSERT_PTR_NULL(nodes[3].prev)
   418     CU_ASSERT_PTR_EQUAL(nodes[4].next, &nodes[0])
   419 }
   421 void test_linked_list_first(void) {
   422     struct node *begin = create_test_data(3, NULL);
   423     CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin, loc_prev), begin)
   424     CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next, loc_prev), begin)
   425     CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next->next, loc_prev), begin)
   426     destroy_test_data(begin);
   427 }
   429 void test_linked_list_last(void) {
   430     struct node *begin = create_test_data(3, NULL);
   431     struct node *end = begin->next->next;
   432     CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin, loc_next), end)
   433     CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next, loc_next), end)
   434     CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next->next, loc_next), end)
   435     destroy_test_data(begin);
   436 }
   438 void test_linked_list_prev(void) {
   439     struct node *begin = create_test_data(3, NULL);
   440     CU_ASSERT_PTR_NULL(cx_linked_list_prev(begin, loc_next, begin))
   441     CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next), begin)
   442     CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next->next), begin->next)
   443     destroy_test_data(begin);
   444 }
   446 void test_linked_list_remove(void) {
   447     void *begin, *end;
   449     int data[] = {2, 4, 6};
   450     begin = create_test_data(3, data);
   451     struct node *first = begin;
   452     struct node *second = first->next;
   453     struct node *third = second->next;
   454     end = third;
   456     cx_linked_list_remove(&begin, &end, loc_prev, loc_next, second);
   457     CU_ASSERT_PTR_EQUAL(begin, first)
   458     CU_ASSERT_PTR_EQUAL(end, third)
   459     CU_ASSERT_PTR_NULL(first->prev)
   460     CU_ASSERT_PTR_EQUAL(first->next, third)
   461     CU_ASSERT_PTR_EQUAL(third->prev, first)
   462     CU_ASSERT_PTR_NULL(third->next)
   464     cx_linked_list_remove(&begin, &end, loc_prev, loc_next, third);
   465     CU_ASSERT_PTR_EQUAL(begin, first)
   466     CU_ASSERT_PTR_EQUAL(end, first)
   467     CU_ASSERT_PTR_NULL(first->prev)
   468     CU_ASSERT_PTR_NULL(first->next)
   470     cx_linked_list_remove(&begin, &end, loc_prev, loc_next, first);
   471     CU_ASSERT_PTR_NULL(begin)
   472     CU_ASSERT_PTR_NULL(end)
   474     free(first);
   475     free(second);
   476     free(third);
   477 }
   479 void test_linked_list_size(void) {
   480     struct node *list;
   482     CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc_next), 0)
   484     list = create_test_data(5, NULL);
   485     CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 5)
   486     destroy_test_data(list);
   488     list = create_test_data(13, NULL);
   489     CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 13)
   490     destroy_test_data(list);
   491 }
   493 void test_linked_list_sort(void) {
   494     int expected[] = {
   495             14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
   496             894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
   497             1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
   498             2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
   499             3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
   500             4785, 4791, 4801, 4859, 4903, 4973
   501     };
   502     int scrambled[] = {
   503             759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
   504             2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
   505             2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
   506             894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
   507             3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
   508             4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
   509     };
   511     void *begin = create_test_data(100, scrambled);
   512     void *end = cx_linked_list_last(begin, loc_next);
   514     cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data,
   515                         false, (CxListComparator) cmp_int);
   517     struct node *check = begin;
   518     struct node *check_last = NULL;
   519     CU_ASSERT_PTR_NULL(check->prev)
   520     CU_ASSERT_EQUAL(check->data, expected[0])
   521     for (int i = 0; i < 100; i++) {
   522         CU_ASSERT_EQUAL(check->data, expected[i])
   523         CU_ASSERT_PTR_EQUAL(check->prev, check_last)
   524         if (i < 99) {
   525             CU_ASSERT_PTR_NOT_NULL(check->next)
   526         }
   527         check_last = check;
   528         check = check->next;
   529     }
   530     CU_ASSERT_PTR_NULL(check)
   531     CU_ASSERT_PTR_EQUAL(end, check_last)
   533     destroy_test_data(begin);
   534 }
   536 void test_linked_list_reverse(void) {
   537     void *begin, *end;
   539     int data[] = {2, 4, 6, 8};
   540     int reversed[] = {8, 6, 4, 2};
   542     void *list = create_test_data(4, data);
   543     void *expected = create_test_data(4, reversed);
   545     begin = list;
   546     end = cx_linked_list_last(list, loc_next);
   548     cx_linked_list_reverse(&begin, &end, loc_prev, loc_next);
   549     CU_ASSERT_PTR_EQUAL(end, list)
   550     CU_ASSERT_PTR_EQUAL(begin, cx_linked_list_first(end, loc_prev))
   551     CU_ASSERT_TRUE(0 == cx_linked_list_compare(begin, expected, loc_next, loc_data,
   552                                                0, (CxListComparator) cmp_int))
   554     destroy_test_data(begin);
   555     destroy_test_data(expected);
   556 }
   558 void test_hl_linked_list_create(void) {
   559     cxTestingAllocatorReset();
   561     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   563     CU_ASSERT_EQUAL(list->size, 0)
   564     CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
   565     CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
   566     CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
   567     CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
   569     cxLinkedListDestroy(list);
   570     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   571 }
   573 void test_hl_linked_list_add(void) {
   574     cxTestingAllocatorReset();
   576     int data;
   577     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   579     data = 5;
   580     CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
   581     data = 47;
   582     CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
   583     data = 13;
   584     CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
   586     CU_ASSERT_EQUAL(list->size, 3)
   587     CU_ASSERT_TRUE(list->capacity >= list->size)
   589     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
   590     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
   591     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
   593     cxLinkedListDestroy(list);
   594     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   595 }
   597 void test_hl_linked_list_insert(void) {
   598     cxTestingAllocatorReset();
   600     int data;
   601     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   603     data = 5;
   604     CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0)
   605     CU_ASSERT_EQUAL(list->size, 0)
   606     CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
   607     CU_ASSERT_EQUAL(list->size, 1)
   608     data = 47;
   609     CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
   610     CU_ASSERT_EQUAL(list->size, 2)
   611     data = 13;
   612     CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0)
   613     CU_ASSERT_EQUAL(list->size, 3)
   614     data = 42;
   615     CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0)
   617     CU_ASSERT_EQUAL(list->size, 4)
   618     CU_ASSERT_TRUE(list->capacity >= list->size)
   620     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   621     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
   622     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
   623     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
   625     cxLinkedListDestroy(list);
   626     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   627 }
   629 void test_hl_linked_list_remove(void) {
   630     cxTestingAllocatorReset();
   632     int data;
   633     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   635     data = 5;
   636     cxListAdd(list, &data);
   637     data = 47;
   638     cxListAdd(list, &data);
   639     data = 42;
   640     cxListAdd(list, &data);
   641     data = 13;
   642     cxListAdd(list, &data);
   644     CU_ASSERT_EQUAL(list->size, 4)
   645     CU_ASSERT_TRUE(list->capacity >= list->size)
   647     CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
   649     CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
   650     CU_ASSERT_EQUAL(list->size, 3)
   651     CU_ASSERT_TRUE(list->capacity >= list->size)
   652     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
   653     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
   654     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
   656     CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
   657     CU_ASSERT_EQUAL(list->size, 2)
   658     CU_ASSERT_TRUE(list->capacity >= list->size)
   659     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   660     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
   662     CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
   663     CU_ASSERT_EQUAL(list->size, 1)
   664     CU_ASSERT_TRUE(list->capacity >= list->size)
   665     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   667     CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
   668     CU_ASSERT_EQUAL(list->size, 0)
   669     CU_ASSERT_TRUE(list->capacity >= list->size)
   671     CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
   673     cxLinkedListDestroy(list);
   674     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   675 }
   677 void test_hl_linked_list_at(void) {
   678     cxTestingAllocatorReset();
   680     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   682     int data;
   683     data = 5;
   684     cxListAdd(list, &data);
   685     data = 47;
   686     cxListAdd(list, &data);
   687     data = 13;
   688     cxListAdd(list, &data);
   690     CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5)
   691     CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47)
   692     CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13)
   693     CU_ASSERT_PTR_NULL(cxListAt(list, 3))
   695     cxLinkedListDestroy(list);
   696     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   697 }
   699 void test_hl_linked_list_find(void) {
   700     cxTestingAllocatorReset();
   702     int data, criteria;
   703     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   705     data = 5;
   706     cxListAdd(list, &data);
   707     data = 47;
   708     cxListAdd(list, &data);
   709     data = 13;
   710     cxListAdd(list, &data);
   712     CU_ASSERT_EQUAL(list->size, 3)
   713     CU_ASSERT_TRUE(list->capacity >= list->size)
   715     criteria = 5;
   716     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
   717     criteria = 47;
   718     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
   719     criteria = 13;
   720     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
   721     criteria = 9000;
   722     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
   723     criteria = -5;
   724     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
   726     cxLinkedListDestroy(list);
   727     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   728 }
   730 void test_hl_linked_list_sort(void) {
   731     int expected[] = {
   732             14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
   733             894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
   734             1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
   735             2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
   736             3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
   737             4785, 4791, 4801, 4859, 4903, 4973
   738     };
   739     int scrambled[] = {
   740             759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
   741             2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
   742             2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
   743             894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
   744             3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
   745             4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
   746     };
   748     cxTestingAllocatorReset();
   750     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   752     for (int i = 0; i < 100; i++) {
   753         cxListAdd(list, &scrambled[i]);
   754     }
   756     cxListSort(list);
   758     for (int i = 0; i < 100; i++) {
   759         CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
   760     }
   762     cxLinkedListDestroy(list);
   763     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   764 }
   766 void test_hl_ptr_linked_list_create(void) {
   767     cxTestingAllocatorReset();
   769     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   771     CU_ASSERT_EQUAL(list->size, 0)
   772     CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
   773     CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
   774     CU_ASSERT_EQUAL(list->itemsize, sizeof(void *))
   775     CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
   777     cxLinkedListDestroy(list);
   778     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   779 }
   781 void test_hl_ptr_linked_list_add(void) {
   782     cxTestingAllocatorReset();
   784     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   786     int a = 5, b = 47, c = 13;
   788     CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
   789     CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
   790     CU_ASSERT_EQUAL(cxListAdd(list, &c), 0)
   792     CU_ASSERT_EQUAL(list->size, 3)
   793     CU_ASSERT_TRUE(list->capacity >= list->size)
   795     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
   796     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
   797     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
   799     a = 9;
   800     b = 10;
   801     c = 11;
   803     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9)
   804     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10)
   805     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11)
   807     cxLinkedListDestroy(list);
   808     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   809 }
   811 void test_hl_ptr_linked_list_insert(void) {
   812     cxTestingAllocatorReset();
   814     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   816     int a = 5, b = 47, c = 13, d = 42;
   818     CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0)
   819     CU_ASSERT_EQUAL(list->size, 0)
   820     CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0)
   821     CU_ASSERT_EQUAL(list->size, 1)
   822     CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0)
   823     CU_ASSERT_EQUAL(list->size, 2)
   824     CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0)
   825     CU_ASSERT_EQUAL(list->size, 3)
   826     CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0)
   828     CU_ASSERT_EQUAL(list->size, 4)
   829     CU_ASSERT_TRUE(list->capacity >= list->size)
   831     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   832     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
   833     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
   834     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
   836     cxLinkedListDestroy(list);
   837     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   838 }
   840 void test_hl_ptr_linked_list_remove(void) {
   841     cxTestingAllocatorReset();
   843     int a = 5, b = 47, c = 42, d = 13;
   844     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   846     cxListAdd(list, &a);
   847     cxListAdd(list, &b);
   848     cxListAdd(list, &c);
   849     cxListAdd(list, &d);
   851     CU_ASSERT_EQUAL(list->size, 4)
   852     CU_ASSERT_TRUE(list->capacity >= list->size)
   854     CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
   856     CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
   857     CU_ASSERT_EQUAL(list->size, 3)
   858     CU_ASSERT_TRUE(list->capacity >= list->size)
   859     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
   860     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
   861     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
   863     CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
   864     CU_ASSERT_EQUAL(list->size, 2)
   865     CU_ASSERT_TRUE(list->capacity >= list->size)
   866     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   867     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
   869     CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
   870     CU_ASSERT_EQUAL(list->size, 1)
   871     CU_ASSERT_TRUE(list->capacity >= list->size)
   872     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   874     CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
   875     CU_ASSERT_EQUAL(list->size, 0)
   876     CU_ASSERT_TRUE(list->capacity >= list->size)
   878     CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
   880     cxLinkedListDestroy(list);
   881     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   882 }
   884 void test_hl_ptr_linked_list_at(void) {
   885     cxTestingAllocatorReset();
   887     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   889     int a = 5, b = 47, c = 13;
   890     cxListAdd(list, &a);
   891     cxListAdd(list, &b);
   892     cxListAdd(list, &c);
   894     CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5)
   895     CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47)
   896     CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13)
   897     CU_ASSERT_PTR_NULL(cxListAt(list, 3))
   899     cxLinkedListDestroy(list);
   900     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   901 }
   903 void test_hl_ptr_linked_list_find(void) {
   904     cxTestingAllocatorReset();
   906     int a = 5, b = 47, c = 13, criteria;
   907     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   909     cxListAdd(list, &a);
   910     cxListAdd(list, &b);
   911     cxListAdd(list, &c);
   913     CU_ASSERT_EQUAL(list->size, 3)
   914     CU_ASSERT_TRUE(list->capacity >= list->size)
   916     criteria = 5;
   917     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
   918     criteria = 47;
   919     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
   920     criteria = 13;
   921     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
   922     criteria = 9000;
   923     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
   924     criteria = -5;
   925     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
   926     b = -5;
   927     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
   929     cxLinkedListDestroy(list);
   930     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   931 }
   933 void test_hl_ptr_linked_list_sort(void) {
   934     int expected[] = {
   935             14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
   936             894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
   937             1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
   938             2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
   939             3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
   940             4785, 4791, 4801, 4859, 4903, 4973
   941     };
   942     int scrambled[] = {
   943             759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
   944             2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
   945             2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
   946             894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
   947             3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
   948             4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
   949     };
   951     cxTestingAllocatorReset();
   953     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   955     for (int i = 0; i < 100; i++) {
   956         cxListAdd(list, &scrambled[i]);
   957     }
   959     cxListSort(list);
   961     for (int i = 0; i < 100; i++) {
   962         CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
   963     }
   965     cxLinkedListDestroy(list);
   966     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   967 }
   969 int main() {
   970     CU_pSuite suite = NULL;
   972     if (CUE_SUCCESS != CU_initialize_registry()) {
   973         return CU_get_error();
   974     }
   976     suite = CU_add_suite("low level linked list", NULL, NULL);
   978     cu_add_test(suite, test_linked_list_link_unlink);
   979     cu_add_test(suite, test_linked_list_at);
   980     cu_add_test(suite, test_linked_list_find);
   981     cu_add_test(suite, test_linked_list_compare);
   982     cu_add_test(suite, test_linked_list_prepend);
   983     cu_add_test(suite, test_linked_list_add);
   984     cu_add_test(suite, test_linked_list_insert);
   985     cu_add_test(suite, test_linked_list_insert_chain);
   986     cu_add_test(suite, test_linked_list_first);
   987     cu_add_test(suite, test_linked_list_last);
   988     cu_add_test(suite, test_linked_list_prev);
   989     cu_add_test(suite, test_linked_list_remove);
   990     cu_add_test(suite, test_linked_list_size);
   991     cu_add_test(suite, test_linked_list_sort);
   992     cu_add_test(suite, test_linked_list_reverse);
   994     suite = CU_add_suite("high level linked list", NULL, NULL);
   996     cu_add_test(suite, test_hl_linked_list_create);
   997     cu_add_test(suite, test_hl_linked_list_add);
   998     cu_add_test(suite, test_hl_linked_list_insert);
   999     cu_add_test(suite, test_hl_linked_list_remove);
  1000     cu_add_test(suite, test_hl_linked_list_at);
  1001     cu_add_test(suite, test_hl_linked_list_find);
  1002     cu_add_test(suite, test_hl_linked_list_sort);
  1004     suite = CU_add_suite("high level pointer linked list", NULL, NULL);
  1006     cu_add_test(suite, test_hl_ptr_linked_list_create);
  1007     cu_add_test(suite, test_hl_ptr_linked_list_add);
  1008     cu_add_test(suite, test_hl_ptr_linked_list_insert);
  1009     cu_add_test(suite, test_hl_ptr_linked_list_remove);
  1010     cu_add_test(suite, test_hl_ptr_linked_list_at);
  1011     cu_add_test(suite, test_hl_ptr_linked_list_find);
  1012     cu_add_test(suite, test_hl_ptr_linked_list_sort);
  1014     CU_basic_set_mode(UCX_CU_BRM);
  1016     int exitcode;
  1017     if (CU_basic_run_tests()) {
  1018         exitcode = CU_get_error();
  1019     } else {
  1020         exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
  1022     CU_cleanup_registry();
  1023     return exitcode;

mercurial