| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Created by kifir on 12/19/24. | |||
| 3 | // | |||
| 4 | ||||
| 5 | #include "pch.h" | |||
| 6 | ||||
| 7 | #include "util/test_base.h" | |||
| 8 | ||||
| 9 | #include "fuel_level_func.h" | |||
| 10 | ||||
| 11 | namespace { | |||
| 12 | constexpr float calculateNextFilteredValue(float currentFilteredValue, float newValue, float alpha) { | |||
| 13 | return currentFilteredValue + (newValue - currentFilteredValue) * alpha; | |||
| 14 | } | |||
| 15 | ||||
| 16 | class FuelLevelFuncTest: public TestBase<> { | |||
| 17 | protected: | |||
| 18 | static constexpr float TEST_FUEL_LEVEL_UPDATE_PERIOD_SEC = 0.932f; | |||
| 19 | static constexpr float TEST_FUEL_LEVEL_UPDATE_PERIOD_USEC = static_cast<int>( | |||
| 20 | TEST_FUEL_LEVEL_UPDATE_PERIOD_SEC * 1000000.0f | |||
| 21 | ); | |||
| 22 | static constexpr float TEST_FUEL_LEVEL_ALPHA = 0.17f; | |||
| 23 | static constexpr float TEST_FUEL_LEVEL_LOW_THRESHOLD_VOLTAGE = 0.01f; | |||
| 24 | static constexpr float TEST_FUEL_LEVEL_HIGH_THRESHOLD_VOLTAGE = 100.0f; | |||
| 25 | ||||
| 26 | static constexpr uint8_t TEST_DEFAULT_FUEL_LEVEL = 12; | |||
| 27 | ||||
| 28 | static constexpr float INPUT_VALUE_0 = 51.932f; | |||
| 29 | static constexpr float EXPECTED_FILTERED_VALUE_0 = INPUT_VALUE_0; | |||
| 30 | static constexpr uint8_t EXPECTED_FUEL_LEVEL_0 = 89; | |||
| 31 | ||||
| 32 | static constexpr float INPUT_VALUE_1 = 1.932f; | |||
| 33 | static constexpr float EXPECTED_FILTERED_VALUE_1 = | |||
| 34 | calculateNextFilteredValue(EXPECTED_FILTERED_VALUE_0, INPUT_VALUE_1, TEST_FUEL_LEVEL_ALPHA); | |||
| 35 | static constexpr uint8_t EXPECTED_FUEL_LEVEL_1 = 78; | |||
| 36 | ||||
| 37 | static constexpr float INPUT_VALUE_2 = INPUT_VALUE_1; | |||
| 38 | static constexpr float EXPECTED_FILTERED_VALUE_2 = | |||
| 39 | calculateNextFilteredValue(EXPECTED_FILTERED_VALUE_1, INPUT_VALUE_2, TEST_FUEL_LEVEL_ALPHA); | |||
| 40 | static constexpr uint8_t EXPECTED_FUEL_LEVEL_2 = 67; | |||
| 41 | ||||
| 42 | static const FuelLevelBinsCurve TEST_FUEL_LEVEL_BINS; | |||
| 43 | static const FuelLevelValuesCurve TEST_FUEL_LEVEL_VALUES; | |||
| 44 | ||||
| 45 | void SetUp() override; | |||
| 46 | ||||
| 47 | float convert(float newValue); | |||
| 48 | UnexpectedCode invalidConvert(float newValue); | |||
| 49 | ||||
| 50 | void checkThatNewValueIsIgnoredDuringUpdatePeriod(float previousValue); | |||
| 51 | private: | |||
| 52 | static FuelLevelBinsCurve getTestFuelLevelBins(); | |||
| 53 | static FuelLevelValuesCurve getTestFuelLevelValues(); | |||
| 54 | std::unique_ptr<FuelLevelFunc> m_fuelLevelFunc; | |||
| 55 | }; | |||
| 56 | ||||
| 57 | const FuelLevelBinsCurve FuelLevelFuncTest::TEST_FUEL_LEVEL_BINS = FuelLevelFuncTest::getTestFuelLevelBins(); | |||
| 58 | const FuelLevelValuesCurve FuelLevelFuncTest::TEST_FUEL_LEVEL_VALUES = FuelLevelFuncTest::getTestFuelLevelValues(); | |||
| 59 | ||||
| 60 | 2 | void FuelLevelFuncTest::SetUp() { | ||
| 61 | 2 | TestBase::SetUp(); | ||
| 62 | ||||
| 63 |
1/1✓ Branch 2 taken 2 times.
|
6 | setUpEngineConfiguration(EngineConfig() | |
| 64 |
1/1✓ Branch 5 taken 2 times.
|
6 | .setFuelLevelUpdatePeriodSec(TEST_FUEL_LEVEL_UPDATE_PERIOD_SEC) | |
| 65 |
1/1✓ Branch 4 taken 2 times.
|
8 | .setFuelLevelAveragingAlpha(TEST_FUEL_LEVEL_ALPHA) | |
| 66 |
1/1✓ Branch 4 taken 2 times.
|
8 | .setFuelLevelLowThresholdVoltage(TEST_FUEL_LEVEL_LOW_THRESHOLD_VOLTAGE) | |
| 67 |
1/1✓ Branch 4 taken 2 times.
|
8 | .setFuelLevelHighThresholdVoltage(TEST_FUEL_LEVEL_HIGH_THRESHOLD_VOLTAGE) | |
| 68 | ); | |||
| 69 | ||||
| 70 | 2 | getTestPersistentConfiguration().setFuelLevelBinsCurve(TEST_FUEL_LEVEL_BINS); | ||
| 71 | 2 | getTestPersistentConfiguration().setFuelLevelValuesCurve(TEST_FUEL_LEVEL_VALUES); | ||
| 72 | ||||
| 73 |
1/1✓ Branch 2 taken 2 times.
|
2 | m_fuelLevelFunc = std::make_unique<FuelLevelFunc>(); | |
| 74 | 2 | } | ||
| 75 | ||||
| 76 | 12 | float FuelLevelFuncTest::convert(const float newValue) { | ||
| 77 |
1/1✓ Branch 3 taken 12 times.
|
12 | const SensorResult result = m_fuelLevelFunc->convert(newValue); | |
| 78 |
1/6✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 7 not taken.
✗ Branch 12 not taken.
✗ Branch 16 not taken.
✗ Branch 19 not taken.
|
12 | EXPECT_TRUE(result.Valid); | |
| 79 | 12 | return result.Value; | ||
| 80 | } | |||
| 81 | ||||
| 82 | 2 | UnexpectedCode FuelLevelFuncTest::invalidConvert(const float newValue) { | ||
| 83 | 2 | const SensorResult result = m_fuelLevelFunc->convert(newValue); | ||
| 84 |
1/6✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 14 not taken.
✗ Branch 18 not taken.
✗ Branch 21 not taken.
|
2 | EXPECT_FALSE(result.Valid); | |
| 85 | 2 | return result.Code; | ||
| 86 | } | |||
| 87 | ||||
| 88 | 3 | void FuelLevelFuncTest::checkThatNewValueIsIgnoredDuringUpdatePeriod(const float previousValue) { | ||
| 89 | 3 | advanceTimeUs(1); | ||
| 90 |
3/7✓ Branch 3 taken 3 times.
✓ Branch 6 taken 3 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 3 times.
✗ Branch 14 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
|
3 | EXPECT_EQ(convert(99.9f), previousValue); | |
| 91 | ||||
| 92 | 3 | advanceTimeUs(TEST_FUEL_LEVEL_UPDATE_PERIOD_USEC - 1); | ||
| 93 |
3/7✓ Branch 3 taken 3 times.
✓ Branch 6 taken 3 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 3 times.
✗ Branch 14 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
|
3 | EXPECT_EQ(convert(11.1f), previousValue); | |
| 94 | 3 | } | ||
| 95 | ||||
| 96 | 1 | FuelLevelBinsCurve FuelLevelFuncTest::getTestFuelLevelBins() { | ||
| 97 | FuelLevelBinsCurve result; | |||
| 98 | 1 | const long binsCount = result.size(); | ||
| 99 | 1 | float nextBin = 0.0f; | ||
| 100 | // Initialize first bins with arbitrary increasing values: | |||
| 101 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 time.
|
2/2✓ Decision 'true' taken 5 times.
✓ Decision 'false' taken 1 time.
|
6 | for (int i = 0; i < binsCount - 3; i++) { |
| 102 | 5 | nextBin += 0.1f; | ||
| 103 | 5 | result[i] = nextBin; | ||
| 104 | } | |||
| 105 | 1 | result[binsCount - 3] = EXPECTED_FILTERED_VALUE_2; | ||
| 106 | 1 | result[binsCount - 2] = EXPECTED_FILTERED_VALUE_1; | ||
| 107 | 1 | result[binsCount - 1] = EXPECTED_FILTERED_VALUE_0; | ||
| 108 | ||||
| 109 | // Validate bins - they should be increasing | |||
| 110 | 1 | float latestBin = 0.0f; | ||
| 111 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
|
2/2✓ Decision 'true' taken 8 times.
✓ Decision 'false' taken 1 time.
|
9 | for (const float bin: result) { |
| 112 |
1/6✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 9 not taken.
✗ Branch 14 not taken.
✗ Branch 18 not taken.
✗ Branch 21 not taken.
|
8 | EXPECT_TRUE(latestBin < bin); | |
| 113 | 8 | latestBin = bin; | ||
| 114 | } | |||
| 115 | ||||
| 116 | 1 | return result; | ||
| 117 | } | |||
| 118 | ||||
| 119 | 1 | FuelLevelValuesCurve FuelLevelFuncTest::getTestFuelLevelValues() { | ||
| 120 | 1 | FuelLevelValuesCurve result; | ||
| 121 |
1/1✓ Branch 1 taken 1 time.
|
1 | result.fill(TEST_DEFAULT_FUEL_LEVEL); | |
| 122 | 1 | const long valuesCount = result.size(); | ||
| 123 | 1 | result[valuesCount - 3] = EXPECTED_FUEL_LEVEL_2; | ||
| 124 | 1 | result[valuesCount - 2] = EXPECTED_FUEL_LEVEL_1; | ||
| 125 | 1 | result[valuesCount - 1] = EXPECTED_FUEL_LEVEL_0; | ||
| 126 | 1 | return result; | ||
| 127 | } | |||
| 128 | ||||
| 129 | 4 | TEST_F(FuelLevelFuncTest, checkConversion) { | ||
| 130 |
3/7✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
|
1 | EXPECT_EQ(convert(INPUT_VALUE_0), EXPECTED_FUEL_LEVEL_0); | |
| 131 | 1 | checkThatNewValueIsIgnoredDuringUpdatePeriod(EXPECTED_FUEL_LEVEL_0); | ||
| 132 | ||||
| 133 | 1 | advanceTimeUs(1); | ||
| 134 |
3/7✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
|
1 | EXPECT_EQ(convert(INPUT_VALUE_1), EXPECTED_FUEL_LEVEL_1); | |
| 135 | 1 | checkThatNewValueIsIgnoredDuringUpdatePeriod(EXPECTED_FUEL_LEVEL_1); | ||
| 136 | ||||
| 137 | 1 | advanceTimeUs(1); | ||
| 138 |
3/7✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
|
1 | EXPECT_EQ(convert(INPUT_VALUE_2), EXPECTED_FUEL_LEVEL_2); | |
| 139 | 1 | checkThatNewValueIsIgnoredDuringUpdatePeriod(EXPECTED_FUEL_LEVEL_2); | ||
| 140 | 1 | } | ||
| 141 | ||||
| 142 | 4 | TEST_F(FuelLevelFuncTest, checkThresholds) { | ||
| 143 |
3/7✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
|
1 | EXPECT_EQ(convert(TEST_FUEL_LEVEL_LOW_THRESHOLD_VOLTAGE), TEST_DEFAULT_FUEL_LEVEL); | |
| 144 |
3/7✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 time.
✗ Branch 16 not taken.
✗ Branch 21 not taken.
✗ Branch 24 not taken.
|
1 | EXPECT_EQ(invalidConvert(TEST_FUEL_LEVEL_LOW_THRESHOLD_VOLTAGE - EPS5D), UnexpectedCode::Low); | |
| 145 | ||||
| 146 | // conversion result is not affected by previous successful conversion result: | |||
| 147 |
3/7✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
|
1 | EXPECT_EQ(convert(INPUT_VALUE_0), EXPECTED_FUEL_LEVEL_0); | |
| 148 | ||||
| 149 |
3/7✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 time.
✗ Branch 16 not taken.
✗ Branch 21 not taken.
✗ Branch 24 not taken.
|
1 | EXPECT_EQ(invalidConvert(TEST_FUEL_LEVEL_HIGH_THRESHOLD_VOLTAGE + EPS5D), UnexpectedCode::High); | |
| 150 | // conversion result is not affected by previous successful conversion result: | |||
| 151 |
3/7✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✗ Branch 14 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
|
1 | EXPECT_EQ(convert(TEST_FUEL_LEVEL_HIGH_THRESHOLD_VOLTAGE), EXPECTED_FUEL_LEVEL_0); | |
| 152 | 1 | } | ||
| 153 | } | |||
| 154 |