encoding.ha (2293B)
1 use bytes; 2 3 @test fn encode() void = { 4 const decoded: [12]u8 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; 5 let result: [12]u8 = [0...]; 6 7 let buf: [5]word = [0...]; 8 9 encode(buf, decoded); 10 decode(result, buf); 11 12 assert(bytes::equal(result, decoded)); 13 }; 14 15 @test fn encodebigger() void = { 16 const decoded: [25]u8 = [ 17 0x8c, 0x99, 0xc4, 0x51, 0x53, 0x75, 0x86, 0x20, 0x73, 0x02, 18 0x2a, 0x08, 0xf6, 0x01, 0xcd, 0x8a, 0xc8, 0x39, 0xa8, 0xb3, 19 0x95, 0xb4, 0x27, 0xa1, 0xbb, 20 ]; 21 let result: [25]u8 = [0...]; 22 23 let buf: []word = alloc([0...], encodelen(decoded)); 24 defer free(buf); 25 26 encode(buf, decoded); 27 decode(result, buf); 28 29 assert(bytes::equal(result, decoded)); 30 }; 31 32 33 @test fn encmoddec() void = { 34 const input: [4]u8 = [0, 0, 0, 10]; 35 36 let mod = fromhex("00190000"); 37 let resultbuf: []word = alloc([0...], encodelen(input)); 38 let result: [4]u8 = [0...]; 39 40 let ret = encodemod(resultbuf[..], input, mod); 41 42 decode(result[..], resultbuf); 43 assert(ret == 1); 44 assert(bytes::equal(result, input)); 45 46 const input: [4]u8 = [0, 25, 0, 0]; 47 let ret = encodemod(resultbuf[..], input, mod); 48 assert(ret == 0); 49 assert(iszero(resultbuf) == 1); 50 51 const input: [4]u8 = [0, 26, 0, 0]; 52 let ret = encodemod(resultbuf[..], input, mod); 53 assert(ret == 0); 54 assert(iszero(resultbuf) == 1); 55 }; 56 57 58 @test fn encreddec() void = { 59 const input: [4]u8 = [0, 0, 0, 0x0a]; 60 61 let mod = fromhex("190000"); 62 defer free(mod); 63 let resultbuf: []word = alloc([0...], encodelen(input)); 64 let result: [4]u8 = [0...]; 65 66 encodereduce(resultbuf, input, mod); 67 decode(result, resultbuf); 68 assert(bytes::equal(result, input)); 69 70 const input: [4]u8 = [0, 0x19, 0, 0]; 71 let resultbuf: []word = alloc([0...], encodelen(input)); 72 encodereduce(resultbuf, input, mod); 73 decode(result, resultbuf); 74 assert(iszero(resultbuf) == 1); 75 76 const input: [4]u8 = [0x24, 0x17, 0x01, 0x05]; 77 let resultbuf: []word = alloc([0...], encodelen(input)); 78 encodereduce(resultbuf, input, mod); 79 decode(result, resultbuf); 80 assert(bytes::equal(result, [0x00, 0x0e, 0x01, 0x05])); 81 }; 82 83 @test fn word_countbits() void = { 84 assert(word_countbits(0) == 0); 85 assert(word_countbits(1) == 1); 86 assert(word_countbits(2) == 2); 87 assert(word_countbits(4) == 3); 88 assert(word_countbits(7) == 3); 89 assert(word_countbits(8) == 4); 90 assert(word_countbits(1131) == 11); 91 };