LCOV - code coverage report
Current view: top level - unit_tests/tests/sensor - test_frequency_sensor.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 23 25 92.0 %
Date: 2024-12-19 02:33:10 Functions: 7 8 87.5 %

          Line data    Source code
       1             : #include "pch.h"
       2             : 
       3             : #include "frequency_sensor.h"
       4             : #include "sensor_type.h"
       5             : 
       6             : // Fake converter just passes value straight though
       7             : struct IdentityFunction : public SensorConverter {
       8           0 :         SensorResult convert(float raw) const {
       9           0 :                 return raw;
      10             :         }
      11             : };
      12             : 
      13             : static IdentityFunction identityFunc;
      14             : 
      15             : class FrequencySensorTest : public ::testing::Test {
      16             : public:
      17           1 :         FrequencySensorTest()
      18           1 :                 : dut(SensorType::FuelEthanolPercent, MS2NT(50))
      19             :         {
      20           1 :         }
      21             : 
      22           1 :         void SetUp() override {
      23             :                 // If somehow prodcode will be unwrapped for test it MAYBE! will fire with error.
      24             :                 // At least we must init FlexSensor somehow
      25           1 :                 dut.initIfValid(Gpio::A0, identityFunc, 0.1f);
      26           1 :         }
      27             : 
      28             :         /*
      29             :          *  This method must simulate some amount periods of square-wave
      30             :          *  and fire callback on every falling edge.
      31             :          *  (as Sensor works by falling edge)
      32             :          */
      33           1 :         void generatePwm(EngineTestHelper &eth, float freqHz) {
      34           1 :                 constexpr auto periods = 1000;
      35           1 :                 auto period = (1 / freqHz);
      36             : 
      37           1 :                 std::cout << "PERIOD: " << period << std::endl;
      38             : 
      39        1001 :                 for (auto i = 0; i < periods; i++) {
      40        1000 :                         eth.moveTimeForwardSec(period);
      41        1000 :                         dut.onEdge(getTimeNowNt());
      42             :                 }
      43           1 :         }
      44             : 
      45             :         FrequencySensor dut;
      46             : };
      47             : 
      48             : /*
      49             :  *  Sensor must take PWM input on "valid" frequency and generate any input.
      50             :  */
      51           4 : TEST_F(FrequencySensorTest, testValidWithPwm) {
      52           1 :         ASSERT_TRUE(dut.Register());
      53           2 :         EngineTestHelper eth(engine_type_e::TEST_ENGINE);
      54             : 
      55             :         // Should be invalid - not set yet
      56             :         {
      57           1 :                 auto s = Sensor::get(SensorType::FuelEthanolPercent);
      58           1 :                 EXPECT_FALSE(s.Valid);
      59             :         }
      60             : 
      61           1 :         generatePwm(eth, 10);
      62             : 
      63             :         // Should be valid, correct frequency
      64             :         {
      65           1 :                 auto s = Sensor::get(SensorType::FuelEthanolPercent);
      66           1 :                 EXPECT_TRUE(s.Valid);
      67           1 :                 EXPECT_NEAR(s.Value, 10, 1e-3);
      68             :         }
      69             : }

Generated by: LCOV version 1.14