migrate allocator tests to gtest

Sat, 16 Apr 2022 08:49:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 16 Apr 2022 08:49:31 +0200
changeset 511
a32e6a6b1ca7
parent 510
133ac0f8f3fc
child 512
096d206b63f9

migrate allocator tests to gtest

test/CMakeLists.txt file | annotate | diff | comparison | revisions
test/test_allocator.c file | annotate | diff | comparison | revisions
test/test_allocator.cpp file | annotate | diff | comparison | revisions
     1.1 --- a/test/CMakeLists.txt	Fri Apr 15 21:28:51 2022 +0200
     1.2 +++ b/test/CMakeLists.txt	Sat Apr 16 08:49:31 2022 +0200
     1.3 @@ -9,7 +9,6 @@
     1.4  if (CUNIT_FOUND)
     1.5      message(CHECK_PASS "found: compiling tests.")
     1.6      set(TESTS
     1.7 -            test_allocator
     1.8              test_list
     1.9              test_tree
    1.10      )
    1.11 @@ -41,9 +40,10 @@
    1.12  
    1.13  set(TESTS
    1.14          selftest
    1.15 +        test_allocator
    1.16          )
    1.17  foreach (test ${TESTS})
    1.18      add_executable(${test} ${test}.cpp)
    1.19      target_link_libraries(${test} PRIVATE ucx_static gtest_main)
    1.20 +    gtest_discover_tests(${test})
    1.21  endforeach ()
    1.22 -gtest_discover_tests(${TESTS})
     2.1 --- a/test/test_allocator.c	Fri Apr 15 21:28:51 2022 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,101 +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/allocator.h"
    2.33 -#include "test_config.h"
    2.34 -#include <errno.h>
    2.35 -
    2.36 -void test_default_allocator_available(void) {
    2.37 -    cx_allocator_class *clazz = cxDefaultAllocator->cl;
    2.38 -    CU_ASSERT_PTR_NOT_NULL(clazz)
    2.39 -}
    2.40 -
    2.41 -void test_default_malloc(void) {
    2.42 -    void *test = cxMalloc(cxDefaultAllocator, 16);
    2.43 -    CU_ASSERT_PTR_NOT_NULL(test);
    2.44 -    free(test);
    2.45 -}
    2.46 -
    2.47 -void test_default_realloc(void) {
    2.48 -    void *test = calloc(8, 1);
    2.49 -    memcpy(test, "Test", 5);
    2.50 -    test = cxRealloc(cxDefaultAllocator, test, 16);
    2.51 -    CU_ASSERT_PTR_NOT_NULL(test)
    2.52 -    CU_ASSERT_STRING_EQUAL("Test", test)
    2.53 -    free(test);
    2.54 -}
    2.55 -
    2.56 -void test_default_reallocate(void) {
    2.57 -    void *test = calloc(8, 1);
    2.58 -    memcpy(test, "Test", 5);
    2.59 -    int rval = cxReallocate(cxDefaultAllocator, &test, 16);
    2.60 -    CU_ASSERT_EQUAL(rval, 0);
    2.61 -    CU_ASSERT_PTR_NOT_NULL(test)
    2.62 -    CU_ASSERT_STRING_EQUAL("Test", test)
    2.63 -    free(test);
    2.64 -}
    2.65 -
    2.66 -void test_default_calloc(void) {
    2.67 -    void *test = cxCalloc(cxDefaultAllocator, 8, 2);
    2.68 -    CU_ASSERT_PTR_NOT_NULL(test)
    2.69 -    free(test);
    2.70 -}
    2.71 -
    2.72 -void test_default_free(void) {
    2.73 -    void *test = malloc(16);
    2.74 -    cxFree(cxDefaultAllocator, test);
    2.75 -    CU_PASS("Testing standard free is not possible.")
    2.76 -}
    2.77 -
    2.78 -int main() {
    2.79 -    CU_pSuite suite = NULL;
    2.80 -
    2.81 -    if (CUE_SUCCESS != CU_initialize_registry()) {
    2.82 -        return CU_get_error();
    2.83 -    }
    2.84 -
    2.85 -    suite = CU_add_suite("default allocator", NULL, NULL);
    2.86 -
    2.87 -    CU_add_test(suite, "default allocator available", test_default_allocator_available);
    2.88 -    CU_add_test(suite, "test of malloc()", test_default_malloc);
    2.89 -    CU_add_test(suite, "test of realloc()", test_default_realloc);
    2.90 -    CU_add_test(suite, "test of realloc() via cxReallocate", test_default_reallocate);
    2.91 -    CU_add_test(suite, "test of calloc()", test_default_calloc);
    2.92 -    CU_add_test(suite, "test of free()", test_default_free);
    2.93 -
    2.94 -    CU_basic_set_mode(UCX_CU_BRM);
    2.95 -
    2.96 -    int exitcode;
    2.97 -    if (CU_basic_run_tests()) {
    2.98 -        exitcode = CU_get_error();
    2.99 -    } else {
   2.100 -        exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
   2.101 -    }
   2.102 -    CU_cleanup_registry();
   2.103 -    return exitcode;
   2.104 -}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/test_allocator.cpp	Sat Apr 16 08:49:31 2022 +0200
     3.3 @@ -0,0 +1,76 @@
     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/allocator.h"
    3.33 +#include <gtest/gtest.h>
    3.34 +
    3.35 +#define SUITE_NAME Allocator
    3.36 +
    3.37 +TEST(SUITE_NAME, DefaultAllocator) {
    3.38 +    cx_allocator_class *clazz = cxDefaultAllocator->cl;
    3.39 +    ASSERT_NE(clazz, nullptr);
    3.40 +}
    3.41 +
    3.42 +TEST(SUITE_NAME, DefaultMalloc) {
    3.43 +    void *test = cxMalloc(cxDefaultAllocator, 16);
    3.44 +    ASSERT_NE(test, nullptr);
    3.45 +    free(test);
    3.46 +}
    3.47 +
    3.48 +TEST(SUITE_NAME, DefaultRealloc) {
    3.49 +    void *test = calloc(8, 1);
    3.50 +    memcpy(test, "Test", 5);
    3.51 +    test = cxRealloc(cxDefaultAllocator, test, 16);
    3.52 +    ASSERT_NE(test, nullptr);
    3.53 +    EXPECT_STREQ(reinterpret_cast<char *>(test), "Test");
    3.54 +    free(test);
    3.55 +}
    3.56 +
    3.57 +TEST(SUITE_NAME, Reallocate) {
    3.58 +    void *test = calloc(8, 1);
    3.59 +    memcpy(test, "Test", 5);
    3.60 +    int ret = cxReallocate(cxDefaultAllocator, &test, 16);
    3.61 +    EXPECT_EQ(ret, 0);
    3.62 +    ASSERT_NE(test, nullptr);
    3.63 +    EXPECT_STREQ(reinterpret_cast<char *>(test), "Test");
    3.64 +    free(test);
    3.65 +}
    3.66 +
    3.67 +TEST(SUITE_NAME, DefaultCalloc) {
    3.68 +    char *test = reinterpret_cast<char *>(cxCalloc(cxDefaultAllocator, 8, 2));
    3.69 +    ASSERT_NE(test, nullptr);
    3.70 +    for (int i = 0; i < 16; i++) ASSERT_EQ(test[i], 0);
    3.71 +    free(test);
    3.72 +}
    3.73 +
    3.74 +TEST(SUITE_NAME, DefaultFree) {
    3.75 +    void *test = malloc(16);
    3.76 +    EXPECT_NO_FATAL_FAILURE(
    3.77 +            cxFree(cxDefaultAllocator, test);
    3.78 +    );
    3.79 +}

mercurial