nx_doc_inref_test.nx source
↩ module page · 70 lines · 2835 B
1// nx_doc_inref_test.nx -- KAT for doc-reference counter.
2
3import "nx_syscalls.nx"
4import "nx_string_ops.nx"
5import "nx_grep.nx"
6import "nx_doc_inref.nx"
7
8func main() -> i64 {
9 // ----- T1 Pattern builder: 'foo.md' -> '[[foo]]' -----
10 let pat: *u8 = sys_mmap(64)
11 let n_pat: i64 = nx_doc_build_inref_pattern("foo.md", 6, pat, 64)
12 if n_pat != 7 { return 1 } // [[foo]] = 7 bytes
13 if pat[0] != 91 { return 2 } // '['
14 if pat[1] != 91 { return 3 } // '['
15 if pat[2] != 102 { return 4 } // 'f'
16 if pat[3] != 111 { return 5 } // 'o'
17 if pat[4] != 111 { return 6 } // 'o'
18 if pat[5] != 93 { return 7 } // ']'
19 if pat[6] != 93 { return 8 } // ']'
20
21 // ----- T2 Pattern builder: name too short -----
22 let pat2: *u8 = sys_mmap(64)
23 if nx_doc_build_inref_pattern("ab", 2, pat2, 64) != -1 { return 10 }
24 if nx_doc_build_inref_pattern(".md", 3, pat2, 64) != -1 { return 11 }
25
26 // ----- T3 Pattern builder: cap exhaustion -----
27 if nx_doc_build_inref_pattern("foo.md", 6, pat2, 5) != -1 { return 20 } // need 7, cap 5
28
29 // ----- T4 Pattern builder: dash + underscore in name -----
30 let pat3: *u8 = sys_mmap(64)
31 let np3: i64 = nx_doc_build_inref_pattern("nx-foo_bar.md", 13, pat3, 64)
32 if np3 != 14 { return 30 } // [[nx-foo_bar]] = 14 bytes
33
34 // ----- T5 Inref counter: synthetic 3-doc array -----
35 let buf_a: *u8 = sys_mmap(128)
36 let buf_b: *u8 = sys_mmap(128)
37 let buf_c: *u8 = sys_mmap(128)
38 let txt_a: *u8 = "see [[target]] for more"
39 let txt_b: *u8 = "no references here"
40 let txt_c: *u8 = "twice [[target]] and again [[target]]"
41 nx_str_slice_copy(txt_a, 0, 23, buf_a, 128)
42 nx_str_slice_copy(txt_b, 0, 18, buf_b, 128)
43 nx_str_slice_copy(txt_c, 0, 37, buf_c, 128)
44
45 // Build contents array (3 entries x 16 bytes each)
46 let contents_buf: *u8 = sys_mmap(3 * 16 + 8)
47 let r0: *NxInrefContents = contents_buf as *NxInrefContents
48 r0.ptr = buf_a; r0.len = 23
49 let r1: *NxInrefContents = ((contents_buf as *u8) + 16) as *NxInrefContents
50 r1.ptr = buf_b; r1.len = 18
51 let r2: *NxInrefContents = ((contents_buf as *u8) + 32) as *NxInrefContents
52 r2.ptr = buf_c; r2.len = 37
53
54 let target_pat: *u8 = "[[target]]"
55 // Doc A + doc C reference target; doc B doesn't. Count = 2.
56 let count: i64 = nx_doc_count_inref_across_docs(r0, 3, target_pat, 10)
57 if count != 2 { return 50 }
58
59 // ----- T6 Pattern that no doc references -----
60 let absent_pat: *u8 = "[[missing]]"
61 let count2: i64 = nx_doc_count_inref_across_docs(r0, 3, absent_pat, 11)
62 if count2 != 0 { return 60 }
63
64 // ----- T7 Self-skip helper -----
65 if nx_doc_should_skip_self(2, 2) != 1 { return 70 }
66 if nx_doc_should_skip_self(1, 3) != 0 { return 71 }
67 if nx_doc_should_skip_self(0, 0) != 1 { return 72 }
68
69 return 0
70}