summaryrefslogtreecommitdiff
path: root/src/user/app/tests/libc
diff options
context:
space:
mode:
authordzwdz2022-08-04 21:43:38 +0200
committerdzwdz2022-08-04 21:43:38 +0200
commit26dc784103914b9d6ba36e0a96fa4b3af977626f (patch)
treed1c9938bac9426d4f6709344315d845b45b20865 /src/user/app/tests/libc
parent81a58004d51547d074b4218f906b0b95f2b2c5dc (diff)
user/tests: split the tests by parts of codebase
Diffstat (limited to 'src/user/app/tests/libc')
-rw-r--r--src/user/app/tests/libc/esemaphore.c94
-rw-r--r--src/user/app/tests/libc/string.c25
2 files changed, 119 insertions, 0 deletions
diff --git a/src/user/app/tests/libc/esemaphore.c b/src/user/app/tests/libc/esemaphore.c
new file mode 100644
index 0000000..c78dd56
--- /dev/null
+++ b/src/user/app/tests/libc/esemaphore.c
@@ -0,0 +1,94 @@
+#include "../tests.h"
+#include <camellia/flags.h>
+#include <camellia/syscalls.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <user/lib/esemaphore.h>
+
+static void odd(handle_t out, struct evil_sem *sem1, struct evil_sem *sem2) {
+ _syscall_write(out, "1", 1, -1, 0);
+ esem_signal(sem1);
+
+ esem_wait(sem2);
+ _syscall_write(out, "3", 1, -1, 0);
+ esem_signal(sem1);
+
+ esem_wait(sem2);
+ _syscall_write(out, "5", 1, -1, 0);
+ esem_signal(sem1);
+}
+
+static void even(handle_t out, struct evil_sem *sem1, struct evil_sem *sem2) {
+ esem_wait(sem1);
+ _syscall_write(out, "2", 1, -1, 0);
+ esem_signal(sem2);
+
+ esem_wait(sem1);
+ _syscall_write(out, "4", 1, -1, 0);
+ esem_signal(sem2);
+
+ esem_wait(sem1);
+ _syscall_write(out, "6", 1, -1, 0);
+ esem_signal(sem2);
+}
+
+static void test_semaphore(void) {
+ struct evil_sem *sem1, *sem2;
+ handle_t pipe[2];
+ test(_syscall_pipe(pipe, 0) >= 0);
+
+ if (!fork()) {
+ sem1 = esem_new(0);
+ sem2 = esem_new(0);
+ test(sem1 && sem2);
+ if (!fork()) {
+ odd(pipe[1], sem1, sem2);
+ exit(69);
+ } else {
+ even(pipe[1], sem1, sem2);
+ test(_syscall_await() == 69);
+ }
+ esem_free(sem1);
+ esem_free(sem2);
+
+ _syscall_write(pipe[1], "|", 1, -1, 0);
+
+ sem1 = esem_new(0);
+ sem2 = esem_new(0);
+ test(sem1 && sem2);
+ if (!fork()) {
+ even(pipe[1], sem1, sem2);
+ exit(69);
+ } else {
+ odd(pipe[1], sem1, sem2);
+ test(_syscall_await() == 69);
+ _syscall_await();
+ }
+ esem_free(sem1);
+ esem_free(sem2);
+
+ exit(0);
+ } else {
+ close(pipe[1]);
+
+ char buf[16];
+ size_t pos = 0;
+ for (;;) {
+ int ret = _syscall_read(pipe[0], buf + pos, sizeof(buf) - pos, 0);
+ if (ret < 0) break;
+ pos += ret;
+ }
+ buf[pos] = '\0'; // idc about the "potential" overflow
+ if (strcmp(buf, "123456|123456")) {
+ printf("%s\n", buf);
+ test_fail();
+ }
+
+ _syscall_await();
+ }
+}
+
+void r_libc_esemaphore(void) {
+ run_test(test_semaphore);
+}
diff --git a/src/user/app/tests/libc/string.c b/src/user/app/tests/libc/string.c
new file mode 100644
index 0000000..8f889dc
--- /dev/null
+++ b/src/user/app/tests/libc/string.c
@@ -0,0 +1,25 @@
+#include "../tests.h"
+#include <string.h>
+
+static void test_strtol(void) {
+ char *end;
+ test(1234 == strtol("1234", NULL, 10));
+ test(1234 == strtol("+1234", NULL, 10));
+ test(-1234 == strtol("-1234", NULL, 10));
+
+ test(1234 == strtol("1234", &end, 10));
+ test(!strcmp("", end));
+ test(1234 == strtol(" 1234hello", &end, 10));
+ test(!strcmp("hello", end));
+
+ test(1234 == strtol(" 1234hello", &end, 0));
+ test(!strcmp("hello", end));
+ test(0xCAF3 == strtol(" 0xCaF3hello", &end, 0));
+ test(!strcmp("hello", end));
+ test(01234 == strtol(" 01234hello", &end, 0));
+ test(!strcmp("hello", end));
+}
+
+void r_libc_string(void) {
+ run_test(test_strtol);
+}