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 "utils_dualtariff.h"
15#include "utils_override.h" // TargetMap // Real header - pure C++
16
17namespace
18{
19constexpr uint32_t MINUTE{ 60UL * 1000UL };
20constexpr uint32_t HOUR{ 60UL * MINUTE };
21} // namespace
22
23void setUp(void) {}
24void tearDown(void) {}
25
26// ============================================================================
27// Scheduled boosts: BoostWindow
28// ============================================================================
29
31{
32 TEST_ASSERT_EQUAL_UINT16(120, HOURS(2).value);
33 TEST_ASSERT_EQUAL_UINT16(720, HOURS(12).value);
34 TEST_ASSERT_EQUAL_UINT16(90, MINUTES(90).value);
35}
36
38{
39 TEST_ASSERT_EQUAL_UINT32(0, DualTariff::windowStart(FIRST(HOURS(2)), 8));
40 TEST_ASSERT_EQUAL_UINT32(7200000UL, DualTariff::windowEnd(FIRST(HOURS(2)), 8));
41}
42
44{
45 TEST_ASSERT_EQUAL_UINT32(21600000UL, DualTariff::windowStart(LAST(HOURS(2)), 8));
46 TEST_ASSERT_EQUAL_UINT32(UINT32_MAX, DualTariff::windowEnd(LAST(HOURS(2)), 8));
47}
48
50{
51 constexpr BoostWindow w{ FROM_START(HOURS(2)), FOR(HOURS(2)) };
52 TEST_ASSERT_EQUAL_UINT32(7200000UL, DualTariff::windowStart(w, 8));
53 TEST_ASSERT_EQUAL_UINT32(14400000UL, DualTariff::windowEnd(w, 8));
54}
55
57{
58 constexpr BoostWindow w{ BEFORE_END(HOURS(3)), FOR(MINUTES(90)) };
59 TEST_ASSERT_EQUAL_UINT32(18000000UL, DualTariff::windowStart(w, 8));
60 TEST_ASSERT_EQUAL_UINT32(23400000UL, DualTariff::windowEnd(w, 8));
61}
62
64{
66 TEST_ASSERT_EQUAL_UINT32(3600000UL, DualTariff::windowStart(w, 8));
67 TEST_ASSERT_EQUAL_UINT32(UINT32_MAX, DualTariff::windowEnd(w, 8));
68}
69
71{
72 TEST_ASSERT_EQUAL_UINT32(0, DualTariff::windowStart(LAST(HOURS(12)), 12));
73 TEST_ASSERT_EQUAL_UINT32(43140000UL, DualTariff::windowStart(LAST(MINUTES(1)), 12));
74 TEST_ASSERT_EQUAL_UINT32(43200000UL, DualTariff::windowEnd(FIRST(HOURS(12)), 12));
75}
76
77// 3 h before the end, for 2 h, and 1 h 30 before the end, for 1 h, whatever the period: no
78// 16-bit overflow from 10 h on (#214)
80{
81 for (uint8_t hours = 3; hours <= 12; ++hours)
82 {
83 const BoostWindow a{ BEFORE_END(HOURS(3)), FOR(HOURS(2)) };
84 TEST_ASSERT_EQUAL_UINT32((hours - 3) * HOUR, DualTariff::windowStart(a, hours));
85 TEST_ASSERT_EQUAL_UINT32((hours - 1) * HOUR, DualTariff::windowEnd(a, hours));
86 TEST_ASSERT_TRUE(DualTariff::startsWithinOffPeak(a, hours));
87 TEST_ASSERT_TRUE(DualTariff::endsWithinOffPeak(a, hours));
88
89 const BoostWindow b{ BEFORE_END(MINUTES(90)), FOR(MINUTES(60)) };
90 TEST_ASSERT_EQUAL_UINT32(hours * HOUR - 90 * MINUTE, DualTariff::windowStart(b, hours));
91 TEST_ASSERT_EQUAL_UINT32(hours * HOUR - 30 * MINUTE, DualTariff::windowEnd(b, hours));
92 }
93}
94
95// FOR(MINUTES(65535)) is a (wrong) length, not 'until the end': it ends after any period
97{
98 constexpr BoostWindow w{ FROM_START(MINUTES(0)), FOR(MINUTES(65535)) };
99 TEST_ASSERT_NOT_EQUAL(UNTIL_THE_END, w.length.minutes);
100 TEST_ASSERT_FALSE(DualTariff::endsWithinOffPeak(w, 12));
101}
102
104{
105 TEST_ASSERT_TRUE(DualTariff::startsWithinOffPeak(FIRST(HOURS(10)), 8)); // starts within, but ends after: see below
106 TEST_ASSERT_TRUE(DualTariff::startsWithinOffPeak(LAST(HOURS(8)), 8)); // starts at 0
107 TEST_ASSERT_FALSE(DualTariff::startsWithinOffPeak(LAST(MINUTES(481)), 8)); // 1 min before the start
108 TEST_ASSERT_FALSE(DualTariff::startsWithinOffPeak(LAST(MINUTES(0)), 8)); // at the end
109 TEST_ASSERT_FALSE(DualTariff::startsWithinOffPeak(BoostWindow{ FROM_START(HOURS(8)), FOR(HOURS(1)) }, 8));
110 TEST_ASSERT_TRUE(DualTariff::startsWithinOffPeak(BoostWindow{ FROM_START(MINUTES(479)), FOR(HOURS(1)) }, 8));
111}
112
114{
115 TEST_ASSERT_FALSE(DualTariff::endsWithinOffPeak(FIRST(HOURS(10)), 8));
116 TEST_ASSERT_TRUE(DualTariff::endsWithinOffPeak(FIRST(HOURS(8)), 8)); // ends at the end
117 TEST_ASSERT_FALSE(DualTariff::endsWithinOffPeak(BoostWindow{ BEFORE_END(HOURS(1)), FOR(HOURS(2)) }, 8));
118 TEST_ASSERT_TRUE(DualTariff::endsWithinOffPeak(BoostWindow{ BEFORE_END(HOURS(3)), FOR(HOURS(2)) }, 8));
119 TEST_ASSERT_TRUE(DualTariff::endsWithinOffPeak(BoostWindow{ FROM_START(HOURS(7)), FOR(HOURS(1)) }, 8));
120 TEST_ASSERT_FALSE(DualTariff::endsWithinOffPeak(BoostWindow{ FROM_START(HOURS(7)), FOR(MINUTES(61)) }, 8));
121 TEST_ASSERT_TRUE(DualTariff::endsWithinOffPeak(LAST(HOURS(8)), 8)); // until the end
122 TEST_ASSERT_TRUE(DualTariff::endsWithinOffPeak(BoostWindow{ FROM_START(HOURS(1)), BoostLength{ UNTIL_THE_END } }, 8));
123 TEST_ASSERT_TRUE(DualTariff::endsWithinOffPeak(FIRST(HOURS(12)), 12));
124}
125
127{
128 TEST_ASSERT_FALSE(DualTariff::hasLength(FIRST(MINUTES(0))));
129 TEST_ASSERT_FALSE(DualTariff::hasLength(BoostWindow{ BEFORE_END(HOURS(2)), FOR(MINUTES(0)) }));
130 TEST_ASSERT_TRUE(DualTariff::hasLength(LAST(HOURS(2))));
131}
132
134{
135 static_assert(DualTariff::windowEnd(FIRST(HOURS(2)), 8) == 7200000UL, "computed at compile time");
136 TEST_PASS();
137}
138
140{
141 constexpr BoostWindow w{ FROM_START(HOURS(2)), FOR(HOURS(3)) }; // 2 h to 5 h
142 const uint32_t start{ DualTariff::windowStart(w, 8) };
143 const uint32_t end{ DualTariff::windowEnd(w, 8) };
144
145 TEST_ASSERT_FALSE(DualTariff::inWindow(2 * HOUR - 1, start, end));
146 TEST_ASSERT_TRUE(DualTariff::inWindow(2 * HOUR, start, end));
147 TEST_ASSERT_TRUE(DualTariff::inWindow(5 * HOUR - 1, start, end));
148 TEST_ASSERT_FALSE(DualTariff::inWindow(5 * HOUR, start, end));
149 TEST_ASSERT_TRUE(DualTariff::inWindow(UINT32_MAX - 1, DualTariff::windowStart(LAST(HOURS(2)), 8), DualTariff::windowEnd(LAST(HOURS(2)), 8)));
150}
151
153{
154 const uint32_t offPeakStart{ UINT32_MAX - MINUTE + 1 }; // 1 min before millis() wraps
155 const uint32_t start{ DualTariff::windowStart(LAST(HOURS(3)), 8) }; // 5 h
156 const uint32_t end{ DualTariff::windowEnd(LAST(HOURS(3)), 8) };
157
158 const uint32_t at6h{ offPeakStart + 6 * HOUR }; // wrapped around
159 const uint32_t at4h{ offPeakStart + 4 * HOUR };
160
161 TEST_ASSERT_TRUE(at6h < offPeakStart);
162 TEST_ASSERT_TRUE(DualTariff::inWindow(at6h - offPeakStart, start, end));
163 TEST_ASSERT_FALSE(DualTariff::inWindow(at4h - offPeakStart, start, end));
164}
165
166namespace
167{
168// pins written raw: LOAD() needs a load map (test_boosts_from_the_settings)
169constexpr BoostSchedule boosts{ {
170 { FIRST(HOURS(2)), { 5, 7 } },
171 { LAST(HOURS(1)), { REMOTE_PIN_BASE + 1 } },
172 { FROM_START(HOURS(1)), { 5 } }, // no FOR: until the end
173 { BEFORE_END(HOURS(3)), FOR(MINUTES(90)), { 9 } },
174} };
175static_assert(boosts.size() == 4);
176} // namespace
177
179{
180 TEST_ASSERT_EQUAL_UINT16((1U << 5) | (1U << 7), boosts.getLocalBitmask(0));
181 TEST_ASSERT_EQUAL_UINT8(0, boosts.getRemoteBitmask(0));
182 TEST_ASSERT_EQUAL_UINT16(0, boosts.getLocalBitmask(1));
183 TEST_ASSERT_EQUAL_UINT8(0b10, boosts.getRemoteBitmask(1));
184}
185
187{
188 TEST_ASSERT_EQUAL_UINT32(3600000UL, DualTariff::resolve(boosts, 2, 8).start);
189 TEST_ASSERT_EQUAL_UINT32(UINT32_MAX, DualTariff::resolve(boosts, 2, 8).end);
190}
191
192void test_resolve(void)
193{
194 constexpr auto b{ DualTariff::resolve(boosts, 3, 8) };
195 static_assert(b.start == 18000000UL && b.end == 23400000UL && b.local == (1U << 9) && b.remote == 0);
196 TEST_ASSERT_EQUAL_UINT32(18000000UL, b.start);
197}
198
200{
201 TEST_ASSERT_TRUE(boosts.getLocalBitmask(0) & boosts.getLocalBitmask(2)); // pin 5 twice
202}
203
205{
206 constexpr BoostSchedule none{};
207 static_assert(none.size() == 0);
208 TEST_ASSERT_EQUAL_UINT8(0, none.size());
209}
210
212{
213 // as config_derived.h builds boostSchedule from the scheduledBoosts of config.h
214 static constexpr uint8_t map[]{ Load::local(5), Load::remote(1) };
215 static constexpr RelaySetting relaySettings[]{ { 8 } };
216 static constexpr RelayEngine relays{ relaySettings };
217 constexpr TargetMap targets{ map, relays, true };
218 constexpr ScheduledBoosts settings{ { { FIRST(HOURS(2)), { LOAD(0), RELAY(0) } },
219 { LAST(MINUTES(90)), { LOAD(1) } } } };
220 constexpr BoostSchedule fromSettings{ settings, targets };
221
222 static_assert(fromSettings.size() == 2);
223 TEST_ASSERT_EQUAL_UINT16((1U << 5) | (1U << 8), fromSettings.getLocalBitmask(0));
224 TEST_ASSERT_EQUAL_UINT8(0, fromSettings.getRemoteBitmask(0));
225 TEST_ASSERT_EQUAL_UINT16(0, fromSettings.getLocalBitmask(1));
226 TEST_ASSERT_EQUAL_UINT8(0b1, fromSettings.getRemoteBitmask(1));
227 TEST_ASSERT_EQUAL_UINT16(90, fromSettings.getWindow(1).start.minutes);
228 TEST_ASSERT_TRUE(fromSettings.getWindow(1).start.fromEnd);
229}
230
232{
233 static constexpr uint8_t map[]{ Load::local(5) };
234 static constexpr RelaySetting relaySettings[]{ { 8 } };
235 static constexpr RelayEngine relays{ relaySettings };
236 constexpr ScheduledBoosts settings{};
237 constexpr BoostSchedule none{ settings, TargetMap{ map, relays, false } };
238 static_assert(none.size() == 0);
239 TEST_ASSERT_EQUAL_UINT8(0, none.size());
240}
241
243{
244 // 1 h period, start 2 h before the end: -60 min, in uint32_t. On the host, this only pins
245 // the value: the uint32_t cast in windowStart() matters on AVR, where an int is 16 bits.
246 const BoostWindow w{ BEFORE_END(HOURS(2)), FOR(MINUTES(10)) };
247 TEST_ASSERT_EQUAL_UINT32(static_cast< uint32_t >(-60) * 60000UL, DualTariff::windowStart(w, 1));
248}
249
The scheduled boosts: a window and the bitmasks of its targets, for each entry.
The relays, as in config.h: the sliding average and the settings of each relay.
The scheduled boosts, as in config.h: each window with the targets it forces.
What the target codes of config.h stand for, once the load map and the relays are known.
constexpr RelayEngine relays
Definition config.h:130
constexpr BoostStart BEFORE_END(const Minutes m)
A start m before the end of the off-peak period.
constexpr uint8_t REMOTE_PIN_BASE
Base value for virtual pins representing remote loads.
constexpr BoostLength FOR(const Minutes m)
A boost lasting m.
constexpr BoostWindow FIRST(const Minutes d)
The first d of the off-peak period.
constexpr BoostWindow LAST(const Minutes d)
The last d of the off-peak period, until the end.
constexpr BoostStart FROM_START(const Minutes m)
A start m after the beginning of the off-peak period.
constexpr uint16_t UNTIL_THE_END
The length of a boost that only the meter ends: the end of the off-peak period stops it anyway.
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 LOAD(const uint8_t loadNum)
Load loadNum of physicalLoadPin[], local or remote (0, no load, if out of range).
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
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
void test_until_the_end_has_no_end(void)
Definition test_main.cpp:63
void test_from_start_for(void)
Definition test_main.cpp:49
void test_boost_window_includes_its_start_and_excludes_its_end(void)
void test_without_for_lasts_until_the_end(void)
void test_ends_within_off_peak(void)
void test_hours_and_minutes(void)
Definition test_main.cpp:30
void test_boost_window_across_the_millis_wraparound(void)
void test_a_target_may_be_in_several_boosts(void)
void test_first_starts_at_the_start_and_lasts_its_duration(void)
Definition test_main.cpp:37
void test_before_end_for(void)
Definition test_main.cpp:56
void test_before_end_from_3_to_12_hours(void)
Definition test_main.cpp:79
int main(void)
void test_no_boost_in_the_settings(void)
void test_targets_become_masks(void)
void test_twelve_hours(void)
Definition test_main.cpp:70
void test_boosts_from_the_settings(void)
void test_last_starts_back_from_the_end_and_has_no_end(void)
Definition test_main.cpp:43
void test_resolve(void)
void test_starts_within_off_peak(void)
void test_an_empty_list(void)
void test_a_start_before_the_period_wraps_to_a_large_value(void)
void test_for_the_largest_minutes_is_not_until_the_end(void)
Definition test_main.cpp:96
void test_boost_windows_are_computed_at_compile_time(void)
void test_a_boost_of_zero_minutes_has_no_length(void)
How long a boost lasts, in minutes, or UNTIL_THE_END.
When a load is boosted during the off-peak period.
The settings of one relay, as in config.h: { pin, surplus, import, minON, minOFF }...
#define MINUTES(value)
A compile-time number of minutes, as an integral_constant.
#define HOURS(value)
A compile-time number of hours, as MINUTES().
Some utility functions for dual tariff feature.
Compile-time utilities for managing override pins and index-to-bitmask mapping.