29import static jau.test.util.BitDemoData.dumpData;
30import static jau.test.util.BitDemoData.toBinaryString;
31import static jau.test.util.BitDemoData.toHexBinaryString;
32import static jau.test.util.BitDemoData.toHexString;
34import java.io.IOException;
35import java.nio.ByteBuffer;
36import java.nio.ByteOrder;
38import org.jau.io.Bitstream;
39import org.jau.io.Buffers;
40import org.junit.Assert;
41import org.junit.FixMethodOrder;
43import org.junit.runners.MethodSorters;
45import jau.test.junit.util.JunitTracer;
55@FixMethodOrder(MethodSorters.NAME_ASCENDING)
60 test01Int32BitsImpl(
null);
61 test01Int32BitsImpl(ByteOrder.BIG_ENDIAN);
62 test01Int32BitsImpl(ByteOrder.LITTLE_ENDIAN);
64 void test01Int32BitsImpl(
final ByteOrder byteOrder)
throws IOException {
65 test01Int32BitsAlignedImpl(byteOrder, 0, 0);
66 test01Int32BitsAlignedImpl(byteOrder, 1, 1);
67 test01Int32BitsAlignedImpl(byteOrder, -1, -1);
68 test01Int32BitsAlignedImpl(byteOrder, 7, 7);
69 test01Int32BitsAlignedImpl(byteOrder, 0x0fffffff, 0x0fffffff);
70 test01Int32BitsAlignedImpl(byteOrder, Integer.MIN_VALUE, -1);
71 test01Int32BitsAlignedImpl(byteOrder, Integer.MAX_VALUE, Integer.MAX_VALUE);
72 test01Int32BitsAlignedImpl(byteOrder, 0xffffffff, -1);
74 void test01Int32BitsAlignedImpl(
final ByteOrder byteOrder,
final int val32,
final int expUInt32Int)
throws IOException {
76 final ByteBuffer bb = ByteBuffer.allocate(Buffers.SIZEOF_INT);
77 if(
null != byteOrder ) {
80 final boolean bigEndian = ByteOrder.BIG_ENDIAN == bb.order();
81 final String val32_hs = toHexString(val32);
82 System.err.println(
"XXX Test01Int32BitsAligned: byteOrder "+byteOrder+
" (bigEndian "+bigEndian+
"), value "+val32+
", "+toHexBinaryString(val32, 32));
83 System.err.println(
"XXX Test01Int32BitsAligned: "+val32+
", "+val32_hs);
86 dumpData(
"TestData.1: ", bb, 0, 4);
88 final Bitstream.ByteBufferStream bbs =
new Bitstream.ByteBufferStream(bb);
89 final Bitstream<ByteBuffer> bs =
new Bitstream<ByteBuffer>(bbs,
false );
91 final long uint32_l = bs.readUInt32(bigEndian);
92 final int int32_l = (int)uint32_l;
93 final String uint32_l_hs = toHexString(uint32_l);
94 final int uint32_i = Bitstream.uint32LongToInt(uint32_l);
95 System.err.printf(
"Read32.1 uint32_l %012d, %10s; int32_l %012d %10s; uint32_i %012d %10s%n",
96 uint32_l, uint32_l_hs, int32_l, toHexString(int32_l), uint32_i, toHexString(uint32_i));
97 Assert.assertEquals(val32_hs, uint32_l_hs);
98 Assert.assertEquals(val32, int32_l);
99 Assert.assertEquals(expUInt32Int, uint32_i);
103 bs.setStream(bs.getSubStream(),
true );
104 bs.writeInt32(bigEndian, val32);
105 bs.setStream(bs.getSubStream(),
false );
106 dumpData(
"TestData.2: ", bb, 0, 4);
108 final long uint32_l = bs.readUInt32(bigEndian);
109 final int int32_l = (int)uint32_l;
110 final String uint32_l_hs = toHexString(uint32_l);
111 final int uint32_i = Bitstream.uint32LongToInt(uint32_l);
112 System.err.printf(
"Read32.2 uint32_l %012d, %10s; int32_l %012d %10s; uint32_i %012d %10s%n",
113 uint32_l, uint32_l_hs, int32_l, toHexString(int32_l), uint32_i, toHexString(uint32_i));
114 Assert.assertEquals(val32_hs, uint32_l_hs);
115 Assert.assertEquals(val32, int32_l);
116 Assert.assertEquals(expUInt32Int, uint32_i);
122 test02Int32BitsUnalignedImpl(
null);
123 test02Int32BitsUnalignedImpl(ByteOrder.BIG_ENDIAN);
124 test02Int32BitsUnalignedImpl(ByteOrder.LITTLE_ENDIAN);
126 void test02Int32BitsUnalignedImpl(
final ByteOrder byteOrder)
throws IOException {
127 test02Int32BitsUnalignedImpl(byteOrder, 0);
128 test02Int32BitsUnalignedImpl(byteOrder, 1);
129 test02Int32BitsUnalignedImpl(byteOrder, 7);
130 test02Int32BitsUnalignedImpl(byteOrder, 8);
131 test02Int32BitsUnalignedImpl(byteOrder, 15);
132 test02Int32BitsUnalignedImpl(byteOrder, 24);
133 test02Int32BitsUnalignedImpl(byteOrder, 25);
135 void test02Int32BitsUnalignedImpl(
final ByteOrder byteOrder,
final int preBits)
throws IOException {
136 test02Int32BitsUnalignedImpl(byteOrder, preBits, 0, 0);
137 test02Int32BitsUnalignedImpl(byteOrder, preBits, 1, 1);
138 test02Int32BitsUnalignedImpl(byteOrder, preBits, -1, -1);
139 test02Int32BitsUnalignedImpl(byteOrder, preBits, 7, 7);
140 test02Int32BitsUnalignedImpl(byteOrder, preBits, 0x0fffffff, 0x0fffffff);
141 test02Int32BitsUnalignedImpl(byteOrder, preBits, Integer.MIN_VALUE, -1);
142 test02Int32BitsUnalignedImpl(byteOrder, preBits, Integer.MAX_VALUE, Integer.MAX_VALUE);
143 test02Int32BitsUnalignedImpl(byteOrder, preBits, 0xffffffff, -1);
145 void test02Int32BitsUnalignedImpl(
final ByteOrder byteOrder,
final int preBits,
final int val32,
final int expUInt32Int)
throws IOException {
146 final int preBytes = ( preBits + 7 ) >>> 3;
147 final int byteCount = preBytes + Buffers.SIZEOF_INT;
148 final ByteBuffer bb = ByteBuffer.allocate(byteCount);
149 if(
null != byteOrder ) {
152 final boolean bigEndian = ByteOrder.BIG_ENDIAN == bb.order();
153 final String val32_hs = toHexString(val32);
154 System.err.println(
"XXX Test02Int32BitsUnaligned: byteOrder "+byteOrder+
" (bigEndian "+bigEndian+
"), preBits "+preBits+
", value "+val32+
", "+toHexBinaryString(val32, 32));
155 System.err.println(
"XXX Test02Int32BitsUnaligned: "+val32+
", "+val32_hs);
158 final Bitstream.ByteBufferStream bbs =
new Bitstream.ByteBufferStream(bb);
159 final Bitstream<ByteBuffer> bs =
new Bitstream<ByteBuffer>(bbs,
true );
160 bs.writeBits31(preBits, 0);
161 bs.writeInt32(bigEndian, val32);
162 bs.setStream(bs.getSubStream(),
false );
164 final int rPre = bs.readBits31(preBits);
165 final long uint32_l = bs.readUInt32(bigEndian);
166 final int int32_l = (int)uint32_l;
167 final String uint32_l_hs = toHexString(uint32_l);
168 final int uint32_i = Bitstream.uint32LongToInt(uint32_l);
169 System.err.println(
"ReadPre "+rPre+
", "+toBinaryString(rPre, preBits));
170 System.err.printf(
"Read32 uint32_l %012d, %10s; int32_l %012d %10s; uint32_i %012d %10s%n",
171 uint32_l, uint32_l_hs, int32_l, toHexString(int32_l), uint32_i, toHexString(uint32_i));
172 Assert.assertEquals(val32_hs, uint32_l_hs);
173 Assert.assertEquals(val32, int32_l);
174 Assert.assertEquals(expUInt32Int, uint32_i);
177 public static void main(
final String args[])
throws IOException {
179 org.junit.runner.JUnitCore.
main(tstname);
Test Bitstream w/ int32 read/write access w/ semantics as well as with aligned and unaligned access.
void test02Int32BitsUnaligned()
void test01Int32BitsAligned()
static void main(final String args[])