@Test
public void testDuplicate() {

    for (int i = 0; i < buffer.capacity(); i ++) {
        byte value = (byte) random.nextInt();
        buffer.setByte(i, value);
    }
    final int readerIndex = CAPACITY / 3;
    final int writerIndex = CAPACITY * 2 / 3;
    buffer.setIndex(readerIndex, writerIndex);
    // Make sure all properties are copied.
    ByteBuf duplicate = buffer.duplicate();
    assertSame(buffer.order(), duplicate.order());
    assertEquals(buffer.readableBytes(), duplicate.readableBytes());
    assertEquals(0, buffer.compareTo(duplicate));
    // Make sure the buffer content is shared.
    buffer.setByte(readerIndex, (byte) (buffer.getByte(readerIndex) + 1));
    assertEquals(buffer.getByte(readerIndex), duplicate.getByte(duplicate.readerIndex()));
    duplicate.setByte(duplicate.readerIndex(), (byte) (duplicate.getByte(duplicate.readerIndex()) + 1));
    assertEquals(buffer.getByte(readerIndex), duplicate.getByte(duplicate.readerIndex()));
}