Skip to content

Commit 18538bc

Browse files
committed
fix coverage yet 2
1 parent bf36cb3 commit 18538bc

File tree

4 files changed

+77
-52
lines changed

4 files changed

+77
-52
lines changed

modules/core/task/func_tests/task_tests.cpp

+27-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <vector>
77

88
#include "core/task/func_tests/test_task.hpp"
9+
#include "core/util/include/util.hpp"
910

1011
TEST(task_tests, check_int32_t) {
1112
// Create data
@@ -50,6 +51,10 @@ TEST(task_tests, check_validate_func) {
5051

5152
// Check Result
5253
ASSERT_EQ(is_valid, false);
54+
55+
test_task.PreProcessing();
56+
test_task.Run();
57+
test_task.PostProcessing();
5358
}
5459

5560
TEST(task_tests, check_double) {
@@ -124,18 +129,30 @@ TEST(task_tests, check_float) {
124129
EXPECT_NEAR(test_task.Get(), in.size(), 1e-3);
125130
}
126131

127-
TEST(task_tests, check_wrong_order) {
128-
// Create data
129-
std::vector<float> in(20, 1);
132+
DEATH_TEST(task_tests, check_wrong_order) {
133+
auto destroy_function = [] {
134+
// Create data
135+
std::vector<float> in(20, 1);
136+
137+
// Create Task
138+
ppc::test::task::TestTask<float> test_task(in);
139+
bool is_valid = test_task.Validation();
140+
ASSERT_EQ(is_valid, true);
141+
test_task.PreProcessing();
142+
test_task.PostProcessing();
143+
};
144+
EXPECT_DEATH_IF_SUPPORTED(destroy_function(), ".*ORDER OF FUNCTIONS IS NOT RIGHT.*");
145+
}
130146

131-
// Create Task
132-
ppc::test::task::TestTask<float> test_task(in);
133-
bool is_valid = test_task.Validation();
134-
ASSERT_EQ(is_valid, true);
147+
DEATH_TEST(task_tests, check_empty_order) {
148+
auto destroy_function = [] {
149+
// Create data
150+
std::vector<float> in(20, 1);
135151

136-
// Run Task
137-
test_task.PreProcessing();
138-
ASSERT_ANY_THROW(test_task.PostProcessing());
152+
// Create Task
153+
ppc::test::task::TestTask<float> test_task(in);
154+
};
155+
EXPECT_DEATH_IF_SUPPORTED(destroy_function(), ".*ORDER OF FUNCTIONS IS NOT RIGHT.*");
139156
}
140157

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

modules/core/task/include/task.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class Task {
3434
virtual ~Task();
3535

3636
protected:
37-
virtual void InternalOrderTest(const std::string &str = __builtin_FUNCTION()) final;
37+
virtual void InternalOrderTest(const std::string &str) final;
38+
virtual void InternalTimeTest(const std::string &str) final;
3839

3940
// implementation of "Validation" function
4041
virtual bool ValidationImpl() = 0;
@@ -54,7 +55,9 @@ class Task {
5455
std::vector<std::string> right_functions_order_ = {"Validation", "PreProcessing", "Run", "PostProcessing"};
5556
static constexpr double kMaxTestTime = 1.0;
5657
std::chrono::high_resolution_clock::time_point tmp_time_point_;
57-
bool functions_order_validation_ = true;
58+
bool was_worked = false;
59+
60+
bool IsFullPipelineStage();
5861
};
5962

6063
} // namespace ppc::core

modules/core/task/src/task.cpp

+42-40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "core/task/include/task.hpp"
22

3+
#include <algorithm>
34
#include <chrono>
45
#include <cstddef>
56
#include <exception>
@@ -10,81 +11,82 @@
1011
#include <string>
1112
#include <utility>
1213

14+
using namespace std::chrono;
15+
1316
ppc::core::Task::Task(StateOfTesting state_of_testing) : state_of_testing_(state_of_testing) {
1417
functions_order_.clear();
1518
}
1619

1720
bool ppc::core::Task::Validation() {
18-
InternalOrderTest();
21+
InternalOrderTest(__builtin_FUNCTION());
1922
return ValidationImpl();
2023
}
2124

2225
bool ppc::core::Task::PreProcessing() {
23-
InternalOrderTest();
26+
InternalOrderTest(__builtin_FUNCTION());
27+
if (state_of_testing_ == StateOfTesting::kFunc) {
28+
InternalTimeTest(__builtin_FUNCTION());
29+
}
2430
return PreProcessingImpl();
2531
}
2632

2733
bool ppc::core::Task::Run() {
28-
InternalOrderTest();
34+
InternalOrderTest(__builtin_FUNCTION());
2935
return RunImpl();
3036
}
3137

3238
bool ppc::core::Task::PostProcessing() {
33-
InternalOrderTest();
39+
InternalOrderTest(__builtin_FUNCTION());
40+
if (state_of_testing_ == StateOfTesting::kFunc) {
41+
InternalTimeTest(__builtin_FUNCTION());
42+
}
3443
return PostProcessingImpl();
3544
}
3645

37-
void ppc::core::Task::InternalOrderTest(const std::string& str) {
38-
if (!functions_order_.empty() && str == functions_order_.back() && str == "Run") {
39-
return;
40-
}
41-
46+
void ppc::core::Task::InternalOrderTest(const std::string &str) {
47+
was_worked = true;
4248
functions_order_.push_back(str);
43-
44-
for (size_t i = 0; i < functions_order_.size(); i++) {
45-
if (functions_order_[i] != right_functions_order_[i % right_functions_order_.size()]) {
46-
functions_order_validation_ = false;
47-
throw std::invalid_argument("ORDER OF FUNCTIONS IS NOT RIGHT: \n" + std::string("Serial number: ") +
48-
std::to_string(i + 1) + "\n" + std::string("Your function: ") + functions_order_[i] +
49-
"\n" + std::string("Expected function: ") + right_functions_order_[i]);
50-
}
49+
if (str == "PostProcessing" && IsFullPipelineStage()) {
50+
functions_order_.clear();
5151
}
52-
53-
if (str == "PreProcessing" && state_of_testing_ == StateOfTesting::kFunc) {
52+
}
53+
void ppc::core::Task::InternalTimeTest(const std::string &str) {
54+
if (str == "PreProcessing") {
5455
tmp_time_point_ = std::chrono::high_resolution_clock::now();
5556
}
5657

57-
if (str == "PostProcessing" && state_of_testing_ == StateOfTesting::kFunc) {
58-
auto end = std::chrono::high_resolution_clock::now();
59-
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - tmp_time_point_).count();
60-
auto current_time = static_cast<double>(duration) * 1e-9;
58+
if (str == "PostProcessing") {
59+
auto duration = duration_cast<nanoseconds>(high_resolution_clock::now() - tmp_time_point_).count();
60+
auto diff = static_cast<double>(duration) * 1e-9;
61+
6162
std::stringstream err_msg;
62-
if (current_time < kMaxTestTime) {
63-
err_msg << "Test time:" << std::fixed << std::setprecision(10) << current_time;
63+
if (diff < kMaxTestTime) {
64+
err_msg << "Test time:" << std::fixed << std::setprecision(10) << diff;
6465
} else {
6566
err_msg << "\nTask execute time need to be: ";
6667
err_msg << "time < " << kMaxTestTime << " secs.\n";
67-
err_msg << "Original time in secs: " << current_time << '\n';
68+
err_msg << "Original time in secs: " << diff << '\n';
6869
throw std::runtime_error(err_msg.str().c_str());
6970
}
7071
}
7172
}
7273

74+
bool ppc::core::Task::IsFullPipelineStage() {
75+
auto it = std::adjacent_find(functions_order_.begin() + 2, functions_order_.begin() + (functions_order_.size() - 2),
76+
std::not_equal_to<>());
77+
78+
return (functions_order_.size() >= 4 && functions_order_[0] == "Validation" &&
79+
functions_order_[1] == "PreProcessing" && functions_order_[2] == "Run" &&
80+
it == (functions_order_.begin() + (functions_order_.size() - 2)) &&
81+
functions_order_[functions_order_.size() - 1] == "PostProcessing");
82+
}
83+
7384
ppc::core::Task::~Task() {
74-
if (functions_order_.empty()) {
75-
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT: No task functions were executed\n";
85+
if (!functions_order_.empty() || !was_worked) {
86+
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT! \n"
87+
"Expected - \"Validation\", \"PreProcessing\", \"Run\", \"PostProcessing\" \n";
7688
std::terminate();
89+
} else {
90+
functions_order_.clear();
7791
}
78-
if (functions_order_validation_) {
79-
for (size_t i = 0; i < functions_order_.size(); i++) {
80-
if (functions_order_[i] != right_functions_order_[i % right_functions_order_.size()]) {
81-
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT: \n"
82-
<< std::string("Serial number: ") << std::to_string(i + 1) << "\n"
83-
<< std::string("Your function: ") << functions_order_[i] << "\n"
84-
<< std::string("Expected function: ") << right_functions_order_[i] << "\n";
85-
std::terminate();
86-
}
87-
}
88-
}
89-
functions_order_.clear();
9092
}

modules/core/util/include/util.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#define INSTANTIATE_TEST_SUITE_P_NOLINT(prefix, test_case_name, generator) \
66
INSTANTIATE_TEST_SUITE_P(prefix, test_case_name, generator) // NOLINT
77

8+
// NOLINTNEXTLINE
9+
#define DEATH_TEST(test_suite_name, test_name) TEST(test_suite_name, test_name) // NOLINT
10+
811
namespace ppc::util {
912

1013
std::string GetAbsolutePath(const std::string &relative_path);

0 commit comments

Comments
 (0)