@Test
public void writeVarInt() throws IOException {

    // v == 0, encode with one byte
    writer.writeVarInt(0L);
    assertAndResetStreamContents(new byte[]{(byte) 0x00});
    // v == 0xfd - 1, encode with one byte
    writer.writeVarInt(252);
    assertAndResetStreamContents(new byte[]{(byte) 0xfc});
    // v == 0xfd, encode with uint16
    writer.writeVarInt(253);
    assertAndResetStreamContents(new byte[]{(byte) 0xfd, (byte) 0xfd, (byte) 0x00});
    // v == 0xfd + 1, encode with uint16
    writer.writeVarInt(254);
    assertAndResetStreamContents(new byte[]{(byte) 0xfd, (byte) 0xfe, (byte) 0x00});
    // v == 0xffff - 1, encode with uint16
    writer.writeVarInt(65_534);
    assertAndResetStreamContents(new byte[]{(byte) 0xfd, (byte) 0xfe, (byte) 0xff});
    // v == 0xffff, encode with uint16
    writer.writeVarInt(65_535);
    assertAndResetStreamContents(new byte[]{(byte) 0xfd, (byte) 0xff, (byte) 0xff});
    // v == 0xffff + 1, encode with uint32
    writer.writeVarInt(65_536);
    assertAndResetStreamContents(new byte[]{(byte) 0xfe, (byte) 0x00, (byte) 0x00, (byte) 0x01,
        (byte) 0x00});
    // v == 0xffffffff - 1, encode with uint32
    writer.writeVarInt(4_294_967_294L);
    assertAndResetStreamContents(new byte[]{(byte) 0xfe, (byte) 0xfe, (byte) 0xff, (byte) 0xff,
        (byte) 0xff});
    // v == 0xffffffff, encode with uint32
    writer.writeVarInt(4_294_967_295L);
    assertAndResetStreamContents(new byte[]{(byte) 0xfe, (byte) 0xff, (byte) 0xff, (byte) 0xff,
        (byte) 0xff});
    // v == 0xffffffff + 1, encode with uint64
    writer.writeVarInt(4_294_967_296L);
    assertAndResetStreamContents(new byte[]{(byte) 0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
        0x00, 0x00});
}