Skip to content

Commit edbef3c

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

7 files changed

Lines changed: 121 additions & 24 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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1+
# Add one test.
2+
#
3+
# The executable is always linked to 'kokkostools' and 'test_common'.
4+
#
5+
# Arguments:
6+
# TARGET_NAME : name of the test
7+
# SOURCE_FILE : source file, defaults to <TARGET_NAME>.cpp
8+
function(add_one_test)
9+
10+
cmake_parse_arguments(aot_args "" "TARGET_NAME;SOURCE_FILE" "" ${ARGN})
11+
12+
if(NOT DEFINED aot_args_TARGET_NAME)
13+
message(FATAL_ERROR "Wrong usage.")
14+
endif()
15+
16+
if(NOT DEFINED aot_args_SOURCE_FILE)
17+
set(aot_args_SOURCE_FILE "${aot_args_NAME}.cpp")
18+
endif()
19+
20+
add_executable(${aot_args_TARGET_NAME})
21+
22+
target_sources(
23+
${aot_args_TARGET_NAME}
24+
PRIVATE
25+
${aot_args_SOURCE_FILE}
26+
)
27+
target_link_libraries(
28+
${aot_args_TARGET_NAME}
29+
PRIVATE
30+
kokkostools test_common
31+
)
32+
33+
add_test(
34+
NAME ${aot_args_TARGET_NAME}
35+
COMMAND $<TARGET_FILE:${aot_args_TARGET_NAME}>
36+
)
37+
38+
endfunction(add_one_test)
39+
40+
# Create a test library that contains the required Kokkos and Google Test
41+
# initialization sequence.
42+
add_library(test_common OBJECT)
43+
target_sources(
44+
test_common
45+
PRIVATE
46+
TestBase.cpp
47+
)
48+
target_link_libraries(test_common PUBLIC GTest::gtest GTest::gmock Kokkos::kokkos)
49+
150
add_subdirectory(space-time-stack)

tests/TestBase.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
//! Initialize Google Test.
8+
::testing::InitGoogleTest(&argc, argv);
9+
10+
//! Run tests.
11+
auto success = RUN_ALL_TESTS();
12+
13+
return success;
14+
}
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
# 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
2+
add_one_test(
3+
TARGET_NAME test_space_time_stack_demangling
4+
SOURCE_FILE test_demangling
75
)
8-
target_link_libraries(
6+
target_compile_definitions(
97
test_space_time_stack_demangling
108
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
9+
LIBKP_SPACE_TIME_STACK="$<TARGET_FILE:kp_space_time_stack>"
1810
)

tests/space-time-stack/test_demangling.cpp

Lines changed: 15 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,18 @@ 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) {
57+
//! Fake arguments a user would give via CLI.
58+
std::string arg {"--kokkos-tools-libs=" LIBKP_SPACE_TIME_STACK};
59+
int argc = 1;
60+
std::vector<char*> argv {arg.data(), nullptr};
61+
5362
//! Initialize @c Kokkos.
54-
Kokkos::initialize(argc, argv);
63+
Kokkos::initialize(argc, argv.data());
5564

5665
//! Redirect output for later analysis.
5766
std::cout.flush();
@@ -71,10 +80,6 @@ int main(int argc, char* argv[]) {
7180

7281
//! Analyze test output.
7382
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());
83+
EXPECT_THAT(output.str(), ::testing::ContainsRegex(matcher));
7784
}
78-
79-
return EXIT_SUCCESS;
8085
}

0 commit comments

Comments
 (0)