Repository navigation
Expand file tree
/
Copy pathtest_button.c
More file actions
522 lines (419 loc) · 13.9 KB
/
Copy pathtest_button.c
File metadata and controls
522 lines (419 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* MultiButton Unit Tests
* Minimal test framework with no external dependencies.
* Uses a mock GPIO function to drive the state machine deterministically.
*/
#include "multi_button.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ---- Minimal test framework ---- */
static int tests_run = 0;
static int tests_passed = 0;
static int tests_failed = 0;
#define ASSERT(expr) do { \
if (!(expr)) { \
printf(" FAIL: %s (line %d)\n", #expr, __LINE__); \
return 1; \
} \
} while(0)
#define RUN_TEST(fn) do { \
tests_run++; \
printf(" [%d] %s ... ", tests_run, #fn); \
if (fn() == 0) { tests_passed++; printf("OK\n"); } \
else { tests_failed++; printf("FAILED\n"); } \
} while(0)
/* ---- Mock GPIO ---- */
static uint8_t mock_gpio_value = 0;
static uint8_t mock_read_gpio(uint8_t button_id)
{
(void)button_id;
return mock_gpio_value;
}
/* ---- Helper: advance N ticks ---- */
static void tick_n(int n)
{
for (int i = 0; i < n; i++) {
button_ticks();
}
}
/* ---- Event tracking via callbacks ---- */
#define MAX_EVENTS 64
static ButtonEvent event_log[MAX_EVENTS];
static int event_count = 0;
static void reset_event_log(void) { event_count = 0; }
static void log_press_down(Button* btn, void* user_data) { (void)btn; (void)user_data; if (event_count < MAX_EVENTS) event_log[event_count++] = BTN_PRESS_DOWN; }
static void log_press_up(Button* btn, void* user_data) { (void)btn; (void)user_data; if (event_count < MAX_EVENTS) event_log[event_count++] = BTN_PRESS_UP; }
static void log_single_click(Button* btn, void* user_data) { (void)btn; (void)user_data; if (event_count < MAX_EVENTS) event_log[event_count++] = BTN_SINGLE_CLICK; }
static void log_double_click(Button* btn, void* user_data) { (void)btn; (void)user_data; if (event_count < MAX_EVENTS) event_log[event_count++] = BTN_DOUBLE_CLICK; }
static void log_long_start(Button* btn, void* user_data) { (void)btn; (void)user_data; if (event_count < MAX_EVENTS) event_log[event_count++] = BTN_LONG_PRESS_START; }
static void log_long_hold(Button* btn, void* user_data) { (void)btn; (void)user_data; if (event_count < MAX_EVENTS) event_log[event_count++] = BTN_LONG_PRESS_HOLD; }
static void log_repeat(Button* btn, void* user_data) { (void)btn; (void)user_data; if (event_count < MAX_EVENTS) event_log[event_count++] = BTN_PRESS_REPEAT; }
static int has_event(ButtonEvent ev)
{
for (int i = 0; i < event_count; i++) {
if (event_log[i] == ev) return 1;
}
return 0;
}
static int count_event(ButtonEvent ev)
{
int c = 0;
for (int i = 0; i < event_count; i++) {
if (event_log[i] == ev) c++;
}
return c;
}
/* ---- Helper: init button with all logging callbacks ---- */
static Button test_btn;
static void setup_button(void)
{
mock_gpio_value = 0;
reset_event_log();
button_init(&test_btn, mock_read_gpio, 1, 1);
button_attach(&test_btn, BTN_PRESS_DOWN, log_press_down, NULL);
button_attach(&test_btn, BTN_PRESS_UP, log_press_up, NULL);
button_attach(&test_btn, BTN_SINGLE_CLICK, log_single_click, NULL);
button_attach(&test_btn, BTN_DOUBLE_CLICK, log_double_click, NULL);
button_attach(&test_btn, BTN_LONG_PRESS_START, log_long_start, NULL);
button_attach(&test_btn, BTN_LONG_PRESS_HOLD, log_long_hold, NULL);
button_attach(&test_btn, BTN_PRESS_REPEAT, log_repeat, NULL);
button_start(&test_btn);
}
static void teardown_button(void)
{
button_stop(&test_btn);
mock_gpio_value = 0;
}
/* ============================================================
* Test cases
* ============================================================ */
/* Test 1: Single click */
static int test_single_click(void)
{
setup_button();
/* Press for ~50ms (10 ticks), then release */
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 10);
mock_gpio_value = 0;
tick_n(DEBOUNCE_TICKS + SHORT_TICKS + 10); /* wait for timeout */
ASSERT(has_event(BTN_PRESS_DOWN));
ASSERT(has_event(BTN_PRESS_UP));
ASSERT(has_event(BTN_SINGLE_CLICK));
ASSERT(!has_event(BTN_DOUBLE_CLICK));
ASSERT(!has_event(BTN_LONG_PRESS_START));
teardown_button();
return 0;
}
/* Test 2: Double click */
static int test_double_click(void)
{
setup_button();
/* First click */
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 10);
mock_gpio_value = 0;
tick_n(DEBOUNCE_TICKS + 5);
/* Second click (within SHORT_TICKS) */
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 10);
mock_gpio_value = 0;
tick_n(DEBOUNCE_TICKS + SHORT_TICKS + 10);
ASSERT(has_event(BTN_DOUBLE_CLICK));
ASSERT(count_event(BTN_PRESS_DOWN) == 2);
teardown_button();
return 0;
}
/* Test 3: Long press */
static int test_long_press(void)
{
setup_button();
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + LONG_TICKS + 20);
ASSERT(has_event(BTN_PRESS_DOWN));
ASSERT(has_event(BTN_LONG_PRESS_START));
ASSERT(has_event(BTN_LONG_PRESS_HOLD));
ASSERT(count_event(BTN_LONG_PRESS_START) == 1); /* exactly once */
/* Release */
mock_gpio_value = 0;
tick_n(DEBOUNCE_TICKS + 10);
ASSERT(has_event(BTN_PRESS_UP));
teardown_button();
return 0;
}
/* Test 4: Repeat press count */
static int test_repeat_press(void)
{
setup_button();
for (int i = 0; i < 3; i++) {
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 8);
mock_gpio_value = 0;
tick_n(DEBOUNCE_TICKS + 5);
}
/* Wait for timeout */
tick_n(SHORT_TICKS + 20);
ASSERT(count_event(BTN_PRESS_DOWN) == 3);
ASSERT(has_event(BTN_PRESS_REPEAT));
teardown_button();
return 0;
}
/* Test 5: Debounce rejects noise */
static int test_debounce(void)
{
setup_button();
/* Toggle GPIO rapidly (less than DEBOUNCE_TICKS consecutive readings) */
for (int i = 0; i < 20; i++) {
mock_gpio_value = (i % 2); /* alternate every tick */
tick_n(1);
}
mock_gpio_value = 0;
tick_n(10);
ASSERT(!has_event(BTN_PRESS_DOWN));
ASSERT(!has_event(BTN_SINGLE_CLICK));
teardown_button();
return 0;
}
/* Test 6: REPEAT->PRESS transition resets ticks (bug fix verification) */
static int test_repeat_to_press_transition(void)
{
setup_button();
/* First click: quick press and release */
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 8);
mock_gpio_value = 0;
tick_n(DEBOUNCE_TICKS + 5);
/* Second click: press and hold past SHORT_TICKS into REPEAT, then stay held */
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + SHORT_TICKS + 20);
/* At this point, should have transitioned REPEAT->PRESS with ticks reset.
* It should NOT have fired BTN_LONG_PRESS_START yet because ticks was reset. */
ASSERT(!has_event(BTN_LONG_PRESS_START));
/* Continue holding for LONG_TICKS more to trigger long press */
tick_n(LONG_TICKS + 10);
ASSERT(has_event(BTN_LONG_PRESS_START));
teardown_button();
return 0;
}
/* Test 7: button_start duplicate returns -1 */
static int test_start_duplicate(void)
{
setup_button();
int ret = button_start(&test_btn);
ASSERT(ret == -1);
teardown_button();
return 0;
}
/* Test 8: button_stop then restart works cleanly */
static int test_stop_and_restart(void)
{
setup_button();
button_stop(&test_btn);
reset_event_log();
/* Ticks should not process the stopped button */
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 10);
mock_gpio_value = 0;
tick_n(DEBOUNCE_TICKS + SHORT_TICKS + 10);
ASSERT(!has_event(BTN_PRESS_DOWN));
/* Restart and verify it works */
button_reset(&test_btn);
button_start(&test_btn);
reset_event_log();
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 10);
mock_gpio_value = 0;
tick_n(DEBOUNCE_TICKS + SHORT_TICKS + 10);
ASSERT(has_event(BTN_SINGLE_CLICK));
teardown_button();
return 0;
}
/* Test 9: NULL handle safety */
static int test_null_handle(void)
{
/* These should not crash */
button_init(NULL, mock_read_gpio, 1, 1);
button_attach(NULL, BTN_SINGLE_CLICK, log_single_click, NULL);
button_detach(NULL, BTN_SINGLE_CLICK);
button_stop(NULL);
button_reset(NULL);
ASSERT(button_get_event(NULL) == BTN_NONE_PRESS);
ASSERT(button_get_repeat_count(NULL) == 0);
ASSERT(button_is_pressed(NULL) == -1);
ASSERT(button_start(NULL) == -2);
return 0;
}
/* Test 10: button_reset clears state properly */
static int test_reset(void)
{
setup_button();
/* Generate some activity */
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 10);
ASSERT(has_event(BTN_PRESS_DOWN));
button_reset(&test_btn);
ASSERT(button_get_event(&test_btn) == BTN_NONE_PRESS);
ASSERT(button_get_repeat_count(&test_btn) == 0);
teardown_button();
return 0;
}
/* Test 11: button_stop called from callback (linked list safety) */
static Button stop_btn_a, stop_btn_b;
static int stop_cb_called = 0;
static void cb_stop_self(Button* btn, void* user_data)
{
(void)user_data;
stop_cb_called++;
button_stop(btn); /* Remove self from list during iteration */
}
static int test_stop_in_callback(void)
{
stop_cb_called = 0;
mock_gpio_value = 0;
button_init(&stop_btn_a, mock_read_gpio, 1, 10);
button_init(&stop_btn_b, mock_read_gpio, 1, 11);
button_attach(&stop_btn_a, BTN_PRESS_DOWN, cb_stop_self, NULL);
button_attach(&stop_btn_b, BTN_PRESS_DOWN, log_press_down, NULL);
button_start(&stop_btn_a);
button_start(&stop_btn_b);
reset_event_log();
/* Press: both buttons should fire BTN_PRESS_DOWN.
* stop_btn_a's callback removes itself from the list.
* The iteration must still process stop_btn_b safely. */
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 5);
ASSERT(stop_cb_called >= 1);
ASSERT(has_event(BTN_PRESS_DOWN)); /* stop_btn_b should have fired */
/* Cleanup */
button_stop(&stop_btn_b);
mock_gpio_value = 0;
return 0;
}
/* Test 12: ticks counter saturation (no overflow) */
static int test_ticks_saturation(void)
{
setup_button();
/* Press and hold the button */
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 5);
/* Manually set ticks near UINT16_MAX to test saturation */
test_btn.ticks = UINT16_MAX - 2;
/* Tick a few more times */
tick_n(5);
/* ticks should saturate at UINT16_MAX, not wrap to 0 */
ASSERT(test_btn.ticks == UINT16_MAX);
teardown_button();
return 0;
}
/* Test 13: Triple click via repeat count */
static int test_triple_click(void)
{
setup_button();
/* Three rapid clicks */
for (int i = 0; i < 3; i++) {
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 8);
mock_gpio_value = 0;
tick_n(DEBOUNCE_TICKS + 5);
}
/* Wait for timeout */
tick_n(SHORT_TICKS + 20);
/* Should have 3 press downs and repeat events */
ASSERT(count_event(BTN_PRESS_DOWN) == 3);
ASSERT(has_event(BTN_PRESS_REPEAT));
/* repeat count should be 3 (or check via last known state) */
/* Note: after timeout, state goes to IDLE, but repeat count is preserved */
ASSERT(button_get_repeat_count(&test_btn) >= 3);
teardown_button();
return 0;
}
/* Test 14: user_data is passed to callbacks */
static int user_data_received = 0;
static void* user_data_value = NULL;
static void cb_check_user_data(Button* btn, void* user_data)
{
(void)btn;
user_data_received = 1;
user_data_value = user_data;
}
static int test_user_data(void)
{
Button ud_btn;
int my_context = 42;
mock_gpio_value = 0;
user_data_received = 0;
user_data_value = NULL;
button_init(&ud_btn, mock_read_gpio, 1, 99);
button_attach(&ud_btn, BTN_PRESS_DOWN, cb_check_user_data, &my_context);
button_start(&ud_btn);
/* Trigger press down */
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 5);
ASSERT(user_data_received == 1);
ASSERT(user_data_value == &my_context);
ASSERT(*(int*)user_data_value == 42);
button_stop(&ud_btn);
mock_gpio_value = 0;
return 0;
}
/* Test 15: Boundary - DEBOUNCE_TICKS edge behavior */
static int test_debounce_boundary(void)
{
setup_button();
/* Hold for exactly DEBOUNCE_TICKS - should register */
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS);
/* After exactly DEBOUNCE_TICKS of consistent high reading,
* the level should have changed and press detected */
tick_n(5); /* a few more ticks for state machine to process */
ASSERT(has_event(BTN_PRESS_DOWN));
teardown_button();
return 0;
}
/* Test 16: Stress test - rapid alternating press/release */
static int test_rapid_press_release(void)
{
setup_button();
/* 50 rapid press/release cycles */
for (int i = 0; i < 50; i++) {
mock_gpio_value = 1;
tick_n(DEBOUNCE_TICKS + 3);
mock_gpio_value = 0;
tick_n(DEBOUNCE_TICKS + 3);
}
/* Wait for timeout */
tick_n(SHORT_TICKS + 20);
/* Should not crash, and should have registered some presses */
ASSERT(count_event(BTN_PRESS_DOWN) > 0);
teardown_button();
return 0;
}
/* ============================================================ */
int main(void)
{
printf("MultiButton Unit Tests (v%d.%d.%d)\n",
MULTIBUTTON_VERSION_MAJOR, MULTIBUTTON_VERSION_MINOR, MULTIBUTTON_VERSION_PATCH);
printf("=====================================\n");
RUN_TEST(test_single_click);
RUN_TEST(test_double_click);
RUN_TEST(test_long_press);
RUN_TEST(test_repeat_press);
RUN_TEST(test_debounce);
RUN_TEST(test_repeat_to_press_transition);
RUN_TEST(test_start_duplicate);
RUN_TEST(test_stop_and_restart);
RUN_TEST(test_null_handle);
RUN_TEST(test_reset);
RUN_TEST(test_stop_in_callback);
RUN_TEST(test_ticks_saturation);
RUN_TEST(test_triple_click);
RUN_TEST(test_user_data);
RUN_TEST(test_debounce_boundary);
RUN_TEST(test_rapid_press_release);
printf("\nResults: %d/%d passed", tests_passed, tests_run);
if (tests_failed > 0) {
printf(", %d FAILED", tests_failed);
}
printf("\n");
return tests_failed > 0 ? 1 : 0;
}