test/test_list.c

Sun, 26 Sep 2021 14:45:51 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 26 Sep 2021 14:45:51 +0200
changeset 427
ec92b4ed23aa
parent 422
afd87df80b13
child 435
0fe204d50f54
permissions
-rw-r--r--

Automated merge

390
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 390
diff changeset
29 #include "cx/linked_list.h"
411
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
30 #include "test_config.h"
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents: 413
diff changeset
31 #include "util_allocator.h"
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 390
diff changeset
32
412
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
33 int cmp_int(int const *l, int const *r) {
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
34 int left = *l, right = *r;
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
35 return left == right ? 0 : (left < right ? -1 : 1);
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
36 }
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
37
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
38 void test_linked_list_create() {
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents: 413
diff changeset
39 cxTestingAllocatorReset();
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents: 413
diff changeset
40
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents: 413
diff changeset
41 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
412
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
42
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
43 CU_ASSERT_EQUAL(list->data.size, 0)
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
44 CU_ASSERT_EQUAL(list->data.capacity, (size_t) -1)
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents: 413
diff changeset
45 CU_ASSERT_PTR_EQUAL(list->data.allocator, cxTestingAllocator)
412
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
46 CU_ASSERT_EQUAL(list->data.itemsize, sizeof(int))
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
47 CU_ASSERT_PTR_EQUAL(list->data.cmpfunc, cmp_int)
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
48
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
49 struct node {
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents: 413
diff changeset
50 void *begin;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents: 413
diff changeset
51 void *end;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents: 413
diff changeset
52 ptrdiff_t ploc;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents: 413
diff changeset
53 ptrdiff_t nloc;
412
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
54 };
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
55
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents: 413
diff changeset
56 struct node *actual = (struct node *) list->data.listdata;
412
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
57 CU_ASSERT_PTR_NULL(actual->begin)
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
58 CU_ASSERT_PTR_NULL(actual->end)
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
59 CU_ASSERT_EQUAL(0, actual->ploc)
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents: 413
diff changeset
60 CU_ASSERT_EQUAL(sizeof(void *), actual->nloc)
412
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
61
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 411
diff changeset
62 cxLinkedListDestroy(list);
413
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
63
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents: 413
diff changeset
64 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
411
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
65 }
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 390
diff changeset
66
390
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 int main() {
411
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
68 CU_pSuite suite = NULL;
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
69
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
70 if (CUE_SUCCESS != CU_initialize_registry()) {
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
71 return CU_get_error();
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
72 }
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
73
413
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
74 suite = CU_add_suite("linked list suite", NULL, NULL);
411
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
75 if (NULL == suite) {
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
76 CU_cleanup_registry();
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
77 return CU_get_error();
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
78 }
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
79
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
80 if (
413
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
81 !CU_add_test(suite, "create and destroy linked list", test_linked_list_create)
411
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
82 ) {
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
83 CU_cleanup_registry();
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
84 return CU_get_error();
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
85 }
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
86
413
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
87 suite = CU_add_suite("array suite", NULL, NULL);
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
88 if (NULL == suite) {
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
89 CU_cleanup_registry();
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
90 return CU_get_error();
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
91 }
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
92
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
93 /*
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
94 if (
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
95 !CU_add_test(suite, "array...", test_array...)
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
96 ) {
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
97 CU_cleanup_registry();
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
98 return CU_get_error();
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
99 }
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
100 */
0f4aa9fc75d9 perform array and list tests in the same test binary (use the same assertions, later)
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
101
411
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
102 CU_basic_set_mode(UCX_CU_BRM);
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
103
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
104 int exitcode;
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
105 if (CU_basic_run_tests()) {
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
106 exitcode = CU_get_error();
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
107 } else {
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
108 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
109 }
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
110 CU_cleanup_registry();
2842f729caab add first test suite to test_linked_list.c
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
111 return exitcode;
390
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 }

mercurial