diff options
Diffstat (limited to 'src/tangara/test/battery/test_battery.cpp')
| -rw-r--r-- | src/tangara/test/battery/test_battery.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/tangara/test/battery/test_battery.cpp b/src/tangara/test/battery/test_battery.cpp new file mode 100644 index 00000000..7b55bd59 --- /dev/null +++ b/src/tangara/test/battery/test_battery.cpp @@ -0,0 +1,67 @@ +/* + * Copyright 2024 jacqueline <me@jacqueline.id.au> + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include "battery/battery.hpp" + +#include <cstdint> +#include <memory> + +#include "catch2/catch.hpp" +#include "drivers/adc.hpp" +#include "i2c_fixture.hpp" + +namespace battery { + +class FakeAdc : public drivers::AdcBattery { + private: + uint32_t mv_; + + public: + virtual auto Millivolts() -> uint32_t override { return mv_; } + auto Millivolts(uint32_t mv) -> void { mv_ = mv; } +}; + +TEST_CASE("battery charge state", "[unit]") { + I2CFixture i2c; + + // FIXME: mock the SAMD21 as well. + std::unique_ptr<drivers::Samd> samd{drivers::Samd::Create()}; + FakeAdc* adc = new FakeAdc{}; // Freed by Battery. + Battery battery{*samd, std::unique_ptr<drivers::AdcBattery>{adc}}; + + SECTION("full charge is 100%") { + // NOTE: in practice, our curve-fitting slightly undershoots + adc->Millivolts(4210); + + battery.Update(); + + auto state = battery.State(); + REQUIRE(state.has_value()); + REQUIRE(state->percent == 100); + } + + SECTION("empty charge is 0%") { + adc->Millivolts(3000); + + battery.Update(); + + auto state = battery.State(); + REQUIRE(state.has_value()); + REQUIRE(state->percent == 0); + } + + SECTION("overcharge is clamped to 100%") { + adc->Millivolts(5000); + + battery.Update(); + + auto state = battery.State(); + REQUIRE(state.has_value()); + REQUIRE(state->percent == 100); + } +} + +} // namespace battery |
