code wiki / (root) / nx_doc_func_scan_test.nx

nx_doc_func_scan_test.nx source

↩ module page · 95 lines · 3877 B

1// nx_doc_func_scan_test.nx -- KAT for bits-up func-pattern scanner. 2 3import "nx_syscalls.nx" 4import "nx_string_ops.nx" 5import "nx_doc_func_scan.nx" 6 7func main() -> i64 { 8 let cap: nx_int = 1024 9 let content: *u8 = sys_mmap(cap) 10 let out_name: *u8 = sys_mmap(64) 11 let res_buf: *u8 = sys_mmap(32) 12 let res: *NxFuncScanResult = res_buf as *NxFuncScanResult 13 14 // ----- T1 Single func def ----- 15 let bs1: *u8 = "func nx_foo(x: i64) -> i64 {\nreturn x\n}\n" 16 let n1: nx_int = 40 17 nx_str_slice_copy(bs1, 0, n1, content, cap) 18 nx_doc_func_scan_next(content, n1, 0, out_name, 64, res) 19 if res.next_offset < 0 { return 1 } 20 if res.n_name != 6 { return 2 } 21 if out_name[0] != 110 { return 3 } // 'n' 22 if out_name[1] != 120 { return 4 } // 'x' 23 if out_name[2] != 95 { return 5 } // '_' 24 if out_name[3] != 102 { return 6 } // 'f' 25 if out_name[4] != 111 { return 7 } // 'o' 26 if out_name[5] != 111 { return 8 } // 'o' 27 if out_name[6] != 0 { return 9 } // NUL 28 29 // ----- T2 No match (no func) ----- 30 let bs2: *u8 = "// just a comment\nlet x = 1\n" 31 let n2: nx_int = 28 32 nx_str_slice_copy(bs2, 0, n2, content, cap) 33 nx_doc_func_scan_next(content, n2, 0, out_name, 64, res) 34 if res.next_offset != -1 { return 20 } 35 36 // ----- T3 Forward declaration NOT matched ----- 37 let bs3: *u8 = "func nx_fwd(x: i64) -> i64;\nother\n" 38 let n3: nx_int = 34 39 nx_str_slice_copy(bs3, 0, n3, content, cap) 40 nx_doc_func_scan_next(content, n3, 0, out_name, 64, res) 41 if res.next_offset != -1 { return 30 } // ends with ';' not '{' 42 43 // ----- T4 Multiple defs: iterate ----- 44 let bs4: *u8 = "func alpha(x: i64) -> i64 {\nreturn x\n}\nfunc bravo() -> i64 {\nreturn 0\n}\n" 45 let n4: nx_int = 73 46 nx_str_slice_copy(bs4, 0, n4, content, cap) 47 nx_doc_func_scan_next(content, n4, 0, out_name, 64, res) 48 if res.n_name != 5 { return 40 } // "alpha" 49 if out_name[0] != 97 { return 41 } // 'a' 50 51 let next1: i64 = res.next_offset 52 nx_doc_func_scan_next(content, n4, next1, out_name, 64, res) 53 if res.n_name != 5 { return 42 } // "bravo" 54 if out_name[0] != 98 { return 43 } // 'b' 55 56 // Third call -> no more 57 let next2: i64 = res.next_offset 58 nx_doc_func_scan_next(content, n4, next2, out_name, 64, res) 59 if res.next_offset != -1 { return 44 } 60 61 // ----- T5 Indented func (NOT column 0) skipped ----- 62 let bs5: *u8 = " func inner(x: i64) -> i64 {\nreturn 0\n}\n" 63 let n5: nx_int = 43 64 nx_str_slice_copy(bs5, 0, n5, content, cap) 65 nx_doc_func_scan_next(content, n5, 0, out_name, 64, res) 66 if res.next_offset != -1 { return 50 } 67 68 // ----- T6 Cap exhaustion: name too big ----- 69 let bs6: *u8 = "func very_long_function_name(x: i64) -> i64 {\n" 70 let n6: nx_int = 46 71 nx_str_slice_copy(bs6, 0, n6, content, cap) 72 nx_doc_func_scan_next(content, n6, 0, out_name, 8, res) 73 if res.n_name != -1 { return 60 } 74 if res.next_offset <= 0 { return 61 } 75 76 // ----- T7 Function with no args ----- 77 let bs7: *u8 = "func nullary() -> i64 {\nreturn 0\n}\n" 78 let n7: nx_int = 35 79 nx_str_slice_copy(bs7, 0, n7, content, cap) 80 nx_doc_func_scan_next(content, n7, 0, out_name, 64, res) 81 if res.n_name != 7 { return 70 } // "nullary" 82 83 // ----- T8 Function followed by other code on same line ----- 84 // Substrate-honest: compact one-liner ('func X() { body }') ends 85 // with '}' not '{', so detector does NOT match it. By design -- 86 // multi-line definitions are the canonical pattern; one-liners 87 // would require a separate fast-path. 88 let bs8: *u8 = "func compact(x: i64) -> i64 { return x }\n" 89 let n8: nx_int = 41 90 nx_str_slice_copy(bs8, 0, n8, content, cap) 91 nx_doc_func_scan_next(content, n8, 0, out_name, 64, res) 92 if res.next_offset != -1 { return 80 } // verify no match 93 94 return 0 95}