code wiki / (root) / nx_cbor_decode_test.nx

nx_cbor_decode_test.nx source

↩ module page · 135 lines · 4869 B

1// nx_cbor_decode_test.nx -- encoder/decoder roundtrip + spec patterns. 2 3import "nx_syscalls.nx" 4import "nx_tier.nx" 5import "nx_cbor.nx" 6import "nx_cbor_decode.nx" 7 8func main() -> nx_int { 9 let buf: *u8 = (sys_mmap(256)) as *u8 10 let np: *i64 = (sys_mmap(8)) as *i64 11 12 // ===== Roundtrip: u64 ====================================== 13 nx_cbor_emit_u64(buf, 0, 0) 14 if nx_cbor_read_i64(buf, 0, np) != 0 { return 1 } 15 if np[0] != 1 { return 2 } 16 17 nx_cbor_emit_u64(buf, 0, 23) 18 if nx_cbor_read_i64(buf, 0, np) != 23 { return 3 } 19 if np[0] != 1 { return 4 } 20 21 nx_cbor_emit_u64(buf, 0, 24) 22 if nx_cbor_read_i64(buf, 0, np) != 24 { return 5 } 23 if np[0] != 2 { return 6 } 24 25 nx_cbor_emit_u64(buf, 0, 255) 26 if nx_cbor_read_i64(buf, 0, np) != 255 { return 7 } 27 if np[0] != 2 { return 8 } 28 29 nx_cbor_emit_u64(buf, 0, 256) 30 if nx_cbor_read_i64(buf, 0, np) != 256 { return 9 } 31 if np[0] != 3 { return 10 } 32 33 nx_cbor_emit_u64(buf, 0, 65535) 34 if nx_cbor_read_i64(buf, 0, np) != 65535 { return 11 } 35 36 nx_cbor_emit_u64(buf, 0, 65536) 37 if nx_cbor_read_i64(buf, 0, np) != 65536 { return 12 } 38 if np[0] != 5 { return 13 } 39 40 // 4-byte boundary: 4294967295 (2^32 - 1) 41 nx_cbor_emit_u64(buf, 0, 4294967295) 42 if nx_cbor_read_i64(buf, 0, np) != 4294967295 { return 14 } 43 44 // 8-byte boundary: 1 trillion 45 nx_cbor_emit_u64(buf, 0, 1000000000000) 46 if nx_cbor_read_i64(buf, 0, np) != 1000000000000 { return 15 } 47 48 // ===== Roundtrip: i64 (negatives) ========================= 49 nx_cbor_emit_i64(buf, 0, 0 - 1) 50 if nx_cbor_read_i64(buf, 0, np) != 0 - 1 { return 20 } 51 52 nx_cbor_emit_i64(buf, 0, 0 - 24) 53 if nx_cbor_read_i64(buf, 0, np) != 0 - 24 { return 21 } 54 55 nx_cbor_emit_i64(buf, 0, 0 - 100) 56 if nx_cbor_read_i64(buf, 0, np) != 0 - 100 { return 22 } 57 58 nx_cbor_emit_i64(buf, 0, 0 - 65536) 59 if nx_cbor_read_i64(buf, 0, np) != 0 - 65536 { return 23 } 60 61 // ===== Peek-kind correctness ============================= 62 nx_cbor_emit_u64(buf, 0, 5) 63 if nx_cbor_peek_kind(buf, 0) != NX_CBOR_DK_UINT { return 30 } 64 65 nx_cbor_emit_i64(buf, 0, 0 - 5) 66 if nx_cbor_peek_kind(buf, 0) != NX_CBOR_DK_NINT { return 31 } 67 68 nx_cbor_emit_array_header(buf, 0, 3) 69 if nx_cbor_peek_kind(buf, 0) != NX_CBOR_DK_ARRAY { return 32 } 70 71 nx_cbor_emit_map_header(buf, 0, 2) 72 if nx_cbor_peek_kind(buf, 0) != NX_CBOR_DK_MAP { return 33 } 73 74 // ===== Array roundtrip ==================================== 75 var p: nx_int = nx_cbor_emit_array_header(buf, 0, 3) 76 p = nx_cbor_emit_u64(buf, p, 100) 77 p = nx_cbor_emit_u64(buf, p, 200) 78 p = nx_cbor_emit_u64(buf, p, 300) 79 80 let n: nx_int = nx_cbor_read_array_header(buf, 0, np) 81 if n != 3 { return 40 } 82 var cur: nx_int = np[0] 83 if nx_cbor_read_i64(buf, cur, np) != 100 { return 41 } 84 cur = np[0] 85 if nx_cbor_read_i64(buf, cur, np) != 200 { return 42 } 86 cur = np[0] 87 if nx_cbor_read_i64(buf, cur, np) != 300 { return 43 } 88 89 // ===== Full span roundtrip ================================ 90 let attrs_in: *i64 = (sys_mmap(48)) as *i64 91 attrs_in[0] = 0xAA; attrs_in[1] = 100 92 attrs_in[2] = 0xBB; attrs_in[3] = 0 - 50 93 attrs_in[4] = 0xCC; attrs_in[5] = 1234567 94 95 p = nx_cbor_emit_span(buf, 0, 1000, 7, 0 - 1, 13, 100, 200, attrs_in, 3) 96 97 let trace_o: *i64 = (sys_mmap(8)) as *i64 98 let span_o: *i64 = (sys_mmap(8)) as *i64 99 let parent_o: *i64 = (sys_mmap(8)) as *i64 100 let kind_o: *i64 = (sys_mmap(8)) as *i64 101 let start_o: *i64 = (sys_mmap(8)) as *i64 102 let end_o: *i64 = (sys_mmap(8)) as *i64 103 let attrs_o: *i64 = (sys_mmap(48)) as *i64 104 let n_attrs_o: *i64 = (sys_mmap(8)) as *i64 105 106 let rc: nx_int = nx_cbor_read_span(buf, 0, np, 107 trace_o, span_o, parent_o, kind_o, 108 start_o, end_o, 109 attrs_o, 6, n_attrs_o) 110 if rc != 0 { return 50 } 111 if trace_o[0] != 1000 { return 51 } 112 if span_o[0] != 7 { return 52 } 113 if parent_o[0] != 0 - 1 { return 53 } 114 if kind_o[0] != 13 { return 54 } 115 if start_o[0] != 100 { return 55 } 116 if end_o[0] != 200 { return 56 } 117 if n_attrs_o[0] != 3 { return 57 } 118 if attrs_o[0] != 0xAA { return 58 } 119 if attrs_o[1] != 100 { return 59 } 120 if attrs_o[2] != 0xBB { return 60 } 121 if attrs_o[3] != 0 - 50 { return 61 } 122 if attrs_o[4] != 0xCC { return 62 } 123 if attrs_o[5] != 1234567 { return 63 } 124 125 // np[0] should be at p (end of span bytes) 126 if np[0] != p { return 64 } 127 128 // ===== DK bands valid ===================================== 129 if nx_cbor_dk_is_valid(NX_CBOR_DK_UINT) != 1 { return 70 } 130 if nx_cbor_dk_is_valid(NX_CBOR_DK_OTHER) != 1 { return 71 } 131 if nx_cbor_dk_is_valid(0 - 1) != 0 { return 72 } 132 if nx_cbor_dk_is_valid(NX_CBOR_DK_N_KINDS) != 0 { return 73 } 133 134 return 0 135}