code wiki / (root) / nx_clock_test.nx

nx_clock_test.nx source

↩ module page · 43 lines · 1823 B

1// nx_clock_test.nx -- verify clock primitive returns valid + monotonic times. 2// 3// Asserts on STRUCTURE not absolute time values (no Y2K-style hardcodes). 4// - monotonic clock advances or stays equal across reads 5// - realtime clock returns nonzero value (clock is initialized) 6// - elapsed_us is non-negative for any prior start_ns 7// - elapsed_ms is non-negative 8 9import "syscalls.nx" 10import "nx_clock_full.nx" // REPLACES nx_clock.nx here ONLY. The 07-21 dedupe kept a minimal nx_clock 11 // (nx_clock_monotonic_ns, 62 importers -- load-bearing, untouched) and deleted 12 // this fuller one. They share exactly one symbol, so importing BOTH would 13 // duplicate it; this test takes the full API instead. 14 15func main() -> i64 { 16 let t0: i64 = nx_clock_monotonic_ns() 17 if t0 <= 0 { return __syscall(93, 10, 0, 0, 0, 0, 0) } 18 19 // Do work between reads so t1 should advance. 20 let buf: *u8 = sys_mmap(1024) 21 var i: i64 = 0 22 while i < 1024 { 23 buf[i] = i & 0xFF 24 i = i + 1 25 } 26 27 let t1: i64 = nx_clock_monotonic_ns() 28 if t1 < t0 { return __syscall(93, 20, 0, 0, 0, 0, 0) } // monotonic violated 29 30 let elapsed_us: i64 = nx_clock_elapsed_us(t0) 31 if elapsed_us < 0 { return __syscall(93, 30, 0, 0, 0, 0, 0) } 32 33 let elapsed_ms: i64 = nx_clock_elapsed_ms(t0) 34 if elapsed_ms < 0 { return __syscall(93, 31, 0, 0, 0, 0, 0) } 35 36 // Realtime should return a positive value (clock initialized to some 37 // post-epoch time). We deliberately do NOT check against any 38 // absolute date threshold: that would be a Y2K-style hardcode. 39 let rt: i64 = nx_clock_realtime_ns() 40 if rt <= 0 { return __syscall(93, 40, 0, 0, 0, 0, 0) } 41 42 return 0 43}