Neil Balch | 6040a35 | 2018-03-04 16:02:56 -0800 | [diff] [blame] | 1 | #include "y2018/control_loops/superstructure/debouncer.h" |
| 2 | |
| 3 | #include "gtest/gtest.h" |
| 4 | |
| 5 | namespace y2018 { |
| 6 | namespace control_loops { |
| 7 | namespace superstructure { |
| 8 | namespace testing { |
| 9 | |
| 10 | // Tests that the debouncer behaves as it should. This tests the following: |
| 11 | // - The debouncer changes its internal state after the desired number of |
| 12 | // repeated inputs. |
| 13 | // - The debouncer doesn't change its internal state before the desired number |
| 14 | // of repeated inputs. |
| 15 | TEST(DebouncerTest, Debouncer) { |
| 16 | Debouncer bouncer(false, 2); |
| 17 | |
| 18 | bouncer.Update(true); |
| 19 | bouncer.Update(true); |
| 20 | EXPECT_EQ(true, bouncer.current_state()); |
| 21 | |
| 22 | bouncer.Update(false); |
| 23 | |
| 24 | // Only one false, state shouldn't have changed. |
| 25 | EXPECT_EQ(true, bouncer.current_state()); |
| 26 | |
| 27 | bouncer.Update(false); |
| 28 | // Now there are two falses in a row, the state should've changed. |
| 29 | EXPECT_EQ(false, bouncer.current_state()); |
| 30 | } |
| 31 | |
| 32 | // Test that the debouncer will hold its state through a short-lived state |
| 33 | // change. |
| 34 | TEST(DebouncerTest, DebouncerLongSequence) { |
| 35 | Debouncer bouncer(false, 2); |
| 36 | |
| 37 | bouncer.Update(true); |
| 38 | |
| 39 | // Only one true, should still read false. |
| 40 | EXPECT_EQ(false, bouncer.current_state()); |
| 41 | |
| 42 | bouncer.Update(true); |
| 43 | |
| 44 | // Two trues, should now read true. |
| 45 | EXPECT_EQ(true, bouncer.current_state()); |
| 46 | |
| 47 | bouncer.Update(false); |
| 48 | |
| 49 | // Only one false, should still read true. |
| 50 | EXPECT_EQ(true, bouncer.current_state()); |
| 51 | |
| 52 | bouncer.Update(true); |
| 53 | |
| 54 | EXPECT_EQ(true, bouncer.current_state()); |
| 55 | } |
| 56 | } // namespace testing |
| 57 | } // namespace superstructure |
| 58 | } // namespace control_loops |
| 59 | } // namespace y2018 |