9
9
#include " core/performance/tests/test_task.hpp"
10
10
11
11
TEST (perf_tests, check_perf_pipeline) {
12
- // Create data
12
+
13
13
std::vector<uint32_t > in (2000 , 1 );
14
14
15
- // Create Task
15
+
16
16
auto test_task = std::make_shared<ppc::test::perf::TestTask<std::vector<uint32_t >, uint32_t >>(in);
17
17
18
- // Create Perf analyzer
18
+
19
19
ppc::core::Perf<std::vector<uint32_t >, uint32_t > perf_analyzer (test_task);
20
20
21
- // Create Perf attributes
21
+
22
22
ppc::core::PerfAttr perf_attr;
23
23
perf_analyzer.PipelineRun (perf_attr);
24
24
25
- // Get perf statistic
25
+
26
26
perf_analyzer.PrintPerfStatistic (" check_perf_pipeline" );
27
27
ASSERT_LE (perf_analyzer.GetPerfResults ().time_sec , ppc::core::PerfResults::kMaxTime );
28
28
EXPECT_EQ (test_task->GetOutput (), in.size ());
29
29
}
30
30
31
31
TEST (perf_tests, check_perf_pipeline_float) {
32
- // Create data
32
+
33
33
std::vector<float > in (2000 , 1 );
34
34
35
- // Create Task
35
+
36
36
auto test_task = std::make_shared<ppc::test::perf::TestTask<std::vector<float >, float >>(in);
37
37
38
- // Create Perf analyzer
38
+
39
39
ppc::core::Perf<std::vector<float >, float > perf_analyzer (test_task);
40
40
41
- // Create Perf attributes
41
+
42
42
ppc::core::PerfAttr perf_attr;
43
43
perf_analyzer.PipelineRun (perf_attr);
44
44
45
- // Get perf statistic
45
+
46
46
perf_analyzer.PrintPerfStatistic (" check_perf_pipeline_float" );
47
47
ASSERT_LE (perf_analyzer.GetPerfResults ().time_sec , ppc::core::PerfResults::kMaxTime );
48
48
EXPECT_EQ (test_task->GetOutput (), in.size ());
49
49
}
50
50
51
51
TEST (perf_tests, check_perf_pipeline_uint8_t_slow_test) {
52
- // Create data
52
+
53
53
std::vector<uint8_t > in (128 , 1 );
54
54
55
- // Create Task
55
+
56
56
auto test_task = std::make_shared<ppc::test::perf::FakePerfTask<std::vector<uint8_t >, uint8_t >>(in);
57
57
58
- // Create Perf analyzer
58
+
59
59
ppc::core::Perf<std::vector<uint8_t >, uint8_t > perf_analyzer (test_task);
60
60
61
- // Create Perf attributes
61
+
62
62
ppc::core::PerfAttr perf_attr;
63
63
perf_attr.num_running = 1 ;
64
64
@@ -70,46 +70,244 @@ TEST(perf_tests, check_perf_pipeline_uint8_t_slow_test) {
70
70
};
71
71
perf_analyzer.PipelineRun (perf_attr);
72
72
73
- // Get perf statistic
74
- // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto)
75
- ASSERT_ANY_THROW (perf_analyzer.PrintPerfStatistic (" check_perf_pipeline_uint8_t_slow_test" ));
73
+
74
+
75
+ ASSERT_ANY_THROW_NOLINT (perf_analyzer.PrintPerfStatistic (" check_perf_pipeline_uint8_t_slow_test" ));
76
76
}
77
77
78
78
TEST (perf_tests, check_perf_task_exception) {
79
- // Create data
79
+
80
80
std::vector<uint32_t > in (2000 , 1 );
81
81
82
- // Create Task
82
+
83
83
auto test_task = std::make_shared<ppc::test::perf::TestTask<std::vector<uint32_t >, uint32_t >>(in);
84
84
85
- // Create Perf analyzer
85
+
86
86
ppc::core::Perf<std::vector<uint32_t >, uint32_t > perf_analyzer (test_task);
87
87
88
- // Get perf statistic
89
- // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto)
90
- ASSERT_ANY_THROW (perf_analyzer.PrintPerfStatistic (" check_perf_task_exception" ));
88
+
89
+
90
+ ASSERT_ANY_THROW_NOLINT (perf_analyzer.PrintPerfStatistic (" check_perf_task_exception" ));
91
91
92
- // Create Perf attributes
92
+
93
93
ppc::core::PerfAttr perf_attr;
94
94
perf_analyzer.TaskRun (perf_attr);
95
95
}
96
96
97
97
TEST (perf_tests, check_perf_task_float) {
98
- // Create data
98
+
99
99
std::vector<float > in (2000 , 1 );
100
100
101
- // Create Task
101
+
102
102
auto test_task = std::make_shared<ppc::test::perf::TestTask<std::vector<float >, float >>(in);
103
103
104
- // Create Perf analyzer
104
+
105
105
ppc::core::Perf<std::vector<float >, float > perf_analyzer (test_task);
106
106
107
- // Create Perf attributes
107
+
108
108
ppc::core::PerfAttr perf_attr;
109
109
perf_analyzer.TaskRun (perf_attr);
110
110
111
- // Get perf statistic
111
+
112
112
perf_analyzer.PrintPerfStatistic (" check_perf_task_float" );
113
113
ASSERT_LE (perf_analyzer.GetPerfResults ().time_sec , ppc::core::PerfResults::kMaxTime );
114
114
EXPECT_EQ (test_task->GetOutput (), in.size ());
115
115
}
116
+
117
+ struct ParamTestCase {
118
+ ppc::core::PerfResults::TypeOfRunning input;
119
+ std::string expected_output;
120
+ };
121
+
122
+ class GetStringParamNameParamTest : public ::testing::TestWithParam<ParamTestCase> {};
123
+
124
+ TEST_P (GetStringParamNameParamTest, ReturnsExpectedString) {
125
+ const auto & param = GetParam ();
126
+ EXPECT_EQ (ppc::core::GetStringParamName (param.input ), param.expected_output );
127
+ }
128
+
129
+ INSTANTIATE_TEST_SUITE_P (
130
+ ParamTests,
131
+ GetStringParamNameParamTest,
132
+ ::testing::Values (
133
+ ParamTestCase{ppc::core::PerfResults::kTaskRun , " task_run" },
134
+ ParamTestCase{ppc::core::PerfResults::kPipeline , " pipeline" },
135
+ ParamTestCase{static_cast <ppc::core::PerfResults::TypeOfRunning>(999 ), " none" }));
136
+
137
+ struct TaskTypeTestCase {
138
+ ppc::core::TypeOfTask type;
139
+ std::string expected;
140
+ std::string label;
141
+ };
142
+
143
+ class GetStringTaskTypeTest : public ::testing::TestWithParam<TaskTypeTestCase> {
144
+ protected:
145
+ std::string temp_path;
146
+
147
+ void SetUp () override {
148
+ temp_path = std::filesystem::temp_directory_path () / " test_settings.json" ;
149
+ nlohmann::json j;
150
+ j[" tasks" ][" all" ] = " ALL" ;
151
+ j[" tasks" ][" stl" ] = " STL" ;
152
+ j[" tasks" ][" omp" ] = " OMP" ;
153
+ j[" tasks" ][" mpi" ] = " MPI" ;
154
+ j[" tasks" ][" tbb" ] = " TBB" ;
155
+ j[" tasks" ][" seq" ] = " SEQ" ;
156
+
157
+ std::ofstream (temp_path) << j.dump ();
158
+ }
159
+
160
+ void TearDown () override {
161
+ std::filesystem::remove (temp_path);
162
+ }
163
+ };
164
+
165
+ TEST_P (GetStringTaskTypeTest, ReturnsExpectedString) {
166
+ const auto & param = GetParam ();
167
+ EXPECT_EQ (GetStringTaskType (param.type , temp_path), param.expected ) << " Failed on: " << param.label ;
168
+ }
169
+
170
+ INSTANTIATE_TEST_SUITE_P_NOLINT (
171
+ AllTypeCases,
172
+ GetStringTaskTypeTest,
173
+ ::testing::Values (
174
+ TaskTypeTestCase{ppc::core::TypeOfTask::kALL , " all_ALL" , " kALL" },
175
+ TaskTypeTestCase{ppc::core::TypeOfTask::kSTL , " stl_STL" , " kSTL" },
176
+ TaskTypeTestCase{ppc::core::TypeOfTask::kOMP , " omp_OMP" , " kOMP" },
177
+ TaskTypeTestCase{ppc::core::TypeOfTask::kMPI , " mpi_MPI" , " kMPI" },
178
+ TaskTypeTestCase{ppc::core::TypeOfTask::kTBB , " tbb_TBB" , " kTBB" },
179
+ TaskTypeTestCase{ppc::core::TypeOfTask::kSEQ , " seq_SEQ" , " kSEQ" }));
180
+
181
+ DEATH_TEST (GetStringTaskTypeStandaloneTest, ThrowsIfFileMissing) {
182
+ std::string missing_path = " non_existent_settings.json" ;
183
+ EXPECT_THROW_NOLINT (GetStringTaskType (ppc::core::TypeOfTask::kSEQ , missing_path), std::runtime_error);
184
+ }
185
+
186
+ TEST (GetStringTaskTypeStandaloneTest, ReturnsUnknownForInvalidEnum) {
187
+
188
+ std::string path = std::filesystem::temp_directory_path () / " tmp_settings.json" ;
189
+ std::ofstream (path) << R"( {"tasks":{"seq":"SEQ"}})" ;
190
+
191
+ auto result = GetStringTaskType (static_cast <ppc::core::TypeOfTask>(999 ), path);
192
+ EXPECT_EQ (result, " unknown" );
193
+
194
+ std::filesystem::remove (path);
195
+ }
196
+
197
+ TEST (GetStringTaskTypeEdgeCases, ThrowsIfFileCannotBeOpened) {
198
+ EXPECT_THROW_NOLINT (GetStringTaskType (ppc::core::TypeOfTask::kSEQ , " definitely_missing_file.json" ), std::runtime_error);
199
+ }
200
+
201
+ TEST (GetStringTaskTypeEdgeCases, ThrowsIfJsonIsMalformed) {
202
+ std::string path = std::filesystem::temp_directory_path () / " bad_json.json" ;
203
+ std::ofstream (path) << " { this is not valid json " ;
204
+ EXPECT_THROW_NOLINT (GetStringTaskType (ppc::core::TypeOfTask::kSEQ , path), nlohmann::json::parse_error);
205
+ std::filesystem::remove (path);
206
+ }
207
+
208
+ TEST (GetStringTaskTypeEdgeCases, ThrowsIfJsonValueIsNull) {
209
+ std::string path = std::filesystem::temp_directory_path () / " null_value.json" ;
210
+ std::ofstream (path) << R"( {"tasks": { "seq": null }})" ;
211
+
212
+ EXPECT_THROW (
213
+ GetStringTaskType (ppc::core::TypeOfTask::kSEQ , path),
214
+ nlohmann::json::type_error);
215
+
216
+ std::filesystem::remove (path);
217
+ }
218
+
219
+ TEST (GetStringTaskTypeEdgeCases, ReturnsUnknownIfEnumOutOfRange) {
220
+ std::string path = std::filesystem::temp_directory_path () / " ok.json" ;
221
+ std::ofstream (path) << R"( {"tasks":{"seq":"SEQ"}})" ;
222
+ auto result = GetStringTaskType (static_cast <ppc::core::TypeOfTask>(255 ), path);
223
+ EXPECT_EQ (result, " unknown" );
224
+ std::filesystem::remove (path);
225
+ }
226
+
227
+ TEST (GetStringTaskStatusTest, HandlesEnabledAndDisabled) {
228
+ EXPECT_EQ (GetStringTaskStatus (ppc::core::StatusOfTask::kEnabled ), " enabled" );
229
+ EXPECT_EQ (GetStringTaskStatus (ppc::core::StatusOfTask::kDisabled ), " disabled" );
230
+ }
231
+
232
+ class DummyTask : public ppc ::core::Task<int , int > {
233
+ public:
234
+ using Task::Task;
235
+ bool ValidationImpl () override { return true ; }
236
+ bool PreProcessingImpl () override { return true ; }
237
+ bool RunImpl () override { return true ; }
238
+ bool PostProcessingImpl () override { return true ; }
239
+ };
240
+
241
+ class SlowTask : public ppc ::core::Task<int , int > {
242
+ public:
243
+ using Task::Task;
244
+ bool ValidationImpl () override { return true ; }
245
+ bool PreProcessingImpl () override { return true ; }
246
+ bool RunImpl () override { return true ; }
247
+ bool PostProcessingImpl () override {
248
+ std::this_thread::sleep_for (std::chrono::seconds (2 ));
249
+ return true ;
250
+ }
251
+ };
252
+
253
+ TEST (TaskTest, GetDynamicTypeReturnsCorrectEnum) {
254
+ DummyTask task;
255
+ task.SetTypeOfTask (ppc::core::TypeOfTask::kOMP );
256
+ task.Validation ();
257
+ task.PreProcessing ();
258
+ task.Run ();
259
+ task.PostProcessing ();
260
+ EXPECT_EQ (task.GetDynamicTypeOfTask (), ppc::core::TypeOfTask::kOMP );
261
+ }
262
+
263
+ DEATH_TEST (TaskTest, DestructorTerminatesIfWrongOrder) {
264
+ testing::FLAGS_gtest_death_test_style = " threadsafe" ;
265
+ ASSERT_DEATH_IF_SUPPORTED ({
266
+ DummyTask task;
267
+ task.Run ();
268
+ }, " " );
269
+ }
270
+
271
+
272
+ namespace my {
273
+ namespace nested {
274
+ struct Type {};
275
+ } // namespace nested
276
+
277
+ class Another {};
278
+ } // namespace my
279
+
280
+ namespace {
281
+ struct NoNamespace {};
282
+ } // anonymous namespace
283
+
284
+ template <typename T>
285
+ class GetNamespaceTest : public ::testing::Test {};
286
+
287
+ using TestTypes = ::testing::Types<
288
+ my::nested::Type,
289
+ my::Another,
290
+ NoNamespace,
291
+ int ,
292
+ std::vector<int >
293
+ >;
294
+
295
+ TYPED_TEST_SUITE (GetNamespaceTest, TestTypes);
296
+
297
+ TYPED_TEST (GetNamespaceTest, ExtractsNamespaceCorrectly) {
298
+ constexpr std::string_view ns = ppc::util::GetNamespace<TypeParam>();
299
+
300
+ if constexpr (std::is_same_v<TypeParam, my::nested::Type>) {
301
+ EXPECT_EQ (ns, " my::nested" );
302
+ } else if constexpr (std::is_same_v<TypeParam, my::Another>) {
303
+ EXPECT_EQ (ns, " my" );
304
+ } else if constexpr (std::is_same_v<TypeParam, NoNamespace>) {
305
+ EXPECT_EQ (ns, " " );
306
+ } else if constexpr (std::is_same_v<TypeParam, int >) {
307
+ EXPECT_EQ (ns, " " );
308
+ } else if constexpr (std::is_same_v<TypeParam, std::vector<int >>) {
309
+ EXPECT_EQ (ns, " std" );
310
+ } else {
311
+ FAIL () << " Unhandled type in test" ;
312
+ }
313
+ }
0 commit comments