#include "../energy_day_history.h" #include #include #include #include #include static_assert(std::is_trivially_copyable::value, "ESPHome restore_value requires trivially copyable state"); #define CHECK(condition) \ do { \ if (!(condition)) { \ std::cerr << __FILE__ << ':' << __LINE__ << ": " #condition "\n"; \ std::exit(EXIT_FAILURE); \ } \ } while (false) static bool near(float actual, float expected) { return std::fabs(actual - expected) < 0.0001f; } static void first_zero_and_ordinary() { EnergyDayHistory h; CHECK(h.day() == 0); for (int c = 0; c < 3; ++c) { for (int hour = 0; hour < 24; ++hour) { CHECK(!h.has_data(c, hour)); CHECK(h.value(c, hour) == 0.0f); CHECK(h.coverage_seconds(c, hour) == 0); } h.observe(c, 1000.0f, 20261113, 8, 100, 1000); CHECK(h.day() == 20261113); CHECK(!h.has_data(c, 8)); // Lifetime totals are never bars. CHECK(h.coverage_seconds(c, 8) == 0); h.observe(c, 1000.0f, 20261113, 8, 110, 1010); CHECK(h.has_data(c, 8)); CHECK(h.value(c, 8) == 0.0f); CHECK(h.coverage_seconds(c, 8) == 10); h.observe(c, 1000.25f, 20261113, 8, 120, 1020); h.observe(c, 1000.75f, 20261113, 8, 130, 1030); CHECK(near(h.value(c, 8), 0.75f)); CHECK(h.coverage_seconds(c, 8) == 30); CHECK(!h.has_data(c, 7)); CHECK(!h.has_data(c, 9)); } } static void hour_boundaries() { EnergyDayHistory h; h.observe(0, 10.0f, 20261113, 8, 3595, 1000); h.observe(0, 10.4f, 20261113, 9, 15, 1020); CHECK(near(h.value(0, 8), 0.1f)); CHECK(near(h.value(0, 9), 0.3f)); CHECK(h.coverage_seconds(0, 8) == 5); CHECK(h.coverage_seconds(0, 9) == 15); h.observe(1, 5.0f, 20261113, 8, 3590, 1000); h.observe(1, 5.2f, 20261113, 9, 0, 1010); CHECK(h.has_data(1, 8)); CHECK(near(h.value(1, 8), 0.2f)); CHECK(!h.has_data(1, 9)); // No time in hour 9 has been observed yet. CHECK(h.coverage_seconds(1, 8) == 10); CHECK(h.coverage_seconds(1, 9) == 0); h.observe(1, 5.2f, 20261113, 9, 10, 1020); CHECK(h.has_data(1, 9)); CHECK(h.value(1, 9) == 0.0f); CHECK(h.coverage_seconds(1, 9) == 10); h.observe(2, 4.0f, 20261113, 8, 3595, 1000); h.observe(2, 4.0f, 20261113, 9, 5, 1010); CHECK(h.has_data(2, 8) && h.has_data(2, 9)); CHECK(h.value(2, 8) == 0.0f && h.value(2, 9) == 0.0f); CHECK(h.coverage_seconds(2, 8) == 5 && h.coverage_seconds(2, 9) == 5); } static void midnight() { EnergyDayHistory h; h.observe(0, 10.0f, 20261231, 23, 3580, 1000); h.observe(0, 10.1f, 20261231, 23, 3590, 1010); h.observe(1, 20.0f, 20261231, 23, 3595, 1015); h.observe(2, 30.0f, 20261231, 23, 3590, 1010); h.observe(0, 10.5f, 20270101, 0, 10, 1030); CHECK(h.day() == 20270101); CHECK(h.coverage_seconds(0, 0) == 10); for (int c = 0; c < 3; ++c) CHECK(h.coverage_seconds(c, 23) == 0); CHECK(near(h.value(0, 0), 0.2f)); CHECK(!h.has_data(0, 23)); // Baselines for the other channels survive the shared histogram reset. h.observe(1, 20.6f, 20270101, 0, 15, 1035); CHECK(near(h.value(1, 0), 0.45f)); CHECK(h.coverage_seconds(1, 0) == 15); CHECK(!h.has_data(1, 23)); h.observe(2, 30.0f, 20270101, 0, 0, 1020); CHECK(!h.has_data(2, 0)); CHECK(h.coverage_seconds(2, 0) == 0); h.observe(2, 30.0f, 20270101, 0, 10, 1030); CHECK(h.has_data(2, 0)); CHECK(h.value(2, 0) == 0.0f); CHECK(h.coverage_seconds(2, 0) == 10); } static void restored_copy() { EnergyDayHistory original; original.observe(0, 100.0f, 20261113, 10, 100, 1000); original.observe(0, 100.5f, 20261113, 10, 110, 1010); EnergyDayHistory restored = original; restored.observe(0, 100.75f, 20261113, 10, 120, 1020); CHECK(near(restored.value(0, 10), 0.75f)); CHECK(near(original.value(0, 10), 0.5f)); CHECK(restored.coverage_seconds(0, 10) == 20); CHECK(original.coverage_seconds(0, 10) == 10); original.observe(1, 200.0f, 20261113, 23, 3590, 2000); restored = original; restored.observe(1, 200.4f, 20261114, 0, 10, 2020); CHECK(near(restored.value(1, 0), 0.2f)); CHECK(!restored.has_data(0, 10)); CHECK(restored.coverage_seconds(0, 10) == 0); CHECK(restored.coverage_seconds(1, 0) == 10); } static void disconnected_and_invalid_totals() { const float bad[] = {std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity(), -1.0f}; for (float total : bad) { EnergyDayHistory h; h.observe(0, 10.0f, 20261113, 8, 100, 1000); h.observe(1, 20.0f, 20261113, 8, 100, 1000); h.observe(0, total, 20261113, 8, 110, 1010); h.observe(0, 12.0f, 20261113, 8, 120, 1020); CHECK(!h.has_data(0, 8)); CHECK(h.coverage_seconds(0, 8) == 0); h.observe(0, 12.25f, 20261113, 8, 130, 1030); CHECK(near(h.value(0, 8), 0.25f)); h.observe(1, 20.5f, 20261113, 8, 130, 1030); CHECK(near(h.value(1, 8), 0.5f)); h.observe(0, total, 20261113, 8, 140, 1040); CHECK(near(h.value(0, 8), 0.25f)); // Existing data is preserved. CHECK(h.coverage_seconds(0, 8) == 10); CHECK(h.coverage_seconds(1, 8) == 30); } } static void gaps_and_counter_reset() { EnergyDayHistory h; h.observe(0, 10.0f, 20261113, 8, 3400, 1000); h.observe(0, 11.0f, 20261113, 9, 101, 1301); CHECK(!h.has_data(0, 8) && !h.has_data(0, 9)); CHECK(h.coverage_seconds(0, 8) == 0 && h.coverage_seconds(0, 9) == 0); h.observe(0, 11.25f, 20261113, 9, 111, 1311); CHECK(near(h.value(0, 9), 0.25f)); h.observe(0, 0.0f, 20261113, 9, 121, 1321); CHECK(near(h.value(0, 9), 0.25f)); CHECK(h.coverage_seconds(0, 9) == 10); h.observe(0, 0.5f, 20261113, 9, 131, 1331); CHECK(near(h.value(0, 9), 0.75f)); CHECK(h.coverage_seconds(0, 9) == 20); h.observe(1, 5.0f, 20261113, 8, 100, 1000); h.observe(1, 5.5f, 20261113, 8, 400, 1300); CHECK(near(h.value(1, 8), 0.5f)); // Exactly 300 seconds is valid. CHECK(h.coverage_seconds(1, 8) == 300); h.observe(2, 5.0f, 20261113, 23, 3590, 1000); h.observe(2, 9.0f, 20261114, 0, 291, 1301); CHECK(h.day() == 20261114); CHECK(!h.has_data(2, 0) && !h.has_data(2, 23)); CHECK(h.coverage_seconds(1, 8) == 0); CHECK(h.coverage_seconds(2, 0) == 0 && h.coverage_seconds(2, 23) == 0); } static void clock_rewind() { for (uint32_t timestamp : {1000u, 990u}) { EnergyDayHistory h; h.observe(0, 10.0f, 20261113, 8, 100, 1000); h.observe(0, 11.0f, 20261113, 8, 90, timestamp); CHECK(!h.has_data(0, 8)); CHECK(h.coverage_seconds(0, 8) == 0); h.observe(0, 11.25f, 20261113, 8, 100, timestamp + 10); CHECK(near(h.value(0, 8), 0.25f)); CHECK(h.coverage_seconds(0, 8) == 10); } EnergyDayHistory h; h.observe(0, 10.0f, 20261113, 8, 100, 1000); h.observe(0, 11.0f, 20261113, 8, 90, 1010); // Inconsistent local clock. CHECK(!h.has_data(0, 8)); CHECK(h.coverage_seconds(0, 8) == 0); h.observe(0, 11.25f, 20261113, 8, 100, 1020); CHECK(near(h.value(0, 8), 0.25f)); CHECK(h.coverage_seconds(0, 8) == 10); } static void dst() { EnergyDayHistory fallback; fallback.observe(0, 10.0f, 20261025, 2, 3580, 1000); fallback.observe(0, 10.1f, 20261025, 2, 3590, 1010); fallback.observe(0, 10.5f, 20261025, 2, 10, 1030); CHECK(near(fallback.value(0, 2), 0.5f)); CHECK(fallback.coverage_seconds(0, 2) == 30); CHECK(!fallback.has_data(0, 1) && !fallback.has_data(0, 3)); EnergyDayHistory spring; spring.observe(0, 10.0f, 20260329, 1, 3595, 1000); spring.observe(0, 10.4f, 20260329, 3, 15, 1020); CHECK(near(spring.value(0, 1), 0.1f)); CHECK(!spring.has_data(0, 2)); CHECK(near(spring.value(0, 3), 0.3f)); CHECK(spring.coverage_seconds(0, 1) == 5); CHECK(spring.coverage_seconds(0, 2) == 0); CHECK(spring.coverage_seconds(0, 3) == 15); // A fully observed repeated hour contains two actual hours, not a capped 3600s. EnergyDayHistory repeated; for (int elapsed = 0; elapsed <= 7200; elapsed += 300) { repeated.observe(0, 10.0f, 20261025, elapsed < 7200 ? 2 : 3, elapsed % 3600, 1000 + elapsed); if (elapsed == 3600) CHECK(repeated.coverage_seconds(0, 2) == 3600); } CHECK(repeated.coverage_seconds(0, 2) == 7200); CHECK(repeated.coverage_seconds(0, 3) == 0); } static void invalid_inputs() { EnergyDayHistory h; for (int c : {-1, 3, 100}) { h.observe(c, 10.0f, 20261113, 8, 100, 1000); CHECK(!h.has_data(c, 8) && h.value(c, 8) == 0.0f); CHECK(h.coverage_seconds(c, 8) == 0); } const uint32_t bad_days[] = {0, 20260001, 20261301, 20261100, 20261131, 20260229, 19000229, 100000101}; for (uint32_t day : bad_days) h.observe(0, 10.0f, day, 8, 100, 1000); for (int hour : {-1, 24, 100}) { h.observe(0, 10.0f, 20261113, hour, 100, 1000); CHECK(!h.has_data(0, hour) && h.value(0, hour) == 0.0f); CHECK(h.coverage_seconds(0, hour) == 0); } for (int seconds : {-1, 3600, 10000}) h.observe(0, 10.0f, 20261113, 8, seconds, 1000); CHECK(h.day() == 0); h.observe(0, 10.0f, 20000229, 8, 100, 1000); CHECK(h.day() == 20000229); CHECK(!h.has_data(0, 8)); // Bad clock samples must not change the established baseline or day. h.observe(0, 100.0f, 20261131, 8, 105, 1005); h.observe(0, 100.0f, 20261113, 24, 105, 1005); h.observe(0, 100.0f, 20261113, 8, -1, 1005); CHECK(h.day() == 20000229); h.observe(0, 10.25f, 20000229, 8, 110, 1010); CHECK(near(h.value(0, 8), 0.25f)); CHECK(h.coverage_seconds(0, 8) == 10); } static void explicit_invalidation() { EnergyDayHistory h; h.invalidate(0); CHECK(h.day() == 0); CHECK(h.coverage_seconds(0, 8) == 0); for (int c = 0; c < 3; ++c) { h.observe(c, 10.0f, 20261113, 8, 100, 1000); h.observe(c, 10.25f, 20261113, 8, 110, 1010); } h.invalidate(0); h.invalidate(0); // Repeated invalidations are harmless. CHECK(h.day() == 20261113); CHECK(h.has_data(0, 8)); CHECK(near(h.value(0, 8), 0.25f)); CHECK(h.coverage_seconds(0, 8) == 10); h.observe(0, 20.0f, 20261113, 8, 120, 1020); CHECK(near(h.value(0, 8), 0.25f)); // First fresh receipt only rebaselines. CHECK(h.coverage_seconds(0, 8) == 10); h.observe(0, 20.5f, 20261113, 8, 130, 1030); CHECK(near(h.value(0, 8), 0.75f)); CHECK(h.coverage_seconds(0, 8) == 20); for (int c : {-1, 3, std::numeric_limits::min(), std::numeric_limits::max()}) { h.invalidate(c); CHECK(h.coverage_seconds(c, 8) == 0); } for (int c = 1; c < 3; ++c) { h.observe(c, 10.5f, 20261113, 8, 120, 1020); CHECK(near(h.value(c, 8), 0.5f)); // Other channel baselines are untouched. CHECK(h.coverage_seconds(c, 8) == 20); } h.observe(0, 20.5f, 20261113, 8, 140, 1040); CHECK(near(h.value(0, 8), 0.75f)); CHECK(h.coverage_seconds(0, 8) == 30); } int main() { first_zero_and_ordinary(); hour_boundaries(); midnight(); restored_copy(); disconnected_and_invalid_totals(); gaps_and_counter_reset(); clock_rewind(); dst(); invalid_inputs(); explicit_invalidation(); std::cout << "All 10 energy day history test groups passed.\n"; }