test/test_list.c

Tue, 05 Oct 2021 16:33:11 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 05 Oct 2021 16:33:11 +0200
changeset 468
75ae1dccd101
parent 466
28bc3e10ac28
child 469
0458bff0b1cd
permissions
-rw-r--r--

add cx_linked_list_sort()

     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(int const *l, int const *r) {
    34     int left = *l, right = *r;
    35     return left == right ? 0 : (left < right ? -1 : 1);
    36 }
    38 void test_linked_list_at(void) {
    39     struct node {
    40         void *next;
    41         void *prev;
    42     };
    43     const ptrdiff_t loc_prev = offsetof(struct node, prev);
    44     const ptrdiff_t loc_next = offsetof(struct node, next);
    46     struct node a, b, c, d;
    47     a.prev = NULL;
    48     a.next = &b;
    49     b.prev = &a;
    50     b.next = &c;
    51     c.prev = &b;
    52     c.next = &d;
    53     d.prev = &c;
    54     d.next = NULL;
    56     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 0), &a)
    57     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 1), &b)
    58     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 2), &c)
    59     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 3), &d)
    60     CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4))
    62     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_prev, 0), &a)
    63     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 1), &b)
    64     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 2), &c)
    65     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 3), &d)
    66     CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4))
    68     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 0), &a)
    69     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b)
    70 }
    72 void test_linked_list_add(void) {
    73     struct node {
    74         void *prev;
    75         void *next;
    76     };
    78     struct node nodes[4];
    80     // test with begin, end / prev, next
    81     memset(nodes, 0, 4 * sizeof(struct node));
    82     void *begin = NULL;
    83     void *end = NULL;
    85     ptrdiff_t loc_prev = offsetof(struct node, prev);
    86     ptrdiff_t loc_next = offsetof(struct node, next);
    88     cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
    89     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
    90     CU_ASSERT_PTR_EQUAL(end, &nodes[0])
    91     CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL)
    92     CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL)
    94     cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
    95     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
    96     CU_ASSERT_PTR_EQUAL(end, &nodes[1])
    97     CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
    98     CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
   100     // test with begin only / prev, next
   101     memset(nodes, 0, 4 * sizeof(struct node));
   102     begin = NULL;
   103     end = NULL;
   105     cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]);
   106     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   107     cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]);
   108     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   109     CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
   110     CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
   112     cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]);
   113     CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
   114     CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
   116     // test with begin, end / next
   117     memset(nodes, 0, 4 * sizeof(struct node));
   118     begin = NULL;
   119     end = NULL;
   121     cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
   122     CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
   123     CU_ASSERT_PTR_EQUAL(end, &nodes[0])
   124     cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
   125     CU_ASSERT_PTR_EQUAL(end, &nodes[1])
   126     CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
   127     CU_ASSERT_PTR_NULL(nodes[1].prev)
   128 }
   130 void test_linked_list_last(void) {
   131     CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, -1))
   132     CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, 0))
   134     struct node {
   135         int data;
   136         void *next;
   137     };
   138     ptrdiff_t loc = offsetof(struct node, next);
   140     struct node third = {3, NULL};
   141     struct node second = {2, &third};
   142     struct node first = {1, &second};
   144     CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&first, loc), &third)
   145     CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&second, loc), &third)
   146     CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&third, loc), &third)
   147 }
   149 void test_linked_list_size(void) {
   150     struct node {
   151         void *next;
   152     };
   153     ptrdiff_t loc = offsetof(struct node, next);
   155     struct node first = {NULL};
   156     struct node second = {NULL};
   157     struct node third = {NULL};
   159     CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc), 0)
   160     CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 1)
   161     first.next = &second;
   162     CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 2)
   163     second.next = &third;
   164     CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 3)
   165     CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&second, loc), 2)
   166 }
   168 void test_linked_list_sort(void) {
   169     struct node {
   170         void *prev;
   171         void *next;
   172         int data;
   173     };
   175     int expected[] = {
   176             14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
   177             894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
   178             1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
   179             2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
   180             3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
   181             4785, 4791, 4801, 4859, 4903, 4973
   182     };
   183     int scrambled[] = {
   184             759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
   185             2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
   186             2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
   187             894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
   188             3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
   189             4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
   190     };
   192     struct node *nodes = calloc(100, sizeof(struct node));
   193     for (int i = 0; i < 100; i++) {
   194         nodes[i].prev = i == 0 ? NULL : &nodes[i - 1];
   195         nodes[i].next = i == 99 ? NULL : &nodes[i + 1];
   196         nodes[i].data = scrambled[i];
   197     }
   199     struct node *begin = &nodes[0];
   200     struct node *end = &nodes[99];
   202     cx_linked_list_sort((void **) &begin, (void **) &end,
   203                         offsetof(struct node, prev),
   204                         offsetof(struct node, next),
   205                         offsetof(struct node, data),
   206                         0, (CxListComparator) cmp_int);
   208     CU_ASSERT_PTR_NULL(begin->prev)
   209     CU_ASSERT_EQUAL(begin->data, expected[0])
   210     struct node *check = begin;
   211     struct node *check_last = NULL;
   212     for (int i = 0 ; i < 100 ; i++) {
   213         CU_ASSERT_EQUAL(check->data, expected[i])
   214         CU_ASSERT_PTR_EQUAL(check->prev, check_last)
   215         if (i < 99) {
   216             CU_ASSERT_PTR_NOT_NULL(check->next)
   217         }
   218         check_last = check;
   219         check = check->next;
   220     }
   221     CU_ASSERT_PTR_NULL(check)
   222     CU_ASSERT_EQUAL(end->data, expected[99])
   223 }
   226 void test_hl_linked_list_create(void) {
   227     cxTestingAllocatorReset();
   229     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   231     CU_ASSERT_EQUAL(list->size, 0)
   232     CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
   233     CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
   234     CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
   235     CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
   237     cxLinkedListDestroy(list);
   238     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   239 }
   241 void test_hl_linked_list_add(void) {
   242     cxTestingAllocatorReset();
   244     int data;
   245     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   247     data = 5;
   248     CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
   249     data = 47;
   250     CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
   251     data = 13;
   252     CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
   254     CU_ASSERT_EQUAL(list->size, 3)
   255     CU_ASSERT_TRUE(list->capacity >= list->size)
   257     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
   258     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
   259     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
   261     cxLinkedListDestroy(list);
   262     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   263 }
   265 void test_hl_linked_list_last(void) {
   266     cxTestingAllocatorReset();
   268     int data;
   269     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   271     CU_ASSERT_PTR_NULL(cxListLast(list))
   273     data = 5;
   274     CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
   275     CU_ASSERT_EQUAL(*(int *) cxListLast(list), 5)
   277     data = 47;
   278     CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
   279     CU_ASSERT_EQUAL(*(int *) cxListLast(list), 47)
   281     cxLinkedListDestroy(list);
   282     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   283 }
   285 void test_hl_linked_list_insert(void) {
   286     cxTestingAllocatorReset();
   288     int data;
   289     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   291     data = 5;
   292     CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0)
   293     CU_ASSERT_EQUAL(list->size, 0)
   294     CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
   295     CU_ASSERT_EQUAL(list->size, 1)
   296     data = 47;
   297     CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
   298     CU_ASSERT_EQUAL(list->size, 2)
   299     data = 13;
   300     CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0)
   301     CU_ASSERT_EQUAL(list->size, 3)
   302     data = 42;
   303     CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0)
   305     CU_ASSERT_EQUAL(list->size, 4)
   306     CU_ASSERT_TRUE(list->capacity >= list->size)
   308     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   309     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
   310     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
   311     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
   313     cxLinkedListDestroy(list);
   314     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   315 }
   317 void test_hl_linked_list_remove(void) {
   318     cxTestingAllocatorReset();
   320     int data;
   321     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   323     data = 5;
   324     cxListAdd(list, &data);
   325     data = 47;
   326     cxListAdd(list, &data);
   327     data = 42;
   328     cxListAdd(list, &data);
   329     data = 13;
   330     cxListAdd(list, &data);
   332     CU_ASSERT_EQUAL(list->size, 4)
   333     CU_ASSERT_TRUE(list->capacity >= list->size)
   335     CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
   337     CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
   338     CU_ASSERT_EQUAL(list->size, 3)
   339     CU_ASSERT_TRUE(list->capacity >= list->size)
   340     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
   341     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
   342     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
   344     CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
   345     CU_ASSERT_EQUAL(list->size, 2)
   346     CU_ASSERT_TRUE(list->capacity >= list->size)
   347     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   348     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
   350     CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
   351     CU_ASSERT_EQUAL(list->size, 1)
   352     CU_ASSERT_TRUE(list->capacity >= list->size)
   353     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   355     CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
   356     CU_ASSERT_EQUAL(list->size, 0)
   357     CU_ASSERT_TRUE(list->capacity >= list->size)
   359     CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
   361     cxLinkedListDestroy(list);
   362     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   363 }
   365 void test_hl_linked_list_find(void) {
   366     cxTestingAllocatorReset();
   368     int data, criteria;
   369     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   371     data = 5;
   372     cxListAdd(list, &data);
   373     data = 47;
   374     cxListAdd(list, &data);
   375     data = 13;
   376     cxListAdd(list, &data);
   378     CU_ASSERT_EQUAL(list->size, 3)
   379     CU_ASSERT_TRUE(list->capacity >= list->size)
   381     criteria = 5;
   382     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
   383     criteria = 47;
   384     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
   385     criteria = 13;
   386     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
   387     criteria = 9000;
   388     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
   389     criteria = -5;
   390     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
   392     cxLinkedListDestroy(list);
   393     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   394 }
   396 void test_hl_ptr_linked_list_create(void) {
   397     cxTestingAllocatorReset();
   399     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   401     CU_ASSERT_EQUAL(list->size, 0)
   402     CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
   403     CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
   404     CU_ASSERT_EQUAL(list->itemsize, sizeof(void *))
   405     CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
   407     cxLinkedListDestroy(list);
   408     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   409 }
   411 void test_hl_ptr_linked_list_add(void) {
   412     cxTestingAllocatorReset();
   414     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   416     int a = 5, b = 47, c = 13;
   418     CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
   419     CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
   420     CU_ASSERT_EQUAL(cxListAdd(list, &c), 0)
   422     CU_ASSERT_EQUAL(list->size, 3)
   423     CU_ASSERT_TRUE(list->capacity >= list->size)
   425     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
   426     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
   427     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
   429     a = 9;
   430     b = 10;
   431     c = 11;
   433     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9)
   434     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10)
   435     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11)
   437     cxLinkedListDestroy(list);
   438     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   439 }
   441 void test_hl_ptr_linked_list_last(void) {
   442     cxTestingAllocatorReset();
   444     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   445     CU_ASSERT_PTR_NULL(cxListLast(list))
   447     int a = 5, b = 47;
   449     CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
   450     CU_ASSERT_EQUAL(*(int *) cxListLast(list), 5)
   451     CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
   452     CU_ASSERT_EQUAL(*(int *) cxListLast(list), 47)
   454     cxLinkedListDestroy(list);
   455     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   456 }
   458 void test_hl_ptr_linked_list_insert(void) {
   459     cxTestingAllocatorReset();
   461     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   463     int a = 5, b = 47, c = 13, d = 42;
   465     CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0)
   466     CU_ASSERT_EQUAL(list->size, 0)
   467     CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0)
   468     CU_ASSERT_EQUAL(list->size, 1)
   469     CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0)
   470     CU_ASSERT_EQUAL(list->size, 2)
   471     CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0)
   472     CU_ASSERT_EQUAL(list->size, 3)
   473     CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0)
   475     CU_ASSERT_EQUAL(list->size, 4)
   476     CU_ASSERT_TRUE(list->capacity >= list->size)
   478     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   479     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
   480     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
   481     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
   483     cxLinkedListDestroy(list);
   484     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   485 }
   487 void test_hl_ptr_linked_list_remove(void) {
   488     cxTestingAllocatorReset();
   490     int a = 5, b = 47, c = 42, d = 13;
   491     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   493     cxListAdd(list, &a);
   494     cxListAdd(list, &b);
   495     cxListAdd(list, &c);
   496     cxListAdd(list, &d);
   498     CU_ASSERT_EQUAL(list->size, 4)
   499     CU_ASSERT_TRUE(list->capacity >= list->size)
   501     CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
   503     CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
   504     CU_ASSERT_EQUAL(list->size, 3)
   505     CU_ASSERT_TRUE(list->capacity >= list->size)
   506     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
   507     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
   508     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
   510     CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
   511     CU_ASSERT_EQUAL(list->size, 2)
   512     CU_ASSERT_TRUE(list->capacity >= list->size)
   513     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   514     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
   516     CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
   517     CU_ASSERT_EQUAL(list->size, 1)
   518     CU_ASSERT_TRUE(list->capacity >= list->size)
   519     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
   521     CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
   522     CU_ASSERT_EQUAL(list->size, 0)
   523     CU_ASSERT_TRUE(list->capacity >= list->size)
   525     CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
   527     cxLinkedListDestroy(list);
   528     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   529 }
   531 void test_hl_ptr_linked_list_find(void) {
   532     cxTestingAllocatorReset();
   534     int a = 5, b = 47, c = 13, criteria;
   535     CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
   537     cxListAdd(list, &a);
   538     cxListAdd(list, &b);
   539     cxListAdd(list, &c);
   541     CU_ASSERT_EQUAL(list->size, 3)
   542     CU_ASSERT_TRUE(list->capacity >= list->size)
   544     criteria = 5;
   545     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
   546     criteria = 47;
   547     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
   548     criteria = 13;
   549     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
   550     criteria = 9000;
   551     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
   552     criteria = -5;
   553     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
   554     b = -5;
   555     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
   557     cxLinkedListDestroy(list);
   558     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   559 }
   561 int main() {
   562     CU_pSuite suite = NULL;
   564     if (CUE_SUCCESS != CU_initialize_registry()) {
   565         return CU_get_error();
   566     }
   568     suite = CU_add_suite("low level linked list", NULL, NULL);
   570     cu_add_test(suite, test_linked_list_at);
   571     cu_add_test(suite, test_linked_list_add);
   572     cu_add_test(suite, test_linked_list_last);
   573     cu_add_test(suite, test_linked_list_size);
   574     cu_add_test(suite, test_linked_list_sort);
   576     suite = CU_add_suite("high level linked list", NULL, NULL);
   578     cu_add_test(suite, test_hl_linked_list_create);
   579     cu_add_test(suite, test_hl_linked_list_add);
   580     cu_add_test(suite, test_hl_linked_list_last);
   581     cu_add_test(suite, test_hl_linked_list_insert);
   582     cu_add_test(suite, test_hl_linked_list_remove);
   583     cu_add_test(suite, test_hl_linked_list_find);
   585     suite = CU_add_suite("high level pointer linked list", NULL, NULL);
   587     cu_add_test(suite, test_hl_ptr_linked_list_create);
   588     cu_add_test(suite, test_hl_ptr_linked_list_add);
   589     cu_add_test(suite, test_hl_ptr_linked_list_last);
   590     cu_add_test(suite, test_hl_ptr_linked_list_insert);
   591     cu_add_test(suite, test_hl_ptr_linked_list_remove);
   592     cu_add_test(suite, test_hl_ptr_linked_list_find);
   594     CU_basic_set_mode(UCX_CU_BRM);
   596     int exitcode;
   597     if (CU_basic_run_tests()) {
   598         exitcode = CU_get_error();
   599     } else {
   600         exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
   601     }
   602     CU_cleanup_registry();
   603     return exitcode;
   604 }

mercurial