Browse Source

posix: more tests

Laytan Laats 1 year ago
parent
commit
726891588f
2 changed files with 77 additions and 6 deletions
  1. 3 3
      core/sys/posix/signal.odin
  2. 74 3
      tests/core/sys/posix/posix.odin

+ 3 - 3
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,
 				},

+ 74 - 3
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
+	}
+}