hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

encoding_test.ha (2676B)


      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 	let result: [4]u8 = [0...];
     57 
     58 	let ret = encodemod(resultbuf[..], input, mod);
     59 
     60 	decode(result[..], resultbuf);
     61 	assert(ret == 1);
     62 	assert(bytes::equal(result, input));
     63 
     64 	const input: [4]u8 = [0, 25, 0, 0];
     65 	let ret = encodemod(resultbuf[..], input, mod);
     66 	assert(ret == 0);
     67 	assert(iszero(resultbuf) == 1);
     68 
     69 	const input: [4]u8 = [0, 26, 0, 0];
     70 	let ret = encodemod(resultbuf[..], input, mod);
     71 	assert(ret == 0);
     72 	assert(iszero(resultbuf) == 1);
     73 };
     74 
     75 
     76 @test fn encreddec() void = {
     77 	const input: [4]u8 = [0, 0, 0, 0x0a];
     78 
     79 	let mod = fromhex("190000");
     80 	defer free(mod);
     81 	let resultbuf: []word = alloc([0...], encodelen(input));
     82 	let result: [4]u8 = [0...];
     83 
     84 	encodereduce(resultbuf, input, mod);
     85 	decode(result, resultbuf);
     86 	assert(bytes::equal(result, input));
     87 
     88 	const input: [4]u8 = [0, 0x19, 0, 0];
     89 	let resultbuf: []word = alloc([0...], encodelen(input));
     90 	encodereduce(resultbuf, input, mod);
     91 	decode(result, resultbuf);
     92 	assert(iszero(resultbuf) == 1);
     93 
     94 	const input: [4]u8 = [0x24, 0x17, 0x01, 0x05];
     95 	let resultbuf: []word = alloc([0...], encodelen(input));
     96 	encodereduce(resultbuf, input, mod);
     97 	decode(result, resultbuf);
     98 	assert(bytes::equal(result, [0x00, 0x0e, 0x01, 0x05]));
     99 };
    100 
    101 @test fn word_countbits() void = {
    102 	assert(word_countbits(0) == 0);
    103 	assert(word_countbits(1) == 1);
    104 	assert(word_countbits(2) == 2);
    105 	assert(word_countbits(4) == 3);
    106 	assert(word_countbits(7) == 3);
    107 	assert(word_countbits(8) == 4);
    108 	assert(word_countbits(1131) == 11);
    109 };