aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2026-02-28 17:37:19 +0530
committerAkshay Nair <phenax5@gmail.com>2026-02-28 17:37:19 +0530
commit18268280dc6c0bd44035b53655ae43e9c88245bd (patch)
treec17b1703d8a3deb4cd67a6e6466d024f02a49bb7 /test
parent02c12bfedcf669df81491ea49502a982e980bcda (diff)
downloaddaft-watch-18268280dc6c0bd44035b53655ae43e9c88245bd.tar.gz
daft-watch-18268280dc6c0bd44035b53655ae43e9c88245bd.zip
Add set time mode + test for button
Diffstat (limited to 'test')
-rw-r--r--test/button_test.c67
-rw-r--r--test/justfile13
-rw-r--r--test/stubbed/avr/io.h46
-rw-r--r--test/stubbed/testindex.c26
4 files changed, 129 insertions, 23 deletions
diff --git a/test/button_test.c b/test/button_test.c
new file mode 100644
index 0000000..3c48e43
--- /dev/null
+++ b/test/button_test.c
@@ -0,0 +1,67 @@
+#include "button.h"
+#include "mode.h"
+#include <assert.h>
+#include <avr/io.h>
+
+void test_button_press() {
+ assert(check_button_state() == ButtonIdle);
+
+ PINA &= ~(1 << PA0); // Button is pressed
+ assert(check_button_state() == ButtonActive);
+ for (int i = 0; i < DEBOUNCE_TICK_COUNT; i++)
+ assert(check_button_state() == ButtonActive);
+
+ PINA |= (1 << PA0); // Button is released
+
+ assert(check_button_state() == ButtonPressed); // Detects button press
+ assert(check_button_state() == ButtonIdle); // Resets to idle
+}
+
+void test_button_long_press() {
+ assert(check_button_state() == ButtonIdle);
+
+ // Button is pressed for a while
+ PINA &= ~(1 << PA0);
+ for (int i = 0; i < LONG_PRESS_TICK_COUNT - 1; i++)
+ assert(check_button_state() == ButtonActive);
+
+ assert(check_button_state() == ButtonLongPressed); // Detects button press
+
+ assert(check_button_state() == ButtonIdle); // Resets to idle
+}
+
+void test_button_inactive_after_press() {
+ assert(check_button_state() == ButtonIdle);
+
+ // Button is pressed
+ PINA &= ~(1 << PA0);
+ for (int i = 0; i < DEBOUNCE_TICK_COUNT; i++)
+ assert(check_button_state() == ButtonActive);
+ PINA |= (1 << PA0); // Button is released
+ assert(check_button_state() == ButtonPressed);
+ assert(check_button_state() == ButtonIdle); // Resets to idle
+
+ // Button is pressed again
+ PINA &= ~(1 << PA0);
+
+ // Button stays inactive for some time
+ for (int i = 0; i < BUTTON_INACTIVE_TICK_COUNT - 1; i++)
+ assert(check_button_state() == ButtonIdle);
+ // Button becomes active again after delay
+ 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();
+ TEST test_button_inactive_after_press();
+ return 0;
+}
+
diff --git a/test/justfile b/test/justfile
new file mode 100644
index 0000000..626e094
--- /dev/null
+++ b/test/justfile
@@ -0,0 +1,13 @@
+OUTDIR := "../out"
+TSRCS := "../src/display.c ../src/rtc.c ../src/button.c ../src/mode.c ./stubbed/testindex.c"
+
+TCFLAGS := f"-g -std=gnu99 -DSTUBBED=1 -I../src -I./stubbed/ -Wall -Wno-unused-variable {{TSRCS}}"
+
+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
+
diff --git a/test/stubbed/avr/io.h b/test/stubbed/avr/io.h
index 40cc460..c056ef1 100644
--- a/test/stubbed/avr/io.h
+++ b/test/stubbed/avr/io.h
@@ -2,13 +2,13 @@
#define STUB_AVR_IO
#include <stdio.h>
-static int DDRA = 0;
-static int DDRB = 0;
+extern int DDRA;
+extern int DDRB;
-static int PORTA = 0;
-static int PORTB = 0;
-static int PINA = 0;
-static int PINB = 0;
+extern int PORTA;
+extern int PORTB;
+extern int PINA;
+extern int PINB;
#define PA0 0
#define PA1 1
@@ -24,23 +24,23 @@ static int PINB = 0;
#define PB3 3
#define PB4 4
-static int PRR = 0;
-static int TCCR0B = 0;
-static int TCCR0A = 0;
-static int TIMSK0 = 0;
-static int TIFR0 = 0;
-static int OCR0A = 0;
-static int OCR0B = 0;
-static int TCNT0 = 0;
-static int TCCR1B = 0;
-static int TCCR1A = 0;
-static int TIMSK1 = 0;
-static int TIFR1 = 0;
-static int OCR1A = 0;
-static int OCR1B = 0;
-static int TCNT1 = 0;
-static int TCNT1H = 0;
-static int TCNT1L = 0;
+extern int PRR;
+extern int TCCR0B;
+extern int TCCR0A;
+extern int TIMSK0;
+extern int TIFR0;
+extern int OCR0A;
+extern int OCR0B;
+extern int TCNT0;
+extern int TCCR1B;
+extern int TCCR1A;
+extern int TIMSK1;
+extern int TIFR1;
+extern int OCR1A;
+extern int OCR1B;
+extern int TCNT1;
+extern int TCNT1H;
+extern int TCNT1L;
#define CS00 1
#define CS01 1
diff --git a/test/stubbed/testindex.c b/test/stubbed/testindex.c
new file mode 100644
index 0000000..e3c8866
--- /dev/null
+++ b/test/stubbed/testindex.c
@@ -0,0 +1,26 @@
+
+int DDRA = 0;
+int DDRB = 0;
+
+int PORTA = 0;
+int PORTB = 0;
+int PINA = 0;
+int PINB = 0;
+
+int PRR = 0;
+int TCCR0B = 0;
+int TCCR0A = 0;
+int TIMSK0 = 0;
+int TIFR0 = 0;
+int OCR0A = 0;
+int OCR0B = 0;
+int TCNT0 = 0;
+int TCCR1B = 0;
+int TCCR1A = 0;
+int TIMSK1 = 0;
+int TIFR1 = 0;
+int OCR1A = 0;
+int OCR1B = 0;
+int TCNT1 = 0;
+int TCNT1H = 0;
+int TCNT1L = 0;