code wiki / _hdl_build / nx_galx_durbin_gate.nx
nx_galx_durbin_gate.nx source
↩ module page · 106 lines · 6731 B
1// nx_galx_durbin_gate.nx -- sovereign gate for nx_galx_durbin_lib. Writes a crafted galx_dur.raw fixture
2// (4 recordings, line N = recording N, one with a zero duration), compacts it to galx_dur.bin via db_build,
3// then asserts O(1) db_lookup returns the exact duration per id -- and -1 for out-of-range / negative id /
4// stale vidpaths_size / missing .bin. No bash, no curl: this exercises the real library on real bytes.
5// license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "nx_galx_durbin_lib.nx"
8
9func gp_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func gp_n(v: i64) -> i64 {
11 var m: i64=v; var neg: i64=0; if m<0 { neg=1; m=0-m }
12 let b: *u8=sys_mmap(32); var i: i64=32
13 if m==0 { i=i-1; b[i]=(48 as u8) }
14 while m>0 { let q: i64=m/10; i=i-1; b[i]=((48+(m-q*10)) as u8); m=q }
15 if neg==1 { i=i-1; b[i]=(45 as u8) }
16 sys_write(1, ((b as i64)+i) as *u8, 32-i); return 0
17}
18func gp_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
19// compare out[0..len) to a null-terminated expected string (exact, including length). 1 if equal.
20func gp_jeq(out: *u8, len: i64, exp: *u8) -> i64 {
21 var i: i64 = 0
22 while i < len { if out[i] != exp[i] { return 0 } i = i + 1 }
23 if exp[len] != (0 as u8) { return 0 }
24 return 1
25}
26func gp_bcat(buf: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8) { buf[off+i]=s[i]; i=i+1 } return off+i }
27func gp_bn(buf: *u8, off: i64, v: i64) -> i64 {
28 if v == 0 { buf[off] = 48 as u8; return off + 1 }
29 var m: i64 = v; var o: i64 = off
30 let t: *u8 = sys_mmap(28); var k: i64 = 0
31 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
32 var j: i64 = k; while j > 0 { j = j - 1; buf[o] = t[j]; o = o + 1 }
33 return o
34}
35// assert got==want; print + return 1 on pass, 0 on fail.
36func chk(label: *u8, got: i64, want: i64) -> i64 {
37 if got == want { gp_p(" PASS " as *u8); gp_p(label); gp_p(" = " as *u8); gp_n(got); gp_p("\n" as *u8); return 1 }
38 gp_p(" FAIL " as *u8); gp_p(label); gp_p(" got=" as *u8); gp_n(got); gp_p(" want=" as *u8); gp_n(want); gp_p("\n" as *u8); return 0
39}
40
41func main(argc: i64, argv: *i64) -> i64 {
42 let raw: *u8 = "/tmp/nx_durbin_gate.raw" as *u8
43 let bin: *u8 = "/tmp/nx_durbin_gate.bin" as *u8
44 // craft the fixture: 5 lines, line N = recording N (durs 120000 / 0 / 3256853 / 45000 ms; line 4 is a
45 // CORRUPT >24h mis-parse that db_build must cap to 0 so it never sorts/badges as the "longest" video).
46 let data: *u8 = "size=1000000 first_pcr90=0 last_pcr90=0 dur_ms=120000\nsize=2000000 first_pcr90=0 last_pcr90=0 dur_ms=0\nsize=999999999 first_pcr90=0 last_pcr90=0 dur_ms=3256853\nsize=500000 first_pcr90=0 last_pcr90=0 dur_ms=45000\nsize=700000 first_pcr90=0 last_pcr90=0 dur_ms=99999999999\n" as *u8
47 let fd: i64 = sys_openat_wr(raw, 0x1a4)
48 if fd < 0 { gp_p("FATAL cannot write fixture\n" as *u8); return 1 }
49 sys_write(fd, data, gp_strlen(data)); sys_close(fd)
50
51 gp_p("nx_galx_durbin_gate: build + O(1) lookup\n" as *u8)
52 var pass: i64 = 0; var tot: i64 = 0
53 let n: i64 = db_build(raw, bin, 1000)
54 tot=tot+1; pass=pass+chk("db_build n" as *u8, n, 5)
55 tot=tot+1; pass=pass+chk("lookup id0" as *u8, db_lookup(bin, 0, 1000), 120000)
56 tot=tot+1; pass=pass+chk("lookup id1(zero)" as *u8, db_lookup(bin, 1, 1000), 0)
57 tot=tot+1; pass=pass+chk("lookup id2" as *u8, db_lookup(bin, 2, 1000), 3256853)
58 tot=tot+1; pass=pass+chk("lookup id3" as *u8, db_lookup(bin, 3, 1000), 45000)
59 tot=tot+1; pass=pass+chk("lookup id4 >24h capped" as *u8, db_lookup(bin, 4, 1000), 0)
60 tot=tot+1; pass=pass+chk("lookup id5 oob" as *u8, db_lookup(bin, 5, 1000), 0-1)
61 tot=tot+1; pass=pass+chk("lookup id-1 neg" as *u8, db_lookup(bin, 0-1, 1000), 0-1)
62 tot=tot+1; pass=pass+chk("lookup stale vps" as *u8, db_lookup(bin, 0, 2000), 0-1)
63 tot=tot+1; pass=pass+chk("lookup vps=0 skip" as *u8, db_lookup(bin, 2, 0), 3256853)
64 tot=tot+1; pass=pass+chk("lookup missing bin" as *u8, db_lookup("/tmp/nx_durbin_gate_nope.bin" as *u8, 0, 1000), 0-1)
65
66 // batch dur emit (the /api/durs page endpoint): omit zero/out-of-range, preserve request order.
67 let jb: *u8 = sys_mmap(4096); var jl: i64 = 0
68 jl = db_batch_durs(jb, "0,1,2,3" as *u8, bin, 1000)
69 tot=tot+1; pass=pass+chk("batch omit-zero" as *u8, gp_jeq(jb, jl, "{\"0\":120000,\"2\":3256853,\"3\":45000}" as *u8), 1)
70 jl = db_batch_durs(jb, "3" as *u8, bin, 1000)
71 tot=tot+1; pass=pass+chk("batch single" as *u8, gp_jeq(jb, jl, "{\"3\":45000}" as *u8), 1)
72 jl = db_batch_durs(jb, "6,7" as *u8, bin, 1000)
73 tot=tot+1; pass=pass+chk("batch oob empty" as *u8, gp_jeq(jb, jl, "{}" as *u8), 1)
74 jl = db_batch_durs(jb, "0,4" as *u8, bin, 1000)
75 tot=tot+1; pass=pass+chk("batch omit-capped(id4)" as *u8, gp_jeq(jb, jl, "{\"0\":120000}" as *u8), 1)
76 jl = db_batch_durs(jb, "2,0" as *u8, bin, 1000)
77 tot=tot+1; pass=pass+chk("batch order" as *u8, gp_jeq(jb, jl, "{\"2\":3256853,\"0\":120000}" as *u8), 1)
78 jl = db_batch_durs(jb, "0,2,3" as *u8, bin, 2000)
79 tot=tot+1; pass=pass+chk("batch stale vps empty" as *u8, gp_jeq(jb, jl, "{}" as *u8), 1)
80
81 // SCALE: reproduce the production path -- 60000 lines (~3.1MB), the size that SIGSEGV'd on the NAS.
82 // dur_ms = line number, so db_lookup(id) must equal id (a verifiable round-trip across the whole file).
83 let rawbig: *u8 = "/tmp/nx_durbin_gate_big.raw" as *u8
84 let binbig: *u8 = "/tmp/nx_durbin_gate_big.bin" as *u8
85 let N: i64 = 60000
86 let big: *u8 = sys_mmap(N*56 + 64)
87 var off: i64 = 0; var r: i64 = 0
88 while r < N {
89 off = gp_bcat(big, off, "size=1000 first_pcr90=0 last_pcr90=0 dur_ms=" as *u8)
90 off = gp_bn(big, off, r); big[off] = 10 as u8; off = off + 1
91 r = r + 1
92 }
93 let lf: i64 = sys_openat_wr(rawbig, 0x1a4)
94 if lf < 0 { gp_p("FATAL cannot write big fixture\n" as *u8); return 1 }
95 sys_write(lf, big, off); sys_close(lf)
96 gp_p("scale: wrote " as *u8); gp_n(N); gp_p(" lines (" as *u8); gp_n(off); gp_p(" bytes)\n" as *u8)
97 tot=tot+1; pass=pass+chk("big build n" as *u8, db_build(rawbig, binbig, 1000), N)
98 tot=tot+1; pass=pass+chk("big lookup 0" as *u8, db_lookup(binbig, 0, 1000), 0)
99 tot=tot+1; pass=pass+chk("big lookup 30000" as *u8, db_lookup(binbig, 30000, 1000), 30000)
100 tot=tot+1; pass=pass+chk("big lookup 59999" as *u8, db_lookup(binbig, 59999, 1000), 59999)
101 tot=tot+1; pass=pass+chk("big lookup 60000 oob" as *u8, db_lookup(binbig, 60000, 1000), 0-1)
102
103 gp_p("RESULT " as *u8); gp_n(pass); gp_p("/" as *u8); gp_n(tot); gp_p(" passed\n" as *u8)
104 if pass == tot { gp_p("GATE GREEN\n" as *u8); return 0 }
105 gp_p("GATE RED\n" as *u8); return 1
106}