blob: ba3bd87999a2177da480ff15e6222f0c807945e6 [file] [log] [blame]
Neil Balch6040a352018-03-04 16:02:56 -08001#include "y2018/control_loops/superstructure/debouncer.h"
2
3#include "gtest/gtest.h"
4
Stephan Pleinesf63bde82024-01-13 15:59:33 -08005namespace y2018::control_loops::superstructure::testing {
Neil Balch6040a352018-03-04 16:02:56 -08006
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.
12TEST(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.
31TEST(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 Pleinesf63bde82024-01-13 15:59:33 -080053} // namespace y2018::control_loops::superstructure::testing