}
@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) {
}
}