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
10
11#include <unity.h>
12
13#include "config_types.h"
14#include "load_map.h"
15#include "load_priorities.h"
16#include "rotation_groups.h"
17
18void setUp(void) {}
19void tearDown(void) {}
20
21// ---- the setting ----
22
23void test_no_group(void)
24{
25 constexpr RotationGroups groups{};
26 TEST_ASSERT_EQUAL(0, groups.size());
27}
28
30{
31 constexpr RotationGroups groups{ { ALL_LOCAL_LOADS() } };
32 TEST_ASSERT_EQUAL(1, groups.size());
33 TEST_ASSERT_EQUAL(1, groups.getGroup(0).count);
34 TEST_ASSERT_EQUAL(ALL_LOCAL_LOADS(), groups.getGroup(0).pins[0]);
35}
36
38{
39 constexpr RotationGroups groups{ { LOAD(0), LOAD(1), LOAD(2) }, { LOAD(3), LOAD(4), LOAD(5) } };
40 TEST_ASSERT_EQUAL(2, groups.size());
41 TEST_ASSERT_EQUAL(3, groups.getGroup(1).count);
42 TEST_ASSERT_EQUAL(LOAD(4), groups.getGroup(1).pins[1]);
43}
44
46{
47 constexpr RotationGroups groups{ LOAD(0), LOAD(1) };
48 TEST_ASSERT_EQUAL(2, groups.size());
49 TEST_ASSERT_EQUAL(1, groups.getGroup(0).count);
50}
51
52// ---- from codes to load numbers ----
53
54namespace
55{
56constexpr uint8_t ON{ loadStateOnBit };
57
58// 3 local loads, then 2 on unit 1, then 1 on unit 2
59constexpr uint8_t loadMap[]{ Load::local(5), Load::local(6), Load::local(7), Load::remote(1), Load::remote(1), Load::remote(2) };
60constexpr uint8_t prio[]{ 0, 1, 2, 3, 4, 5 };
61
62template< typename Groups >
63constexpr bool allChecksPass(const Groups& groups)
64{
65 return Rotation::targetsAreLoads(groups, loadMap) && Rotation::groupsHaveTwoLoads(groups, loadMap)
66 && Rotation::noLoadTwice(groups, loadMap) && Rotation::groupsAreConsistent(groups, loadMap);
67}
68} // namespace
69
71{
72 TEST_ASSERT_EQUAL_HEX32(0b10, Rotation::loadsOf(LOAD(1), loadMap));
73 TEST_ASSERT_EQUAL_HEX32(0b100, Rotation::loadsOf(LOCAL_LOAD(2), loadMap));
74 TEST_ASSERT_EQUAL_HEX32(0, Rotation::loadsOf(LOCAL_LOAD(3), loadMap)); // load 3 is remote
75 TEST_ASSERT_EQUAL_HEX32(0, Rotation::loadsOf(LOAD(6), loadMap)); // only 6 loads
76}
77
79{
80 constexpr uint8_t mixed[]{ Load::local(5), Load::remote(1), Load::local(6), Load::remote(2) };
81 TEST_ASSERT_EQUAL_HEX32(0b0010, Rotation::loadsOf(REMOTE_LOAD(0), mixed));
82 TEST_ASSERT_EQUAL_HEX32(0b1000, Rotation::loadsOf(REMOTE_LOAD(1), mixed));
83 TEST_ASSERT_EQUAL_HEX32(0, Rotation::loadsOf(REMOTE_LOAD(2), mixed));
84}
85
87{
88 TEST_ASSERT_EQUAL_HEX32(0b10, Rotation::loadsOf(6, loadMap));
89 TEST_ASSERT_EQUAL_HEX32(0, Rotation::loadsOf(9, loadMap));
90}
91
93{
94 TEST_ASSERT_EQUAL_HEX32(0b000111, Rotation::loadsOf(ALL_LOCAL_LOADS(), loadMap));
95 TEST_ASSERT_EQUAL_HEX32(0b111000, Rotation::loadsOf(ALL_REMOTE_LOADS(), loadMap));
96 TEST_ASSERT_EQUAL_HEX32(0b111111, Rotation::loadsOf(ALL_LOADS(), loadMap));
97 TEST_ASSERT_EQUAL_HEX32(0, Rotation::loadsOf(RELAY(0), loadMap));
98 TEST_ASSERT_EQUAL_HEX32(0, Rotation::loadsOf(ALL_RELAYS(), loadMap));
99 TEST_ASSERT_EQUAL_HEX32(0, Rotation::loadsOf(ALL_LOADS_AND_RELAYS(), loadMap));
100}
101
102// ---- the checks ----
103
105{
106 constexpr RotationGroups groups{ { LOAD(0), LOAD(1), LOAD(2) }, { LOAD(3), LOAD(4) } };
107 TEST_ASSERT_TRUE(allChecksPass(groups));
108}
109
111{
112 constexpr RotationGroups groups{ { LOAD(0), RELAY(0) } };
113 TEST_ASSERT_FALSE(Rotation::targetsAreLoads(groups, loadMap));
114}
115
117{
118 constexpr RotationGroups groups{ { LOAD(0) }, { LOAD(1), LOAD(2) } };
119 TEST_ASSERT_FALSE(Rotation::groupsHaveTwoLoads(groups, loadMap));
120}
121
123{
124 constexpr RotationGroups across{ { LOAD(0), LOAD(1) }, { LOAD(1), LOAD(2) } };
125 constexpr RotationGroups within{ { LOAD(0), LOAD(0), LOAD(1) } };
126 constexpr RotationGroups byGroup{ { ALL_LOCAL_LOADS(), LOAD(1) } };
127 TEST_ASSERT_FALSE(Rotation::noLoadTwice(across, loadMap));
128 TEST_ASSERT_FALSE(Rotation::noLoadTwice(within, loadMap));
129 TEST_ASSERT_FALSE(Rotation::noLoadTwice(byGroup, loadMap));
130}
131
133{
134 constexpr RotationGroups groups{ { LOAD(2), LOAD(3) } };
135 TEST_ASSERT_FALSE(Rotation::groupsAreConsistent(groups, loadMap));
136}
137
139{
140 constexpr RotationGroups groups{ { LOAD(4), LOAD(5) } };
141 TEST_ASSERT_FALSE(Rotation::groupsAreConsistent(groups, loadMap));
142}
143
145{
146 constexpr RotationGroups groups{};
147 TEST_ASSERT_TRUE(allChecksPass(groups));
148}
149
150// ---- the rotation ----
151
153{
154 constexpr uint8_t map4[]{ Load::local(5), Load::local(6), Load::local(7), Load::local(8) };
155 constexpr uint8_t prio4[]{ 0, 1, 2, 3 };
156 constexpr Rotation::Table table{ RotationGroups{ { ALL_LOADS() } }, map4, prio4 };
157 uint8_t a[]{ 0, 1, 2 | ON, 3 };
158 uint8_t b[]{ 0, 1, 2 | ON, 3 };
159 table.rotate(a);
161 TEST_ASSERT_EQUAL_UINT8_ARRAY(b, a, 4);
162}
163
165{
166 constexpr uint8_t map6[]{ Load::local(2), Load::local(3), Load::local(4), Load::local(5), Load::local(6), Load::local(7) };
167 constexpr uint8_t prio6[]{ 0, 3, 1, 4, 2, 5 };
168 constexpr Rotation::Table table{ RotationGroups{ { LOAD(0), LOAD(1), LOAD(2) }, { LOAD(3), LOAD(4), LOAD(5) } }, map6, prio6 };
169 uint8_t p[]{ 0, 3, 1, 4, 2, 5 };
170 table.rotate(p);
171 constexpr uint8_t expected[]{ 2, 5, 0, 3, 1, 4 };
172 TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, p, 6);
173}
174
176{
177 constexpr Rotation::Table table{ RotationGroups{ { LOAD(0), LOAD(1), LOAD(2) } }, loadMap, prio };
178 uint8_t p[]{ 0, 1, 2, 3, 4, 5 };
179 table.rotate(p);
180 constexpr uint8_t expected[]{ 2, 0, 1, 3, 4, 5 };
181 TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, p, 6);
182}
183
185{
186 constexpr uint8_t map3[]{ Load::local(5), Load::local(6), Load::local(7) };
187 constexpr uint8_t prio3[]{ 0, 1, 2 };
188 constexpr Rotation::Table table{ RotationGroups{ { LOAD(0), LOAD(1) } }, map3, prio3 };
189 uint8_t p[]{ 0 | ON, 1, 2 };
190 table.rotate(p);
191 constexpr uint8_t expected[]{ 1, 0 | ON, 2 };
192 TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, p, 3);
193}
194
196{
197 constexpr Rotation::Table table{ RotationGroups{ { LOAD(3), LOAD(4) }, { ALL_LOCAL_LOADS() } }, loadMap, prio };
198 uint8_t p[]{ 0, 1 | ON, 2, 3, 4 | ON, 5 };
199 for (uint8_t i = 0; i != 6; ++i) // 6: a multiple of both group sizes
200 {
201 table.rotate(p);
202 }
203 constexpr uint8_t expected[]{ 0, 1 | ON, 2, 3, 4 | ON, 5 };
204 TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, p, 6);
205}
206
208{
209 constexpr Rotation::Table table{ RotationGroups{}, loadMap, prio };
210 uint8_t p[]{ 5, 4, 3, 2, 1, 0 };
211 table.rotate(p);
212 constexpr uint8_t expected[]{ 5, 4, 3, 2, 1, 0 };
213 TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, p, 6);
214 TEST_ASSERT_EQUAL(0, table.size());
215}
216
218{
219 constexpr Rotation::Table table{ RotationGroups{ { LOAD(3), LOAD(4) }, { ALL_LOCAL_LOADS() } }, loadMap, prio };
220 TEST_ASSERT_EQUAL(2, table.size());
221 TEST_ASSERT_EQUAL_HEX32(0b011000, table.loads(0));
222 TEST_ASSERT_EQUAL_HEX32(0b000111, table.loads(1));
223}
224
The rotation groups, as in config.h: the loads that take turns at the top of the priority list,...
The rotation groups, resolved: the priority places each group holds.
void rotate(uint8_t(&prio)[N]) const
Rotates each group by one among its places; each entry keeps its ON bit.
What config.h is written with: its types, constants and helpers.
constexpr uint8_t ALL_REMOTE_LOADS()
Every remote load.
constexpr uint8_t LOCAL_LOAD(const uint8_t loadNum)
Same as LOAD(), for a load known to be local.
constexpr uint8_t ALL_RELAYS()
Every relay (none without relay diversion).
constexpr uint8_t ALL_LOCAL_LOADS()
Every local load.
constexpr uint8_t RELAY(const uint8_t relayNum)
Relay relayNum of relays (0, no relay, if out of range or without relay diversion).
constexpr uint8_t ALL_LOADS()
Every load, local and remote.
constexpr uint8_t LOAD(const uint8_t loadNum)
Load loadNum of physicalLoadPin[], local or remote (0, no load, if out of range).
constexpr uint8_t ALL_LOADS_AND_RELAYS()
Every load and every relay.
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
uint8_t i
Encoding of the load map: one packed byte per dump load.
The load priority state machine: which load to switch at each mains cycle.
constexpr uint8_t loadStateOnBit
void rotate(uint8_t(&priorities)[N])
Rotates the priorities by one: the last load becomes the first, the others move down one place....
constexpr uint8_t local(uint8_t pin)
Declares a load driven locally by the router.
Definition load_map.h:68
constexpr uint8_t remote(uint8_t unit, uint8_t ledPin=0)
Declares a load driven by a remote unit over RF.
Definition load_map.h:79
constexpr bool noLoadTwice(const RotationGroups< NG, MaxPins > &groups, const uint8_t(&loadMap)[NL])
No load is named twice, within a group or across groups.
constexpr bool targetsAreLoads(const RotationGroups< NG, MaxPins > &groups, const uint8_t(&loadMap)[NL])
Every code of every group stands for at least one load.
constexpr uint32_t loadsOf(const uint8_t code, const uint8_t(&loadMap)[NL])
The loads a target code stands for, by load number.
constexpr bool groupsHaveTwoLoads(const RotationGroups< NG, MaxPins > &groups, const uint8_t(&loadMap)[NL])
Every group holds at least 2 loads.
constexpr bool groupsAreConsistent(const RotationGroups< NG, MaxPins > &groups, const uint8_t(&loadMap)[NL])
In each group, every load is local, or every load is on the same remote unit.
int main(void)
void test_checks_pass_without_groups(void)
void test_check_mixed_local_and_remote(void)
void test_checks_pass_for_two_good_groups(void)
void test_loads_of_pin_is_the_local_load_on_it(void)
Definition test_main.cpp:86
void test_check_group_of_one(void)
void test_table_gives_the_loads_of_each_group(void)
void test_check_two_remote_units(void)
void test_loads_of_load_and_local_load(void)
Definition test_main.cpp:70
void test_missing_inner_braces_make_one_load_groups(void)
Definition test_main.cpp:45
void test_table_without_groups_changes_nothing(void)
void test_n_rotations_are_back_to_the_start(void)
void test_two_groups_keep_their_codes(void)
Definition test_main.cpp:37
void test_loads_of_groups_and_relays(void)
Definition test_main.cpp:92
void test_one_group_of_every_local_load(void)
Definition test_main.cpp:29
void test_on_bits_travel_with_their_load(void)
void test_two_interleaved_groups_rotate_apart(void)
void test_one_group_of_every_load_is_todays_rotation(void)
void test_loads_of_remote_load_follows_map_order(void)
Definition test_main.cpp:78
void test_loads_outside_groups_never_move(void)
void test_check_relay_is_not_a_load(void)
void test_no_group(void)
Definition test_main.cpp:23
void test_check_load_twice(void)
#define REMOTE_LOAD(n)
The rotation groups: which loads take turns, checked and resolved at compile time.