code wiki / _hdl_build / nx_figure_gate.nx
nx_figure_gate.nx source
↩ module page · 306 lines · 14542 B
1// nx_figure_gate.nx -- IMS Thrust D R2 REFEREE: proves nx_chart (native SVG) + nx_dataset_export
2// (CSV/JSON/TSV) are CORRECT and HONEST, with negative-control TEETH (a green can't be faked).
3//
4// REAL DATASET (not invented): the live wiki's own page-count growth across the IMS rungs --
5// 8 -> 12 -> 13 -> 20 -> 21 pages (the documented arc progression; the corpus is at 21 today and
6// becomes 22 with this very page). Labels = the rung tags, values = the page count at each.
7//
8// CHECKS:
9// (a) CHART well-formed:
10// A1 balanced: exactly one "<svg" and one "</svg>" in the output.
11// A2 bar count: exactly N occurrences of "<rect class=\"bar\"" for N data values.
12// A3 value->height: a known value's bar height equals the INDEPENDENTLY recomputed scaled
13// height vs_linear(0, nice_top, 0, CH_PLOT_H, v) -- read back from the emitted height="..".
14// (b) BYTE-FAITHFUL multi-format export (the round-trip oracle dx_equal):
15// B1 CSV -> parse -> dx_equal(original) (identical records)
16// B2 JSON -> parse -> dx_equal(original)
17// B3 TSV -> parse -> dx_equal(original)
18// B4 CROSS chain: original -> CSV -> ds1 -> JSON -> ds2 -> TSV -> ds3, all == original.
19// B5 ESCAPING TEETH: a cell containing a comma AND a double-quote (`a,"b`) survives the CSV
20// round trip byte-exact AND the CSV bytes actually quote+double it ("a,""b"). A naive
21// emitter that forgot to escape would corrupt the parse -> this check would FAIL.
22// (c) GRACEFUL edges:
23// C1 empty dataset (0 rows): CSV emits header-only (valid), chart emits 0 bars + stays balanced.
24// C2 malformed JSON ("not json") -> dx_parse_json returns a NEGATIVE verdict, no crash.
25//
26// GREEN iff all pass. Evidence -> knowledge/status/figure_gate.log. Sovereign: imports the two organs
27// + nx_framed_append + nx_syscalls. license_tier: ORIGINAL
28import "nx_chart.nx"
29import "nx_dataset_export.nx"
30import "nx_framed_append.nx"
31import "nx_syscalls.nx"
32
33const FG_LOG: *u8 = "knowledge/status/figure_gate.log"
34const FG_CAP: i64 = 512
35
36func fg_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
37func fg_num(v: i64) -> i64 {
38 let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
39 let t: *u8 = sys_mmap(28); var k: i64 = 0
40 if m == 0 { t[0] = 48 as u8; k = 1 }
41 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
42 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
43 sys_write(1, bb, k); return 0
44}
45func fg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != 0 as u8 { dst[off + i] = s[i]; i = i + 1 } return off + i }
46func fg_catn(dst: *u8, off: i64, v: i64) -> i64 {
47 var m: i64 = v; var o: i64 = off
48 if m < 0 { m = 0 - m }
49 let t: *u8 = sys_mmap(28); var k: i64 = 0
50 if m == 0 { t[0] = 48 as u8; k = 1 }
51 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
52 var i: i64 = 0; while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 }
53 return o + k
54}
55
56func fg_row(name: *u8, pass: i64) -> i64 {
57 let buf: *u8 = sys_mmap(FG_CAP + 16)
58 var o: i64 = 0
59 o = fg_cat(buf, o, "FGATE row=" as *u8); o = fg_cat(buf, o, name)
60 if pass == 1 { o = fg_cat(buf, o, " verdict=PASS" as *u8) } else { o = fg_cat(buf, o, " verdict=FAIL" as *u8) }
61 buf[o] = 0 as u8
62 fa_appendz(FG_LOG, buf, FG_CAP)
63 fg_w(" "); fg_w(name)
64 if pass == 1 { fg_w(" PASS\n") } else { fg_w(" FAIL\n") }
65 return 0
66}
67
68// count non-overlapping occurrences of NUL-terminated needle in buf[0..n).
69func fg_count(buf: *u8, n: i64, needle: *u8) -> i64 {
70 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 }
71 if nl == 0 { return 0 }
72 var cnt: i64 = 0
73 var i: i64 = 0
74 while i + nl <= n {
75 var j: i64 = 0
76 var hit: i64 = 1
77 while j < nl { if buf[i + j] != needle[j] { hit = 0; j = nl } else { j = j + 1 } }
78 if hit == 1 { cnt = cnt + 1; i = i + nl } else { i = i + 1 }
79 }
80 return cnt
81}
82
83// offset of the first occurrence of NUL-terminated needle in buf[0..n); -1 if none.
84func fg_find(buf: *u8, n: i64, needle: *u8) -> i64 {
85 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 }
86 if nl == 0 { return 0 - 1 }
87 var i: i64 = 0
88 while i + nl <= n {
89 var j: i64 = 0; var hit: i64 = 1
90 while j < nl { if buf[i + j] != needle[j] { hit = 0; j = nl } else { j = j + 1 } }
91 if hit == 1 { return i }
92 i = i + 1
93 }
94 return 0 - 1
95}
96
97// find the first 'height="<N>"' integer in buf[0..n) AT or AFTER offset 'from'; -1 if none.
98// NOTE: the <svg> root element ALSO carries height="..", so callers pass from = offset of the first
99// "<rect class=\"bar\"" so we read the BAR's height, not the canvas height.
100func fg_first_height(buf: *u8, n: i64, from: i64) -> i64 {
101 let key: *u8 = "height=\"" as *u8
102 var kl: i64 = 0; while key[kl] != (0 as u8) { kl = kl + 1 }
103 var i: i64 = from
104 while i + kl <= n {
105 var j: i64 = 0; var hit: i64 = 1
106 while j < kl { if buf[i + j] != key[j] { hit = 0; j = kl } else { j = j + 1 } }
107 if hit == 1 {
108 var p: i64 = i + kl
109 var v: i64 = 0
110 var any: i64 = 0
111 while p < n { let c: i64 = buf[p] as i64; if c >= 48 { if c <= 57 { v = v * 10 + (c - 48); any = 1; p = p + 1 } else { p = n } } else { p = n } }
112 if any == 1 { return v }
113 }
114 i = i + 1
115 }
116 return 0 - 1
117}
118
119func main() -> i64 {
120 fg_w("=== nx_figure_gate: native SVG chart + byte-faithful CSV/JSON/TSV export (real dataset = wiki page-count growth) ===\n")
121
122 // ---------- REAL DATASET: wiki page-count growth across IMS rungs ----------
123 // 2 columns: rung, pages. 5 rows.
124 let ds: *NxDataset = sys_mmap(128) as *NxDataset
125 dx_alloc(ds, 2, 5)
126 dx_set_hdr(ds, 0, "rung" as *u8)
127 dx_set_hdr(ds, 1, "pages" as *u8)
128 dx_set_cell_z(ds, 0, 0, "thrustA" as *u8); dx_set_cell_z(ds, 0, 1, "8" as *u8)
129 dx_set_cell_z(ds, 1, 0, "thrustB" as *u8); dx_set_cell_z(ds, 1, 1, "12" as *u8)
130 dx_set_cell_z(ds, 2, 0, "thrustC" as *u8); dx_set_cell_z(ds, 2, 1, "13" as *u8)
131 dx_set_cell_z(ds, 3, 0, "substrate" as *u8);dx_set_cell_z(ds, 3, 1, "20" as *u8)
132 dx_set_cell_z(ds, 4, 0, "figures" as *u8); dx_set_cell_z(ds, 4, 1, "21" as *u8)
133
134 // ---------- CHART over the same values ----------
135 let N: i64 = 5
136 let lp: *i64 = sys_mmap(64) as *i64
137 let ll: *i64 = sys_mmap(64) as *i64
138 let vv: *i64 = sys_mmap(64) as *i64
139 var ci: i64 = 0
140 while ci < N {
141 lp[ci] = dx_cell_ptr(ds, ci, 0) as i64
142 ll[ci] = dx_cell_len(ds, ci, 0)
143 // parse the pages cell to an int for the chart values.
144 let pp: *u8 = dx_cell_ptr(ds, ci, 1)
145 let pn: i64 = dx_cell_len(ds, ci, 1)
146 var v: i64 = 0; var z: i64 = 0
147 while z < pn { v = v * 10 + ((pp[z] as i64) - 48); z = z + 1 }
148 vv[ci] = v
149 ci = ci + 1
150 }
151 let svg: *u8 = sys_mmap(16384)
152 let svgu: *i64 = sys_mmap(16) as *i64
153 let crc: i64 = chart_svg(lp, ll, vv, N, "Nishi wiki page-count growth across IMS rungs" as *u8, svg, 16384, svgu)
154 let svgn: i64 = svgu[0]
155
156 // ===== (a) chart well-formed =====
157 var a1: i64 = 0
158 let n_open: i64 = fg_count(svg, svgn, "<svg" as *u8)
159 let n_close: i64 = fg_count(svg, svgn, "</svg>" as *u8)
160 if crc == CH_OK { if n_open == 1 { if n_close == 1 { a1 = 1 } } }
161
162 var a2: i64 = 0
163 let n_bars: i64 = fg_count(svg, svgn, "<rect class=\"bar\"" as *u8)
164 if n_bars == N { a2 = 1 }
165
166 // A3: first bar value=8 over the INDEPENDENTLY-recomputed domain top, plot_h=300.
167 // ch_nice_top rounds up to the smallest {1,2,5}x10^k >= max(21) -> 50 ; height = 8*300/50 = 48.
168 // We read the bar's height from the FIRST "<rect class=\"bar\"" block (NOT the <svg> canvas
169 // height attribute), and compare to the value scaled by the SAME vs_linear the organ uses.
170 var a3: i64 = 0
171 let exp_top: i64 = ch_nice_top(21) // independently computed domain top (== 50)
172 let exp_h: i64 = vs_linear(0, exp_top, 0, CH_PLOT_H, 8) // independently computed height (== 48)
173 let bar0_off: i64 = fg_find(svg, svgn, "<rect class=\"bar\"" as *u8)
174 var got_h: i64 = 0 - 1
175 if bar0_off >= 0 { got_h = fg_first_height(svg, svgn, bar0_off) }
176 if got_h == exp_h { a3 = 1 }
177
178 fg_row("A1-svg-balanced " as *u8, a1)
179 fg_row("A2-bar-count-eq-N " as *u8, a2)
180 fg_row("A3-value-maps-to-height " as *u8, a3)
181
182 // ===== (b) byte-faithful round trips =====
183 let csv: *u8 = sys_mmap(16384); let cn: i64 = dx_emit_csv(ds, csv, 16384)
184 let jsn: *u8 = sys_mmap(16384); let jn: i64 = dx_emit_json(ds, jsn, 16384)
185 let tsv: *u8 = sys_mmap(16384); let tn: i64 = dx_emit_tsv(ds, tsv, 16384)
186
187 var b1: i64 = 0
188 let dc: *NxDataset = sys_mmap(128) as *NxDataset
189 if cn > 0 { if dx_parse_csv(csv, cn, dc) == DX_OK { if dx_equal(ds, dc) == 1 { b1 = 1 } } }
190
191 var b2: i64 = 0
192 let dj: *NxDataset = sys_mmap(128) as *NxDataset
193 if jn > 0 { if dx_parse_json(jsn, jn, dj) == DX_OK { if dx_equal(ds, dj) == 1 { b2 = 1 } } }
194
195 var b3: i64 = 0
196 let dt: *NxDataset = sys_mmap(128) as *NxDataset
197 if tn > 0 { if dx_parse_tsv(tsv, tn, dt) == DX_OK { if dx_equal(ds, dt) == 1 { b3 = 1 } } }
198
199 // B4 cross-chain: ds -> CSV -> ds1 -> JSON -> ds2 -> TSV -> ds3 ; ds3 == ds.
200 var b4: i64 = 0
201 let ds1: *NxDataset = sys_mmap(128) as *NxDataset
202 let ds2: *NxDataset = sys_mmap(128) as *NxDataset
203 let ds3: *NxDataset = sys_mmap(128) as *NxDataset
204 let buf2: *u8 = sys_mmap(16384)
205 let buf3: *u8 = sys_mmap(16384)
206 if dx_parse_csv(csv, cn, ds1) == DX_OK {
207 let j2: i64 = dx_emit_json(ds1, buf2, 16384)
208 if j2 > 0 { if dx_parse_json(buf2, j2, ds2) == DX_OK {
209 let t3: i64 = dx_emit_tsv(ds2, buf3, 16384)
210 if t3 > 0 { if dx_parse_tsv(buf3, t3, ds3) == DX_OK {
211 if dx_equal(ds, ds3) == 1 { b4 = 1 }
212 } }
213 } }
214 }
215
216 // B5 ESCAPING TEETH: dataset with a nasty cell `a,"b` (comma + quote).
217 var b5: i64 = 0
218 let nasty: *u8 = sys_mmap(16); nasty[0] = 97 as u8; nasty[1] = 44 as u8; nasty[2] = 34 as u8; nasty[3] = 98 as u8; nasty[4] = 0 as u8 // a , " b
219 let dn: *NxDataset = sys_mmap(128) as *NxDataset
220 dx_alloc(dn, 1, 1)
221 dx_set_hdr(dn, 0, "f" as *u8)
222 dx_set_cell(dn, 0, 0, nasty, 4)
223 let ncsv: *u8 = sys_mmap(4096); let ncn: i64 = dx_emit_csv(dn, ncsv, 4096)
224 // the emitted CSV must contain the quoted+doubled form "a,""b"
225 let quoted_form: i64 = fg_count(ncsv, ncn, "\"a,\"\"b\"" as *u8)
226 let dn2: *NxDataset = sys_mmap(128) as *NxDataset
227 var rt_ok: i64 = 0
228 if ncn > 0 { if dx_parse_csv(ncsv, ncn, dn2) == DX_OK { if dx_equal(dn, dn2) == 1 { rt_ok = 1 } } }
229 if quoted_form == 1 { if rt_ok == 1 { b5 = 1 } }
230
231 fg_row("B1-csv-roundtrip-faithful " as *u8, b1)
232 fg_row("B2-json-roundtrip-faithful " as *u8, b2)
233 fg_row("B3-tsv-roundtrip-faithful " as *u8, b3)
234 fg_row("B4-cross-format-chain-eq " as *u8, b4)
235 fg_row("B5-csv-escaping-teeth " as *u8, b5)
236
237 // ===== (c) graceful edges =====
238 // C1 empty dataset (0 rows): CSV header-only valid, chart 0 bars + balanced.
239 var c1: i64 = 0
240 let de: *NxDataset = sys_mmap(128) as *NxDataset
241 dx_alloc(de, 2, 0)
242 dx_set_hdr(de, 0, "rung" as *u8); dx_set_hdr(de, 1, "pages" as *u8)
243 let ecsv: *u8 = sys_mmap(1024); let ecn: i64 = dx_emit_csv(de, ecsv, 1024)
244 let esvg: *u8 = sys_mmap(4096); let esu: *i64 = sys_mmap(16) as *i64
245 let ecrc: i64 = chart_svg(0 as *i64, 0 as *i64, 0 as *i64, 0, "empty" as *u8, esvg, 4096, esu)
246 let e_bars: i64 = fg_count(esvg, esu[0], "<rect class=\"bar\"" as *u8)
247 let e_open: i64 = fg_count(esvg, esu[0], "<svg" as *u8)
248 let e_close: i64 = fg_count(esvg, esu[0], "</svg>" as *u8)
249 // CSV header row present (contains "rung"); no crash; chart balanced + 0 bars.
250 let e_hdr: i64 = fg_count(ecsv, ecn, "rung" as *u8)
251 if ecn > 0 { if e_hdr == 1 { if ecrc == CH_OK { if e_bars == 0 { if e_open == 1 { if e_close == 1 { c1 = 1 } } } } } }
252
253 // C2 malformed JSON -> negative verdict (graceful, no crash).
254 var c2: i64 = 0
255 let bad: *u8 = "not json at all" as *u8
256 var bn: i64 = 0; while bad[bn] != (0 as u8) { bn = bn + 1 }
257 let dbad: *NxDataset = sys_mmap(128) as *NxDataset
258 let badrc: i64 = dx_parse_json(bad, bn, dbad)
259 if badrc < 0 { c2 = 1 }
260
261 fg_row("C1-empty-graceful " as *u8, c1)
262 fg_row("C2-malformed-json-negative " as *u8, c2)
263
264 // ===== tally =====
265 var passes: i64 = 0
266 if a1 == 1 { passes = passes + 1 }
267 if a2 == 1 { passes = passes + 1 }
268 if a3 == 1 { passes = passes + 1 }
269 if b1 == 1 { passes = passes + 1 }
270 if b2 == 1 { passes = passes + 1 }
271 if b3 == 1 { passes = passes + 1 }
272 if b4 == 1 { passes = passes + 1 }
273 if b5 == 1 { passes = passes + 1 }
274 if c1 == 1 { passes = passes + 1 }
275 if c2 == 1 { passes = passes + 1 }
276 let total: i64 = 10
277 var green: i64 = 0
278 if passes == total { green = 1 }
279
280 // evidence detail
281 let eb: *u8 = sys_mmap(FG_CAP + 64); var eo: i64 = 0
282 eo = fg_cat(eb, eo, "FGATE detail svg_bytes=" as *u8); eo = fg_catn(eb, eo, svgn)
283 eo = fg_cat(eb, eo, " bars=" as *u8); eo = fg_catn(eb, eo, n_bars)
284 eo = fg_cat(eb, eo, " nice_top=" as *u8); eo = fg_catn(eb, eo, exp_top)
285 eo = fg_cat(eb, eo, " exp_h=" as *u8); eo = fg_catn(eb, eo, exp_h)
286 eo = fg_cat(eb, eo, " got_h=" as *u8); eo = fg_catn(eb, eo, got_h)
287 eo = fg_cat(eb, eo, " csv_bytes=" as *u8); eo = fg_catn(eb, eo, cn)
288 eo = fg_cat(eb, eo, " json_bytes=" as *u8); eo = fg_catn(eb, eo, jn)
289 eo = fg_cat(eb, eo, " tsv_bytes=" as *u8); eo = fg_catn(eb, eo, tn)
290 eo = fg_cat(eb, eo, " teeth_quoted=" as *u8); eo = fg_catn(eb, eo, quoted_form)
291 eb[eo] = 0 as u8
292 fa_appendz(FG_LOG, eb, FG_CAP)
293 sys_write(1, eb, eo); fg_w("\n")
294
295 let vb: *u8 = sys_mmap(FG_CAP + 16); var o: i64 = 0
296 o = fg_cat(vb, o, "FIGURE-GATE verdict=" as *u8)
297 if green == 1 { o = fg_cat(vb, o, "GREEN" as *u8) } else { o = fg_cat(vb, o, "RED" as *u8) }
298 o = fg_cat(vb, o, " passes=" as *u8); o = fg_catn(vb, o, passes); o = fg_cat(vb, o, "/" as *u8); o = fg_catn(vb, o, total)
299 o = fg_cat(vb, o, " END" as *u8)
300 vb[o] = 0 as u8
301 fa_appendz(FG_LOG, vb, FG_CAP)
302 sys_write(1, vb, o); fg_w("\n")
303
304 if green == 1 { sys_exit(0); return 0 }
305 sys_exit(1); return 1
306}