Skip to content

Commit 677e6ea

Browse files
committed
fix coverage 3
1 parent bf36cb3 commit 677e6ea

File tree

4 files changed

+86
-51
lines changed

4 files changed

+86
-51
lines changed

modules/core/task/func_tests/task_tests.cpp

Lines changed: 27 additions & 10 deletions
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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ 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+
39+
virtual void InternalTimeTest(const std::string &str) final;
3840

3941
// implementation of "Validation" function
4042
virtual bool ValidationImpl() = 0;
@@ -54,7 +56,9 @@ class Task {
5456
std::vector<std::string> right_functions_order_ = {"Validation", "PreProcessing", "Run", "PostProcessing"};
5557
static constexpr double kMaxTestTime = 1.0;
5658
std::chrono::high_resolution_clock::time_point tmp_time_point_;
57-
bool functions_order_validation_ = true;
59+
bool was_worked_ = false;
60+
61+
bool IsFullPipelineStage();
5862
};
5963

6064
} // namespace ppc::core

modules/core/task/src/task.cpp

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,101 @@
11
#include "core/task/include/task.hpp"
22

3+
#include <algorithm>
34
#include <chrono>
45
#include <cstddef>
56
#include <exception>
7+
#include <functional>
68
#include <iomanip>
79
#include <iostream>
810
#include <sstream>
911
#include <stdexcept>
1012
#include <string>
1113
#include <utility>
1214

15+
using namespace std::chrono;
16+
1317
ppc::core::Task::Task(StateOfTesting state_of_testing) : state_of_testing_(state_of_testing) {
1418
functions_order_.clear();
1519
}
1620

1721
bool ppc::core::Task::Validation() {
18-
InternalOrderTest();
22+
InternalOrderTest(__builtin_FUNCTION());
1923
return ValidationImpl();
2024
}
2125

2226
bool ppc::core::Task::PreProcessing() {
23-
InternalOrderTest();
27+
InternalOrderTest(__builtin_FUNCTION());
28+
if (state_of_testing_ == StateOfTesting::kFunc) {
29+
InternalTimeTest(__builtin_FUNCTION());
30+
}
2431
return PreProcessingImpl();
2532
}
2633

2734
bool ppc::core::Task::Run() {
28-
InternalOrderTest();
35+
InternalOrderTest(__builtin_FUNCTION());
2936
return RunImpl();
3037
}
3138

3239
bool ppc::core::Task::PostProcessing() {
33-
InternalOrderTest();
40+
InternalOrderTest(__builtin_FUNCTION());
41+
if (state_of_testing_ == StateOfTesting::kFunc) {
42+
InternalTimeTest(__builtin_FUNCTION());
43+
}
3444
return PostProcessingImpl();
3545
}
3646

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-
47+
void ppc::core::Task::InternalOrderTest(const std::string &str) {
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();
51+
} else {
52+
was_worked_ = true;
5153
}
54+
}
5255

53-
if (str == "PreProcessing" && state_of_testing_ == StateOfTesting::kFunc) {
56+
void ppc::core::Task::InternalTimeTest(const std::string &str) {
57+
if (str == "PreProcessing") {
5458
tmp_time_point_ = std::chrono::high_resolution_clock::now();
5559
}
5660

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;
61+
if (str == "PostProcessing") {
62+
auto duration = duration_cast<nanoseconds>(high_resolution_clock::now() - tmp_time_point_).count();
63+
auto diff = static_cast<double>(duration) * 1e-9;
64+
6165
std::stringstream err_msg;
62-
if (current_time < kMaxTestTime) {
63-
err_msg << "Test time:" << std::fixed << std::setprecision(10) << current_time;
66+
if (diff < kMaxTestTime) {
67+
err_msg << "Test time:" << std::fixed << std::setprecision(10) << diff;
6468
} else {
6569
err_msg << "\nTask execute time need to be: ";
6670
err_msg << "time < " << kMaxTestTime << " secs.\n";
67-
err_msg << "Original time in secs: " << current_time << '\n';
71+
err_msg << "Original time in secs: " << diff << '\n';
6872
throw std::runtime_error(err_msg.str().c_str());
6973
}
7074
}
7175
}
7276

77+
bool ppc::core::Task::IsFullPipelineStage() {
78+
auto it = std::adjacent_find(functions_order_.begin() + 2,
79+
functions_order_.begin() + static_cast<long>(functions_order_.size() - 2),
80+
std::not_equal_to<>());
81+
82+
return (functions_order_.size() >= 4 && functions_order_[0] == "Validation" &&
83+
functions_order_[1] == "PreProcessing" && functions_order_[2] == "Run" &&
84+
it == (functions_order_.begin() + static_cast<long>(functions_order_.size() - 2)) &&
85+
functions_order_[functions_order_.size() - 1] == "PostProcessing");
86+
}
87+
7388
ppc::core::Task::~Task() {
74-
if (functions_order_.empty()) {
75-
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT: No task functions were executed\n";
89+
auto custom_terminate = []() {
90+
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT! \n"
91+
"Expected - \"Validation\", \"PreProcessing\", \"Run\", \"PostProcessing\" \n";
92+
std::exit(404);
93+
};
94+
std::set_terminate(custom_terminate);
95+
96+
if (!functions_order_.empty() || !was_worked_) {
7697
std::terminate();
98+
} else {
99+
functions_order_.clear();
77100
}
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();
90101
}

modules/core/util/include/util.hpp

Lines changed: 3 additions & 0 deletions
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)