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