From 6f96887d74111c4faba9284432994bcc65a09a14 Mon Sep 17 00:00:00 2001 From: alxrMironov <30952168+alxrMironov@users.noreply.github.com> Date: Mon, 2 Aug 2021 05:14:01 +0300 Subject: [PATCH] Unit test for flex sensor.cpp #3091 (#3093) * Add test_flex_sensor.cpp * Add test_flex_sensor.cpp to makefile * Edit test_flex_sensor.cpp code formatting * Fix test case name in test_flex_sensor.cpp * Remove explicitly defined constructor of "FlexSensorForTests" class * Edit included headers replaced by single one in test_flex_sensor.cpp * Edit format to make similar to other files. Co-authored-by: alxrMironov <330OMcorporative> --- unit_tests/tests/sensor/test_flex_sensor.cpp | 132 +++++++++++++++++++ unit_tests/tests/tests.mk | 1 + 2 files changed, 133 insertions(+) create mode 100644 unit_tests/tests/sensor/test_flex_sensor.cpp diff --git a/unit_tests/tests/sensor/test_flex_sensor.cpp b/unit_tests/tests/sensor/test_flex_sensor.cpp new file mode 100644 index 0000000000..795093da5a --- /dev/null +++ b/unit_tests/tests/sensor/test_flex_sensor.cpp @@ -0,0 +1,132 @@ +#include "pch.h" + +#include "flex_sensor.h" + +class FlexSensorForTests : public ::testing::Test +{ + +protected: + + void SetUp () override { + // If somehow prodcode will be unwrapped for test it MAYBE! will fire with error. + // At least we must init FlexSensor somehow + dut.init (GPIO_INVALID); + + Sensor::resetRegistry (); // IDK why i must use this. Just copypaste. + } + + void TearDown () override { + Sensor::resetRegistry (); + } + + /* + * This method must simulate some amount periods of square-wave + * and fire callback on every falling edge. + * (as Sensor works by falling edge) + */ + void WithEngineHelperGenerateInputPWM (EngineTestHelper ð, float freqHz) { + auto const PERIODS_TO_SIMULATE = 50; + auto period = (1 / freqHz); + + std::cout << "PERIOD: " << period << std::endl; + for (auto i = PERIODS_TO_SIMULATE; i > 0; i--) + { + eth.moveTimeForwardSec (period); + dut.onEdge (getTimeNowNt ()); + } + } + + // todo: some function to convert output value to frequency? + float ConvertExpectedValueToFrequency (float expectedValue) { + return VALID_LOW_FREQ; + } + + const float INVALID_LOW_FREQ = 44.0f; + const float VALID_LOW_FREQ = 46.0f; + const float INVALID_HIGH_FREQ = 156.0f; + const float VALID_HIGH_FREQ = 154.0f; + const float VALID_FREQ = 100.0f; + + FlexFuelSensor dut; +}; + +/* + * Sensor must take PWM input on "valid" frequency and generate any input. + */ +TEST_F(FlexSensorForTests, testCreationAndGeneratingAnyValidResult) +{ + + ASSERT_TRUE(dut.Register ()); + WITH_ENGINE_TEST_HELPER(TEST_ENGINE); + + // Should be invalid - not set yet + { + auto s = Sensor::get (SensorType::FuelEthanolPercent); + EXPECT_FALSE(s.Valid); + } + + WithEngineHelperGenerateInputPWM (eth, VALID_FREQ); + // Should be valid + { + auto s = Sensor::get (SensorType::FuelEthanolPercent); + EXPECT_TRUE(s.Valid); + } +} + +/* + * Sensor must take PWM input on "invalid" frequency and generate no input. + */ +TEST_F(FlexSensorForTests, testInvalidFrequency) +{ + + ASSERT_TRUE(dut.Register ()); + WITH_ENGINE_TEST_HELPER(TEST_ENGINE); + + // Should be invalid - not set yet + { + auto s = Sensor::get (SensorType::FuelEthanolPercent); + EXPECT_FALSE(s.Valid); + } + + WithEngineHelperGenerateInputPWM (eth, INVALID_LOW_FREQ); + // Should be invalid - too low freq + { + auto s = Sensor::get (SensorType::FuelEthanolPercent); + EXPECT_FALSE(s.Valid); + } + + WithEngineHelperGenerateInputPWM (eth, INVALID_HIGH_FREQ); + // Should be invalid - too high freq + { + auto s = Sensor::get (SensorType::FuelEthanolPercent); + EXPECT_FALSE(s.Valid); + } +} + +/* + * Sensor must take PWM input on frequency with "known output value" + * and generate similiar output. + */ +TEST_F(FlexSensorForTests, testFrequencyConversion) +{ + + ASSERT_TRUE(dut.Register ()); + WITH_ENGINE_TEST_HELPER(TEST_ENGINE); + + // Should be invalid - not set yet + { + auto s = Sensor::get (SensorType::FuelEthanolPercent); + EXPECT_FALSE(s.Valid); + } + + float expectedValue = 75; // Any between 0 and 100 + float freq = ConvertExpectedValueToFrequency (expectedValue); + + WithEngineHelperGenerateInputPWM (eth, freq); + // Should be invalid - too low freq + { + auto s = Sensor::get (SensorType::FuelEthanolPercent); + EXPECT_TRUE(s.Valid); + EXPECT_FLOAT_EQ(s.Value, expectedValue); + } +} diff --git a/unit_tests/tests/tests.mk b/unit_tests/tests/tests.mk index ccef07c53c..cb6cfbc582 100644 --- a/unit_tests/tests/tests.mk +++ b/unit_tests/tests/tests.mk @@ -84,4 +84,5 @@ TESTS_SRC_CPP = \ tests/test_gpio.cpp \ tests/test_limp.cpp \ tests/trigger/test_all_triggers.cpp \ + tests/sensor/test_flex_sensor.cpp \