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
12
13#include <unity.h>
14
15#include "load_priorities.h" // Real header - pure C++
16
17namespace
18{
19constexpr uint8_t ON{ loadStateOnBit };
20
21// a bucket of 1000 units; normal mode: both thresholds at the middle
22constexpr LoadPriorities::Bucket bucket{ 1000, 500, 500, 500 };
23// anti-flicker mode: thresholds away from the middle
24constexpr LoadPriorities::Bucket bucketAF{ 1000, 500, 400, 600 };
25
27
28bool isOn(const uint8_t entry)
29{
30 return (entry & ON) != 0;
31}
32
34template< uint8_t N >
35void cycles(LoadPriorities::Switching< N >& s, uint8_t (&prio)[N], const int32_t energy, uint8_t n,
36 const LoadPriorities::Bucket& b = bucket)
37{
38 while (n--)
39 {
40 s.onNewCycle(prio, energy, b);
41 }
42}
43
50{
52 s.lowerThreshold = b.lowerDefault;
53 s.upperThreshold = b.upperDefault;
54 return s;
55}
56
57bool noOverride(uint8_t /* iLoad */)
58{
59 return false;
60}
61} // namespace
62
63void setUp(void) {}
64void tearDown(void) {}
65
66// ============================================================================
67// nextToAdd / nextToRemove
68// ============================================================================
69
71{
72 uint8_t allOff[3]{ 0, 1, 2 };
73 uint8_t firstOn[3]{ 0 | ON, 1, 2 };
74 uint8_t gap[3]{ 0 | ON, 1, 2 | ON };
75
76 TEST_ASSERT_EQUAL_UINT8(0, LoadPriorities::nextToAdd(allOff));
77 TEST_ASSERT_EQUAL_UINT8(1, LoadPriorities::nextToAdd(firstOn));
78 TEST_ASSERT_EQUAL_UINT8(1, LoadPriorities::nextToAdd(gap));
79}
80
82{
83 uint8_t allOn[3]{ 0 | ON, 1 | ON, 2 | ON };
84
85 TEST_ASSERT_EQUAL_UINT8(3, LoadPriorities::nextToAdd(allOn));
86}
87
89{
90 uint8_t allOn[3]{ 0 | ON, 1 | ON, 2 | ON };
91 uint8_t twoOn[3]{ 0 | ON, 1 | ON, 2 };
92 uint8_t gap[3]{ 0 | ON, 1, 2 | ON };
93
94 TEST_ASSERT_EQUAL_UINT8(2, LoadPriorities::nextToRemove(allOn));
95 TEST_ASSERT_EQUAL_UINT8(1, LoadPriorities::nextToRemove(twoOn));
96 TEST_ASSERT_EQUAL_UINT8(2, LoadPriorities::nextToRemove(gap));
97}
98
100{
101 uint8_t allOff[3]{ 0, 1, 2 };
102
103 TEST_ASSERT_EQUAL_UINT8(3, LoadPriorities::nextToRemove(allOff));
104}
105
107{
108 // positions in priority order, whatever the physical load numbers
109 uint8_t prio[3]{ 2 | ON, 0, 1 };
110
111 TEST_ASSERT_EQUAL_UINT8(1, LoadPriorities::nextToAdd(prio));
112 TEST_ASSERT_EQUAL_UINT8(0, LoadPriorities::nextToRemove(prio));
113}
114
115// ============================================================================
116// Switching: one load at a time, in priority order
117// ============================================================================
118
120{
122 uint8_t prio[3]{ 0, 1, 2 };
123
124 s.onNewCycle(prio, 800, bucket);
125
126 TEST_ASSERT_TRUE(isOn(prio[0]));
127 TEST_ASSERT_FALSE(isOn(prio[1]));
128 TEST_ASSERT_FALSE(isOn(prio[2]));
129 TEST_ASSERT_EQUAL_UINT8(0, s.activeLoad);
130 TEST_ASSERT_TRUE(s.recentTransition);
131}
132
134{
136 uint8_t prio[3]{ 2, 0, 1 }; // load 2 first
137
138 s.onNewCycle(prio, 800, bucket);
139
140 TEST_ASSERT_EQUAL_HEX8(2 | ON, prio[0]);
141 TEST_ASSERT_EQUAL_HEX8(0, prio[1]);
142 TEST_ASSERT_EQUAL_HEX8(1, prio[2]);
143}
144
146{
148 uint8_t prio[3]{ 0, 1, 2 };
149
150 // the bucket keeps rising: there is still surplus once the first load is on
151 int32_t energy{ 800 };
152 s.onNewCycle(prio, energy, bucket);
153 for (uint8_t cycle = 1; cycle != MAX_COUNT; ++cycle)
154 {
155 s.onNewCycle(prio, energy += 10, bucket);
156 TEST_ASSERT_FALSE_MESSAGE(isOn(prio[1]), "second load switched during the post-transition period");
157 }
158
159 s.onNewCycle(prio, energy += 10, bucket);
160
161 TEST_ASSERT_TRUE(isOn(prio[1]));
162 TEST_ASSERT_FALSE(isOn(prio[2]));
163 TEST_ASSERT_EQUAL_UINT8(1, s.activeLoad);
164}
165
167{
169 uint8_t prio[3]{ 0, 1, 2 };
170
171 s.onNewCycle(prio, 800, bucket);
172 s.onNewCycle(prio, 850, bucket);
173
174 TEST_ASSERT_EQUAL_INT32(850, s.upperThreshold);
175
176 // once the period is over, a bucket that stays at that level switches nothing more
177 cycles(s, prio, 850, 5);
178
179 TEST_ASSERT_FALSE(isOn(prio[1]));
180}
181
183{
185 uint8_t prio[3]{ 0, 1, 2 };
186
187 s.onNewCycle(prio, 800, bucket);
188 // a bucket above its capacity: onNewCycle() clamps the threshold it takes from it
189 // (the ISR clamps the bucket itself, afterwards)
190 s.onNewCycle(prio, 1200, bucket);
191
192 TEST_ASSERT_EQUAL_INT32(1000, s.upperThreshold);
193}
194
196{
197 auto s{ settled() };
198 uint8_t prio[3]{ 0 | ON, 1 | ON, 2 | ON };
199
200 s.onNewCycle(prio, 200, bucket);
201
202 TEST_ASSERT_TRUE(isOn(prio[0]));
203 TEST_ASSERT_TRUE(isOn(prio[1]));
204 TEST_ASSERT_FALSE(isOn(prio[2]));
205 TEST_ASSERT_EQUAL_UINT8(2, s.activeLoad);
206}
207
209{
210 auto s{ settled() };
211 uint8_t prio[3]{ 0 | ON, 1 | ON, 2 | ON };
212
213 // the bucket keeps falling: still importing once the last load is off
214 int32_t energy{ 200 };
215 s.onNewCycle(prio, energy, bucket);
216 for (uint8_t cycle = 1; cycle != MAX_COUNT; ++cycle)
217 {
218 s.onNewCycle(prio, energy -= 10, bucket);
219 TEST_ASSERT_TRUE_MESSAGE(isOn(prio[1]), "second load switched off during the post-transition period");
220 }
221
222 s.onNewCycle(prio, energy -= 10, bucket);
223
224 TEST_ASSERT_FALSE(isOn(prio[1]));
225 TEST_ASSERT_TRUE(isOn(prio[0]));
226}
227
229{
230 auto s{ settled() };
231 uint8_t prio[3]{ 0 | ON, 1 | ON, 2 | ON };
232
233 s.onNewCycle(prio, 200, bucket);
234 // a bucket below 0: onNewCycle() clamps the threshold it takes from it
235 // (the ISR clamps the bucket itself, afterwards)
236 s.onNewCycle(prio, -300, bucket);
237
238 TEST_ASSERT_EQUAL_INT32(0, s.lowerThreshold);
239}
240
242{
244 uint8_t prio[3]{ 0 | ON, 1, 2 };
245
246 s.onNewCycle(prio, 800, bucket); // load 1 on
247 TEST_ASSERT_TRUE(isOn(prio[1]));
248
249 s.onNewCycle(prio, 200, bucket); // the next cycle, the bucket falls: load 1 off again
250
251 TEST_ASSERT_FALSE(isOn(prio[1]));
252 TEST_ASSERT_TRUE(isOn(prio[0]));
253}
254
256{
258 uint8_t prio[3]{ 0, 1, 2 };
259
260 s.onNewCycle(prio, 500, bucket);
261
262 TEST_ASSERT_FALSE(isOn(prio[0]));
263 TEST_ASSERT_EQUAL_INT32(500, s.upperThreshold); // the "opposite" threshold, reset
264}
265
267{
269 uint8_t prio[3]{ 0 | ON, 1, 2 };
270
271 s.onNewCycle(prio, 800, bucket); // load 1 on
272 s.onNewCycle(prio, 900, bucket); // upper threshold follows: 900
273 TEST_ASSERT_EQUAL_INT32(900, s.upperThreshold);
274
275 s.onNewCycle(prio, 450, bucket); // lower half: upper threshold back to its default
276
277 TEST_ASSERT_EQUAL_INT32(500, s.upperThreshold);
278
279 s.onNewCycle(prio, 600, bucket); // upper half: lower threshold back to its default
280
281 TEST_ASSERT_EQUAL_INT32(500, s.lowerThreshold);
282}
283
285{
287 uint8_t prio[3]{ 0 | ON, 1, 2 };
288
289 // between the thresholds (400..600), nothing switches, on either side of the middle
290 cycles(s, prio, 450, 5, bucketAF);
291 cycles(s, prio, 550, 5, bucketAF);
292 TEST_ASSERT_EQUAL_HEX8(0 | ON, prio[0]);
293 TEST_ASSERT_EQUAL_HEX8(1, prio[1]);
294
295 s.onNewCycle(prio, 601, bucketAF);
296 TEST_ASSERT_TRUE(isOn(prio[1]));
297}
298
300{
302 uint8_t allOn[3]{ 0 | ON, 1 | ON, 2 | ON };
303 uint8_t allOff[3]{ 0, 1, 2 };
304
305 cycles(s, allOn, 900, MAX_COUNT);
306 TEST_ASSERT_FALSE(s.recentTransition);
307 TEST_ASSERT_EQUAL_UINT8(3, s.activeLoad);
308
309 cycles(s, allOff, 100, MAX_COUNT);
310 TEST_ASSERT_FALSE(s.recentTransition);
311 TEST_ASSERT_EQUAL_HEX8(0, allOff[0]);
312}
313
315{
317 uint8_t prio[3]{ 0, 1, 2 };
318
319 int32_t energy{ 600 };
320 for (uint8_t cycle = 0; cycle != 20; ++cycle)
321 {
322 s.onNewCycle(prio, energy += 10, bucket);
323 }
324 TEST_ASSERT_EQUAL_UINT8(3, LoadPriorities::nextToAdd(prio));
325
326 energy = 400;
327 for (uint8_t cycle = 0; cycle != 20; ++cycle)
328 {
329 s.onNewCycle(prio, energy -= 10, bucket);
330 }
331 TEST_ASSERT_EQUAL_UINT8(3, LoadPriorities::nextToRemove(prio));
332}
333
334// ============================================================================
335// rotate
336// ============================================================================
337
339{
340 uint8_t prio[3]{ 0, 1, 2 };
341
343
344 TEST_ASSERT_EQUAL_HEX8(2, prio[0]);
345 TEST_ASSERT_EQUAL_HEX8(0, prio[1]);
346 TEST_ASSERT_EQUAL_HEX8(1, prio[2]);
347}
348
350{
351 uint8_t prio[3]{ 0 | ON, 1, 2 | ON };
352
354
355 TEST_ASSERT_EQUAL_HEX8(2 | ON, prio[0]);
356 TEST_ASSERT_EQUAL_HEX8(0 | ON, prio[1]);
357 TEST_ASSERT_EQUAL_HEX8(1, prio[2]);
358}
359
361{
362 uint8_t prio[4]{ 3, 1, 0, 2 };
363
364 for (uint8_t i = 0; i != 4; ++i)
365 {
367 }
368
369 TEST_ASSERT_EQUAL_HEX8(3, prio[0]);
370 TEST_ASSERT_EQUAL_HEX8(1, prio[1]);
371 TEST_ASSERT_EQUAL_HEX8(0, prio[2]);
372 TEST_ASSERT_EQUAL_HEX8(2, prio[3]);
373}
374
376{
377 // guards around the array: rotate() must write nothing outside it
378 struct
379 {
380 uint8_t before{ 0xAA };
381 uint8_t prio[1]{ 0 | ON };
382 uint8_t after[4]{ 0x55, 0x55, 0x55, 0x55 };
383 } mem;
384
385 LoadPriorities::rotate(mem.prio);
386
387 TEST_ASSERT_EQUAL_HEX8(0 | ON, mem.prio[0]);
388 TEST_ASSERT_EQUAL_HEX8(0xAA, mem.before);
389 TEST_ASSERT_EQUAL_HEX8(0x55, mem.after[0]);
390 TEST_ASSERT_EQUAL_HEX8(0x55, mem.after[3]);
391}
392
393// ============================================================================
394// toPhysical
395// ============================================================================
396
398{
399 const uint8_t prio[3]{ 2 | ON, 0, 1 | ON };
400 LoadStates physical[3]{};
401
402 LoadPriorities::toPhysical< false >(prio, physical, true, noOverride);
403
404 TEST_ASSERT_TRUE(LoadStates::LOAD_OFF == physical[0]);
405 TEST_ASSERT_TRUE(LoadStates::LOAD_ON == physical[1]);
406 TEST_ASSERT_TRUE(LoadStates::LOAD_ON == physical[2]);
407}
408
410{
411 const uint8_t prio[3]{ 0 | ON, 1 | ON, 2 | ON };
413
414 LoadPriorities::toPhysical< false >(prio, physical, false, noOverride);
415
416 TEST_ASSERT_TRUE(LoadStates::LOAD_OFF == physical[0]);
417 TEST_ASSERT_TRUE(LoadStates::LOAD_OFF == physical[1]);
418 TEST_ASSERT_TRUE(LoadStates::LOAD_OFF == physical[2]);
419}
420
422{
423 const uint8_t prio[3]{ 0, 1, 2 };
424 LoadStates physical[3]{};
425 const auto overrideLoad1 = [](const uint8_t iLoad)
426 {
427 return iLoad == 1;
428 };
429
430 // diversion on or off: the forced load is on, the others follow their logical state
431 LoadPriorities::toPhysical< false >(prio, physical, true, overrideLoad1);
432 TEST_ASSERT_TRUE(LoadStates::LOAD_OFF == physical[0]);
433 TEST_ASSERT_TRUE(LoadStates::LOAD_ON == physical[1]);
434
435 LoadPriorities::toPhysical< false >(prio, physical, false, overrideLoad1);
436 TEST_ASSERT_TRUE(LoadStates::LOAD_ON == physical[1]);
437 TEST_ASSERT_TRUE(LoadStates::LOAD_OFF == physical[2]);
438}
439
441{
442 const uint8_t prio[3]{ 2, 0, 1 };
443 LoadStates physical[3]{};
444 // the override of load 2, the first in priority order
445 const auto overrideLoad2 = [](const uint8_t iLoad)
446 {
447 return iLoad == 2;
448 };
449
450 LoadPriorities::toPhysical< false >(prio, physical, true, overrideLoad2);
451
452 TEST_ASSERT_TRUE(LoadStates::LOAD_ON == physical[2]);
453 TEST_ASSERT_TRUE(LoadStates::LOAD_OFF == physical[0]);
454 TEST_ASSERT_TRUE(LoadStates::LOAD_OFF == physical[1]);
455}
456
458{
459 const uint8_t prio[3]{ 0 | ON, 1 | ON, 2 };
461 const auto overrideAll = [](uint8_t /* iLoad */)
462 {
463 return true;
464 };
465
466 LoadPriorities::toPhysical< true >(prio, physical, true, overrideAll);
467
468 TEST_ASSERT_TRUE(LoadStates::LOAD_OFF == physical[0]);
469 TEST_ASSERT_TRUE(LoadStates::LOAD_OFF == physical[1]);
470 TEST_ASSERT_TRUE(LoadStates::LOAD_OFF == physical[2]);
471}
472
473int main(void)
474{
475 UNITY_BEGIN();
476
482
497
502
508
509 return UNITY_END();
510}
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
uint8_t i
LoadStates
Definition load_map.h:32
The load priority state machine: which load to switch at each mains cycle.
constexpr uint8_t loadStateOnBit
uint8_t nextToRemove(const uint8_t(&priorities)[N])
The next logical load to switch OFF: the last one ON, in priority order.
uint8_t nextToAdd(const uint8_t(&priorities)[N])
The next logical load to switch ON: the first one OFF, in priority order.
void toPhysical(const uint8_t(&priorities)[N], LoadStates(&physical)[N], const bool diversionEnabled, IsOverridden isOverridden)
Maps the logical load states to the physical ones.
void rotate(uint8_t(&priorities)[N])
Rotates the priorities by one: the last load becomes the first, the others move down one place....
int main(void)
void test_the_lower_threshold_stays_within_the_bucket(void)
void test_rotate_n_times_is_back_to_the_start(void)
void test_nothing_to_switch_starts_no_post_transition_period(void)
void test_rotate_a_single_load_changes_nothing(void)
void test_next_to_remove_none_when_all_off(void)
Definition test_main.cpp:99
void test_the_override_is_asked_for_the_physical_load(void)
void test_the_upper_threshold_stays_within_the_bucket(void)
void test_rotate_moves_the_last_load_first(void)
void test_next_to_add_none_when_all_on(void)
Definition test_main.cpp:81
void test_a_surplus_brings_every_load_on_then_its_end_every_load_off(void)
void test_the_upper_threshold_follows_the_bucket_during_the_post_transition_period(void)
void test_rotate_keeps_each_load_state(void)
void test_the_previous_load_waits_for_the_end_of_the_post_transition_period(void)
void test_an_override_forces_its_load_on(void)
void test_switching_on_follows_the_priorities_not_the_load_numbers(void)
void test_calibration_mode_keeps_every_load_off(void)
void test_next_to_remove_is_the_last_load_on(void)
Definition test_main.cpp:88
void test_diversion_off_switches_every_load_off(void)
void test_the_middle_of_the_bucket_counts_as_the_lower_half(void)
void test_each_half_resets_the_opposite_threshold(void)
void test_next_ignores_the_load_numbers(void)
void test_next_to_add_is_the_first_load_off(void)
Definition test_main.cpp:70
void test_high_energy_switches_the_first_load_on(void)
void test_low_energy_switches_the_last_load_off(void)
void test_the_next_load_waits_for_the_end_of_the_post_transition_period(void)
void test_physical_states_follow_the_load_numbers(void)
void test_anti_flicker_thresholds_leave_a_dead_band(void)
void test_the_load_switched_last_may_switch_back_at_once(void)
static void cycle(RemoteLoadCore< NumUnits > &core, const LoadStates(&states)[NumLoads])
Run one mains cycle: feed the states to the core, for the given map.
Definition test_main.cpp:26
The fixed levels of the energy bucket, in bucket units.
The switching decision, once per mains cycle, and its state.
static constexpr uint8_t POST_TRANSITION_MAX_COUNT
void onNewCycle(uint8_t(&priorities)[N], const int32_t &energy, const Bucket &bucket)
The decision of one mains cycle: may switch one load ON or OFF.