Skip to content

Commit e5ebbf6

Browse files
committed
refactor task tests: remove redundant comments, consolidate assertions, and streamline test case code for readability
1 parent 9958b4b commit e5ebbf6

File tree

2 files changed

+89
-140
lines changed

2 files changed

+89
-140
lines changed

modules/core/task/include/task.hpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,22 @@ inline std::string GetStringTaskType(TypeOfTask type_of_task, const std::string
7777
return type + "_" + std::string((*list_settings)["tasks"][type]);
7878
};
7979

80-
if (type_of_task == TypeOfTask::kALL) {
81-
return to_type_str("all");
80+
switch (type_of_task) {
81+
case TypeOfTask::kALL:
82+
return to_type_str("all");
83+
case TypeOfTask::kSTL:
84+
return to_type_str("stl");
85+
case TypeOfTask::kOMP:
86+
return to_type_str("omp");
87+
case TypeOfTask::kMPI:
88+
return to_type_str("mpi");
89+
case TypeOfTask::kTBB:
90+
return to_type_str("tbb");
91+
case TypeOfTask::kSEQ:
92+
return to_type_str("seq");
93+
default:
94+
return "unknown";
8295
}
83-
if (type_of_task == TypeOfTask::kSTL) {
84-
return to_type_str("stl");
85-
}
86-
if (type_of_task == TypeOfTask::kOMP) {
87-
return to_type_str("omp");
88-
}
89-
if (type_of_task == TypeOfTask::kMPI) {
90-
return to_type_str("mpi");
91-
}
92-
if (type_of_task == TypeOfTask::kTBB) {
93-
return to_type_str("tbb");
94-
}
95-
if (type_of_task == TypeOfTask::kSEQ) {
96-
return to_type_str("seq");
97-
}
98-
return "unknown";
9996
}
10097

10198
enum StateOfTesting : uint8_t { kFunc, kPerf };

modules/core/task/tests/task_tests.cpp

Lines changed: 74 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -8,152 +8,64 @@
88
#include "core/task/tests/test_task.hpp"
99

1010
TEST(task_tests, check_int32_t) {
11-
// Create data
1211
std::vector<int32_t> in(20, 1);
13-
14-
// Create and check Task
1512
ppc::test::task::TestTask<std::vector<int32_t>, int32_t> test_task(in);
16-
bool is_valid = test_task.Validation();
17-
ASSERT_EQ(is_valid, true);
18-
19-
// Run Task
13+
ASSERT_EQ(test_task.Validation(), true);
2014
test_task.PreProcessing();
2115
test_task.Run();
2216
test_task.PostProcessing();
23-
24-
// Check Result
2517
ASSERT_EQ(static_cast<size_t>(test_task.GetOutput()), in.size());
2618
}
2719

2820
TEST(task_tests, check_int32_t_slow) {
29-
// Create data
3021
std::vector<int32_t> in(20, 1);
31-
32-
// Create and check Task
3322
ppc::test::task::FakeSlowTask<std::vector<int32_t>, int32_t> test_task(in);
34-
bool is_valid = test_task.Validation();
35-
ASSERT_EQ(is_valid, true);
36-
37-
// Run Task
23+
ASSERT_EQ(test_task.Validation(), true);
3824
test_task.PreProcessing();
3925
test_task.Run();
4026
ASSERT_ANY_THROW(test_task.PostProcessing());
4127
}
4228

4329
TEST(task_tests, check_validate_func) {
44-
// Create data
4530
std::vector<int32_t> in;
46-
47-
// Create and check Task
4831
ppc::test::task::TestTask<std::vector<int32_t>, int32_t> test_task(in);
49-
bool is_valid = test_task.Validation();
50-
51-
// Check Result
52-
ASSERT_EQ(is_valid, false);
53-
32+
ASSERT_EQ(test_task.Validation(), false);
5433
test_task.PreProcessing();
5534
test_task.Run();
5635
test_task.PostProcessing();
5736
}
5837

