forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionProcess.cpp
More file actions
185 lines (151 loc) · 5.71 KB
/
Copy pathTransactionProcess.cpp
File metadata and controls
185 lines (151 loc) · 5.71 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <ArduinoOcpp/Tasks/Transactions/TransactionProcess.h>
#include "./catch2/catch.hpp"
#include "./helpers/testHelper.h"
#include <array>
using namespace ArduinoOcpp;
TEST_CASE( "Transaction process - trivial" ) {
TransactionProcess txProcess {1};
SECTION("Trivial transaction process"){
txProcess.evaluateProcessSteps();
REQUIRE( !txProcess.existsActiveTrigger() );
REQUIRE( txProcess.getState() == TxEnableState::Inactive );
}
SECTION("Trivial transaction process with outputs"){
bool notified = false;
txProcess.addEnableStep([¬ified] (TxTrigger trigger) -> TxEnableState {
REQUIRE( trigger == TxTrigger::Inactive );
notified = true;
return TxEnableState::Inactive;
});
txProcess.evaluateProcessSteps();
REQUIRE( notified );
REQUIRE( txProcess.getState() == TxEnableState::Inactive );
}
}
TEST_CASE("Transaction process - inputs") {
TransactionProcess txProcess {1};
TxPrecondition precondition = TxPrecondition::Active;
txProcess.addPrecondition([&precondition] () {
return precondition;
});
TxTrigger trigger = TxTrigger::Active;
txProcess.addTrigger([&trigger] () {
return trigger;
});
TxEnableState enable = TxEnableState::Active;
TxTrigger checkTrigger = TxTrigger::Active;
txProcess.addEnableStep([&enable, &checkTrigger] (TxTrigger input) {
REQUIRE(checkTrigger == input);
return enable;
});
SECTION("All active") {
txProcess.evaluateProcessSteps();
REQUIRE(txProcess.getState() == TxEnableState::Active);
}
SECTION("Precondition") {
precondition = TxPrecondition::Inactive;
checkTrigger = TxTrigger::Inactive;
txProcess.evaluateProcessSteps();
REQUIRE( txProcess.getState() != TxEnableState::Active );
}
SECTION("Trigger") {
trigger = TxTrigger::Inactive;
checkTrigger = TxTrigger::Inactive;
txProcess.evaluateProcessSteps();
REQUIRE( txProcess.getState() != TxEnableState::Active );
}
SECTION("Output enable") {
enable = TxEnableState::Inactive;
txProcess.evaluateProcessSteps();
REQUIRE( txProcess.getState() == TxEnableState::Pending );
enable = TxEnableState::Pending;
txProcess.evaluateProcessSteps();
REQUIRE( txProcess.getState() == TxEnableState::Pending );
}
}
TEST_CASE("Transaction process - execution order") {
TransactionProcess txProcess {1};
std::array<TxPrecondition, 2> preconditions {TxPrecondition::Active, TxPrecondition::Active};
for (unsigned int i = 0; i < preconditions.size(); i++) {
txProcess.addPrecondition([&preconditions, i] () {
return preconditions[i];
});
}
std::array<TxTrigger, 2> triggers {TxTrigger::Active, TxTrigger::Active};
for (unsigned int i = 0; i < triggers.size(); i++) {
txProcess.addTrigger([&triggers, i] () {
return triggers[i];
});
}
std::array<TxEnableState, 2> enableSteps {TxEnableState::Active, TxEnableState::Active};
std::array<int, 2> checkSeq {-1, -1};
int checkCount = 0;
for (unsigned int i = 0; i < enableSteps.size(); i++) {
txProcess.addEnableStep([&enableSteps, &checkSeq, &checkCount, i] (TxTrigger) {
checkSeq[i] = checkCount;
checkCount++;
return enableSteps[i];
});
}
SECTION("Precondition"){
preconditions[0] = TxPrecondition::Inactive;
txProcess.evaluateProcessSteps();
REQUIRE( txProcess.getState() != TxEnableState::Active );
preconditions[0] = TxPrecondition::Active;
preconditions[1] = TxPrecondition::Inactive;
txProcess.evaluateProcessSteps();
REQUIRE( txProcess.getState() != TxEnableState::Active );
}
SECTION("Trigger") {
triggers[0] = TxTrigger::Inactive;
txProcess.evaluateProcessSteps();
REQUIRE( txProcess.getState() != TxEnableState::Active );
REQUIRE( txProcess.existsActiveTrigger());
triggers[0] = TxTrigger::Active;
triggers[1] = TxTrigger::Inactive;
txProcess.evaluateProcessSteps();
REQUIRE( txProcess.getState() != TxEnableState::Active );
REQUIRE( txProcess.existsActiveTrigger());
}
SECTION("Output enable - all active"){
txProcess.evaluateProcessSteps();
REQUIRE(checkSeq[0] == 1);
REQUIRE(checkSeq[1] == 0);
}
SECTION("Output enable - active pending"){
enableSteps[1] = TxEnableState::Pending;
txProcess.evaluateProcessSteps();
REQUIRE(checkSeq[0] == -1);
REQUIRE(checkSeq[1] == 0);
}
SECTION("Output enable - inactive"){
preconditions[0] = TxPrecondition::Inactive;
enableSteps[0] = TxEnableState::Inactive;
txProcess.evaluateProcessSteps();
REQUIRE(checkSeq[0] == 0);
REQUIRE(checkSeq[1] == 1);
}
SECTION("Output enable - inactive pending"){
preconditions[0] = TxPrecondition::Inactive;
enableSteps[0] = TxEnableState::Pending;
txProcess.evaluateProcessSteps();
REQUIRE(checkSeq[0] == 0);
REQUIRE(checkSeq[1] == -1);
}
}
TEST_CASE("Transaction process - begin new transaction") {
TransactionProcess txProcess {1};
txProcess.addPrecondition([] () {
return TxPrecondition::Active;
});
TxTrigger trigger = TxTrigger::Active;
txProcess.addTrigger([&trigger] () {
return trigger;
});
TxEnableState enable = TxEnableState::Active;
TxTrigger checkTrigger = TxTrigger::Active;
txProcess.addEnableStep([&enable, &checkTrigger] (TxTrigger input) {
checkTrigger = input;
return enable;
});
}