88#include " core/task/tests/test_task.hpp"
99
1010TEST (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
2820TEST (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
4329TEST (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
5938TEST (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-
11348TEST (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
13158TEST (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-
15769TEST (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
17788TEST (TaskTest, GetStringTaskStatus_Disabled) {
17889 EXPECT_EQ (GetStringTaskStatus (ppc::core::StatusOfTask::kDisabled ), " disabled" );
17990}
18091
181- // Test for enabled status string
18292TEST (TaskTest, GetStringTaskStatus_Enabled) {
18393 EXPECT_EQ (GetStringTaskStatus (ppc::core::StatusOfTask::kEnabled ), " enabled" );
18494}
18595
186- // Test for exception thrown when file cannot be opened
18796TEST (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
192100TEST (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
235187int main (int argc, char **argv) {
0 commit comments