nx_doc_incumbent_test.nx source
↩ module page · 108 lines · 4669 B
1// nx_doc_incumbent_test.nx -- KAT for D21+D22 primitives.
2
3import "nx_syscalls.nx"
4import "nx_string_ops.nx"
5import "nx_doc_incumbent.nx"
6
7func main() -> i64 {
8 // ----- T1 Provenance sealed-enum -----
9 var i: i64 = 0
10 while i < NX_PROVENANCE_N {
11 if nx_provenance_is_valid(i) != 1 { return 1 + i }
12 i = i + 1
13 }
14 if nx_provenance_is_valid(NX_PROVENANCE_N) != 0 { return 10 }
15 if nx_provenance_is_valid(0 - 1) != 0 { return 11 }
16
17 // ----- T2 Substrate-measured threshold -----
18 if nx_provenance_is_substrate_measured(NX_PROVENANCE_INDUSTRY_STATED) != 0 { return 20 }
19 if nx_provenance_is_substrate_measured(NX_PROVENANCE_PERSONALLY_MEASURED) != 1 { return 21 }
20 if nx_provenance_is_substrate_measured(NX_PROVENANCE_CROSS_VALIDATED) != 1 { return 22 }
21 if nx_provenance_is_substrate_measured(NX_PROVENANCE_HARSH_VALIDATED) != 1 { return 23 }
22 if nx_provenance_is_substrate_measured(NX_PROVENANCE_TIME_PROVEN) != 1 { return 24 }
23
24 // ----- T3 Primitive class sealed-enum -----
25 var j: i64 = 0
26 while j < NX_PCLASS_N {
27 if nx_pclass_is_valid(j) != 1 { return 30 + j }
28 j = j + 1
29 }
30 if nx_pclass_is_valid(NX_PCLASS_N) != 0 { return 50 }
31
32 // ----- T4 Class name lookup -----
33 if nx_str_equals(nx_pclass_name(NX_PCLASS_PATTERN_MATCHING), 16, "PATTERN_MATCHING", 16) != 1 { return 60 }
34 if nx_str_equals(nx_pclass_name(NX_PCLASS_CARDINALITY), 11, "CARDINALITY", 11) != 1 { return 61 }
35 if nx_str_equals(nx_pclass_name(NX_PCLASS_TLS), 3, "TLS", 3) != 1 { return 62 }
36
37 // ----- T5 Incumbent row init -----
38 let row_buf: *u8 = sys_mmap(64)
39 let row: *NxIncumbentRow = row_buf as *NxIncumbentRow
40 let rc1: i64 = nx_incumbent_row_init(row, "ripgrep", 7, "14.1.0", 6, 24576, NX_PROVENANCE_INDUSTRY_STATED)
41 if rc1 != 0 { return 70 }
42 if row.name_len != 7 { return 71 }
43 if row.version_len != 6 { return 72 }
44 if row.measured_value_q14 != 24576 { return 73 }
45 if row.provenance_tier != NX_PROVENANCE_INDUSTRY_STATED { return 74 }
46
47 // ----- T6 Incumbent row rejects invalid provenance -----
48 let rc_bad: i64 = nx_incumbent_row_init(row, "ripgrep", 7, "14.1.0", 6, 24576, 9999)
49 if rc_bad != -1 { return 80 }
50
51 // ----- T7 Table init -----
52 let table_buf: *u8 = sys_mmap(128)
53 let t: *NxIncumbentTable = table_buf as *NxIncumbentTable
54 let rc_t: i64 = nx_incumbent_table_init(
55 t,
56 NX_PCLASS_PATTERN_MATCHING,
57 "substring search 1 GiB", 22,
58 "ops/sec (Q14)", 13,
59 16384, // substrate value Q14 = 1.0
60 NX_PROVENANCE_PERSONALLY_MEASURED
61 )
62 if rc_t != 0 { return 90 }
63 if t.primitive_class != NX_PCLASS_PATTERN_MATCHING { return 91 }
64 if t.substrate_value_q14 != 16384 { return 92 }
65 if t.n_rows != 0 { return 93 }
66
67 // ----- T8 Table init rejects invalid pclass / provenance -----
68 let rc_t_bad: i64 = nx_incumbent_table_init(t, 9999, "w", 1, "m", 1, 0, 0)
69 if rc_t_bad != -1 { return 100 }
70 let rc_t_bad2: i64 = nx_incumbent_table_init(t, NX_PCLASS_SORT, "w", 1, "m", 1, 0, 9999)
71 if rc_t_bad2 != -2 { return 101 }
72
73 // ----- T9 Gap percentage: substrate beats incumbent by 50% -----
74 // sub=24576 (Q14 1.5), inc=16384 (Q14 1.0) -> +50% gap
75 // gap = ((24576 - 16384) * 100 * 16384) / 16384 = 819200
76 let gap1: i64 = nx_incumbent_gap_pct_q14(24576, 16384)
77 if gap1 != 819200 { return 110 } // +50% in Q14
78
79 // Substrate trails incumbent: sub=8192, inc=16384 -> -50%
80 let gap2: i64 = nx_incumbent_gap_pct_q14(8192, 16384)
81 if gap2 != 0 - 819200 { return 111 } // -50% in Q14
82
83 // Parity: sub == inc -> 0
84 let gap3: i64 = nx_incumbent_gap_pct_q14(16384, 16384)
85 if gap3 != 0 { return 112 }
86
87 // Division-by-zero guard: inc == 0 -> 0
88 let gap4: i64 = nx_incumbent_gap_pct_q14(16384, 0)
89 if gap4 != 0 { return 113 }
90
91 // ----- T10 Table set_rows + emit -----
92 let rows_buf: *u8 = sys_mmap(3 * 48 + 16)
93 let rows: *NxIncumbentRow = rows_buf as *NxIncumbentRow
94
95 let r0: *NxIncumbentRow = rows
96 nx_incumbent_row_init(r0, "ripgrep", 7, "14.1.0", 6, 24576, NX_PROVENANCE_INDUSTRY_STATED)
97 let r1: *NxIncumbentRow = ((rows_buf as *u8) + 48) as *NxIncumbentRow
98 nx_incumbent_row_init(r1, "Hyperscan", 9, "5.4.2", 5, 32768, NX_PROVENANCE_PERSONALLY_MEASURED)
99 let r2: *NxIncumbentRow = ((rows_buf as *u8) + 96) as *NxIncumbentRow
100 nx_incumbent_row_init(r2, "GNU grep", 8, "3.11", 4, 8192, NX_PROVENANCE_CROSS_VALIDATED)
101
102 nx_incumbent_table_set_rows(t, rows, 3)
103 if t.n_rows != 3 { return 120 }
104
105 if nx_incumbent_table_emit(t) != 0 { return 130 }
106
107 return 0
108}