29import static jau.test.util.BitDemoData.*;
31import java.io.IOException;
32import java.nio.ByteBuffer;
33import java.nio.ByteOrder;
34import java.nio.IntBuffer;
35import java.nio.LongBuffer;
37import org.jau.io.Bitstream;
38import org.jau.io.Buffers;
39import org.jau.sys.PlatformProps;
40import org.junit.Assert;
41import org.junit.FixMethodOrder;
43import org.junit.runners.MethodSorters;
45import jau.test.junit.util.JunitTracer;
50@FixMethodOrder(MethodSorters.NAME_ASCENDING)
55 final int i_ff = 0xff;
56 final byte b_ff = (byte)i_ff;
57 System.err.println(
"i_ff "+i_ff+
", "+toHexBinaryString(i_ff, 8));
58 System.err.println(
"b_ff "+b_ff+
", "+toHexBinaryString(0xff & b_ff, 8));
60 System.err.println(
"Platform.LITTLE_ENDIAN: "+PlatformProps.LITTLE_ENDIAN);
62 showOrderImpl(ByteOrder.BIG_ENDIAN);
63 showOrderImpl(ByteOrder.LITTLE_ENDIAN);
65 dumpData(
"tstMSB.whole", testBytesMSB, 0, testBytesMSB.length);
66 dumpData(
"tstLSB.pbyte", testBytesLSB_revByte, 0, testBytesLSB_revByte.length);
67 dumpData(
"tstLSB.whole", testBytesLSB, 0, testBytesLSB.length);
69 void showOrderImpl(
final ByteOrder byteOrder) {
70 final ByteBuffer bb_long = ByteBuffer.allocate(Buffers.SIZEOF_LONG);
71 if(
null != byteOrder ) {
72 bb_long.order(byteOrder);
74 System.err.println(
"Order: "+byteOrder+
" -> "+bb_long.order());
75 final LongBuffer lb = bb_long.asLongBuffer();
76 lb.put(0, 0x0807060504030201L);
77 dumpData(
"long."+byteOrder, bb_long, 0, bb_long.capacity());
79 final ByteBuffer bb_int = ByteBuffer.allocate(Buffers.SIZEOF_INT);
80 if(
null != byteOrder ) {
81 bb_int.order(byteOrder);
83 final IntBuffer ib = bb_int.asIntBuffer();
84 ib.put(0, 0x04030201);
85 dumpData(
"long."+byteOrder, bb_int, 0, bb_int.capacity());
87 dumpData(
"tstMSB.whole", testBytesMSB, 0, testBytesMSB.length);
88 dumpData(
"tstLSB.pbyte", testBytesLSB_revByte, 0, testBytesLSB_revByte.length);
89 dumpData(
"tstLSB.whole", testBytesLSB, 0, testBytesLSB.length);
94 testUInt32Conversion(1, 1);
95 testUInt32Conversion(-2, -1);
96 testUInt32Conversion(Integer.MAX_VALUE, Integer.MAX_VALUE);
97 testUInt32Conversion(0xffff0000, -1);
98 testUInt32Conversion(0xffffffff, -1);
100 void testUInt32Conversion(
final int int32,
final int expUInt32Int) {
101 final String int32_hStr = toHexString(int32);
102 final long l = Bitstream.toUInt32Long(int32);
103 final String l_hStr = toHexString(l);
104 final int i = Bitstream.toUInt32Int(int32);
105 final String i_hStr = toHexString(i);
106 System.err.printf(
"int32_t %012d %10s -> (long) %012d %10s, (int) %012d %10s%n", int32, int32_hStr, l, l_hStr, i, i_hStr);
107 Assert.assertEquals(int32_hStr, l_hStr);
108 Assert.assertEquals(expUInt32Int, i);
113 shiftSigned(0xA0000000);
116 void shiftSigned(
final int i0) {
117 System.err.printf(
"i0 %012d, %s%n", i0, toHexBinaryString(i0, 32));
120 for(
int i=0; i<32; i++) {
121 final int bitA = ( 0 != ( i0 & ( 1 << i ) ) ) ? 1 : 0;
122 final int bitB = im & 0x01;
123 System.err.printf(
"[%02d]: bit[%d, %d], im %012d, %s%n", i, bitA, bitB, im, toHexBinaryString(im, 32));
130 public void test10ReadWrite_13() throws UnsupportedOperationException, IllegalStateException, IOException {
136 test10ReadWrite1_31Impl(8, 8, 8, 0x030201,
"000000110000001000000001");
140 test10ReadWrite1_31Impl(5, 6, 5, 0x1841,
"0001100001000001");
142 void test10ReadWrite1_31Impl(
final int c1,
final int c2,
final int c3,
final int v,
final String vStrHigh2LowExp)
143 throws UnsupportedOperationException, IllegalStateException, IOException
146 final int bitCount = c1+c2+c3;
147 final int byteCount = ( bitCount + 7 ) / 8;
148 final String vStrHigh2Low0 = Bitstream.toBinString(
true, v, bitCount);
149 System.err.printf(
"test10ReadWrite31 bits %d:%d:%d = %d = %d bytes%n",
150 c1, c2, c3, bitCount, byteCount);
151 System.err.printf(
"test10ReadWrite31 %s%n", Bitstream.toHexBinString(
true, v, bitCount));
152 System.err.printf(
"test10ReadWrite31 %s%n", Bitstream.toHexBinString(
false, v, bitCount));
153 Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low0);
155 final ByteBuffer bbRead = ByteBuffer.allocate(byteCount);
156 for(
int i=0; i<byteCount; i++) {
157 final int b = ( v >>> 8*i ) & 0xff;
158 bbRead.put(i, (
byte) b);
159 System.err.printf(
"testBytes[%d]: %s%n", i, Bitstream.toHexBinString(
true, b, 8));
161 final Bitstream.ByteBufferStream bbsRead =
new Bitstream.ByteBufferStream(bbRead);
162 final Bitstream<ByteBuffer> bsRead =
new Bitstream<ByteBuffer>(bbsRead,
false );
164 String vStrHigh2Low1C1 =
"";
165 String vStrHigh2Low1C2 =
"";
166 String vStrHigh2Low1C3 =
"";
167 String vStrHigh2Low1 =
"";
169 bsRead.mark(byteCount);
170 System.err.println(
"readBit (msbFirst false): ");
173 String vStrHigh2Low1T =
"";
174 while( Bitstream.EOS != ( b = bsRead.readBit(
false ) ) ) {
175 vStrHigh2Low1T = b + vStrHigh2Low1T;
177 vStrHigh2Low1C1 = b + vStrHigh2Low1C1;
178 }
else if(i < c1+c2) {
179 vStrHigh2Low1C2 = b + vStrHigh2Low1C2;
181 vStrHigh2Low1C3 = b + vStrHigh2Low1C3;
185 vStrHigh2Low1 = vStrHigh2Low1C3 + vStrHigh2Low1C2 + vStrHigh2Low1C1;
186 System.err.printf(
"readBit.1 %s, 0x%s%n", vStrHigh2Low1C1, Integer.toHexString(Integer.valueOf(vStrHigh2Low1C1, 2)));
187 System.err.printf(
"readBit.2 %s, 0x%s%n", vStrHigh2Low1C2, Integer.toHexString(Integer.valueOf(vStrHigh2Low1C2, 2)));
188 System.err.printf(
"readBit.3 %s, 0x%s%n", vStrHigh2Low1C3, Integer.toHexString(Integer.valueOf(vStrHigh2Low1C3, 2)));
189 System.err.printf(
"readBit.T %s, ok %b%n%n", vStrHigh2Low1T, vStrHigh2LowExp.equals(vStrHigh2Low1T));
190 System.err.printf(
"readBit.X %s, ok %b%n%n", vStrHigh2Low1, vStrHigh2LowExp.equals(vStrHigh2Low1));
195 String vStrHigh2Low3T =
"";
196 System.err.println(
"readBits32: ");
197 final int b = bsRead.readBits31(bitCount);
198 vStrHigh2Low3T = Bitstream.toBinString(
true, b, bitCount);
199 System.err.printf(
"readBits31.T %s, ok %b, %s%n%n", vStrHigh2Low3T, vStrHigh2LowExp.equals(vStrHigh2Low3T), Bitstream.toHexBinString(
true, b, bitCount));
203 String vStrHigh2Low2 =
"";
205 System.err.println(
"readBits32: ");
206 final int bC1 = bsRead.readBits31(c1);
207 System.err.printf(
"readBits31.1 %s%n", Bitstream.toHexBinString(
true, bC1, c1));
208 final int bC2 = bsRead.readBits31(c2);
209 System.err.printf(
"readBits31.2 %s%n", Bitstream.toHexBinString(
true, bC2, c2));
210 final int bC3 = bsRead.readBits31(c3);
211 System.err.printf(
"readBits31.3 %s%n", Bitstream.toHexBinString(
true, bC3, c3));
212 final int b = bC3 << (c1+c2) | bC2 << c1 | bC1;
213 vStrHigh2Low2 = Bitstream.toBinString(
true, b, bitCount);
214 System.err.printf(
"readBits31.X %s, ok %b, %s%n%n", vStrHigh2Low2, vStrHigh2LowExp.equals(vStrHigh2Low2), Bitstream.toHexBinString(
true, b, bitCount));
218 Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low1);
219 Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low2);
223 final ByteBuffer bbWrite = ByteBuffer.allocate(byteCount);
224 final Bitstream.ByteBufferStream bbsWrite =
new Bitstream.ByteBufferStream(bbWrite);
225 final Bitstream<ByteBuffer> bsWrite =
new Bitstream<ByteBuffer>(bbsWrite,
true );
228 while( Bitstream.EOS != ( b = bsRead.readBit(
false)) ) {
229 bsWrite.writeBit(
false, b);
233 for(
int i=0; i<byteCount; i++) {
234 final int bR = bbWrite.get(i);
235 final int bW = bbWrite.get(i);
236 System.err.printf(
"readWriteBit [%d]: read %s, write %s, ok %b%n",
237 i, Bitstream.toHexBinString(
true, bR, 8), Bitstream.toHexBinString(
true, bW, 8), bR==bW);
240 Assert.assertTrue(ok);
243 final ByteBuffer bbWrite = ByteBuffer.allocate(byteCount);
244 final Bitstream.ByteBufferStream bbsWrite =
new Bitstream.ByteBufferStream(bbWrite);
245 final Bitstream<ByteBuffer> bsWrite =
new Bitstream<ByteBuffer>(bbsWrite,
true );
247 bsWrite.writeBits31(bitCount, bsRead.readBits31(bitCount));
250 for(
int i=0; i<byteCount; i++) {
251 final int bR = bbWrite.get(i);
252 final int bW = bbWrite.get(i);
253 System.err.printf(
"readWriteBits31[%d]: read %s, write %s, ok %b%n",
254 i, Bitstream.toHexBinString(
true, bR, 8), Bitstream.toHexBinString(
true, bW, 8), bR==bW);
257 Assert.assertTrue(ok);
261 public static void main(
final String args[])
throws IOException {
263 org.junit.runner.JUnitCore.
main(tstname);
Test basic bit operations for Bitstream.
static void main(final String args[])
void test00ShowByteOrder()
void test10ReadWrite_13()
void test01Uint32Conversion()