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
--- a/test/CMakeLists.txt	Sat Apr 09 18:02:53 2022 +0200
+++ b/test/CMakeLists.txt	Fri Apr 15 21:28:51 2022 +0200
@@ -1,3 +1,4 @@
+# Transitional support for CTest written tests
 message(CHECK_START "Searching for CUnit test framework")
 
 find_path(CUNIT_INCLUDE_DIR NAMES CUnit/CUnit.h)
@@ -5,7 +6,7 @@
 include(FindPackageHandleStandardArgs)
 find_package_handle_standard_args(CUnit DEFAULT_MSG CUNIT_LIBRARY CUNIT_INCLUDE_DIR)
 
-if(CUNIT_FOUND)
+if (CUNIT_FOUND)
     message(CHECK_PASS "found: compiling tests.")
     set(TESTS
             test_allocator
@@ -13,12 +14,36 @@
             test_tree
     )
 
-    foreach(test ${TESTS})
-        add_executable(${test} util_allocator.c ${test}.c)
+    foreach (test ${TESTS})
+        add_executable(${test} ${test}.c)
         target_link_libraries(${test} PRIVATE ucx_static ${CUNIT_LIBRARY})
         target_include_directories(${test} PRIVATE ${CUNIT_INCLUDE_DIR})
         add_test(NAME ${test} COMMAND ${test} WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
-    endforeach()
-else()
-    message(CHECK_FAIL "not found: unit tests will not be available.")
-endif()
+    endforeach ()
+else ()
+    message(CHECK_FAIL "not found: CUnit tests will not be available.")
+endif ()
+
+# Load Google Test Framework
+set(CMAKE_CXX_STANDARD 11)
+
+include(FetchContent)
+FetchContent_Declare(
+        googletest
+        GIT_REPOSITORY https://github.com/google/googletest.git
+        GIT_TAG e2239ee6043f73722e7aa812a459f54a28552929 # release 1.11.0
+)
+# For Windows: Prevent overriding the parent project's compiler/linker settings
+set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
+FetchContent_MakeAvailable(googletest)
+include(GoogleTest)
+message(STATUS "Google Test made available")
+
+set(TESTS
+        selftest
+        )
+foreach (test ${TESTS})
+    add_executable(${test} ${test}.cpp)
+    target_link_libraries(${test} PRIVATE ucx_static gtest_main)
+endforeach ()
+gtest_discover_tests(${TESTS})
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/selftest.cpp	Fri Apr 15 21:28:51 2022 +0200
@@ -0,0 +1,45 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <gtest/gtest.h>
+#include <cx/common.h>
+
+TEST(SelfTest, BasicAssertion) {
+    EXPECT_EQ(7 * 6, 42);
+}
+
+TEST(SelfTest, UcxVersion) {
+    EXPECT_GE(UCX_VERSION_MAJOR, 3);
+    EXPECT_GE(UCX_VERSION, 3 << 16);
+}
+
+TEST(SelfTest, CommonDefinitions) {
+    // the following lines won't compile when the definitions are missing
+    EXPECT_EQ(__WORDSIZE, 8 * sizeof(void*));
+    EXPECT_EQ(sizeof(ssize_t), sizeof(size_t));
+}

mercurial