add googletest framework

Fri, 15 Apr 2022 21:28:51 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 15 Apr 2022 21:28:51 +0200
changeset 510
133ac0f8f3fc
parent 509
0d3c6075f82c
child 511
a32e6a6b1ca7

add googletest framework

test/CMakeLists.txt file | annotate | diff | comparison | revisions
test/selftest.cpp file | annotate | diff | comparison | revisions
     1.1 --- a/test/CMakeLists.txt	Sat Apr 09 18:02:53 2022 +0200
     1.2 +++ b/test/CMakeLists.txt	Fri Apr 15 21:28:51 2022 +0200
     1.3 @@ -1,3 +1,4 @@
     1.4 +# Transitional support for CTest written tests
     1.5  message(CHECK_START "Searching for CUnit test framework")
     1.6  
     1.7  find_path(CUNIT_INCLUDE_DIR NAMES CUnit/CUnit.h)
     1.8 @@ -5,7 +6,7 @@
     1.9  include(FindPackageHandleStandardArgs)
    1.10  find_package_handle_standard_args(CUnit DEFAULT_MSG CUNIT_LIBRARY CUNIT_INCLUDE_DIR)
    1.11  
    1.12 -if(CUNIT_FOUND)
    1.13 +if (CUNIT_FOUND)
    1.14      message(CHECK_PASS "found: compiling tests.")
    1.15      set(TESTS
    1.16              test_allocator
    1.17 @@ -13,12 +14,36 @@
    1.18              test_tree
    1.19      )
    1.20  
    1.21 -    foreach(test ${TESTS})
    1.22 -        add_executable(${test} util_allocator.c ${test}.c)
    1.23 +    foreach (test ${TESTS})
    1.24 +        add_executable(${test} ${test}.c)
    1.25          target_link_libraries(${test} PRIVATE ucx_static ${CUNIT_LIBRARY})
    1.26          target_include_directories(${test} PRIVATE ${CUNIT_INCLUDE_DIR})
    1.27          add_test(NAME ${test} COMMAND ${test} WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
    1.28 -    endforeach()
    1.29 -else()
    1.30 -    message(CHECK_FAIL "not found: unit tests will not be available.")
    1.31 -endif()
    1.32 +    endforeach ()
    1.33 +else ()
    1.34 +    message(CHECK_FAIL "not found: CUnit tests will not be available.")
    1.35 +endif ()
    1.36 +
    1.37 +# Load Google Test Framework
    1.38 +set(CMAKE_CXX_STANDARD 11)
    1.39 +
    1.40 +include(FetchContent)
    1.41 +FetchContent_Declare(
    1.42 +        googletest
    1.43 +        GIT_REPOSITORY https://github.com/google/googletest.git
    1.44 +        GIT_TAG e2239ee6043f73722e7aa812a459f54a28552929 # release 1.11.0
    1.45 +)
    1.46 +# For Windows: Prevent overriding the parent project's compiler/linker settings
    1.47 +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
    1.48 +FetchContent_MakeAvailable(googletest)
    1.49 +include(GoogleTest)
    1.50 +message(STATUS "Google Test made available")
    1.51 +
    1.52 +set(TESTS
    1.53 +        selftest
    1.54 +        )
    1.55 +foreach (test ${TESTS})
    1.56 +    add_executable(${test} ${test}.cpp)
    1.57 +    target_link_libraries(${test} PRIVATE ucx_static gtest_main)
    1.58 +endforeach ()
    1.59 +gtest_discover_tests(${TESTS})
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/selftest.cpp	Fri Apr 15 21:28:51 2022 +0200
     2.3 @@ -0,0 +1,45 @@
     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 <gtest/gtest.h>
    2.33 +#include <cx/common.h>
    2.34 +
    2.35 +TEST(SelfTest, BasicAssertion) {
    2.36 +    EXPECT_EQ(7 * 6, 42);
    2.37 +}
    2.38 +
    2.39 +TEST(SelfTest, UcxVersion) {
    2.40 +    EXPECT_GE(UCX_VERSION_MAJOR, 3);
    2.41 +    EXPECT_GE(UCX_VERSION, 3 << 16);
    2.42 +}
    2.43 +
    2.44 +TEST(SelfTest, CommonDefinitions) {
    2.45 +    // the following lines won't compile when the definitions are missing
    2.46 +    EXPECT_EQ(__WORDSIZE, 8 * sizeof(void*));
    2.47 +    EXPECT_EQ(sizeof(ssize_t), sizeof(size_t));
    2.48 +}

mercurial