nx_bench_companion_tsv_test.nx source
↩ module page · 135 lines · 5319 B
1// nx_bench_companion_tsv_test.nx -- smoke for TSV emitter.
2
3import "nx_syscalls.nx"
4import "nx_tier.nx"
5import "nx_bench_companion.nx"
6import "nx_bench_companion_tsv.nx"
7
8func main() -> i64 {
9 // 1: verdict enum validity
10 if nx_bct_v_is_valid(NX_BCT_OK) != 1 { return 1 }
11 if nx_bct_v_is_valid(NX_BCT_OVERFLOW) != 1 { return 2 }
12 if nx_bct_v_is_valid(NX_BCT_INVALID) != 1 { return 3 }
13 if nx_bct_v_is_valid(NX_BCT_N) != 0 { return 4 }
14 if nx_bct_v_is_valid(-1) != 0 { return 5 }
15
16 // 2: header emission writes the expected leading bytes
17 let buf: *u8 = sys_mmap(512)
18 let n_hdr: nx_int = nx_bc_emit_tsv_header(buf, 512)
19 if n_hdr <= 0 { return 6 }
20 // First 6 bytes = "label\t"
21 if buf[0] != (0x6C as u8) { return 7 } // 'l'
22 if buf[1] != (0x61 as u8) { return 8 } // 'a'
23 if buf[2] != (0x62 as u8) { return 9 } // 'b'
24 if buf[3] != (0x65 as u8) { return 10 } // 'e'
25 if buf[4] != (0x6C as u8) { return 11 } // 'l'
26 if buf[5] != (0x09 as u8) { return 12 } // \t
27 // Last byte must be \n
28 if buf[n_hdr - 1] != (0x0A as u8) { return 13 }
29
30 // 3: header overflow -> negative
31 let tiny_buf: *u8 = sys_mmap(8)
32 if nx_bc_emit_tsv_header(tiny_buf, 4) >= 0 { return 14 }
33
34 // 4: build a report with known counter values
35 let r: *NxBenchReport = nx_bc_report_new()
36 r.n_actors = 3
37 r.actors_completed = 1
38 r.actors_failed = 0
39 r.actors_running = 2
40 r.actors_waiting = 0
41 r.total_actor_steps = 42
42 r.cumulative_runtime_us = 123456
43 r.n_subscriptions = 5
44 r.mux_pending = 7
45 r.arb_grants_full = 11
46 r.arb_grants_partial = 2
47 r.arb_denials = 1
48 r.arb_preemptions = 0
49 r.broker_grants = 9
50 r.broker_refusals = 0
51 r.broker_deadline_misses = 0
52 r.epoch_count = 4
53 r.captured_at_us = 999000
54 r.verdict = NX_BC_V_OK
55
56 // 5: emit row with label "v1_arm"
57 let row_buf: *u8 = sys_mmap(512)
58 let label: *u8 = sys_mmap(6)
59 label[0]=0x76 as u8; label[1]=0x31 as u8; label[2]=0x5F as u8
60 label[3]=0x61 as u8; label[4]=0x72 as u8; label[5]=0x6D as u8
61
62 let n_row: nx_int = nx_bc_emit_tsv_row(row_buf, 512, r, label, 6)
63 if n_row <= 0 { return 15 }
64 // First 7 bytes = "v1_arm\t"
65 if row_buf[0] != (0x76 as u8) { return 16 }
66 if row_buf[1] != (0x31 as u8) { return 17 }
67 if row_buf[5] != (0x6D as u8) { return 18 }
68 if row_buf[6] != (0x09 as u8) { return 19 }
69 // After label\t, decimal "3" then \t -> bytes 7='3' 8='\t'
70 if row_buf[7] != (0x33 as u8) { return 20 } // '3'
71 if row_buf[8] != (0x09 as u8) { return 21 }
72 // Last byte must be \n
73 if row_buf[n_row - 1] != (0x0A as u8) { return 22 }
74 // Second-to-last byte must be '0' (verdict = NX_BC_V_OK = 0)
75 if row_buf[n_row - 2] != (0x30 as u8) { return 23 }
76
77 // 6: row count tabs = 19 (one between every column; last column
78 // is followed by \n not \t). Plus the leading label\t.
79 var i: nx_int = 0
80 var tabs: nx_int = 0
81 while i < n_row {
82 if row_buf[i] == (0x09 as u8) { tabs = tabs + 1 }
83 i = i + 1
84 }
85 // label\t + 19 counter\t separators (last column ends with \n).
86 // Actually columns are: label + 19 counters = 20 columns -> 19 tabs.
87 if tabs != 19 { return 24 }
88
89 // 7: row with null label still works (treated as empty leader)
90 let row_buf2: *u8 = sys_mmap(512)
91 let null_label: *u8 = (0 as i64) as *u8
92 let n_row2: nx_int = nx_bc_emit_tsv_row(row_buf2, 512, r, null_label, 0)
93 if n_row2 <= 0 { return 25 }
94 // First byte is \t (no label)
95 if row_buf2[0] != (0x09 as u8) { return 26 }
96
97 // 8: emit negative counter -- include '-' sign
98 let r2: *NxBenchReport = nx_bc_report_new()
99 r2.n_actors = -5
100 let row_buf3: *u8 = sys_mmap(512)
101 let n_row3: nx_int = nx_bc_emit_tsv_row(row_buf3, 512, r2, label, 6)
102 if n_row3 <= 0 { return 27 }
103 // After "v1_arm\t" comes "-5\t"
104 if row_buf3[7] != (0x2D as u8) { return 28 } // '-'
105 if row_buf3[8] != (0x35 as u8) { return 29 } // '5'
106 if row_buf3[9] != (0x09 as u8) { return 30 }
107
108 // 9: overflow row buffer -> negative verdict
109 let small_buf: *u8 = sys_mmap(16)
110 if nx_bc_emit_tsv_row(small_buf, 8, r, label, 6) >= 0 { return 31 }
111
112 // 10: null guards
113 let null_buf: *u8 = (0 as i64) as *u8
114 if nx_bc_emit_tsv_header(null_buf, 512) >= 0 { return 32 }
115 if nx_bc_emit_tsv_row(null_buf, 512, r, label, 6) >= 0 { return 33 }
116 let null_r: *NxBenchReport = (0 as i64) as *NxBenchReport
117 if nx_bc_emit_tsv_row(row_buf, 512, null_r, label, 6) >= 0 { return 34 }
118
119 // 11: invalid (negative) cap / label_len
120 if nx_bc_emit_tsv_header(buf, -1) >= 0 { return 35 }
121 if nx_bc_emit_tsv_row(buf, -1, r, label, 6) >= 0 { return 36 }
122 if nx_bc_emit_tsv_row(buf, 512, r, label, -1) >= 0 { return 37 }
123
124 // 12: round-trip with 0-valued counters produces all '0' fields
125 let r_zero: *NxBenchReport = nx_bc_report_new()
126 let row_buf4: *u8 = sys_mmap(256)
127 let n_row4: nx_int = nx_bc_emit_tsv_row(row_buf4, 256, r_zero, label, 6)
128 if n_row4 <= 0 { return 38 }
129 // After label\t: each counter = '0' followed by separator
130 if row_buf4[7] != (0x30 as u8) { return 39 } // '0'
131 if row_buf4[8] != (0x09 as u8) { return 40 }
132 if row_buf4[9] != (0x30 as u8) { return 41 } // next '0'
133
134 return 0
135}