From b0e1ed405acac3469b5fb5d21a91a34cfebac241 Mon Sep 17 00:00:00 2001 From: Nathan Schulte Date: Fri, 24 May 2024 03:24:57 -0500 Subject: [PATCH] RedundantSensor unit tests for "monitoring" vs "averaging" --- unit_tests/tests/sensor/redundant.cpp | 198 ++++++++++++++++++++------ 1 file changed, 155 insertions(+), 43 deletions(-) diff --git a/unit_tests/tests/sensor/redundant.cpp b/unit_tests/tests/sensor/redundant.cpp index e6c83f04e0..cec94f0d27 100644 --- a/unit_tests/tests/sensor/redundant.cpp +++ b/unit_tests/tests/sensor/redundant.cpp @@ -24,7 +24,7 @@ class SensorRedundant : public ::testing::Test ASSERT_TRUE(m1.Register()); ASSERT_TRUE(m2.Register()); - dut.configure(5.0f, false, 100); + dut.configure(5.0f, false); } void TearDown() override @@ -58,23 +58,11 @@ TEST_F(SensorRedundant, SetOnlyOneSensor) auto result = dut.get(); EXPECT_FALSE(result.Valid); } -} - -TEST_F(SensorRedundant, SetTwoSensors) -{ - // Don't set any sensors - expect invalid - { - auto result = dut.get(); - EXPECT_FALSE(result.Valid); - EXPECT_EQ(result.Code, UnexpectedCode::Inconsistent); - } - // Set one sensor - m1.set(24.0f); // Set the other sensor m2.set(26.0f); - // Should now be valid - and the average of the two input + // Should now be valid - and the average of both sensors { auto result = dut.get(); EXPECT_TRUE(result.Valid); @@ -82,6 +70,18 @@ TEST_F(SensorRedundant, SetTwoSensors) } } +TEST_F(SensorRedundant, CheckOnlySecondInvalid) +{ + // Set second sensor only + m2.set(66.0f); + + // Should be invalid - only one is set! + { + auto result = dut.get(); + EXPECT_FALSE(result.Valid); + } +} + TEST_F(SensorRedundant, DifferenceNone) { // Set both sensors to the same value @@ -137,13 +137,13 @@ TEST_F(SensorRedundant, DifferenceOverLimitSwapped) } -class SensorRedundantIgnoreSecond : public ::testing::Test +class SensorRedundantMonitoring : public ::testing::Test { protected: RedundantSensor dut; MockSensor m1, m2; - SensorRedundantIgnoreSecond() + SensorRedundantMonitoring() : dut(SensorType::Tps1, SensorType::Tps1Primary, SensorType::Tps1Secondary) , m1(SensorType::Tps1Primary) , m2(SensorType::Tps1Secondary) @@ -159,7 +159,7 @@ class SensorRedundantIgnoreSecond : public ::testing::Test ASSERT_TRUE(m1.Register()); ASSERT_TRUE(m2.Register()); - dut.configure(5.0f, true, 100); + dut.configure(5.0f, false, false); } void TearDown() override @@ -168,53 +168,144 @@ class SensorRedundantIgnoreSecond : public ::testing::Test } }; -TEST_F(SensorRedundantIgnoreSecond, CheckIsRedundant) +TEST_F(SensorRedundantMonitoring, CheckIsRedundant) { - // Expect not isRedundant - { - EXPECT_FALSE(dut.isRedundant()); - } + EXPECT_TRUE(dut.isRedundant()); } -TEST_F(SensorRedundantIgnoreSecond, OnlyFirst) + +TEST_F(SensorRedundantMonitoring, CheckSensorsInvalid) { // Don't set any sensors - expect invalid { auto result = dut.get(); EXPECT_FALSE(result.Valid); - EXPECT_EQ(result.Code, UnexpectedCode::Unknown); } // Set one sensor - m1.set(44.0f); + m1.set(24.0f); - // Should be valid - we don't care about second sensor + // Should still be invalid - only one is set! + { + auto result = dut.get(); + EXPECT_FALSE(result.Valid); + } + + // Set the other sensor + m2.set(26.0f); + + // Should now be valid - and directly track the first sensor { auto result = dut.get(); EXPECT_TRUE(result.Valid); - EXPECT_FLOAT_EQ(result.Value, 44.0f); + EXPECT_FLOAT_EQ(result.Value, 24.0f); } } -TEST_F(SensorRedundantIgnoreSecond, OnlySecond) +TEST_F(SensorRedundantMonitoring, CheckOnlySecondInvalid) { - // Don't set any sensors - expect invalid + // Set second sensor only + m2.set(66.0f); + // Should be invalid - only one is set! { auto result = dut.get(); EXPECT_FALSE(result.Valid); - EXPECT_EQ(result.Code, UnexpectedCode::Unknown); } +} - // Set second sensor only - m2.set(66.0f); +TEST_F(SensorRedundantMonitoring, Differencenone) +{ + // Set both sensors to the same value + m1.set(10.0f); + m2.set(10.0f); - // Should be invalid - should ignore second sensor + // Expect valid, and 10 output + { + auto result = dut.get(); + EXPECT_TRUE(result.Valid); + EXPECT_FLOAT_EQ(result.Value, 10.0f); + } +} + +TEST_F(SensorRedundantMonitoring, DifferenceNearLimit) +{ + // Set both sensors to nearly the limit (4.998 apart) + m1.set(7.501f); + m2.set(12.499f); + + // Expect valid, and 10 output + { + auto result = dut.get(); + EXPECT_TRUE(result.Valid); + EXPECT_FLOAT_EQ(result.Value, 7.501f); + } +} + +TEST_F(SensorRedundantMonitoring, DifferenceOverLimit) +{ + // Set both sensors barely over the limit (5.002 apart) + m1.set(7.499f); + m2.set(12.501f); + + // Expect invalid + { + auto result = dut.get(); + EXPECT_FALSE(result.Valid); + } +} + +TEST_F(SensorRedundantMonitoring, DifferenceOverLimitSwapped) +{ + // Now try it the other way (m1 > m2) + m1.set(12.501f); + m2.set(7.499f); + + // Expect invalid { auto result = dut.get(); EXPECT_FALSE(result.Valid); } } -TEST_F(SensorRedundantIgnoreSecond, SetBothIgnoreSecond) +class SensorRedundantIgnoreSecond : public ::testing::Test +{ +protected: + RedundantSensor dut; + MockSensor m1, m2; + + SensorRedundantIgnoreSecond() + : dut(SensorType::Tps1, SensorType::Tps1Primary, SensorType::Tps1Secondary) + , m1(SensorType::Tps1Primary) + , m2(SensorType::Tps1Secondary) + { + } + + void SetUp() override + { + Sensor::resetRegistry(); + + // Other tests verify registry function - don't re-test it here + ASSERT_TRUE(dut.Register()); + ASSERT_TRUE(m1.Register()); + ASSERT_TRUE(m2.Register()); + + dut.configure(5.0f, true); + } + + void TearDown() override + { + Sensor::resetRegistry(); + } +}; + +TEST_F(SensorRedundantIgnoreSecond, CheckIsRedundant) +{ + // Expect not isRedundant + { + EXPECT_FALSE(dut.isRedundant()); + } +} + +TEST_F(SensorRedundantIgnoreSecond, OnlyFirst) { // Don't set any sensors - expect invalid { @@ -223,15 +314,36 @@ TEST_F(SensorRedundantIgnoreSecond, SetBothIgnoreSecond) EXPECT_EQ(result.Code, UnexpectedCode::Unknown); } - // Set both sensors - m1.set(74.0f); - m2.set(76.0f); + // Set first sensor + m1.set(44.0f); + + // Should be valid - we don't care about second sensor + { + auto result = dut.get(); + EXPECT_TRUE(result.Valid); + EXPECT_FLOAT_EQ(result.Value, 44.0f); + } + + // Set the second sensor too + m2.set(46.0f); // Should be valid, but only get the value from m1 { auto result = dut.get(); EXPECT_TRUE(result.Valid); - EXPECT_FLOAT_EQ(result.Value, 74.0f); + EXPECT_FLOAT_EQ(result.Value, 44.0f); + } +} + +TEST_F(SensorRedundantIgnoreSecond, CheckOnlySecondInvalid) +{ + // Set second sensor only + m2.set(66.0f); + + // Should be invalid - should ignore second sensor + { + auto result = dut.get(); + EXPECT_FALSE(result.Valid); } } @@ -257,7 +369,7 @@ class SensorRedundantPartialSecond : public ::testing::Test ASSERT_TRUE(m1.Register()); ASSERT_TRUE(m2.Register()); - dut.configure(5.0f, false, 50); + dut.configure(5.0f, false, true, 50); } void TearDown() override @@ -302,15 +414,15 @@ TEST_F(SensorRedundantPartialSecond, SetTwoSensors) } // Set first sensor - m1.set(12.0f); - // Set second sensor at double the first - m2.set(28.0f); + m1.set(0.0f); + // Set second sensor + m2.set(0.0f); // Should now be valid - and the average of the two input { auto result = dut.get(); EXPECT_TRUE(result.Valid); - EXPECT_FLOAT_EQ(result.Value, 13.0f); + EXPECT_FLOAT_EQ(result.Value, 0.0f); } }