GlueGen: src/junit/com/jogamp/common/util/TestBitstream00.java Source File

GlueGen v2.6.0-rc-20250822 GlueGen, Native Binding Generator for Java™ (public API).

TestBitstream00.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.common.util;
30
31import java.io.IOException;
32import java.nio.ByteBuffer;
33import java.nio.ByteOrder;
34import java.nio.IntBuffer;
35import java.nio.LongBuffer;
36
37import org.junit.Test;
38import org.junit.Assert;
39
40import com.jogamp.common.nio.Buffers;
41import com.jogamp.common.os.Platform;
42
43import static com.jogamp.common.util.BitDemoData.*;
44
45import com.jogamp.junit.util.SingletonJunitCase;
46
47import org.junit.FixMethodOrder;
48import org.junit.runners.MethodSorters;
49
50/**
51 * Test basic bit operations for {@link Bitstream}
52 */
53@FixMethodOrder(MethodSorters.NAME_ASCENDING)
54public class TestBitstream00 extends SingletonJunitCase {
55
56 @Test
57 public void test00ShowByteOrder() {
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));
62
63 System.err.println("Platform.LITTLE_ENDIAN: "+Platform.isLittleEndian());
64 showOrderImpl(null);
65 showOrderImpl(ByteOrder.BIG_ENDIAN);
66 showOrderImpl(ByteOrder.LITTLE_ENDIAN);
67
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);
71 }
72 void showOrderImpl(final ByteOrder byteOrder) {
73 final ByteBuffer bb_long = ByteBuffer.allocate(Buffers.SIZEOF_LONG);
74 if( null != byteOrder ) {
75 bb_long.order(byteOrder);
76 }
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());
81
82 final ByteBuffer bb_int = ByteBuffer.allocate(Buffers.SIZEOF_INT);
83 if( null != byteOrder ) {
84 bb_int.order(byteOrder);
85 }
86 final IntBuffer ib = bb_int.asIntBuffer();
87 ib.put(0, 0x04030201);
88 dumpData("long."+byteOrder, bb_int, 0, bb_int.capacity());
89
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);
93 }
94
95 @Test
96 public void test01Uint32Conversion() {
97 testUInt32Conversion(1, 1);
98 testUInt32Conversion(-2, -1);
99 testUInt32Conversion(Integer.MAX_VALUE, Integer.MAX_VALUE);
100 testUInt32Conversion(0xffff0000, -1);
101 testUInt32Conversion(0xffffffff, -1);
102 }
103 void testUInt32Conversion(final int int32, final int expUInt32Int) {
104 final String int32_hStr = toHexString(int32);
105 final long l = Bitstream.toUInt32Long(int32);
106 final String l_hStr = toHexString(l);
107 final int i = Bitstream.toUInt32Int(int32);
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);
112 }
113
114 @Test
115 public void test02ShiftSigned() {
116 shiftSigned(0xA0000000); // negative w/ '1010' top-nibble
117 shiftSigned(-1);
118 }
119 void shiftSigned(final int i0) {
120 System.err.printf("i0 %012d, %s%n", i0, toHexBinaryString(i0, 32));
121 {
122 int im = i0;
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));
127 im = im >>> 1;
128 }
129 }
130 }
131
132 @Test
133 public void test10ReadWrite_13() throws UnsupportedOperationException, IllegalStateException, IOException {
134 // H->L : 00000011 00000010 00000001 000000110000001000000001
135 // H->L rev: 10000000 01000000 11000000 100000000100000011000000
136 //
137 // L->H : 00000001 00000010 00000011 000000010000001000000011
138 // L->H rev: 11000000 01000000 10000000 110000000100000010000000
139 test10ReadWrite1_31Impl(8, 8, 8, 0x030201, "000000110000001000000001");
140
141 // H->L: 00011 000010 00001 0001100001000001
142 // L->H: 10000 010000 11000 1000001000011000
143 test10ReadWrite1_31Impl(5, 6, 5, 0x1841, "0001100001000001");
144 }
145 void test10ReadWrite1_31Impl(final int c1, final int c2, final int c3, final int v, final String vStrHigh2LowExp)
146 throws UnsupportedOperationException, IllegalStateException, IOException
147 {
148 // final Bitstream<ByteBuffer> source = new Bitstream<ByteBuffer>();
149 final int bitCount = c1+c2+c3;
150 final int byteCount = ( bitCount + 7 ) / 8;
151 final String vStrHigh2Low0 = Bitstream.toBinString(true, v, bitCount);
152 System.err.printf("test10ReadWrite31 bits %d:%d:%d = %d = %d bytes%n",
153 c1, c2, c3, bitCount, byteCount);
154 System.err.printf("test10ReadWrite31 %s%n", Bitstream.toHexBinString(true, v, bitCount));
155 System.err.printf("test10ReadWrite31 %s%n", Bitstream.toHexBinString(false, v, bitCount));
156 Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low0);
157
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);
162 System.err.printf("testBytes[%d]: %s%n", i, Bitstream.toHexBinString(true, b, 8));
163 }
164 final Bitstream.ByteBufferStream bbsRead = new Bitstream.ByteBufferStream(bbRead);
165 final Bitstream<ByteBuffer> bsRead = new Bitstream<ByteBuffer>(bbsRead, false /* outputMode */);
166
167 String vStrHigh2Low1C1 = "";
168 String vStrHigh2Low1C2 = "";
169 String vStrHigh2Low1C3 = "";
170 String vStrHigh2Low1 = "";
171 {
172 bsRead.mark(byteCount);
173 System.err.println("readBit (msbFirst false): ");
174 int b;
175 int i=0;
176 String vStrHigh2Low1T = ""; // OK for LSB, MSB segmented
177 while( Bitstream.EOS != ( b = bsRead.readBit(false /* msbFirst */) ) ) {
178 vStrHigh2Low1T = b + vStrHigh2Low1T;
179 if(i < c1) {
180 vStrHigh2Low1C1 = b + vStrHigh2Low1C1;
181 } else if(i < c1+c2) {
182 vStrHigh2Low1C2 = b + vStrHigh2Low1C2;
183 } else {
184 vStrHigh2Low1C3 = b + vStrHigh2Low1C3;
185 }
186 i++;
187 }
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));
194 bsRead.reset();
195 }
196
197 {
198 String vStrHigh2Low3T = ""; // OK for LSB, MSB segmented
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));
203 bsRead.reset();
204 }
205
206 String vStrHigh2Low2 = "";
207 {
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));
218 bsRead.reset();
219 }
220
221 Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low1);
222 Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low2);
223
224 boolean ok = true;
225 {
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 /* outputMode */);
229 {
230 int b;
231 while( Bitstream.EOS != ( b = bsRead.readBit(false)) ) {
232 bsWrite.writeBit(false, b);
233 }
234 }
235 bsRead.reset();
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);
241 ok = ok && bR==bW;
242 }
243 Assert.assertTrue(ok);
244 }
245 {
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 /* outputMode */);
249 {
250 bsWrite.writeBits31(bitCount, bsRead.readBits31(bitCount));
251 }
252 bsRead.reset();
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);
258 ok = ok && bR==bW;
259 }
260 Assert.assertTrue(ok);
261 }
262 }
263
264 public static void main(final String args[]) throws IOException {
265 final String tstname = TestBitstream00.class.getName();
266 org.junit.runner.JUnitCore.main(tstname);
267 }
268
269}
com.jogamp.common.nio.Buffers
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:70
com.jogamp.common.nio.Buffers.SIZEOF_LONG
static final int SIZEOF_LONG
Definition: Buffers.java:82
com.jogamp.common.os.Platform
Utility class for querying platform specific properties.
Definition: Platform.java:58
com.jogamp.common.os.Platform.isLittleEndian
static boolean isLittleEndian()
Returns true if this machine is little endian, otherwise false.
Definition: Platform.java:364
com.jogamp.common.util.Bitstream
Versatile Bitstream implementation supporting:
Definition: Bitstream.java:51
com.jogamp.common.util.Bitstream.toHexBinString
static String toHexBinString(final boolean msbFirst, final int v, final int bitCount)
Definition: Bitstream.java:1533
com.jogamp.common.util.Bitstream.toBinString
static String toBinString(final boolean msbFirst, final int v, final int bitCount)
Definition: Bitstream.java:1516
com.jogamp.common.util.Bitstream.toUInt32Long
static final long toUInt32Long(final int int32)
Reinterpret the given int32_t value as uint32_t, i.e.
Definition: Bitstream.java:1470
com.jogamp.common.util.Bitstream.toUInt32Int
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...
Definition: Bitstream.java:1480
com.jogamp.common.util.TestBitstream00
Test basic bit operations for Bitstream.
Definition: TestBitstream00.java:54
com.jogamp.common.util.TestBitstream00.test00ShowByteOrder
void test00ShowByteOrder()
Definition: TestBitstream00.java:57
com.jogamp.common.util.TestBitstream00.test02ShiftSigned
void test02ShiftSigned()
Definition: TestBitstream00.java:115
com.jogamp.common.util.TestBitstream00.test10ReadWrite_13
void test10ReadWrite_13()
Definition: TestBitstream00.java:133
com.jogamp.common.util.TestBitstream00.main
static void main(final String args[])
Definition: TestBitstream00.java:264
com.jogamp.common.util.TestBitstream00.test01Uint32Conversion
void test01Uint32Conversion()
Definition: TestBitstream00.java:96
com.jogamp.junit.util.SingletonJunitCase
Definition: SingletonJunitCase.java:39