Skip to content

Commit 1ce11d2

Browse files
committed
Refactor and modularize test utilities.
Separated functional and performance test logic into distinct headers (`func_test_util.hpp` and `perf_test_util.hpp`) for clearer structure and better modularity. Updated references and improved clarity in test implementation.
1 parent 4340dd4 commit 1ce11d2

File tree

6 files changed

+105
-89
lines changed

6 files changed

+105
-89
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#pragma once
2+
3+
#include <gtest/gtest.h>
4+
#include <mpi.h>
5+
#include <omp.h>
6+
#include <tbb/tick_count.h>
7+
8+
#include <csignal>
9+
#include <filesystem>
10+
#include <fstream>
11+
12+
#include "core/perf/include/perf.hpp"
13+
#include "core/util/include/util.hpp"
14+
#include "nlohmann/json.hpp"
15+
16+
namespace ppc::util {
17+
18+
template <typename InType, typename OutType, typename TestType = void>
19+
using FuncTestParam = std::tuple<std::function<ppc::core::TaskPtr<InType, OutType>(InType)>, std::string, TestType>;
20+
21+
template <typename InType, typename OutType, typename TestType = void>
22+
using GTestFuncParam = ::testing::TestParamInfo<FuncTestParam<InType, OutType, TestType>>;
23+
24+
template <typename T, typename TestType>
25+
concept HasPrintTestParam = requires(TestType value) {
26+
{ T::PrintTestParam(value) } -> std::same_as<std::string>;
27+
};
28+
29+
template <typename InType, typename OutType, typename TestType = void>
30+
class BaseRunFuncTests : public ::testing::TestWithParam<FuncTestParam<InType, OutType, TestType>> {
31+
public:
32+
virtual bool CheckTestOutputData(OutType& output_data) = 0;
33+
virtual InType GetTestInputData() = 0;
34+
35+
template <typename Derived>
36+
static void RequireStaticInterface() {
37+
static_assert(HasPrintTestParam<Derived, TestType>,
38+
"Derived class must implement: static std::string PrintTestParam(TestType)");
39+
}
40+
41+
template <typename Derived>
42+
static std::string PrintFuncTestName(const GTestFuncParam<InType, OutType, TestType>& info) {
43+
RequireStaticInterface<Derived>();
44+
TestType test_param = std::get<ppc::util::GTestParamIndex::kTestParams>(info.param);
45+
return std::get<GTestParamIndex::kNameTest>(info.param) + "_" + Derived::PrintTestParam(test_param);
46+
}
47+
48+
protected:
49+
void ExecuteTest(FuncTestParam<InType, OutType, TestType> test_param) {
50+
ASSERT_FALSE(std::get<GTestParamIndex::kNameTest>(test_param).find("unknown") != std::string::npos);
51+
if (std::get<GTestParamIndex::kNameTest>(test_param).find("disabled") != std::string::npos) {
52+
GTEST_SKIP();
53+
}
54+
55+
task_ = std::get<GTestParamIndex::kTaskGetter>(test_param)(GetTestInputData());
56+
ASSERT_TRUE(task_->Validation());
57+
ASSERT_TRUE(task_->PreProcessing());
58+
ASSERT_TRUE(task_->Run());
59+
ASSERT_TRUE(task_->PostProcessing());
60+
ASSERT_TRUE(CheckTestOutputData(task_->GetOutput()));
61+
}
62+
63+
private:
64+
ppc::core::TaskPtr<InType, OutType> task_;
65+
};
66+
67+
template <typename Tuple, std::size_t... Is>
68+
auto ExpandToValuesImpl(const Tuple& t, std::index_sequence<Is...> /*unused*/) {
69+
return ::testing::Values(std::get<Is>(t)...);
70+
}
71+
72+
template <typename Tuple>
73+
auto ExpandToValues(const Tuple& t) {
74+
constexpr std::size_t kN = std::tuple_size_v<Tuple>;
75+
return ExpandToValuesImpl(t, std::make_index_sequence<kN>{});
76+
}
77+
78+
#define INIT_TASK_GENERATOR(InTypeParam, SizesParam) \
79+
template <typename Task, std::size_t... Is> \
80+
auto GenTaskTuplesImpl(std::index_sequence<Is...>) { \
81+
return std::make_tuple( \
82+
std::make_tuple(ppc::core::TaskGetter<Task, InTypeParam>, \
83+
std::string(ppc::util::GetNamespace<Task>()) + std::string("_") + \
84+
ppc::core::GetStringTaskType(Task::GetStaticTypeOfTask(), PPC_STUDENT_SETTINGS), \
85+
SizesParam[Is])...); \
86+
} \
87+
\
88+
template <typename Task> \
89+
auto TaskListGenerator() { \
90+
return GenTaskTuplesImpl<Task>(std::make_index_sequence<SizesParam.size()>{}); \
91+
}
92+
93+
} // namespace ppc::util

