summaryrefslogtreecommitdiff
path: root/mcproto/+test.ha
blob: 5ce070c5c8170260138d5a1df9b3f2923c67abba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use bytes;
use trace;

fn varint_roundtrip(value: i32, bytes: []u8) void = {
	let buf: []u8 = [];
	encode_varint(&buf, value);
	assert(bytes::equal(buf, bytes));
	let dec = Decoder {
		input = buf,
		pos = 0,
		tracer = &trace::silent,
	};
	assert(decode_varint(&root(&dec))! == value);
};

fn varint_invalid(bytes: []u8) void = {
	let dec = Decoder {
		input = bytes,
		pos = 0,
		tracer = &trace::silent,
	};
	assert(decode_varint(&root(&dec)) is trace::failed);
};

@test fn varints() void = {
        // sample varints from https://wiki.vg/protocol?oldid=17184#varint_and_varlong
        varint_roundtrip(0, [0x00]);
        varint_roundtrip(1, [0x01]);
        varint_roundtrip(2, [0x02]);
        varint_roundtrip(127, [0x7f]);
        varint_roundtrip(128, [0x80, 0x01]);
        varint_roundtrip(255, [0xff, 0x01]);
        varint_roundtrip(25565, [0xdd, 0xc7, 0x01]);
        varint_roundtrip(2097151, [0xff, 0xff, 0x7f]);
        varint_roundtrip(2147483647, [0xff, 0xff, 0xff, 0xff, 0x07]);
        varint_roundtrip(-1, [0xff, 0xff, 0xff, 0xff, 0x0f]);
        varint_roundtrip(-2147483648, [0x80, 0x80, 0x80, 0x80, 0x08]);
};

@test fn varints_invalid() void = {
	varint_invalid([]);
	varint_invalid([0x8f]);
	varint_invalid([0x8f, 0x8f]);
	varint_invalid([0x80, 0x80, 0x80, 0x80, 0x80, 0x00]);
	varint_invalid([0x80, 0x80, 0x80, 0x80, 0x10]);
	varint_invalid([0xff, 0xff, 0xff, 0xff, 0xff]);
};