test/test_list.c

Sun, 03 Oct 2021 16:02:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Oct 2021 16:02:53 +0200
changeset 455
8168e16cd1e9
parent 453
bb144d08cd44
child 456
227c2eabbef8
permissions
-rw-r--r--

change test names

     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 }
   131 void test_linked_list_create(void) {
   132     cxTestingAllocatorReset();
   134     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
   136     CU_ASSERT_EQUAL(list->size, 0)
   137     CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
   138     CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
   139     CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
   140     CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
   142     // assume this structure for a linked list
   143     struct ll_check {
   144         cx_list_s base;
   145         void *begin;
   146         void *end;
   147     };
   149     struct ll_check *actual = (struct ll_check *) list;
   150     CU_ASSERT_PTR_NULL(actual->begin)
   151     CU_ASSERT_PTR_NULL(actual->end)
   153     cxLinkedListDestroy(list);
   155     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
   156 }
   158 int main() {
   159     CU_pSuite suite = NULL;
   161     if (CUE_SUCCESS != CU_initialize_registry()) {
   162         return CU_get_error();
   163     }
   165     suite = CU_add_suite("low level linked list suite", NULL, NULL);
   167     cu_add_test(suite, test_linked_list_at);
   168     cu_add_test(suite, test_linked_list_add);
   170     suite = CU_add_suite("high level linked list suite", NULL, NULL);
   172     cu_add_test(suite, test_linked_list_create);
   174     CU_basic_set_mode(UCX_CU_BRM);
   176     int exitcode;
   177     if (CU_basic_run_tests()) {
   178         exitcode = CU_get_error();
   179     } else {
   180         exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
   181     }
   182     CU_cleanup_registry();
   183     return exitcode;
   184 }

mercurial