diff options
| author | jacqueline <me@jacqueline.id.au> | 2024-06-25 16:09:36 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2024-06-25 16:09:36 +1000 |
| commit | 525ed2ae1bc710e2a80de0fc10300da83d594ccb (patch) | |
| tree | 49af557432f7e6fc86ae6025f7616137baaa91ad /src/tangara/test | |
| parent | 8db57d6dc5cf5c83cffd393a37ca37bc1a67f1af (diff) | |
| download | tangara-fw-525ed2ae1bc710e2a80de0fc10300da83d594ccb.tar.gz | |
Add a basic overview of writing and running tests
Diffstat (limited to 'src/tangara/test')
| -rw-r--r-- | src/tangara/test/CMakeLists.txt | 7 | ||||
| -rw-r--r-- | src/tangara/test/battery/test_battery.cpp | 67 |
2 files changed, 74 insertions, 0 deletions
diff --git a/src/tangara/test/CMakeLists.txt b/src/tangara/test/CMakeLists.txt new file mode 100644 index 00000000..728c06b0 --- /dev/null +++ b/src/tangara/test/CMakeLists.txt @@ -0,0 +1,7 @@ +# Copyright 2023 jacqueline <me@jacqueline.id.au> +# +# SPDX-License-Identifier: GPL-3.0-only + +idf_component_register( + SRC_DIRS "battery" + INCLUDE_DIRS "." REQUIRES catch2 cmock tangara fixtures) 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 |