5938
TEST(task_tests, check_double) {
60-
// Create data
6139
std::vector<double> in(20, 1);
62-
63-
// Create and check Task
6440
ppc::test::task::TestTask<std::vector<double>, double> test_task(in);
65-
bool is_valid = test_task.Validation();
66-
ASSERT_EQ(is_valid, true);
67-
68-
// Run Task
41+
ASSERT_EQ(test_task.Validation(), true);
6942
test_task.PreProcessing();
7043
test_task.Run();
7144
test_task.PostProcessing();
72-
73-
// Check Result
7445
EXPECT_NEAR(test_task.GetOutput(), static_cast<double>(in.size()), 1e-6);
7546
}
7647

77-
TEST(task_tests, check_uint8_t) {
78-
// Create data
79-
std::vector<uint8_t> in(20, 1);
80-
81-
// Create Task
82-
ppc::test::task::TestTask<std::vector<uint8_t>, uint8_t> test_task(in);
83-
bool is_valid = test_task.Validation();
84-
ASSERT_EQ(is_valid, true);
85-
86-
// Run Task
87-
test_task.PreProcessing();
88-
test_task.Run();
89-
test_task.PostProcessing();
90-
91-
// Check Result
92-
ASSERT_EQ(static_cast<size_t>(test_task.GetOutput()), in.size());
93-
}
94-
95-
TEST(task_tests, check_int64_t) {
96-
// Create data
97-
std::vector<int64_t> in(20, 1);
98-
99-
// Create Task
100-
ppc::test::task::TestTask<std::vector<int64_t>, int64_t> test_task(in);
101-
bool is_valid = test_task.Validation();
102-
ASSERT_EQ(is_valid, true);
103-
104-
// Run Task
105-
test_task.PreProcessing();
106-
test_task.Run();
107-
test_task.PostProcessing();
108-
109-
// Check Result
110-
ASSERT_EQ(static_cast<size_t>(test_task.GetOutput()), in.size());
111-
}
112-
11348
TEST(task_tests, check_float) {
114-
// Create data
11549
std::vector<float> in(20, 1);
116-
117-
// Create Task
11850
ppc::test::task::TestTask<std::vector<float>, float> test_task(in);
119-
bool is_valid = test_task.Validation();
120-
ASSERT_EQ(is_valid, true);
121-
122-
// Run Task
51+
ASSERT_EQ(test_task.Validation(), true);
12352
test_task.PreProcessing();
12453
test_task.Run();
12554
test_task.PostProcessing();
126-
127-
// Check Result
12855
EXPECT_NEAR(test_task.GetOutput(), in.size(), 1e-3);
12956
}
13057

13158
TEST(task_tests, check_wrong_order_disabled_valgrind) {
13259
auto destroy_function = [] {
133-
// Create data
13460
std::vector<float> in(20, 1);
135-
136-
// Create Task
13761
ppc::test::task::TestTask<std::vector<float>, float> test_task(in);
138-
bool is_valid = test_task.Validation();
139-
ASSERT_EQ(is_valid, true);
62+
ASSERT_EQ(test_task.Validation(), true);
14063
test_task.PreProcessing();
14164
test_task.PostProcessing();
14265
};
14366
EXPECT_DEATH_IF_SUPPORTED(destroy_function(), ".*ORDER OF FUNCTIONS IS NOT RIGHT.*");
14467
}
14568

