Add some tests for ByteBuffer

This commit is contained in:
konsoletyper 2014-10-25 12:40:23 +04:00
parent f4641dc642
commit e7d51b6435
2 changed files with 95 additions and 3 deletions

View File

@ -56,9 +56,6 @@ public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBu
public abstract TByteBuffer put(int index, byte b); public abstract TByteBuffer put(int index, byte b);
public TByteBuffer get(byte[] dst, int offset, int length) { public TByteBuffer get(byte[] dst, int offset, int length) {
if (remaining() < length) {
throw new TBufferUnderflowException();
}
if (offset < 0 || offset >= dst.length) { if (offset < 0 || offset >= dst.length) {
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")"); throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
} }
@ -66,6 +63,9 @@ public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBu
throw new IndexOutOfBoundsException("The last byte in dst " + (offset + length) + " is outside " + throw new IndexOutOfBoundsException("The last byte in dst " + (offset + length) + " is outside " +
"of array of size " + dst.length); "of array of size " + dst.length);
} }
if (remaining() < length) {
throw new TBufferUnderflowException();
}
if (length < 0) { if (length < 0) {
throw new IndexOutOfBoundsException("Length " + length + " must be non-negative"); throw new IndexOutOfBoundsException("Length " + length + " must be non-negative");
} }

View File

@ -283,4 +283,96 @@ public class ByteBufferTest {
ByteBuffer buffer = ByteBuffer.wrap(array).asReadOnlyBuffer(); ByteBuffer buffer = ByteBuffer.wrap(array).asReadOnlyBuffer();
buffer.put(0, (byte)2); buffer.put(0, (byte)2);
} }
@Test
public void getsBytes() {
byte[] array = { 2, 3, 5, 7 };
ByteBuffer buffer = ByteBuffer.wrap(array);
buffer.get();
byte[] receiver = new byte[2];
buffer.get(receiver, 0, 2);
assertThat(buffer.position(), is(3));
assertThat(receiver, is(new byte[] { 3, 5 }));
}
@Test
public void gettingBytesFromEmptyBufferCausesError() {
byte[] array = { 2, 3, 5, 7 };
ByteBuffer buffer = ByteBuffer.wrap(array);
buffer.limit(3);
byte[] receiver = new byte[4];
try {
buffer.get(receiver, 0, 4);
fail("Error expected");
} catch (BufferUnderflowException e) {
assertThat(receiver, is(new byte[4]));
assertThat(buffer.position(), is(0));
}
}
@Test
public void gettingBytesWithIllegalArgumentsCausesError() {
byte[] array = { 2, 3, 5, 7 };
ByteBuffer buffer = ByteBuffer.wrap(array);
byte[] receiver = new byte[4];
try {
buffer.get(receiver, 0, 5);
} catch (IndexOutOfBoundsException e) {
assertThat(receiver, is(new byte[4]));
assertThat(buffer.position(), is(0));
}
try {
buffer.get(receiver, -1, 3);
} catch (IndexOutOfBoundsException e) {
assertThat(receiver, is(new byte[4]));
assertThat(buffer.position(), is(0));
}
try {
buffer.get(receiver, 6, 3);
} catch (IndexOutOfBoundsException e) {
assertThat(receiver, is(new byte[4]));
assertThat(buffer.position(), is(0));
}
}
@Test
public void putsBytes() {
byte[] array = new byte[4];
ByteBuffer buffer = ByteBuffer.wrap(array);
buffer.get();
byte[] data = { 2, 3 };
buffer.put(data, 0, 2);
assertThat(buffer.position(), is(3));
assertThat(array, is(new byte[] {0, 2, 3, 0 }));
}
@Test
public void compacts() {
byte[] array = { 2, 3, 5, 7 };
ByteBuffer buffer = ByteBuffer.wrap(array);
buffer.get();
buffer.mark();
buffer.compact();
assertThat(array, is(new byte[] { 3, 5, 7, 7 }));
assertThat(buffer.position(), is(3));
assertThat(buffer.limit(), is(4));
assertThat(buffer.capacity(), is(4));
try {
buffer.reset();
fail("Exception expected");
} catch (InvalidMarkException e) {
// ok
}
}
@Test
public void marksPosition() {
byte[] array = { 2, 3, 5, 7 };
ByteBuffer buffer = ByteBuffer.wrap(array);
buffer.position(1);
buffer.mark();
buffer.position(2);
buffer.reset();
assertThat(buffer.position(), is(1));
}
} }