29package com.jogamp.common.util;
31import java.io.IOException;
32import java.nio.ByteBuffer;
33import java.nio.ByteOrder;
34import java.nio.IntBuffer;
35import java.nio.LongBuffer;
38import org.junit.Assert;
40import com.jogamp.common.nio.Buffers;
41import com.jogamp.common.os.Platform;
43import static com.jogamp.common.util.BitDemoData.*;
45import com.jogamp.junit.util.SingletonJunitCase;
47import org.junit.FixMethodOrder;
48import org.junit.runners.MethodSorters;
53@FixMethodOrder(MethodSorters.NAME_ASCENDING)
58 final int i_ff = 0xff;
59 final byte b_ff = (byte)i_ff;
60 System.err.println(
"i_ff "+i_ff+
", "+toHexBinaryString(i_ff, 8));
61 System.err.println(
"b_ff "+b_ff+
", "+toHexBinaryString(0xff & b_ff, 8));
65 showOrderImpl(ByteOrder.BIG_ENDIAN);
66 showOrderImpl(ByteOrder.LITTLE_ENDIAN);
68 dumpData(
"tstMSB.whole", testBytesMSB, 0, testBytesMSB.length);
69 dumpData(
"tstLSB.pbyte", testBytesLSB_revByte, 0, testBytesLSB_revByte.length);
70 dumpData(
"tstLSB.whole", testBytesLSB, 0, testBytesLSB.length);
72 void showOrderImpl(
final ByteOrder byteOrder) {
74 if(
null != byteOrder ) {
75 bb_long.order(byteOrder);
77 System.err.println(
"Order: "+byteOrder+
" -> "+bb_long.order());
78 final LongBuffer lb = bb_long.asLongBuffer();
79 lb.put(0, 0x0807060504030201L);
80 dumpData(
"long."+byteOrder, bb_long, 0, bb_long.capacity());
82 final ByteBuffer bb_int = ByteBuffer.allocate(Buffers.SIZEOF_INT);
83 if(
null != byteOrder ) {
84 bb_int.order(byteOrder);
86 final IntBuffer ib = bb_int.asIntBuffer();
87 ib.put(0, 0x04030201);
88 dumpData(
"long."+byteOrder, bb_int, 0, bb_int.capacity());
90 dumpData(
"tstMSB.whole", testBytesMSB, 0, testBytesMSB.length);
91 dumpData(
"tstLSB.pbyte", testBytesLSB_revByte, 0, testBytesLSB_revByte.length);
92 dumpData(
"tstLSB.whole", testBytesLSB, 0, testBytesLSB.length);
97 testUInt32Conversion(1, 1);
98 testUInt32Conversion(-2, -1);
99 testUInt32Conversion(Integer.MAX_VALUE, Integer.MAX_VALUE);
100 testUInt32Conversion(0xffff0000, -1);
101 testUInt32Conversion(0xffffffff, -1);
103 void testUInt32Conversion(
final int int32,
final int expUInt32Int) {
104 final String int32_hStr = toHexString(int32);
106 final String l_hStr = toHexString(l);
108 final String i_hStr = toHexString(i);
109 System.err.printf(
"int32_t %012d %10s -> (long) %012d %10s, (int) %012d %10s%n", int32, int32_hStr, l, l_hStr, i, i_hStr);
110 Assert.assertEquals(int32_hStr, l_hStr);
111 Assert.assertEquals(expUInt32Int, i);
116 shiftSigned(0xA0000000);
119 void shiftSigned(
final int i0) {
120 System.err.printf(
"i0 %012d, %s%n", i0, toHexBinaryString(i0, 32));
123 for(
int i=0; i<32; i++) {
124 final int bitA = ( 0 != ( i0 & ( 1 << i ) ) ) ? 1 : 0;
125 final int bitB = im & 0x01;
126 System.err.printf(
"[%02d]: bit[%d, %d], im %012d, %s%n", i, bitA, bitB, im, toHexBinaryString(im, 32));
133 public void test10ReadWrite_13() throws UnsupportedOperationException, IllegalStateException, IOException {
139 test10ReadWrite1_31Impl(8, 8, 8, 0x030201,
"000000110000001000000001");
143 test10ReadWrite1_31Impl(5, 6, 5, 0x1841,
"0001100001000001");
145 void test10ReadWrite1_31Impl(
final int c1,
final int c2,
final int c3,
final int v,
final String vStrHigh2LowExp)
146 throws UnsupportedOperationException, IllegalStateException, IOException
149 final int bitCount = c1+c2+c3;
150 final int byteCount = ( bitCount + 7 ) / 8;
152 System.err.printf(
"test10ReadWrite31 bits %d:%d:%d = %d = %d bytes%n",
153 c1, c2, c3, bitCount, byteCount);
156 Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low0);
158 final ByteBuffer bbRead = ByteBuffer.allocate(byteCount);
159 for(
int i=0; i<byteCount; i++) {
160 final int b = ( v >>> 8*i ) & 0xff;
161 bbRead.put(i, (
byte) b);
164 final Bitstream.ByteBufferStream bbsRead =
new Bitstream.ByteBufferStream(bbRead);
165 final Bitstream<ByteBuffer> bsRead =
new Bitstream<ByteBuffer>(bbsRead,
false );
167 String vStrHigh2Low1C1 =
"";
168 String vStrHigh2Low1C2 =
"";
169 String vStrHigh2Low1C3 =
"";
170 String vStrHigh2Low1 =
"";
172 bsRead.mark(byteCount);
173 System.err.println(
"readBit (msbFirst false): ");
176 String vStrHigh2Low1T =
"";
177 while( Bitstream.EOS != ( b = bsRead.readBit(
false ) ) ) {
178 vStrHigh2Low1T = b + vStrHigh2Low1T;
180 vStrHigh2Low1C1 = b + vStrHigh2Low1C1;
181 }
else if(i < c1+c2) {
182 vStrHigh2Low1C2 = b + vStrHigh2Low1C2;
184 vStrHigh2Low1C3 = b + vStrHigh2Low1C3;
188 vStrHigh2Low1 = vStrHigh2Low1C3 + vStrHigh2Low1C2 + vStrHigh2Low1C1;
189 System.err.printf(
"readBit.1 %s, 0x%s%n", vStrHigh2Low1C1, Integer.toHexString(Integer.valueOf(vStrHigh2Low1C1, 2)));
190 System.err.printf(
"readBit.2 %s, 0x%s%n", vStrHigh2Low1C2, Integer.toHexString(Integer.valueOf(vStrHigh2Low1C2, 2)));
191 System.err.printf(
"readBit.3 %s, 0x%s%n", vStrHigh2Low1C3, Integer.toHexString(Integer.valueOf(vStrHigh2Low1C3, 2)));
192 System.err.printf(
"readBit.T %s, ok %b%n%n", vStrHigh2Low1T, vStrHigh2LowExp.equals(vStrHigh2Low1T));
193 System.err.printf(
"readBit.X %s, ok %b%n%n", vStrHigh2Low1, vStrHigh2LowExp.equals(vStrHigh2Low1));
198 String vStrHigh2Low3T =
"";
199 System.err.println(
"readBits32: ");
200 final int b = bsRead.readBits31(bitCount);
201 vStrHigh2Low3T = Bitstream.toBinString(
true, b, bitCount);
202 System.err.printf(
"readBits31.T %s, ok %b, %s%n%n", vStrHigh2Low3T, vStrHigh2LowExp.equals(vStrHigh2Low3T), Bitstream.toHexBinString(
true, b, bitCount));
206 String vStrHigh2Low2 =
"";
208 System.err.println(
"readBits32: ");
209 final int bC1 = bsRead.readBits31(c1);
210 System.err.printf(
"readBits31.1 %s%n", Bitstream.toHexBinString(
true, bC1, c1));
211 final int bC2 = bsRead.readBits31(c2);
212 System.err.printf(
"readBits31.2 %s%n", Bitstream.toHexBinString(
true, bC2, c2));
213 final int bC3 = bsRead.readBits31(c3);
214 System.err.printf(
"readBits31.3 %s%n", Bitstream.toHexBinString(
true, bC3, c3));
215 final int b = bC3 << (c1+c2) | bC2 << c1 | bC1;
216 vStrHigh2Low2 = Bitstream.toBinString(
true, b, bitCount);
217 System.err.printf(
"readBits31.X %s, ok %b, %s%n%n", vStrHigh2Low2, vStrHigh2LowExp.equals(vStrHigh2Low2), Bitstream.toHexBinString(
true, b, bitCount));
221 Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low1);
222 Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low2);
226 final ByteBuffer bbWrite = ByteBuffer.allocate(byteCount);
227 final Bitstream.ByteBufferStream bbsWrite =
new Bitstream.ByteBufferStream(bbWrite);
228 final Bitstream<ByteBuffer> bsWrite =
new Bitstream<ByteBuffer>(bbsWrite,
true );
231 while( Bitstream.EOS != ( b = bsRead.readBit(
false)) ) {
232 bsWrite.writeBit(
false, b);
236 for(
int i=0; i<byteCount; i++) {
237 final int bR = bbWrite.get(i);
238 final int bW = bbWrite.get(i);
239 System.err.printf(
"readWriteBit [%d]: read %s, write %s, ok %b%n",
240 i, Bitstream.toHexBinString(
true, bR, 8), Bitstream.toHexBinString(
true, bW, 8), bR==bW);
243 Assert.assertTrue(ok);
246 final ByteBuffer bbWrite = ByteBuffer.allocate(byteCount);
247 final Bitstream.ByteBufferStream bbsWrite =
new Bitstream.ByteBufferStream(bbWrite);
248 final Bitstream<ByteBuffer> bsWrite =
new Bitstream<ByteBuffer>(bbsWrite,
true );
250 bsWrite.writeBits31(bitCount, bsRead.readBits31(bitCount));
253 for(
int i=0; i<byteCount; i++) {
254 final int bR = bbWrite.get(i);
255 final int bW = bbWrite.get(i);
256 System.err.printf(
"readWriteBits31[%d]: read %s, write %s, ok %b%n",
257 i, Bitstream.toHexBinString(
true, bR, 8), Bitstream.toHexBinString(
true, bW, 8), bR==bW);
260 Assert.assertTrue(ok);
264 public static void main(
final String args[])
throws IOException {
266 org.junit.runner.JUnitCore.
main(tstname);
Utility methods allowing easy java.nio.Buffer manipulations.
static final int SIZEOF_LONG
Versatile Bitstream implementation supporting:
static String toHexBinString(final boolean msbFirst, final int v, final int bitCount)
static String toBinString(final boolean msbFirst, final int v, final int bitCount)
static final long toUInt32Long(final int int32)
Reinterpret the given int32_t value as uint32_t, i.e.
static final int toUInt32Int(final int int32)
Returns the reinterpreted given int32_t value as uint32_t if ≤ Integer#MAX_VALUE as within an int sto...
Test basic bit operations for Bitstream.
void test00ShowByteOrder()
void test10ReadWrite_13()
static void main(final String args[])
void test01Uint32Conversion()