LCOV - code coverage report
Current view: top level - unit_tests/tests/sensor - test_sensor_init.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 131 131 100.0 %
Date: 2024-12-19 02:33:10 Functions: 37 37 100.0 %

          Line data    Source code
       1             : #include "pch.h"
       2             : 
       3             : #include "unit_test_framework.h"
       4             : #include "init.h"
       5             : #include "functional_sensor.h"
       6             : 
       7          22 : static void postToFuncSensor(Sensor* s, float value) {
       8          22 :         static_cast<FunctionalSensor*>(s)->postRawValue(value, getTimeNowNt());
       9          22 : }
      10             : 
      11             : #define EXPECT_POINT_VALID(s, raw, expect) \
      12             :         {\
      13             :                 postToFuncSensor(s, raw); \
      14             :                 auto res = s->get(); \
      15             :                 EXPECT_TRUE(res.Valid); \
      16             :                 EXPECT_NEAR(res.Value, expect, EPS2D); \
      17             :         }
      18             : 
      19             : #define EXPECT_POINT_INVALID(s, raw) \
      20             :         {\
      21             :                 postToFuncSensor(s, raw); \
      22             :                 auto res = s->get(); \
      23             :                 EXPECT_FALSE(res.Valid); \
      24             :         }
      25             : 
      26           4 : TEST(SensorInit, Tps) {
      27           1 :         EngineTestHelper eth(engine_type_e::TEST_ENGINE);
      28             : 
      29           1 :         engineConfiguration->tpsMin = 200;   // 1 volt
      30           1 :         engineConfiguration->tpsMax = 800;   // 4 volts
      31             : 
      32           1 :         initTps();
      33             : 
      34             :         // Ensure the sensors were registered
      35           1 :         auto s = const_cast<Sensor*>(Sensor::getSensorOfType(SensorType::Tps1Primary));
      36           1 :         ASSERT_NE(nullptr, s);
      37             : 
      38             :         // Test in range
      39           2 :         EXPECT_POINT_VALID(s, 1.0f, 0.0f);      // closed throttle
      40           2 :         EXPECT_POINT_VALID(s, 2.5f, 50.0f);     // half throttle
      41           2 :         EXPECT_POINT_VALID(s, 4.0f, 100.0f) // full throttle
      42             : 
      43             :         // Test out of range
      44           1 :         EXPECT_POINT_INVALID(s, 0.0f);
      45           1 :         EXPECT_POINT_INVALID(s, 5.0f);
      46             : 
      47             :         // Test that the passthru (redundant sensor) is working
      48           2 :         EXPECT_POINT_VALID(s, 2.5f, 50.0f);
      49           1 :         EXPECT_NEAR(50.0f, Sensor::get(SensorType::Tps1).value_or(-1), EPS2D);
      50           1 : }
      51             : 
      52           4 : TEST(SensorInit, TpsValuesTooClose) {
      53           1 :         EngineTestHelper eth(engine_type_e::TEST_ENGINE);
      54             : 
      55             :         // Should fail, 0.49 volts apart
      56           1 :         engineConfiguration->tpsMin = 200;   // 1.00 volt
      57           1 :         engineConfiguration->tpsMax = 298;   // 1.49 volts
      58           1 :         EXPECT_FATAL_ERROR(initTps());
      59           1 :         Sensor::resetRegistry();
      60             : 
      61             :         // Should fail, -0.49 volts apart
      62           1 :         engineConfiguration->tpsMin = 298;   // 1.49 volt
      63           1 :         engineConfiguration->tpsMax = 200;   // 1.00 volts
      64           1 :         EXPECT_FATAL_ERROR(initTps());
      65           1 :         Sensor::resetRegistry();
      66             : 
      67             :         // Should succeed, 0.51 volts apart
      68           1 :         engineConfiguration->tpsMin = 200;   // 1.00 volt
      69           1 :         engineConfiguration->tpsMax = 302;   // 1.51 volts
      70           1 :         EXPECT_NO_FATAL_ERROR(initTps());
      71           1 :         Sensor::resetRegistry();
      72             : 
      73             :         // Should succeed, -0.51 volts apart
      74           1 :         engineConfiguration->tpsMin = 302;   // 1.51 volt
      75           1 :         engineConfiguration->tpsMax = 200;   // 1.00 volts
      76           1 :         EXPECT_NO_FATAL_ERROR(initTps());
      77           1 :         Sensor::resetRegistry();
      78             : 
      79             :         // With no pin, it should be ok that they are the same
      80             :         // Should succeed, -0.51 volts apart
      81           1 :         engineConfiguration->tps1_1AdcChannel = EFI_ADC_NONE;
      82           1 :         engineConfiguration->tpsMin = 200;   // 1.00 volt
      83           1 :         engineConfiguration->tpsMax = 200;   // 1.00 volts
      84           1 :         EXPECT_NO_FATAL_ERROR(initTps());
      85           1 :         Sensor::resetRegistry();
      86             : 
      87             :         // Test a random bogus pin index, shouldn't fail
      88           1 :         engineConfiguration->tps1_1AdcChannel = static_cast<adc_channel_e>(EFI_ADC_ERROR);
      89           1 :         engineConfiguration->tpsMin = 200;   // 1.00 volt
      90           1 :         engineConfiguration->tpsMax = 200;   // 1.00 volt
      91           1 :         EXPECT_NO_FATAL_ERROR(initTps());
      92           1 :         Sensor::resetRegistry();
      93             : 
      94             :         // de-init and re-init should also work without error
      95           1 :         EXPECT_NO_FATAL_ERROR(deinitTps());
      96           1 :         EXPECT_NO_FATAL_ERROR(initTps());
      97           2 : }
      98             : 
      99           4 : TEST(SensorInit, Pedal) {
     100           1 :         EngineTestHelper eth(engine_type_e::TEST_ENGINE);
     101             : 
     102           1 :         engineConfiguration->throttlePedalPositionAdcChannel = EFI_ADC_0;
     103           1 :         engineConfiguration->throttlePedalUpVoltage = 1;
     104           1 :         engineConfiguration->throttlePedalWOTVoltage = 4;
     105             : 
     106           1 :         initTps();
     107             : 
     108             :         // Ensure the sensors were registered
     109           1 :         auto s = const_cast<Sensor*>(Sensor::getSensorOfType(SensorType::AcceleratorPedalPrimary));
     110           1 :         ASSERT_NE(nullptr, s);
     111             : 
     112             :         // Test in range
     113           2 :         EXPECT_POINT_VALID(s, 1.0f, 0.0f);      // closed throttle
     114           2 :         EXPECT_POINT_VALID(s, 2.5f, 50.0f);     // half throttle
     115           2 :         EXPECT_POINT_VALID(s, 4.0f, 100.0f) // full throttle
     116             : 
     117             :         // Test out of range
     118           1 :         EXPECT_POINT_INVALID(s, 0.0f);
     119           1 :         EXPECT_POINT_INVALID(s, 5.0f);
     120             : 
     121             :         // Test that the passthru (redundant sensor) is working
     122           2 :         EXPECT_POINT_VALID(s, 2.5f, 50.0f);
     123           1 :         EXPECT_NEAR(50.0f, Sensor::get(SensorType::AcceleratorPedal).value_or(-1), EPS2D);
     124           1 : }
     125             : 
     126           4 : TEST(SensorInit, DriverIntentNoPedal) {
     127           1 :         EngineTestHelper eth(engine_type_e::TEST_ENGINE);
     128             : 
     129             :         // We have no pedal - so we should get the TPS
     130           1 :         engineConfiguration->throttlePedalPositionAdcChannel = EFI_ADC_NONE;
     131             : 
     132           1 :         initTps();
     133             : 
     134           1 :         ASSERT_TRUE(Sensor::hasSensor(SensorType::Tps1));
     135             :         // Ensure a sensor got set
     136           1 :         ASSERT_TRUE(Sensor::hasSensor(SensorType::DriverThrottleIntent));
     137             : 
     138             :         // Set values so we can identify which one got proxied
     139           1 :         Sensor::setMockValue(SensorType::Tps1, 25);
     140           1 :         Sensor::setMockValue(SensorType::AcceleratorPedal, 75);
     141             : 
     142             :         // Should get the TPS
     143           1 :         EXPECT_EQ(Sensor::get(SensorType::DriverThrottleIntent).Value, 25);
     144           1 : }
     145             : 
     146             : 
     147           4 : TEST(SensorInit, DriverIntentWithPedal) {
     148           1 :         EngineTestHelper eth(engine_type_e::TEST_ENGINE);
     149             : 
     150             :         // We have a pedal, so we should get it
     151           1 :         engineConfiguration->throttlePedalPositionAdcChannel = EFI_ADC_0;
     152             : 
     153           1 :         initTps();
     154             : 
     155             :         // Ensure a sensor got set
     156           1 :         ASSERT_TRUE(Sensor::hasSensor(SensorType::DriverThrottleIntent));
     157             : 
     158             :         // Set values so we can identify which one got proxied
     159           1 :         Sensor::setMockValue(SensorType::Tps1, 25);
     160           1 :         Sensor::setMockValue(SensorType::AcceleratorPedal, 75);
     161             : 
     162             :         // Should get the pedal
     163           1 :         EXPECT_EQ(Sensor::get(SensorType::DriverThrottleIntent).Value, 75);
     164           1 : }
     165             : 
     166           4 : TEST(SensorInit, OilPressure) {
     167           1 :         EngineTestHelper eth(engine_type_e::TEST_ENGINE);
     168             : 
     169           1 :         engineConfiguration->oilPressure.hwChannel = EFI_ADC_0;
     170           1 :         engineConfiguration->oilPressure.v1 = 1;
     171           1 :         engineConfiguration->oilPressure.v2 = 4;
     172           1 :         engineConfiguration->oilPressure.value1 = 0;
     173           1 :         engineConfiguration->oilPressure.value2 = 1000;
     174             : 
     175           1 :         initFluidPressure();
     176             : 
     177             :         // Ensure the sensors were registered
     178           1 :         auto s = const_cast<Sensor*>(Sensor::getSensorOfType(SensorType::OilPressure));
     179           1 :         ASSERT_NE(nullptr, s);
     180             : 
     181             :         // Test in range
     182           2 :         EXPECT_POINT_VALID(s, 1.0f, 0.0f);      // minimum
     183           2 :         EXPECT_POINT_VALID(s, 2.5f, 500.0f);    // mid
     184           2 :         EXPECT_POINT_VALID(s, 4.0f, 1000.0f) // maximium
     185             : 
     186             :         // Test out of range
     187           1 :         EXPECT_POINT_INVALID(s, 0.0f);
     188           1 :         EXPECT_POINT_INVALID(s, 5.0f);
     189           1 : }
     190             : 
     191           4 : TEST(SensorInit, Clt) {
     192           1 :         EngineTestHelper eth(engine_type_e::TEST_ENGINE);
     193             : 
     194             :         // 2003 neon sensor
     195           1 :         engineConfiguration->clt.config = {0, 30, 100, 32500, 7550, 700, 2700};
     196           1 :         engineConfiguration->clt.adcChannel = EFI_ADC_6;
     197             : 
     198           1 :         initThermistors();
     199             : 
     200             :         // Ensure the sensors were registered
     201           1 :         auto s = const_cast<Sensor*>(Sensor::getSensorOfType(SensorType::Clt));
     202           1 :         ASSERT_NE(nullptr, s);
     203             : 
     204             :         // Test in range
     205           2 :         EXPECT_POINT_VALID(s, 4.61648f, 0.0f);  // minimum - 0C
     206           2 :         EXPECT_POINT_VALID(s, 3.6829f, 30.0f);  // mid - 30C
     207           2 :         EXPECT_POINT_VALID(s, 1.0294f, 100.0f)  // maximium - 100C
     208             : 
     209             :         // Test out of range
     210           1 :         EXPECT_POINT_INVALID(s, 0.0f);
     211           1 :         EXPECT_POINT_INVALID(s, 5.0f);
     212           1 : }
     213             : 
     214           4 : TEST(SensorInit, Lambda) {
     215           1 :         EngineTestHelper eth(engine_type_e::TEST_ENGINE);
     216             : 
     217           1 :         initLambda();
     218             : 
     219           1 :         auto s = Sensor::getSensorOfType(SensorType::Lambda1);
     220           1 :         ASSERT_NE(nullptr, s);
     221           1 : }
     222             : 
     223           4 : TEST(SensorInit, Map) {
     224           1 :         EngineTestHelper eth(engine_type_e::TEST_ENGINE);
     225           1 :         engineConfiguration->map.sensor.hwChannel = EFI_ADC_4;
     226             : 
     227           1 :         initMap();
     228             : 
     229           1 :         auto s = Sensor::getSensorOfType(SensorType::Map);
     230           1 :         ASSERT_NE(nullptr, s);
     231             : 
     232           1 :         Sensor::setMockValue(SensorType::MapFast, 25);
     233           1 :         Sensor::setMockValue(SensorType::MapSlow, 75);
     234             : 
     235             :         // Should prefer fast MAP
     236           1 :         EXPECT_FLOAT_EQ(25, Sensor::getOrZero(SensorType::Map));
     237             : 
     238             :         // But when that fails, should return slow MAP
     239           1 :         Sensor::resetMockValue(SensorType::MapFast);
     240           1 :         EXPECT_FLOAT_EQ(75, Sensor::getOrZero(SensorType::Map));
     241           1 : }

Generated by: LCOV version 1.14