| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use bytes; |
| 5 |
|
| 6 |
|
| 7 |
@test fn encode() void = { |
| 8 |
const decoded: [12]u8 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; |
| 9 |
let result: [12]u8 = [0...]; |
| 10 |
|
| 11 |
let buf: [5]word = [0...]; |
| 12 |
|
| 13 |
encode(buf, decoded); |
| 14 |
decode(result, buf); |
| 15 |
|
| 16 |
assert(bytes::equal(result, decoded)); |
| 17 |
}; |
| 18 |
|
| 19 |
@test fn encodebigger() void = { |
| 20 |
const decoded: [25]u8 = [ |
| 21 |
0x8c, 0x99, 0xc4, 0x51, 0x53, 0x75, 0x86, 0x20, 0x73, 0x02, |
| 22 |
0x2a, 0x08, 0xf6, 0x01, 0xcd, 0x8a, 0xc8, 0x39, 0xa8, 0xb3, |
| 23 |
0x95, 0xb4, 0x27, 0xa1, 0xbb, |
| 24 |
]; |
| 25 |
let result: [25]u8 = [0...]; |
| 26 |
|
| 27 |
let buf: []word = alloc([0...], encodelen(decoded))!; |
| 28 |
defer free(buf); |
| 29 |
|
| 30 |
encode(buf, decoded); |
| 31 |
decode(result, buf); |
| 32 |
|
| 33 |
assert(bytes::equal(result, decoded)); |
| 34 |
}; |
| 35 |
|
| 36 |
@test fn decodebigger() void = { |
| 37 |
const encoded: [_]word = [ |
| 38 |
0x32, // only 50 effective bits |
| 39 |
0x7fffffff, 0x0007ffff, 0x7fffffff, |
| 40 |
]; |
| 41 |
|
| 42 |
let result: [8]u8 = [0xaa...]; |
| 43 |
|
| 44 |
decode(result, encoded); |
| 45 |
assert(bytes::equal(result, |
| 46 |
[0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])); |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
@test fn encmoddec() void = { |
| 51 |
const input: [4]u8 = [0, 0, 0, 10]; |
| 52 |
|
| 53 |
let mod = fromhex("00190000"); |
| 54 |
defer free(mod); |
| 55 |
let resultbuf: []word = alloc([0...], encodelen(input))!; |
| 56 |
defer free(resultbuf); |
| 57 |
let result: [4]u8 = [0...]; |
| 58 |
|
| 59 |
let ret = encodemod(resultbuf[..], input, mod); |
| 60 |
|
| 61 |
decode(result[..], resultbuf); |
| 62 |
assert(ret == 1); |
| 63 |
assert(bytes::equal(result, input)); |
| 64 |
|
| 65 |
const input: [4]u8 = [0, 25, 0, 0]; |
| 66 |
let ret = encodemod(resultbuf[..], input, mod); |
| 67 |
assert(ret == 0); |
| 68 |
assert(iszero(resultbuf) == 1); |
| 69 |
|
| 70 |
const input: [4]u8 = [0, 26, 0, 0]; |
| 71 |
let ret = encodemod(resultbuf[..], input, mod); |
| 72 |
assert(ret == 0); |
| 73 |
assert(iszero(resultbuf) == 1); |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
@test fn encreddec() void = { |
| 78 |
const input: [4]u8 = [0, 0, 0, 0x0a]; |
| 79 |
|
| 80 |
let mod = fromhex("190000"); |
| 81 |
defer free(mod); |
| 82 |
let resultbuf: []word = alloc([0...], encodelen(input))!; |
| 83 |
defer free(resultbuf); |
| 84 |
let result: [4]u8 = [0...]; |
| 85 |
|
| 86 |
encodereduce(resultbuf, input, mod); |
| 87 |
decode(result, resultbuf); |
| 88 |
assert(bytes::equal(result, input)); |
| 89 |
|
| 90 |
const input: [4]u8 = [0, 0x19, 0, 0]; |
| 91 |
let resultbuf: []word = alloc([0...], encodelen(input))!; |
| 92 |
defer free(resultbuf); |
| 93 |
encodereduce(resultbuf, input, mod); |
| 94 |
decode(result, resultbuf); |
| 95 |
assert(iszero(resultbuf) == 1); |
| 96 |
|
| 97 |
const input: [4]u8 = [0x24, 0x17, 0x01, 0x05]; |
| 98 |
let resultbuf: []word = alloc([0...], encodelen(input))!; |
| 99 |
defer free(resultbuf); |
| 100 |
encodereduce(resultbuf, input, mod); |
| 101 |
decode(result, resultbuf); |
| 102 |
assert(bytes::equal(result, [0x00, 0x0e, 0x01, 0x05])); |
| 103 |
}; |
| 104 |
|
| 105 |
@test fn word_countbits() void = { |
| 106 |
assert(word_countbits(0) == 0); |
| 107 |
assert(word_countbits(1) == 1); |
| 108 |
assert(word_countbits(2) == 2); |
| 109 |
assert(word_countbits(4) == 3); |
| 110 |
assert(word_countbits(7) == 3); |
| 111 |
assert(word_countbits(8) == 4); |
| 112 |
assert(word_countbits(1131) == 11); |
| 113 |
}; |