perform array and list tests in the same test binary (use the same assertions, later)

Sun, 14 Feb 2021 15:37:12 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Feb 2021 15:37:12 +0100
changeset 413
0f4aa9fc75d9
parent 412
af766caea48d
child 414
81a4c3a63e65

perform array and list tests in the same test binary (use the same assertions, later)

test/CMakeLists.txt file | annotate | diff | comparison | revisions
test/test_linked_list.c file | annotate | diff | comparison | revisions
test/test_list.c file | annotate | diff | comparison | revisions
     1.1 --- a/test/CMakeLists.txt	Sun Feb 14 15:13:53 2021 +0100
     1.2 +++ b/test/CMakeLists.txt	Sun Feb 14 15:37:12 2021 +0100
     1.3 @@ -9,7 +9,7 @@
     1.4      message(CHECK_PASS "found: compiling tests.")
     1.5      set(TESTS
     1.6              test_allocator
     1.7 -            test_linked_list
     1.8 +            test_list
     1.9      )
    1.10  
    1.11      foreach(test ${TESTS})
     2.1 --- a/test/test_linked_list.c	Sun Feb 14 15:13:53 2021 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,89 +0,0 @@
     2.4 -/*
     2.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 - *
     2.7 - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     2.8 - *
     2.9 - * Redistribution and use in source and binary forms, with or without
    2.10 - * modification, are permitted provided that the following conditions are met:
    2.11 - *
    2.12 - *   1. Redistributions of source code must retain the above copyright
    2.13 - *      notice, this list of conditions and the following disclaimer.
    2.14 - *
    2.15 - *   2. Redistributions in binary form must reproduce the above copyright
    2.16 - *      notice, this list of conditions and the following disclaimer in the
    2.17 - *      documentation and/or other materials provided with the distribution.
    2.18 - *
    2.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    2.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    2.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    2.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    2.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    2.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    2.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    2.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    2.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    2.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    2.29 - * POSSIBILITY OF SUCH DAMAGE.
    2.30 - */
    2.31 -
    2.32 -#include "cx/linked_list.h"
    2.33 -#include "test_config.h"
    2.34 -
    2.35 -int cmp_int(int const *l, int const *r) {
    2.36 -    int left = *l, right = *r;
    2.37 -    return left == right ? 0 : (left < right ? -1 : 1);
    2.38 -}
    2.39 -
    2.40 -void test_linked_list_create() {
    2.41 -    CxList list = cxLinkedListCreate(cxDefaultAllocator, (CxListComparator) cmp_int, sizeof(int));
    2.42 -
    2.43 -    CU_ASSERT_EQUAL(list->data.size, 0)
    2.44 -    CU_ASSERT_EQUAL(list->data.capacity, (size_t) -1)
    2.45 -    CU_ASSERT_PTR_EQUAL(list->data.allocator, cxDefaultAllocator)
    2.46 -    CU_ASSERT_EQUAL(list->data.itemsize, sizeof(int))
    2.47 -    CU_ASSERT_PTR_EQUAL(list->data.cmpfunc, cmp_int)
    2.48 -
    2.49 -    struct node {
    2.50 -        void* begin; void* end; ptrdiff_t ploc; ptrdiff_t nloc;
    2.51 -    };
    2.52 -
    2.53 -    struct node* actual = (struct node*) list->data.listdata;
    2.54 -    CU_ASSERT_PTR_NULL(actual->begin)
    2.55 -    CU_ASSERT_PTR_NULL(actual->end)
    2.56 -    CU_ASSERT_EQUAL(0, actual->ploc)
    2.57 -    CU_ASSERT_EQUAL(sizeof(void*), actual->nloc)
    2.58 -
    2.59 -    cxLinkedListDestroy(list);
    2.60 -}
    2.61 -
    2.62 -int main() {
    2.63 -    CU_pSuite suite = NULL;
    2.64 -
    2.65 -    if (CUE_SUCCESS != CU_initialize_registry()) {
    2.66 -        return CU_get_error();
    2.67 -    }
    2.68 -
    2.69 -    suite = CU_add_suite("linked list memory management", NULL, NULL);
    2.70 -    if (NULL == suite) {
    2.71 -        CU_cleanup_registry();
    2.72 -        return CU_get_error();
    2.73 -    }
    2.74 -
    2.75 -    if (
    2.76 -            !CU_add_test(suite, "create linked list", test_linked_list_create)
    2.77 -            ) {
    2.78 -        CU_cleanup_registry();
    2.79 -        return CU_get_error();
    2.80 -    }
    2.81 -
    2.82 -    CU_basic_set_mode(UCX_CU_BRM);
    2.83 -
    2.84 -    int exitcode;
    2.85 -    if (CU_basic_run_tests()) {
    2.86 -        exitcode = CU_get_error();
    2.87 -    } else {
    2.88 -        exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
    2.89 -    }
    2.90 -    CU_cleanup_registry();
    2.91 -    return exitcode;
    2.92 -}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/test_list.c	Sun Feb 14 15:37:12 2021 +0100
     3.3 @@ -0,0 +1,106 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     3.8 + *
     3.9 + * Redistribution and use in source and binary forms, with or without
    3.10 + * modification, are permitted provided that the following conditions are met:
    3.11 + *
    3.12 + *   1. Redistributions of source code must retain the above copyright
    3.13 + *      notice, this list of conditions and the following disclaimer.
    3.14 + *
    3.15 + *   2. Redistributions in binary form must reproduce the above copyright
    3.16 + *      notice, this list of conditions and the following disclaimer in the
    3.17 + *      documentation and/or other materials provided with the distribution.
    3.18 + *
    3.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    3.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    3.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    3.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    3.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    3.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    3.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    3.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    3.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    3.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    3.29 + * POSSIBILITY OF SUCH DAMAGE.
    3.30 + */
    3.31 +
    3.32 +#include "cx/linked_list.h"
    3.33 +#include "test_config.h"
    3.34 +
    3.35 +int cmp_int(int const *l, int const *r) {
    3.36 +    int left = *l, right = *r;
    3.37 +    return left == right ? 0 : (left < right ? -1 : 1);
    3.38 +}
    3.39 +
    3.40 +void test_linked_list_create() {
    3.41 +    CxList list = cxLinkedListCreate(cxDefaultAllocator, (CxListComparator) cmp_int, sizeof(int));
    3.42 +
    3.43 +    CU_ASSERT_EQUAL(list->data.size, 0)
    3.44 +    CU_ASSERT_EQUAL(list->data.capacity, (size_t) -1)
    3.45 +    CU_ASSERT_PTR_EQUAL(list->data.allocator, cxDefaultAllocator)
    3.46 +    CU_ASSERT_EQUAL(list->data.itemsize, sizeof(int))
    3.47 +    CU_ASSERT_PTR_EQUAL(list->data.cmpfunc, cmp_int)
    3.48 +
    3.49 +    struct node {
    3.50 +        void* begin; void* end; ptrdiff_t ploc; ptrdiff_t nloc;
    3.51 +    };
    3.52 +
    3.53 +    struct node* actual = (struct node*) list->data.listdata;
    3.54 +    CU_ASSERT_PTR_NULL(actual->begin)
    3.55 +    CU_ASSERT_PTR_NULL(actual->end)
    3.56 +    CU_ASSERT_EQUAL(0, actual->ploc)
    3.57 +    CU_ASSERT_EQUAL(sizeof(void*), actual->nloc)
    3.58 +
    3.59 +    cxLinkedListDestroy(list);
    3.60 +
    3.61 +    // TODO: use allocator that keeps track of the freed memory
    3.62 +}
    3.63 +
    3.64 +int main() {
    3.65 +    CU_pSuite suite = NULL;
    3.66 +
    3.67 +    if (CUE_SUCCESS != CU_initialize_registry()) {
    3.68 +        return CU_get_error();
    3.69 +    }
    3.70 +
    3.71 +    suite = CU_add_suite("linked list suite", NULL, NULL);
    3.72 +    if (NULL == suite) {
    3.73 +        CU_cleanup_registry();
    3.74 +        return CU_get_error();
    3.75 +    }
    3.76 +
    3.77 +    if (
    3.78 +            !CU_add_test(suite, "create and destroy linked list", test_linked_list_create)
    3.79 +            ) {
    3.80 +        CU_cleanup_registry();
    3.81 +        return CU_get_error();
    3.82 +    }
    3.83 +
    3.84 +    suite = CU_add_suite("array suite", NULL, NULL);
    3.85 +    if (NULL == suite) {
    3.86 +        CU_cleanup_registry();
    3.87 +        return CU_get_error();
    3.88 +    }
    3.89 +
    3.90 +    /*
    3.91 +    if (
    3.92 +            !CU_add_test(suite, "array...", test_array...)
    3.93 +            ) {
    3.94 +        CU_cleanup_registry();
    3.95 +        return CU_get_error();
    3.96 +    }
    3.97 +    */
    3.98 +
    3.99 +    CU_basic_set_mode(UCX_CU_BRM);
   3.100 +
   3.101 +    int exitcode;
   3.102 +    if (CU_basic_run_tests()) {
   3.103 +        exitcode = CU_get_error();
   3.104 +    } else {
   3.105 +        exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
   3.106 +    }
   3.107 +    CU_cleanup_registry();
   3.108 +    return exitcode;
   3.109 +}

mercurial