modules/core/util/include/test_util.hpp renamed to modules/core/util/include/perf_test_util.hpp

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,9 @@
1515

1616
namespace ppc::util {
1717

18-
enum GTestParamIndex : uint8_t { kTaskGetter, kNameTest, kTestParams };
19-
20-
template <typename InType, typename OutType, typename TestType = void>
21-
using FuncTestParam = std::tuple<std::function<ppc::core::TaskPtr<InType, OutType>(InType)>, std::string, TestType>;
22-
23-
template <typename InType, typename OutType, typename TestType = void>
24-
using GTestFuncParam = ::testing::TestParamInfo<FuncTestParam<InType, OutType, TestType>>;
25-
2618
template <typename InType, typename OutType>
27-
using PerfTestParam = FuncTestParam<InType, OutType, ppc::core::PerfResults::TypeOfRunning>;
19+
using PerfTestParam = std::tuple<std::function<ppc::core::TaskPtr<InType, OutType>(InType)>, std::string,
20+
ppc::core::PerfResults::TypeOfRunning>;
2821

2922
template <typename InType, typename OutType>
3023
class BaseRunPerfTests : public ::testing::TestWithParam<PerfTestParam<InType, OutType>> {
@@ -100,7 +93,7 @@ class BaseRunPerfTests : public ::testing::TestWithParam<PerfTestParam<InType, O
10093
ppc::core::TaskPtr<InType, OutType> task_;
10194
};
10295

103-
#define ADD_PERF_TASK(TaskType, InputTypeParam) \
96+
#define ADD_PERF_TASK(TaskType, InputTypeParam) \
10497
std::make_tuple(ppc::core::TaskGetter<TaskType, InputTypeParam>, \
10598
std::string(ppc::util::GetNamespace<TaskType>()) + std::string("_") + \
10699
ppc::core::GetStringTaskType(TaskType::GetStaticTypeOfTask(), PPC_STUDENT_SETTINGS), \
@@ -110,73 +103,4 @@ class BaseRunPerfTests : public ::testing::TestWithParam<PerfTestParam<InType, O
110103
ppc::core::GetStringTaskType(TaskType::GetStaticTypeOfTask(), PPC_STUDENT_SETTINGS), \
111104
ppc::core::PerfResults::TypeOfRunning::kTaskRun)
112105

113-
template <typename T, typename TestType>
114-
concept HasPrintTestParam = requires(TestType value) {
115-
{ T::PrintTestParam(value) } -> std::same_as<std::string>;
116-
};
117-
118-
template <typename InType, typename OutType, typename TestType = void>
119-
class BaseRunFuncTests : public ::testing::TestWithParam<FuncTestParam<InType, OutType, TestType>> {
120-
public:
121-
virtual bool CheckTestOutputData(OutType& output_data) = 0;
122-
virtual InType GetTestInputData() = 0;
123-
124-
template <typename Derived>
125-
static void RequireStaticInterface() {
126-
static_assert(HasPrintTestParam<Derived, TestType>,
127-
"Derived class must implement: static std::string PrintTestParam(TestType)");
128-
}
129-
130-
template <typename Derived>
131-
static std::string PrintFuncTestName(const GTestFuncParam<InType, OutType, TestType>& info) {
132-
RequireStaticInterface<Derived>();
133-
TestType test_param = std::get<ppc::util::GTestParamIndex::kTestParams>(info.param);
134-
return std::get<GTestParamIndex::kNameTest>(info.param) + "_" + Derived::PrintTestParam(test_param);
135-
}
136-
137-
protected:
138-
void ExecuteTest(FuncTestParam<InType, OutType, TestType> test_param) {
139-
ASSERT_FALSE(std::get<GTestParamIndex::kNameTest>(test_param).find("unknown") != std::string::npos);
140-
if (std::get<GTestParamIndex::kNameTest>(test_param).find("disabled") != std::string::npos) {
141-
GTEST_SKIP();
142-
}
143-
144-
task_ = std::get<GTestParamIndex::kTaskGetter>(test_param)(GetTestInputData());
145-
ASSERT_TRUE(task_->Validation());
146-
ASSERT_TRUE(task_->PreProcessing());
147-
ASSERT_TRUE(task_->Run());
148-
ASSERT_TRUE(task_->PostProcessing());
149-
ASSERT_TRUE(CheckTestOutputData(task_->GetOutput()));
150-
}
151-
152-
private:
153-
ppc::core::TaskPtr<InType, OutType> task_;
154-
};
155-
156-
template <typename Tuple, std::size_t... Is>
157-
auto ExpandToValuesImpl(const Tuple& t, std::index_sequence<Is...> /*unused*/) {
158-
return ::testing::Values(std::get<Is>(t)...);
159-
}
160-
161-
template <typename Tuple>
162-
auto ExpandToValues(const Tuple& t) {
163-
constexpr std::size_t kN = std::tuple_size_v<Tuple>;
164-
return ExpandToValuesImpl(t, std::make_index_sequence<kN>{});
165-
}
166-
167-
#define INIT_TASK_GENERATOR(InTypeParam, SizesParam) \
168-
template <typename Task, std::size_t... Is> \
169-
auto GenTaskTuplesImpl(std::index_sequence<Is...>) { \
170-
return std::make_tuple( \
171-
std::make_tuple(ppc::core::TaskGetter<Task, InTypeParam>, \
172-
std::string(ppc::util::GetNamespace<Task>()) + std::string("_") + \
173-
ppc::core::GetStringTaskType(Task::GetStaticTypeOfTask(), PPC_STUDENT_SETTINGS), \
174-
SizesParam[Is])...); \
175-
} \
176-
\
177-
template <typename Task> \
178-
auto TaskListGenerator() { \
179-
return GenTaskTuplesImpl<Task>(std::make_index_sequence<SizesParam.size()>{}); \
180-
}
181-
182106
} // namespace ppc::util

