nx_itoa_lib_gate.nx source
↩ module page · 105 lines · 4530 B
1// nx_itoa_lib_gate.nx -- gate for the shared integer->decimal emitter (nx_itoa_lib).
2// Authored ON nx_gate_verdict per the D001 migrate-on-touch law: a new gate imports THE lib.
3//
4// WHAT IT PROVES: ccz_cat_num is byte-identical across every boundary the ~87 hand-rolled clones it
5// replaces got right only by accident -- zero, single digit, the 9-to-10 and 99-to-100 carries,
6// interior zeros (100, 1024, which a sloppy reverse drops), negatives, and the full 19-digit range.
7// If this is not byte-identical the migration silently corrupts every number the ecosystem prints,
8// so the teeth are on OUTPUT BYTES, never on "it returned".
9//
10// T13 is the one the clones could not do at all: appending at a NON-ZERO offset without disturbing
11// what is already in the buffer. That property is what makes the zero-allocation form possible, and
12// it is why the fd shim (nxi_fd) needed to exist separately instead of being baked into the emitter.
13//
14// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
15import "nx_syscalls.nx"
16import "nx_gate_verdict.nx"
17import "nx_itoa_lib.nx"
18
19const IG_BUF: i64 = 64
20const IG_X: i64 = 120
21const IG_FOUR: i64 = 52
22const IG_TWO: i64 = 50
23const IG_ZERO: i64 = 48
24const IG_SEVEN: i64 = 55
25const IG_SENT: i64 = 88
26
27// buf[0..n) equals the NUL-terminated expect, and expect has no extra tail
28func ig_eq(buf: *u8, n: i64, expect: *u8) -> i64 {
29 var i: i64 = 0
30 while i < n {
31 if expect[i] == (0 as u8) { return 0 }
32 if buf[i] != expect[i] { return 0 }
33 i = i + 1
34 }
35 if expect[n] != (0 as u8) { return 0 }
36 return 1
37}
38
39func ig_case(name: *u8, v: i64, expect: *u8, ctr: *i64) -> i64 {
40 let b: *u8 = sys_mmap(IG_BUF)
41 let n: i64 = ccz_cat_num(b, 0, v)
42 let ok: i64 = ig_eq(b, n, expect)
43 gv_check(name, ok, ctr)
44 sys_munmap(b, IG_BUF)
45 return ok
46}
47
48func main(argc: i64, argv: *i64) -> i64 {
49 gv_head("nx_itoa_lib -- shared integer->decimal emitter; MSB-first, zero-allocation buffer form")
50 let ctr: *i64 = gv_ctr()
51
52 ig_case("T1 zero emits 0 not empty", 0, "0" as *u8, ctr)
53 ig_case("T2 single digit", 7, "7" as *u8, ctr)
54 ig_case("T3 nine (just below carry)", 9, "9" as *u8, ctr)
55 ig_case("T4 ten (first carry)", 10, "10" as *u8, ctr)
56 ig_case("T5 ninety-nine (double carry edge)", 99, "99" as *u8, ctr)
57 ig_case("T6 hundred (interior zeros survive)", 100, "100" as *u8, ctr)
58 ig_case("T7 mixed digits", 123, "123" as *u8, ctr)
59 ig_case("T8 interior zero not dropped", 1024, "1024" as *u8, ctr)
60 ig_case("T9 negative", 0 - 123, "-123" as *u8, ctr)
61 ig_case("T10 negative single digit", 0 - 7, "-7" as *u8, ctr)
62 ig_case("T11 real debt id (10 digits)", 1785516350, "1785516350" as *u8, ctr)
63 ig_case("T12 full 19-digit i64 range", 9223372036854775807, "9223372036854775807" as *u8, ctr)
64
65 // Offset append: must start exactly at off, leave earlier bytes untouched, return off+len.
66 let ob: *u8 = sys_mmap(IG_BUF)
67 ob[0] = IG_X as u8
68 let end: i64 = ccz_cat_num(ob, 1, 42)
69 var offok: i64 = 0
70 if end == 3 {
71 if ob[0] == (IG_X as u8) {
72 if ob[1] == (IG_FOUR as u8) {
73 if ob[2] == (IG_TWO as u8) { offok = 1 }
74 }
75 }
76 }
77 gv_check("T13 offset append preserves prefix and returns off+len", offok, ctr)
78 sys_munmap(ob, IG_BUF)
79
80 // T14/T15 cover nxi_buf, the NUL-FREE contract. T15 is the entire reason it exists: a sentinel
81 // byte sitting immediately past the returned offset MUST survive. ccz_cat_num would write a NUL
82 // there, which is why the clone family whose callers patch numbers into the middle of a buffer
83 // cannot simply be pointed at it. If T15 ever passes vacuously, prefill is the thing to check.
84 let sb: *u8 = sys_mmap(IG_BUF)
85 var z: i64 = 0
86 while z < IG_BUF { sb[z] = IG_SENT as u8; z = z + 1 }
87 let bend: i64 = nxi_buf(sb, 2, 407)
88 var bok: i64 = 0
89 if bend == 5 {
90 if sb[2] == (IG_FOUR as u8) {
91 if sb[3] == (IG_ZERO as u8) {
92 if sb[4] == (IG_SEVEN as u8) { bok = 1 }
93 }
94 }
95 }
96 gv_check("T14 nxi_buf appends at offset, interior zero kept, returns off+len", bok, ctr)
97 var nok: i64 = 0
98 if sb[bend] == (IG_SENT as u8) { nok = 1 }
99 gv_check("T15 nxi_buf writes NO byte past the returned offset (no NUL)", nok, ctr)
100 sys_munmap(sb, IG_BUF)
101
102 let rc: i64 = gv_verdict("ITOA-LIB", ctr, "MSB-first emission byte-identical to the clones and allocates nothing")
103 sys_exit(rc)
104 return rc
105}