diff options
| author | Akshay Nair <phenax5@gmail.com> | 2026-03-01 10:35:53 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2026-03-01 10:35:53 +0530 |
| commit | 96b074de6104161c62e09b2b1a725316dbf1c5f3 (patch) | |
| tree | 0ff68fd7fdf611365a20f718664818f27437e630 | |
| parent | 18268280dc6c0bd44035b53655ae43e9c88245bd (diff) | |
| download | daft-watch-96b074de6104161c62e09b2b1a725316dbf1c5f3.tar.gz daft-watch-96b074de6104161c62e09b2b1a725316dbf1c5f3.zip | |
Add tests for mode state
| -rw-r--r-- | src/mode.c | 3 | ||||
| -rw-r--r-- | src/mode.h | 6 | ||||
| -rw-r--r-- | test/button_test.c | 16 | ||||
| -rw-r--r-- | test/justfile | 9 | ||||
| -rw-r--r-- | test/mode_test.c | 95 | ||||
| -rw-r--r-- | test/stubbed/avr/interrupt.h | 4 | ||||
| -rw-r--r-- | test/stubbed/testutils.h | 8 | ||||
| -rw-r--r-- | test/stubbed/util/delay.h | 2 |
8 files changed, 129 insertions, 14 deletions
@@ -34,6 +34,7 @@ void apply_button_state(enum ButtonState button_state) { new_minutes = minutes; new_hours = hours; current_digit_index = 0; + time_set_timeout_ticks = SET_MODE_TIMEOUT; current_mode = ModeSetTime; break; default: @@ -49,7 +50,7 @@ void apply_button_state(enum ButtonState button_state) { return; } } else { - time_set_timeout_ticks = 10 * 1024; + time_set_timeout_ticks = SET_MODE_TIMEOUT; } switch (button_state) { @@ -6,6 +6,8 @@ enum Mode { ModeSetTime, }; +#define SET_MODE_TIMEOUT 1000 + enum ButtonState { ButtonIdle = 0, ButtonActive = 1, @@ -19,4 +21,8 @@ extern volatile uint8_t new_hours; void apply_button_state(enum ButtonState button_state); +// Just for tests +extern volatile uint8_t current_digit_index; +extern volatile uint16_t time_set_timeout_ticks; + #endif diff --git a/test/button_test.c b/test/button_test.c index 3c48e43..bef557c 100644 --- a/test/button_test.c +++ b/test/button_test.c @@ -1,8 +1,17 @@ #include "button.h" #include "mode.h" +#include "stubbed/testutils.h" #include <assert.h> #include <avr/io.h> +void before_each() { + PINA = (1 << PA0); // Active on low + button_tick_count = 0; + button_inactive_for_ticks = 0; +} + +void after_each() {} + void test_button_press() { assert(check_button_state() == ButtonIdle); @@ -51,13 +60,6 @@ void test_button_inactive_after_press() { assert(check_button_state() == ButtonActive); } -void before_each() { - PINA = (1 << PA0); // Active on low - button_tick_count = 0; - button_inactive_for_ticks = 0; -} -#define TEST before_each(); - int main() { TEST test_button_press(); TEST test_button_long_press(); diff --git a/test/justfile b/test/justfile index 626e094..5ec1733 100644 --- a/test/justfile +++ b/test/justfile @@ -7,7 +7,10 @@ main: gcc {{TCFLAGS}} ../src/main.c -o {{OUTDIR}}/test_main {{OUTDIR}}/test_main -button: - gcc {{TCFLAGS}} ./button_test.c -o {{OUTDIR}}/test_button - {{OUTDIR}}/test_button +button: (test "button") +mode: (test "mode") + +test source: + gcc {{TCFLAGS}} ./{{source}}_test.c -o {{OUTDIR}}/test_{{source}} + {{OUTDIR}}/test_{{source}} diff --git a/test/mode_test.c b/test/mode_test.c new file mode 100644 index 0000000..1a76ed9 --- /dev/null +++ b/test/mode_test.c @@ -0,0 +1,95 @@ +#include <assert.h> +#include <avr/io.h> + +#include "button.h" +#include "mode.h" +#include "rtc.h" +#include "stubbed/testutils.h" + +void before_each() { + button_inactive_for_ticks = 0; + current_digit_index = 0; + new_minutes = 0; + new_hours = 0; + current_mode = ModeDisplayTime; +} + +void after_each() {} + +void test_switch_to_set_time_mode() { + current_mode = ModeDisplayTime; + apply_button_state(ButtonLongPressed); + assert(current_mode == ModeSetTime); +} + +void test_switch_back_to_display_after_timeout_on_idle() { + current_mode = ModeSetTime; + apply_button_state(ButtonActive); // Start on active + + for (int i = 0; i < SET_MODE_TIMEOUT - 1; i++) { + apply_button_state(ButtonIdle); + assert(current_mode == ModeSetTime); + } + apply_button_state(ButtonIdle); + + // Go back to display mode after timeout + assert(current_mode == ModeDisplayTime); +} + +void test_extend_timeout_on_button_activity() { + current_mode = ModeSetTime; + apply_button_state(ButtonActive); // Start on active + + for (int i = 0; i < SET_MODE_TIMEOUT - 1; i++) { + apply_button_state(ButtonIdle); + assert(current_mode == ModeSetTime); + } + apply_button_state(ButtonActive); + apply_button_state(ButtonIdle); + + // Stay on set mode if interrupted with a button active state + assert(current_mode == ModeSetTime); +} + +void test_update_time() { + current_mode = ModeSetTime; + current_digit_index = 0; + new_hours = 12; + new_minutes = 34; + hours = 0; + minutes = 0; + + apply_button_state(ButtonPressed); + assert(new_hours == 22); + assert(new_minutes == 34); + + apply_button_state(ButtonLongPressed); // Next digit + apply_button_state(ButtonPressed); + assert(new_hours == 23); + assert(new_minutes == 34); + + apply_button_state(ButtonLongPressed); // Next digit + apply_button_state(ButtonPressed); + assert(new_hours == 23); + assert(new_minutes == 44); + + apply_button_state(ButtonLongPressed); // Next digit + apply_button_state(ButtonPressed); + assert(new_hours == 23); + assert(new_minutes == 45); + + assert(hours == 0); + assert(minutes == 0); + apply_button_state(ButtonLongPressed); // Apply new time to rtc + assert(hours == 23); + assert(minutes == 45); +} + +int main() { + TEST test_switch_to_set_time_mode(); + TEST test_switch_back_to_display_after_timeout_on_idle(); + TEST test_extend_timeout_on_button_activity(); + TEST test_update_time(); + return 0; +} + diff --git a/test/stubbed/avr/interrupt.h b/test/stubbed/avr/interrupt.h index 19dfa36..5163846 100644 --- a/test/stubbed/avr/interrupt.h +++ b/test/stubbed/avr/interrupt.h @@ -1,7 +1,7 @@ #define ISR(INTER, ...) void generated_interrupt_##INTER() -static void cli() {} -static void sei() {} +inline void cli() {} +inline void sei() {} #define stub_run_timers() \ { \ diff --git a/test/stubbed/testutils.h b/test/stubbed/testutils.h new file mode 100644 index 0000000..64d90e2 --- /dev/null +++ b/test/stubbed/testutils.h @@ -0,0 +1,8 @@ +#ifndef LOADED_TESTUTILS +#define LOADED_TESTUTILS + +#define TEST \ + before_each(); \ + for (int i = 0; i < 1; i++, after_each()) + +#endif diff --git a/test/stubbed/util/delay.h b/test/stubbed/util/delay.h index 3954609..6167586 100644 --- a/test/stubbed/util/delay.h +++ b/test/stubbed/util/delay.h @@ -3,5 +3,5 @@ #include <unistd.h> -static void _delay_ms(int ms) { usleep(ms * 1000); } +inline void _delay_ms(int ms) { usleep(ms * 1000); } #endif |
