nx_code_quality_test.nx source
↩ module page · 174 lines · 6176 B
1// nx_code_quality_test.nx -- smoke for refactor-trigger primitive.
2
3import "nx_syscalls.nx"
4import "nx_tier.nx"
5import "nx_code_quality.nx"
6
7func _fill_buf(buf: *u8, lit: *u8, n: nx_int) -> nx_int {
8 var i: nx_int = 0
9 while i < n {
10 buf[i] = lit[i]
11 i = i + 1
12 }
13 return n
14}
15
16func main() -> nx_int {
17 // === Test 1: clean small file scores high ===
18 // 6 lines, well-commented, no hardcoding, no bare i64, shallow nesting.
19 let buf_clean: *u8 = sys_mmap(256)
20 // "// good\nfunc f() {\n return 0\n}\n// done\n"
21 buf_clean[0] = 47 // /
22 buf_clean[1] = 47 // /
23 buf_clean[2] = 32 //
24 buf_clean[3] = 103 // g
25 buf_clean[4] = 111 // o
26 buf_clean[5] = 111 // o
27 buf_clean[6] = 100 // d
28 buf_clean[7] = 10 // \n
29 buf_clean[8] = 102 // f
30 buf_clean[9] = 117 // u
31 buf_clean[10] = 110 // n
32 buf_clean[11] = 99 // c
33 buf_clean[12] = 32 //
34 buf_clean[13] = 102 // f
35 buf_clean[14] = 40 // (
36 buf_clean[15] = 41 // )
37 buf_clean[16] = 32 //
38 buf_clean[17] = 123 // {
39 buf_clean[18] = 10 // \n
40 buf_clean[19] = 32 //
41 buf_clean[20] = 32 //
42 buf_clean[21] = 32 //
43 buf_clean[22] = 32 //
44 buf_clean[23] = 114 // r
45 buf_clean[24] = 101 // e
46 buf_clean[25] = 116 // t
47 buf_clean[26] = 117 // u
48 buf_clean[27] = 114 // r
49 buf_clean[28] = 110 // n
50 buf_clean[29] = 32 //
51 buf_clean[30] = 48 // 0
52 buf_clean[31] = 10 // \n
53 buf_clean[32] = 125 // }
54 buf_clean[33] = 10 // \n
55 buf_clean[34] = 47 // /
56 buf_clean[35] = 47 // /
57 buf_clean[36] = 32 //
58 buf_clean[37] = 100 // d
59 buf_clean[38] = 111 // o
60 buf_clean[39] = 110 // n
61 buf_clean[40] = 101 // e
62 buf_clean[41] = 10 // \n
63
64 let r_clean: *CodeQualityReport = (sys_mmap(15 * NX_SIZEOF_NX_INT)) as *CodeQualityReport
65 nx_code_quality_compute(buf_clean, 42, r_clean)
66 // 5 newlines + tail-without-newline? Actually 5 \n -> 5 lines counted.
67 if r_clean.line_count < 4 { return 1 }
68 if r_clean.line_count > 6 { return 2 }
69 // No hardcoding, no nulls, no bare i64 -> all three axes near max.
70 if r_clean.null_ptr_count != 0 { return 3 }
71 if r_clean.hardcoded_size_count != 0 { return 4 }
72 if r_clean.bare_i64_count != 0 { return 5 }
73 if r_clean.max_nesting > 1 { return 6 }
74 // Two comment lines out of ~5 -> 40% density.
75 if r_clean.comment_line_count < 2 { return 7 }
76
77 // === Test 2: file with `* 8` hardcoding flag fires ===
78 // "let x: nx_int = a * 8\n"
79 let buf_hc: *u8 = sys_mmap(64)
80 buf_hc[0] = 108 // l
81 buf_hc[1] = 101 // e
82 buf_hc[2] = 116 // t
83 buf_hc[3] = 32 //
84 buf_hc[4] = 120 // x
85 buf_hc[5] = 58 // :
86 buf_hc[6] = 32 //
87 buf_hc[7] = 105 // i
88 buf_hc[8] = 54 // 6
89 buf_hc[9] = 52 // 4
90 buf_hc[10] = 32 //
91 buf_hc[11] = 61 // =
92 buf_hc[12] = 32 //
93 buf_hc[13] = 97 // a
94 buf_hc[14] = 32 //
95 buf_hc[15] = 42 // *
96 buf_hc[16] = 32 //
97 buf_hc[17] = 56 // 8
98 buf_hc[18] = 10 // \n
99 let r_hc: *CodeQualityReport = (sys_mmap(15 * NX_SIZEOF_NX_INT)) as *CodeQualityReport
100 nx_code_quality_compute(buf_hc, 19, r_hc)
101 if r_hc.hardcoded_size_count < 1 { return 10 }
102 if r_hc.bare_i64_count < 1 { return 11 }
103 // With 1 hardcoded literal + 1 bare i64 on a 1-line file, hardcoding
104 // and tier axes should both be FAR from clean.
105 if r_hc.hardcoding_q10 > 800 { return 12 }
106 if r_hc.tier_q10 > 500 { return 13 }
107
108 // === Test 3: null-pointer sentinel detection ===
109 // "let p: *u8 = 0 as *u8\n"
110 let buf_np: *u8 = sys_mmap(64)
111 buf_np[0] = 108 // l
112 buf_np[1] = 101 // e
113 buf_np[2] = 116 // t
114 buf_np[3] = 32
115 buf_np[4] = 112 // p
116 buf_np[5] = 58 // :
117 buf_np[6] = 32
118 buf_np[7] = 42 // *
119 buf_np[8] = 117 // u
120 buf_np[9] = 56 // 8
121 buf_np[10] = 32
122 buf_np[11] = 61 // =
123 buf_np[12] = 32
124 buf_np[13] = 48 // 0
125 buf_np[14] = 32
126 buf_np[15] = 97 // a
127 buf_np[16] = 115 // s
128 buf_np[17] = 32
129 buf_np[18] = 42 // *
130 buf_np[19] = 117 // u
131 buf_np[20] = 56 // 8
132 buf_np[21] = 10
133 let r_np: *CodeQualityReport = (sys_mmap(15 * NX_SIZEOF_NX_INT)) as *CodeQualityReport
134 nx_code_quality_compute(buf_np, 22, r_np)
135 if r_np.null_ptr_count < 1 { return 20 }
136 if r_np.null_ptr_q10 > 1000 { return 21 }
137
138 // === Test 4: deep nesting flagged ===
139 // "{{{{{{{{}}}}}}}}\n" -- 8 levels of nesting, max ok = 5.
140 let buf_nest: *u8 = sys_mmap(64)
141 var i_n: nx_int = 0
142 while i_n < 8 {
143 buf_nest[i_n] = 123 // {
144 i_n = i_n + 1
145 }
146 var j_n: nx_int = 0
147 while j_n < 8 {
148 buf_nest[8 + j_n] = 125 // }
149 j_n = j_n + 1
150 }
151 buf_nest[16] = 10 // \n
152 let r_nest: *CodeQualityReport = (sys_mmap(15 * NX_SIZEOF_NX_INT)) as *CodeQualityReport
153 nx_code_quality_compute(buf_nest, 17, r_nest)
154 if r_nest.max_nesting < 8 { return 30 }
155 // Nesting 8 > 5 -> nesting_q10 reduced.
156 if r_nest.nesting_q10 >= 1024 { return 31 }
157 // Worst axis should be nesting on this synthetic case.
158 if r_nest.worst_axis != NX_CQ_AXIS_NESTING { return 32 }
159
160 // === Test 5: sealed-enum validity ===
161 if nx_code_quality_verdict_is_valid(NX_CQ_REFACTOR_HOT) != 1 { return 40 }
162 if nx_code_quality_verdict_is_valid(NX_CQ_EXCELLENT) != 1 { return 41 }
163 if nx_code_quality_verdict_is_valid(99) != 0 { return 42 }
164 if nx_code_quality_axis_is_valid(NX_CQ_AXIS_TIER) != 1 { return 43 }
165 if nx_code_quality_axis_is_valid(99) != 0 { return 44 }
166
167 // === Test 6: report fields in valid ranges ===
168 if r_clean.composite_q10 < 0 { return 50 }
169 if r_clean.composite_q10 > 1024 { return 51 }
170 if nx_code_quality_verdict_is_valid(r_clean.verdict) != 1 { return 52 }
171 if nx_code_quality_axis_is_valid(r_clean.worst_axis) != 1 { return 53 }
172
173 return 0
174}