Skip to content

Commit 2c0875f

Browse files
committed
fix coverage yet 2
1 parent bf36cb3 commit 2c0875f

File tree

4 files changed

+76
-52
lines changed

4 files changed

+76
-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

+41-40
Original file line numberDiff line numberDiff line change
@@ -10,81 +10,82 @@
1010
#include <string>
1111
#include <utility>
1212

13+
using namespace std::chrono;
14+
1315
ppc::core::Task::Task(StateOfTesting state_of_testing) : state_of_testing_(state_of_testing) {
1416
functions_order_.clear();
1517
}
1618

1719
bool ppc::core::Task::Validation() {
18-
InternalOrderTest();
20+
InternalOrderTest(__builtin_FUNCTION());
1921
return ValidationImpl();
2022
}
2123

2224
bool ppc::core::Task::PreProcessing() {
23-
InternalOrderTest();
25+
InternalOrderTest(__builtin_FUNCTION());
26+
if (state_of_testing_ == StateOfTesting::kFunc) {
27+
InternalTimeTest(__builtin_FUNCTION());
28+
}
2429
return PreProcessingImpl();
2530
}
2631

2732
bool ppc::core::Task::Run() {
28-
InternalOrderTest();
33+
InternalOrderTest(__builtin_FUNCTION());
2934
return RunImpl();
3035
}
3136

3237
bool ppc::core::Task::PostProcessing() {
33-
InternalOrderTest();
38+
InternalOrderTest(__builtin_FUNCTION());
39+
if (state_of_testing_ == StateOfTesting::kFunc) {
40+
InternalTimeTest(__builtin_FUNCTION());
41+
}
3442
return PostProcessingImpl();
3543
}
3644

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-
45+
void ppc::core::Task::InternalOrderTest(const std::string &str) {
46+
was_worked = true;
4247
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-
}
48+
if (str == "PostProcessing" && IsFullPipelineStage()) {
49+
functions_order_.clear();
5150
}
52-
53-
if (str == "PreProcessing" && state_of_testing_ == StateOfTesting::kFunc) {
51+
}
52+
void ppc::core::Task::InternalTimeTest(const std::string &str) {
53+
if (str == "PreProcessing") {
5454
tmp_time_point_ = std::chrono::high_resolution_clock::now();
5555
}
5656

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;
57+
if (str == "PostProcessing") {
58+
auto duration = duration_cast<nanoseconds>(high_resolution_clock::now() - tmp_time_point_).count();
59+
auto diff = static_cast<double>(duration) * 1e-9;
60+
6161
std::stringstream err_msg;
62-
if (current_time < kMaxTestTime) {
63-
err_msg << "Test time:" << std::fixed << std::setprecision(10) << current_time;
62+
if (diff < kMaxTestTime) {
63+
err_msg << "Test time:" << std::fixed << std::setprecision(10) << diff;
6464
} else {
6565
err_msg << "\nTask execute time need to be: ";
6666
err_msg << "time < " << kMaxTestTime << " secs.\n";
67-
err_msg << "Original time in secs: " << current_time << '\n';
67+
err_msg << "Original time in secs: " << diff << '\n';
6868
throw std::runtime_error(err_msg.str().c_str());
6969
}
7070
}
7171
}
7272

73+
bool ppc::core::Task::IsFullPipelineStage() {
74+
auto it = std::adjacent_find(functions_order_.begin() + 2, functions_order_.begin() + (functions_order_.size() - 2),
75+
std::not_equal_to<>());
76+
77+
return (functions_order_.size() >= 4 && functions_order_[0] == "Validation" &&
78+
functions_order_[1] == "PreProcessing" && functions_order_[2] == "Run" &&
79+
it == (functions_order_.begin() + (functions_order_.size() - 2)) &&
80+
functions_order_[functions_order_.size() - 1] == "PostProcessing");
81+
}
82+
7383
ppc::core::Task::~Task() {
74-
if (functions_order_.empty()) {
75-
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT: No task functions were executed\n";
84+
if (!functions_order_.empty() || !was_worked) {
85+
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT! \n"
86+
"Expected - \"Validation\", \"PreProcessing\", \"Run\", \"PostProcessing\" \n";
7687
std::terminate();
88+
} else {
89+
functions_order_.clear();
7790
}
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();
9091
}

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)