code wiki / _hdl_build / nx_reftest_gate.nx
nx_reftest_gate.nx source
↩ module page · 100 lines · 6046 B
1// nx_reftest_gate.nx -- the VALIDATION SPINE: a sovereign WPT-style REFTEST runner + its gate. A reftest
2// renders a TEST page and a REFERENCE page that the SPEC says must look identical (via different CSS
3// mechanisms), then PIXEL-COMPARES the two renders: 0 diff => the engine is spec-consistent => PASS. This
4// is the externally-defined conformance mechanism WPT itself uses for CSS/layout (the half that needs no
5// JS). Seeded here with spec-equivalent pairs (tag==class selector; margin-left == padding-on-wrapper
6// offset); REAL WPT reftest .html files drop into the SAME `reftest()` runner. A deliberate MISMATCH pair
7// (blue vs red) is the LIAR-KILL: the comparator must report them DIFFERENT, proving it has teeth. The
8// pass-% is the WPT-style metric. 100% sovereign (no Chrome/headless/gcc). license_tier: ORIGINAL
9import "nx_syscalls.nx"
10import "nx_render_html.nx"
11
12func g_w(s: *u8) -> i64 { var k: i64=0; while s[k]!=(0 as u8){k=k+1} sys_write(1,s,k); return 0 }
13func pn(v0: i64) -> i64 { var v: i64=v0; if v<0 { let m: *u8=sys_mmap(8); m[0]=45 as u8; sys_write(1,m,1); v=0-v } let b: *u8=sys_mmap(24); var k: i64=0; if v==0 {b[0]=48;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 }
14func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
15func u32le(d: *u8, p: i64) -> i64 { return (d[p]&0xff) + ((d[p+1]&0xff)<<8) + ((d[p+2]&0xff)<<16) + ((d[p+3]&0xff)<<24) }
16
17// Count differing PIXELS between two BMP buffers. Mismatched dimensions => huge diff (a fail/mismatch).
18func bmp_diff(a: *u8, b: *u8) -> i64 {
19 let wa: i64=u32le(a,18); let ha: i64=u32le(a,22); let oa: i64=u32le(a,10)
20 let wb: i64=u32le(b,18); let hb: i64=u32le(b,22); let ob: i64=u32le(b,10)
21 if wa != wb { return 999999 }
22 if ha != hb { return 999999 }
23 let row3: i64 = wa*3
24 let pad: i64 = (4-(row3%4))%4
25 let rowstride: i64 = row3+pad
26 var diff: i64 = 0
27 var y: i64 = 0
28 while y < ha {
29 var x: i64 = 0
30 let rba: i64 = oa + y*rowstride
31 let rbb: i64 = ob + y*rowstride
32 while x < wa {
33 let pa: i64 = rba + x*3
34 let pb: i64 = rbb + x*3
35 var d: i64 = 0
36 if (a[pa]&0xff) != (b[pb]&0xff) { d=1 }
37 if (a[pa+1]&0xff) != (b[pb+1]&0xff) { d=1 }
38 if (a[pa+2]&0xff) != (b[pb+2]&0xff) { d=1 }
39 if d==1 { diff = diff + 1 }
40 x = x+1
41 }
42 y = y+1
43 }
44 return diff
45}
46
47// Run ONE reftest: render test + ref, return the differing-pixel count. The reusable WPT-reftest mechanism.
48func reftest(test_html: *u8, ref_html: *u8) -> i64 {
49 nx_render_html_to_bmp(test_html, slen(test_html), "knowledge/status/reftest_t.bmp\x00" as *u8)
50 nx_render_html_to_bmp(ref_html, slen(ref_html), "knowledge/status/reftest_r.bmp\x00" as *u8)
51 let lp: *i64 = sys_mmap(8) as *i64
52 let a: *u8 = sys_read_file("knowledge/status/reftest_t.bmp\x00" as *u8, lp); let la: i64 = lp[0]
53 let b: *u8 = sys_read_file("knowledge/status/reftest_r.bmp\x00" as *u8, lp); let lb: i64 = lp[0]
54 if la < 54 { return 999999 }
55 if lb < 54 { return 999999 }
56 return bmp_diff(a, b)
57}
58func chk(cond: i64, label: *u8, pass: *i64) -> i64 {
59 g_w(" " as *u8); g_w(label); g_w(": " as *u8)
60 if cond==1 { g_w("OK\n" as *u8); pass[0]=pass[0]+1 } else { g_w("FAIL\n" as *u8) }
61 return cond
62}
63
64func main() -> i64 {
65 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0
66 g_w("=== REFTEST GATE (sovereign WPT-style reftest runner, pixel-compare) ===\n" as *u8)
67
68 // R1 (expect MATCH): a tag selector and a class selector that target the same element must render the
69 // same box. test: div{...}; ref: .x{...} on <div class=x>. Spec-equivalent.
70 let r1t: *u8 = "<body><style>div{background-color:#1040c0;height:40px}</style><div></div></body>\x00" as *u8
71 let r1r: *u8 = "<body><style>.x{background-color:#1040c0;height:40px}</style><div class=\"x\"></div></body>\x00" as *u8
72 let d1: i64 = reftest(r1t, r1r)
73 g_w("R1 tag==class selector (expect MATCH): diff=" as *u8); pn(d1); g_w("\n" as *u8)
74 chk((d1 == 0) as i64, "R1 renders IDENTICAL (selector equivalence)\x00" as *u8, pass)
75
76 // R2 (expect MATCH): margin-left on a box == padding-left on a wrapper, for offsetting a child by 40px.
77 let r2t: *u8 = "<body><style>.b{margin-left:40px;width:80px;height:30px;background-color:#c02020}</style><div class=\"b\"></div></body>\x00" as *u8
78 let r2r: *u8 = "<body><style>.w{padding-left:40px}.b{width:80px;height:30px;background-color:#c02020}</style><div class=\"w\"><div class=\"b\"></div></div></body>\x00" as *u8
79 let d2: i64 = reftest(r2t, r2r)
80 g_w("R2 margin-left == padding-on-wrapper (expect MATCH): diff=" as *u8); pn(d2); g_w("\n" as *u8)
81 chk((d2 == 0) as i64, "R2 renders IDENTICAL (margin/padding offset consistency)\x00" as *u8, pass)
82
83 // R3 (expect MISMATCH = LIAR-KILL): blue vs red box MUST differ -> proves the comparator has teeth.
84 let r3t: *u8 = "<body><style>div{background-color:#0000ff;height:40px}</style><div></div></body>\x00" as *u8
85 let r3r: *u8 = "<body><style>div{background-color:#ff0000;height:40px}</style><div></div></body>\x00" as *u8
86 let d3: i64 = reftest(r3t, r3r)
87 g_w("R3 blue vs red (expect MISMATCH): diff=" as *u8); pn(d3); g_w("\n" as *u8)
88 chk((d3 > 1000) as i64, "LIAR-KILL: comparator DETECTS the mismatch (diff>1000)\x00" as *u8, pass)
89
90 // WPT-style pass-% over the MATCH reftests (R1,R2) -- the externally-defined conformance metric.
91 var matched: i64 = 0
92 if d1 == 0 { matched = matched + 1 }
93 if d2 == 0 { matched = matched + 1 }
94 let permil: i64 = (matched * 1000) / 2
95 g_w("REFTEST match-rate=" as *u8); pn(matched); g_w("/2 = " as *u8); pn(permil); g_w(" permil (WPT-style)\n" as *u8)
96
97 g_w("REFTEST rows=3 pass=" as *u8); pn(pass[0])
98 if pass[0]==3 { g_w(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
99 g_w(" verdict=RED\n" as *u8); sys_exit(1); return 1
100}