Skip to content

Commit 8502180

Browse files
tests: add Google Test
1 parent b3ab0cb commit 8502180

7 files changed

Lines changed: 119 additions & 28 deletions

File tree

.github/workflows/build-with-kokkos.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ jobs:
7171
exit -1
7272
esac
7373
74-
- name: Install CMake, OpenMPI, PAPI and dtrace
74+
- name: Install git, CMake, OpenMPI, PAPI and dtrace
7575
run: |
7676
apt --yes --no-install-recommends install \
77+
git ca-certificates \
7778
cmake make \
7879
libopenmpi-dev \
7980
systemtap-sdt-dev \

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ endif()
272272
# Tests
273273
if(KokkosTools_ENABLE_TESTS)
274274
enable_testing()
275+
include(cmake/BuildGTest.cmake)
275276
add_subdirectory(tests)
276277
endif()
277278

cmake/BuildGTest.cmake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Look for Google Test and enable it as a target.
2+
#
3+
# The main targets that will be available are:
4+
# * GTest::gtest
5+
# * GTest::gmock
6+
#
7+
# References:
8+
# * https://github.com/google/googletest
9+
# * https://matgomes.com/integrate-google-test-into-cmake/
10+
# * https://google.github.io/googletest/quickstart-cmake.html
11+
# * https://jeremimucha.com/2021/04/cmake-fetchcontent/
12+
13+
include(FetchContent)
14+
15+
# Declare the Google Test dependency
16+
FetchContent_Declare(
17+
googletest
18+
GIT_REPOSITORY https://github.com/google/googletest.git
19+
GIT_TAG v1.14.0
20+
)
21+
22+
# If not yet populated, add Google Test to the build with the following options:
23+
# * disable installation of Google Test
24+
# * enable GMock
25+
# Note that we could have used FetchContent_MakeAvailable instead, but it would then
26+
# use the default configuration that would install Google Test.
27+
FetchContent_GetProperties(googletest)
28+
if (NOT googletest_POPULATED)
29+
FetchContent_Populate(googletest)
30+
31+
set(BUILD_GMOCK ON)
32+
set(INSTALL_GTEST OFF)
33+
34+
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL)
35+
endif()

tests/CMakeLists.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,56 @@
1+
# Add an executable and its related test.
2+
#
3+
# The executable is always linked to 'kokkostools' and 'test_common'.
4+
#
5+
# The test environment always contains 'KOKKOS_TOOLS_LIBS' that is set as the path
6+
# to the target file of 'CONNECTOR_LIB'.
7+
#
8+
# Arguments:
9+
# TARGET_NAME : name of the test
10+
# SOURCE_FILE : source file, defaults to <TARGET_NAME>.cpp
11+
# CONNECTOR_LIB : connector library target name under test
12+
function(kp_add_executable_and_test)
13+
14+
cmake_parse_arguments(kaeat_args "" "TARGET_NAME;SOURCE_FILE;CONNECTOR_LIB" "" ${ARGN})
15+
16+
if(NOT DEFINED kaeat_args_TARGET_NAME OR NOT DEFINED kaeat_args_SOURCE_FILE OR NOT DEFINED kaeat_args_CONNECTOR_LIB)
17+
message(FATAL_ERROR "Wrong usage.")
18+
endif()
19+
20+
add_executable(${kaeat_args_TARGET_NAME})
21+
22+
target_sources(
23+
${kaeat_args_TARGET_NAME}
24+
PRIVATE
25+
${kaeat_args_SOURCE_FILE}
26+
)
27+
target_link_libraries(
28+
${kaeat_args_TARGET_NAME}
29+
PRIVATE
30+
kokkostools test_common
31+
)
32+
33+
add_test(
34+
NAME ${kaeat_args_TARGET_NAME}
35+
COMMAND $<TARGET_FILE:${kaeat_args_TARGET_NAME}>
36+
)
37+
set_property(
38+
TEST ${kaeat_args_TARGET_NAME}
39+
APPEND
40+
PROPERTY
41+
ENVIRONMENT "KOKKOS_TOOLS_LIBS=$<TARGET_FILE:${kaeat_args_CONNECTOR_LIB}>"
42+
)
43+
44+
endfunction(kp_add_executable_and_test)
45+
46+
# Create a test library that contains the required Kokkos and Google Test
47+
# initialization sequence.
48+
add_library(test_common OBJECT)
49+
target_sources(
50+
test_common
51+
PRIVATE
52+
UnitTestMain.cpp
53+
)
54+
target_link_libraries(test_common PUBLIC GTest::gtest GTest::gmock Kokkos::kokkos)
55+
156
add_subdirectory(space-time-stack)

tests/UnitTestMain.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "gtest/gtest.h"
2+
3+
#include "Kokkos_Core.hpp"
4+
5+
//! Main entry point for tests.
6+
int main(int argc, char* argv[]) {
7+
::testing::InitGoogleTest(&argc, argv);
8+
9+
auto success = RUN_ALL_TESTS();
10+
11+
return success;
12+
}
Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
# A function to add such "simple" tests in 'tests/CMakeLists.txt' might be a good option.
2-
add_executable(test_space_time_stack_demangling)
3-
target_sources(
4-
test_space_time_stack_demangling
5-
PRIVATE
6-
test_demangling.cpp
7-
)
8-
target_link_libraries(
9-
test_space_time_stack_demangling
10-
PRIVATE
11-
Kokkos::kokkos kokkostools
12-
)
13-
add_test(
14-
NAME test_space_time_stack_demangling
15-
COMMAND $<TARGET_FILE:test_space_time_stack_demangling>
16-
--kokkos-tools-libs=$<TARGET_FILE:kp_space_time_stack>
17-
--kokkos-tools-args=1e-9
1+
kp_add_executable_and_test(
2+
TARGET_NAME test_space_time_stack_demangling
3+
SOURCE_FILE test_demangling.cpp
4+
CONNECTOR_LIB kp_space_time_stack
185
)

tests/space-time-stack/test_demangling.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include <iostream>
2-
#include <regex>
32
#include <sstream>
43

5-
#include "Kokkos_Core.hpp"
4+
#include "gmock/gmock.h"
5+
#include "gtest/gtest.h"
66

7-
#include "utils/demangle.hpp"
7+
#include "Kokkos_Core.hpp"
88

99
struct Tester {
1010
struct TagNamed {};
@@ -49,9 +49,13 @@ static const std::vector<std::string> matchers{
4949
"[0-9.e]+ sec [0-9.]+% 100.0% 0.0% ------ 1 Tester/Tester::TagUnnamed "
5050
"\\[for\\]"};
5151

52-
int main(int argc, char* argv[]) {
52+
/**
53+
* @test This test checks that the tool effectively uses
54+
* the demangling helpers.
55+
*/
56+
TEST(SpaceTimeStackTest, demangling) {
5357
//! Initialize @c Kokkos.
54-
Kokkos::initialize(argc, argv);
58+
Kokkos::initialize();
5559

5660
//! Redirect output for later analysis.
5761
std::cout.flush();
@@ -71,10 +75,6 @@ int main(int argc, char* argv[]) {
7175

7276
//! Analyze test output.
7377
for (const auto& matcher : matchers) {
74-
if (!std::regex_search(output.str(), std::regex(matcher)))
75-
throw std::runtime_error("Couln't find " + matcher + " in output\n" +
76-
output.str());
78+
EXPECT_THAT(output.str(), ::testing::ContainsRegex(matcher));
7779
}
78-
79-
return EXIT_SUCCESS;
8080
}

0 commit comments

Comments
 (0)