Loading files
f4329a85eThis file is pinned to the selected release. Download the original to open it in your design tool.
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2025 Jibril Sharafi
//
// Host unit tests for the pure meter logic. Run with: pio test -e native
// (On Windows the native toolchain is unreliable - run it from WSL.)
//
// These need no hardware: they exercise the CT-polarity detector, the negative
// clamp, the WDRR weighting and the scheduler core entirely in memory. The
// integration simulations at the bottom reproduce the dev-bench starvation
// regression and prove the conduction-gated boost fixes it without breaking
// reversed-CT detection on circuits that are idle at install time.
#include <unity.h>
#include <cstdio>
#include <cmath>
#include "meter_logic.h"
using namespace MeterLogic;
void setUp(void) {}
void tearDown(void) {}
// Config values mirror the firmware defaults so the tests track real behaviour.
static const WeightConfig WCFG = {0.4f, 0.4f, 0.1f, 1.5f, 2.0f};
static const float MIN_A = 0.02f; // validation threshold (firmware: CT rating * MINIMUM_CURRENT_RATIO_VALIDATION, e.g. 20 A * 0.001)
static const float COND_MIN_A = 0.006f; // conducting gate (firmware: CT rating * MINIMUM_CURRENT_RATIO_CONDUCTING, e.g. 20 A * 0.0003)
static const PolarityConfig PCFG = {5, 50, MIN_A};
static const PolarityConfig PCFG_SMALL = {5, 50, COND_MIN_A}; // mirrors firmware cfg after the small-load-polarity fix
static const float COND_A = 1.0f; // a nominal "real load" current (>= MIN_A)
static const float SMALL_LOAD_A = 0.009f; // between COND_MIN_A and MIN_A: a real small load that should now vote
static const float NOISE_A = 0.004f; // below COND_MIN_A: offset noise that must never vote
// ============================================================================
// updatePolarity
// ============================================================================
void test_polarity_not_armed_is_noop(void) {
PolarityState s{false, 0, 0};
PolarityResult r = updatePolarity(s, -100.0f, COND_A, PCFG);
TEST_ASSERT_EQUAL(PolarityAction::Continue, r.action);
TEST_ASSERT_FALSE(r.state.armed);
TEST_ASSERT_EQUAL_INT8(0, r.state.voteCount);
TEST_ASSERT_EQUAL_UINT16(0, r.state.conductingReads);
}
void test_polarity_steady_reverse_flips_at_threshold(void) {
PolarityState s{true, 0, 0};
PolarityAction action = PolarityAction::Continue;
int reads = 0;
for (int i = 0; i < 10; i++) {
PolarityResult r = updatePolarity(s, -230.0f, COND_A, PCFG);
s = r.state;
action = r.action;
reads++;
if (action != PolarityAction::Continue) break;
}
TEST_ASSERT_EQUAL(PolarityAction::Flip, action);
TEST_ASSERT_EQUAL_INT(5, reads); // flips on exactly the 5th reversed vote
TEST_ASSERT_FALSE(s.armed); // disarms after deciding
}
void test_polarity_steady_forward_confirms_without_flip(void) {
PolarityState s{true, 0, 0};
PolarityAction action = PolarityAction::Continue;
for (int i = 0; i < 10; i++) {
PolarityResult r = updatePolarity(s, 1500.0f, COND_A, PCFG);
s = r.state;
action = r.action;
if (action != PolarityAction::Continue) break;
}
TEST_ASSERT_EQUAL(PolarityAction::Disarm, action); // confirmed correct, no flip
TEST_ASSERT_FALSE(s.armed);
}
void test_polarity_is_magnitude_independent(void) {
// A tiny steady reversed load must flip exactly like a huge one.
PolarityState s{true, 0, 0};
PolarityAction action = PolarityAction::Continue;
for (int i = 0; i < 10; i++) {
PolarityResult r = updatePolarity(s, -0.3f, COND_A, PCFG);
s = r.state;
action = r.action;
if (action != PolarityAction::Continue) break;
}
TEST_ASSERT_EQUAL(PolarityAction::Flip, action);
}
void test_polarity_idle_is_noop_and_stays_armed(void) {
// No load: a non-conducting reading must not vote, must not count toward the
// bound, and must leave the channel armed (waiting for load).
PolarityState s{true, 0, 0};
for (int i = 0; i < 500; i++) {
PolarityResult r = updatePolarity(s, 0.0f, 0.0f, PCFG);
s = r.state;
TEST_ASSERT_EQUAL(PolarityAction::Continue, r.action);
}
TEST_ASSERT_TRUE(s.armed);
TEST_ASSERT_EQUAL_INT8(0, s.voteCount);
TEST_ASSERT_EQUAL_UINT16(0, s.conductingReads);
}
void test_polarity_nan_is_noop(void) {
PolarityState s{true, 0, 0};
PolarityResult r = updatePolarity(s, NAN, COND_A, PCFG);
TEST_ASSERT_EQUAL(PolarityAction::Continue, r.action);
TEST_ASSERT_TRUE(r.state.armed);
TEST_ASSERT_EQUAL_INT8(0, r.state.voteCount);
TEST_ASSERT_EQUAL_UINT16(0, r.state.conductingReads);
}
void test_polarity_delayed_load_still_detects(void) {
// THE KEY CORRECTNESS CASE: a CT clipped on a circuit that is off at install
// time. The channel stays armed through a long idle period, then a reversed
// load finally appears - and it must still be detected and flipped.
PolarityState s{true, 0, 0};
for (int i = 0; i < 200; i++) {
s = updatePolarity(s, 0.0f, 0.0f, PCFG).state; // idle: no load yet
}
TEST_ASSERT_TRUE(s.armed); // still armed after a long wait
PolarityAction action = PolarityAction::Continue;
for (int i = 0; i < 10; i++) {
PolarityResult r = updatePolarity(s, -230.0f, COND_A, PCFG); // load finally flows, reversed
s = r.state;
action = r.action;
if (action != PolarityAction::Continue) break;
}
TEST_ASSERT_EQUAL(PolarityAction::Flip, action);
}
void test_polarity_oscillating_conducting_disarms_via_bound(void) {
// A channel that keeps conducting but whose sign oscillates can never decide;
// the conducting-read bound makes it give up (no flip) so it cannot stay
// armed-and-boosted forever.
PolarityState s{true, 0, 0};
PolarityAction action = PolarityAction::Continue;
for (int i = 0; i < 200; i++) {
float p = (i % 2 == 0) ? -50.0f : 50.0f;
PolarityResult r = updatePolarity(s, p, COND_A, PCFG);
s = r.state;
action = r.action;
if (!s.armed) break;
}
TEST_ASSERT_EQUAL(PolarityAction::Disarm, action); // gave up, did not flip
TEST_ASSERT_FALSE(s.armed);
}
void test_polarity_unbounded_oscillation_never_disarms(void) {
// Documents why the bound exists: with maxConductingReads == 0 an oscillating
// conducting channel never decides and never disarms.
PolarityState s{true, 0, 0};
PolarityConfig unbounded{5, 0, MIN_A};
for (int i = 0; i < 1000; i++) {
float p = (i % 2 == 0) ? -50.0f : 50.0f;
s = updatePolarity(s, p, COND_A, unbounded).state;
}
TEST_ASSERT_TRUE(s.armed);
}
// ============================================================================
// isConductingReading (the current gate that stops offset-noise false flips)
// ============================================================================
void test_conducting_true_for_real_load(void) {
TEST_ASSERT_TRUE(isConductingReading(100.0f, 5.0f, MIN_A)); // forward load
TEST_ASSERT_TRUE(isConductingReading(1.0f, MIN_A, MIN_A)); // exactly at the threshold
}
void test_conducting_true_for_reversed_load_with_current(void) {
// CRITICAL for the load/PV priority work: a reversed load reads NEGATIVE power
// but with REAL current, so it must still count as conducting - that is what lets
// a reversed CT both vote (to flip) and earn the armed boost (to resolve fast).
TEST_ASSERT_TRUE(isConductingReading(-100.0f, 5.0f, MIN_A));
TEST_ASSERT_TRUE(isConductingReading(-0.3f, 5.0f, MIN_A)); // tiny reversed power, real current
}
void test_conducting_false_for_offset_noise(void) {
// THE BUG: ADE7953 offset noise is a small, steady, signed power at ~0 current.
// On an unclamped role it must NOT conduct, or it votes a false reversal and hogs.
TEST_ASSERT_FALSE(isConductingReading(1.2f, 0.004f, MIN_A)); // the observed bench value
TEST_ASSERT_FALSE(isConductingReading(-3.5f, 0.0f, MIN_A));
TEST_ASSERT_FALSE(isConductingReading(1.0f, 0.0199f, MIN_A)); // just below the threshold
}
void test_conducting_false_for_zero_power_or_nan(void) {
TEST_ASSERT_FALSE(isConductingReading(0.0f, 5.0f, MIN_A)); // no power, current irrelevant
TEST_ASSERT_FALSE(isConductingReading(NAN, 5.0f, MIN_A)); // bad power read
TEST_ASSERT_FALSE(isConductingReading(100.0f, NAN, MIN_A)); // bad current read
}
void test_conducting_zero_threshold_degrades_to_power_only(void) {
// minCurrent == 0 disables the gate: any non-zero power conducts (old behaviour).
TEST_ASSERT_TRUE(isConductingReading(1.0f, 0.0f, 0.0f));
TEST_ASSERT_FALSE(isConductingReading(0.0f, 0.0f, 0.0f));
}
// ============================================================================
// shouldClampNegative
// ============================================================================
void test_clamp_load_and_pv_negatives(void) {
TEST_ASSERT_TRUE(shouldClampNegative(-5.0f, CHANNEL_ROLE_LOAD));
TEST_ASSERT_TRUE(shouldClampNegative(-5.0f, CHANNEL_ROLE_PV));
}
void test_clamp_allows_grid_battery_inverter_negatives(void) {
TEST_ASSERT_FALSE(shouldClampNegative(-5.0f, CHANNEL_ROLE_GRID));
TEST_ASSERT_FALSE(shouldClampNegative(-5.0f, CHANNEL_ROLE_BATTERY));
TEST_ASSERT_FALSE(shouldClampNegative(-5.0f, CHANNEL_ROLE_INVERTER));
}
void test_clamp_never_touches_positive(void) {
TEST_ASSERT_FALSE(shouldClampNegative(5.0f, CHANNEL_ROLE_LOAD));
TEST_ASSERT_FALSE(shouldClampNegative(0.0f, CHANNEL_ROLE_PV));
TEST_ASSERT_FALSE(shouldClampNegative(0.0f, CHANNEL_ROLE_LOAD)); // exact zero is not negative
TEST_ASSERT_FALSE(shouldClampNegative(NAN, CHANNEL_ROLE_LOAD)); // NaN < 0 is false in IEEE-754
}
// ============================================================================
// energyDirectionFlag
// ============================================================================
void test_energy_direction_splits_export_from_import(void) {
// Regression: the angle branch (channels off the base phase) used to hard-code a
// +1 flag here, which made the exported-energy accumulator unreachable and booked
// every exported Wh as imported. Seen in prod on a 3-phase site: grid L1 export
// grew normally while L2/L3 export stayed pinned at 0 despite thousands of
// readings averaging -1.8 kW.
TEST_ASSERT_EQUAL_FLOAT(-1.0f, energyDirectionFlag(-1800.0f));
TEST_ASSERT_EQUAL_FLOAT(1.0f, energyDirectionFlag(1800.0f));
}
void test_energy_direction_tiny_magnitudes_keep_their_sign(void) {
// Magnitude is irrelevant - only the sign selects the accumulator.
TEST_ASSERT_EQUAL_FLOAT(-1.0f, energyDirectionFlag(-0.001f));
TEST_ASSERT_EQUAL_FLOAT(1.0f, energyDirectionFlag(0.001f));
}
void test_energy_direction_zero_and_nan_are_import(void) {
// Neither carries direction, and both increment by zero, so the branch is moot.
TEST_ASSERT_EQUAL_FLOAT(1.0f, energyDirectionFlag(0.0f));
TEST_ASSERT_EQUAL_FLOAT(1.0f, energyDirectionFlag(-0.0f));
TEST_ASSERT_EQUAL_FLOAT(1.0f, energyDirectionFlag(NAN));
}
// ============================================================================
// weighting
// ============================================================================
void test_role_priority_grid_battery_only(void) {
TEST_ASSERT_TRUE(roleHasSchedulingPriority(CHANNEL_ROLE_GRID));
TEST_ASSERT_TRUE(roleHasSchedulingPriority(CHANNEL_ROLE_BATTERY));
TEST_ASSERT_FALSE(roleHasSchedulingPriority(CHANNEL_ROLE_LOAD));
TEST_ASSERT_FALSE(roleHasSchedulingPriority(CHANNEL_ROLE_PV));
TEST_ASSERT_FALSE(roleHasSchedulingPriority(CHANNEL_ROLE_INVERTER));
}
void test_weight_inactive_is_zero(void) {
ChannelWeightInput in{false, 100.0f, 1.0f, false, CHANNEL_ROLE_LOAD};
TEST_ASSERT_EQUAL_FLOAT(0.0f, computeChannelWeight(in, 0.5f, 0.5f, WCFG));
}
void test_weight_idle_load_is_min_base(void) {
ChannelWeightInput in{true, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
TEST_ASSERT_EQUAL_FLOAT(WCFG.minBase, computeChannelWeight(in, 0.0f, 0.0f, WCFG));
}
void test_weight_grid_gets_multiplier(void) {
ChannelWeightInput load{true, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
ChannelWeightInput grid{true, 0.0f, 0.0f, false, CHANNEL_ROLE_GRID};
float wLoad = computeChannelWeight(load, 0.0f, 0.0f, WCFG);
float wGrid = computeChannelWeight(grid, 0.0f, 0.0f, WCFG);
TEST_ASSERT_EQUAL_FLOAT(wLoad * WCFG.rolePriorityMult, wGrid);
}
void test_weight_armed_boost_only_when_conducting(void) {
// Armed but NOT conducting (no current): NO boost - this is what stops a no-load
// armed channel from hogging the scheduler.
ChannelWeightInput idleArmed{true, 0.0f, 0.0f, true, CHANNEL_ROLE_LOAD, false};
TEST_ASSERT_EQUAL_FLOAT(WCFG.minBase, computeChannelWeight(idleArmed, 0.0f, 0.0f, WCFG));
// Armed AND conducting: boost added on top of the normal weight.
ChannelWeightInput condArmed{true, 500.0f, 0.0f, true, CHANNEL_ROLE_LOAD, true};
ChannelWeightInput condUnarmed{true, 500.0f, 0.0f, false, CHANNEL_ROLE_LOAD, true};
float wArmed = computeChannelWeight(condArmed, 1.0f, 0.0f, WCFG);
float wUnarmed = computeChannelWeight(condUnarmed, 1.0f, 0.0f, WCFG);
TEST_ASSERT_EQUAL_FLOAT(wUnarmed + WCFG.armedBoost, wArmed);
}
void test_weight_clamped_reversed_load_still_boosted(void) {
// The load/PV priority fix: a reversed LOAD is stored clamped to 0, but it IS
// conducting - so it must still earn the armed boost (driven by `conducting`,
// not the clamped stored power). Without this, reversed loads resolved slowly.
ChannelWeightInput in{true, 0.0f /*clamped*/, 0.0f, true, CHANNEL_ROLE_LOAD, true /*conducting*/};
TEST_ASSERT_FLOAT_WITHIN(1e-5, WCFG.minBase + WCFG.armedBoost,
computeChannelWeight(in, 0.0f, 0.0f, WCFG));
// And a clamped reversed PV likewise.
ChannelWeightInput pv{true, 0.0f, 0.0f, true, CHANNEL_ROLE_PV, true};
TEST_ASSERT_FLOAT_WITHIN(1e-5, WCFG.minBase + WCFG.armedBoost,
computeChannelWeight(pv, 0.0f, 0.0f, WCFG));
}
void test_weight_shares_contribute(void) {
ChannelWeightInput in{true, 500.0f, 2.0f, false, CHANNEL_ROLE_LOAD};
// full power share + full variability share + base
float expected = WCFG.powerShare * 1.0f + WCFG.variability * 1.0f + WCFG.minBase;
TEST_ASSERT_EQUAL_FLOAT(expected, computeChannelWeight(in, 1.0f, 1.0f, WCFG));
}
void test_computeWeights_zeroes_channel_zero_and_handles_nan(void) {
const uint8_t N = 4;
ChannelWeightInput in[N];
in[0] = {true, 1000.0f, 0.0f, false, CHANNEL_ROLE_GRID}; // ch0 - excluded
in[1] = {true, NAN, NAN, false, CHANNEL_ROLE_LOAD}; // bad reading
in[2] = {true, 100.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
in[3] = {false, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD}; // inactive
float w[N] = {9, 9, 9, 9};
computeWeights(in, N, 1, WCFG, w);
TEST_ASSERT_EQUAL_FLOAT(0.0f, w[0]); // channel 0 not scheduled
TEST_ASSERT_EQUAL_FLOAT(0.0f, w[3]); // inactive
// NaN channel does not crash and gets a finite (base) weight; ch2 carries the share.
TEST_ASSERT_TRUE(w[1] >= WCFG.minBase);
TEST_ASSERT_TRUE(w[2] > w[1]);
}
// ============================================================================
// scheduler core
// ============================================================================
void test_find_starved_channel(void) {
const uint8_t N = 4;
bool active[N] = {true, true, true, false};
uint64_t last[N] = {0, 19000, 1000, 0};
uint64_t now = 20000;
uint8_t s = findStarvedChannel(last, active, N, 1, now, 15000);
TEST_ASSERT_EQUAL_UINT8(2, s); // ch2 gap 19000 > 15000 (ch1 gap 1000 is fine)
}
void test_find_starved_skips_unbaselined_and_inactive(void) {
const uint8_t N = 4;
bool active[N] = {true, false, true, true};
uint64_t last[N] = {0, 1, 0, 1}; // ch1 inactive, ch2 never baselined (0), ch3 baselined
uint64_t now = 100000;
uint8_t s = findStarvedChannel(last, active, N, 1, now, 15000);
TEST_ASSERT_EQUAL_UINT8(3, s); // only ch3 qualifies
}
void test_find_starved_none(void) {
const uint8_t N = 3;
bool active[N] = {true, true, true};
uint64_t last[N] = {0, 99000, 99500};
uint64_t now = 100000;
TEST_ASSERT_EQUAL_UINT8(NO_CHANNEL, findStarvedChannel(last, active, N, 1, now, 15000));
}
void test_wdrr_accumulate_gains_zeroes_and_clamps(void) {
const uint8_t N = 4;
float weights[N] = {0, 1.0f, 0.1f, 99.0f};
bool active[N] = {true, true, true, false};
float deficits[N] = {0, 4.9f, 0.0f, 7.0f};
wdrrAccumulate(weights, deficits, active, N, 1, 5.0f);
TEST_ASSERT_FLOAT_WITHIN(1e-5, 5.0f, deficits[1]); // 4.9 + 1.0 -> clamped to 5.0
TEST_ASSERT_FLOAT_WITHIN(1e-5, 0.1f, deficits[2]); // 0 + 0.1
TEST_ASSERT_EQUAL_FLOAT(0.0f, deficits[3]); // inactive -> zeroed
}
void test_wdrr_pick_highest_deficit(void) {
const uint8_t N = 4;
float deficits[N] = {0, 0.2f, 0.9f, 0.5f};
bool active[N] = {true, true, true, true};
uint8_t cursor = 0;
uint8_t pick = wdrrPick(deficits, active, N, 1, &cursor);
TEST_ASSERT_EQUAL_UINT8(2, pick);
TEST_ASSERT_FLOAT_WITHIN(1e-5, -0.1f, deficits[2]); // decremented by 1.0
TEST_ASSERT_EQUAL_UINT8(2, cursor);
}
void test_wdrr_pick_all_inactive_returns_invalid(void) {
const uint8_t N = 3;
float deficits[N] = {0, 1.0f, 2.0f};
bool active[N] = {true, false, false};
uint8_t cursor = 0;
TEST_ASSERT_EQUAL_UINT8(NO_CHANNEL, wdrrPick(deficits, active, N, 1, &cursor));
}
void test_wdrr_tie_break_rotates_no_monopoly(void) {
// Equal-deficit idle channels must rotate, not let the lowest index win every
// time. Run many rounds and assert service counts are balanced.
const uint8_t N = 6; // ch0 + 5 schedulable
bool active[N] = {true, true, true, true, true, true};
float weights[N] = {0, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f};
float deficits[N] = {0, 0, 0, 0, 0, 0};
uint8_t cursor = 0;
int picks[N] = {0};
for (int r = 0; r < 500; r++) {
wdrrAccumulate(weights, deficits, active, N, 1, 5.0f);
uint8_t p = wdrrPick(deficits, active, N, 1, &cursor);
TEST_ASSERT_NOT_EQUAL(NO_CHANNEL, p);
picks[p]++;
}
for (uint8_t i = 1; i < N; i++) {
TEST_ASSERT_TRUE(picks[i] > 50); // none starved
}
}
// ============================================================================
// Integration simulations
// ============================================================================
//
// A small driver that runs the full per-round pipeline (weights -> accumulate ->
// pick -> detector) so the pieces are exercised together exactly as the firmware
// composes them.
void test_armed_no_load_channel_does_not_starve_peers(void) {
// ch0 + 6 active LOAD channels, none carrying any load. ch1 is armed (the
// installer just toggled it). The conduction-gated boost means ch1 is NEVER
// boosted while idle, so peers are serviced fairly from the very first round,
// and ch1 simply stays armed waiting for load.
const uint8_t N = 7;
const uint8_t START = 1;
ChannelWeightInput in[N];
bool active[N];
float deficits[N];
float weights[N];
PolarityState pol[N];
for (uint8_t i = 0; i < N; i++) {
in[i] = {i >= START, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
active[i] = (i >= START);
deficits[i] = 0.0f;
weights[i] = 0.0f;
pol[i] = {false, 0, 0};
}
pol[1].armed = true;
in[1].armed = true;
uint8_t cursor = 0;
int gap[N] = {0};
int maxGap[N] = {0};
const int ROUNDS = 600;
for (int r = 0; r < ROUNDS; r++) {
for (uint8_t i = START; i < N; i++) {
in[i].armed = pol[i].armed;
in[i].conducting = (in[i].activePower != 0.0f); // pre-clamp conduction signal
}
computeWeights(in, N, START, WCFG, weights);
wdrrAccumulate(weights, deficits, active, N, START, 5.0f);
uint8_t pick = wdrrPick(deficits, active, N, START, &cursor);
TEST_ASSERT_NOT_EQUAL(NO_CHANNEL, pick);
PolarityResult pr = updatePolarity(pol[pick], in[pick].activePower, COND_A, PCFG);
pol[pick] = pr.state;
for (uint8_t i = START; i < N; i++) {
if (i == pick) gap[i] = 0;
else {
gap[i]++;
if (gap[i] > maxGap[i]) maxGap[i] = gap[i];
}
}
}
// ch1 stays armed (no load ever arrived) - it must keep waiting, not give up.
TEST_ASSERT_TRUE(pol[1].armed);
// No starvation at any point: with 6 equal channels, fair service is ~6 rounds.
for (uint8_t i = START; i < N; i++) {
TEST_ASSERT_TRUE(maxGap[i] > 0);
TEST_ASSERT_TRUE(maxGap[i] < 12);
}
}
void test_armed_conducting_channel_resolves_quickly(void) {
// ch1 armed AND carrying a steady reversed load: the conduction-gated boost
// makes it win most slots and resolve (flip) within a handful of rounds.
const uint8_t N = 7;
const uint8_t START = 1;
ChannelWeightInput in[N];
bool active[N];
float deficits[N];
float weights[N];
PolarityState pol[N];
for (uint8_t i = 0; i < N; i++) {
in[i] = {i >= START, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
active[i] = (i >= START);
deficits[i] = 0.0f;
weights[i] = 0.0f;
pol[i] = {false, 0, 0};
}
pol[1].armed = true;
in[1].activePower = -500.0f; // reversed load conducting
uint8_t cursor = 0;
int flipRound = -1;
for (int r = 0; r < 200; r++) {
for (uint8_t i = START; i < N; i++) {
in[i].armed = pol[i].armed;
in[i].conducting = (in[i].activePower != 0.0f); // pre-clamp conduction signal
}
computeWeights(in, N, START, WCFG, weights);
wdrrAccumulate(weights, deficits, active, N, START, 5.0f);
uint8_t pick = wdrrPick(deficits, active, N, START, &cursor);
PolarityResult pr = updatePolarity(pol[pick], in[pick].activePower, COND_A, PCFG);
if (pr.action == PolarityAction::Flip && flipRound < 0) flipRound = r;
pol[pick] = pr.state;
}
TEST_ASSERT_TRUE(flipRound >= 0);
TEST_ASSERT_TRUE(flipRound < 20); // boosted -> resolves fast
TEST_ASSERT_FALSE(pol[1].armed); // disarmed after the flip
}
void test_grid_sampled_more_often_than_load(void) {
// A grid channel and load channels, all idle: grid should be picked clearly
// more often thanks to the role multiplier (which is NOT gated on conduction).
const uint8_t N = 5; // ch0 + ch1 grid + ch2..4 load
ChannelWeightInput in[N];
bool active[N];
float deficits[N];
float weights[N];
for (uint8_t i = 0; i < N; i++) {
ChannelRole role = (i == 1) ? CHANNEL_ROLE_GRID : CHANNEL_ROLE_LOAD;
in[i] = {i >= 1, 0.0f, 0.0f, false, role};
active[i] = (i >= 1);
deficits[i] = 0.0f;
weights[i] = 0.0f;
}
uint8_t cursor = 0;
int picks[N] = {0};
for (int r = 0; r < 1000; r++) {
computeWeights(in, N, 1, WCFG, weights);
wdrrAccumulate(weights, deficits, active, N, 1, 5.0f);
uint8_t p = wdrrPick(deficits, active, N, 1, &cursor);
picks[p]++;
}
TEST_ASSERT_TRUE(picks[1] > picks[2]);
TEST_ASSERT_TRUE(picks[1] > picks[3]);
TEST_ASSERT_TRUE(picks[1] > picks[4]);
}
// ============================================================================
// Additional coverage (boundaries, precedence, multi-channel, full-size)
// ============================================================================
void test_polarity_nonpositive_threshold_is_noop(void) {
// Misconfigured threshold must be a safe no-op, not decide on the first read.
PolarityState s{true, 0, 0};
PolarityConfig zero{0, 50, MIN_A};
PolarityResult r = updatePolarity(s, -230.0f, COND_A, zero);
TEST_ASSERT_EQUAL(PolarityAction::Continue, r.action);
TEST_ASSERT_TRUE(r.state.armed);
TEST_ASSERT_EQUAL_INT8(0, r.state.voteCount);
}
void test_polarity_threshold_one_decides_immediately(void) {
PolarityState s{true, 0, 0};
PolarityConfig one{1, 50, MIN_A};
TEST_ASSERT_EQUAL(PolarityAction::Flip, updatePolarity(s, -100.0f, COND_A, one).action);
TEST_ASSERT_EQUAL(PolarityAction::Disarm, updatePolarity(s, 100.0f, COND_A, one).action);
}
void test_polarity_one_below_threshold_stays_armed(void) {
PolarityState s{true, 0, 0};
PolarityAction action = PolarityAction::Continue;
for (int i = 0; i < 4; i++) { // threshold is 5
PolarityResult r = updatePolarity(s, -230.0f, COND_A, PCFG);
s = r.state;
action = r.action;
}
TEST_ASSERT_EQUAL(PolarityAction::Continue, action);
TEST_ASSERT_TRUE(s.armed);
TEST_ASSERT_EQUAL_INT8(-4, s.voteCount);
TEST_ASSERT_EQUAL_UINT16(4, s.conductingReads);
}
void test_polarity_flip_takes_precedence_over_bound(void) {
// bound == threshold: a genuinely reversed CT must Flip, not Disarm, on the
// read where both conditions are first satisfiable.
PolarityState s{true, 0, 0};
PolarityConfig cfg{5, 5, MIN_A};
PolarityAction action = PolarityAction::Continue;
for (int i = 0; i < 5; i++) {
PolarityResult r = updatePolarity(s, -230.0f, COND_A, cfg);
s = r.state;
action = r.action;
}
TEST_ASSERT_EQUAL(PolarityAction::Flip, action);
TEST_ASSERT_FALSE(s.armed);
}
void test_polarity_bound_disarms_without_flip(void) {
PolarityState s{true, 0, 0};
PolarityConfig cfg{5, 4, MIN_A};
PolarityAction action = PolarityAction::Continue;
for (int i = 0; i < 4; i++) {
float p = (i % 2 == 0) ? -50.0f : 50.0f; // net stays near 0, never reaches 5
PolarityResult r = updatePolarity(s, p, COND_A, cfg);
s = r.state;
action = r.action;
}
TEST_ASSERT_EQUAL(PolarityAction::Disarm, action);
TEST_ASSERT_FALSE(s.armed);
}
void test_polarity_vote_recovers_and_confirms(void) {
// Goes to -4, then a run of forward votes recovers it to +5 -> confirm (no flip).
PolarityState s{true, 0, 0};
PolarityAction action = PolarityAction::Continue;
for (int i = 0; i < 4; i++) s = updatePolarity(s, -100.0f, COND_A, PCFG).state; // vote -4
TEST_ASSERT_EQUAL_INT8(-4, s.voteCount);
for (int i = 0; i < 9; i++) {
PolarityResult r = updatePolarity(s, 100.0f, COND_A, PCFG); // -3,-2,-1,0,1,2,3,4,5
s = r.state;
action = r.action;
if (action != PolarityAction::Continue) break;
}
TEST_ASSERT_EQUAL(PolarityAction::Disarm, action); // confirmed forward, no flip
}
void test_polarity_vote_recovers_and_flips(void) {
// Mirror: +4 then a run of reversed votes -> reaches -5 -> flip.
PolarityState s{true, 0, 0};
PolarityAction action = PolarityAction::Continue;
for (int i = 0; i < 4; i++) s = updatePolarity(s, 100.0f, COND_A, PCFG).state; // vote +4
for (int i = 0; i < 9; i++) {
PolarityResult r = updatePolarity(s, -100.0f, COND_A, PCFG);
s = r.state;
action = r.action;
if (action != PolarityAction::Continue) break;
}
TEST_ASSERT_EQUAL(PolarityAction::Flip, action);
}
void test_polarity_disarmed_mid_sequence_is_noop(void) {
PolarityState s{false, -3, 3}; // disarmed but with residual counters
PolarityResult r = updatePolarity(s, -230.0f, COND_A, PCFG);
TEST_ASSERT_EQUAL(PolarityAction::Continue, r.action);
TEST_ASSERT_FALSE(r.state.armed);
TEST_ASSERT_EQUAL_INT8(-3, r.state.voteCount); // unchanged
TEST_ASSERT_EQUAL_UINT16(3, r.state.conductingReads);
}
void test_weight_grid_armed_conducting_all_bumps_stack(void) {
// grid (x2) + armed + conducting + full power share: pins the arithmetic order
// (role multiply on the base, THEN add the flat armed boost).
ChannelWeightInput in{true, -500.0f, 0.0f, true, CHANNEL_ROLE_GRID, true};
// base = 0.4*1 + 0.4*0 + 0.1 = 0.5; *2.0 = 1.0; + 1.5 = 2.5
TEST_ASSERT_FLOAT_WITHIN(1e-5, 2.5f, computeChannelWeight(in, 1.0f, 0.0f, WCFG));
// same but not conducting -> no boost: 0.5*2.0 = 1.0
ChannelWeightInput idle{true, 0.0f, 0.0f, true, CHANNEL_ROLE_GRID, false};
TEST_ASSERT_FLOAT_WITHIN(1e-5, 1.0f, computeChannelWeight(idle, 1.0f, 0.0f, WCFG));
}
void test_computeWeights_battery_gets_multiplier(void) {
const uint8_t N = 3;
ChannelWeightInput in[N];
in[0] = {false, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
in[1] = {true, 100.0f, 0.0f, false, CHANNEL_ROLE_BATTERY};
in[2] = {true, 100.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
float w[N] = {};
computeWeights(in, N, 1, WCFG, w);
TEST_ASSERT_TRUE(w[1] > w[2]);
TEST_ASSERT_FLOAT_WITHIN(1e-5, w[2] * WCFG.rolePriorityMult, w[1]);
}
void test_computeWeights_tiny_channel_gets_min_base(void) {
const uint8_t N = 3;
ChannelWeightInput in[N];
in[0] = {false, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
in[1] = {true, 10000.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
in[2] = {true, 1.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
float w[N] = {};
computeWeights(in, N, 1, WCFG, w);
TEST_ASSERT_TRUE(w[2] >= WCFG.minBase); // anti-starvation floor holds under skew
TEST_ASSERT_TRUE(w[1] > w[2]);
}
void test_computeWeights_all_inactive_all_zeros(void) {
const uint8_t N = 4;
ChannelWeightInput in[N];
for (uint8_t i = 0; i < N; i++) in[i] = {false, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
float w[N] = {9, 9, 9, 9};
computeWeights(in, N, 1, WCFG, w);
for (uint8_t i = 0; i < N; i++) TEST_ASSERT_EQUAL_FLOAT(0.0f, w[i]);
}
void test_computeWeights_variability_differentiates_equal_power(void) {
const uint8_t N = 3;
ChannelWeightInput in[N];
in[0] = {false, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
in[1] = {true, 100.0f, 0.1f, false, CHANNEL_ROLE_LOAD};
in[2] = {true, 100.0f, 0.9f, false, CHANNEL_ROLE_LOAD};
float w[N] = {};
computeWeights(in, N, 1, WCFG, w);
TEST_ASSERT_TRUE(w[2] > w[1]); // higher variability -> more weight
}
void test_find_starved_returns_lowest_index_when_multiple(void) {
const uint8_t N = 5;
bool active[N] = {true, true, true, true, true};
uint64_t last[N] = {0, 1000, 2000, 1000, 2000}; // all past gap at now=30000
TEST_ASSERT_EQUAL_UINT8(1, findStarvedChannel(last, active, N, 1, 30000, 5000));
}
void test_find_starved_exactly_at_gap_is_not_starved(void) {
const uint8_t N = 3;
bool active[N] = {true, true, true};
uint64_t last[N] = {0, 5000, 4999}; // ch1 gap=15000 (==), ch2 gap=15001 (>)
TEST_ASSERT_EQUAL_UINT8(2, findStarvedChannel(last, active, N, 1, 20000, 15000));
uint64_t last2[N] = {0, 4999, 5000}; // ch1 gap=15001 (>), ch2 gap=15000 (==)
TEST_ASSERT_EQUAL_UINT8(1, findStarvedChannel(last2, active, N, 1, 20000, 15000));
}
void test_find_starved_now_before_lastMillis_returns_channel(void) {
// Defensive: if lastMillis > now (clock skew / torn read), the unsigned gap
// underflows to a huge value and the channel is force-picked. This matches the
// original firmware and is the intended (force-read rather than defer) behavior.
const uint8_t N = 3;
bool active[N] = {true, true, true};
uint64_t last[N] = {0, 5000, 5000};
TEST_ASSERT_EQUAL_UINT8(1, findStarvedChannel(last, active, N, 1, 1000, 5000));
}
void test_wdrr_accumulate_nan_weight_is_zeroed(void) {
const uint8_t N = 3;
float weights[N] = {0, NAN, 0.5f};
bool active[N] = {false, true, true};
float deficits[N] = {0, 2.0f, 2.0f};
wdrrAccumulate(weights, deficits, active, N, 1, 5.0f);
TEST_ASSERT_EQUAL_FLOAT(0.0f, deficits[1]); // NaN poisoned then reset
TEST_ASSERT_FLOAT_WITHIN(1e-5, 2.5f, deficits[2]);
}
void test_wdrr_accumulate_clamps_negative(void) {
const uint8_t N = 2;
float weights[N] = {0, 0.0f};
bool active[N] = {false, true};
float deficits[N] = {0, -5.5f};
wdrrAccumulate(weights, deficits, active, N, 1, 5.0f);
TEST_ASSERT_FLOAT_WITHIN(1e-5, -5.0f, deficits[1]); // clamped to -bound
}
void test_wdrr_pick_single_channel_always_wins(void) {
const uint8_t N = 4;
float deficits[N] = {0, 0.0f, 0.0f, 0.5f};
bool active[N] = {true, false, false, true}; // only ch3 schedulable
uint8_t cursor = 3;
TEST_ASSERT_EQUAL_UINT8(3, wdrrPick(deficits, active, N, 1, &cursor));
TEST_ASSERT_FLOAT_WITHIN(1e-5, -0.5f, deficits[3]);
cursor = 1; // even starting elsewhere, the wrap finds ch3
deficits[3] = 0.5f;
TEST_ASSERT_EQUAL_UINT8(3, wdrrPick(deficits, active, N, 1, &cursor));
}
void test_wdrr_pick_cursor_wraps_at_end(void) {
// cursor at the last index must wrap to startIndex on the next pick.
const uint8_t N = 4;
float deficits[N] = {0, 0.1f, 0.1f, 0.1f};
bool active[N] = {true, true, true, true};
uint8_t cursor = 3; // last index -> next starts at ch1
TEST_ASSERT_EQUAL_UINT8(1, wdrrPick(deficits, active, N, 1, &cursor));
TEST_ASSERT_EQUAL_UINT8(1, cursor);
// next pick starts one past ch1 -> ch2 (deficits ch2,ch3 still 0.1, ch1 now -0.9)
TEST_ASSERT_EQUAL_UINT8(2, wdrrPick(deficits, active, N, 1, &cursor));
}
void test_wdrr_pick_tie_goes_to_channel_after_cursor(void) {
const uint8_t N = 4;
float deficits[N] = {0, 1.0f, 1.0f, 0.0f};
bool active[N] = {true, true, true, false};
uint8_t cursor = 1; // iteration starts at ch2
TEST_ASSERT_EQUAL_UINT8(2, wdrrPick(deficits, active, N, 1, &cursor));
}
void test_wdrr_pick_all_negative_deficits_picks_max(void) {
// Validates the no-sentinel picker: all deficits negative, highest still wins.
const uint8_t N = 4;
float deficits[N] = {0, -3.0f, -1.0f, -2.0f};
bool active[N] = {true, true, true, true};
uint8_t cursor = 0;
TEST_ASSERT_EQUAL_UINT8(2, wdrrPick(deficits, active, N, 1, &cursor));
TEST_ASSERT_FLOAT_WITHIN(1e-5, -2.0f, deficits[2]);
}
void test_multiple_armed_channels_both_resolve(void) {
// Two reversed CTs armed at once (panel with several reversed clips): both must
// flip, neither starving the other.
const uint8_t N = 5;
const uint8_t START = 1;
ChannelWeightInput in[N];
bool active[N];
float deficits[N];
float weights[N];
PolarityState pol[N];
float power[N] = {0, -500.0f, -300.0f, 200.0f, 100.0f};
for (uint8_t i = 0; i < N; i++) {
in[i] = {i >= START, power[i], 0.0f, false, CHANNEL_ROLE_LOAD};
active[i] = (i >= START);
deficits[i] = 0.0f;
weights[i] = 0.0f;
pol[i] = {false, 0, 0};
}
pol[1].armed = true;
pol[2].armed = true;
uint8_t cursor = 0;
bool flipped1 = false, flipped2 = false;
for (int r = 0; r < 200; r++) {
for (uint8_t i = START; i < N; i++) {
in[i].armed = pol[i].armed;
in[i].conducting = (in[i].activePower != 0.0f); // pre-clamp conduction signal
}
computeWeights(in, N, START, WCFG, weights);
wdrrAccumulate(weights, deficits, active, N, START, 5.0f);
uint8_t pick = wdrrPick(deficits, active, N, START, &cursor);
PolarityResult pr = updatePolarity(pol[pick], in[pick].activePower, COND_A, PCFG);
if (pr.action == PolarityAction::Flip && pick == 1) flipped1 = true;
if (pr.action == PolarityAction::Flip && pick == 2) flipped2 = true;
pol[pick] = pr.state;
}
TEST_ASSERT_TRUE(flipped1);
TEST_ASSERT_TRUE(flipped2);
TEST_ASSERT_FALSE(pol[1].armed);
TEST_ASSERT_FALSE(pol[2].armed);
}
void test_full_17_channel_no_starvation(void) {
// Production array size: 17 channels (ch0 + 16 mux), all active LOAD, equal load.
// Every schedulable channel must be serviced and none may monopolise.
const uint8_t N = 17;
const uint8_t START = 1;
ChannelWeightInput in[N];
bool active[N];
float deficits[N];
float weights[N];
for (uint8_t i = 0; i < N; i++) {
in[i] = {i >= START, 100.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
active[i] = (i >= START);
deficits[i] = 0.0f;
weights[i] = 0.0f;
}
uint8_t cursor = 0;
int picks[N] = {0};
for (int r = 0; r < 1600; r++) {
computeWeights(in, N, START, WCFG, weights);
wdrrAccumulate(weights, deficits, active, N, START, 5.0f);
uint8_t p = wdrrPick(deficits, active, N, START, &cursor);
TEST_ASSERT_NOT_EQUAL(NO_CHANNEL, p);
picks[p]++;
}
// 16 equal channels over 1600 rounds -> ~100 each. Assert balanced, none starved.
for (uint8_t i = START; i < N; i++) {
TEST_ASSERT_TRUE(picks[i] > 50);
TEST_ASSERT_TRUE(picks[i] < 200);
}
}
void test_mixed_fleet_grid_battery_sampled_more(void) {
// grid + battery (priority roles) vs load + PV: priority roles sampled clearly
// more often, and nothing is starved.
const uint8_t N = 6;
const uint8_t START = 1;
ChannelWeightInput in[N];
bool active[N];
float deficits[N];
float weights[N];
ChannelRole roles[N] = {CHANNEL_ROLE_LOAD, CHANNEL_ROLE_GRID, CHANNEL_ROLE_BATTERY,
CHANNEL_ROLE_LOAD, CHANNEL_ROLE_LOAD, CHANNEL_ROLE_PV};
float power[N] = {0, 1000.0f, 500.0f, 300.0f, 200.0f, 400.0f};
for (uint8_t i = 0; i < N; i++) {
in[i] = {i >= START, power[i], 0.0f, false, roles[i]};
active[i] = (i >= START);
deficits[i] = 0.0f;
weights[i] = 0.0f;
}
uint8_t cursor = 0;
int picks[N] = {0};
for (int r = 0; r < 2000; r++) {
computeWeights(in, N, START, WCFG, weights);
wdrrAccumulate(weights, deficits, active, N, START, 5.0f);
uint8_t p = wdrrPick(deficits, active, N, START, &cursor);
picks[p]++;
}
TEST_ASSERT_TRUE(picks[1] > picks[3]); // grid > load
TEST_ASSERT_TRUE(picks[2] > picks[3]); // battery > load
TEST_ASSERT_TRUE(picks[1] > picks[5]); // grid > PV
for (uint8_t i = START; i < N; i++) TEST_ASSERT_TRUE(picks[i] > 50); // none starved
}
// ============================================================================
// Current-gate integration (reproduces the bench false-flip and proves the fix)
// ============================================================================
void test_offset_noise_never_false_flips_unclamped_role(void) {
// THE HARDWARE REGRESSION, end to end: a grid channel (unclamped role) armed with
// no real load. The ADE7953 reports a small steady offset (+1.2 W) at ~0.004 A.
// Current-gated, every such read is non-conducting: updatePolarity no-ops, the
// channel never votes and stays armed - no false "CT reversal detected".
PolarityState s{true, 0, 0};
for (int i = 0; i < 1000; i++) {
PolarityResult r = updatePolarity(s, 1.2f, 0.004f, PCFG);
s = r.state;
TEST_ASSERT_EQUAL(PolarityAction::Continue, r.action);
}
TEST_ASSERT_TRUE(s.armed);
TEST_ASSERT_EQUAL_INT8(0, s.voteCount);
TEST_ASSERT_EQUAL_UINT16(0, s.conductingReads);
// A genuine reversed load finally flows (-2000 W at 8.7 A): now it must flip.
PolarityAction action = PolarityAction::Continue;
for (int i = 0; i < 10; i++) {
PolarityResult r = updatePolarity(s, -2000.0f, 8.7f, PCFG);
s = r.state;
action = r.action;
if (action != PolarityAction::Continue) break;
}
TEST_ASSERT_EQUAL(PolarityAction::Flip, action);
}
void test_offset_noise_earns_no_boost(void) {
// The other half of the regression: offset noise must not earn the armed boost,
// or the noisy channel hogs the scheduler. The caller derives `conducting` from
// isConductingReading, so an armed grid channel on offset noise gets only its
// normal (role-multiplied) weight - no boost.
bool conducting = isConductingReading(1.2f, 0.004f, MIN_A);
TEST_ASSERT_FALSE(conducting);
ChannelWeightInput in{true, 1.2f, 0.0f, true /*armed*/, CHANNEL_ROLE_GRID, conducting};
// base 0.1 * rolePriorityMult 2.0 = 0.2; NO armed boost.
TEST_ASSERT_FLOAT_WITHIN(1e-5, WCFG.minBase * WCFG.rolePriorityMult,
computeChannelWeight(in, 0.0f, 0.0f, WCFG));
}
void test_low_current_noise_does_not_burn_the_bound(void) {
// A noisy unclamped channel that oscillates sign at ~0 current must NOT burn its
// conducting-read bound: it stays armed indefinitely (waiting for real load),
// exactly like an idle channel, rather than disarming on noise.
PolarityState s{true, 0, 0};
for (int i = 0; i < 500; i++) {
float p = (i % 2 == 0) ? -2.0f : 2.0f; // offset noise, both signs
s = updatePolarity(s, p, 0.004f, PCFG).state;
}
TEST_ASSERT_TRUE(s.armed);
TEST_ASSERT_EQUAL_UINT16(0, s.conductingReads);
}
// ============================================================================
// Small-load conducting gate (#180: MINIMUM_CURRENT_RATIO_CONDUCTING)
// ============================================================================
void test_small_reversed_load_flips_with_conducting_gate(void) {
// THE REGRESSION from #180: a ~2.5 W reversed load on a 30 A CT sits below
// MINIMUM_CURRENT_RATIO_VALIDATION (~7 W) but above MINIMUM_CURRENT_RATIO_CONDUCTING
// (~2 W). With the old PCFG it was non-conducting and read a constant 0 forever.
// With PCFG_SMALL (the firmware's new cfg) it must vote and flip within threshold reads.
PolarityState s{true, 0, 0};
// SMALL_LOAD_A (0.009) > COND_MIN_A (0.006) but < MIN_A (0.02)
PolarityAction action = PolarityAction::Continue;
for (int i = 0; i < 10; i++) {
PolarityResult r = updatePolarity(s, -2.5f, SMALL_LOAD_A, PCFG_SMALL);
s = r.state;
action = r.action;
if (action != PolarityAction::Continue) break;
}
TEST_ASSERT_EQUAL(PolarityAction::Flip, action);
// Same load is non-conducting (and therefore never votes) with the old strict gate.
PolarityState s2{true, 0, 0};
for (int i = 0; i < 100; i++) {
PolarityResult r = updatePolarity(s2, -2.5f, SMALL_LOAD_A, PCFG);
s2 = r.state;
TEST_ASSERT_EQUAL(PolarityAction::Continue, r.action);
}
TEST_ASSERT_TRUE(s2.armed);
TEST_ASSERT_EQUAL_INT8(0, s2.voteCount);
}
void test_noise_below_conducting_gate_does_not_vote(void) {
// Offset noise at NOISE_A (0.004 A) is below both gates. With PCFG_SMALL it must
// still produce no votes (the lower gate still excludes it).
PolarityState s{true, 0, 0};
for (int i = 0; i < 1000; i++) {
PolarityResult r = updatePolarity(s, 1.2f, NOISE_A, PCFG_SMALL);
s = r.state;
TEST_ASSERT_EQUAL(PolarityAction::Continue, r.action);
}
TEST_ASSERT_TRUE(s.armed);
TEST_ASSERT_EQUAL_INT8(0, s.voteCount);
TEST_ASSERT_EQUAL_UINT16(0, s.conductingReads);
}
void test_small_reversed_load_earns_armed_boost(void) {
// isConductingReading with the new lower gate must return true for SMALL_LOAD_A,
// so the WDRR armed boost fires and the channel resolves quickly.
bool conducting = isConductingReading(-2.5f, SMALL_LOAD_A, COND_MIN_A);
TEST_ASSERT_TRUE(conducting);
// The same reading is non-conducting under the old strict gate.
bool notConductingOld = isConductingReading(-2.5f, SMALL_LOAD_A, MIN_A);
TEST_ASSERT_FALSE(notConductingOld);
// Verify the boost is awarded when conducting=true under the new gate.
ChannelWeightInput in{true, 0.0f /*clamped*/, 0.0f, true /*armed*/, CHANNEL_ROLE_LOAD, conducting};
float w = computeChannelWeight(in, 0.0f, 0.0f, WCFG);
TEST_ASSERT_FLOAT_WITHIN(1e-5f, WCFG.minBase + WCFG.armedBoost, w);
}
// ============================================================================
// Weight normalisation (regression: constant inter-sample interval on
// high-channel-count devices)
// ============================================================================
//
// Un-normalised, the total weight added per round was 0.4 + 0.4 + 0.1*N while
// only one channel is serviced (-1.0) per round. For N >= 3 the system was
// oversubscribed, deficits saturated at the clamp, and once every channel
// re-clamped within one rotation (w_i * N >= 1, guaranteed for N >= 10 since
// w_i >= 0.1) every pick became an all-way tie at the bound. The cursor
// tie-break then degenerated to pure sequential round-robin: weights stopped
// mattering and every channel was serviced exactly every N rounds regardless
// of load. Observed on prod device 8856a682ff6c: 14 active channels, p50
// elapsed 5604 ms == 14 slots * 2 linecyc * 200 ms for every channel.
// computeWeights now normalises to sum 1 (demand == supply), so these sims
// must show weight differentiation at ANY channel count.
static void _runSaturationSim(uint8_t n, int rounds, int* picks, int* lastGap,
bool* gapsConstant) {
ChannelWeightInput in[32];
bool active[32];
float deficits[32];
float weights[32];
int lastPickRound[32];
for (uint8_t i = 0; i < n; i++) {
in[i] = {i >= 1, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD, false};
active[i] = (i >= 1);
deficits[i] = 0.0f;
weights[i] = 0.0f;
picks[i] = 0;
lastGap[i] = -1;
lastPickRound[i] = -1;
}
// ch1 is the hog: it carries ALL the power and ALL the variability, so it
// holds the maximum possible non-boosted weight (0.4 + 0.4 + 0.1 = 0.9)
// while every other channel sits at minBase (0.1). A 9x weight ratio.
in[1].activePower = 3000.0f;
in[1].variability = 500.0f;
in[1].conducting = true;
*gapsConstant = true;
uint8_t cursor = 0;
const int WARMUP = 200; // let deficits reach steady state first
for (int r = 0; r < WARMUP + rounds; r++) {
computeWeights(in, n, 1, WCFG, weights);
wdrrAccumulate(weights, deficits, active, n, 1, 5.0f);
uint8_t pick = wdrrPick(deficits, active, n, 1, &cursor);
TEST_ASSERT_NOT_EQUAL(NO_CHANNEL, pick);
if (r < WARMUP) continue;
picks[pick]++;
if (lastPickRound[pick] >= 0) {
int gap = r - lastPickRound[pick];
if (lastGap[pick] >= 0 && gap != lastGap[pick]) *gapsConstant = false;
lastGap[pick] = gap;
}
lastPickRound[pick] = r;
}
}
void test_fleet_keeps_weight_differentiation_across_all_channel_counts(void) {
// Sweep every schedulable fleet size from 2 up to 16 (the hardware max:
// MAX_CHANNEL_COUNT=17 total incl. ch0). The old bug only appeared once
// per-round demand (0.8 + 0.1*N) started to exceed the supply of 1.0/round
// AND every idle channel could re-clamp within one rotation - i.e. roughly
// N >= 10. This sweep proves the fix holds BELOW that threshold too (where
// the bug never showed) and ABOVE it (where it did, including the 14-channel
// prod-device case), with one test instead of picking two magic sizes.
for (uint8_t schedulable = 2; schedulable <= 16; schedulable++) {
const uint8_t N = schedulable + 1; // + ch0
const int ROUNDS = schedulable * 60;
int picks[32], lastGap[32];
bool gapsConstant;
_runSaturationSim(N, ROUNDS, picks, lastGap, &gapsConstant);
// The old bug made every gap identical and every count equal. At every
// fleet size the hog (9x raw weight) must now be serviced meaningfully
// more than an idle channel, and nobody may starve (picks == 0).
for (uint8_t i = 2; i < N; i++) {
char msg[96];
snprintf(msg, sizeof(msg), "schedulable=%u channel=%u hog=%d idle=%d",
schedulable, i, picks[1], picks[i]);
TEST_ASSERT_TRUE_MESSAGE(picks[1] >= 2 * picks[i], msg);
TEST_ASSERT_TRUE_MESSAGE(picks[i] > 0, msg);
}
}
}
// ============================================================================
// computeWeights normalisation (isolated)
// ============================================================================
static float _sumWeights(const float* w, uint8_t count, uint8_t startIndex) {
float s = 0.0f;
for (uint8_t i = startIndex; i < count; i++) s += w[i];
return s;
}
void test_computeWeights_sums_to_one_uniform_fleet(void) {
// 14 idle LOAD channels: each ends up at exactly 1/14.
const uint8_t N = 15;
ChannelWeightInput in[N];
in[0] = {true, 0.0f, 0.0f, false, CHANNEL_ROLE_GRID};
for (uint8_t i = 1; i < N; i++) in[i] = {true, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
float w[N] = {};
computeWeights(in, N, 1, WCFG, w);
TEST_ASSERT_FLOAT_WITHIN(1e-4f, 1.0f, _sumWeights(w, N, 1));
for (uint8_t i = 1; i < N; i++) TEST_ASSERT_FLOAT_WITHIN(1e-5f, 1.0f / 14.0f, w[i]);
}
void test_computeWeights_sums_to_one_mixed_fleet(void) {
// Mixed shapes: hog + battery (2x mult) + armed-conducting (boost) + idles.
// Whatever the raw pile-up, the output must always sum to 1.
const uint8_t N = 10;
ChannelWeightInput in[N];
in[0] = {true, 5000.0f, 0.0f, false, CHANNEL_ROLE_GRID};
in[1] = {true, 3000.0f, 500.0f, false, CHANNEL_ROLE_LOAD}; // hog
in[2] = {true, 200.0f, 10.0f, false, CHANNEL_ROLE_BATTERY}; // 2x mult
in[3] = {true, -50.0f, 5.0f, true, CHANNEL_ROLE_LOAD, true}; // armed boost
in[4] = {true, NAN, NAN, false, CHANNEL_ROLE_LOAD}; // bad reading
in[5] = {false, 999.0f, 99.0f, false, CHANNEL_ROLE_LOAD}; // inactive
for (uint8_t i = 6; i < N; i++) in[i] = {true, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
float w[N] = {};
computeWeights(in, N, 1, WCFG, w);
TEST_ASSERT_FLOAT_WITHIN(1e-4f, 1.0f, _sumWeights(w, N, 1));
TEST_ASSERT_EQUAL_FLOAT(0.0f, w[0]); // ch0 stays out of the rotation
TEST_ASSERT_EQUAL_FLOAT(0.0f, w[5]); // inactive stays zero
}
void test_computeWeights_normalisation_preserves_ratios(void) {
// The battery multiplier ratio must survive normalisation exactly.
const uint8_t N = 4;
ChannelWeightInput in[N];
in[0] = {false, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
in[1] = {true, 100.0f, 0.0f, false, CHANNEL_ROLE_BATTERY};
in[2] = {true, 100.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
in[3] = {true, 100.0f, 0.0f, false, CHANNEL_ROLE_LOAD};
float w[N] = {};
computeWeights(in, N, 1, WCFG, w);
TEST_ASSERT_FLOAT_WITHIN(1e-4f, 1.0f, _sumWeights(w, N, 1));
TEST_ASSERT_FLOAT_WITHIN(1e-5f, w[2] * WCFG.rolePriorityMult, w[1]);
TEST_ASSERT_FLOAT_WITHIN(1e-6f, w[2], w[3]);
}
void test_wdrr_no_deficit_saturation_with_normalised_weights(void) {
// Demand == supply: with normalised weights the deficits must stay well
// inside the clamp bound forever (the clamp becomes a dead safety net),
// at every fleet size from 2 to 16 schedulable channels, not just one.
for (uint8_t schedulable = 2; schedulable <= 16; schedulable++) {
const uint8_t N = schedulable + 1;
ChannelWeightInput in[17];
bool active[17];
for (uint8_t i = 0; i < N; i++) {
in[i] = {i >= 1, 0.0f, 0.0f, false, CHANNEL_ROLE_LOAD, false};
active[i] = (i >= 1);
}
in[1].activePower = 3000.0f;
in[1].variability = 500.0f;
in[1].conducting = true;
float weights[17] = {};
float deficits[17] = {};
uint8_t cursor = 0;
for (int r = 0; r < 1000; r++) {
computeWeights(in, N, 1, WCFG, weights);
wdrrAccumulate(weights, deficits, active, N, 1, 5.0f);
uint8_t pick = wdrrPick(deficits, active, N, 1, &cursor);
TEST_ASSERT_NOT_EQUAL(NO_CHANNEL, pick);
for (uint8_t i = 1; i < N; i++) {
char msg[64];
snprintf(msg, sizeof(msg), "schedulable=%u channel=%u round=%d", schedulable, i, r);
TEST_ASSERT_TRUE_MESSAGE(deficits[i] < 4.9f, msg); // never reaches the clamp
TEST_ASSERT_TRUE_MESSAGE(deficits[i] > -4.9f, msg);
}
}
}
}
// ============================================================================
// apparentWitnessDiverges (RMS witness)
// ============================================================================
// Divergence = |sEnergy - sRms| / sRms; discard when > maxDivergence.
static const float WITNESS_TOL = 0.5f; // mirrors APPARENT_WITNESS_MAX_DIVERGENCE
static const float WITNESS_FLOOR = 20.0f; // near-zero VA floor (below this, ratio is meaningless)
void test_witness_glitch_energy_zero_current_flows_discards(void) {
// Energy path glitched to ~0 while RMS says ~3900 VA -> discard.
TEST_ASSERT_TRUE(apparentWitnessDiverges(0.0f, 3900.0f, WITNESS_TOL, WITNESS_FLOOR));
}
void test_witness_partial_energy_discards(void) {
// Energy ~40% of RMS (partial window) -> 60% divergence -> discard.
TEST_ASSERT_TRUE(apparentWitnessDiverges(1560.0f, 3900.0f, WITNESS_TOL, WITNESS_FLOOR));
}
void test_witness_steady_agreement_keeps(void) {
// Within a few percent -> keep.
TEST_ASSERT_FALSE(apparentWitnessDiverges(3850.0f, 3900.0f, WITNESS_TOL, WITNESS_FLOOR));
}
void test_witness_low_power_factor_does_not_trip(void) {
// Apparent-vs-apparent: both are S (not P), so a low-PF load (large reactive) still
// has S_energy == S_rms. Power factor is irrelevant to the witness.
TEST_ASSERT_FALSE(apparentWitnessDiverges(2000.0f, 2000.0f, WITNESS_TOL, WITNESS_FLOOR));
}
void test_witness_genuine_transient_moves_together_keeps(void) {
// A real load change moves BOTH witnesses together, so they still agree -> keep.
TEST_ASSERT_FALSE(apparentWitnessDiverges(900.0f, 950.0f, WITNESS_TOL, WITNESS_FLOOR));
}
void test_witness_near_zero_current_no_false_trip(void) {
// Idle channel: sRms below the floor -> never rejects (ratio is meaningless there).
TEST_ASSERT_FALSE(apparentWitnessDiverges(0.0f, 5.0f, WITNESS_TOL, WITNESS_FLOOR));
}
void test_witness_nan_never_rejects(void) {
TEST_ASSERT_FALSE(apparentWitnessDiverges(NAN, 3900.0f, WITNESS_TOL, WITNESS_FLOOR));
TEST_ASSERT_FALSE(apparentWitnessDiverges(3900.0f, NAN, WITNESS_TOL, WITNESS_FLOOR));
}
// ============================================================================
// hasUnhandledIrqBits
// ============================================================================
// handledMask covers bits 0, 1, 3 (0b1011) for all cases below - arbitrary bit
// positions, chosen only so "recognized" vs "unrecognized" is unambiguous.
static const int32_t IRQ_HANDLED_MASK = 0b1011;
void test_unhandled_irq_all_recognized_bits_is_false(void) {
// Bits 0 and 3 both recognized; nothing outside the handled set.
TEST_ASSERT_FALSE(hasUnhandledIrqBits(0b1001, IRQ_HANDLED_MASK));
}
void test_unhandled_irq_recognized_plus_unrecognized_is_true(void) {
// Bit 0 (recognized) co-pends with bit 2 (not in the handled set) - the
// exact ZXTO-next-to-ZXV scenario this predicate exists to catch.
TEST_ASSERT_TRUE(hasUnhandledIrqBits(0b0101, IRQ_HANDLED_MASK));
}
void test_unhandled_irq_all_unrecognized_is_true(void) {
TEST_ASSERT_TRUE(hasUnhandledIrqBits(0b0100, IRQ_HANDLED_MASK));
}
void test_unhandled_irq_zero_status_is_false(void) {
TEST_ASSERT_FALSE(hasUnhandledIrqBits(0, IRQ_HANDLED_MASK));
}
// ============================================================================
// runner
// ============================================================================
int main(int, char **) {
UNITY_BEGIN();
RUN_TEST(test_polarity_not_armed_is_noop);
RUN_TEST(test_polarity_steady_reverse_flips_at_threshold);
RUN_TEST(test_polarity_steady_forward_confirms_without_flip);
RUN_TEST(test_polarity_is_magnitude_independent);
RUN_TEST(test_polarity_idle_is_noop_and_stays_armed);
RUN_TEST(test_polarity_nan_is_noop);
RUN_TEST(test_polarity_delayed_load_still_detects);
RUN_TEST(test_polarity_oscillating_conducting_disarms_via_bound);
RUN_TEST(test_polarity_unbounded_oscillation_never_disarms);
RUN_TEST(test_conducting_true_for_real_load);
RUN_TEST(test_conducting_true_for_reversed_load_with_current);
RUN_TEST(test_conducting_false_for_offset_noise);
RUN_TEST(test_conducting_false_for_zero_power_or_nan);
RUN_TEST(test_conducting_zero_threshold_degrades_to_power_only);
RUN_TEST(test_clamp_load_and_pv_negatives);
RUN_TEST(test_clamp_allows_grid_battery_inverter_negatives);
RUN_TEST(test_clamp_never_touches_positive);
RUN_TEST(test_energy_direction_splits_export_from_import);
RUN_TEST(test_energy_direction_tiny_magnitudes_keep_their_sign);
RUN_TEST(test_energy_direction_zero_and_nan_are_import);
RUN_TEST(test_role_priority_grid_battery_only);
RUN_TEST(test_weight_inactive_is_zero);
RUN_TEST(test_weight_idle_load_is_min_base);
RUN_TEST(test_weight_grid_gets_multiplier);
RUN_TEST(test_weight_armed_boost_only_when_conducting);
RUN_TEST(test_weight_shares_contribute);
RUN_TEST(test_computeWeights_zeroes_channel_zero_and_handles_nan);
RUN_TEST(test_find_starved_channel);
RUN_TEST(test_find_starved_skips_unbaselined_and_inactive);
RUN_TEST(test_find_starved_none);
RUN_TEST(test_wdrr_accumulate_gains_zeroes_and_clamps);
RUN_TEST(test_wdrr_pick_highest_deficit);
RUN_TEST(test_wdrr_pick_all_inactive_returns_invalid);
RUN_TEST(test_wdrr_tie_break_rotates_no_monopoly);
RUN_TEST(test_armed_no_load_channel_does_not_starve_peers);
RUN_TEST(test_armed_conducting_channel_resolves_quickly);
RUN_TEST(test_grid_sampled_more_often_than_load);
// Additional coverage
RUN_TEST(test_polarity_nonpositive_threshold_is_noop);
RUN_TEST(test_polarity_threshold_one_decides_immediately);
RUN_TEST(test_polarity_one_below_threshold_stays_armed);
RUN_TEST(test_polarity_flip_takes_precedence_over_bound);
RUN_TEST(test_polarity_bound_disarms_without_flip);
RUN_TEST(test_polarity_vote_recovers_and_confirms);
RUN_TEST(test_polarity_vote_recovers_and_flips);
RUN_TEST(test_polarity_disarmed_mid_sequence_is_noop);
RUN_TEST(test_weight_grid_armed_conducting_all_bumps_stack);
RUN_TEST(test_weight_clamped_reversed_load_still_boosted);
RUN_TEST(test_computeWeights_battery_gets_multiplier);
RUN_TEST(test_computeWeights_tiny_channel_gets_min_base);
RUN_TEST(test_computeWeights_all_inactive_all_zeros);
RUN_TEST(test_computeWeights_variability_differentiates_equal_power);
RUN_TEST(test_find_starved_returns_lowest_index_when_multiple);
RUN_TEST(test_find_starved_exactly_at_gap_is_not_starved);
RUN_TEST(test_find_starved_now_before_lastMillis_returns_channel);
RUN_TEST(test_wdrr_accumulate_nan_weight_is_zeroed);
RUN_TEST(test_wdrr_accumulate_clamps_negative);
RUN_TEST(test_wdrr_pick_single_channel_always_wins);
RUN_TEST(test_wdrr_pick_cursor_wraps_at_end);
RUN_TEST(test_wdrr_pick_tie_goes_to_channel_after_cursor);
RUN_TEST(test_wdrr_pick_all_negative_deficits_picks_max);
RUN_TEST(test_multiple_armed_channels_both_resolve);
RUN_TEST(test_full_17_channel_no_starvation);
RUN_TEST(test_mixed_fleet_grid_battery_sampled_more);
RUN_TEST(test_offset_noise_never_false_flips_unclamped_role);
RUN_TEST(test_offset_noise_earns_no_boost);
RUN_TEST(test_low_current_noise_does_not_burn_the_bound);
RUN_TEST(test_small_reversed_load_flips_with_conducting_gate);
RUN_TEST(test_noise_below_conducting_gate_does_not_vote);
RUN_TEST(test_small_reversed_load_earns_armed_boost);
RUN_TEST(test_fleet_keeps_weight_differentiation_across_all_channel_counts);
RUN_TEST(test_computeWeights_sums_to_one_uniform_fleet);
RUN_TEST(test_computeWeights_sums_to_one_mixed_fleet);
RUN_TEST(test_computeWeights_normalisation_preserves_ratios);
RUN_TEST(test_wdrr_no_deficit_saturation_with_normalised_weights);
RUN_TEST(test_witness_glitch_energy_zero_current_flows_discards);
RUN_TEST(test_witness_partial_energy_discards);
RUN_TEST(test_witness_steady_agreement_keeps);
RUN_TEST(test_witness_low_power_factor_does_not_trip);
RUN_TEST(test_witness_genuine_transient_moves_together_keeps);
RUN_TEST(test_witness_near_zero_current_no_false_trip);
RUN_TEST(test_witness_nan_never_rejects);
RUN_TEST(test_unhandled_irq_all_recognized_bits_is_false);
RUN_TEST(test_unhandled_irq_recognized_plus_unrecognized_is_true);
RUN_TEST(test_unhandled_irq_all_unrecognized_is_true);
RUN_TEST(test_unhandled_irq_zero_status_is_false);
return UNITY_END();
}