29import static jau.test.util.BitDemoData.dumpData;
30import static jau.test.util.BitDemoData.toBinaryString;
31import static jau.test.util.BitDemoData.toHexBinaryString;
33import java.io.IOException;
34import java.nio.ByteBuffer;
35import java.nio.ByteOrder;
37import org.jau.io.Bitstream;
38import org.jau.io.Buffers;
39import org.junit.Assert;
40import org.junit.FixMethodOrder;
42import org.junit.runners.MethodSorters;
44import jau.test.junit.util.JunitTracer;
54@FixMethodOrder(MethodSorters.NAME_ASCENDING)
59 test01Int16BitsImpl(
null);
60 test01Int16BitsImpl(ByteOrder.BIG_ENDIAN);
61 test01Int16BitsImpl(ByteOrder.LITTLE_ENDIAN);
63 void test01Int16BitsImpl(
final ByteOrder byteOrder)
throws IOException {
64 test01Int16BitsAlignedImpl(byteOrder, (
short)0);
65 test01Int16BitsAlignedImpl(byteOrder, (
short)1);
66 test01Int16BitsAlignedImpl(byteOrder, (
short)7);
67 test01Int16BitsAlignedImpl(byteOrder, (
short)0x0fff);
68 test01Int16BitsAlignedImpl(byteOrder, Short.MIN_VALUE);
69 test01Int16BitsAlignedImpl(byteOrder, Short.MAX_VALUE);
70 test01Int16BitsAlignedImpl(byteOrder, (
short)0xffff);
72 void test01Int16BitsAlignedImpl(
final ByteOrder byteOrder,
final short val16)
throws IOException {
74 final ByteBuffer bb = ByteBuffer.allocate(Buffers.SIZEOF_SHORT);
75 if(
null != byteOrder ) {
78 final boolean bigEndian = ByteOrder.BIG_ENDIAN == bb.order();
79 System.err.println(
"XXX Test01Int16BitsAligned: byteOrder "+byteOrder+
" (bigEndian "+bigEndian+
"), value "+val16+
", "+toHexBinaryString(val16, 16));
80 bb.putShort(0, val16);
81 dumpData(
"TestData.1: ", bb, 0, 2);
83 final Bitstream.ByteBufferStream bbs =
new Bitstream.ByteBufferStream(bb);
84 final Bitstream<ByteBuffer> bs =
new Bitstream<ByteBuffer>(bbs,
false );
86 final short r16 = (short) bs.readUInt16(bigEndian);
87 System.err.println(
"Read16.1 "+r16+
", "+toHexBinaryString(r16, 16));
88 Assert.assertEquals(val16, r16);
92 bs.setStream(bs.getSubStream(),
true );
93 bs.writeInt16(bigEndian, val16);
94 bs.setStream(bs.getSubStream(),
false );
95 dumpData(
"TestData.2: ", bb, 0, 2);
97 final short r16 = (short) bs.readUInt16(bigEndian);
98 System.err.println(
"Read16.2 "+r16+
", "+toHexBinaryString(r16, 16));
99 Assert.assertEquals(val16, r16);
105 test02Int16BitsUnalignedImpl(
null);
106 test02Int16BitsUnalignedImpl(ByteOrder.BIG_ENDIAN);
107 test02Int16BitsUnalignedImpl(ByteOrder.LITTLE_ENDIAN);
109 void test02Int16BitsUnalignedImpl(
final ByteOrder byteOrder)
throws IOException {
110 test02Int16BitsUnalignedImpl(byteOrder, 0);
111 test02Int16BitsUnalignedImpl(byteOrder, 1);
112 test02Int16BitsUnalignedImpl(byteOrder, 7);
113 test02Int16BitsUnalignedImpl(byteOrder, 8);
114 test02Int16BitsUnalignedImpl(byteOrder, 15);
115 test02Int16BitsUnalignedImpl(byteOrder, 24);
116 test02Int16BitsUnalignedImpl(byteOrder, 25);
118 void test02Int16BitsUnalignedImpl(
final ByteOrder byteOrder,
final int preBits)
throws IOException {
119 test02Int16BitsUnalignedImpl(byteOrder, preBits, (
short)0);
120 test02Int16BitsUnalignedImpl(byteOrder, preBits, (
short)1);
121 test02Int16BitsUnalignedImpl(byteOrder, preBits, (
short)7);
122 test02Int16BitsUnalignedImpl(byteOrder, preBits, (
short)0x0fff);
123 test02Int16BitsUnalignedImpl(byteOrder, preBits, Short.MIN_VALUE);
124 test02Int16BitsUnalignedImpl(byteOrder, preBits, Short.MAX_VALUE);
125 test02Int16BitsUnalignedImpl(byteOrder, preBits, (
short)0xffff);
127 void test02Int16BitsUnalignedImpl(
final ByteOrder byteOrder,
final int preBits,
final short val16)
throws IOException {
128 final int preBytes = ( preBits + 7 ) >>> 3;
129 final int byteCount = preBytes + Buffers.SIZEOF_SHORT;
130 final ByteBuffer bb = ByteBuffer.allocate(byteCount);
131 if(
null != byteOrder ) {
134 final boolean bigEndian = ByteOrder.BIG_ENDIAN == bb.order();
135 System.err.println(
"XXX Test02Int16BitsUnaligned: byteOrder "+byteOrder+
" (bigEndian "+bigEndian+
"), preBits "+preBits+
", value "+val16+
", "+toHexBinaryString(val16, 16));
138 final Bitstream.ByteBufferStream bbs =
new Bitstream.ByteBufferStream(bb);
139 final Bitstream<ByteBuffer> bs =
new Bitstream<ByteBuffer>(bbs,
true );
140 bs.writeBits31(preBits, 0);
141 bs.writeInt16(bigEndian, val16);
142 bs.setStream(bs.getSubStream(),
false );
143 dumpData(
"TestData.1: ", bb, 0, byteCount);
145 final int rPre = (short) bs.readBits31(preBits);
146 final short r16 = (short) bs.readUInt16(bigEndian);
147 System.err.println(
"ReadPre "+rPre+
", "+toBinaryString(rPre, preBits));
148 System.err.println(
"Read16 "+r16+
", "+toHexBinaryString(r16, 16));
149 Assert.assertEquals(val16, r16);
152 public static void main(
final String args[])
throws IOException {
154 org.junit.runner.JUnitCore.
main(tstname);
Test Bitstream w/ int16 read/write access w/ semantics as well as with aligned and unaligned access.
void test01Int16BitsAligned()
void test02Int16BitsUnaligned()
static void main(final String args[])