Skip to content

Commit ee536fe

Browse files
committed
Refactor test execution and data handling in func_tests
Refactored the `BaseRunFuncTests` class to simplify and improve test execution and data handling. Replaced `GetTestInput` and `GetTaskPtr` with `GetTestInputData` and `CheckTestOutputData` for cleaner abstraction. Updated error handling and validation logic for better test reliability and maintainability.
1 parent 376dfa4 commit ee536fe

2 files changed

Lines changed: 27 additions & 26 deletions

File tree

modules/core/util/include/test_util.hpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class BaseRunPerfTests : public ::testing::TestWithParam<PerfTestParam<InType, O
3838
virtual bool CheckTestOutputData(OutType& output_data) = 0;
3939
virtual InType GetTestInputData() = 0;
4040

41-
void ExecuteTest(const PerfTestParam<InType, OutType>& perfTestParam) {
42-
auto task_getter = std::get<ppc::util::FuncTestParamIndex::kTaskGetter>(perfTestParam);
43-
auto test_name = std::get<ppc::util::FuncTestParamIndex::kNameTest>(perfTestParam);
44-
auto mode = std::get<ppc::util::FuncTestParamIndex::kTestParams>(perfTestParam);
41+
void ExecuteTest(const PerfTestParam<InType, OutType>& perf_test_param) {
42+
auto task_getter = std::get<ppc::util::FuncTestParamIndex::kTaskGetter>(perf_test_param);
43+
auto test_name = std::get<ppc::util::FuncTestParamIndex::kNameTest>(perf_test_param);
44+
auto mode = std::get<ppc::util::FuncTestParamIndex::kTestParams>(perf_test_param);
4545

4646
task_ = task_getter(GetTestInputData());
4747
ppc::core::Perf perf(task_);
@@ -80,22 +80,21 @@ 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() { return task; }
85-
InType& GetTestInput() { return test_input_; }
83+
virtual bool CheckTestOutputData(OutType& output_data) = 0;
84+
virtual InType GetTestInputData() = 0;
8685

8786
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();
87+
void ExecuteTest(ppc::util::FuncTestParam<InType, OutType, TestType> test_param) {
88+
task_ = std::get<ppc::util::FuncTestParamIndex::kTaskGetter>(test_param)(GetTestInputData());
89+
ASSERT_TRUE(task_->Validation());
90+
ASSERT_TRUE(task_->PreProcessing());
91+
ASSERT_TRUE(task_->Run());
92+
ASSERT_TRUE(task_->PostProcessing());
93+
ASSERT_TRUE(CheckTestOutputData(task_->GetOutput()));
9494
}
9595

9696
private:
97-
ppc::core::TaskPtr<InType, OutType> task;
98-
InType test_input_;
97+
ppc::core::TaskPtr<InType, OutType> task_;
9998
};
10099

101100
} // namespace ppc::util

tasks/example/tests/func_tests/main.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ using OutType = std::vector<int>;
2020
using TestType = int;
2121

2222
class NesterovARunFuncTests : public ppc::util::BaseRunFuncTests<InType, OutType, TestType> {
23+
InType input_data_;
2324
public:
2425
static std::string CustomFuncTestName(
2526
const ::testing::TestParamInfo<ppc::util::FuncTestParam<InType, OutType, TestType>> &info) {
@@ -30,35 +31,36 @@ class NesterovARunFuncTests : public ppc::util::BaseRunFuncTests<InType, OutType
3031

3132
protected:
3233
void SetUp() override {
33-
PrepareData();
34-
GetTaskPtr() = std::get<ppc::util::FuncTestParamIndex::kTaskGetter>(GetParam())(GetTestInput());
35-
}
36-
37-
void PrepareData() {
3834
int width = -1;
3935
int height = -1;
4036
int channels = -1;
4137
// Read image
4238
{
4339
std::string abs_path = ppc::util::GetAbsolutePath("example/tests/data/pic_all.jpg");
4440
auto *data = stbi_load(abs_path.c_str(), &width, &height, &channels, 0);
45-
ASSERT_TRUE(data != nullptr) << "Failed to load image: " << stbi_failure_reason();
41+
if (data == nullptr) {
42+
throw std::runtime_error("Failed to load image: " + std::string(stbi_failure_reason()));
43+
}
4644
auto img = std::vector<uint8_t>(data, data + (static_cast<ptrdiff_t>(width * height * channels)));
4745
stbi_image_free(data);
48-
ASSERT_EQ(width, height);
46+
if(width != height) {
47+
throw std::runtime_error("width != height: ");
48+
}
4949
}
5050

5151
const int k_count = (width + height) / std::get<ppc::util::FuncTestParamIndex::kTestParams>(GetParam());
52-
GetTestInput() = InType(static_cast<std::vector<int>::size_type>(k_count * k_count), 0);
52+
input_data_ = InType(static_cast<std::vector<int>::size_type>(k_count * k_count), 0);
5353
for (int i = 0; i < k_count; i++) {
54-
GetTestInput()[(i * k_count) + i] = 1;
54+
input_data_[(i * k_count) + i] = 1;
5555
}
5656
}
5757

58-
void CheckTestOutputData() final { EXPECT_EQ(GetTestInput(), GetTaskPtr()->GetOutput()); }
58+
bool CheckTestOutputData(OutType& output_data) final { return input_data_ == output_data; }
59+
60+
InType GetTestInputData() final { return input_data_; }
5961
};
6062

61-
TEST_P(NesterovARunFuncTests, MatmulFromPic) { ExecuteTest(); }
63+
TEST_P(NesterovARunFuncTests, MatmulFromPic) { ExecuteTest(GetParam()); }
6264

6365
#define ADD_FUNC_TASK(TASK) \
6466
std::make_tuple(ppc::core::TaskGetter<TASK, InType>, ppc::util::GetNamespace<TASK>(), 5), \

0 commit comments

Comments
 (0)