summaryrefslogtreecommitdiff
path: root/mcproto/+test.ha
diff options
context:
space:
mode:
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]);
+};