|
1 | 1 | #include "core/task/include/task.hpp" |
2 | 2 |
|
| 3 | +#include <algorithm> |
3 | 4 | #include <chrono> |
4 | 5 | #include <cstddef> |
5 | 6 | #include <exception> |
| 7 | +#include <functional> |
6 | 8 | #include <iomanip> |
7 | 9 | #include <iostream> |
8 | 10 | #include <sstream> |
9 | 11 | #include <stdexcept> |
10 | 12 | #include <string> |
11 | 13 | #include <utility> |
12 | 14 |
|
| 15 | +using namespace std::chrono; |
| 16 | + |
13 | 17 | ppc::core::Task::Task(StateOfTesting state_of_testing) : state_of_testing_(state_of_testing) { |
14 | 18 | functions_order_.clear(); |
15 | 19 | } |
16 | 20 |
|
17 | 21 | bool ppc::core::Task::Validation() { |
18 | | - InternalOrderTest(); |
| 22 | + InternalOrderTest(__builtin_FUNCTION()); |
19 | 23 | return ValidationImpl(); |
20 | 24 | } |
21 | 25 |
|
22 | 26 | bool ppc::core::Task::PreProcessing() { |
23 | | - InternalOrderTest(); |
| 27 | + InternalOrderTest(__builtin_FUNCTION()); |
| 28 | + if (state_of_testing_ == StateOfTesting::kFunc) { |
| 29 | + InternalTimeTest(__builtin_FUNCTION()); |
| 30 | + } |
24 | 31 | return PreProcessingImpl(); |
25 | 32 | } |
26 | 33 |
|
27 | 34 | bool ppc::core::Task::Run() { |
28 | | - InternalOrderTest(); |
| 35 | + InternalOrderTest(__builtin_FUNCTION()); |
29 | 36 | return RunImpl(); |
30 | 37 | } |
31 | 38 |
|
32 | 39 | bool ppc::core::Task::PostProcessing() { |
33 | | - InternalOrderTest(); |
| 40 | + InternalOrderTest(__builtin_FUNCTION()); |
| 41 | + if (state_of_testing_ == StateOfTesting::kFunc) { |
| 42 | + InternalTimeTest(__builtin_FUNCTION()); |
| 43 | + } |
34 | 44 | return PostProcessingImpl(); |
35 | 45 | } |
36 | 46 |
|
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) { |
| 48 | + was_worked_ = true; |
42 | 49 | 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 | | - } |
| 50 | + if (str == "PostProcessing" && IsFullPipelineStage()) { |
| 51 | + functions_order_.clear(); |
51 | 52 | } |
52 | | - |
53 | | - if (str == "PreProcessing" && state_of_testing_ == StateOfTesting::kFunc) { |
| 53 | +} |
| 54 | +void ppc::core::Task::InternalTimeTest(const std::string &str) { |
| 55 | + if (str == "PreProcessing") { |
54 | 56 | tmp_time_point_ = std::chrono::high_resolution_clock::now(); |
55 | 57 | } |
56 | 58 |
|
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; |
| 59 | + if (str == "PostProcessing") { |
| 60 | + auto duration = duration_cast<nanoseconds>(high_resolution_clock::now() - tmp_time_point_).count(); |
| 61 | + auto diff = static_cast<double>(duration) * 1e-9; |
| 62 | + |
61 | 63 | std::stringstream err_msg; |
62 | | - if (current_time < kMaxTestTime) { |
63 | | - err_msg << "Test time:" << std::fixed << std::setprecision(10) << current_time; |
| 64 | + if (diff < kMaxTestTime) { |
| 65 | + err_msg << "Test time:" << std::fixed << std::setprecision(10) << diff; |
64 | 66 | } else { |
65 | 67 | err_msg << "\nTask execute time need to be: "; |
66 | 68 | err_msg << "time < " << kMaxTestTime << " secs.\n"; |
67 | | - err_msg << "Original time in secs: " << current_time << '\n'; |
| 69 | + err_msg << "Original time in secs: " << diff << '\n'; |
68 | 70 | throw std::runtime_error(err_msg.str().c_str()); |
69 | 71 | } |
70 | 72 | } |
71 | 73 | } |
72 | 74 |
|
| 75 | +bool ppc::core::Task::IsFullPipelineStage() { |
| 76 | + auto it = std::adjacent_find(functions_order_.begin() + 2, |
| 77 | + functions_order_.begin() + static_cast<long>(functions_order_.size() - 2), |
| 78 | + std::not_equal_to<>()); |
| 79 | + |
| 80 | + return (functions_order_.size() >= 4 && functions_order_[0] == "Validation" && |
| 81 | + functions_order_[1] == "PreProcessing" && functions_order_[2] == "Run" && |
| 82 | + it == (functions_order_.begin() + static_cast<long>(functions_order_.size() - 2)) && |
| 83 | + functions_order_[functions_order_.size() - 1] == "PostProcessing"); |
| 84 | +} |
| 85 | + |
73 | 86 | ppc::core::Task::~Task() { |
74 | | - if (functions_order_.empty()) { |
75 | | - std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT: No task functions were executed\n"; |
| 87 | + if (!functions_order_.empty() || !was_worked_) { |
| 88 | + std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT! \n" |
| 89 | + "Expected - \"Validation\", \"PreProcessing\", \"Run\", \"PostProcessing\" \n"; |
76 | 90 | std::terminate(); |
| 91 | + } else { |
| 92 | + functions_order_.clear(); |
77 | 93 | } |
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(); |
90 | 94 | } |
0 commit comments