public int writeVarUint32(int v) {
    // ensure at least 8 bytes are writable at once, so jvm-jit
    // generated code is smaller. Otherwise, `MapRefResolver.writeRefOrNull`
    // may be `callee is too large`/`already compiled into a big method`
    ensure(writerIndex + 8);
    int varintBytes = _unsafePutVarUint36Small(writerIndex, v);
    writerIndex += varintBytes;
    return varintBytes;
  }
@Test
  public void testWriteVarUint32() {
    for (int i = 0; i < 32; i++) {
      MemoryBuffer buf = MemoryUtils.buffer(8);
      for (int j = 0; j < i; j++) {
        buf.writeByte((byte) 1); // make address unaligned.
        buf.readByte();
      }
      checkVarUint32(buf, 1, 1);
      checkVarUint32(buf, 1 << 6, 1);
      checkVarUint32(buf, 1 << 7, 2);
      checkVarUint32(buf, 1 << 13, 2);
      checkVarUint32(buf, 1 << 14, 3);
      checkVarUint32(buf, 1 << 20, 3);
      checkVarUint32(buf, 1 << 21, 4);
      checkVarUint32(buf, 1 << 27, 4);
      checkVarUint32(buf, 1 << 28, 5);
      checkVarUint32(buf, Integer.MAX_VALUE, 5);

      checkVarUint32(buf, -1);
      checkVarUint32(buf, -1 << 6);
      checkVarUint32(buf, -1 << 7);
      checkVarUint32(buf, -1 << 13);
      checkVarUint32(buf, -1 << 14);
      checkVarUint32(buf, -1 << 20);
      checkVarUint32(buf, -1 << 21);
      checkVarUint32(buf, -1 << 27);
      checkVarUint32(buf, -1 << 28);
      checkVarUint32(buf, Byte.MIN_VALUE);
      checkVarUint32(buf, Short.MIN_VALUE);
      checkVarUint32(buf, Integer.MIN_VALUE);
    }
  }