Skip to content

Commit 9cce2f3

Browse files
committed
Refactor test parametrization and naming for clarity
Refactored test parameter structs and macros to enhance readability and flexibility, including the addition of `FuncTestParam` and `PerfTestParam` types. Updated test case naming conventions with custom functions for better identification. Simplified utility functions and macros for improved code maintainability.
1 parent 276601b commit 9cce2f3

4 files changed

Lines changed: 90 additions & 52 deletions

File tree

modules/core/util/include/test_util.hpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,41 @@
77

88
namespace ppc::util {
99

10+
enum FuncTestParamIndex : uint8_t { kTaskGetter, kNameTest, kAddParams };
11+
12+
inline std::string GetStringParamName(ppc::core::PerfResults::TypeOfRunning type_of_running) {
13+
if (type_of_running == core::PerfResults::kTaskRun) {
14+
return "task_run";
15+
}
16+
if (type_of_running == core::PerfResults::kPipeline) {
17+
return "pipeline";
18+
}
19+
return "none";
20+
}
21+
22+
template <typename InType, typename OutType, typename AddParams = void>
23+
using FuncTestParam = std::tuple<std::function<ppc::core::TaskPtr<InType, OutType>(InType)>, std::string, AddParams>;
24+
1025
template <typename InType, typename OutType>
11-
using TestParam = std::tuple<ppc::core::PerfResults::TypeOfRunning,
12-
std::function<ppc::core::TaskPtr<InType, OutType>(InType)>, std::string>;
26+
using PerfTestParam = FuncTestParam<InType, OutType, ppc::core::PerfResults::TypeOfRunning>;
1327

1428
template <typename InType, typename OutType>
15-
class BaseRunPerfTests : public ::testing::TestWithParam<TestParam<InType, OutType>> {
29+
class BaseRunPerfTests : public ::testing::TestWithParam<PerfTestParam<InType, OutType>> {
30+
public:
31+
static std::string CustomPerfTestName(const ::testing::TestParamInfo<PerfTestParam<InType, OutType>>& info) {
32+
return ppc::util::GetStringParamName(std::get<FuncTestParamIndex::kAddParams>(info.param)) + "_" +
33+
std::get<FuncTestParamIndex::kNameTest>(info.param);
34+
}
35+
1636
protected:
1737
virtual void SetPerfAttributes(ppc::core::PerfAttr& perf_attrs) = 0;
1838
virtual bool CheckTestOutputData(OutType& output_data) = 0;
1939
virtual InType GetTestInputData() = 0;
2040

2141
void ExecuteTest(ppc::core::PerfResults::TypeOfRunning mode,
2242
std::function<ppc::core::TaskPtr<InType, OutType>(InType)> task_getter, std::string test_name) {
23-
task = task_getter(GetTestInputData());
24-
ppc::core::Perf perf(task);
43+
task_ = task_getter(GetTestInputData());
44+
ppc::core::Perf perf(task_);
2545
ppc::core::PerfAttr perf_attr;
2646
SetPerfAttributes(perf_attr);
2747

@@ -40,18 +60,18 @@ class BaseRunPerfTests : public ::testing::TestWithParam<TestParam<InType, OutTy
4060
if (rank == 0) {
4161
perf.PrintPerfStatistic(test_name);
4262
}
43-
OutType output_data = task->GetOutput();
63+
OutType output_data = task_->GetOutput();
4464
ASSERT_TRUE(CheckTestOutputData(output_data));
4565
}
4666

4767
private:
48-
ppc::core::TaskPtr<InType, OutType> task;
68+
ppc::core::TaskPtr<InType, OutType> task_;
4969
};
5070

51-
#define ADD_MODES(TaskType, InputTypeParam) \
52-
std::make_tuple(ppc::core::PerfResults::TypeOfRunning::kPipeline, ppc::core::TaskGetter<TaskType, InputTypeParam>, \
53-
ppc::util::GetNamespace<TaskType>()), \
54-
std::make_tuple(ppc::core::PerfResults::TypeOfRunning::kTaskRun, \
55-
ppc::core::TaskGetter<TaskType, InputTypeParam>, ppc::util::GetNamespace<TaskType>())
71+
#define ADD_PERF_MODES(TaskType, InputTypeParam) \
72+
std::make_tuple(ppc::core::TaskGetter<TaskType, InputTypeParam>, ppc::util::GetNamespace<TaskType>(), \
73+
ppc::core::PerfResults::TypeOfRunning::kPipeline), \
74+
std::make_tuple(ppc::core::TaskGetter<TaskType, InputTypeParam>, ppc::util::GetNamespace<TaskType>(), \
75+
ppc::core::PerfResults::TypeOfRunning::kTaskRun)
5676

5777
} // namespace ppc::util

modules/core/util/include/util.hpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include <string_view>
44

55
/* NOLINTBEGIN */
6-
#define INSTANTIATE_TEST_SUITE_P_NOLINT(prefix, test_case_name, generator) \
7-
INSTANTIATE_TEST_SUITE_P(prefix, test_case_name, generator)
6+
#define INSTANTIATE_TEST_SUITE_P_NOLINT(prefix, test_case_name, generator, custom_test_name) \
7+
INSTANTIATE_TEST_SUITE_P(prefix, test_case_name, generator, custom_test_name)
88
/* NOLINTEND */
99

1010
/* NOLINTBEGIN */
@@ -16,29 +16,35 @@ namespace ppc::util {
1616
std::string GetAbsolutePath(const std::string &relative_path);
1717
int GetPPCNumThreads();
1818

19+
#if defined(__clang__) || defined(__GNUC__)
1920
template <typename T>
2021
consteval std::string_view GetNamespace() {
21-
#if defined(__clang__) || defined(__GNUC__)
22-
constexpr std::string_view func = __PRETTY_FUNCTION__;
22+
constexpr std::string_view kFunc = __PRETTY_FUNCTION__;
2323
// example: "consteval std::string_view get_namespace() [with T = my_namespace::MyClass]"
24-
constexpr std::string_view key = "T = ";
24+
constexpr std::string_view kKey = "T = ";
2525
#elif defined(_MSC_VER)
26-
constexpr std::string_view func = __FUNCSIG__;
26+
template <typename T>
27+
constexpr std::string_view GetNamespace() {
28+
constexpr std::string_view kFunc = __FUNCSIG__;
2729
// example: "class std::basic_string_view<char,struct std::char_traits<char> > __cdecl get_namespace<class
2830
// my_namespace::MyClass>(void)"
29-
constexpr std::string_view key = "get_namespace<";
31+
constexpr std::string_view kKey = "get_namespace<";
3032
#else
31-
static_assert(false, "Unsupported compiler");
33+
static_assert(false, "Unsupported compiler");
3234
#endif
3335

34-
auto start = func.find(key);
35-
if (start == std::string_view::npos) return {};
36-
start += key.size();
36+
auto start = kFunc.find(kKey);
37+
if (start == std::string_view::npos) {
38+
return {};
39+
}
40+
start += kKey.size();
3741

38-
auto end = func.find("::", start);
39-
if (end == std::string_view::npos) return {};
42+
auto end = kFunc.find("::", start);
43+
if (end == std::string_view::npos) {
44+
return {};
45+
}
4046

41-
return func.substr(start, end - start);
47+
return kFunc.substr(start, end - start);
4248
}
4349

4450
} // namespace ppc::util

tasks/example/tests/func_tests/main.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include <gtest/gtest.h>
22

3+
#include <cstddef>
34
#include <cstdint>
45
#include <stb_library.hpp>
56
#include <string>
67
#include <vector>
78

9+
#include "core/util/include/test_util.hpp"
810
#include "core/util/include/util.hpp"
911
#include "example/all/include/ops_all.hpp"
1012
#include "example/mpi/include/ops_mpi.hpp"
@@ -16,25 +18,29 @@
1618
using InType = std::vector<int>;
1719
using OutType = std::vector<int>;
1820

19-
using TestParam = std::tuple<int, std::function<ppc::core::TaskPtr<InType, OutType>(InType)>>;
21+
class NesterovARunFuncTests : public ::testing::TestWithParam<ppc::util::FuncTestParam<InType, OutType, int>> {
22+
public:
23+
static std::string CustomFuncTestName(
24+
const ::testing::TestParamInfo<ppc::util::FuncTestParam<InType, OutType, int>>& info) {
25+
return std::to_string(info.index);
26+
}
2027

21-
class NesterovARunFuncTests : public ::testing::TestWithParam<TestParam> {
2228
protected:
2329
void SetUp() override {
2430
// Read image
2531
std::string abs_path = ppc::util::GetAbsolutePath("example/tests/data/pic_all.jpg");
2632
data = stbi_load(abs_path.c_str(), &width, &height, &channels, 0);
2733
ASSERT_TRUE(data != nullptr) << "Failed to load image: " << stbi_failure_reason();
28-
img = std::vector<uint8_t>(data, data + (width * height * channels));
34+
img = std::vector<uint8_t>(data, data + (static_cast<ptrdiff_t>(width * height * channels)));
2935
stbi_image_free(data);
3036
ASSERT_EQ(width, height);
3137

32-
const int k_count = (width + height) / std::get<0>(GetParam());
33-
std::vector<int> in(k_count * k_count, 0);
38+
const int k_count = (width + height) / std::get<ppc::util::FuncTestParamIndex::kAddParams>(GetParam());
39+
std::vector<int> in(static_cast<std::vector<int>::size_type>(k_count * k_count), 0);
3440
for (int i = 0; i < k_count; i++) {
3541
in[(i * k_count) + i] = 1;
3642
}
37-
task = std::get<1>(GetParam())(in);
43+
task = std::get<ppc::util::FuncTestParamIndex::kTaskGetter>(GetParam())(in);
3844
}
3945

4046
void ExecuteTest() {
@@ -53,13 +59,15 @@ class NesterovARunFuncTests : public ::testing::TestWithParam<TestParam> {
5359

5460
TEST_P(NesterovARunFuncTests, MatmulFromPic) { ExecuteTest(); }
5561

56-
#define ADD_TASK(TASK) \
57-
std::make_tuple(5, ppc::core::TaskGetter<TASK, InType>), std::make_tuple(10, ppc::core::TaskGetter<TASK, InType>)
62+
#define ADD_FUNC_TASK(TASK) \
63+
std::make_tuple(ppc::core::TaskGetter<TASK, InType>, "", 5), \
64+
std::make_tuple(ppc::core::TaskGetter<TASK, InType>, "", 10)
5865

5966
INSTANTIATE_TEST_SUITE_P_NOLINT(PicMatrixTests, NesterovARunFuncTests,
60-
::testing::Values(ADD_TASK(nesterov_a_test_task_all::TestTaskALL),
61-
ADD_TASK(nesterov_a_test_task_mpi::TestTaskMPI),
62-
ADD_TASK(nesterov_a_test_task_omp::TestTaskOMP),
63-
ADD_TASK(nesterov_a_test_task_seq::TestTaskSEQ),
64-
ADD_TASK(nesterov_a_test_task_stl::TestTaskSTL),
65-
ADD_TASK(nesterov_a_test_task_tbb::TestTaskTBB)));
67+
::testing::Values(ADD_FUNC_TASK(nesterov_a_test_task_all::TestTaskALL),
68+
ADD_FUNC_TASK(nesterov_a_test_task_mpi::TestTaskMPI),
69+
ADD_FUNC_TASK(nesterov_a_test_task_omp::TestTaskOMP),
70+
ADD_FUNC_TASK(nesterov_a_test_task_seq::TestTaskSEQ),
71+
ADD_FUNC_TASK(nesterov_a_test_task_stl::TestTaskSTL),
72+
ADD_FUNC_TASK(nesterov_a_test_task_tbb::TestTaskTBB)),
73+
NesterovARunFuncTests::CustomFuncTestName);

tasks/example/tests/perf_tests/main.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ using OutType = std::vector<int>;
2020

2121
class ExampleRunPerfTest : public ppc::util::BaseRunPerfTests<InType, OutType> {
2222
static constexpr int kCount = 400;
23-
InType input_data;
23+
InType input_data_;
2424

2525
void SetUp() override {
26-
input_data.assign(kCount * kCount, 0);
26+
input_data_.assign(kCount * kCount, 0);
2727
for (int i = 0; i < kCount; ++i) {
28-
input_data[(i * kCount) + i] = 1;
28+
input_data_[(i * kCount) + i] = 1;
2929
}
3030
}
3131

@@ -38,19 +38,23 @@ class ExampleRunPerfTest : public ppc::util::BaseRunPerfTests<InType, OutType> {
3838
};
3939
}
4040

41-
bool CheckTestOutputData(OutType& output_data) final { return input_data == output_data; }
41+
bool CheckTestOutputData(OutType& output_data) final { return input_data_ == output_data; }
4242

43-
InType GetTestInputData() final { return input_data; }
43+
InType GetTestInputData() final { return input_data_; }
4444
};
4545

4646
TEST_P(ExampleRunPerfTest, RunModes) {
47-
ExecuteTest(std::get<0>(GetParam()), std::get<1>(GetParam()), std::get<2>(GetParam()));
47+
auto task_getter = std::get<ppc::util::FuncTestParamIndex::kTaskGetter>(GetParam());
48+
auto test_name = std::get<ppc::util::FuncTestParamIndex::kNameTest>(GetParam());
49+
auto perf_type = std::get<ppc::util::FuncTestParamIndex::kAddParams>(GetParam());
50+
ExecuteTest(perf_type, task_getter, test_name);
4851
}
4952

5053
INSTANTIATE_TEST_SUITE_P_NOLINT(RunModeTests, ExampleRunPerfTest,
51-
::testing::Values(ADD_MODES(nesterov_a_test_task_all::TestTaskALL, InType),
52-
ADD_MODES(nesterov_a_test_task_mpi::TestTaskMPI, InType),
53-
ADD_MODES(nesterov_a_test_task_omp::TestTaskOMP, InType),
54-
ADD_MODES(nesterov_a_test_task_seq::TestTaskSEQ, InType),
55-
ADD_MODES(nesterov_a_test_task_stl::TestTaskSTL, InType),
56-
ADD_MODES(nesterov_a_test_task_tbb::TestTaskTBB, InType)));
54+
::testing::Values(ADD_PERF_MODES(nesterov_a_test_task_all::TestTaskALL, InType),
55+
ADD_PERF_MODES(nesterov_a_test_task_mpi::TestTaskMPI, InType),
56+
ADD_PERF_MODES(nesterov_a_test_task_omp::TestTaskOMP, InType),
57+
ADD_PERF_MODES(nesterov_a_test_task_seq::TestTaskSEQ, InType),
58+
ADD_PERF_MODES(nesterov_a_test_task_stl::TestTaskSTL, InType),
59+
ADD_PERF_MODES(nesterov_a_test_task_tbb::TestTaskTBB, InType)),
60+
ExampleRunPerfTest::CustomPerfTestName);

0 commit comments

Comments
 (0)