nx_lib_store_test.nx source
↩ module page · 78 lines · 3000 B
1// nx_lib_store_test.nx -- sovereign gate for the catalog store (SQLite replacement).
2// Proves PARITY (put / get round-trip / absent / enumerate / count / update-in-place)
3// on the durable sovereign seg_store. Exit = failed assertion #; 0 = all pass.
4
5import "nx_syscalls.nx"
6import "nx_lib_store.nx"
7
8func lst_eq(a: *u8, alen: i64, b: *u8, blen: i64) -> i64 {
9 if alen != blen { return 0 }
10 var i: i64 = 0
11 while i < alen { if a[i] != b[i] { return 0 } i = i + 1 }
12 return 1
13}
14
15func lst_has(hay: *u8, hayn: i64, needle: *u8) -> i64 {
16 let nn: i64 = ls_strlen(needle)
17 if nn == 0 { return 1 }
18 if hayn < nn { return 0 }
19 let last: i64 = hayn - nn
20 var i: i64 = 0
21 while i <= last {
22 var j: i64 = 0
23 var st: i64 = 0
24 while st == 0 {
25 if j >= nn { st = 2 }
26 if st == 0 { if hay[i+j] != needle[j] { st = 1 } if st == 0 { j = j + 1 } }
27 }
28 if st == 2 { return 1 }
29 i = i + 1
30 }
31 return 0
32}
33
34func main() -> i64 {
35 let rec1: *u8 = "Sovereign Title One\t10.1/x\t2026\tcc-by" as *u8
36 let rec2: *u8 = "Second Work\t10.2/y\t2025\tcc0" as *u8
37 let r1n: i64 = ls_strlen(rec1)
38 let r2n: i64 = ls_strlen(rec2)
39
40 nx_lib_store_put_work("LIBT1HK" as *u8, rec1, r1n)
41 nx_lib_store_put_work("LIBT2HK" as *u8, rec2, r2n)
42
43 let po: *i64 = sys_mmap(16) as *i64
44 let lo: *i64 = sys_mmap(16) as *i64
45
46 // round-trip parity: get returns exactly what was put
47 if nx_lib_store_get_work("LIBT1HK" as *u8, po, lo) < 0 { return 1 }
48 if lst_eq(po[0] as *u8, lo[0], rec1, r1n) != 1 { return 2 }
49 if nx_lib_store_get_work("LIBT2HK" as *u8, po, lo) < 0 { return 3 }
50 if lst_eq(po[0] as *u8, lo[0], rec2, r2n) != 1 { return 4 }
51
52 // absent key -> rc < 0 (no false hit)
53 if nx_lib_store_get_work("LIBT_NOPE_MISSING" as *u8, po, lo) >= 0 { return 5 }
54
55 // enumeration: the __works__ index carries both keys
56 let idxbuf: *u8 = sys_mmap(1048576)
57 let iln: i64 = nx_lib_store_index(idxbuf)
58 if lst_has(idxbuf, iln, "LIBT1HK" as *u8) != 1 { return 6 }
59 if lst_has(idxbuf, iln, "LIBT2HK" as *u8) != 1 { return 7 }
60
61 // at least the two distinct works are counted
62 if nx_lib_store_count() < 2 { return 8 }
63
64 // UPDATE in place (additive new version) -> get returns the NEW value
65 let rec1b: *u8 = "Sovereign Title One rev2\t10.1/x\t2026\tcc-by-sa" as *u8
66 let r1bn: i64 = ls_strlen(rec1b)
67 nx_lib_store_put_work("LIBT1HK" as *u8, rec1b, r1bn)
68 if nx_lib_store_get_work("LIBT1HK" as *u8, po, lo) < 0 { return 9 }
69 if lst_eq(po[0] as *u8, lo[0], rec1b, r1bn) != 1 { return 10 }
70
71 // update did NOT add a duplicate index line (key still enumerable once)
72 let iln2: i64 = nx_lib_store_index(idxbuf)
73 if lst_has(idxbuf, iln2, "LIBT1HK" as *u8) != 1 { return 11 }
74
75 let msg: *u8 = "nx_lib_store: 11/11 sovereign catalog-store assertions PASS (seg_store, O(1) get, append-only, no SQLite)\n" as *u8
76 sys_write(1, msg, ls_strlen(msg))
77 return 0
78}