encoder_test.ha (3375B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use bufio; 5 use bytes; 6 use errors; 7 use io; 8 use memio; 9 use os; 10 use strings; 11 use time::date; 12 use types; 13 14 15 @test fn write_id() void = { 16 let buf = memio::dynamic(); 17 defer io::close(&buf)!; 18 let e = derencoder(&buf); 19 20 write_fixedprim(&e, class::UNIVERSAL, 0x2aa, [0x00])!; 21 encode(&e)!; 22 assert(bytes::equal([0x1f, 0x85, 0x2a, 0x01, 0x00], 23 memio::buffer(&buf))); 24 25 io::seek(&buf, 0, io::whence::SET)!; 26 let d = derdecoder(&buf); 27 let h = peek(&d)!; 28 assert(h.tagid == 0x2aa); 29 30 let buf = memio::dynamic(); 31 defer io::close(&buf)!; 32 let e = derencoder(&buf); 33 34 write_fixedprim(&e, class::UNIVERSAL, types::U32_MAX, [0x00])!; 35 encode(&e)!; 36 assert(bytes::equal([0x1f, 0x8f, 0xff, 0xff, 0xff, 0x7f, 0x01, 0x00], 37 memio::buffer(&buf))); 38 39 io::seek(&buf, 0, io::whence::SET)!; 40 let d = derdecoder(&buf); 41 let h = peek(&d)!; 42 assert(h.tagid == types::U32_MAX); 43 }; 44 45 @test fn write_prim() void = { 46 let buf = memio::dynamic(); 47 defer io::close(&buf)!; 48 let dest = memio::dynamic(); 49 defer io::close(&dest)!; 50 51 let enc = derencoder(&buf); 52 53 create_prim(&enc, class::UNIVERSAL, utag::INTEGER)!; 54 write(&enc, [0x01, 0x05, 0x07])!; 55 finish_prim(&enc); 56 57 assert(encodeto(&enc, &dest)! == 5); 58 59 assert(bytes::equal(memio::buffer(&dest), [ 60 0x02, 0x03, 0x01, 0x05, 0x07 61 ])); 62 }; 63 64 @test fn encode_dsz() void = { 65 assert(bytes::equal([0x7f], encode_dsz(0x7f))); 66 assert(bytes::equal([0x81, 0x8f], encode_dsz(0x8f))); 67 assert(bytes::equal([0x81, 0xff], encode_dsz(0xff))); 68 assert(bytes::equal([0x82, 0x01, 0x00], encode_dsz(0x100))); 69 }; 70 71 @test fn write_seq() void = { 72 let buf = memio::dynamic(); 73 defer io::close(&buf)!; 74 let dest = memio::dynamic(); 75 defer io::close(&dest)!; 76 77 let enc = derencoder(&buf); 78 79 create_seq(&enc)!; 80 write_bool(&enc, false)!; 81 create_seq(&enc)!; 82 write_int(&enc, [0x01, 0x02, 0x03])!; 83 finish_seq(&enc); 84 finish_seq(&enc); 85 assert(encodeto(&enc, &dest)! == 12); 86 87 assert(bytes::equal(memio::buffer(&dest), [ 88 0x30, 0x0a, // seq 89 0x01, 0x01, 0x00, // bool 90 0x30, 0x05, // seq 91 0x02, 0x03, 0x01, 0x02, 0x03, // int 92 ])); 93 }; 94 95 @test fn write_bool() void = { 96 let dest = memio::dynamic(); 97 defer io::close(&dest)!; 98 99 let buf = memio::dynamic(); 100 defer io::close(&buf)!; 101 let enc = derencoder(&buf); 102 103 write_bool(&enc, true)!; 104 encodeto(&enc, &dest)!; 105 106 assert(bytes::equal(memio::buffer(&dest), [0x01, 0x01, 0xff])); 107 }; 108 109 @test fn write_int() void = { 110 let dest = memio::dynamic(); 111 defer io::close(&dest)!; 112 let buf = memio::dynamic(); 113 defer io::close(&buf)!; 114 115 let enc = derencoder(&buf); 116 117 write_int(&enc, [0x00, 0x00, 0x00, 0x00, 0x80])!; 118 encodeto(&enc, &dest)!; 119 120 assert(bytes::equal(memio::buffer(&dest), [0x02, 0x02, 0x00, 0x80])); 121 122 memio::reset(&dest); 123 memio::reset(&buf); 124 let enc = derencoder(&buf); 125 126 write_int(&enc, [0xff, 0xff, 0xff, 0x80, 0x10])!; 127 encodeto(&enc, &dest)!; 128 129 assert(bytes::equal(memio::buffer(&dest), [0x02, 0x02, 0x80, 0x10])); 130 131 memio::reset(&dest); 132 memio::reset(&buf); 133 let enc = derencoder(&buf); 134 135 write_int(&enc, [0x00, 0x00, 0x00])!; 136 encodeto(&enc, &dest)!; 137 138 assert(bytes::equal(memio::buffer(&dest), [0x02, 0x01, 0x00])); 139 140 memio::reset(&dest); 141 memio::reset(&buf); 142 let enc = derencoder(&buf); 143 144 write_uint(&enc, [0x8f, 0x01])!; 145 encodeto(&enc, &dest)!; 146 147 assert(bytes::equal(memio::buffer(&dest), [0x02, 0x03, 0x00, 0x8f, 0x01])); 148 };