99#include " core/performance/tests/test_task.hpp"
1010
1111TEST (perf_tests, check_perf_pipeline) {
12- // Create data
1312 std::vector<uint32_t > in (2000 , 1 );
1413
15- // Create Task
1614 auto test_task = std::make_shared<ppc::test::perf::TestTask<std::vector<uint32_t >, uint32_t >>(in);
1715
18- // Create Perf analyzer
1916 ppc::core::Perf<std::vector<uint32_t >, uint32_t > perf_analyzer (test_task);
2017
21- // Create Perf attributes
2218 ppc::core::PerfAttr perf_attr;
2319 perf_analyzer.PipelineRun (perf_attr);
2420
25- // Get perf statistic
2621 perf_analyzer.PrintPerfStatistic (" check_perf_pipeline" );
2722 ASSERT_LE (perf_analyzer.GetPerfResults ().time_sec , ppc::core::PerfResults::kMaxTime );
2823 EXPECT_EQ (test_task->GetOutput (), in.size ());
2924}
3025
3126TEST (perf_tests, check_perf_pipeline_float) {
32- // Create data
3327 std::vector<float > in (2000 , 1 );
3428
35- // Create Task
3629 auto test_task = std::make_shared<ppc::test::perf::TestTask<std::vector<float >, float >>(in);
3730
38- // Create Perf analyzer
3931 ppc::core::Perf<std::vector<float >, float > perf_analyzer (test_task);
4032
41- // Create Perf attributes
4233 ppc::core::PerfAttr perf_attr;
4334 perf_analyzer.PipelineRun (perf_attr);
4435
45- // Get perf statistic
4636 perf_analyzer.PrintPerfStatistic (" check_perf_pipeline_float" );
4737 ASSERT_LE (perf_analyzer.GetPerfResults ().time_sec , ppc::core::PerfResults::kMaxTime );
4838 EXPECT_EQ (test_task->GetOutput (), in.size ());
4939}
5040
5141TEST (perf_tests, check_perf_pipeline_uint8_t_slow_test) {
52- // Create data
5342 std::vector<uint8_t > in (128 , 1 );
5443
55- // Create Task
5644 auto test_task = std::make_shared<ppc::test::perf::FakePerfTask<std::vector<uint8_t >, uint8_t >>(in);
5745
58- // Create Perf analyzer
5946 ppc::core::Perf<std::vector<uint8_t >, uint8_t > perf_analyzer (test_task);
6047
61- // Create Perf attributes
6248 ppc::core::PerfAttr perf_attr;
6349 perf_attr.num_running = 1 ;
6450
@@ -70,46 +56,217 @@ TEST(perf_tests, check_perf_pipeline_uint8_t_slow_test) {
7056 };
7157 perf_analyzer.PipelineRun (perf_attr);
7258
73- // Get perf statistic
74- // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto)
75- ASSERT_ANY_THROW (perf_analyzer.PrintPerfStatistic (" check_perf_pipeline_uint8_t_slow_test" ));
59+ ASSERT_ANY_THROW_NOLINT (perf_analyzer.PrintPerfStatistic (" check_perf_pipeline_uint8_t_slow_test" ));
7660}
7761
7862TEST (perf_tests, check_perf_task_exception) {
79- // Create data
8063 std::vector<uint32_t > in (2000 , 1 );
8164
82- // Create Task
8365 auto test_task = std::make_shared<ppc::test::perf::TestTask<std::vector<uint32_t >, uint32_t >>(in);
8466
85- // Create Perf analyzer
8667 ppc::core::Perf<std::vector<uint32_t >, uint32_t > perf_analyzer (test_task);
8768
88- // Get perf statistic
89- // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto)
90- ASSERT_ANY_THROW (perf_analyzer.PrintPerfStatistic (" check_perf_task_exception" ));
69+ ASSERT_ANY_THROW_NOLINT (perf_analyzer.PrintPerfStatistic (" check_perf_task_exception" ));
9170
92- // Create Perf attributes
9371 ppc::core::PerfAttr perf_attr;
9472 perf_analyzer.TaskRun (perf_attr);
9573}
9674
9775TEST (perf_tests, check_perf_task_float) {
98- // Create data
9976 std::vector<float > in (2000 , 1 );
10077
101- // Create Task
10278 auto test_task = std::make_shared<ppc::test::perf::TestTask<std::vector<float >, float >>(in);
10379
104- // Create Perf analyzer
10580 ppc::core::Perf<std::vector<float >, float > perf_analyzer (test_task);
10681
107- // Create Perf attributes
10882 ppc::core::PerfAttr perf_attr;
10983 perf_analyzer.TaskRun (perf_attr);
11084
111- // Get perf statistic
11285 perf_analyzer.PrintPerfStatistic (" check_perf_task_float" );
11386 ASSERT_LE (perf_analyzer.GetPerfResults ().time_sec , ppc::core::PerfResults::kMaxTime );
11487 EXPECT_EQ (test_task->GetOutput (), in.size ());
11588}
89+
90+ struct ParamTestCase {
91+ ppc::core::PerfResults::TypeOfRunning input;
92+ std::string expected_output;
93+ };
94+
95+ class GetStringParamNameParamTest : public ::testing::TestWithParam<ParamTestCase> {};
96+
97+ TEST_P (GetStringParamNameParamTest, ReturnsExpectedString) {
98+ const auto & param = GetParam ();
99+ EXPECT_EQ (ppc::core::GetStringParamName (param.input ), param.expected_output );
100+ }
101+
102+ INSTANTIATE_TEST_SUITE_P (ParamTests, GetStringParamNameParamTest,
103+ ::testing::Values (ParamTestCase{ppc::core::PerfResults::kTaskRun , " task_run" },
104+ ParamTestCase{ppc::core::PerfResults::kPipeline , " pipeline" },
105+ ParamTestCase{static_cast <ppc::core::PerfResults::TypeOfRunning>(999 ),
106+ " none" }));
107+
108+ struct TaskTypeTestCase {
109+ ppc::core::TypeOfTask type;
110+ std::string expected;
111+ std::string label;
112+ };
113+
114+ class GetStringTaskTypeTest : public ::testing::TestWithParam<TaskTypeTestCase> {
115+ protected:
116+ std::string temp_path;
117+
118+ void SetUp () override {
119+ temp_path = std::filesystem::temp_directory_path () / " test_settings.json" ;
120+ nlohmann::json j;
121+ j[" tasks" ][" all" ] = " ALL" ;
122+ j[" tasks" ][" stl" ] = " STL" ;
123+ j[" tasks" ][" omp" ] = " OMP" ;
124+ j[" tasks" ][" mpi" ] = " MPI" ;
125+ j[" tasks" ][" tbb" ] = " TBB" ;
126+ j[" tasks" ][" seq" ] = " SEQ" ;
127+
128+ std::ofstream (temp_path) << j.dump ();
129+ }
130+
131+ void TearDown () override { std::filesystem::remove (temp_path); }
132+ };
133+
134+ TEST_P (GetStringTaskTypeTest, ReturnsExpectedString) {
135+ const auto & param = GetParam ();
136+ EXPECT_EQ (GetStringTaskType (param.type , temp_path), param.expected ) << " Failed on: " << param.label ;
137+ }
138+
139+ INSTANTIATE_TEST_SUITE_P_NOLINT (AllTypeCases, GetStringTaskTypeTest,
140+ ::testing::Values (TaskTypeTestCase{ppc::core::TypeOfTask::kALL , " all_ALL" , " kALL" },
141+ TaskTypeTestCase{ppc::core::TypeOfTask::kSTL , " stl_STL" , " kSTL" },
142+ TaskTypeTestCase{ppc::core::TypeOfTask::kOMP , " omp_OMP" , " kOMP" },
143+ TaskTypeTestCase{ppc::core::TypeOfTask::kMPI , " mpi_MPI" , " kMPI" },
144+ TaskTypeTestCase{ppc::core::TypeOfTask::kTBB , " tbb_TBB" , " kTBB" },
145+ TaskTypeTestCase{ppc::core::TypeOfTask::kSEQ , " seq_SEQ" , " kSEQ" }));
146+
147+ DEATH_TEST (GetStringTaskTypeStandaloneTest, ThrowsIfFileMissing) {
148+ std::string missing_path = " non_existent_settings.json" ;
149+ EXPECT_THROW_NOLINT (GetStringTaskType (ppc::core::TypeOfTask::kSEQ , missing_path), std::runtime_error);
150+ }
151+
152+ TEST (GetStringTaskTypeStandaloneTest, ReturnsUnknownForInvalidEnum) {
153+ std::string path = std::filesystem::temp_directory_path () / " tmp_settings.json" ;
154+ std::ofstream (path) << R"( {"tasks":{"seq":"SEQ"}})" ;
155+
156+ auto result = GetStringTaskType (static_cast <ppc::core::TypeOfTask>(999 ), path);
157+ EXPECT_EQ (result, " unknown" );
158+
159+ std::filesystem::remove (path);
160+ }
161+
162+ TEST (GetStringTaskTypeEdgeCases, ThrowsIfFileCannotBeOpened) {
163+ EXPECT_THROW_NOLINT (GetStringTaskType (ppc::core::TypeOfTask::kSEQ , " definitely_missing_file.json" ),
164+ std::runtime_error);
165+ }
166+
167+ TEST (GetStringTaskTypeEdgeCases, ThrowsIfJsonIsMalformed) {
168+ std::string path = std::filesystem::temp_directory_path () / " bad_json.json" ;
169+ std::ofstream (path) << " { this is not valid json " ;
170+ EXPECT_THROW_NOLINT (GetStringTaskType (ppc::core::TypeOfTask::kSEQ , path), nlohmann::json::parse_error);
171+ std::filesystem::remove (path);
172+ }
173+
174+ TEST (GetStringTaskTypeEdgeCases, ThrowsIfJsonValueIsNull) {
175+ std::string path = std::filesystem::temp_directory_path () / " null_value.json" ;
176+ std::ofstream (path) << R"( {"tasks": { "seq": null }})" ;
177+
178+ EXPECT_THROW (GetStringTaskType (ppc::core::TypeOfTask::kSEQ , path), nlohmann::json::type_error);
179+
180+ std::filesystem::remove (path);
181+ }
182+
183+ TEST (GetStringTaskTypeEdgeCases, ReturnsUnknownIfEnumOutOfRange) {
184+ std::string path = std::filesystem::temp_directory_path () / " ok.json" ;
185+ std::ofstream (path) << R"( {"tasks":{"seq":"SEQ"}})" ;
186+ auto result = GetStringTaskType (static_cast <ppc::core::TypeOfTask>(255 ), path);
187+ EXPECT_EQ (result, " unknown" );
188+ std::filesystem::remove (path);
189+ }
190+
191+ TEST (GetStringTaskStatusTest, HandlesEnabledAndDisabled) {
192+ EXPECT_EQ (GetStringTaskStatus (ppc::core::StatusOfTask::kEnabled ), " enabled" );
193+ EXPECT_EQ (GetStringTaskStatus (ppc::core::StatusOfTask::kDisabled ), " disabled" );
194+ }
195+
196+ class DummyTask : public ppc ::core::Task<int , int > {
197+ public:
198+ using Task::Task;
199+ bool ValidationImpl () override { return true ; }
200+ bool PreProcessingImpl () override { return true ; }
201+ bool RunImpl () override { return true ; }
202+ bool PostProcessingImpl () override { return true ; }
203+ };
204+
205+ class SlowTask : public ppc ::core::Task<int , int > {
206+ public:
207+ using Task::Task;
208+ bool ValidationImpl () override { return true ; }
209+ bool PreProcessingImpl () override { return true ; }
210+ bool RunImpl () override { return true ; }
211+ bool PostProcessingImpl () override {
212+ std::this_thread::sleep_for (std::chrono::seconds (2 ));
213+ return true ;
214+ }
215+ };
216+
217+ TEST (TaskTest, GetDynamicTypeReturnsCorrectEnum) {
218+ DummyTask task;
219+ task.SetTypeOfTask (ppc::core::TypeOfTask::kOMP );
220+ task.Validation ();
221+ task.PreProcessing ();
222+ task.Run ();
223+ task.PostProcessing ();
224+ EXPECT_EQ (task.GetDynamicTypeOfTask (), ppc::core::TypeOfTask::kOMP );
225+ }
226+
227+ DEATH_TEST (TaskTest, DestructorTerminatesIfWrongOrder) {
228+ testing::FLAGS_gtest_death_test_style = " threadsafe" ;
229+ ASSERT_DEATH_IF_SUPPORTED (
230+ {
231+ DummyTask task;
232+ task.Run ();
233+ },
234+ " " );
235+ }
236+
237+ namespace my {
238+ namespace nested {
239+ struct Type {};
240+ } // namespace nested
241+
242+ class Another {};
243+ } // namespace my
244+
245+ namespace {
246+ struct NoNamespace {};
247+ } // anonymous namespace
248+
249+ template <typename T>
250+ class GetNamespaceTest : public ::testing::Test {};
251+
252+ using TestTypes = ::testing::Types<my::nested::Type, my::Another, NoNamespace, int , std::vector<int >>;
253+
254+ TYPED_TEST_SUITE (GetNamespaceTest, TestTypes);
255+
256+ TYPED_TEST (GetNamespaceTest, ExtractsNamespaceCorrectly) {
257+ constexpr std::string_view ns = ppc::util::GetNamespace<TypeParam>();
258+
259+ if constexpr (std::is_same_v<TypeParam, my::nested::Type>) {
260+ EXPECT_EQ (ns, " my::nested" );
261+ } else if constexpr (std::is_same_v<TypeParam, my::Another>) {
262+ EXPECT_EQ (ns, " my" );
263+ } else if constexpr (std::is_same_v<TypeParam, NoNamespace>) {
264+ EXPECT_EQ (ns, " " );
265+ } else if constexpr (std::is_same_v<TypeParam, int >) {
266+ EXPECT_EQ (ns, " " );
267+ } else if constexpr (std::is_same_v<TypeParam, std::vector<int >>) {
268+ EXPECT_EQ (ns, " std" );
269+ } else {
270+ FAIL () << " Unhandled type in test" ;
271+ }
272+ }
0 commit comments