package com.peterhi.runtime;
import static org.junit.Assert.*;
import java.math.BigInteger;
import java.util.Arrays;
import org.junit.Test;
import com.peterhi.runtime.Buffer;
public class BufferTest {
@Test
public void testIsValidEndianness() throws Exception {
assertEquals(true, Buffer.isValidEndianness(Buffer.BIG_ENDIAN));
assertEquals(true, Buffer.isValidEndianness(Buffer.LITTLE_ENDIAN));
assertEquals(false, Buffer.isValidEndianness(Integer.MAX_VALUE));
}
@Test
public void testConstructors() throws Exception {
{
Buffer capacityInBytes = new Buffer(Buffer.CAPACITY_DEFAULT);
assertEquals(Buffer.CAPACITY_DEFAULT, capacityInBytes.size());
assertEquals(0, capacityInBytes.readable());
assertEquals(Byte.SIZE * Buffer.CAPACITY_DEFAULT, capacityInBytes.writable());
assertEquals(0, capacityInBytes.written());
}
try {
Buffer negativeCapacityInBytes = new Buffer(-1);
fail();
} catch (IllegalArgumentException ex) {
}
try {
Buffer zeroCapacityInBytes = new Buffer(0);
fail();
} catch (IllegalArgumentException ex) {
}
{
Buffer defaultConstructor = new Buffer();
assertEquals(Buffer.CAPACITY_DEFAULT, defaultConstructor.size());
assertEquals(0, defaultConstructor.readable());
assertEquals(Byte.SIZE * Buffer.CAPACITY_DEFAULT, defaultConstructor.writable());
assertEquals(0, defaultConstructor.written());
}
{
byte[] existingData = new byte[32];
Buffer usingExistingData = new Buffer(existingData);
assertEquals(existingData.length, usingExistingData.size());
assertEquals(Byte.SIZE * existingData.length, usingExistingData.readable());
assertEquals(0, usingExistingData.writable());
assertEquals(Byte.SIZE * existingData.length, usingExistingData.written());
}
try {
Buffer usingNullExistingData = new Buffer(null);
fail();
} catch (NullPointerException ex) {
}
try {
byte[] emptyExistingData = new byte[0];
Buffer usingEmptyExistingData = new Buffer(emptyExistingData);
fail();
} catch (IllegalArgumentException ex) {
}
{
byte[] existingData = new byte[32];
Buffer existingDataOffsetLength = new Buffer(existingData, 0, existingData.length);
assertEquals(existingData.length, existingDataOffsetLength.size());
assertEquals(existingData.length, existingDataOffsetLength.size());
assertEquals(Byte.SIZE * existingData.length, existingDataOffsetLength.readable());
assertEquals(0, existingDataOffsetLength.writable());
assertEquals(Byte.SIZE * existingData.length, existingDataOffsetLength.written());
}
try {
Buffer nullDataOffsetLength = new Buffer(null, 0, 1);
fail();
} catch (NullPointerException ex) {
}
try {
byte[] emptyExistingData = new byte[0];
Buffer emptyDataOffetLength = new Buffer(emptyExistingData, 0, 1);
fail();
} catch (IllegalArgumentException ex) {
}
try {
byte[] existingData = new byte[32];
Buffer dataNegativeOffsetLength = new Buffer(existingData, -1, existingData.length);
fail();
} catch (IllegalArgumentException ex) {
}
try {
byte[] existingData = new byte[32];
Buffer dataOffsetNegativeLength = new Buffer(existingData, 0, -1);
fail();
} catch (IllegalArgumentException ex) {
}
try {
byte[] existingData = new byte[32];
Buffer dataOffsetZeroLength = new Buffer(existingData, 0, 0);
fail();
} catch (IllegalArgumentException ex) {
}
try {
byte[] existingData = new byte[32];
Buffer dataOutOfBoundsOffsetLength = new Buffer(existingData, 0, existingData.length + 1);
fail();
} catch (IllegalArgumentException ex) {
}
{
// 11[111111 1111]1111
// 00[111111 1111]0000 ==> not aligned
byte[] existingData = new byte[] { -1, -1 };
Buffer existingDataOffsetLengthBitsNotAligned = new Buffer(existingData, 4, 10, false);
assertEquals(2, existingDataOffsetLengthBitsNotAligned.size());
assertEquals(14, existingDataOffsetLengthBitsNotAligned.readable());
assertEquals(14, existingDataOffsetLengthBitsNotAligned.written());
assertEquals(2, existingDataOffsetLengthBitsNotAligned.writable());
assertEquals(0, existingDataOffsetLengthBitsNotAligned.read());
existingDataOffsetLengthBitsNotAligned.skip(3);
assertEquals(1, existingDataOffsetLengthBitsNotAligned.read());
existingDataOffsetLengthBitsNotAligned.skip(9);
assertEquals(-1, existingDataOffsetLengthBitsNotAligned.read());
}
{
// 111[11111 1111]1111
// 0000000[1 11111111] ==> aligned
byte[] existingData = new byte[] { -1, -1 };
Buffer existingDataOffsetLengthBitsAligned = new Buffer(existingData, 4, 9, true);
assertEquals(2, existingDataOffsetLengthBitsAligned.size());
assertEquals(9, existingDataOffsetLengthBitsAligned.readable());
assertEquals(9, existingDataOffsetLengthBitsAligned.written());
assertEquals(7, existingDataOffsetLengthBitsAligned.writable());
assertEquals(1, existingDataOffsetLengthBitsAligned.read());
existingDataOffsetLengthBitsAligned.skip(7);
assertEquals(1, existingDataOffsetLengthBitsAligned.read());
assertEquals(-1, existingDataOffsetLengthBitsAligned.read());
}
{
byte[] existingData = new byte[] { (byte )254, (byte )254 };
Buffer testBuffer = new Buffer(existingData, 2, 8, true);
assertArrayEquals(new byte[] { (byte )191 }, testBuffer.toByteArray(Buffer.COPY_METHOD_ALL_DATA));
}
try {
Buffer nullExistingDataOffsetLengthBits = new Buffer(null, 0, 0, false);
fail();
} catch (NullPointerException ex) {
}
try {
byte[] existingData = new byte[] { -1, -1 };
Buffer existingDataNegativeOffsetLengthBits = new Buffer(existingData, -1, 0, false);
fail();
} catch (IllegalArgumentException ex) {
}
try {
byte[] existingData = new byte[] { -1, -1 };
Buffer existingDataOffsetNegativeLengthBits = new Buffer(existingData, 0, -1, false);
fail();
} catch (IllegalArgumentException ex) {
}
try {
byte[] existingData = new byte[] { -1, -1 };
Buffer existingDataOffsetZeroLengthBits = new Buffer(existingData, 0, 0, false);
fail();
} catch (IllegalArgumentException ex) {
}
try {
byte[] existingData = new byte[] { -1, -1 };
Buffer existingDataOutOfBoundsOffsetLengthBits = new Buffer(existingData, 0, 17, false);
fail();
} catch (IllegalArgumentException ex) {
}
}
@Test
public void testSizeFunctions() throws Exception {
Buffer testBuffer = new Buffer(1);
assertEquals(0, testBuffer.readable());
assertEquals(Byte.SIZE, testBuffer.writable());
assertEquals(0, testBuffer.written());
assertEquals(1, testBuffer.size());
}
@Test
public void testReadWriteBit() throws Exception {
Buffer readWriteBuffer = new Buffer();
readWriteBuffer.write(1);
readWriteBuffer.write(0);
readWriteBuffer.write(1);
assertEquals(1, readWriteBuffer.read());
assertEquals(0, readWriteBuffer.read());
assertEquals(1, readWriteBuffer.read());
assertEquals(-1, readWriteBuffer.read());
assertEquals(Buffer.CAPACITY_DEFAULT, readWriteBuffer.size());
assertEquals(Byte.SIZE * Buffer.CAPACITY_DEFAULT - 3, readWriteBuffer.writable());
assertEquals(0, readWriteBuffer.readable());
assertEquals(3, readWriteBuffer.written());
}
@Test
public void testReadWriteBitAsBoolean() throws Exception {
Buffer readWriteBuffer = new Buffer();
readWriteBuffer.writeAsBoolean(true);
readWriteBuffer.writeAsBoolean(false);
readWriteBuffer.writeAsBoolean(true);
assertEquals(true, readWriteBuffer.readAsBoolean());
assertEquals(false, readWriteBuffer.readAsBoolean());
assertEquals(true, readWriteBuffer.readAsBoolean());
assertEquals(-1, readWriteBuffer.read());
assertEquals(Buffer.CAPACITY_DEFAULT, readWriteBuffer.size());
assertEquals(Byte.SIZE * Buffer.CAPACITY_DEFAULT - 3, readWriteBuffer.writable());
assertEquals(0, readWriteBuffer.readable());
assertEquals(3, readWriteBuffer.written());
}
@Test
public void testReadWriteBits() throws Exception {
Buffer readWriteBuffer = new Buffer();
BigInteger aValueBiggerThanIntMax = BigInteger.valueOf((long )Integer.MAX_VALUE + 1L);
readWriteBuffer.write(Integer.SIZE, aValueBiggerThanIntMax, Buffer.BIG_ENDIAN);
BigInteger aTestValueBiggerThanIntMax = readWriteBuffer.read(Integer.SIZE, false, Buffer.BIG_ENDIAN);
assertEquals(aValueBiggerThanIntMax, aTestValueBiggerThanIntMax);
readWriteBuffer.write(Integer.SIZE, aValueBiggerThanIntMax, Buffer.BIG_ENDIAN);
int anOverflowedIntBiggerThanIntMax = readWriteBuffer.read(Integer.SIZE, false, Buffer.BIG_ENDIAN).intValue();
assertEquals(Integer.MIN_VALUE, anOverflowedIntBiggerThanIntMax);
readWriteBuffer.write(Integer.SIZE, aValueBiggerThanIntMax, Buffer.BIG_ENDIAN);
BigInteger anEmulatedOverflowedBigIntBiggerThanIntMax = readWriteBuffer.read(Integer.SIZE, true, Buffer.BIG_ENDIAN);
assertEquals(anOverflowedIntBiggerThanIntMax, anEmulatedOverflowedBigIntBiggerThanIntMax.intValue());
assertFalse(aValueBiggerThanIntMax.equals(anEmulatedOverflowedBigIntBiggerThanIntMax));
}
@Test
public void testSkipFill() throws Exception {
// 00000000[]
Buffer testBuffer = new Buffer(1);
// 0000000[1]
testBuffer.write(1);
// 0[1111111]
// ||||||
// filled
testBuffer.fill(6, 1);
// [11111111]
// ||||||
// filled
testBuffer.write(1);
// [1111111]1
assertEquals(1, testBuffer.read());
// [111111]11
assertEquals(1, testBuffer.read());
// [1]1111111
// |||||
// skipped
testBuffer.skip(5);
// []11111111
// |||||
// skipped
assertEquals(1, testBuffer.read());
// []11111111 ==> EOF
// |||||
// skipped
assertEquals(-1, testBuffer.read());
}
@Test
public void testMarkReset() throws Exception {
// [01100000]
Buffer withExistingData = new Buffer(new byte[] { 96 });
// [0110000]0 !
assertEquals(0, withExistingData.read());
// [011]00000 !
withExistingData.skip(4);
// [011]!00000 ==> SNAPSHOT marked as '!'
withExistingData.mark();
// [01]1!00000
assertEquals(1, withExistingData.read());
// [0]11!00000
assertEquals(1, withExistingData.read());
// []011!00000
assertEquals(0, withExistingData.read());
// []011!00000 ==> EOF
assertEquals(-1, withExistingData.read());
// [011]00000 ! ==> Replace '!' with ']', and reset '!'
withExistingData.reset();
// [01]100000 !
assertEquals(1, withExistingData.read());
// [0]1100000 !
assertEquals(1, withExistingData.read());
// []01100000 !
assertEquals(0, withExistingData.read());
// []01100000 ! ==> EOF
assertEquals(-1, withExistingData.read());
}
@Test
public void testReadWriteAlign() throws Exception {
// 00000000 00000000[]
Buffer testBuffer = new Buffer(2);
// 00000000 0000000[1]
testBuffer.write(1);
// 00000000 [00000001]
assertEquals(7, testBuffer.alignWrite());
// 00000000 [00000001]
assertEquals(0, testBuffer.alignWrite());
// 0000000[1 00000001]
testBuffer.write(1);
// 0000000[1 0000000]1
assertEquals(1, testBuffer.read());
// 0000000[1 000000]01
assertEquals(0, testBuffer.read());
// 0000000[1 ]00000001
assertEquals(6, testBuffer.alignRead());
// 0000000[1 ]00000001
assertEquals(0, testBuffer.alignRead());
// 0000000[]1 00000001
assertEquals(1, testBuffer.read());
// 0000000[]1 00000001 ==> EOF
assertEquals(-1, testBuffer.read());
// 0000000[]1 00000001 ==> ERR: ']' cannot go beyond '['
assertEquals(-1, testBuffer.alignRead());
}
@Test
public void testToByteArray() throws Exception {
{
Buffer allData = new Buffer(1);
allData.fill(8, 1);
allData.read();
assertArrayEquals(new byte[] { -1 }, allData.toByteArray(Buffer.COPY_METHOD_ALL_DATA));
}
{
Buffer allData = new Buffer(2);
allData.fill(12, 1);
allData.skip(4);
assertArrayEquals(new byte[] { -1, 15 }, allData.toByteArray(Buffer.COPY_METHOD_ALL_DATA));
}
{
Buffer defaultBuf = new Buffer(1);
defaultBuf.fill(8, 1);
defaultBuf.read();
assertArrayEquals(new byte[] { (byte )254 }, defaultBuf.toByteArray(Buffer.COPY_METHOD_DEFAULT));
}
{
Buffer defaultBuf = new Buffer(2);
defaultBuf.fill(12, 1);
defaultBuf.skip(4);
assertArrayEquals(new byte[] { (byte )240, 15 }, defaultBuf.toByteArray(Buffer.COPY_METHOD_DEFAULT));
}
{
Buffer aligned = new Buffer(1);
aligned.fill(8, 1);
aligned.read();
assertArrayEquals(new byte[] { 127 }, aligned.toByteArray(Buffer.COPY_METHOD_ALIGNED));
}
{
Buffer aligned = new Buffer(2);
aligned.fill(12, 1);
aligned.skip(4);
assertArrayEquals(new byte[] { (byte )255 }, aligned.toByteArray(Buffer.COPY_METHOD_ALIGNED));
}
try {
Buffer invalidCopyMethod = new Buffer(new byte[0]);
invalidCopyMethod.toByteArray(Integer.MIN_VALUE);
fail();
} catch (IllegalArgumentException ex) {
}
}
}