Sun, 14 Feb 2021 15:37:12 +0100
perform array and list tests in the same test binary (use the same assertions, later)
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@413 | 57 | |
universe@413 | 58 | // TODO: use allocator that keeps track of the freed memory |
universe@411 | 59 | } |
universe@398 | 60 | |
universe@390 | 61 | int main() { |
universe@411 | 62 | CU_pSuite suite = NULL; |
universe@411 | 63 | |
universe@411 | 64 | if (CUE_SUCCESS != CU_initialize_registry()) { |
universe@411 | 65 | return CU_get_error(); |
universe@411 | 66 | } |
universe@411 | 67 | |
universe@413 | 68 | suite = CU_add_suite("linked list suite", NULL, NULL); |
universe@411 | 69 | if (NULL == suite) { |
universe@411 | 70 | CU_cleanup_registry(); |
universe@411 | 71 | return CU_get_error(); |
universe@411 | 72 | } |
universe@411 | 73 | |
universe@411 | 74 | if ( |
universe@413 | 75 | !CU_add_test(suite, "create and destroy linked list", test_linked_list_create) |
universe@411 | 76 | ) { |
universe@411 | 77 | CU_cleanup_registry(); |
universe@411 | 78 | return CU_get_error(); |
universe@411 | 79 | } |
universe@411 | 80 | |
universe@413 | 81 | suite = CU_add_suite("array suite", NULL, NULL); |
universe@413 | 82 | if (NULL == suite) { |
universe@413 | 83 | CU_cleanup_registry(); |
universe@413 | 84 | return CU_get_error(); |
universe@413 | 85 | } |
universe@413 | 86 | |
universe@413 | 87 | /* |
universe@413 | 88 | if ( |
universe@413 | 89 | !CU_add_test(suite, "array...", test_array...) |
universe@413 | 90 | ) { |
universe@413 | 91 | CU_cleanup_registry(); |
universe@413 | 92 | return CU_get_error(); |
universe@413 | 93 | } |
universe@413 | 94 | */ |
universe@413 | 95 | |
universe@411 | 96 | CU_basic_set_mode(UCX_CU_BRM); |
universe@411 | 97 | |
universe@411 | 98 | int exitcode; |
universe@411 | 99 | if (CU_basic_run_tests()) { |
universe@411 | 100 | exitcode = CU_get_error(); |
universe@411 | 101 | } else { |
universe@411 | 102 | exitcode = CU_get_number_of_failures() == 0 ? 0 : 1; |
universe@411 | 103 | } |
universe@411 | 104 | CU_cleanup_registry(); |
universe@411 | 105 | return exitcode; |
universe@390 | 106 | } |