test/test_list.c

Sun, 26 Sep 2021 14:41:16 +0200

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

add utility to verify allocations

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@412 43 CU_ASSERT_EQUAL(list->data.size, 0)
universe@412 44 CU_ASSERT_EQUAL(list->data.capacity, (size_t) -1)
universe@422 45 CU_ASSERT_PTR_EQUAL(list->data.allocator, cxTestingAllocator)
universe@412 46 CU_ASSERT_EQUAL(list->data.itemsize, sizeof(int))
universe@412 47 CU_ASSERT_PTR_EQUAL(list->data.cmpfunc, cmp_int)
universe@412 48
universe@412 49 struct node {
universe@422 50 void *begin;
universe@422 51 void *end;
universe@422 52 ptrdiff_t ploc;
universe@422 53 ptrdiff_t nloc;
universe@412 54 };
universe@412 55
universe@422 56 struct node *actual = (struct node *) list->data.listdata;
universe@412 57 CU_ASSERT_PTR_NULL(actual->begin)
universe@412 58 CU_ASSERT_PTR_NULL(actual->end)
universe@412 59 CU_ASSERT_EQUAL(0, actual->ploc)
universe@422 60 CU_ASSERT_EQUAL(sizeof(void *), actual->nloc)
universe@412 61
universe@412 62 cxLinkedListDestroy(list);
universe@413 63
universe@422 64 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@411 65 }
universe@398 66
universe@390 67 int main() {
universe@411 68 CU_pSuite suite = NULL;
universe@411 69
universe@411 70 if (CUE_SUCCESS != CU_initialize_registry()) {
universe@411 71 return CU_get_error();
universe@411 72 }
universe@411 73
universe@413 74 suite = CU_add_suite("linked list suite", NULL, NULL);
universe@411 75 if (NULL == suite) {
universe@411 76 CU_cleanup_registry();
universe@411 77 return CU_get_error();
universe@411 78 }
universe@411 79
universe@411 80 if (
universe@413 81 !CU_add_test(suite, "create and destroy linked list", test_linked_list_create)
universe@411 82 ) {
universe@411 83 CU_cleanup_registry();
universe@411 84 return CU_get_error();
universe@411 85 }
universe@411 86
universe@413 87 suite = CU_add_suite("array suite", NULL, NULL);
universe@413 88 if (NULL == suite) {
universe@413 89 CU_cleanup_registry();
universe@413 90 return CU_get_error();
universe@413 91 }
universe@413 92
universe@413 93 /*
universe@413 94 if (
universe@413 95 !CU_add_test(suite, "array...", test_array...)
universe@413 96 ) {
universe@413 97 CU_cleanup_registry();
universe@413 98 return CU_get_error();
universe@413 99 }
universe@413 100 */
universe@413 101
universe@411 102 CU_basic_set_mode(UCX_CU_BRM);
universe@411 103
universe@411 104 int exitcode;
universe@411 105 if (CU_basic_run_tests()) {
universe@411 106 exitcode = CU_get_error();
universe@411 107 } else {
universe@411 108 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
universe@411 109 }
universe@411 110 CU_cleanup_registry();
universe@411 111 return exitcode;
universe@390 112 }

mercurial