LCOV - code coverage report
Current view: top level - unit_tests/tests/sensor - func_chain.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 55 55 100.0 %
Date: 2024-07-27 05:50:15 Functions: 23 23 100.0 %

          Line data    Source code
       1             : #include "pch.h"
       2             : 
       3             : #include "func_chain.h"
       4             : #include "init.h"
       5             : #include "lua_hooks.h"
       6             : 
       7             : struct AddOne final : public SensorConverter {
       8           6 :     SensorResult convert(float input) const {
       9           6 :         return input + 1;
      10             :     }
      11             : };
      12             : 
      13             : struct SubOne final : public SensorConverter {
      14           2 :     SensorResult convert(float input) const {
      15           2 :         return input - 1;
      16             :     }
      17             : };
      18             : 
      19             : struct Doubler final : public SensorConverter {
      20           4 :     SensorResult convert(float input) const {
      21           4 :         return input * 2;
      22             :     }
      23             : };
      24             : 
      25           4 : TEST(FunctionChain, TestSingle)
      26             : {
      27           1 :     FuncChain<AddOne> fc;
      28             : 
      29             :     {
      30           1 :         auto r = fc.convert(5);
      31           1 :         EXPECT_TRUE(r.Valid);
      32           1 :         EXPECT_EQ(r.Value, 6);
      33             :     }
      34             : 
      35             :     {
      36           1 :         auto r = fc.convert(10);
      37           1 :         EXPECT_TRUE(r.Valid);
      38           1 :         EXPECT_EQ(r.Value, 11);
      39             :     }
      40           1 : }
      41             : 
      42           4 : TEST(FunctionChain, TestDouble)
      43             : {
      44             :     // This computes fc(x) = (x + 1) * 2
      45           1 :     FuncChain<AddOne, Doubler> fc;
      46             : 
      47             :     {
      48           1 :         auto r = fc.convert(5);
      49           1 :         EXPECT_TRUE(r.Valid);
      50           1 :         EXPECT_EQ(r.Value, 12);
      51             :     }
      52             : 
      53             :     {
      54           1 :         auto r = fc.convert(10);
      55           1 :         EXPECT_TRUE(r.Valid);
      56           1 :         EXPECT_EQ(r.Value, 22);
      57             :     }
      58           1 : }
      59             : 
      60           4 : TEST(FunctionChain, TestTriple)
      61             : {
      62             :     // This computes fc(x) = ((x + 1) * 2) - 1
      63           1 :     FuncChain<AddOne, Doubler, SubOne> fc;
      64             : 
      65             :     {
      66           1 :         auto r = fc.convert(5);
      67           1 :         EXPECT_TRUE(r.Valid);
      68           1 :         EXPECT_EQ(r.Value, 11);
      69             :     }
      70             : 
      71             :     {
      72           1 :         auto r = fc.convert(10);
      73           1 :         EXPECT_TRUE(r.Valid);
      74           1 :         EXPECT_EQ(r.Value, 21);
      75             :     }
      76           1 : }
      77             : 
      78           4 : TEST(FunctionChain, TestGet)
      79             : {
      80             :     // No logic here - the test is that it compiles
      81           1 :     FuncChain<AddOne, Doubler, SubOne> fc;
      82             : 
      83           1 :     fc.get<AddOne>();
      84           1 :     fc.get<Doubler>();
      85           1 :     fc.get<SubOne>();
      86             : 
      87           1 :     ASSERT_TRUE(fc.getPtr<AddOne>() == &fc.get<AddOne>());
      88           1 :     ASSERT_TRUE(fc.getPtr<Doubler>() == &fc.get<Doubler>());
      89           1 :     ASSERT_TRUE(fc.getPtr<SubOne>() == &fc.get<SubOne>());
      90             : }
      91             : 
      92           4 : TEST(Sensor, OverrideValue) {
      93           1 :         EngineTestHelper eth(engine_type_e::HARLEY);
      94             :         // huh? i do not get this EXPECT_FALSE(Sensor::get(SensorType::Rpm).Valid);
      95           1 :         initOverrideSensors();
      96             : 
      97           1 :         Sensor::setMockValue(SensorType::Rpm, 1000);
      98           1 :         EXPECT_TRUE(Sensor::get(SensorType::Rpm).Valid);
      99           1 :         EXPECT_TRUE(Sensor::get(SensorType::DashOverrideRpm).Valid);
     100             : 
     101           1 :         ASSERT_DOUBLE_EQ(1000, Sensor::get(SensorType::Rpm).Value);
     102           1 :         ASSERT_DOUBLE_EQ(1000, Sensor::get(SensorType::DashOverrideRpm).Value);
     103             : 
     104           1 :         LuaOverrideSensor * sensor = (LuaOverrideSensor*)Sensor::getSensorOfType(SensorType::DashOverrideRpm);
     105           1 :         sensor->setOverrideValue(3);
     106           1 :         ASSERT_DOUBLE_EQ(3, Sensor::get(SensorType::DashOverrideRpm).Value);
     107           1 :         sensor->reset();
     108           1 :         ASSERT_DOUBLE_EQ(1000, Sensor::get(SensorType::DashOverrideRpm).Value);
     109           1 : }

Generated by: LCOV version 1.14