Sun, 26 Sep 2021 14:45:42 +0200
implement at least freeing the linked list container
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_create() {
39 cxTestingAllocatorReset();
41 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
43 CU_ASSERT_EQUAL(list->data.size, 0)
44 CU_ASSERT_EQUAL(list->data.capacity, (size_t) -1)
45 CU_ASSERT_PTR_EQUAL(list->data.allocator, cxTestingAllocator)
46 CU_ASSERT_EQUAL(list->data.itemsize, sizeof(int))
47 CU_ASSERT_PTR_EQUAL(list->data.cmpfunc, cmp_int)
49 struct node {
50 void *begin;
51 void *end;
52 ptrdiff_t ploc;
53 ptrdiff_t nloc;
54 };
56 struct node *actual = (struct node *) list->data.listdata;
57 CU_ASSERT_PTR_NULL(actual->begin)
58 CU_ASSERT_PTR_NULL(actual->end)
59 CU_ASSERT_EQUAL(0, actual->ploc)
60 CU_ASSERT_EQUAL(sizeof(void *), actual->nloc)
62 cxLinkedListDestroy(list);
64 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
65 }
67 int main() {
68 CU_pSuite suite = NULL;
70 if (CUE_SUCCESS != CU_initialize_registry()) {
71 return CU_get_error();
72 }
74 suite = CU_add_suite("linked list suite", NULL, NULL);
75 if (NULL == suite) {
76 CU_cleanup_registry();
77 return CU_get_error();
78 }
80 if (
81 !CU_add_test(suite, "create and destroy linked list", test_linked_list_create)
82 ) {
83 CU_cleanup_registry();
84 return CU_get_error();
85 }
87 suite = CU_add_suite("array suite", NULL, NULL);
88 if (NULL == suite) {
89 CU_cleanup_registry();
90 return CU_get_error();
91 }
93 /*
94 if (
95 !CU_add_test(suite, "array...", test_array...)
96 ) {
97 CU_cleanup_registry();
98 return CU_get_error();
99 }
100 */
102 CU_basic_set_mode(UCX_CU_BRM);
104 int exitcode;
105 if (CU_basic_run_tests()) {
106 exitcode = CU_get_error();
107 } else {
108 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
109 }
110 CU_cleanup_registry();
111 return exitcode;
112 }