summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/fs_event_local.c30
-rw-r--r--tests/loop_sleep.c47
-rw-r--r--tests/stdin_lines.c31
-rw-r--r--tests/timer_loop.c39
-rw-r--r--tests/timer_thread.c30
5 files changed, 177 insertions, 0 deletions
diff --git a/tests/fs_event_local.c b/tests/fs_event_local.c
new file mode 100644
index 0000000..2e1e41d
--- /dev/null
+++ b/tests/fs_event_local.c
@@ -0,0 +1,30 @@
+#include <aki/common.h>
+#include <aki/event_loop.h>
+#include <aki/fs_event.h>
+
+static str *TEST_FILE_PATH = al_str_c("./test.txt");
+
+static struct aki_event_loop loop;
+static struct aki_fs_event fs;
+
+static void fs_event_callback(void *userdata, struct inotify_event *event)
+{
+ (void)userdata;
+ (void)event;
+ al_printf("WRITE CLOSE\n");
+ aki_fs_event_stop(&fs);
+}
+
+s32 main(void)
+{
+ if (!aki_common_init()) return EXIT_FAILURE;
+
+ aki_event_loop_init(&loop);
+
+ aki_fs_event_init(&fs, TEST_FILE_PATH, AKI_CLOSE_WRITE, fs_event_callback, NULL);
+ if (!aki_fs_event_start(&fs, &loop)) return EXIT_FAILURE;
+
+ aki_event_loop_run(&loop);
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/loop_sleep.c b/tests/loop_sleep.c
new file mode 100644
index 0000000..4823bae
--- /dev/null
+++ b/tests/loop_sleep.c
@@ -0,0 +1,47 @@
+#include <aki/common.h>
+#include <aki/event_loop.h>
+#include <aki/timer.h>
+
+static struct aki_event_loop loop;
+static struct aki_timer timer;
+
+static void timer_callback2(void *userdata, struct aki_timer *timer)
+{
+ (void)userdata;
+ al_printf("4\n");
+ aki_timer_stop(timer);
+ aki_event_loop_sleep(&loop, AKI_TS_FROM_USEC(600000));
+ al_printf("5\n");
+}
+
+static void timer_callback(void *userdata, struct aki_timer *timer)
+{
+ (void)userdata;
+ al_printf("1\n");
+ aki_event_loop_sleep(&loop, AKI_TS_FROM_USEC(100000));
+ al_printf("2\n");
+ timer->callback = timer_callback2;
+ aki_timer_again(timer);
+ aki_event_loop_sleep(&loop, AKI_TS_FROM_USEC(800000));
+ al_printf("3\n");
+ aki_event_loop_sleep(&loop, AKI_TS_FROM_USEC(2000000));
+ al_printf("6\n");
+}
+
+s32 main(void)
+{
+ if (!aki_common_init()) return EXIT_FAILURE;
+
+ aki_event_loop_init(&loop);
+
+ aki_timer_init(&timer, &loop, timer_callback, NULL);
+ aki_timer_set_repeat(&timer, AKI_TS_FROM_USEC(1000000));
+ aki_timer_again(&timer);
+
+ aki_event_loop_sleep(&loop, AKI_TS_FROM_USEC(6000000));
+ al_printf("7\n");
+
+ aki_event_loop_run(&loop);
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/stdin_lines.c b/tests/stdin_lines.c
new file mode 100644
index 0000000..cf0f941
--- /dev/null
+++ b/tests/stdin_lines.c
@@ -0,0 +1,31 @@
+#include <aki/common.h>
+#include <aki/line_processor.h>
+
+static struct aki_event_loop loop;
+static struct aki_line_processor pro;
+
+static u8 line_callback(void *userdata, str *line)
+{
+ (void)userdata;
+ al_printf("%.*s\n", AL_STR_PRINTF(line));
+ if (al_str_eq(line, al_str_c("quit"))) return AKI_LINE_PROCESSOR_STOP;
+ return AKI_LINE_PROCESSOR_CONTINUE;
+}
+
+s32 main(void)
+{
+ if (!aki_common_init()) return EXIT_FAILURE;
+
+ aki_event_loop_init(&loop);
+
+ pro.callback = line_callback;
+ pro.userdata = NULL;
+ aki_line_processor_init(&pro, al_str_c("\n"));
+ aki_line_processor_open_fd(&pro, STDIN_FILENO);
+ aki_line_processor_run(&pro, &loop);
+
+ aki_event_loop_run(&loop);
+ aki_event_loop_destroy(&loop);
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/timer_loop.c b/tests/timer_loop.c
new file mode 100644
index 0000000..3c403f9
--- /dev/null
+++ b/tests/timer_loop.c
@@ -0,0 +1,39 @@
+#include <aki/common.h>
+#include <aki/thread.h>
+#include <aki/timer.h>
+
+#define DELAY 100000L
+#define REPEAT 21
+
+static struct aki_event_loop loop;
+static struct aki_timer timer;
+
+static f64 start;
+static u32 iter = 0;
+
+static void timer_callback(void *userdata, struct aki_timer *timer)
+{
+ (void)userdata;
+ u64 diff = (aki_get_tick() - start) * 1000000Lu;
+ aki_timer_set_repeat(timer, AKI_TS_FROM_USEC(DELAY + ((s64)(iter * DELAY) - diff)));
+ al_printf("[%2u] %lfs\n", iter, diff / 1000000.0);
+ if (++iter == REPEAT) aki_timer_stop(timer);
+ else aki_timer_again(timer);
+}
+
+s32 main(void)
+{
+ if (!aki_common_init()) return EXIT_FAILURE;
+
+ aki_event_loop_init(&loop);
+
+ aki_timer_init(&timer, &loop, timer_callback, NULL);
+ aki_timer_set_repeat(&timer, AKI_TS_FROM_USEC(DELAY));
+ start = aki_get_tick();
+ timer_callback(NULL, &timer);
+
+ aki_event_loop_run(&loop);
+ aki_event_loop_destroy(&loop);
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/timer_thread.c b/tests/timer_thread.c
new file mode 100644
index 0000000..b3a8aa4
--- /dev/null
+++ b/tests/timer_thread.c
@@ -0,0 +1,30 @@
+#include <aki/common.h>
+#include <aki/thread.h>
+
+#define DELAY 100000L
+#define REPEAT 21u
+
+static struct aki_thread thread;
+
+static aki_thread_result AKI_THREADCALL test_thread(void *userdata)
+{
+ (void)userdata;
+ u64 diff;
+ f64 start = aki_get_tick();
+ for (u32 i = 0; i < REPEAT; i++) {
+ diff = (aki_get_tick() - start) * 1000000Lu;
+ aki_thread_sleep(AKI_TS_FROM_USEC(DELAY + ((s64)(i * DELAY) - diff)));
+ al_printf("[%2u] %lfs\n", i, diff / 1000000.0);
+ }
+ return 0;
+}
+
+s32 main(void)
+{
+ if (!aki_common_init()) return EXIT_FAILURE;
+
+ aki_thread_create(&thread, test_thread, NULL);
+ aki_thread_join(&thread);
+
+ return EXIT_SUCCESS;
+}