From 37041b810fbd10aab0834a33ae1dbd9edbb8bcb9 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 9 Nov 2022 16:28:19 +1100 Subject: [PATCH] Add so many tests i am going to die --- src/drivers/test/CMakeLists.txt | 4 +- src/drivers/test/i2c_fixture.hpp | 10 +++ src/drivers/test/spi_fixture.hpp | 10 +++ src/drivers/test/test_battery.cpp | 18 ++++++ src/drivers/test/test_dac.cpp | 21 +++++++ src/drivers/test/test_gpio_expander.cpp | 53 ++++++++++++++++ src/drivers/test/test_storage.cpp | 81 ++++++++++++++++--------- 7 files changed, 167 insertions(+), 30 deletions(-) create mode 100644 src/drivers/test/i2c_fixture.hpp create mode 100644 src/drivers/test/spi_fixture.hpp create mode 100644 src/drivers/test/test_battery.cpp create mode 100644 src/drivers/test/test_dac.cpp create mode 100644 src/drivers/test/test_gpio_expander.cpp diff --git a/src/drivers/test/CMakeLists.txt b/src/drivers/test/CMakeLists.txt index 83904339..78e57a1f 100644 --- a/src/drivers/test/CMakeLists.txt +++ b/src/drivers/test/CMakeLists.txt @@ -1 +1,3 @@ -idf_component_register(SRCS "test_storage.cpp" INCLUDE_DIRS "." REQUIRES catch2 cmock drivers) +idf_component_register( + SRCS "test_storage.cpp" "test_gpio_expander.cpp" "test_battery.cpp" "test_dac.cpp" + INCLUDE_DIRS "." REQUIRES catch2 cmock drivers) diff --git a/src/drivers/test/i2c_fixture.hpp b/src/drivers/test/i2c_fixture.hpp new file mode 100644 index 00000000..54794591 --- /dev/null +++ b/src/drivers/test/i2c_fixture.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include "catch2/catch.hpp" +#include "i2c.hpp" + +class I2CFixture { + public: + I2CFixture() { REQUIRE(drivers::init_i2c() == ESP_OK); } + ~I2CFixture() { drivers::deinit_i2c(); } +}; diff --git a/src/drivers/test/spi_fixture.hpp b/src/drivers/test/spi_fixture.hpp new file mode 100644 index 00000000..c2db484c --- /dev/null +++ b/src/drivers/test/spi_fixture.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include "catch2/catch.hpp" +#include "spi.hpp" + +class SpiFixture { + public: + SpiFixture() { REQUIRE(drivers::init_spi() == ESP_OK); } + ~SpiFixture() { drivers::deinit_spi(); } +}; diff --git a/src/drivers/test/test_battery.cpp b/src/drivers/test/test_battery.cpp new file mode 100644 index 00000000..a1499e40 --- /dev/null +++ b/src/drivers/test/test_battery.cpp @@ -0,0 +1,18 @@ +#include +#include "battery.hpp" + +#include "catch2/catch.hpp" + +namespace drivers { + +TEST_CASE("battery measurement", "[integration]") { + REQUIRE(drivers::init_adc() == ESP_OK); + + SECTION("voltage is within range") { + uint32_t voltage = read_battery_voltage(); + REQUIRE(voltage <= 2200); // Plugged in, no battery. + REQUIRE(voltage >= 1000); + } +} + +} // namespace drivers diff --git a/src/drivers/test/test_dac.cpp b/src/drivers/test/test_dac.cpp new file mode 100644 index 00000000..aa7a521c --- /dev/null +++ b/src/drivers/test/test_dac.cpp @@ -0,0 +1,21 @@ +#include +#include "dac.hpp" +#include "gpio-expander.hpp" +#include "i2c.hpp" +#include "i2c_fixture.hpp" + +#include "catch2/catch.hpp" + +namespace drivers { + +TEST_CASE("dac configuration", "[integration]") { + I2CFixture i2c; + GpioExpander expander; + std::unique_ptr dac = AudioDac::create(&expander).value(); + + auto power_state = dac->ReadPowerState(); + + REQUIRE(power_state.first == true); // booted +} + +} // namespace drivers diff --git a/src/drivers/test/test_gpio_expander.cpp b/src/drivers/test/test_gpio_expander.cpp new file mode 100644 index 00000000..901953d0 --- /dev/null +++ b/src/drivers/test/test_gpio_expander.cpp @@ -0,0 +1,53 @@ +#include "gpio-expander.hpp" +#include "i2c.hpp" +#include "i2c_fixture.hpp" + +#include "catch2/catch.hpp" + +namespace drivers { + +TEST_CASE("gpio expander", "[integration]") { + I2CFixture i2c; + GpioExpander expander; + SECTION("with() writes when ") { + // Initial value. + expander.Read(); + REQUIRE(expander.get_input(GpioExpander::GPIO_1) == true); + + expander.with( + [&](auto& gpio) { gpio.set_pin(GpioExpander::GPIO_1, false); }); + + expander.Read(); + REQUIRE(expander.get_input(GpioExpander::GPIO_1) == false); + } + + SECTION("setting individual pins") { + expander.set_pin(GpioExpander::GPIO_1, true); + expander.set_pin(GpioExpander::GPIO_2, false); + expander.set_pin(GpioExpander::GPIO_3, false); + expander.set_pin(GpioExpander::GPIO_4, true); + + expander.Write(); + expander.Read(); + + REQUIRE(expander.get_input(GpioExpander::GPIO_1) == true); + REQUIRE(expander.get_input(GpioExpander::GPIO_2) == false); + REQUIRE(expander.get_input(GpioExpander::GPIO_3) == false); + REQUIRE(expander.get_input(GpioExpander::GPIO_4) == true); + + expander.set_pin(GpioExpander::GPIO_1, false); + expander.set_pin(GpioExpander::GPIO_2, true); + expander.set_pin(GpioExpander::GPIO_3, true); + expander.set_pin(GpioExpander::GPIO_4, false); + + expander.Write(); + expander.Read(); + + REQUIRE(expander.get_input(GpioExpander::GPIO_1) == false); + REQUIRE(expander.get_input(GpioExpander::GPIO_2) == true); + REQUIRE(expander.get_input(GpioExpander::GPIO_3) == true); + REQUIRE(expander.get_input(GpioExpander::GPIO_4) == false); + } +} + +} // namespace drivers diff --git a/src/drivers/test/test_storage.cpp b/src/drivers/test/test_storage.cpp index 178ec815..a767a52e 100644 --- a/src/drivers/test/test_storage.cpp +++ b/src/drivers/test/test_storage.cpp @@ -4,9 +4,11 @@ #include #include "gpio-expander.hpp" -#include "storage.hpp" #include "i2c.hpp" +#include "i2c_fixture.hpp" #include "spi.hpp" +#include "spi_fixture.hpp" +#include "storage.hpp" #include "catch2/catch.hpp" @@ -17,58 +19,79 @@ static const std::string kTestFilePath = std::string(kStoragePath) + "/" + kTestFilename; TEST_CASE("sd card storage", "[integration]") { - REQUIRE( drivers::init_i2c() == ESP_OK ); - REQUIRE( drivers::init_spi() == ESP_OK ); + I2CFixture i2c; + SpiFixture spi; GpioExpander expander; { std::unique_ptr result = SdStorage::create(&expander).value(); SECTION("write to a file") { - { - std::ofstream test_file; - test_file.open(kTestFilePath.c_str()); - test_file << "hello here is some test"; - test_file.close(); + std::ofstream test_file; + test_file.open(kTestFilePath.c_str()); + test_file << "hello here is some test"; + test_file.close(); } SECTION("read from a file") { - std::ifstream test_file; - test_file.open(kTestFilePath.c_str()); + std::ifstream test_file; + test_file.open(kTestFilePath.c_str()); - std::string line; - REQUIRE(std::getline(test_file, line)); - REQUIRE(line == "hello here is some test"); + std::string line; + REQUIRE(std::getline(test_file, line)); + REQUIRE(line == "hello here is some test"); - test_file.close(); + test_file.close(); } SECTION("list files") { - DIR* dir; - struct dirent* ent; + DIR* dir; + struct dirent* ent; - dir = opendir(kStoragePath); - REQUIRE(dir != nullptr); + dir = opendir(kStoragePath); + REQUIRE(dir != nullptr); - bool found_test_file = false; - while (ent = readdir(dir)) { - if (ent->d_name == kTestFilename) { - found_test_file = true; - } - } - closedir(dir); - - REQUIRE(found_test_file); + bool found_test_file = false; + while (ent = readdir(dir)) { + if (ent->d_name == kTestFilename) { + found_test_file = true; + } + } + closedir(dir); + REQUIRE(found_test_file); } REQUIRE(remove(kTestFilePath.c_str()) == 0); } } +} + +// Failing due to hardware issue. Re-enable in R2. +TEST_CASE("sd card mux", "[integration][!mayfail]") { + I2CFixture i2c; + SpiFixture spi; + GpioExpander expander; + + SECTION("accessible when switched on") { + expander.with([&](auto& gpio) { + gpio.set_pin(GpioExpander::SD_MUX_SWITCH, GpioExpander::SD_MUX_ESP); + }); + + auto result = SdStorage::create(&expander); + REQUIRE(result.has_value()); + } + + SECTION("inaccessible when switched off") { + expander.with([&](auto& gpio) { + gpio.set_pin(GpioExpander::SD_MUX_SWITCH, GpioExpander::SD_MUX_USB); + }); - REQUIRE( drivers::deinit_i2c() == ESP_OK ); - REQUIRE( drivers::deinit_spi() == ESP_OK ); + auto result = SdStorage::create(&expander); + REQUIRE(result.has_error()); + REQUIRE(result.error() == SdStorage::FAILED_TO_READ); + } } } // namespace drivers