diff options
| author | Laytan Laats <laytanlaats@hotmail.com> | 2024-08-02 01:40:05 +0200 |
|---|---|---|
| committer | Laytan Laats <laytanlaats@hotmail.com> | 2024-08-14 01:44:37 +0200 |
| commit | 726891588f67e76a8ab09601c16dfe4e61c0eb3e (patch) | |
| tree | 4a2990197a2150f3ee40e12d41e52b6cba7c3d05 | |
| parent | 2a5ceff6672f05ae8781e8de15f593bbee1d4b3e (diff) | |
posix: more tests
| -rw-r--r-- | core/sys/posix/signal.odin | 6 | ||||
| -rw-r--r-- | tests/core/sys/posix/posix.odin | 77 |
2 files changed, 77 insertions, 6 deletions
diff --git a/core/sys/posix/signal.odin b/core/sys/posix/signal.odin index 3bfd662f0..6b1817d4f 100644 --- a/core/sys/posix/signal.odin +++ b/core/sys/posix/signal.odin @@ -559,7 +559,7 @@ when ODIN_OS == .Darwin { }, si_pid: pid_t, /* [PSX] sending process ID */ si_uid: uid_t, /* [PSX] real user ID of sending process */ - si_status: c.int, /* [PSX] exit value of signal */ + si_status: c.int, /* [PSX] exit value or signal */ si_addr: rawptr, /* [PSX] address of faulting instruction */ si_value: sigval, /* [PSX] signal value */ si_band: c.long, /* [PSX] band event for SIGPOLL */ @@ -709,7 +709,7 @@ when ODIN_OS == .Darwin { }, si_pid: pid_t, /* [PSX] sending process ID */ si_uid: uid_t, /* [PSX] real user ID of sending process */ - si_status: c.int, /* [PSX] exit value of signal */ + si_status: c.int, /* [PSX] exit value or signal */ si_addr: rawptr, /* [PSX] address of faulting instruction */ si_value: sigval, /* [PSX] signal value */ using _reason: struct #raw_union { @@ -889,7 +889,7 @@ when ODIN_OS == .Darwin { using _child: struct { si_pid: pid_t, /* [PSX] sending process ID */ si_uid: uid_t, /* [PSX] real user ID of sending process */ - si_status: c.int, /* [PSX] exit value of signal */ + si_status: c.int, /* [PSX] exit value or signal */ _utime: clock_t, _stime: clock_t, }, diff --git a/tests/core/sys/posix/posix.odin b/tests/core/sys/posix/posix.odin index 1942bfbda..03a2f932c 100644 --- a/tests/core/sys/posix/posix.odin +++ b/tests/core/sys/posix/posix.odin @@ -1,11 +1,15 @@ //+build darwin, freebsd, openbsd, netbsd package tests_core_posix -import "core:sys/posix" -import "core:testing" +import "base:runtime" + import "core:log" -import "core:strings" import "core:path/filepath" +import "core:strings" +import "core:sync" +import "core:sys/posix" +import "core:testing" +import "core:time" @(test) test_arpa_inet :: proc(t: ^testing.T) { @@ -196,6 +200,14 @@ test_stat :: proc(t: ^testing.T) { testing.expect_value(t, posix.S_IRWXG, transmute(posix.mode_t)posix._mode_t(posix._S_IRWXG)) testing.expect_value(t, posix.S_IRWXO, transmute(posix.mode_t)posix._mode_t(posix._S_IRWXO)) testing.expect_value(t, posix._S_IFMT, transmute(posix.mode_t)posix._mode_t(posix.__S_IFMT)) + + stat: posix.stat_t + testing.expect_value(t, posix.stat(#file, &stat), posix.result.OK) + testing.expect(t, posix.S_ISREG(stat.st_mode)) + testing.expect_value(t, stat.st_mode, posix.mode_t{.IROTH, .IRGRP, .IRUSR, .IWUSR, .IFREG}) + + CONTENT := #load(#file) + testing.expect_value(t, stat.st_size, posix.off_t(len(CONTENT))) } @(test) @@ -209,3 +221,62 @@ test_termios :: proc(t: ^testing.T) { testing.expect_value(t, transmute(posix.COutput_Flags)posix.tcflag_t(posix._VTDLY), posix.VTDLY) testing.expect_value(t, transmute(posix.COutput_Flags)posix.tcflag_t(posix._FFDLY), posix.FFDLY) } + +@(test) +test_signal :: proc(t: ^testing.T) { + @static tt: ^testing.T + tt = t + + @static ctx: runtime.Context + ctx = context + + act: posix.sigaction_t + act.sa_flags = {.SIGINFO, .RESETHAND} + act.sa_sigaction = handler + testing.expect_value(t, posix.sigaction(.SIGCHLD, &act, nil), posix.result.OK) + + handler :: proc "c" (sig: posix.Signal, info: ^posix.siginfo_t, address: rawptr) { + context = ctx + testing.expect_value(tt, sig, posix.Signal.SIGCHLD) + testing.expect_value(tt, info.si_signo, posix.Signal.SIGCHLD) + testing.expect_value(tt, info.si_status, 69) + testing.expect_value(tt, info.si_code.chld, posix.CLD_Code.EXITED) + } + + switch pid := posix.fork(); pid { + case -1: + log.errorf("fork() failure: %v", posix.strerror()) + case 0: + posix.exit(69) + case: + status: i32 + posix.waitpid(pid, &status, {}) + testing.expect(t, posix.WIFEXITED(status)) + testing.expect(t, posix.WEXITSTATUS(status) == 69) + } +} + +@(test) +test_pthreads :: proc(t: ^testing.T) { + testing.set_fail_timeout(t, time.Second) + + NTHREADS :: 3 + thread_ids: [NTHREADS]posix.pthread_t + + @static counter: int + + for &tid in thread_ids { + posix.pthread_create(&tid, nil, thread_function, nil) + } + + for tid in thread_ids { + posix.pthread_join(tid, nil) + } + + testing.expect_value(t, counter, NTHREADS) + + thread_function :: proc "c" (_: rawptr) -> rawptr { + sync.atomic_add(&counter, 1) + return nil + } +} |