// Host tests for the battery policy (battery_policy.hpp). No ESP-IDF involved: it compiles unmodified on the PC. #include "battery_policy.hpp" #include #include using namespace microbu; static int failures = 0; #define CHECK(cond) do { if (!(cond)) { std::printf("FAIL %s:%d %s\n", __FILE__, __LINE__, #cond); ++failures; } } while (0) // Feeds the same sample n times, 5 s apart, starting at t_ms. static void feed(BatteryPolicy& m, std::uint16_t mv, int n, bool usb, std::uint32_t& t_ms) { for (int i = 0; i < n; ++i) { m.update(mv, usb, t_ms); t_ms += 5000; } } static void test_smoothing_and_validity() { BatteryPolicy m; CHECK(m.millivolts() == 0); // nothing measured yet m.update(4100, false, 0); CHECK(m.millivolts() == 4100); // first sample taken as is, no ramp from zero m.update(4000, false, 5000); CHECK(m.millivolts() == 4075); // 1/4 of the step m.update(0, false, 10000); // failed measurement CHECK(m.millivolts() == 0 && !m.low() && !m.critical() && !m.shutdown_due()); m.update(4100, false, 15000); CHECK(m.millivolts() == 4100); // restarts from the new reading m.update(5200, false, 20000); // implausible: no cell connected CHECK(m.millivolts() == 0); } static void test_warning_hysteresis() { BatteryPolicy m; std::uint32_t t = 0; feed(m, 3700, 3, false, t); CHECK(!m.low()); feed(m, 3450, 20, false, t); CHECK(m.low()); feed(m, 3550, 20, false, t); // above the warning threshold but below the clear threshold CHECK(m.low()); feed(m, 3650, 20, false, t); CHECK(!m.low()); } static void test_shutdown_rules() { { // sustained low, no USB, after the grace period BatteryPolicy m; std::uint32_t t = 120000; feed(m, 3250, 5, false, t); CHECK(m.critical() && !m.shutdown_due()); // five samples are not enough feed(m, 3250, 1, false, t); CHECK(m.shutdown_due()); } { // a phone on USB powers the board: never shut down, and the count restarts BatteryPolicy m; std::uint32_t t = 120000; feed(m, 3250, 5, false, t); feed(m, 3250, 1, true, t); CHECK(!m.shutdown_due()); feed(m, 3250, 5, false, t); CHECK(!m.shutdown_due()); feed(m, 3250, 1, false, t); CHECK(m.shutdown_due()); } { // not within the first minute after boot BatteryPolicy m; std::uint32_t t = 0; feed(m, 3250, 11, false, t); // 0..50 s CHECK(!m.shutdown_due()); feed(m, 3250, 7, false, t); // 55..85 s: six of them at or past the 60 s grace period CHECK(m.shutdown_due()); } { // a dip that recovers (TX sag) does not count BatteryPolicy m; std::uint32_t t = 120000; feed(m, 3250, 4, false, t); feed(m, 3900, 10, false, t); feed(m, 3250, 4, false, t); CHECK(!m.shutdown_due()); } { // a normal cell never trips it BatteryPolicy m; std::uint32_t t = 120000; feed(m, 3800, 100, false, t); CHECK(!m.shutdown_due() && !m.low() && !m.critical()); } { // no cell / no sensor: no shutdown BatteryPolicy m; std::uint32_t t = 120000; feed(m, 0, 100, false, t); CHECK(!m.shutdown_due()); } } static void test_resume() { BatteryPolicy m; CHECK(!m.resume_ok(3300)); CHECK(!m.resume_ok(3599)); CHECK(m.resume_ok(3600)); CHECK(m.resume_ok(4100)); CHECK(m.resume_ok(0)); // cannot judge: run } int main() { test_smoothing_and_validity(); test_warning_hysteresis(); test_shutdown_rules(); test_resume(); if (failures) { std::printf("%d check(s) failed\n", failures); return EXIT_FAILURE; } std::printf("battery: all checks passed\n"); return EXIT_SUCCESS; }