code wiki / _hdl_build / nx_entity_store_gate.nx
nx_entity_store_gate.nx source
↩ module page · 211 lines · 8946 B
1// nx_entity_store_gate.nx -- CERTIFICATION of nx_entity_store (gamebench gap-queue rank 1, blocks 7 titles).
2// NOTE: distinct from the pre-existing nx_entity_gate.nx, which is a voxel-world RENDER demo (5 creatures
3// wandering per frame). This gate certifies the handle/generation ENTITY STORE. Nothing shared, no clobber.
4// T1 spawn/destroy/count basics
5// T2 ★STALE HANDLE REJECTED -- destroy an entity, respawn into the SAME slot, old handle must be invalid
6// T2b SLOT REUSE CONFIRMED -- proves T2 hit the real hazard rather than a fresh slot (which passes for free)
7// T4 dense iteration visits exactly the alive set: no holes, no duplicates, no dead entities
8// T5 REPLAY-DETERMINISM -- same op sequence twice -> identical handles and identical iteration order
9// T6 capacity bounded -- spawning past cap returns null and leaves the arena intact
10// T7 components per-entity; a recycled slot leaks nothing from its previous occupant
11// T8 ★ANTI-VACUITY -- valid handles must be ACCEPTED (an always-false en_valid would ace T2)
12// T9 composes with nx_gamesave: entity component data round-trips through save/load
13// license_tier: ORIGINAL expect_exit: 0
14import "nx_syscalls.nx"
15import "nx_entity_store.nx"
16import "nx_gamesave.nx"
17
18func ew(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
19func ep(v: i64) -> i64 {
20 if v==0 { sys_write(1,"0" as *u8,1); return 0 }
21 var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m }
22 let t: *u8=sys_mmap(32); var k: i64=0
23 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
24 let o: *u8=sys_mmap(32); var q: i64=k-1; var i: i64=0
25 while q>=0 { o[i]=t[q]; i=i+1; q=q-1 }
26 sys_write(1,o,i); return 0
27}
28// a fixed op sequence, run twice for T5
29func replay(a: *i64, hs: *i64, cap: i64, ncomp: i64) -> i64 {
30 en_init(a, cap, ncomp)
31 var i: i64 = 0
32 while i < 10 { hs[i] = en_spawn(a); en_set(a, hs[i], 0, i*7); i = i + 1 }
33 en_destroy(a, hs[3])
34 en_destroy(a, hs[7])
35 hs[10] = en_spawn(a)
36 hs[11] = en_spawn(a)
37 en_destroy(a, hs[0])
38 hs[12] = en_spawn(a)
39 return 0
40}
41
42func main() -> i64 {
43 ew("=== nx_entity_store_gate: is the entity store safe to compose into every game? ===\n\n")
44 var pass: i64 = 0
45 var checks: i64 = 0
46 let CAP: i64 = 64
47 let NC: i64 = 3
48 let a: *i64 = sys_mmap(en_bytes(CAP, NC)) as *i64
49
50 // ---------- T1 ----------
51 checks = checks + 1
52 en_init(a, CAP, NC)
53 let h1: i64 = en_spawn(a)
54 let h2: i64 = en_spawn(a)
55 let h3: i64 = en_spawn(a)
56 var t1: i64 = 0
57 if en_count(a)==3 { if h1!=EN_NULL { if h1!=h2 { if h2!=h3 {
58 en_destroy(a, h2)
59 if en_count(a)==2 { if en_valid(a,h2)==0 { if en_valid(a,h1)==1 { t1=1 } } }
60 } } } }
61 if t1==1 { ew("T1 GREEN spawn/destroy/count: 3 spawned, 1 destroyed, count=2, survivors valid\n"); pass=pass+1 }
62 else { ew("T1 RED basics failed, count="); ep(en_count(a)); ew("\n") }
63
64 // ---------- T2 stale handle ----------
65 checks = checks + 1
66 en_init(a, CAP, NC)
67 let victim: i64 = en_spawn(a)
68 let vidx: i64 = en_handle_idx(victim)
69 en_destroy(a, victim)
70 let recycled: i64 = en_spawn(a)
71 let ridx: i64 = en_handle_idx(recycled)
72 var reused: i64 = 0
73 if ridx == vidx { reused = 1 }
74 var t2: i64 = 0
75 if en_valid(a, victim)==0 { if en_valid(a, recycled)==1 { if victim != recycled { t2=1 } } }
76 if t2==1 {
77 ew("T2 GREEN stale handle REJECTED: old handle "); ep(victim)
78 ew(" invalid after its slot recycled into "); ep(recycled); ew("\n"); pass=pass+1
79 } else { ew("T2 RED stale handle still validates -- the core hazard is live\n") }
80
81 // ---------- T2b slot reuse actually occurred ----------
82 checks = checks + 1
83 if reused==1 {
84 ew("T2b GREEN slot reuse CONFIRMED (both handles used slot "); ep(vidx)
85 ew(") -- T2 exercised the real hazard\n"); pass=pass+1
86 } else { ew("T2b RED slot was NOT reused -- T2 proved nothing\n") }
87
88 // ---------- T4 dense iteration ----------
89 checks = checks + 1
90 en_init(a, CAP, NC)
91 let hs: *i64 = sys_mmap(32*8) as *i64
92 var i: i64 = 0
93 while i < 12 { hs[i] = en_spawn(a); i = i + 1 }
94 en_destroy(a, hs[2]); en_destroy(a, hs[5]); en_destroy(a, hs[11])
95 var seen: i64 = 0
96 var dup: i64 = 0
97 var deadseen: i64 = 0
98 let mark: *i64 = sys_mmap(CAP*8) as *i64
99 i = 0
100 while i < CAP { mark[i]=0; i=i+1 }
101 var k: i64 = 0
102 while k < en_count(a) {
103 let h: i64 = en_nth(a, k)
104 if en_valid(a,h)==0 { deadseen = deadseen + 1 }
105 let ix: i64 = en_handle_idx(h)
106 if mark[ix]==1 { dup = dup + 1 }
107 mark[ix]=1
108 seen = seen + 1
109 k = k + 1
110 }
111 var t4: i64 = 0
112 if seen==9 { if dup==0 { if deadseen==0 { t4=1 } } }
113 if t4==1 {
114 ew("T4 GREEN dense iteration: 12 spawned - 3 destroyed = 9 visited, 0 duplicates, 0 dead\n"); pass=pass+1
115 } else {
116 ew("T4 RED visited="); ep(seen); ew(" dup="); ep(dup); ew(" dead="); ep(deadseen); ew(" (expected 9/0/0)\n")
117 }
118
119 // ---------- T5 replay determinism ----------
120 checks = checks + 1
121 let b1: *i64 = sys_mmap(en_bytes(CAP,NC)) as *i64
122 let b2: *i64 = sys_mmap(en_bytes(CAP,NC)) as *i64
123 let r1: *i64 = sys_mmap(32*8) as *i64
124 let r2: *i64 = sys_mmap(32*8) as *i64
125 replay(b1, r1, CAP, NC)
126 replay(b2, r2, CAP, NC)
127 var det: i64 = 1
128 i = 0
129 while i < 13 { if r1[i]!=r2[i] { det=0 } i=i+1 }
130 if en_count(b1)!=en_count(b2) { det=0 }
131 k = 0
132 while k < en_count(b1) { if en_nth(b1,k)!=en_nth(b2,k) { det=0 } k=k+1 }
133 if det==1 {
134 ew("T5 GREEN replay-deterministic: identical ops -> identical handles AND iteration order (")
135 ep(en_count(b1)); ew(" alive)\n"); pass=pass+1
136 } else { ew("T5 RED replay diverged -- recorded input would not reproduce\n") }
137
138 // ---------- T6 capacity bounded ----------
139 checks = checks + 1
140 let sm: *i64 = sys_mmap(en_bytes(4,2)) as *i64
141 en_init(sm, 4, 2)
142 let e1: i64 = en_spawn(sm); en_spawn(sm); en_spawn(sm)
143 let e4: i64 = en_spawn(sm)
144 let e5: i64 = en_spawn(sm)
145 var t6: i64 = 0
146 if e5==EN_NULL { if en_count(sm)==4 { if en_valid(sm,e1)==1 { if en_valid(sm,e4)==1 { t6=1 } } } }
147 if t6==1 {
148 ew("T6 GREEN capacity bounded: 5th spawn into cap=4 returned null, arena intact (count=4)\n"); pass=pass+1
149 } else { ew("T6 RED capacity breach, count="); ep(en_count(sm)); ew(" e5="); ep(e5); ew("\n") }
150
151 // ---------- T7 component isolation / no recycle leak ----------
152 checks = checks + 1
153 en_init(a, CAP, NC)
154 let p1: i64 = en_spawn(a); let p2: i64 = en_spawn(a)
155 en_set(a,p1,0,111); en_set(a,p1,1,222); en_set(a,p2,0,333)
156 var t7: i64 = 0
157 if en_get(a,p1,0)==111 { if en_get(a,p1,1)==222 { if en_get(a,p2,0)==333 { if en_get(a,p2,1)==0 {
158 en_destroy(a,p1)
159 let p3: i64 = en_spawn(a)
160 if en_get(a,p3,0)==0 { if en_get(a,p3,1)==0 { t7=1 } }
161 } } } }
162 if t7==1 {
163 ew("T7 GREEN components per-entity; recycled slot leaks nothing (fresh entity reads 0)\n"); pass=pass+1
164 } else { ew("T7 RED component isolation or recycle-leak failure\n") }
165
166 // ---------- T8 anti-vacuity ----------
167 checks = checks + 1
168 en_init(a, CAP, NC)
169 var okc: i64 = 0
170 i = 0
171 while i < 20 { let h: i64 = en_spawn(a); if en_valid(a,h)==1 { okc = okc + 1 } i = i + 1 }
172 if okc==20 {
173 ew("T8 GREEN anti-vacuity: 20/20 fresh handles ACCEPTED -- en_valid is selective, not always-false\n")
174 pass=pass+1
175 } else { ew("T8 RED only "); ep(okc); ew("/20 accepted -- T2 would be vacuous\n") }
176
177 // ---------- T9 composes with nx_gamesave ----------
178 checks = checks + 1
179 let EP: *u8 = "knowledge/nx_entity_world.sav" as *u8
180 sys_unlinkat(EP)
181 en_init(a, CAP, NC)
182 let NE: i64 = 6
183 let ents: *i64 = sys_mmap(NE*8) as *i64
184 i = 0
185 while i < NE { ents[i] = en_spawn(a); en_set(a, ents[i], 0, 1000+i); en_set(a, ents[i], 1, i*i); i = i + 1 }
186 let vec: *i64 = sys_mmap(NE*2*8) as *i64
187 k = 0
188 while k < en_count(a) {
189 let h: i64 = en_nth(a,k)
190 vec[k*2] = en_get(a,h,0)
191 vec[k*2+1] = en_get(a,h,1)
192 k = k + 1
193 }
194 let wrote: i64 = gs_save(EP, 31337, vec, NE*2, 1784578500)
195 let rd: *i64 = sys_mmap(NE*2*8) as *i64
196 let got: i64 = gs_load(EP, rd, NE*2, 0 as *i64)
197 var t9: i64 = 1
198 if got != NE*2 { t9 = 0 }
199 if wrote <= 0 { t9 = 0 }
200 i = 0
201 while i < NE*2 { if rd[i]!=vec[i] { t9=0 } i=i+1 }
202 if t9==1 {
203 ew("T9 GREEN composes with nx_gamesave: "); ep(NE); ew(" entities x2 components round-tripped (")
204 ep(wrote); ew("B)\n"); pass=pass+1
205 } else { ew("T9 RED entity world did not survive save/load, rc="); ep(got); ew("\n") }
206
207 ew("\n=== nx_entity_store_gate "); ep(pass); ew("/"); ep(checks)
208 if pass == checks { ew(" GREEN ===\n"); return 0 }
209 ew(" RED ===\n")
210 return 1
211}