code wiki / (root) / nx_array_kat2.nx

nx_array_kat2.nx source

↩ module page · 28 lines · 1055 B

1// KAT2 for [N]T: the OUT-PARAM pattern (array decays to a WRITABLE address across a call) + a larger array. 2// Threading/buffer code relies on this: a stack buffer filled by a helper. Expect: 3// 0123456789 4// LAST=255 OK 5import "nx_syscalls.nx" 6 7// fills p[0..n) with ASCII digits '0'+i%10 -- writes THROUGH the decayed array pointer 8func fill(p: *u8, n: i64) -> i64 { 9 var i: i64 = 0 10 while i < n { p[i] = (48 + (i % 10)) as u8; i = i + 1 } 11 return 0 12} 13 14func main() -> i64 { 15 var buf: [16]u8 16 fill(buf, 10) // buf decays to &buf[0]; fill writes into the caller's frame 17 sys_write(1, buf, 10) // must show what fill wrote (proves same storage, not a copy) 18 sys_write(1, "\n" as *u8, 1) 19 20 // larger array: write the last element, read it back 21 var big: [256]u8 22 var i: i64 = 0 23 while i < 256 { big[i] = (i % 256) as u8; i = i + 1 } 24 let last: i64 = big[255] 25 if last == 255 { sys_write(1, "LAST=255 OK\n" as *u8, 12) } else { sys_write(1, "LAST BAD\n" as *u8, 9) } 26 27 sys_exit(0); return 0 28}