LCOV - code coverage report
Current view: top level - tests/unit - test_switch_debouncer.c (source / functions) Hit Total Coverage
Test: coverage.clean.info Lines: 59 59 100.0 %
Date: 2026-05-12 07:56:17 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file test_switch_debouncer.c
       3             :  * @brief Unit-Tests fuer den Switch-Debouncer.
       4             :  *
       5             :  * @reqs SWE-025
       6             :  * @arch SWA-006
       7             :  */
       8             : #include "../unit_test_framework.h"
       9             : #include "../../src/switch_debouncer.h"
      10             : 
      11             : TestStats g_test_stats = {0, 0};
      12             : 
      13          50 : static SwitchRaw mk(bool apply, bool release)
      14             : {
      15          50 :     SwitchRaw r = {0};
      16          50 :     r.apply_raw   = apply   ? 1U : 0U;
      17          50 :     r.release_raw = release ? 1U : 0U;
      18          50 :     return r;
      19             : }
      20             : 
      21           2 : static void test_init_state_is_neutral(void)
      22             : {
      23           2 :     TEST_BEGIN("init -> neutral");
      24           2 :     (void)switch_init();
      25           2 :     ASSERT_EQ(switch_get_state(), SWITCH_NEUTRAL);
      26           2 :     TEST_END();
      27           2 : }
      28             : 
      29           2 : static void test_debounce_apply_takes_5_samples(void)
      30             : {
      31           2 :     TEST_BEGIN("apply needs 5 stable samples (50 ms)");
      32           2 :     (void)switch_init();
      33          10 :     for (int i = 0; i < 4; ++i) {
      34           8 :         switch_step_10ms(mk(true, false));
      35           8 :         ASSERT_EQ(switch_get_state(), SWITCH_NEUTRAL);
      36             :     }
      37           2 :     switch_step_10ms(mk(true, false));
      38           2 :     ASSERT_EQ(switch_get_state(), SWITCH_APPLY);
      39           2 :     TEST_END();
      40           2 : }
      41             : 
      42           2 : static void test_debounce_release_takes_5_samples(void)
      43             : {
      44           2 :     TEST_BEGIN("release needs 5 stable samples (50 ms)");
      45           2 :     (void)switch_init();
      46          12 :     for (int i = 0; i < 5; ++i) {
      47          10 :         switch_step_10ms(mk(false, true));
      48             :     }
      49           2 :     ASSERT_EQ(switch_get_state(), SWITCH_RELEASE);
      50           2 :     TEST_END();
      51           2 : }
      52             : 
      53           2 : static void test_bounce_does_not_change_state(void)
      54             : {
      55           2 :     TEST_BEGIN("bouncing input keeps current state");
      56           2 :     (void)switch_init();
      57          12 :     for (int i = 0; i < 5; ++i) {
      58          10 :         switch_step_10ms(mk(true, false));
      59             :     }
      60           2 :     ASSERT_EQ(switch_get_state(), SWITCH_APPLY);
      61             :     /* Bounce: 1 sample apply, 1 sample neutral, 1 sample release ... */
      62           2 :     switch_step_10ms(mk(true, false));
      63           2 :     switch_step_10ms(mk(false, false));
      64           2 :     switch_step_10ms(mk(false, true));
      65           2 :     switch_step_10ms(mk(true, false));
      66           2 :     switch_step_10ms(mk(false, false));
      67           2 :     ASSERT_EQ(switch_get_state(), SWITCH_APPLY);
      68           2 :     TEST_END();
      69           2 : }
      70             : 
      71           2 : static void test_both_pressed_is_neutral(void)
      72             : {
      73           2 :     TEST_BEGIN("both apply+release pressed -> neutral");
      74           2 :     (void)switch_init();
      75          12 :     for (int i = 0; i < 5; ++i) {
      76          10 :         switch_step_10ms(mk(true, true));
      77             :     }
      78           2 :     ASSERT_EQ(switch_get_state(), SWITCH_NEUTRAL);
      79           2 :     TEST_END();
      80           2 : }
      81             : 
      82           2 : int main(void)
      83             : {
      84           2 :     printf("== test_switch_debouncer ==\n");
      85           2 :     test_init_state_is_neutral();
      86           2 :     test_debounce_apply_takes_5_samples();
      87           2 :     test_debounce_release_takes_5_samples();
      88           2 :     test_bounce_does_not_change_state();
      89           2 :     test_both_pressed_is_neutral();
      90           2 :     TEST_SUMMARY();
      91             : }

Generated by: LCOV version 1.14