code wiki / _hdl_build / nx_palmdoc_gate.nx
nx_palmdoc_gate.nx source
↩ module page · 47 lines · 2963 B
1// nx_palmdoc_gate.nx -- de-risk the PalmDOC codec (the MOBI text compression) BEFORE the fixture/decoder build on
2// it. Round-trips a string with REPEATS (exercises 0x80-0xBF back-references) + UTF-8 high bytes (exercises the
3// 0x01 literal-escape path) and asserts compress->decompress is BYTE-IDENTICAL + actually compresses. expect_exit:0
4import "nx_palmdoc.nx"
5import "nx_syscalls.nx"
6import "nx_gate_verdict.nx"
7
8func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func gn(v0: i64) -> i64 { var v: i64=v0; if v<0 { sys_write(1,"-" as *u8,1); v=0-v } let b: *u8=sys_mmap(24); var k: i64=0; if v==0 {b[0]=48 as u8;k=1} while v>0 {b[k]=(48+(v%10)) as u8; v=v/10; k=k+1} let o: *u8=sys_mmap(24); var j: i64=0; while j<k {o[j]=b[k-1-j];j=j+1} sys_write(1,o,k); return 0 }
10func gslen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
11
12func main() -> i64 {
13 gp("=== nx_palmdoc_gate: PalmDOC (MOBI) codec round-trip ===\n" as *u8)
14 let src: *u8 = "Liberate the book. Liberate the book. Liberate the book on your own device. caf\xc3\xa9 r\xc3\xa9sum\xc3\xa9 \xe2\x80\x94 na\xc3\xafve reading, sovereign and DRM-free." as *u8
15 let sn: i64 = gslen(src)
16
17 let cbuf: *u8 = sys_mmap(65536)
18 let dbuf: *u8 = sys_mmap(65536)
19 let cn: i64 = palmdoc_compress(src, sn, cbuf, 65536)
20 let dn: i64 = palmdoc_decompress(cbuf, cn, dbuf, 65536)
21
22 var ident: i64 = 1
23 if dn != sn { ident = 0 } else { var i: i64=0; while i<sn { if dbuf[i]!=src[i] { ident=0; i=sn } else { i=i+1 } } }
24 // proof the back-ref path actually fired (the 3x repeat must compress)
25 var shrank: i64 = 0
26 if cn < sn { shrank = 1 }
27
28 gp(" src_len=" as *u8); gn(sn); gp(" compressed=" as *u8); gn(cn); gp(" decompressed=" as *u8); gn(dn)
29 gp(" identical=" as *u8); gn(ident); gp(" shrank=" as *u8); gn(shrank); gp("\n" as *u8)
30 gp(" round-trip text: " as *u8); sys_write(1, dbuf, dn); gp("\n" as *u8)
31
32 var pass: i64 = 0; var fail: i64 = 0
33 if ident == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not-byte-identical (codec bug)\n" as *u8) }
34 if dn == sn { pass=pass+1 } else { fail=fail+1; gp(" FAIL length-mismatch\n" as *u8) }
35 if shrank == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL did-not-compress (back-ref path never fired)\n" as *u8) }
36
37 gp("PALMDOC-GATE pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail)
38 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
39 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
40 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
41 let ctr__dry: *i64 = gv_ctr()
42 ctr__dry[0] = pass
43 ctr__dry[1] = pass + fail
44 let rc__dry: i64 = gv_verdict("PALMDOC-GATE" as *u8, ctr__dry, "PalmDOC codec round-trips byte-exact; back-references work)" as *u8)
45 sys_exit(rc__dry)
46 return rc__dry
47}