3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
11
12#include <unity.h>
13
14#include "pin_edges.h" // Real header - pure C++
15
16void setUp(void) {}
17void tearDown(void) {}
18
19inline constexpr bool LOW_{ true };
20inline constexpr bool HIGH_{ false };
21
22// ============================================================================
23// PinEdges and pinEdges()
24// ============================================================================
25
27{
28 constexpr PinEdges noneHigh{ pinEdges(HIGH_, HIGH_) };
29 constexpr PinEdges press{ pinEdges(HIGH_, LOW_) };
30 constexpr PinEdges held{ pinEdges(LOW_, LOW_) };
31 constexpr PinEdges release{ pinEdges(LOW_, HIGH_) };
32
33 TEST_ASSERT_FALSE(noneHigh.active);
34 TEST_ASSERT_FALSE(noneHigh.changed);
35 TEST_ASSERT_FALSE(noneHigh.pressed());
36 TEST_ASSERT_FALSE(noneHigh.released());
37
38 TEST_ASSERT_TRUE(press.active);
39 TEST_ASSERT_TRUE(press.changed);
40 TEST_ASSERT_TRUE(press.pressed());
41 TEST_ASSERT_FALSE(press.released());
42
43 TEST_ASSERT_TRUE(held.active);
44 TEST_ASSERT_FALSE(held.changed);
45 TEST_ASSERT_FALSE(held.pressed());
46 TEST_ASSERT_FALSE(held.released());
47
48 TEST_ASSERT_FALSE(release.active);
49 TEST_ASSERT_TRUE(release.changed);
50 TEST_ASSERT_FALSE(release.pressed());
51 TEST_ASSERT_TRUE(release.released());
52}
53
55{
56 // changed: on both edges; active: the level, whether it just changed or not
57 TEST_ASSERT_EQUAL(pinEdges(HIGH_, LOW_).changed, pinEdges(LOW_, HIGH_).changed);
58 TEST_ASSERT_EQUAL(pinEdges(HIGH_, LOW_).active, pinEdges(LOW_, LOW_).active);
59 TEST_ASSERT_EQUAL(pinEdges(LOW_, HIGH_).active, pinEdges(HIGH_, HIGH_).active);
60}
61
62// ============================================================================
63// trackPinEdges<pin>()
64// ============================================================================
65
67{
68 // HIGH at the first reading: no edge, the previous level is "released" at startup
69 const PinEdges first{ trackPinEdges< 2 >(HIGH_) };
70
71 TEST_ASSERT_FALSE(first.active);
72 TEST_ASSERT_FALSE(first.changed);
73 TEST_ASSERT_FALSE(first.released());
74}
75
77{
78 const PinEdges first{ trackPinEdges< 3 >(LOW_) };
79
80 TEST_ASSERT_TRUE(first.active);
81 TEST_ASSERT_TRUE(first.pressed());
82}
83
85{
87
88 const PinEdges press{ trackPinEdges< 4 >(LOW_) };
89 TEST_ASSERT_TRUE(press.pressed());
90
91 for (uint8_t second = 0; second != 5; ++second)
92 {
93 const PinEdges held{ trackPinEdges< 4 >(LOW_) };
94
95 TEST_ASSERT_TRUE(held.active);
96 TEST_ASSERT_FALSE(held.changed);
97 TEST_ASSERT_FALSE(held.pressed());
98 TEST_ASSERT_FALSE(held.released());
99 }
100}
101
103{
106
107 const PinEdges release{ trackPinEdges< 5 >(HIGH_) };
108 TEST_ASSERT_FALSE(release.active);
109 TEST_ASSERT_TRUE(release.changed);
110 TEST_ASSERT_TRUE(release.released());
111 TEST_ASSERT_FALSE(release.pressed());
112
113 const PinEdges after{ trackPinEdges< 5 >(HIGH_) };
114 TEST_ASSERT_FALSE(after.changed);
115 TEST_ASSERT_FALSE(after.released());
116}
117
119{
120 // pin 6 pressed, pin 7 untouched: pin 7 still starts released
122 TEST_ASSERT_FALSE(trackPinEdges< 6 >(LOW_).changed);
123
124 const PinEdges other{ trackPinEdges< 7 >(HIGH_) };
125 TEST_ASSERT_FALSE(other.changed);
126
127 // pin 7 pressed now: pin 6, still held, sees no edge
128 TEST_ASSERT_TRUE(trackPinEdges< 7 >(LOW_).pressed());
129 TEST_ASSERT_FALSE(trackPinEdges< 6 >(LOW_).changed);
130
131 // pin 6 released: pin 7, still held, sees no edge
132 TEST_ASSERT_TRUE(trackPinEdges< 6 >(HIGH_).released());
133 TEST_ASSERT_FALSE(trackPinEdges< 7 >(LOW_).changed);
134}
135
137{
138 constexpr bool levels[]{ HIGH_, LOW_, LOW_, HIGH_, HIGH_, LOW_, HIGH_ };
139 constexpr bool pressed[]{ false, true, false, false, false, true, false };
140 constexpr bool released[]{ false, false, false, true, false, false, true };
141
142 for (uint8_t i = 0; i != sizeof(levels); ++i)
143 {
144 const PinEdges edges{ trackPinEdges< 8 >(levels[i]) };
145
146 TEST_ASSERT_EQUAL_MESSAGE(levels[i], edges.active, "active");
147 TEST_ASSERT_EQUAL_MESSAGE(pressed[i], edges.pressed(), "pressed");
148 TEST_ASSERT_EQUAL_MESSAGE(released[i], edges.released(), "released");
149 }
150}
151
152// ============================================================================
153// isRotationRequested()
154// ============================================================================
155
157{
158 TEST_ASSERT_TRUE(isRotationRequested(pinEdges(HIGH_, LOW_), false));
159 TEST_ASSERT_FALSE(isRotationRequested(pinEdges(LOW_, LOW_), false));
160 TEST_ASSERT_FALSE(isRotationRequested(pinEdges(LOW_, HIGH_), false));
161 TEST_ASSERT_FALSE(isRotationRequested(pinEdges(HIGH_, HIGH_), false));
162}
163
165{
166 TEST_ASSERT_FALSE(isRotationRequested(pinEdges(HIGH_, LOW_), true));
167}
168
170{
171 // the rotation pin is read every second, forced or not
172 TEST_ASSERT_FALSE(isRotationRequested(trackPinEdges< 12 >(HIGH_), false));
173
174 // pressed during forcing: no rotation
175 TEST_ASSERT_FALSE(isRotationRequested(trackPinEdges< 12 >(LOW_), true));
176 TEST_ASSERT_FALSE(isRotationRequested(trackPinEdges< 12 >(LOW_), true));
177
178 // forcing ends, the pin still held: no edge any more, no rotation
179 TEST_ASSERT_FALSE(isRotationRequested(trackPinEdges< 12 >(LOW_), false));
180 TEST_ASSERT_FALSE(isRotationRequested(trackPinEdges< 12 >(HIGH_), false));
181
182 // a new press after forcing rotates
183 TEST_ASSERT_TRUE(isRotationRequested(trackPinEdges< 12 >(LOW_), false));
184}
185
186int main(int argc, char** argv)
187{
188 (void)argc;
189 (void)argv;
190
191 UNITY_BEGIN();
192
195
196 RUN_TEST(test_startup_is_released);
201 RUN_TEST(test_press_release_press);
202
206
207 return UNITY_END();
208}
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
uint8_t i
int main(void)
void test_a_press_while_forced_is_dropped_not_carried_over(void)
constexpr bool HIGH_
Definition test_main.cpp:20
void test_press_once_then_held_is_no_edge(void)
Definition test_main.cpp:84
void test_changed_is_either_edge_active_is_the_level(void)
Definition test_main.cpp:54
void test_each_pin_keeps_its_own_level(void)
void test_startup_is_released(void)
Definition test_main.cpp:66
void test_press_release_press(void)
void test_no_rotation_while_forced(void)
void test_release_once_then_no_edge(void)
void test_low_at_startup_is_a_press(void)
Definition test_main.cpp:76
constexpr bool LOW_
Definition test_main.cpp:19
void test_rotation_on_a_press_when_nothing_is_forced(void)
void test_pinEdges_from_the_two_levels(void)
Definition test_main.cpp:26
The edges of the active-LOW input pins: pressed, released, or no change.
constexpr bool isRotationRequested(const PinEdges &pin, const bool bOverridden)
Tells whether the rotation pin asks for a rotation now.
Definition pin_edges.h:87
constexpr PinEdges pinEdges(const bool wasActive, const bool active)
The edges of a pin, from its previous level and its current one.
Definition pin_edges.h:54
PinEdges trackPinEdges(const bool active)
Gives the edges of a pin from its current level, and keeps that level for the next call.
Definition pin_edges.h:70
State of an active-LOW input pin, and how it changed since the previous reading.
Definition pin_edges.h:30