nx_dec_emit_test.nx source
↩ module page · 100 lines · 3192 B
1// nx_dec_emit_test.nx -- KAT for nx_dec_emit_u63.
2//
3// Asserts:
4// T1 0 -> "0" (1 byte)
5// T2 1 -> "1" (1 byte)
6// T3 9 -> "9" (1 byte)
7// T4 10 -> "10" (2 bytes)
8// T5 100 -> "100" (3 bytes)
9// T6 12345 -> "12345" (5 bytes)
10// T7 1000000 -> "1000000" (7 bytes)
11// T8 9999999999 -> "9999999999" (10 bytes)
12// T9 emit-at-offset writes into the correct slot without clobbering
13// earlier bytes
14// T10 Content-Length use case: typical ACME JWS payload size 1234
15
16import "nx_syscalls.nx"
17import "nx_dec_emit.nx"
18
19func _assert_bytes(buf: *u8, exp: *u8, n: i64) -> i64 {
20 var i: i64 = 0
21 while i < n {
22 if buf[i] != exp[i] { return 0 }
23 i = i + 1
24 }
25 return 1
26}
27
28func main() -> i64 {
29 let buf: *u8 = sys_mmap(64)
30
31 // T1: 0 -> "0"
32 let n1: i64 = nx_dec_emit_u63(buf, 0, 0)
33 if n1 != 1 { return 1 }
34 if buf[0] != 48 as u8 { return 2 }
35
36 // T2: 1 -> "1"
37 let n2: i64 = nx_dec_emit_u63(buf, 0, 1)
38 if n2 != 1 { return 3 }
39 if buf[0] != 49 as u8 { return 4 }
40
41 // T3: 9 -> "9"
42 let n3: i64 = nx_dec_emit_u63(buf, 0, 9)
43 if n3 != 1 { return 5 }
44 if buf[0] != 57 as u8 { return 6 }
45
46 // T4: 10 -> "10"
47 let n4: i64 = nx_dec_emit_u63(buf, 0, 10)
48 if n4 != 2 { return 7 }
49 let exp4: *u8 = "10" as *u8
50 if _assert_bytes(buf, exp4, 2) != 1 { return 8 }
51
52 // T5: 100 -> "100"
53 let n5: i64 = nx_dec_emit_u63(buf, 0, 100)
54 if n5 != 3 { return 9 }
55 let exp5: *u8 = "100" as *u8
56 if _assert_bytes(buf, exp5, 3) != 1 { return 10 }
57
58 // T6: 12345 -> "12345"
59 let n6: i64 = nx_dec_emit_u63(buf, 0, 12345)
60 if n6 != 5 { return 11 }
61 let exp6: *u8 = "12345" as *u8
62 if _assert_bytes(buf, exp6, 5) != 1 { return 12 }
63
64 // T7: 1000000 -> "1000000"
65 let n7: i64 = nx_dec_emit_u63(buf, 0, 1000000)
66 if n7 != 7 { return 13 }
67 let exp7: *u8 = "1000000" as *u8
68 if _assert_bytes(buf, exp7, 7) != 1 { return 14 }
69
70 // T8: 9999999999 -> "9999999999"
71 let n8: i64 = nx_dec_emit_u63(buf, 0, 9999999999)
72 if n8 != 10 { return 15 }
73 let exp8: *u8 = "9999999999" as *u8
74 if _assert_bytes(buf, exp8, 10) != 1 { return 16 }
75
76 // T9: emit-at-offset. Pre-fill buf[0..4] with 'A's, emit at off=4,
77 // verify pre-fill untouched + emitted digits at expected position.
78 var k: i64 = 0
79 while k < 4 { buf[k] = 65 as u8; k = k + 1 } // 'A'
80 let n9: i64 = nx_dec_emit_u63(buf, 4, 42)
81 if n9 != 2 { return 17 }
82 if buf[0] != 65 as u8 { return 18 }
83 if buf[1] != 65 as u8 { return 19 }
84 if buf[2] != 65 as u8 { return 20 }
85 if buf[3] != 65 as u8 { return 21 }
86 if buf[4] != 52 as u8 { return 22 } // '4'
87 if buf[5] != 50 as u8 { return 23 } // '2'
88
89 // T10: Content-Length use case. Emit "1234" at off=0.
90 let n10: i64 = nx_dec_emit_u63(buf, 0, 1234)
91 if n10 != 4 { return 24 }
92 let exp10: *u8 = "1234" as *u8
93 if _assert_bytes(buf, exp10, 4) != 1 { return 25 }
94
95 // "PASS\n"
96 let ok: *u8 = sys_mmap(8)
97 ok[0]=80; ok[1]=65; ok[2]=83; ok[3]=83; ok[4]=10
98 sys_write(1, ok, 5)
99 return 0
100}