9
9
#include " core/performance/tests/test_task.hpp"
10
10
11
11
TEST (perf_tests, check_perf_pipeline) {
12
- // Create data
13
12
std::vector<uint32_t > in (2000 , 1 );
14
13
15
- // Create Task
16
14
auto test_task = std::make_shared<ppc::test::perf::TestTask<std::vector<uint32_t >, uint32_t >>(in);
17
15
18
- // Create Perf analyzer
19
16
ppc::core::Perf<std::vector<uint32_t >, uint32_t > perf_analyzer (test_task);
20
17
21
- // Create Perf attributes
22
18
ppc::core::PerfAttr perf_attr;
23
19
perf_analyzer.PipelineRun (perf_attr);
24
20
25
- // Get perf statistic
26
21
perf_analyzer.PrintPerfStatistic (" check_perf_pipeline" );
27
22
ASSERT_LE (perf_analyzer.GetPerfResults ().time_sec , ppc::core::PerfResults::kMaxTime );
28
23
EXPECT_EQ (test_task->GetOutput (), in.size ());
29
24
}
30
25
31
26
TEST (perf_tests, check_perf_pipeline_float) {
32
- // Create data
33
27
std::vector<float > in (2000 , 1 );
34
28
35
- // Create Task
36
29
auto test_task = std::make_shared<ppc::test::perf::TestTask<std::vector<float >, float >>(in);
37
30
38
- // Create Perf analyzer
39
31
ppc::core::Perf<std::vector<float >, float > perf_analyzer (test_task);
40
32
41
- // Create Perf attributes
42
33
ppc::core::PerfAttr perf_attr;
43
34
perf_analyzer.PipelineRun (perf_attr);
44
35
45
- // Get perf statistic
46
36
perf_analyzer.PrintPerfStatistic (" check_perf_pipeline_float" );
47
37
ASSERT_LE (perf_analyzer.GetPerfResults ().time_sec , ppc::core::PerfResults::kMaxTime );
48
38
EXPECT_EQ (test_task->GetOutput (), in.size ());
49
39
}
50
40
51
41
TEST (perf_tests, check_perf_pipeline_uint8_t_slow_test) {
52
- // Create data
53
42
std::vector<uint8_t > in (128 , 1 );
54
43
55
- // Create Task
56
44
auto test_task = std::make_shared<ppc::test::perf::FakePerfTask<std::vector<uint8_t >, uint8_t >>(in);
57
45
58
- // Create Perf analyzer
59
46
ppc::core::Perf<std::vector<uint8_t >, uint8_t > perf_analyzer (test_task);
60
47
61
- // Create Perf attributes
62
48
ppc::core::PerfAttr perf_attr;
63
49
perf_attr.num_running = 1 ;
64
50
@@ -70,46 +56,217 @@ TEST(perf_tests, check_perf_pipeline_uint8_t_slow_test) {
70
56
};
71
57
perf_analyzer.PipelineRun (perf_attr);
72
58
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" ));
76
60
}
77
61
78
62
TEST (perf_tests, check_perf_task_exception) {
79
- // Create data
80
63
std::vector<uint32_t > in (2000 , 1 );
81
64
82
- // Create Task
83
65
auto test_task = std::make_shared<ppc::test::perf::TestTask<std::vector<uint32_t >, uint32_t >>(in);
84
66
85
- // Create Perf analyzer
86
67
ppc::core::Perf<std::vector<uint32_t >, uint32_t > perf_analyzer (test_task);
87
68
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" ));
91
70
92
- // Create Perf attributes
93
71
ppc::core::PerfAttr perf_attr;
94
72
perf_analyzer.TaskRun (perf_attr);
95
73
}
96
74
97
75
TEST (perf_tests, check_perf_task_float) {
98
- // Create data
99
76
std::vector<float > in (2000 , 1 );
100
77
101
- // Create Task
102
78
auto test_task = std::make_shared<ppc::test::perf::TestTask<std::vector<float >, float >>(in);
103
79
104
- // Create Perf analyzer
105
80
ppc::core::Perf<std::vector<float >, float > perf_analyzer (test_task);
106
81
107
- // Create Perf attributes
108
82
ppc::core::PerfAttr perf_attr;
109
83
perf_analyzer.TaskRun (perf_attr);
110
84
111
- // Get perf statistic
112
85
perf_analyzer.PrintPerfStatistic (" check_perf_task_float" );
113
86
ASSERT_LE (perf_analyzer.GetPerfResults ().time_sec , ppc::core::PerfResults::kMaxTime );
114
87
EXPECT_EQ (test_task->GetOutput (), in.size ());
115
88
}
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