public void testGetAndPut() {
for (int i = 0; i < 10; i++) {
byte[] b = new byte[random.nextInt(1000) + 100];
random.nextBytes(b);
Buffer buffer = newBuffer(b.length);
buffer.put(b);
assertEquals(b.length, buffer.position());
buffer.flip();
assertEquals(0, buffer.position());
assertEquals(b.length, buffer.limit());
assertEquals(b.length, buffer.capacity());
assertEquals(b.length, buffer.remaining());
assertTrue(buffer.hasRemaining());
assertEquals(buffer.asByteBuffer(), ByteBuffer.wrap(b));
random.nextBytes(b);
buffer.put(ByteBuffer.wrap(b));
assertEquals(b.length, buffer.position());
assertEquals(b.length, buffer.limit());
assertEquals(b.length, buffer.capacity());
assertEquals(0, buffer.remaining());
assertFalse(buffer.hasRemaining());
buffer.flip();
assertEquals(buffer.asByteBuffer(), ByteBuffer.wrap(b));
assertEquals(0, buffer.position());
assertEquals(b.length, buffer.limit());
assertEquals(b.length, buffer.remaining());
assertEquals(b.length, buffer.capacity());
assertTrue(buffer.hasRemaining());
Buffer b1 = newBuffer(b.length);
random.nextBytes(b);
b1.put(0, b);
buffer.put(b1);
assertEquals(b.length, buffer.position());
assertEquals(b.length, buffer.limit());
assertEquals(b.length, buffer.capacity());
assertEquals(0, buffer.remaining());
assertFalse(buffer.hasRemaining());
buffer.flip();
assertEquals(buffer.asByteBuffer(), ByteBuffer.wrap(b));
assertEquals(0, buffer.position());
assertEquals(b.length, buffer.limit());
assertEquals(b.length, buffer.remaining());
assertEquals(b.length, buffer.capacity());
assertTrue(buffer.hasRemaining());
ByteBuffer b2 = ByteBuffer.wrap(b);
for (int j = 0; j < 100; j++) {
int index = Math.max(0, random.nextInt(b.length) - 8);
assertEquals(buffer.get(index), b2.get(index));
assertEquals(buffer.getShort(index), b2.getShort(index));
assertEquals(buffer.getChar(index), b2.getChar(index));
assertEquals(buffer.getInt(index), b2.getInt(index));
assertEquals(buffer.getLong(index), b2.getLong(index));
assertEquals(new Float(buffer.getFloat(index)), new Float(b2
.getFloat(index)));
assertEquals(new Double(buffer.getDouble(index)), new Double(b2
.getDouble(index)));
}
while (buffer.hasRemaining()) {
assertEquals(b2.get(), buffer.get());
}
assertFalse(buffer.hasRemaining());
assertEquals(b.length, buffer.position());
assertEquals(b.length, buffer.limit());
assertEquals(0, buffer.remaining());
buffer.clear();
assertEquals(0, buffer.position());
assertEquals(b.length, buffer.limit());
assertEquals(b.length, buffer.remaining());
for (int j = 0; j < b.length; j++) {
buffer.put(b[j]);
assertEquals(j + 1, buffer.position());
assertEquals(b.length, buffer.limit());
assertEquals(b.length - j - 1, buffer.remaining());
}
for (int j = 0; j < 100; j++) {
int index = Math.max(0, random.nextInt(b.length) - 8);
short s = (short) random.nextInt();
buffer.putShort(index, s);
assertEquals(buffer.getShort(index), s);
int in = random.nextInt();
buffer.putInt(index, in);
assertEquals(buffer.getInt(index), in);
long l = random.nextLong();
buffer.putLong(index, l);
assertEquals(buffer.getLong(index), l);
float f = random.nextFloat();
buffer.putFloat(index, f);
assertEquals(new Float(buffer.getFloat(index)), new Float(f));
double d = random.nextDouble();
buffer.putDouble(index, d);
assertEquals(new Double(buffer.getDouble(index)), new Double(d));
}
}
Buffer buffer = newBuffer(256);
for (int i = 0; i < 256; i++) {
buffer.put((byte) i);
}
buffer.flip();
for (int i = 0; i < 5; i++) {
byte[] b = new byte[random.nextInt(buffer.capacity())];
int start = random.nextInt(buffer.capacity() - b.length);
buffer.get(start, b);
for (int j = 0; j < b.length; j++) {
assertEquals(b[j], (byte) (start + j));
}
}
for (int i = 0; i < 5; i++) {
buffer.clear();
byte[] b = new byte[random.nextInt(buffer.capacity())];
int start = random.nextInt(buffer.capacity() - b.length);
buffer.position(start);
buffer.get(b);
assertEquals(buffer.position(), start + b.length);
for (int j = 0; j < b.length; j++) {
assertEquals(b[j], (byte) (start + j));
}
}
}