3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1
8
9#include <unity.h>
10
11#include "utils_override.h" // Real header - pure C++
12
13void setUp(void) {}
14void tearDown(void) {}
15
16// ============================================================================
17// Tests for indicesToBitmask<>()
18// ============================================================================
19
21{
22 TEST_ASSERT_EQUAL(1U << 5, indicesToBitmask< 5 >());
23 TEST_ASSERT_EQUAL(1U << 0, indicesToBitmask< 0 >());
24 TEST_ASSERT_EQUAL(1U << 15, indicesToBitmask< 15 >());
25}
26
28{
29 constexpr uint16_t mask = indicesToBitmask< 2, 4, 7 >();
30 TEST_ASSERT_EQUAL((1U << 2) | (1U << 4) | (1U << 7), mask);
31}
32
34{
35 constexpr uint16_t mask = indicesToBitmask< 3, 4, 5, 6 >();
36 TEST_ASSERT_EQUAL(0b01111000, mask);
37}
38
40{
42 TEST_ASSERT_EQUAL(0xFFFF, mask);
43}
44
45// ============================================================================
46// Tests for are_pins_valid<>()
47// ============================================================================
48
50{
51 TEST_ASSERT_TRUE((are_pins_valid< 2, 7, 10, 13 >()));
52 TEST_ASSERT_TRUE((are_pins_valid< 2 >()));
53 TEST_ASSERT_TRUE((are_pins_valid< 13 >()));
55}
56
58{
59 TEST_ASSERT_FALSE((are_pins_valid< 0 >()));
60 TEST_ASSERT_FALSE((are_pins_valid< 0, 5 >()));
61}
62
64{
65 TEST_ASSERT_FALSE((are_pins_valid< 1 >()));
66 TEST_ASSERT_FALSE((are_pins_valid< 1, 5 >()));
67}
68
70{
71 TEST_ASSERT_FALSE((are_pins_valid< 14 >()));
72 TEST_ASSERT_FALSE((are_pins_valid< 15 >()));
73 TEST_ASSERT_FALSE((are_pins_valid< 5, 14 >()));
74}
75
76// ============================================================================
77// Tests for PinList
78// ============================================================================
79
81{
82 constexpr PinList< 4 > list{};
83 TEST_ASSERT_EQUAL(0, list.count);
84 TEST_ASSERT_EQUAL(0, list.toBitmask());
85}
86
88{
89 constexpr PinList< 4 > list{ 3, 6, 9 };
90 TEST_ASSERT_EQUAL(3, list.count);
91 TEST_ASSERT_EQUAL(3, list.pins[0]);
92 TEST_ASSERT_EQUAL(6, list.pins[1]);
93 TEST_ASSERT_EQUAL(9, list.pins[2]);
94}
95
97{
98 constexpr PinList< 4 > list{ 7 };
99 TEST_ASSERT_EQUAL(1, list.count);
100 TEST_ASSERT_EQUAL(7, list.pins[0]);
101 TEST_ASSERT_EQUAL(1U << 7, list.toBitmask());
102}
103
105{
106 constexpr PinList< 4 > list{ 2, 5, 8, 11 };
107 TEST_ASSERT_EQUAL(4, list.count);
108 TEST_ASSERT_EQUAL((1U << 2) | (1U << 5) | (1U << 8) | (1U << 11), list.toBitmask());
109}
110
112{
113 constexpr PinList< 4 > list{ 2, 5, 8 };
114 constexpr uint16_t expected = (1U << 2) | (1U << 5) | (1U << 8);
115 TEST_ASSERT_EQUAL(expected, list.toBitmask());
116}
117
119{
120 constexpr PinList< 8 > list{ static_cast< uint16_t >(0b10100100) }; // pins 2, 5, 7
121 TEST_ASSERT_EQUAL(3, list.count);
122 TEST_ASSERT_EQUAL(2, list.pins[0]);
123 TEST_ASSERT_EQUAL(5, list.pins[1]);
124 TEST_ASSERT_EQUAL(7, list.pins[2]);
125 TEST_ASSERT_EQUAL(0b10100100, list.toBitmask());
126}
127
129{
130 constexpr PinList< 4 > list{ static_cast< uint16_t >(0) };
131 TEST_ASSERT_EQUAL(0, list.count);
132 TEST_ASSERT_EQUAL(0, list.toBitmask());
133}
134
136{
137 constexpr PinList< 16 > list{ static_cast< uint16_t >(0xFFFF) };
138 TEST_ASSERT_EQUAL(16, list.count);
139 TEST_ASSERT_EQUAL(0xFFFF, list.toBitmask());
140}
141
142// ============================================================================
143// Tests for KeyIndexPair
144// ============================================================================
145
147{
148 constexpr PinList< 4 > list{ 4, 5, 6 };
149 constexpr KeyIndexPair< 4 > pair{ 2, list };
150
151 TEST_ASSERT_EQUAL(2, pair.pin);
152}
153
155{
156 constexpr PinList< 4 > list{ 4, 5, 6 };
157 constexpr KeyIndexPair< 4 > pair{ 2, list };
158
159 TEST_ASSERT_EQUAL((1U << 4) | (1U << 5) | (1U << 6), pair.getBitmask());
160}
161
163{
164 constexpr KeyIndexPair< 4 > pair{ 10, PinList< 4 >{ 7 } };
165 TEST_ASSERT_EQUAL(10, pair.pin);
166 TEST_ASSERT_EQUAL(1U << 7, pair.getBitmask());
167}
168
170{
171 constexpr KeyIndexPair< 8 > pair{ 3, PinList< 8 >{ static_cast< uint16_t >(0b11110000) } };
172 TEST_ASSERT_EQUAL(3, pair.pin);
173 TEST_ASSERT_EQUAL(0b11110000, pair.getBitmask());
174}
175
176// ============================================================================
177// Tests for OverridePins
178// ============================================================================
179
181{
182 constexpr OverridePins pins{
183 { KeyIndexPair< 4 >{ 9, { 2, 3, 4 } } }
184 };
185
186 TEST_ASSERT_EQUAL(1, pins.size());
187 TEST_ASSERT_EQUAL(9, pins.getPin(0));
188 TEST_ASSERT_EQUAL((1U << 2) | (1U << 3) | (1U << 4), pins.getBitmask(0));
189}
190
192{
193 constexpr OverridePins pins{
194 { KeyIndexPair< 4 >{ 2, { 4, 5 } },
195 KeyIndexPair< 4 >{ 3, { 6, 7 } } }
196 };
197
198 TEST_ASSERT_EQUAL(2, pins.size());
199 TEST_ASSERT_EQUAL(2, pins.getPin(0));
200 TEST_ASSERT_EQUAL(3, pins.getPin(1));
201 TEST_ASSERT_EQUAL((1U << 4) | (1U << 5), pins.getBitmask(0));
202 TEST_ASSERT_EQUAL((1U << 6) | (1U << 7), pins.getBitmask(1));
203}
204
206{
207 constexpr OverridePins pins{
208 { KeyIndexPair< 4 >{ 10, { 4, 5 } } }
209 };
210
211 TEST_ASSERT_EQUAL(0, pins.getPin(1));
212 TEST_ASSERT_EQUAL(0, pins.getPin(99));
213}
214
216{
217 constexpr OverridePins pins{
218 { KeyIndexPair< 4 >{ 10, { 4, 5 } } }
219 };
220
221 TEST_ASSERT_EQUAL(0, pins.getBitmask(1));
222 TEST_ASSERT_EQUAL(0, pins.getBitmask(99));
223}
224
226{
227 constexpr OverridePins pins{
228 { KeyIndexPair< 4 >{ 10, { 4, 5 } },
229 KeyIndexPair< 4 >{ 11, { 6, 7 } },
230 KeyIndexPair< 4 >{ 12, { 8, 9 } } }
231 };
232
233 TEST_ASSERT_EQUAL((1U << 4) | (1U << 5), pins.findBitmask(10));
234 TEST_ASSERT_EQUAL((1U << 6) | (1U << 7), pins.findBitmask(11));
235 TEST_ASSERT_EQUAL((1U << 8) | (1U << 9), pins.findBitmask(12));
236}
237
239{
240 constexpr OverridePins pins{
241 { KeyIndexPair< 4 >{ 10, { 4, 5 } } }
242 };
243
244 TEST_ASSERT_EQUAL(0, pins.findBitmask(99));
245 TEST_ASSERT_EQUAL(0, pins.findBitmask(0));
246}
247
249{
250 // Use bitmask to specify pins: 0b11100 = pins 2, 3, 4
251 constexpr OverridePins pins{
252 { KeyIndexPair< 8 >{ 5, static_cast< uint16_t >(0b11100) } }
253 };
254
255 TEST_ASSERT_EQUAL(5, pins.getPin(0));
256 TEST_ASSERT_EQUAL(0b11100, pins.getBitmask(0));
257}
258
260{
261 constexpr OverridePins pins{
262 { KeyIndexPair< 2 >{ 2, { 5, 6 } },
263 KeyIndexPair< 2 >{ 3, { 7, 8 } },
264 KeyIndexPair< 2 >{ 4, { 9, 10 } },
265 KeyIndexPair< 2 >{ 5, { 11, 12 } } }
266 };
267
268 TEST_ASSERT_EQUAL(4, pins.size());
269 TEST_ASSERT_EQUAL((1U << 11) | (1U << 12), pins.findBitmask(5));
270}
271
272// ============================================================================
273// Main
274// ============================================================================
275
276int main(void)
277{
278 UNITY_BEGIN();
279
280 // indicesToBitmask tests
285
286 // are_pins_valid tests
291
292 // PinList tests
295 RUN_TEST(test_PinList_single_pin);
296 RUN_TEST(test_PinList_max_pins);
297 RUN_TEST(test_PinList_toBitmask);
301
302 // KeyIndexPair tests
307
308 // OverridePins tests
317
318 return UNITY_END();
319}
Manages override pins and their associated bitmasks for forced operation.
void setUp(void)
Definition test_main.cpp:15
void tearDown(void)
Definition test_main.cpp:19
int main()
Definition test_main.cpp:82
void test_are_pins_valid_rejects_pin_14_and_above(void)
Definition test_main.cpp:69
void test_PinList_default_constructor(void)
Definition test_main.cpp:80
void test_PinList_single_pin(void)
Definition test_main.cpp:96
void test_OverridePins_getPin_out_of_bounds(void)
void test_OverridePins_getBitmask_out_of_bounds(void)
void test_are_pins_valid_all_valid(void)
Definition test_main.cpp:49
void test_PinList_toBitmask(void)
void test_KeyIndexPair_getBitmask(void)
void test_PinList_from_bitmask(void)
void test_OverridePins_many_entries(void)
void test_KeyIndexPair_from_bitmask(void)
void test_OverridePins_with_bitmask_constructor(void)
void test_indicesToBitmask_consecutive(void)
Definition test_main.cpp:33
void test_PinList_variadic_constructor(void)
Definition test_main.cpp:87
void test_PinList_from_bitmask_full(void)
void test_PinList_from_bitmask_empty(void)
void test_indicesToBitmask_single_index(void)
Definition test_main.cpp:20
void test_KeyIndexPair_construction(void)
void test_OverridePins_multiple_entries(void)
void test_KeyIndexPair_single_pin(void)
void test_are_pins_valid_rejects_pin_0(void)
Definition test_main.cpp:57
void test_OverridePins_single_entry(void)
void test_OverridePins_findBitmask_found(void)
void test_PinList_max_pins(void)
void test_are_pins_valid_rejects_pin_1(void)
Definition test_main.cpp:63
void test_indicesToBitmask_multiple_indices(void)
Definition test_main.cpp:27
void test_indicesToBitmask_all_bits(void)
Definition test_main.cpp:39
void test_OverridePins_findBitmask_not_found(void)
Structure holding a pin and its associated index list.
Wrapper for a list of pins, constructible from variadic arguments.
Compile-time utilities for managing override pins and index-to-bitmask mapping.
constexpr bool are_pins_valid()
Compile-time validation function for pin values.
constexpr uint16_t indicesToBitmask()
Helper to convert pins to a bitmask at compile-time.