@Test
public void testCompareTo() {

    try {
        buffer.compareTo(null);
        fail();
    } catch (NullPointerException e) {
        // Expected
    }
    // Fill the random stuff
    byte[] value = new byte[32];
    random.nextBytes(value);
    // Prevent overflow / underflow
    if (value[0] == 0) {
        value[0] ++;
    } else if (value[0] == -1) {
        value[0] --;
    }
    buffer.setIndex(0, value.length);
    buffer.setBytes(0, value);
    assertEquals(0, buffer.compareTo(wrappedBuffer(value)));
    assertEquals(0, buffer.compareTo(wrappedBuffer(value).order(LITTLE_ENDIAN)));
    value[0] ++;
    assertTrue(buffer.compareTo(wrappedBuffer(value)) < 0);
    assertTrue(buffer.compareTo(wrappedBuffer(value).order(LITTLE_ENDIAN)) < 0);
    value[0] -= 2;
    assertTrue(buffer.compareTo(wrappedBuffer(value)) > 0);
    assertTrue(buffer.compareTo(wrappedBuffer(value).order(LITTLE_ENDIAN)) > 0);
    value[0] ++;
    assertTrue(buffer.compareTo(wrappedBuffer(value, 0, 31)) > 0);
    assertTrue(buffer.compareTo(wrappedBuffer(value, 0, 31).order(LITTLE_ENDIAN)) > 0);
    assertTrue(buffer.slice(0, 31).compareTo(wrappedBuffer(value)) < 0);
    assertTrue(buffer.slice(0, 31).compareTo(wrappedBuffer(value).order(LITTLE_ENDIAN)) < 0);
    ByteBuf retainedSlice = buffer.retainedSlice(0, 31);
    assertTrue(retainedSlice.compareTo(wrappedBuffer(value)) < 0);
    retainedSlice.release();
    retainedSlice = buffer.retainedSlice(0, 31);
    assertTrue(retainedSlice.compareTo(wrappedBuffer(value).order(LITTLE_ENDIAN)) < 0);
    retainedSlice.release();
}