code wiki / _hdl_build / nx_eco_graph_gate.nx
nx_eco_graph_gate.nx source
↩ module page · 84 lines · 5091 B
1// nx_eco_graph_gate.nx -- proves the R0 graph query engine (roots-to-god / children / coupling / orphan) on a
2// synthetic KNOWN graph (diamond + orphan + isolated island), deterministically, with a NEG-CONTROL (the
3// isolated node reaches nothing = the traversal is real, not returning everything). license_tier: ORIGINAL expect_exit: 0
4import "nx_syscalls.nx"
5import "nx_eco_graph.nx"
6import "nx_gate_verdict.nx"
7
8func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func pn(v: i64) -> i64 { let b: *u8=sys_mmap(24); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(24); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var j: i64=0; while j<k{b[j]=t[k-1-j];j=j+1} sys_write(1,b,k); return 0 }
10func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
11func gi(g: *EcoGraph, s: *u8) -> i64 { return eg_intern(g, s, slen(s)) }
12func zero(v: *u8, n: i64) -> i64 { var i: i64=0; while i<n { v[i]=0 as u8; i=i+1 } return 0 }
13
14func main() -> i64 {
15 hw("=== nx_eco_graph_gate -- roots-to-god / children / coupling / orphan on a known graph ===\n" as *u8)
16 let g: *EcoGraph = eg_new(64, 256, 8192, 128)
17 // nodes: god(leaf) <- a,b,orph ; top -> a,b (diamond) ; island isolated
18 let god: i64 = gi(g, "nx_god.nx" as *u8)
19 let a: i64 = gi(g, "nx_a.nx" as *u8)
20 let b: i64 = gi(g, "nx_b.nx" as *u8)
21 let top: i64 = gi(g, "nx_top.nx" as *u8)
22 let orph: i64 = gi(g, "nx_orph.nx" as *u8)
23 let isl: i64 = gi(g, "nx_island.nx" as *u8)
24 let god2: i64 = gi(g, "nx_god.nx" as *u8) // dedup probe
25 eg_add_edge(g, a, god)
26 eg_add_edge(g, b, god)
27 eg_add_edge(g, top, a)
28 eg_add_edge(g, top, b)
29 eg_add_edge(g, orph, god)
30
31 let vis: *u8 = sys_mmap(256)
32 let out: *i64 = sys_mmap(256*8) as *i64
33 let on: *i64 = sys_mmap(8) as *i64
34 var fails: i64 = 0
35
36 // T0: intern dedup + node count
37 var t0: i64 = 0
38 if god2 == god { if g.node_count == 6 { t0 = 1 } }
39 if t0 == 1 { hw("T0 PASS intern dedup (god2==god) + node_count=6\n" as *u8) } else { fails=fails+1; hw("T0 FAIL dedup/count (nc="); pn(g.node_count); hw(")\n" as *u8) }
40
41 // T1: coupling -- Ce(top)=2 imports, Ca(god)=3 importers
42 let cetop: i64 = eg_ce(g, top)
43 let cagod: i64 = eg_ca(g, god)
44 var t1: i64 = 0
45 if cetop == 2 { if cagod == 3 { t1 = 1 } }
46 if t1 == 1 { hw("T1 PASS coupling Ce(top)=2 Ca(god)=3\n" as *u8) } else { fails=fails+1; hw("T1 FAIL Ce(top)="); pn(cetop); hw(" Ca(god)="); pn(cagod); hw("\n" as *u8) }
47
48 // T2: roots-to-god(top) = transitive ancestors {a,b,god} = 3
49 zero(vis, 256); on[0] = 0; vis[top] = 1 as u8
50 eg_ancestors(g, top, vis, out, on, 256)
51 var t2: i64 = 0
52 if on[0] == 3 { if vis[god]==(1 as u8) { if vis[a]==(1 as u8) { if vis[b]==(1 as u8) { t2 = 1 } } } }
53 if t2 == 1 { hw("T2 PASS roots-to-god(top) = {a,b,god} (3 transitive ancestors incl god)\n" as *u8) } else { fails=fails+1; hw("T2 FAIL ancestors(top)="); pn(on[0]); hw("\n" as *u8) }
54
55 // T3: children-to-newest(god) = transitive descendants {a,b,top,orph} = 4
56 zero(vis, 256); on[0] = 0; vis[god] = 1 as u8
57 eg_descendants(g, god, vis, out, on, 256)
58 var t3: i64 = 0
59 if on[0] == 4 { if vis[top]==(1 as u8) { if vis[orph]==(1 as u8) { t3 = 1 } } }
60 if t3 == 1 { hw("T3 PASS children(god) = {a,b,top,orph} (4 transitive descendants)\n" as *u8) } else { fails=fails+1; hw("T3 FAIL descendants(god)="); pn(on[0]); hw("\n" as *u8) }
61
62 // T4 NEG-CONTROL: the isolated island reaches NOTHING both ways + Ca=Ce=0 (traversal is real, not all-nodes)
63 zero(vis, 256); on[0] = 0; vis[isl] = 1 as u8
64 eg_ancestors(g, isl, vis, out, on, 256)
65 let anc_isl: i64 = on[0]
66 zero(vis, 256); on[0] = 0; vis[isl] = 1 as u8
67 eg_descendants(g, isl, vis, out, on, 256)
68 let desc_isl: i64 = on[0]
69 var t4: i64 = 0
70 if anc_isl == 0 { if desc_isl == 0 { if eg_ca(g, isl) == 0 { if eg_ce(g, isl) == 0 { t4 = 1 } } } }
71 if t4 == 1 { hw("T4 PASS neg-control: isolated island reaches 0 up + 0 down, Ca=Ce=0\n" as *u8) } else { fails=fails+1; hw("T4 FAIL island anc="); pn(anc_isl); hw(" desc="); pn(desc_isl); hw("\n" as *u8) }
72
73 // T5: orphan = Ca==0 (orph is imported by nothing)
74 var t5: i64 = 0
75 if eg_ca(g, orph) == 0 { if eg_ca(g, god) > 0 { t5 = 1 } }
76 if t5 == 1 { hw("T5 PASS orphan detect: Ca(orph)=0 (candidate), Ca(god)>0 (not)\n" as *u8) } else { fails=fails+1; hw("T5 FAIL orphan\n" as *u8) }
77
78 if fails == 0 { hw("NX-ECO-GRAPH GREEN -- roots-to-god + children + coupling + orphan queries correct (the living-graph engine; brick-3 = live whole-tree walk)\n" as *u8) } else { hw("NX-ECO-GRAPH RED fails=" as *u8); pn(fails); hw("\n" as *u8) }
79 let ctr: *i64 = gv_ctr()
80 var gok: i64 = 0
81 if fails == 0 { gok = 1 }
82 gv_check("T-ALL graph query engine correct on the known graph incl island neg-control" as *u8, gok, ctr)
83 return gv_verdict("ECO-GRAPH-GATE" as *u8, ctr, "roots-to-god, children, coupling, orphan queries proven on a known graph" as *u8)
84}