test/test_list.c

Sun, 26 Sep 2021 18:31:24 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 26 Sep 2021 18:31:24 +0200
changeset 435
0fe204d50f54
parent 422
afd87df80b13
child 438
cd3069757010
permissions
-rw-r--r--

change inheritance model for lists

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

mercurial