modules/core/util/include/util.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace ppc::util {
1515

16+
enum GTestParamIndex : uint8_t { kTaskGetter, kNameTest, kTestParams };
17+
1618
std::string GetAbsolutePath(const std::string &relative_path);
1719
int GetNumThreads();
1820

tasks/example/common/include/common.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
#include <vector>
55

66
#include "core/task/include/task.hpp"
7-
#include "core/util/include/test_util.hpp"
87
#include "core/util/include/util.hpp"
98

109
namespace nesterov_a_test_task {
1110

1211
using InType = std::vector<int>;
1312
using OutType = std::vector<int>;
1413
using TestType = std::tuple<int, std::string>;
15-
1614
using BaseTask = ppc::core::Task<InType, OutType>;
17-
using BasePerfTests = ppc::util::BaseRunPerfTests<InType, OutType>;
18-
using BaseFuncTests = ppc::util::BaseRunFuncTests<InType, OutType, TestType>;
1915

2016
} // namespace nesterov_a_test_task

tasks/example/tests/functional/main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <utility>
88
#include <vector>
99

10-
#include "core/util/include/test_util.hpp"
10+
#include "core/util/include/func_test_util.hpp"
1111
#include "core/util/include/util.hpp"
1212
#include "example/all/include/ops_all.hpp"
1313
#include "example/mpi/include/ops_mpi.hpp"
@@ -18,9 +18,7 @@
1818

1919
namespace nesterov_a_test_task {
2020

21-
class NesterovARunFuncTests : public BaseFuncTests {
22-
InType input_data_;
23-
21+
class NesterovARunFuncTests : public ppc::util::BaseRunFuncTests<InType, OutType, TestType> {
2422
public:
2523
static std::string PrintTestParam(TestType test_param) {
2624
return std::to_string(std::get<0>(test_param)) + "_" + std::get<1>(test_param);
@@ -57,6 +55,9 @@ class NesterovARunFuncTests : public BaseFuncTests {
5755
bool CheckTestOutputData(OutType &output_data) final { return input_data_ == output_data; }
5856

5957
InType GetTestInputData() final { return input_data_; }
58+
59+
private:
60+
InType input_data_;
6061
};
6162

6263
namespace {

tasks/example/tests/performance/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <vector>
88

99
#include "core/perf/include/perf.hpp"
10-
#include "core/util/include/test_util.hpp"
10+
#include "core/util/include/perf_test_util.hpp"
1111
#include "core/util/include/util.hpp"
1212
#include "example/all/include/ops_all.hpp"
1313
#include "example/mpi/include/ops_mpi.hpp"
@@ -18,7 +18,7 @@
1818

1919
namespace nesterov_a_test_task {
2020

21-
class ExampleRunPerfTest : public BasePerfTests {
21+
class ExampleRunPerfTest : public ppc::util::BaseRunPerfTests<InType, OutType> {
2222
static constexpr std::vector<int>::size_type kCount = 400;
2323
InType input_data_;
2424

0 commit comments

Comments
 (0)