From 525ed2ae1bc710e2a80de0fc10300da83d594ccb Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 25 Jun 2024 16:09:36 +1000 Subject: Add a basic overview of writing and running tests --- src/tangara/test/battery/test_battery.cpp | 67 +++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/tangara/test/battery/test_battery.cpp (limited to 'src/tangara/test/battery/test_battery.cpp') 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 + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include "battery/battery.hpp" + +#include +#include + +#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 samd{drivers::Samd::Create()}; + FakeAdc* adc = new FakeAdc{}; // Freed by Battery. + Battery battery{*samd, std::unique_ptr{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 -- cgit v1.2.3