test/test_linked_list.c

Sun, 14 Feb 2021 15:13:53 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Feb 2021 15:13:53 +0100
changeset 412
af766caea48d
parent 411
2842f729caab
permissions
-rw-r--r--

removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate

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@398 31
universe@412 32 int cmp_int(int const *l, int const *r) {
universe@412 33 int left = *l, right = *r;
universe@412 34 return left == right ? 0 : (left < right ? -1 : 1);
universe@412 35 }
universe@412 36
universe@412 37 void test_linked_list_create() {
universe@412 38 CxList list = cxLinkedListCreate(cxDefaultAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@412 39
universe@412 40 CU_ASSERT_EQUAL(list->data.size, 0)
universe@412 41 CU_ASSERT_EQUAL(list->data.capacity, (size_t) -1)
universe@412 42 CU_ASSERT_PTR_EQUAL(list->data.allocator, cxDefaultAllocator)
universe@412 43 CU_ASSERT_EQUAL(list->data.itemsize, sizeof(int))
universe@412 44 CU_ASSERT_PTR_EQUAL(list->data.cmpfunc, cmp_int)
universe@412 45
universe@412 46 struct node {
universe@412 47 void* begin; void* end; ptrdiff_t ploc; ptrdiff_t nloc;
universe@412 48 };
universe@412 49
universe@412 50 struct node* actual = (struct node*) list->data.listdata;
universe@412 51 CU_ASSERT_PTR_NULL(actual->begin)
universe@412 52 CU_ASSERT_PTR_NULL(actual->end)
universe@412 53 CU_ASSERT_EQUAL(0, actual->ploc)
universe@412 54 CU_ASSERT_EQUAL(sizeof(void*), actual->nloc)
universe@412 55
universe@412 56 cxLinkedListDestroy(list);
universe@411 57 }
universe@398 58
universe@390 59 int main() {
universe@411 60 CU_pSuite suite = NULL;
universe@411 61
universe@411 62 if (CUE_SUCCESS != CU_initialize_registry()) {
universe@411 63 return CU_get_error();
universe@411 64 }
universe@411 65
universe@412 66 suite = CU_add_suite("linked list memory management", NULL, NULL);
universe@411 67 if (NULL == suite) {
universe@411 68 CU_cleanup_registry();
universe@411 69 return CU_get_error();
universe@411 70 }
universe@411 71
universe@411 72 if (
universe@412 73 !CU_add_test(suite, "create linked list", test_linked_list_create)
universe@411 74 ) {
universe@411 75 CU_cleanup_registry();
universe@411 76 return CU_get_error();
universe@411 77 }
universe@411 78
universe@411 79 CU_basic_set_mode(UCX_CU_BRM);
universe@411 80
universe@411 81 int exitcode;
universe@411 82 if (CU_basic_run_tests()) {
universe@411 83 exitcode = CU_get_error();
universe@411 84 } else {
universe@411 85 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
universe@411 86 }
universe@411 87 CU_cleanup_registry();
universe@411 88 return exitcode;
universe@390 89 }

mercurial