Skip to content

Commit 376dfa4

Browse files
committed
Refactor test utilities and improve image handling.
Extracted data preparation logic into a dedicated function for better readability and modularization. Introduced a new input member in the base test class to streamline test input management. Adjusted lambda and tuple type naming for consistency and clarity.
1 parent c6edfda commit 376dfa4

2 files changed

Lines changed: 40 additions & 37 deletions

File tree

modules/core/util/include/test_util.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ inline std::string GetStringParamName(ppc::core::PerfResults::TypeOfRunning type
1919
return "none";
2020
}
2121

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>;
22+
template <typename InType, typename OutType, typename TestType = void>
23+
using FuncTestParam = std::tuple<std::function<ppc::core::TaskPtr<InType, OutType>(InType)>, std::string, TestType>;
2424

2525
template <typename InType, typename OutType>
2626
using PerfTestParam = FuncTestParam<InType, OutType, ppc::core::PerfResults::TypeOfRunning>;
@@ -80,22 +80,22 @@ class BaseRunPerfTests : public ::testing::TestWithParam<PerfTestParam<InType, O
8080
template <typename InType, typename OutType, typename TestType = void>
8181
class BaseRunFuncTests : public ::testing::TestWithParam<ppc::util::FuncTestParam<InType, OutType, TestType>> {
8282
public:
83-
virtual void CheckTestOutputData() = 0;
84-
ppc::core::TaskPtr<InType, OutType>& GetTaskPtr() {
85-
return task;
86-
}
87-
88-
protected:
89-
void ExecuteTest() {
90-
ASSERT_TRUE(task->Validation());
91-
ASSERT_TRUE(task->PreProcessing());
92-
ASSERT_TRUE(task->Run());
93-
ASSERT_TRUE(task->PostProcessing());
94-
CheckTestOutputData();
95-
}
83+
virtual void CheckTestOutputData() = 0;
84+
ppc::core::TaskPtr<InType, OutType>& GetTaskPtr() { return task; }
85+
InType& GetTestInput() { return test_input_; }
86+
87+
protected:
88+
void ExecuteTest() {
89+
ASSERT_TRUE(task->Validation());
90+
ASSERT_TRUE(task->PreProcessing());
91+
ASSERT_TRUE(task->Run());
92+
ASSERT_TRUE(task->PostProcessing());
93+
CheckTestOutputData();
94+
}
9695

9796
private:
9897
ppc::core::TaskPtr<InType, OutType> task;
98+
InType test_input_;
9999
};
100100

101101
} // namespace ppc::util

tasks/example/tests/func_tests/main.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,41 @@ using TestType = int;
2121

2222
class NesterovARunFuncTests : public ppc::util::BaseRunFuncTests<InType, OutType, TestType> {
2323
public:
24-
static std::string CustomFuncTestName(
25-
const ::testing::TestParamInfo<ppc::util::FuncTestParam<InType, OutType, TestType>>& info) {
26-
std::string test_name = std::get<ppc::util::FuncTestParamIndex::kNameTest>(info.param);
27-
std::string test_param = std::to_string(std::get<ppc::util::FuncTestParamIndex::kTestParams>(info.param));
28-
return test_name + "_" + test_param;
29-
}
24+
static std::string CustomFuncTestName(
25+
const ::testing::TestParamInfo<ppc::util::FuncTestParam<InType, OutType, TestType>> &info) {
26+
std::string test_name = std::get<ppc::util::FuncTestParamIndex::kNameTest>(info.param);
27+
std::string test_param = std::to_string(std::get<ppc::util::FuncTestParamIndex::kTestParams>(info.param));
28+
return test_name + "_" + test_param;
29+
}
3030

3131
protected:
3232
void SetUp() override {
33+
PrepareData();
34+
GetTaskPtr() = std::get<ppc::util::FuncTestParamIndex::kTaskGetter>(GetParam())(GetTestInput());
35+
}
36+
37+
void PrepareData() {
38+
int width = -1;
39+
int height = -1;
40+
int channels = -1;
3341
// Read image
34-
std::string abs_path = ppc::util::GetAbsolutePath("example/tests/data/pic_all.jpg");
35-
data = stbi_load(abs_path.c_str(), &width, &height, &channels, 0);
36-
ASSERT_TRUE(data != nullptr) << "Failed to load image: " << stbi_failure_reason();
37-
img = std::vector<uint8_t>(data, data + (static_cast<ptrdiff_t>(width * height * channels)));
38-
stbi_image_free(data);
39-
ASSERT_EQ(width, height);
42+
{
43+
std::string abs_path = ppc::util::GetAbsolutePath("example/tests/data/pic_all.jpg");
44+
auto *data = stbi_load(abs_path.c_str(), &width, &height, &channels, 0);
45+
ASSERT_TRUE(data != nullptr) << "Failed to load image: " << stbi_failure_reason();
46+
auto img = std::vector<uint8_t>(data, data + (static_cast<ptrdiff_t>(width * height * channels)));
47+
stbi_image_free(data);
48+
ASSERT_EQ(width, height);
49+
}
4050

4151
const int k_count = (width + height) / std::get<ppc::util::FuncTestParamIndex::kTestParams>(GetParam());
42-
std::vector<int> in(static_cast<std::vector<int>::size_type>(k_count * k_count), 0);
52+
GetTestInput() = InType(static_cast<std::vector<int>::size_type>(k_count * k_count), 0);
4353
for (int i = 0; i < k_count; i++) {
44-
in[(i * k_count) + i] = 1;
54+
GetTestInput()[(i * k_count) + i] = 1;
4555
}
46-
GetTaskPtr() = std::get<ppc::util::FuncTestParamIndex::kTaskGetter>(GetParam())(in);
47-
}
48-
49-
void CheckTestOutputData() final {
50-
EXPECT_EQ(GetTaskPtr()->GetInput(), GetTaskPtr()->GetOutput());
5156
}
5257

53-
int width = -1, height = -1, channels = -1;
54-
unsigned char* data = nullptr;
55-
std::vector<uint8_t> img;
58+
void CheckTestOutputData() final { EXPECT_EQ(GetTestInput(), GetTaskPtr()->GetOutput()); }
5659
};
5760

5861
TEST_P(NesterovARunFuncTests, MatmulFromPic) { ExecuteTest(); }

0 commit comments

Comments
 (0)