forked from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.cpp
More file actions
96 lines (82 loc) · 3.27 KB
/
task.cpp
File metadata and controls
96 lines (82 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "core/task/include/task.hpp"
#include <chrono>
#include <cstddef>
#include <exception>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
void ppc::core::Task::SetData(TaskDataPtr task_data_ptr) {
task_data_ptr->state_of_testing = TaskData::StateOfTesting::kFunc;
functions_order_.clear();
this->task_data = std::move(task_data_ptr);
}
ppc::core::TaskDataPtr ppc::core::Task::GetData() const { return task_data; }
ppc::core::Task::Task(TaskDataPtr task_data) { SetData(std::move(task_data)); }
bool ppc::core::Task::Validation() {
InternalOrderTest();
return ValidationImpl();
}
bool ppc::core::Task::PreProcessing() {
InternalOrderTest();
return PreProcessingImpl();
}
bool ppc::core::Task::Run() {
InternalOrderTest();
return RunImpl();
}
bool ppc::core::Task::PostProcessing() {
InternalOrderTest();
return PostProcessingImpl();
}
void ppc::core::Task::InternalOrderTest(const std::string& str) {
if (!functions_order_.empty() && str == functions_order_.back() && str == "Run") {
return;
}
functions_order_.push_back(str);
for (size_t i = 0; i < functions_order_.size(); i++) {
if (functions_order_[i] != right_functions_order_[i % right_functions_order_.size()]) {
functions_order_validation_ = false;
throw std::invalid_argument("ORDER OF FUNCTIONS IS NOT RIGHT: \n" + std::string("Serial number: ") +
std::to_string(i + 1) + "\n" + std::string("Your function: ") + functions_order_[i] +
"\n" + std::string("Expected function: ") + right_functions_order_[i]);
}
}
if (str == "PreProcessing" && task_data->state_of_testing == TaskData::StateOfTesting::kFunc) {
tmp_time_point_ = std::chrono::high_resolution_clock::now();
}
if (str == "PostProcessing" && task_data->state_of_testing == TaskData::StateOfTesting::kFunc) {
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - tmp_time_point_).count();
auto current_time = static_cast<double>(duration) * 1e-9;
std::stringstream err_msg;
if (current_time < kMaxTestTime) {
err_msg << "Test time:" << std::fixed << std::setprecision(10) << current_time;
} else {
err_msg << "\nTask execute time need to be: ";
err_msg << "time < " << kMaxTestTime << " secs.\n";
err_msg << "Original time in secs: " << current_time << '\n';
throw std::runtime_error(err_msg.str().c_str());
}
}
}
ppc::core::Task::~Task() {
if (functions_order_.empty()) {
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT: No task functions were executed\n";
std::terminate();
}
if (functions_order_validation_) {
for (size_t i = 0; i < functions_order_.size(); i++) {
if (functions_order_[i] != right_functions_order_[i % right_functions_order_.size()]) {
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT: \n"
<< std::string("Serial number: ") << std::to_string(i + 1) << "\n"
<< std::string("Your function: ") << functions_order_[i] << "\n"
<< std::string("Expected function: ") << right_functions_order_[i] << "\n";
std::terminate();
}
}
}
functions_order_.clear();
}