package com.peterhi.obsolete;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.EOFException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.junit.Test;
import com.peterhi.obsolete.Check;
import com.peterhi.obsolete.Data;
import com.peterhi.obsolete.InsufficientBufferException;
import com.peterhi.obsolete.RT;
import com.peterhi.obsolete.Stream;
public final class StreamTest {
static final class NumWithInfo {
public static final int UNDERFLOW = -1;
public static final int NORMAL = 0;
public static final int OVERFLOW = 1;
private final BigInteger number;
private final int bits;
private final boolean negative;
private final int flow;
public NumWithInfo(long number, int bits, boolean negative, int flow) {
this(BigInteger.valueOf(number), bits, negative, flow);
}
public NumWithInfo(BigInteger number, int bits, boolean negative, int flow) {
Check.forNull(number);
Check.forNegative(bits);
Check.forUndefined(flow, UNDERFLOW, NORMAL, OVERFLOW);
this.number = number;
this.bits = bits;
this.negative = negative;
this.flow = flow;
}
public BigInteger getNumber() {
return number;
}
public int getBits() {
return bits;
}
public boolean isNegative() {
return negative;
}
public int getFlow() {
return flow;
}
public boolean isNormal() {
return flow == NORMAL;
}
public boolean isUnderflow() {
return flow == UNDERFLOW;
}
public boolean isOverflow() {
return flow == OVERFLOW;
}
public int getVarLongType(boolean signed) {
int[] types = new int[] { RT.INT6, RT.INT14, RT.INT30 };
for (int type : types) {
int bits;
long max;
long min;
if (signed) {
bits = (int )(Math.pow(2, type) * Byte.SIZE - 2);
max = (long )(Math.pow(2, bits - 1) - 1);
min = (long )(-Math.pow(2, bits - 1));
} else {
bits = (int )(Math.pow(2, type) * Byte.SIZE - 2);
max = (long )(Math.pow(2, bits) - 1);
min = 0;
}
if (number.longValue() >= min && number.longValue() <= max) {
return type;
}
}
return RT.INT62;
}
public int getVarLongBits(boolean signed) {
switch (getVarLongType(signed)) {
case RT.INT6:
return 6;
case RT.INT14:
return 14;
case RT.INT30:
return 30;
default:
return 62;
}
}
@Override
public String toString() {
return number.toString();
}
}
private static byte[] sampleBytes() {
return new byte[] {
(byte )(Byte.MIN_VALUE - 1),
(byte )(Byte.MIN_VALUE),
(byte )(Byte.MIN_VALUE + 1),
(byte )(-1),
(byte )(0),
(byte )(1),
(byte )(Byte.MAX_VALUE - 1),
(byte )(Byte.MAX_VALUE),
(byte )(Byte.MAX_VALUE + 1)
};
}
private static short[] sampleShorts() {
return new short[] {
(short )(Short.MIN_VALUE - 1),
(short )(Short.MIN_VALUE),
(short )(Short.MIN_VALUE + 1),
(short )(-1),
(short )(0),
(short )(1),
(short )(Short.MAX_VALUE - 1),
(short )(Short.MAX_VALUE),
(short )(Short.MAX_VALUE - 1)
};
}
private static char[] sampleChars() {
return new char[] {
(char )(Character.MIN_VALUE - 1),
(char )(Character.MIN_VALUE),
(char )(Character.MIN_VALUE + 1),
(char )(-1),
(char )(0),
(char )(1),
(char )(Character.MAX_VALUE - 1),
(char )(Character.MAX_VALUE),
(char )(Character.MAX_VALUE + 1)
};
}
private static float[] sampleFloats() {
return new float[] {
(float )(Float.MIN_VALUE),
(float )(-1.0f),
(float )(0.0f),
(float )(1.0f),
(float )(Float.MAX_VALUE)
};
}
private static double[] sampleDoubles() {
return new double[] {
(double )(Double.MIN_VALUE),
(double )(-1.0),
(double )(0.0),
(double )(1.0f),
(double )(Double.MIN_VALUE)
};
}
private static NumWithInfo[] genNumWithInfos(int max) {
List<NumWithInfo> infos = new ArrayList<NumWithInfo>();
infos.add(new NumWithInfo(-2, 1, true, NumWithInfo.UNDERFLOW));
infos.add(new NumWithInfo(-1, 1, true, NumWithInfo.NORMAL));
infos.add(new NumWithInfo(0, 1, false, NumWithInfo.NORMAL));
infos.add(new NumWithInfo(1, 1, false, NumWithInfo.OVERFLOW));
for (int i = 1; i < max; i++) {
BigInteger value = BigInteger.valueOf(2).pow(i);
int bits = i + 1;
NumWithInfo underflow = new NumWithInfo(value.negate().subtract(BigInteger.ONE), bits, true, NumWithInfo.UNDERFLOW);
NumWithInfo bottom = new NumWithInfo(value.negate(), bits, true, NumWithInfo.NORMAL);
NumWithInfo nearBottom = new NumWithInfo(value.negate().add(BigInteger.ONE), bits, true, NumWithInfo.NORMAL);
NumWithInfo nearTop = new NumWithInfo(value.subtract(BigInteger.valueOf(2)), bits, false, NumWithInfo.NORMAL);
NumWithInfo top = new NumWithInfo(value.subtract(BigInteger.ONE), bits, false, NumWithInfo.NORMAL);
NumWithInfo overflow = new NumWithInfo(value, bits, false, NumWithInfo.OVERFLOW);
infos.add(underflow);
infos.add(bottom);
infos.add(nearBottom);
infos.add(nearTop);
infos.add(top);
infos.add(overflow);
}
return infos.toArray(new NumWithInfo[infos.size()]);
}
@Test
public void pCtor_Data() throws Exception {
try {
Data data = new Data();
Stream stream = new Stream(data);
assertEquals(data, stream.getData());
} finally {
}
}
@Test
public void fCtor_Data() throws Exception {
try {
Stream stream = new Stream((Data )null);
fail();
} catch (NullPointerException ex) {
} finally {
}
}
@Test
public void pCtor_void() throws Exception {
try {
Stream stream = new Stream();
} finally {
}
}
@Test
public void fCtor_void() throws Exception {
}
@Test
public void pGetData_void() throws Exception {
try {
Data data = new Data();
Stream stream = new Stream(data);
assertEquals(data, stream.getData());
} finally {
}
}
@Test
public void fGetData_void() throws Exception {
}
@Test
public void pReadBit_void() throws Exception {
try {
byte[] buffer = new byte[] {
Integer.valueOf("01010101", 2).byteValue()
};
Data data = new Data(buffer);
Stream stream = new Stream(buffer);
for (int i = 0; i < 9; i++) {
assertEquals(data.read(), stream.readBit());
}
} finally {
}
}
@Test
public void fReadBit_void() throws Exception {
}
@Test
public void pReadBit2_void() throws Exception {
try {
byte[] buffer = new byte[] {
Integer.valueOf("01010101", 2).byteValue()
};
Data data = new Data(buffer);
Stream stream = new Stream(buffer);
for (int i = 0; i < 8; i++) {
assertEquals(data.read2(), stream.readBit2());
}
} finally {
}
}
@Test
public void fReadBit2_void() throws Exception {
try {
Stream stream = new Stream();
stream.readBit2();
fail();
} catch (EOFException ex) {
}
}
@Test
public void pRead_void() throws Exception {
try {
int[] sample = new int[sampleBytes().length];
for (int i = 0; i < sampleBytes().length; i++) {
sample[i] = sampleBytes()[i] & 0xff;
}
Stream stream = new Stream();
stream.writeBit(1);
stream.write(sampleBytes());
stream.readBit();
for (int i = 0; i < sample.length; i++) {
assertEquals(sample[i], stream.read());
}
assertEquals(-1, stream.read());
assertEquals(0, stream.getData().readable());
} finally {
}
}
@Test
public void fRead_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.read();
fail();
} catch (InsufficientBufferException ex) {
}
}
@Test
public void pRead_byteArray() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.write(sampleBytes());
stream.writeBit(1);
stream.write(sampleBytes());
byte[] sample = new byte[sampleBytes().length];
stream.readBit();
assertEquals(sampleBytes().length, stream.read(sample));
assertArrayEquals(sampleBytes(), sample);
sample = new byte[4];
stream.readBit();
assertEquals(4, stream.read(sample));
for (int i = 0; i < 4; i++) {
assertEquals(sampleBytes()[i], sample[i]);
}
assertEquals(sampleBytes().length - 4, stream.available());
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.writeBit(1);
byte[] sample = new byte[2];
assertEquals(1, stream.read(sample));
assertEquals(1, stream.getData().readable());
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.writeBit(1);
byte[] sample = new byte[0];
assertEquals(0, stream.read(sample));
stream.skip(1);
assertEquals(0, stream.read(sample));
assertEquals(1, stream.readBit());
assertEquals(-1, stream.read(sample));
assertEquals(0, stream.getData().readable());
} finally {
}
}
@Test
public void fRead_byteArray() throws Exception {
try {
Stream stream = new Stream();
stream.read(null);
fail();
} catch (NullPointerException ex) {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
byte[] sample = new byte[1];
stream.read(sample);
fail();
} catch (InsufficientBufferException ex) {
}
}
@Test
public void pRead_byteArray_int_int() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.write(sampleBytes());
stream.writeBit(1);
stream.write(sampleBytes());
byte[] sample = new byte[sampleBytes().length];
stream.readBit();
assertEquals(sampleBytes().length, stream.read(sample));
assertArrayEquals(sampleBytes(), sample);
sample = new byte[sampleBytes().length];
stream.readBit();
assertEquals(4, stream.read(sample, 1, 4));
for (int i = 0; i < 4; i++) {
assertEquals(sampleBytes()[i], sample[i + 1]);
}
assertEquals(sampleBytes().length - 4, stream.available());
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.writeBit(1);
byte[] sample = new byte[5];
assertEquals(1, stream.read(sample, 2, 3));
assertEquals(1, stream.getData().readable());
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.writeBit(1);
byte[] sample = new byte[50];
assertEquals(0, stream.read(sample, 10, 0));
stream.skip(1);
assertEquals(0, stream.read(sample, 20, 0));
assertEquals(1, stream.readBit());
assertEquals(-1, stream.read(sample, 10, 0));
assertEquals(0, stream.getData().readable());
} finally {
}
}
@Test
public void fRead_byteArray_int_int() throws Exception {
try {
Stream stream = new Stream();
stream.read(null, 0, 0);
fail();
} catch (NullPointerException ex) {
}
try {
Stream stream = new Stream();
stream.read(new byte[0], -1, 0);
fail();
} catch (IllegalArgumentException ex) {
}
try {
Stream stream = new Stream();
stream.read(new byte[0], 0, -1);
fail();
} catch (IllegalArgumentException ex) {
}
try {
Stream stream = new Stream();
stream.read(new byte[0], 0, 1);
fail();
} catch (IndexOutOfBoundsException ex) {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.read(new byte[1], 0, 1);
fail();
} catch (InsufficientBufferException ex) {
}
}
@Test
public void pSkip_long() throws Exception {
try {
Stream stream = new Stream(sampleBytes());
stream.writeBit(1);
assertEquals(0, stream.skip(0));
assertEquals(1, stream.skip(1));
assertEquals(sampleBytes()[1], (byte )stream.read());
assertEquals(7, stream.skip(10));
assertEquals(0, stream.skip(0));
assertEquals(1, stream.getData().readable());
stream.readBit();
assertEquals(-1, stream.skip(5));
} finally {
}
}
@Test
public void fSkip_long() throws Exception {
try {
Stream stream = new Stream(sampleBytes());
stream.skip(-1);
fail();
} catch (IllegalArgumentException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.skip(1);
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pAvailable_void() throws Exception {
try {
Stream stream = new Stream();
assertEquals(0, stream.available());
stream.writeBit(1);
assertEquals(0, stream.available());
stream.write(8);
assertEquals(1, stream.available());
stream.writeInt(0, 7);
assertEquals(2, stream.available());
} finally {
}
}
@Test
public void fAvailable_void() throws Exception {
}
@Test
public void pClose_void() throws Exception {
}
@Test
public void fClose_void() throws Exception {
}
@Test
public void pMark_int() throws Exception {
try {
int whatever = 672695543;
Stream stream = new Stream(sampleBytes());
assertEquals(sampleBytes()[0], (byte )stream.read());
stream.mark(whatever);
assertEquals(sampleBytes()[1], (byte )stream.read());
assertEquals(sampleBytes()[2], (byte )stream.read());
stream.reset();
assertEquals(sampleBytes()[1], (byte )stream.read());
assertEquals(7, stream.available());
} finally {
}
}
@Test
public void fMark_int() throws Exception {
}
@Test
public void pReset_void() throws Exception {
try {
int whatever = 546543324;
Stream stream = new Stream(sampleBytes());
assertEquals(sampleBytes()[0], (byte )stream.read());
stream.reset();
assertEquals(sampleBytes()[1], (byte )stream.read());
stream.mark(whatever);
assertEquals(sampleBytes()[2], (byte )stream.read());
stream.reset();
assertEquals(sampleBytes()[2], (byte )stream.read());
assertEquals(6, stream.available());
} finally {
}
}
@Test
public void fReset_void() throws Exception {
}
@Test
public void pMarkSupported_void() throws Exception {
try {
Stream stream = new Stream();
assertEquals(true, stream.markSupported());
} finally {
}
}
@Test
public void fMarkSupported_void() throws Exception {
}
@Test
public void pWriteBit_int() throws Exception {
try {
Stream stream = new Stream();
for (int i = 0; i < 9; i++) {
stream.writeBit(i % 2);
}
stream.writeBit(43553455);
for (int i = 0; i < 9; i++) {
assertEquals(i % 2, stream.readBit());
}
assertEquals(1, stream.readBit());
assertEquals(-1, stream.readBit());
} finally {
}
}
@Test
public void fWriteBit_int() throws Exception {
}
@Test
public void pWriteBit2_boolean() throws Exception {
try {
Stream stream = new Stream();
for (int i = 0; i < 9; i++) {
stream.writeBit2(i % 2 == 0);
}
for (int i = 0; i < 9; i++) {
assertEquals(i % 2 == 0, stream.readBit2());
}
assertEquals(-1, stream.readBit());
} finally {
}
}
@Test
public void fWriteBit2_boolean() throws Exception {
}
@Test
public void pFlush() throws Exception {
}
@Test
public void fFlush() throws Exception {
}
@Test
public void pWrite_byteArray() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.write(sampleBytes());
stream.writeBit(1);
stream.readBit();
for (int i = 0; i < sampleBytes().length; i++) {
assertEquals(sampleBytes()[i], (byte )stream.read());
}
assertEquals(1, stream.getData().readable());
byte[] sample = new byte[0];
stream.write(sample);
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fWrite_byteArray() throws Exception {
try {
Stream stream = new Stream();
stream.write(null);
fail();
} catch (NullPointerException ex) {
}
}
@Test
public void pWrite_byteArray_int_int() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.write(sampleBytes(), 0, 1);
stream.writeBit(1);
stream.write(sampleBytes(), 1, 0);
stream.write(sampleBytes(), 1, sampleBytes().length - 1);
stream.writeBit(1);
stream.readBit();
assertEquals(sampleBytes()[0], (byte )stream.read());
stream.readBit();
for (int i = 0; i < sampleBytes().length - 1; i++) {
assertEquals(sampleBytes()[i + 1], (byte )stream.read());
}
assertEquals(1, stream.getData().readable());
byte[] sample = new byte[0];
stream.write(sample, 0, 0);
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fWrite_byteArray_int_int() throws Exception {
try {
Stream stream = new Stream();
stream.write(null, 0, 0);
fail();
} catch (NullPointerException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(new byte[0], -1, 0);
fail();
} catch (IllegalArgumentException ex) {
}
try {
Stream stream = new Stream();
stream.write(new byte[0], 0, -1);
fail();
} catch (IllegalArgumentException ex) {
}
try {
Stream stream = new Stream();
stream.write(new byte[0], 0, 1);
fail();
} catch (IndexOutOfBoundsException ex) {
}
}
@Test
public void pReadFully_byteArray() throws Exception {
try {
Stream stream = new Stream(sampleBytes());
byte[] sample = new byte[0];
stream.readFully(sample);
assertEquals(sampleBytes().length, stream.available());
sample = new byte[2];
stream.readFully(sample);
for (int i = 0; i < sample.length; i++) {
assertEquals(sampleBytes()[i], sample[i]);
}
sample = new byte[7];
stream.readFully(sample);
for (int i = 0; i < sample.length; i++) {
assertEquals(sampleBytes()[i + 2], sample[i]);
}
assertEquals(0, stream.available());
sample = new byte[0];
stream.readFully(sample);
assertEquals(0, stream.available());
} finally {
}
}
@Test
public void fReadFully_byteArray() throws Exception {
try {
Stream stream = new Stream();
stream.readFully(null);
fail();
} catch (NullPointerException ex) {
}
try {
Stream stream = new Stream();
stream.readFully(new byte[1]);
fail();
} catch (EOFException ex) {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readFully(new byte[1]);
fail();
} catch (InsufficientBufferException ex) {
}
}
@Test
public void fReadFully_byteArray_int_int() throws Exception {
try {
Stream stream = new Stream();
stream.readFully(null, 0, 0);
fail();
} catch (NullPointerException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.readFully(new byte[0], -1, 0);
fail();
} catch (IllegalArgumentException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.readFully(new byte[0], 0, -1);
fail();
} catch (IllegalArgumentException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.readFully(new byte[1], 1, 1);
fail();
} catch (IndexOutOfBoundsException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.readFully(new byte[2], 0, 2);
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeInt(0, 3);
stream.readFully(new byte[1], 0, 1);
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadFully_byteArray_int_int() throws Exception {
try {
Stream stream = new Stream(sampleBytes());
byte[] sample = new byte[0];
stream.readFully(sample, 0, sample.length);
assertEquals(sampleBytes().length, stream.available());
sample = new byte[50];
stream.readFully(sample, 25, 0);
assertEquals(sampleBytes().length, stream.available());
sample = new byte[10];
stream.readFully(sample, 1, 2);
for (int i = 0; i < 2; i++) {
assertEquals(sampleBytes()[i], sample[i + 1]);
}
stream.readFully(sample, 2, 7);
for (int i = 0; i < 7; i++) {
assertEquals(sampleBytes()[i + 2], sample[i + 2]);
}
assertEquals(0, stream.available());
sample = new byte[1];
stream.readFully(sample, 1, 0);
assertEquals(0, stream.available());
} finally {
}
}
@Test
public void pSkipBytes_int() throws Exception {
try {
Stream stream = new Stream(sampleBytes());
stream.writeBit(1);
assertEquals(0, stream.skipBytes(0));
assertEquals(1, stream.skipBytes(1));
assertEquals(sampleBytes()[1], (byte )stream.read());
assertEquals(7, stream.skipBytes(10));
assertEquals(0, stream.skipBytes(0));
assertEquals(1, stream.getData().readable());
stream.readBit();
assertEquals(-1, stream.skipBytes(5));
} finally {
}
}
@Test
public void fSkipBytes_int() throws Exception {
try {
Stream stream = new Stream(sampleBytes());
stream.skip(-1);
fail();
} catch (IllegalArgumentException ex) {
} finally {
}
}
@Test
public void pReadBoolean_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.write(1);
stream.write(0);
stream.writeBit(1);
stream.readBit();
assertEquals(true, stream.readBoolean());
assertEquals(false, stream.readBoolean());
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadBoolean_void() throws Exception {
try {
Stream stream = new Stream();
stream.readBoolean();
fail();
} catch (EOFException ex) {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readBoolean();
fail();
} catch (InsufficientBufferException ex) {
}
}
@Test
public void pReadByte_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.write(sampleBytes());
stream.writeBit(1);
stream.readBit();
for (int i = 0; i < sampleBytes().length; i++) {
assertEquals(sampleBytes()[i], stream.readByte());
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadByte_void() throws Exception {
try {
Stream stream = new Stream();
stream.readByte();
fail();
} catch (EOFException ex) {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readByte();
fail();
} catch (InsufficientBufferException ex) {
}
}
@Test
public void pReadUnsignedByte_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.write(sampleBytes());
stream.writeBit(1);
stream.readBit();
for (int i = 0; i < sampleBytes().length; i++) {
assertEquals(sampleBytes()[i] & 0xff, stream.readUnsignedByte());
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadUnsignedByte_void() throws Exception {
try {
Stream stream = new Stream();
stream.readUnsignedByte();
fail();
} catch (EOFException ex) {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readUnsignedByte();
fail();
} catch (InsufficientBufferException ex) {
}
}
@Test
public void pReadShort_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (short sh : sampleShorts()) {
stream.writeShort(sh);
}
stream.writeBit(1);
stream.readBit();
for (int i = 0; i < sampleShorts().length; i++) {
assertEquals(sampleShorts()[i], stream.readShort());
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadShort_void() throws Exception {
try {
Stream stream = new Stream();
stream.readShort();
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readShort();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.readShort();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadUnsignedShort_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (short sh : sampleShorts()) {
stream.writeShort(sh);
}
stream.writeBit(1);
stream.readBit();
for (int i = 0; i < sampleShorts().length; i++) {
assertEquals(sampleShorts()[i] & 0xffff, stream.readUnsignedShort());
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadUnsignedShort_void() throws Exception {
try {
Stream stream = new Stream();
stream.readUnsignedShort();
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readUnsignedShort();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.readUnsignedShort();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadChar_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (char ch : sampleChars()) {
stream.writeChar(ch);
}
stream.writeBit(1);
stream.readBit();
for (int i = 0; i < sampleChars().length; i++) {
assertEquals(sampleChars()[i], stream.readChar());
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadChar_void() throws Exception {
try {
Stream stream = new Stream();
stream.readChar();
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readChar();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.readChar();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadInt_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (NumWithInfo nwi : genNumWithInfos(Integer.SIZE)) {
stream.writeInt(nwi.getNumber().intValue());
}
stream.writeBit(1);
stream.readBit();
for (NumWithInfo nwi : genNumWithInfos(Integer.SIZE)) {
assertEquals(nwi.getNumber().intValue(), stream.readInt());
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadInt_void() throws Exception {
try {
Stream stream = new Stream();
stream.readInt();
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readInt();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.write(1);
stream.write(1);
stream.readInt();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadUnsignedInt_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (NumWithInfo nwi : genNumWithInfos(Integer.SIZE)) {
stream.writeInt(nwi.getNumber().intValue());
}
stream.writeBit(1);
stream.readBit();
for (NumWithInfo nwi : genNumWithInfos(Integer.SIZE)) {
long mask = 0xffffffffL;
assertEquals(nwi.getNumber().longValue() & mask, stream.readUnsignedInt());
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadUnsignedInt_void() throws Exception {
try {
Stream stream = new Stream();
stream.readUnsignedInt();
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readUnsignedInt();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.write(1);
stream.write(1);
stream.readUnsignedInt();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadInt_int() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (NumWithInfo nwi : genNumWithInfos(Integer.SIZE)) {
stream.writeInt(nwi.getNumber().intValue(), nwi.getBits());
}
stream.writeBit(1);
stream.readBit();
for (NumWithInfo nwi : genNumWithInfos(Integer.SIZE)) {
if (nwi.isNormal()) {
assertEquals(nwi.getNumber().intValue(), stream.readInt(nwi.getBits()));
} else {
BigInteger flow = BigInteger.valueOf(2).pow(nwi.getBits());
BigInteger left = nwi.getNumber();
BigInteger right = BigInteger.valueOf(stream.readInt(nwi.getBits()));
if (nwi.isOverflow()) {
left = left.subtract(flow);
} else {
left = left.add(flow);
}
assertEquals(left, right);
}
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadInt_int() throws Exception {
try {
Stream stream = new Stream();
stream.readInt(1);
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readInt(2);
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.write(1);
stream.write(1);
stream.readInt(32);
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadUnsignedInt_int() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (NumWithInfo nwi : genNumWithInfos(Integer.SIZE)) {
stream.writeInt(nwi.getNumber().intValue(), nwi.getBits());
}
stream.writeBit(1);
stream.readBit();
for (NumWithInfo nwi : genNumWithInfos(Integer.SIZE)) {
BigInteger mask = BigInteger.valueOf(2).pow(nwi.getBits()).subtract(BigInteger.ONE);
BigInteger left = nwi.getNumber();
BigInteger right = BigInteger.valueOf(stream.readUnsignedInt(nwi.getBits()));
left = left.and(mask);
assertEquals(left, right);
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadUnsignedInt_int() throws Exception {
try {
Stream stream = new Stream();
stream.readUnsignedInt(1);
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readUnsignedInt(2);
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.write(1);
stream.write(1);
stream.readUnsignedInt(32);
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadLong_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (NumWithInfo nwi : genNumWithInfos(Long.SIZE)) {
stream.writeLong(nwi.getNumber().longValue());
}
stream.writeBit(1);
stream.readBit();
for (NumWithInfo nwi : genNumWithInfos(Long.SIZE)) {
assertEquals(nwi.getNumber().longValue(), stream.readLong());
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadLong_void() throws Exception {
try {
Stream stream = new Stream();
stream.readLong();
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readLong();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(new byte[7]);
stream.readLong();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadUnsignedLong_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (NumWithInfo nwi : genNumWithInfos(Long.SIZE)) {
stream.writeLong(nwi.getNumber().longValue());
}
stream.writeBit(1);
stream.readBit();
for (NumWithInfo nwi : genNumWithInfos(Long.SIZE)) {
BigInteger mask = BigInteger.valueOf(2).pow(Long.SIZE).subtract(BigInteger.ONE);
assertEquals(nwi.getNumber().and(mask), stream.readUnsignedLong());
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadUnsignedLong_void() throws Exception {
try {
Stream stream = new Stream();
stream.readUnsignedLong();
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readUnsignedLong();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(new byte[7]);
stream.readUnsignedLong();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadLong_int() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (NumWithInfo nwi : genNumWithInfos(Long.SIZE)) {
stream.writeLong(nwi.getNumber().longValue(), nwi.getBits());
}
stream.writeBit(1);
stream.readBit();
for (NumWithInfo nwi : genNumWithInfos(Long.SIZE)) {
if (nwi.isNormal()) {
assertEquals(nwi.getNumber().longValue(), stream.readLong(nwi.getBits()));
} else {
BigInteger flow = BigInteger.valueOf(2).pow(nwi.getBits());
BigInteger left = nwi.getNumber();
BigInteger right = BigInteger.valueOf(stream.readLong(nwi.getBits()));
if (nwi.isOverflow()) {
left = left.subtract(flow);
} else {
left = left.add(flow);
}
assertEquals(left, right);
}
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadLong_int() throws Exception {
try {
Stream stream = new Stream();
stream.readLong(1);
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readLong(2);
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(new byte[7]);
stream.readLong(64);
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadUnsignedLong_int() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (NumWithInfo nwi : genNumWithInfos(Long.SIZE)) {
stream.writeLong(nwi.getNumber().longValue(), nwi.getBits());
}
stream.writeBit(1);
stream.readBit();
for (NumWithInfo nwi : genNumWithInfos(Long.SIZE)) {
BigInteger mask = BigInteger.valueOf(2).pow(nwi.getBits()).subtract(BigInteger.ONE);
BigInteger left = nwi.getNumber();
BigInteger right = stream.readUnsignedLong(nwi.getBits());
left = left.and(mask);
assertEquals(left, right);
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadUnsignedLong_int() throws Exception {
try {
Stream stream = new Stream();
stream.readUnsignedLong(1);
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readUnsignedLong(2);
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(new byte[7]);
stream.readUnsignedLong(64);
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadVarLong_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
int max = (int )(Math.pow(2, RT.INT62) * Byte.SIZE - 2);
for (NumWithInfo nwi : genNumWithInfos(max)) {
if (nwi.getBits() < max) {
if (nwi.isOverflow() || nwi.isUnderflow()) {
continue;
}
}
stream.writeVarLong(nwi.getNumber().longValue());
}
stream.writeBit(1);
stream.readBit();
for (NumWithInfo nwi : genNumWithInfos(max)) {
if (nwi.getBits() < max) {
if (nwi.isOverflow() || nwi.isUnderflow()) {
continue;
}
}
if (nwi.isNormal()) {
assertEquals(nwi.getNumber().longValue(), stream.readVarLong());
} else {
BigInteger flow = BigInteger.valueOf(2).pow(nwi.getBits());
BigInteger left = nwi.getNumber();
BigInteger right = BigInteger.valueOf(stream.readVarLong());
if (nwi.isOverflow()) {
left = left.subtract(flow);
} else {
left = left.add(flow);
}
assertEquals(left, right);
}
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadVarLong_void() throws Exception {
try {
Stream stream = new Stream();
stream.readVarLong();
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readVarLong();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(2);
stream.readVarLong();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.writeBit(1);
stream.write(1);
stream.readVarLong();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadUnsignedVarLong_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
int max = (int )(Math.pow(2, RT.INT62) * Byte.SIZE - 2);
for (NumWithInfo nwi : genNumWithInfos(max)) {
if (nwi.getBits() < max) {
if (nwi.isOverflow() || nwi.isUnderflow()) {
continue;
}
}
stream.writeUnsignedVarLong(nwi.getNumber().longValue());
}
stream.writeBit(1);
stream.readBit();
for (NumWithInfo nwi : genNumWithInfos(max)) {
if (nwi.getBits() < max) {
if (nwi.isOverflow() || nwi.isUnderflow()) {
continue;
}
}
BigInteger mask = BigInteger.valueOf(2).pow(nwi.getVarLongBits(false)).subtract(BigInteger.ONE);
BigInteger left = nwi.getNumber();
BigInteger right = BigInteger.valueOf(stream.readUnsignedVarLong());
left = left.and(mask);
assertEquals(left, right);
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadUnsignedVarLong_void() throws Exception {
try {
Stream stream = new Stream();
stream.readUnsignedVarLong();
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readUnsignedVarLong();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(2);
stream.readUnsignedVarLong();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.writeBit(1);
stream.write(1);
stream.readUnsignedVarLong();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadBigInteger_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (NumWithInfo nwi : genNumWithInfos(128)) {
stream.writeBigInteger(nwi.getNumber());
}
stream.writeBit(1);
stream.readBit();
for (NumWithInfo nwi : genNumWithInfos(128)) {
assertEquals(nwi.getNumber(), stream.readBigInteger());
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadBigInteger_void() throws Exception {
try {
Stream stream = new Stream();
stream.readBigInteger();
fail();
} catch (EOFException ex) {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readBigInteger();
fail();
} catch (InsufficientBufferException ex) {
}
try {
Stream stream = new Stream();
stream.getData().fill(1, 17);
stream.readBigInteger();
fail();
} catch (InsufficientBufferException ex) {
}
}
@Test
public void pReadBigInteger_int() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (NumWithInfo nwi : genNumWithInfos(128)) {
stream.writeBigInteger(nwi.getNumber(), nwi.getNumber().bitLength() + 1);
}
stream.writeBit(1);
stream.readBit();
for (NumWithInfo nwi : genNumWithInfos(128)) {
assertEquals(nwi.getNumber(), stream.readBigInteger(nwi.getNumber().bitLength() + 1));
}
assertEquals(BigInteger.ZERO, stream.readBigInteger(0));
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadBigInteger_int() throws Exception {
try {
Stream stream = new Stream();
stream.readBigInteger(1);
fail();
} catch (EOFException ex) {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readBigInteger(2);
fail();
} catch (InsufficientBufferException ex) {
}
try {
Stream stream = new Stream();
stream.getData().fill(1, 100);
stream.readBigInteger(101);
fail();
} catch (InsufficientBufferException ex) {
}
}
@Test
public void pReadFloat_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (float sample : sampleFloats()) {
stream.writeFloat(sample);
}
stream.writeBit(1);
stream.readBit();
for (float sample : sampleFloats()) {
assertEquals((double )sample, (double )stream.readFloat(), 0.00000001);
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadFloat_void() throws Exception {
try {
Stream stream = new Stream();
stream.readFloat();
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readFloat();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.write(1);
stream.write(1);
stream.readFloat();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadDouble_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
for (double sample : sampleDoubles()) {
stream.writeDouble(sample);
}
stream.writeBit(1);
stream.readBit();
for (double sample : sampleDoubles()) {
assertEquals(sample, stream.readDouble(), 0.00000001);
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadDouble_void() throws Exception {
try {
Stream stream = new Stream();
stream.readDouble();
fail();
} catch (EOFException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readDouble();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.write(1);
stream.write(1);
stream.write(1);
stream.write(1);
stream.write(1);
stream.write(1);
stream.readDouble();
fail();
} catch (InsufficientBufferException ex) {
} finally {
}
}
@Test
public void pReadBigDecimal_void() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
List<BigDecimal> numbers = new ArrayList<BigDecimal>();
for (NumWithInfo nwi : genNumWithInfos(128)) {
BigInteger unscaled = nwi.getNumber();
int scale = new Random(System.currentTimeMillis()).nextInt(0xffff);
numbers.add(new BigDecimal(unscaled, scale));
}
for (BigDecimal number : numbers) {
stream.writeBigDecimal(number);
}
stream.writeBit(1);
stream.readBit();
for (BigDecimal number : numbers) {
assertEquals(number, stream.readBigDecimal());
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadBigDecimal_void() throws Exception {
try {
Stream stream = new Stream();
stream.readBigDecimal();
fail();
} catch (EOFException ex) {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readBigDecimal();
fail();
} catch (InsufficientBufferException ex) {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.write(1);
stream.write(1);
stream.readBigDecimal();
fail();
} catch (InsufficientBufferException ex) {
}
}
@Test
public void pReadBigDecimal_int() throws Exception {
try {
Stream stream = new Stream();
stream.writeBit(1);
List<BigDecimal> numbers = new ArrayList<BigDecimal>();
for (NumWithInfo nwi : genNumWithInfos(128)) {
BigInteger unscaled = nwi.getNumber();
int scale = new Random(System.currentTimeMillis()).nextInt(0xffff);
numbers.add(new BigDecimal(unscaled, scale));
}
for (BigDecimal number : numbers) {
stream.writeBigDecimal(number, number.unscaledValue().bitLength() + 1);
}
stream.writeBit(1);
stream.readBit();
for (BigDecimal number : numbers) {
assertEquals(number, stream.readBigDecimal(number.unscaledValue().bitLength() + 1));
}
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadBigDecimal_int() throws Exception {
try {
Stream stream = new Stream();
stream.readBigDecimal(1);
fail();
} catch (EOFException ex) {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readBigDecimal(1);
fail();
} catch (InsufficientBufferException ex) {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.writeBit(1);
stream.writeBit(1);
stream.readBigDecimal(1);
fail();
} catch (InsufficientBufferException ex) {
}
try {
Stream stream = new Stream();
stream.write(1);
stream.readBigDecimal(0);
fail();
} catch (InsufficientBufferException ex) {
}
}
@Test
public void pReadLine_void() throws Exception {
try {
String template = "Hello World!";
String text = "Hello World!\nHello World!\rHello World!\n\rHello World!\nHello World!\n";
Stream stream = new Stream();
stream.writeBit(1);
stream.write(text.getBytes(Charset.forName("ascii")));
stream.writeBit(1);
stream.readBit();
for (int i = 0; i < 5; i++) {
assertEquals(template, stream.readLine());
}
assertEquals("", stream.readLine());
assertEquals(1, stream.getData().readable());
} finally {
}
}
@Test
public void fReadLine_void() throws Exception {
try {
Stream stream = new Stream();
stream.readLine();
fail();
} catch (EOFException ex) {
}
try {
Stream stream = new Stream();
stream.writeBit(1);
stream.readLine();
fail();
} catch (InsufficientBufferException ex) {
}
try {
Stream stream = new Stream();
stream.write("Hello World!\r".getBytes("ascii"));
fail();
} catch (InsufficientBufferException ex) {
}
}
}