LCOV - code coverage report
Current view: top level - unit_tests/tests - test_sticky_pps.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 19 20 95.0 %
Date: 2024-04-22 23:59:02 Functions: 5 5 100.0 %

          Line data    Source code
       1             : 
       2             : #include "pch.h"
       3             : // todo: should we unify code with deadband.h?
       4             : 
       5             : template <typename T>
       6             : class StickyValue {
       7             : public:
       8           6 :         T get(T currentValue, T deadbandThreshold) {
       9             :                 // If we're within the deadband, be "sticky" with the previous value
      10           6 :                 T absError = absF(currentValue - m_lastValue);
      11             : 
      12             :                 if (std::is_same<T,float>::value) {
      13           6 :                         if (cisnan(currentValue))
      14           1 :                                 return currentValue;
      15           5 :                         if (cisnan(m_lastValue))
      16           0 :                                 m_lastValue = currentValue;
      17             :                 }
      18             : 
      19             :                 // use current value if there's enough error
      20           5 :                 if (absError > deadbandThreshold) {
      21           3 :                         m_lastValue = currentValue;
      22             :                 }
      23             : 
      24           5 :                 return m_lastValue;
      25             :         }
      26             : 
      27             : private:
      28             :         T m_lastValue = 0;
      29             : };
      30             : 
      31             : 
      32           4 : TEST(Sticky, PPS) {
      33           1 :         StickyValue<float> pps;
      34             : 
      35           1 :         float threshold = 0.5;
      36             : 
      37           1 :         EXPECT_EQ(1, pps.get(1, threshold));
      38           1 :         EXPECT_EQ(1, pps.get(1.2, threshold));
      39           1 :         EXPECT_EQ(1, pps.get(0.8, threshold));
      40           1 :         EXPECT_NEAR(2.2, pps.get(2.2, threshold), EPS4D);
      41             : 
      42           1 :         float expectedNaN = pps.get(NAN, threshold);
      43           1 :         EXPECT_TRUE(cisnan(expectedNaN));
      44             : 
      45           1 :         EXPECT_NEAR(33, pps.get(33, threshold), EPS4D);
      46           1 : }

Generated by: LCOV version 1.14