Skip to content

Commit 57e11e7

Browse files
authored
Restore example backend test filtering (#787)
Should also restore test coverage level
1 parent 8ab7b17 commit 57e11e7

7 files changed

Lines changed: 153 additions & 13 deletions

File tree

modules/util/include/func_test_util.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ concept HasPrintTestParam = requires(TestType value) {
2929
{ T::PrintTestParam(value) } -> std::same_as<std::string>;
3030
};
3131

32+
template <typename TestTasksList, typename RunTestCase>
33+
void RunTestCasesWithTag(const TestTasksList &test_tasks_list, std::string_view task_tag, RunTestCase run_test_case);
34+
3235
template <typename InType, typename OutType, typename TestType = void>
3336
/// @brief Base class for running functional tests on parallel tasks.
3437
/// @tparam InType Type of input data.
@@ -56,6 +59,16 @@ class BaseRunFuncTests : public ::testing::TestWithParam<FuncTestParam<InType, O
5659
/// @return Initialized input data.
5760
virtual InType GetTestInputData() = 0;
5861

62+
virtual void RunTestCase(const FuncTestParam<InType, OutType, TestType> &test_param) {
63+
ExecuteTest(test_param);
64+
}
65+
66+
template <typename TestTasksList>
67+
void RunTestCasesWithTag(const TestTasksList &test_tasks_list, std::string_view task_tag) {
68+
ppc::util::RunTestCasesWithTag(test_tasks_list, task_tag,
69+
[this](const auto &test_param) { RunTestCase(test_param); });
70+
}
71+
5972
void ExecuteTest(FuncTestParam<InType, OutType, TestType> test_param) {
6073
const std::string &test_name = std::get<static_cast<std::size_t>(GTestParamIndex::kNameTest)>(test_param);
6174

@@ -120,6 +133,43 @@ class BaseRunFuncTests : public ::testing::TestWithParam<FuncTestParam<InType, O
120133
ppc::task::TaskPtr<InType, OutType> task_;
121134
};
122135

136+
namespace detail {
137+
138+
[[nodiscard]] inline std::string MakeFuncTestTaskTagPattern(std::string_view task_tag) {
139+
std::string tag_pattern{task_tag};
140+
if (!tag_pattern.starts_with('_')) {
141+
tag_pattern.insert(0, 1, '_');
142+
}
143+
if (!tag_pattern.ends_with('_')) {
144+
tag_pattern.push_back('_');
145+
}
146+
return tag_pattern;
147+
}
148+
149+
} // namespace detail
150+
151+
template <typename TestTasksList, typename RunTestCase>
152+
void RunTestCasesWithTag(const TestTasksList &test_tasks_list, std::string_view task_tag, RunTestCase run_test_case) {
153+
if (task_tag.empty()) {
154+
ADD_FAILURE() << "Functional test task tag must not be empty";
155+
return;
156+
}
157+
158+
const std::string task_tag_pattern = detail::MakeFuncTestTaskTagPattern(task_tag);
159+
bool has_matching_task = false;
160+
std::apply([&](const auto &...test_params) {
161+
auto run_if_tagged = [&](const auto &test_param) {
162+
const std::string &test_name = std::get<static_cast<std::size_t>(GTestParamIndex::kNameTest)>(test_param);
163+
if (test_name.contains(task_tag_pattern)) {
164+
has_matching_task = true;
165+
std::invoke(run_test_case, test_param);
166+
}
167+
};
168+
(run_if_tagged(test_params), ...);
169+
}, test_tasks_list);
170+
EXPECT_TRUE(has_matching_task) << "No functional test cases matched tag: " << std::string(task_tag);
171+
}
172+
123173
template <typename Tuple, std::size_t... Is>
124174
auto ExpandToValuesImpl(const Tuple &t, std::index_sequence<Is...> /*unused*/) {
125175
return ::testing::Values(std::get<Is>(t)...);

modules/util/tests/util.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#include "util/include/util.hpp"
22

3+
#include <gtest/gtest-spi.h>
34
#include <gtest/gtest.h>
45

6+
#include <cstddef>
57
#include <libenvpp/detail/environment.hpp>
68
#include <libenvpp/detail/get.hpp>
79
#include <string>
10+
#include <string_view>
11+
#include <tuple>
12+
#include <vector>
813

914
#include "omp.h"
15+
#include "task/include/task.hpp"
16+
#include "util/include/func_test_util.hpp"
1017

1118
namespace my::nested {
1219
struct Type {};
@@ -124,3 +131,49 @@ TEST(GetNumProc, ReadsFromEnvironment) {
124131
env::detail::set_scoped_environment_variable scoped("PPC_NUM_PROC", "4");
125132
EXPECT_EQ(ppc::util::GetNumProc(), 4);
126133
}
134+
135+
namespace {
136+
137+
using FuncTestUtilParam = ppc::util::FuncTestParam<int, int, int>;
138+
139+
FuncTestUtilParam MakeFuncTestUtilParam(const std::string &test_name, int value) {
140+
return FuncTestUtilParam{[](int) -> ppc::task::TaskPtr<int, int> { return {}; }, test_name, value};
141+
}
142+
143+
void ExpectSingleNonFatalFailureContains(const ::testing::TestPartResultArray &failures, std::string_view message) {
144+
ASSERT_EQ(failures.size(), 1);
145+
const ::testing::TestPartResult &failure = failures.GetTestPartResult(0);
146+
EXPECT_EQ(failure.type(), ::testing::TestPartResult::kNonFatalFailure);
147+
EXPECT_NE(std::string_view(failure.message()).find(message), std::string_view::npos);
148+
}
149+
150+
} // namespace
151+
152+
TEST(FuncTestUtil, RunTestCasesWithTagAcceptsBareTags) {
153+
const auto test_tasks = std::make_tuple(MakeFuncTestUtilParam("example_threads_seq_enabled", 1),
154+
MakeFuncTestUtilParam("example_threads_tbb_enabled", 2),
155+
MakeFuncTestUtilParam("example_threads_tbb_disabled", 3));
156+
157+
std::vector<int> visited_params;
158+
ppc::util::RunTestCasesWithTag(test_tasks, "tbb", [&](const auto &test_param) {
159+
visited_params.push_back(std::get<static_cast<std::size_t>(ppc::util::GTestParamIndex::kTestParams)>(test_param));
160+
});
161+
162+
const std::vector<int> expected_params{2, 3};
163+
EXPECT_EQ(visited_params, expected_params);
164+
}
165+
166+
TEST(FuncTestUtil, RunTestCasesWithTagFailsWhenTagIsMissing) {
167+
const auto test_tasks = std::make_tuple(MakeFuncTestUtilParam("example_threads_seq_enabled", 1));
168+
169+
bool callback_was_called = false;
170+
::testing::TestPartResultArray failures;
171+
{
172+
::testing::ScopedFakeTestPartResultReporter reporter(
173+
::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD, &failures);
174+
ppc::util::RunTestCasesWithTag(test_tasks, "omp", [&](const auto & /*test_param*/) { callback_was_called = true; });
175+
}
176+
177+
ExpectSingleNonFatalFailureContains(failures, "No functional test cases matched tag: omp");
178+
EXPECT_FALSE(callback_was_called);
179+
}

scripts/run_tests.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,21 @@ def __build_mpi_cmd(self, ppc_num_proc, additional_mpi_args):
198198

199199
@staticmethod
200200
def __get_gtest_settings(repeats_count, type_task):
201+
type_task_patterns = {
202+
"_all_": ["_all_", "AllEnabled"],
203+
"_mpi_": ["_mpi_", "MpiEnabled"],
204+
"_omp_": ["_omp_", "OmpEnabled"],
205+
"_seq_": ["_seq_", "SeqEnabled"],
206+
"_stl_": ["_stl_", "StlEnabled"],
207+
"_tbb_": ["_tbb_", "TbbEnabled"],
208+
}
209+
filter_patterns = type_task_patterns.get(type_task, [type_task])
201210
command = [
202211
f"--gtest_repeat={repeats_count}",
203212
"--gtest_recreate_environments_when_repeating",
204213
"--gtest_color=0",
205214
"--gtest_shuffle",
206-
f"--gtest_filter=*{type_task}*",
215+
"--gtest_filter=" + ":".join(f"*{pattern}*" for pattern in filter_patterns),
207216
]
208217
return command
209218

tasks/example/processes/t1/tests/functional/main.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class NesterovARunFuncTestsProcesses : public ppc::util::BaseRunFuncTests<InType
2727
}
2828

2929
protected:
30-
void RunTestCase(const ppc::util::FuncTestParam<InType, OutType, TestType> &test_param) {
30+
void RunTestCase(const ppc::util::FuncTestParam<InType, OutType, TestType> &test_param) override {
3131
const std::string &test_name =
3232
std::get<static_cast<std::size_t>(ppc::util::GTestParamIndex::kNameTest)>(test_param);
3333
if (IsTestDisabled(test_name) || ShouldSkipNonMpiTask(test_name)) {
@@ -83,8 +83,12 @@ const auto kTestTasksList = std::tuple_cat(
8383

8484
} // namespace
8585

86-
TEST_F(NesterovARunFuncTestsProcesses, MatmulFromPic) {
87-
std::apply([this](const auto &...test_params) { (RunTestCase(test_params), ...); }, kTestTasksList);
86+
TEST_F(NesterovARunFuncTestsProcesses, MatmulFromPicMpiEnabled) {
87+
RunTestCasesWithTag(kTestTasksList, "mpi");
88+
}
89+
90+
TEST_F(NesterovARunFuncTestsProcesses, MatmulFromPicSeqEnabled) {
91+
RunTestCasesWithTag(kTestTasksList, "seq");
8892
}
8993

9094
} // namespace example_processes_t1

tasks/example/processes/t2/tests/functional/main.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class NesterovARunFuncTestsProcesses2 : public ppc::util::BaseRunFuncTests<InTyp
2727
}
2828

2929
protected:
30-
void RunTestCase(const ppc::util::FuncTestParam<InType, OutType, TestType> &test_param) {
30+
void RunTestCase(const ppc::util::FuncTestParam<InType, OutType, TestType> &test_param) override {
3131
const std::string &test_name =
3232
std::get<static_cast<std::size_t>(ppc::util::GTestParamIndex::kNameTest)>(test_param);
3333
if (IsTestDisabled(test_name) || ShouldSkipNonMpiTask(test_name)) {
@@ -83,8 +83,12 @@ const auto kTestTasksList = std::tuple_cat(
8383

8484
} // namespace
8585

86-
TEST_F(NesterovARunFuncTestsProcesses2, MatmulFromPic) {
87-
std::apply([this](const auto &...test_params) { (RunTestCase(test_params), ...); }, kTestTasksList);
86+
TEST_F(NesterovARunFuncTestsProcesses2, MatmulFromPicMpiEnabled) {
87+
RunTestCasesWithTag(kTestTasksList, "mpi");
88+
}
89+
90+
TEST_F(NesterovARunFuncTestsProcesses2, MatmulFromPicSeqEnabled) {
91+
RunTestCasesWithTag(kTestTasksList, "seq");
8892
}
8993

9094
} // namespace example_processes_t2

tasks/example/processes/t3/tests/functional/main.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class NesterovARunFuncTestsProcesses3 : public ppc::util::BaseRunFuncTests<InTyp
2727
}
2828

2929
protected:
30-
void RunTestCase(const ppc::util::FuncTestParam<InType, OutType, TestType> &test_param) {
30+
void RunTestCase(const ppc::util::FuncTestParam<InType, OutType, TestType> &test_param) override {
3131
const std::string &test_name =
3232
std::get<static_cast<std::size_t>(ppc::util::GTestParamIndex::kNameTest)>(test_param);
3333
if (IsTestDisabled(test_name) || ShouldSkipNonMpiTask(test_name)) {
@@ -83,8 +83,12 @@ const auto kTestTasksList = std::tuple_cat(
8383

8484
} // namespace
8585

86-
TEST_F(NesterovARunFuncTestsProcesses3, MatmulFromPic) {
87-
std::apply([this](const auto &...test_params) { (RunTestCase(test_params), ...); }, kTestTasksList);
86+
TEST_F(NesterovARunFuncTestsProcesses3, MatmulFromPicMpiEnabled) {
87+
RunTestCasesWithTag(kTestTasksList, "mpi");
88+
}
89+
90+
TEST_F(NesterovARunFuncTestsProcesses3, MatmulFromPicSeqEnabled) {
91+
RunTestCasesWithTag(kTestTasksList, "seq");
8892
}
8993

9094
} // namespace example_processes_t3

tasks/example/threads/tests/functional/main.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class NesterovARunFuncTestsThreads : public ppc::util::BaseRunFuncTests<InType,
3030
}
3131

3232
protected:
33-
void RunTestCase(const ppc::util::FuncTestParam<InType, OutType, TestType> &test_param) {
33+
void RunTestCase(const ppc::util::FuncTestParam<InType, OutType, TestType> &test_param) override {
3434
const std::string &test_name =
3535
std::get<static_cast<std::size_t>(ppc::util::GTestParamIndex::kNameTest)>(test_param);
3636
if (IsTestDisabled(test_name) || ShouldSkipNonMpiTask(test_name)) {
@@ -89,8 +89,24 @@ const auto kTestTasksList =
8989

9090
} // namespace
9191

92-
TEST_F(NesterovARunFuncTestsThreads, MatmulFromPic) {
93-
std::apply([this](const auto &...test_params) { (RunTestCase(test_params), ...); }, kTestTasksList);
92+
TEST_F(NesterovARunFuncTestsThreads, MatmulFromPicAllEnabled) {
93+
RunTestCasesWithTag(kTestTasksList, "all");
94+
}
95+
96+
TEST_F(NesterovARunFuncTestsThreads, MatmulFromPicOmpEnabled) {
97+
RunTestCasesWithTag(kTestTasksList, "omp");
98+
}
99+
100+
TEST_F(NesterovARunFuncTestsThreads, MatmulFromPicSeqEnabled) {
101+
RunTestCasesWithTag(kTestTasksList, "seq");
102+
}
103+
104+
TEST_F(NesterovARunFuncTestsThreads, MatmulFromPicStlEnabled) {
105+
RunTestCasesWithTag(kTestTasksList, "stl");
106+
}
107+
108+
TEST_F(NesterovARunFuncTestsThreads, MatmulFromPicTbbEnabled) {
109+
RunTestCasesWithTag(kTestTasksList, "tbb");
94110
}
95111

96112
} // namespace example_threads

0 commit comments

Comments
 (0)