146-
TEST(task_tests, check_empty_order_disabled_valgrind) {
147-
auto destroy_function = [] {
148-
// Create data
149-
std::vector<float> in(20, 1);
150-
151-
// Create Task
152-
ppc::test::task::TestTask<std::vector<float>, float> test_task(in);
153-
};
154-
EXPECT_DEATH_IF_SUPPORTED(destroy_function(), ".*ORDER OF FUNCTIONS IS NOT RIGHT.*");
155-
}
156-
15769
TEST(task_tests, premature_postprocessing_no_steps) {
15870
auto destroy_function = [] {
15971
std::vector<float> in(20, 1);
@@ -173,22 +85,18 @@ TEST(task_tests, premature_postprocessing_after_preprocessing) {
17385
EXPECT_DEATH_IF_SUPPORTED(destroy_function(), ".*ORDER OF FUNCTIONS IS NOT RIGHT.*");
17486
}
17587

176-
// Test for disabled status string
17788
TEST(TaskTest, GetStringTaskStatus_Disabled) {
17889
EXPECT_EQ(GetStringTaskStatus(ppc::core::StatusOfTask::kDisabled), "disabled");
17990
}
18091

181-
// Test for enabled status string
18292
TEST(TaskTest, GetStringTaskStatus_Enabled) {
18393
EXPECT_EQ(GetStringTaskStatus(ppc::core::StatusOfTask::kEnabled), "enabled");
18494
}
18595

186-
// Test for exception thrown when file cannot be opened
18796
TEST(TaskTest, GetStringTaskType_InvalidFileThrows) {
18897
EXPECT_THROW({ GetStringTaskType(ppc::core::TypeOfTask::kALL, "non_existing_file.json"); }, std::runtime_error);
18998
}
19099

191-
// Test unknown type case
192100
TEST(TaskTest, GetStringTaskType_UnknownType_WithValidFile) {
193101
std::string path = "settings_valid.json";
194102
std::ofstream file(path);
@@ -198,38 +106,82 @@ TEST(TaskTest, GetStringTaskType_UnknownType_WithValidFile) {
198106
EXPECT_NO_THROW({ GetStringTaskType(ppc::core::TypeOfTask::kUnknown, path); });
199107
}
200108

201-
// Death test for destructor when pipeline order is invalid
202-
TEST(TaskTest, Destructor_InvalidPipelineOrderTerminates) {
203-
testing::FLAGS_gtest_death_test_style = "threadsafe";
204-
auto test_func = [&] {
205-
struct BadTask : ppc::core::Task<int, int> {
206-
bool ValidationImpl() override { return true; }
207-
bool PreProcessingImpl() override { return true; }
208-
bool RunImpl() override { return true; }
209-
bool PostProcessingImpl() override { return true; }
210-
} task;
211-
};
212-
ASSERT_DEATH_IF_SUPPORTED({ test_func(); }, "");
109+
TEST(TaskTest, GetStringTaskType_ThrowsOnBadJSON) {
110+
std::string path = "bad_settings.json";
111+
std::ofstream file(path);
112+
file << "{";
113+
file.close();
114+
EXPECT_THROW({ GetStringTaskType(ppc::core::TypeOfTask::kALL, path); }, std::exception);
213115
}
214116

215-
// Make sure this sucker writes proper JSON so GetStringTaskType doesn't choke
216-
TEST(TaskTest, GetStringTaskType_UnknownType_ValidJSON) {
217-
std::string path = "settings_valid.json";
117+
TEST(TaskTest, GetStringTaskType_EachType_WithValidFile) {
118+
std::string path = "settings_valid_all.json";
218119
std::ofstream file(path);
219-
file << "{\"tasks\": {\"all\": \"enabled\", \"stl\": \"enabled\", \"omp\": \"enabled\", \"mpi\": \"enabled\", "
220-
"\"tbb\": \"enabled\", \"seq\": \"enabled\"}}";
120+
file << R"({"tasks": {"all": "enabled", "stl": "enabled", "omp": "enabled", "mpi": "enabled", "tbb": "enabled", "seq": "enabled"}})";
221121
file.close();
222-
auto result = GetStringTaskType(ppc::core::TypeOfTask::kALL, path);
223-
EXPECT_TRUE(result.find("all") != std::string::npos);
122+
123+
EXPECT_NO_THROW(ppc::core::GetStringTaskType(ppc::core::TypeOfTask::kALL, path));
124+
EXPECT_NO_THROW(ppc::core::GetStringTaskType(ppc::core::TypeOfTask::kSTL, path));
125+
EXPECT_NO_THROW(ppc::core::GetStringTaskType(ppc::core::TypeOfTask::kOMP, path));
126+
EXPECT_NO_THROW(ppc::core::GetStringTaskType(ppc::core::TypeOfTask::kMPI, path));
127+
EXPECT_NO_THROW(ppc::core::GetStringTaskType(ppc::core::TypeOfTask::kTBB, path));
128+
EXPECT_NO_THROW(ppc::core::GetStringTaskType(ppc::core::TypeOfTask::kSEQ, path));
224129
}
225130

226-
// Feed it garbage JSON and watch it crash like it should
227-
TEST(TaskTest, GetStringTaskType_ThrowsOnBadJSON) {
228-
std::string path = "bad_settings.json";
131+
TEST(TaskTest, GetStringTaskType_ReturnsUnknown_OnDefault) {
132+
std::string path = "settings_valid_unknown.json";
229133
std::ofstream file(path);
230-
file << "{"; // broken trash
134+
file << R"({"tasks": {"all": "enabled"}})";
231135
file.close();
232-
EXPECT_THROW({ GetStringTaskType(ppc::core::TypeOfTask::kALL, path); }, std::exception);
136+
137+
auto result = ppc::core::GetStringTaskType(ppc::core::TypeOfTask::kUnknown, path);
138+
EXPECT_EQ(result, "unknown");
139+
}
140+
141+
TEST(TaskTest, GetStringTaskType_ThrowsIfKeyMissing) {
142+
std::string path = "settings_partial.json";
143+
std::ofstream file(path);
144+
file << R"({"tasks": {"all": "enabled"}})";
145+
file.close();
146+
147+
EXPECT_ANY_THROW(ppc::core::GetStringTaskType(ppc::core::TypeOfTask::kSTL, path));
148+
}
149+
150+
TEST(TaskTest, TaskDestructor_ThrowsIfStageIncomplete) {
151+
testing::FLAGS_gtest_death_test_style = "threadsafe";
152+
auto destroy_func = [] {
153+
std::vector<int32_t> in(20, 1);
154+
struct LocalTask : ppc::core::Task<std::vector<int32_t>, int32_t> {
155+
LocalTask(const std::vector<int32_t>& in) { this->GetInput() = in; }
156+
bool ValidationImpl() override { return true; }
157+
bool PreProcessingImpl() override { return true; }
158+
bool RunImpl() override { return true; }
159+
bool PostProcessingImpl() override { return true; }
160+
} task(in);
161+
task.Validation();
162+
};
163+
EXPECT_DEATH_IF_SUPPORTED(destroy_func(), ".*ORDER OF FUNCTIONS IS NOT RIGHT.*");
164+
}
165+
166+
TEST(TaskTest, InternalTimeTest_ThrowsIfTimeoutExceeded) {
167+
struct SlowTask : ppc::core::Task<std::vector<int32_t>, int32_t> {
168+
SlowTask(const std::vector<int32_t>& in) { this->GetInput() = in; }
169+
bool ValidationImpl() override { return true; }
170+
bool PreProcessingImpl() override {
171+
std::this_thread::sleep_for(std::chrono::seconds(2));
172+
return true;
173+
}
174+
bool RunImpl() override { return true; }
175+
bool PostProcessingImpl() override { return true; }
176+
};
177+
178+
std::vector<int32_t> in(20, 1);
179+
SlowTask task(in);
180+
task.GetStateOfTesting() = ppc::core::StateOfTesting::kFunc;
181+
task.Validation();
182+
EXPECT_NO_THROW(task.PreProcessing());
183+
task.Run();
184+
EXPECT_THROW(task.PostProcessing(), std::runtime_error);
233185
}
234186

235187
int main(int argc, char **argv) {

0 commit comments

Comments
 (0)