|
| 1 | +// Copyright 2025 WheelOS. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Created Date: 2026-01-03 |
| 16 | +// Author: daohu527 |
| 17 | + |
| 18 | +#include "modules/common/util/debouncer.h" |
| 19 | + |
| 20 | +#include <gtest/gtest.h> |
| 21 | + |
| 22 | +namespace apollo { |
| 23 | +namespace control { |
| 24 | + |
| 25 | +/** |
| 26 | + * @class CounterDebouncerTest |
| 27 | + * @brief Test suite class for managing the CounterDebouncer test context. |
| 28 | + */ |
| 29 | +class CounterDebouncerTest : public ::testing::Test { |
| 30 | + protected: |
| 31 | + // Setup logic can be added here if needed |
| 32 | + virtual void SetUp() {} |
| 33 | + virtual void TearDown() {} |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * @test Verify if the initial state is correct after object construction. |
| 38 | + */ |
| 39 | +TEST_F(CounterDebouncerTest, Initialization) { |
| 40 | + uint32_t threshold = 5; |
| 41 | + CounterDebouncer debouncer(threshold); |
| 42 | + |
| 43 | + EXPECT_EQ(debouncer.count(), 0u); |
| 44 | + EXPECT_FALSE(debouncer.IsActive()); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * @test Verify the normal debouncing process until the threshold is reached. |
| 49 | + */ |
| 50 | +TEST_F(CounterDebouncerTest, NormalDebounceProcess) { |
| 51 | + uint32_t threshold = 3; |
| 52 | + CounterDebouncer debouncer(threshold); |
| 53 | + |
| 54 | + // 1st fault sample: fault not yet confirmed |
| 55 | + EXPECT_FALSE(debouncer.Update(true)); |
| 56 | + EXPECT_EQ(debouncer.count(), 1u); |
| 57 | + |
| 58 | + // 2nd fault sample: fault not yet confirmed |
| 59 | + EXPECT_FALSE(debouncer.Update(true)); |
| 60 | + EXPECT_EQ(debouncer.count(), 2u); |
| 61 | + |
| 62 | + // 3rd fault sample: threshold reached, fault confirmed |
| 63 | + EXPECT_TRUE(debouncer.Update(true)); |
| 64 | + EXPECT_EQ(debouncer.count(), 3u); |
| 65 | + EXPECT_TRUE(debouncer.IsActive()); |
| 66 | + |
| 67 | + // 4th fault sample: counter should stay capped at threshold and remain active |
| 68 | + EXPECT_TRUE(debouncer.Update(true)); |
| 69 | + EXPECT_EQ(debouncer.count(), 3u); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * @test Verify the Immediate Reset Strategy. |
| 74 | + * Even if the fault is close to being confirmed, a single normal signal |
| 75 | + * must reset the counter to zero immediately. |
| 76 | + */ |
| 77 | +TEST_F(CounterDebouncerTest, ImmediateResetOnNormalSignal) { |
| 78 | + CounterDebouncer debouncer(10); |
| 79 | + |
| 80 | + // Simulate 9 consecutive fault samples |
| 81 | + for (int i = 0; i < 9; ++i) { |
| 82 | + debouncer.Update(true); |
| 83 | + } |
| 84 | + EXPECT_FALSE(debouncer.IsActive()); |
| 85 | + EXPECT_EQ(debouncer.count(), 9u); |
| 86 | + |
| 87 | + // Receive one normal signal |
| 88 | + EXPECT_FALSE(debouncer.Update(false)); |
| 89 | + |
| 90 | + // Counter must reset to zero immediately |
| 91 | + EXPECT_EQ(debouncer.count(), 0u); |
| 92 | + EXPECT_FALSE(debouncer.IsActive()); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * @test Verify the recovery logic after a fault has been confirmed. |
| 97 | + */ |
| 98 | +TEST_F(CounterDebouncerTest, RecoveryAfterConfirmation) { |
| 99 | + CounterDebouncer debouncer(2); |
| 100 | + |
| 101 | + // Confirm the fault |
| 102 | + debouncer.Update(true); |
| 103 | + debouncer.Update(true); |
| 104 | + ASSERT_TRUE(debouncer.IsActive()); |
| 105 | + |
| 106 | + // Receive a normal signal; fault state should recover immediately |
| 107 | + EXPECT_FALSE(debouncer.Update(false)); |
| 108 | + EXPECT_FALSE(debouncer.IsActive()); |
| 109 | + EXPECT_EQ(debouncer.count(), 0u); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * @test Verify the manual Reset function. |
| 114 | + */ |
| 115 | +TEST_F(CounterDebouncerTest, ManualReset) { |
| 116 | + CounterDebouncer debouncer(5); |
| 117 | + debouncer.Update(true); |
| 118 | + debouncer.Update(true); |
| 119 | + |
| 120 | + debouncer.Reset(); |
| 121 | + |
| 122 | + EXPECT_EQ(debouncer.count(), 0u); |
| 123 | + EXPECT_FALSE(debouncer.IsActive()); |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * @test Boundary condition: threshold is 0. |
| 128 | + * Based on the logic (threshold > 0), IsActive should always be false. |
| 129 | + */ |
| 130 | +TEST_F(CounterDebouncerTest, ZeroThresholdBoundary) { |
| 131 | + CounterDebouncer debouncer(0); |
| 132 | + |
| 133 | + EXPECT_FALSE(debouncer.IsActive()); |
| 134 | + EXPECT_FALSE(debouncer.Update(true)); |
| 135 | + EXPECT_FALSE(debouncer.IsActive()); |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * @test Boundary condition: threshold is 1. |
| 140 | + * Should trigger/confirm fault on the very first fault sample. |
| 141 | + */ |
| 142 | +TEST_F(CounterDebouncerTest, SingleSampleThreshold) { |
| 143 | + CounterDebouncer debouncer(1); |
| 144 | + |
| 145 | + EXPECT_TRUE(debouncer.Update(true)); |
| 146 | + EXPECT_TRUE(debouncer.IsActive()); |
| 147 | + EXPECT_FALSE(debouncer.Update(false)); |
| 148 | +} |
| 149 | + |
| 150 | +} // namespace control |
| 151 | +} // namespace apollo |
0 commit comments