LCOV - code coverage report
Current view: top level - unit_tests/tests/sensor - sensor_reader.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 33 33 100.0 %
Date: 2024-04-25 02:23:43 Functions: 10 10 100.0 %

          Line data    Source code
       1             : #include "pch.h"
       2             : 
       3             : #include "sensor_reader.h"
       4             : 
       5             : class SensorBasicReader : public ::testing::Test {
       6             : protected:
       7           2 :         void SetUp() override {
       8           2 :                 Sensor::resetRegistry();
       9           2 :         }
      10             : 
      11           2 :         void TearDown() override {
      12           2 :                 Sensor::resetRegistry();
      13           2 :         }
      14             : };
      15             : 
      16           4 : TEST_F(SensorBasicReader, Value) {
      17             :         // Create a sensor - but don't register it
      18           1 :         MockSensor dut(SensorType::Tps1);
      19             : 
      20             :         // Create a reader for the same sensor type - fallback value = 123
      21           1 :         SensorReader<SensorType::Tps1> dutReader(123.0f);
      22             : 
      23             :         // Expect not to find it
      24             :         {
      25           1 :                 auto result = dutReader.get();
      26           1 :                 EXPECT_FALSE(result.Valid);
      27             :         }
      28             : 
      29             :         // Register the sensor now
      30           1 :         EXPECT_TRUE(dut.Register());
      31             : 
      32             :         // Still expect invalid - no value has been set yet
      33             :         {
      34           1 :                 auto result = dutReader.get();
      35           1 :                 EXPECT_FALSE(result.Valid);
      36             :         }
      37             : 
      38           1 :         dut.set(47.0f);
      39             : 
      40             :         // Expect valid - with the value 47 we just set
      41             :         {
      42           1 :                 auto result = dutReader.get();
      43           1 :                 EXPECT_TRUE(result.Valid);
      44           1 :                 EXPECT_FLOAT_EQ(result.Value, 47.0f);
      45             :         }
      46           1 : }
      47             : 
      48           4 : TEST_F(SensorBasicReader, FallbackValue) {
      49             :         // Create a sensor - but don't register it
      50           1 :         MockSensor dut(SensorType::Tps1);
      51             : 
      52             :         // Create a reader for the same sensor type - fallback value = 123
      53           1 :         SensorReader<SensorType::Tps1> dutReader(123.0f);
      54             : 
      55             :         // Expect not to find it
      56             :         {
      57           1 :                 auto result = dutReader.get();
      58           1 :                 EXPECT_FALSE(result.Valid);
      59             :         }
      60             : 
      61             :         // Register the sensor now
      62           1 :         EXPECT_TRUE(dut.Register());
      63             : 
      64             :         // Still expect invalid - no value has been set yet
      65             :         {
      66           1 :                 auto result = dutReader.get();
      67           1 :                 EXPECT_FALSE(result.Valid);
      68             :         }
      69             : 
      70             :         // However - we should be able to get the fallback value
      71             :         {
      72           1 :                 auto result = dutReader.getOrDefault();
      73           1 :                 EXPECT_FLOAT_EQ(result, 123.0f);
      74             :         }
      75             : 
      76             :         // Now set the value for real, and ensure we get that instead
      77           1 :         dut.set(63);
      78             : 
      79             :         {
      80           1 :                 auto result = dutReader.getOrDefault();
      81           1 :                 EXPECT_FLOAT_EQ(result, 63.0f);
      82             :         }
      83           1 : }

Generated by: LCOV version 1.14