3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2
3#include <unity.h>
4
5#include "utils_pins.h"
6#include "utils_relay.h"
7
8constexpr RelayEngine relays{ { { 2, 1000, 200, 1, 1 },
9 { 3, 100, 20, 2, 3 } } };
10
11void setUp(void)
12{
13 // Setup for each test
14}
15
16void tearDown(void)
17{
18 // clean stuff up here
19}
20
22{
23 relayOutput relay(4, 500, 100);
24 TEST_ASSERT_EQUAL(4, relay.get_pin());
25 TEST_ASSERT_EQUAL(500, relay.get_surplusThreshold());
26 TEST_ASSERT_EQUAL(100, relay.get_importThreshold());
27}
28
30{
31 relayOutput relay(4, 500, 100);
32 TEST_ASSERT_EQUAL(4, relay.get_pin());
33 TEST_ASSERT_EQUAL(500, relay.get_surplusThreshold());
34 TEST_ASSERT_EQUAL(100, relay.get_importThreshold());
35}
36
38{
39 relayOutput relay(4, -500, -100);
40 TEST_ASSERT_EQUAL(4, relay.get_pin());
41 TEST_ASSERT_EQUAL(500, relay.get_surplusThreshold());
42 TEST_ASSERT_EQUAL(-100, relay.get_importThreshold());
43}
44
45void test_get_pin(void)
46{
47 TEST_ASSERT_EQUAL(2, relays.get_relay(0).get_pin());
48 TEST_ASSERT_EQUAL(3, relays.get_relay(1).get_pin());
49}
50
52{
53 TEST_ASSERT_EQUAL(1000, relays.get_relay(0).get_surplusThreshold());
54 TEST_ASSERT_EQUAL(100, relays.get_relay(1).get_surplusThreshold());
55}
56
58{
59 TEST_ASSERT_EQUAL(200, relays.get_relay(0).get_importThreshold());
60 TEST_ASSERT_EQUAL(20, relays.get_relay(1).get_importThreshold());
61}
62
64{
65 TEST_ASSERT_EQUAL(1 * 60, relays.get_relay(0).get_minON());
66 TEST_ASSERT_EQUAL(2 * 60, relays.get_relay(1).get_minON());
67}
68
70{
71 TEST_ASSERT_EQUAL(1 * 60, relays.get_relay(0).get_minOFF());
72 TEST_ASSERT_EQUAL(3 * 60, relays.get_relay(1).get_minOFF());
73}
74
76{
77 TEST_ASSERT_FALSE(relays.get_relay(1).isRelayON());
78}
79
81{
82 const auto& my_relay{ relays.get_relay(1) };
83
84 TEST_ASSERT_FALSE(my_relay.isRelayON());
85
86 /* The relay is OFF, test the "TurnON" case */
87 const auto surplus{ -my_relay.get_surplusThreshold() - 1 };
88 uint16_t overrideBitmask = 0; // No override active
89
90 TEST_ASSERT_FALSE(my_relay.proceed_relay(surplus, overrideBitmask));
91 delay(100);
92 TEST_ASSERT_FALSE(my_relay.isRelayON());
93
94 for (uint8_t timer = 0; timer < my_relay.get_minOFF() - 1; ++timer)
95 {
96 my_relay.inc_duration();
97 }
98 overrideBitmask = 0; // Reset bitmask
99 TEST_ASSERT_FALSE(my_relay.proceed_relay(surplus, overrideBitmask));
100 delay(100);
101
102 my_relay.inc_duration();
103
104 overrideBitmask = 0; // Reset bitmask
105 TEST_ASSERT_TRUE(my_relay.proceed_relay(surplus, overrideBitmask));
106 TEST_ASSERT_TRUE(my_relay.isRelayON());
107}
108
110{
111 const auto& my_relay{ relays.get_relay(1) };
112
113 TEST_ASSERT_TRUE(my_relay.isRelayON());
114
115 /* The relay is ON, test the "TurnOFF" case */
116 const auto consum{ my_relay.get_importThreshold() + 1 };
117 uint16_t overrideBitmask = 0; // No override active
118
119 TEST_ASSERT_FALSE(my_relay.proceed_relay(consum, overrideBitmask));
120 delay(100);
121 TEST_ASSERT_TRUE(my_relay.isRelayON());
122
123 for (uint8_t timer = 0; timer < my_relay.get_minON() - 1; ++timer)
124 {
125 my_relay.inc_duration();
126 }
127 overrideBitmask = 0; // Reset bitmask
128 TEST_ASSERT_FALSE(my_relay.proceed_relay(consum, overrideBitmask));
129 delay(100);
130
131 my_relay.inc_duration();
132
133 overrideBitmask = 0; // Reset bitmask
134 TEST_ASSERT_TRUE(my_relay.proceed_relay(consum, overrideBitmask));
135 TEST_ASSERT_FALSE(my_relay.isRelayON());
136}
137
139{
140 const auto& my_relay{ relays.get_relay(1) };
141
142 // Ensure relay is OFF initially
143 TEST_ASSERT_FALSE(my_relay.isRelayON());
144
145 // Test override with insufficient surplus (would normally not turn ON)
146 const auto insufficient_surplus{ -my_relay.get_surplusThreshold() + 100 };
147
148 // Test at half the minimum OFF time - should still be blocked
149 for (uint8_t timer = 0; timer < my_relay.get_minOFF() / 2; ++timer)
150 {
151 my_relay.inc_duration();
152 }
153
154 uint16_t overrideBitmask = (1U << my_relay.get_pin());
155 TEST_ASSERT_FALSE(my_relay.proceed_relay(insufficient_surplus, overrideBitmask));
156 TEST_ASSERT_FALSE(my_relay.isRelayON());
157 TEST_ASSERT_EQUAL(0, overrideBitmask & (1U << my_relay.get_pin())); // Bit cleared
158
159 // Increment to just before minimum OFF time is reached
160 for (uint8_t timer = my_relay.get_minOFF() / 2; timer < my_relay.get_minOFF() - 1; ++timer)
161 {
162 my_relay.inc_duration();
163 }
164
165 overrideBitmask = (1U << my_relay.get_pin());
166 TEST_ASSERT_FALSE(my_relay.proceed_relay(insufficient_surplus, overrideBitmask));
167 TEST_ASSERT_FALSE(my_relay.isRelayON());
168 TEST_ASSERT_EQUAL(0, overrideBitmask & (1U << my_relay.get_pin())); // Bit cleared
169
170 // Increment one more time - now minimum OFF time is reached
171 my_relay.inc_duration();
172
173 overrideBitmask = (1U << my_relay.get_pin());
174 TEST_ASSERT_TRUE(my_relay.proceed_relay(insufficient_surplus, overrideBitmask));
175 TEST_ASSERT_TRUE(my_relay.isRelayON());
176 TEST_ASSERT_EQUAL(0, overrideBitmask & (1U << my_relay.get_pin())); // Bit cleared
177}
178
180{
181 const auto& my_relay{ relays.get_relay(1) };
182
183 // Relay should be ON from previous test
184 TEST_ASSERT_TRUE(my_relay.isRelayON());
185
186 // Test that relay respects minimum ON time even after override is released
187 const auto high_import{ my_relay.get_importThreshold() + 100 }; // High import should turn OFF relay
188 uint16_t overrideBitmask = 0; // No override active anymore
189
190 // Test at half the minimum ON time - should NOT turn OFF yet
191 for (uint8_t timer = 0; timer < my_relay.get_minON() / 2; ++timer)
192 {
193 my_relay.inc_duration();
194 }
195
196 TEST_ASSERT_FALSE(my_relay.proceed_relay(high_import, overrideBitmask));
197 TEST_ASSERT_TRUE(my_relay.isRelayON()); // Should stay ON
198
199 // Test just before minimum ON time is reached
200 for (uint8_t timer = my_relay.get_minON() / 2; timer < my_relay.get_minON() - 1; ++timer)
201 {
202 my_relay.inc_duration();
203 }
204
205 TEST_ASSERT_FALSE(my_relay.proceed_relay(high_import, overrideBitmask));
206 TEST_ASSERT_TRUE(my_relay.isRelayON()); // Should still stay ON
207
208 // Increment one more time - now minimum ON time is reached
209 my_relay.inc_duration();
210
211 TEST_ASSERT_TRUE(my_relay.proceed_relay(high_import, overrideBitmask));
212 TEST_ASSERT_FALSE(my_relay.isRelayON()); // Now it can turn OFF
213}
214
216{
217 RUN_TEST(test_relay_turnON);
218 delay(100);
219 RUN_TEST(test_relay_turnOFF);
220 delay(100);
222 delay(100);
224}
225
227{
228 TEST_ASSERT_EQUAL(2, relays.size());
229}
230
231void setup()
232{
233 delay(1000);
234
235 UNITY_BEGIN(); // IMPORTANT LINE!
236}
237
238void loop()
239{
243
244 RUN_TEST(test_get_size);
245
246 RUN_TEST(test_get_pin);
247
249 RUN_TEST(test_get_importThreshold);
250
251 RUN_TEST(test_get_minON);
252 RUN_TEST(test_get_minOFF);
253
254 RUN_TEST(test_isRelayON);
255
256 RUN_TEST(test_proceed_relay);
257
258 UNITY_END(); // stop unit testing
259}
Manages a collection of relays and their behavior based on surplus and import thresholds.
Represents a single relay configuration and its behavior.
Definition utils_relay.h:41
constexpr auto get_surplusThreshold() const
Get the surplus threshold which will turns ON the relay.
constexpr auto get_importThreshold() const
Get the import threshold which will turns OFF the relay.
constexpr auto get_pin() const
Get the control pin of the relay.
Definition utils_relay.h:92
constexpr RelayEngine relays
Definition config.h:126
void setup()
void setUp(void)
Definition test_main.cpp:6
void tearDown(void)
Definition test_main.cpp:11
void loop()
void test_get_pin(void)
Definition test_main.cpp:45
void test_get_minOFF(void)
Definition test_main.cpp:69
void test_relay_override_turnON(void)
void test_proceed_relay(void)
void test_isRelayON(void)
Definition test_main.cpp:75
void test_relay_turnOFF(void)
void test_get_size(void)
void test_get_minON(void)
Definition test_main.cpp:63
void test_get_surplusThreshold(void)
Definition test_main.cpp:51
void test_relay_override_minimum_ON_time(void)
void test_relay_initialization_with_positive_thresholds(void)
Definition test_main.cpp:29
void test_get_importThreshold(void)
Definition test_main.cpp:57
void test_relay_initialization(void)
Definition test_main.cpp:21
void test_relay_turnON(void)
Definition test_main.cpp:80
void test_relay_initialization_with_negative_thresholds(void)
Definition test_main.cpp:37
Some utility functions for pins manipulation.
Some utility functions for the relay output feature.