summaryrefslogtreecommitdiff
path: root/mcproto/+test.ha
diff options
context:
space:
mode:
authorLassi Pulkkinen <lassi@pulk.fi>2024-10-31 03:11:21 +0200
committerLassi Pulkkinen <lassi@pulk.fi>2024-10-31 03:51:35 +0200
commitae44478b30d890fe0fb04022f44d474dcdcc3f9d (patch)
tree5f462459ae4b47d22114eed717d1382d08cf4dfe /mcproto/+test.ha
Initial commit (import old repo)HEADmain
Diffstat (limited to 'mcproto/+test.ha')
-rw-r--r--mcproto/+test.ha47
1 files changed, 47 insertions, 0 deletions
diff --git a/mcproto/+test.ha b/mcproto/+test.ha
new file mode 100644
index 0000000..5ce070c
--- /dev/null
+++ b/mcproto/+test.ha
@@ -0,0 +1,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]);
+};