Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestBitstream00.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2014 Gothel Software e.K.
5 * Copyright (c) 2014 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package jau.test.util;
28
29import static jau.test.util.BitDemoData.*;
30
31import java.io.IOException;
32import java.nio.ByteBuffer;
33import java.nio.ByteOrder;
34import java.nio.IntBuffer;
35import java.nio.LongBuffer;
36
37import org.jau.io.Bitstream;
38import org.jau.io.Buffers;
39import org.jau.sys.PlatformProps;
40import org.junit.Assert;
41import org.junit.FixMethodOrder;
42import org.junit.Test;
43import org.junit.runners.MethodSorters;
44
45import jau.test.junit.util.JunitTracer;
46
47/**
48 * Test basic bit operations for {@link Bitstream}
49 */
50@FixMethodOrder(MethodSorters.NAME_ASCENDING)
51public class TestBitstream00 extends JunitTracer {
52
53 @Test
54 public void test00ShowByteOrder() {
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));
59
60 System.err.println("Platform.LITTLE_ENDIAN: "+PlatformProps.LITTLE_ENDIAN);
61 showOrderImpl(null);
62 showOrderImpl(ByteOrder.BIG_ENDIAN);
63 showOrderImpl(ByteOrder.LITTLE_ENDIAN);
64
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);
68 }
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);
73 }
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());
78
79 final ByteBuffer bb_int = ByteBuffer.allocate(Buffers.SIZEOF_INT);
80 if( null != byteOrder ) {
81 bb_int.order(byteOrder);
82 }
83 final IntBuffer ib = bb_int.asIntBuffer();
84 ib.put(0, 0x04030201);
85 dumpData("long."+byteOrder, bb_int, 0, bb_int.capacity());
86
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);
90 }
91
92 @Test
93 public void test01Uint32Conversion() {
94 testUInt32Conversion(1, 1);
95 testUInt32Conversion(-2, -1);
96 testUInt32Conversion(Integer.MAX_VALUE, Integer.MAX_VALUE);
97 testUInt32Conversion(0xffff0000, -1);
98 testUInt32Conversion(0xffffffff, -1);
99 }
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);
109 }
110
111 @Test
112 public void test02ShiftSigned() {
113 shiftSigned(0xA0000000); // negative w/ '1010' top-nibble
114 shiftSigned(-1);
115 }
116 void shiftSigned(final int i0) {
117 System.err.printf("i0 %012d, %s%n", i0, toHexBinaryString(i0, 32));
118 {
119 int im = i0;
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));
124 im = im >>> 1;
125 }
126 }
127 }
128
129 @Test
130 public void test10ReadWrite_13() throws UnsupportedOperationException, IllegalStateException, IOException {
131 // H->L : 00000011 00000010 00000001 000000110000001000000001
132 // H->L rev: 10000000 01000000 11000000 100000000100000011000000
133 //
134 // L->H : 00000001 00000010 00000011 000000010000001000000011
135 // L->H rev: 11000000 01000000 10000000 110000000100000010000000
136 test10ReadWrite1_31Impl(8, 8, 8, 0x030201, "000000110000001000000001");
137
138 // H->L: 00011 000010 00001 0001100001000001
139 // L->H: 10000 010000 11000 1000001000011000
140 test10ReadWrite1_31Impl(5, 6, 5, 0x1841, "0001100001000001");
141 }
142 void test10ReadWrite1_31Impl(final int c1, final int c2, final int c3, final int v, final String vStrHigh2LowExp)
143 throws UnsupportedOperationException, IllegalStateException, IOException
144 {
145 // final Bitstream<ByteBuffer> source = new Bitstream<ByteBuffer>();
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);
154
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));
160 }
161 final Bitstream.ByteBufferStream bbsRead = new Bitstream.ByteBufferStream(bbRead);
162 final Bitstream<ByteBuffer> bsRead = new Bitstream<ByteBuffer>(bbsRead, false /* outputMode */);
163
164 String vStrHigh2Low1C1 = "";
165 String vStrHigh2Low1C2 = "";
166 String vStrHigh2Low1C3 = "";
167 String vStrHigh2Low1 = "";
168 {
169 bsRead.mark(byteCount);
170 System.err.println("readBit (msbFirst false): ");
171 int b;
172 int i=0;
173 String vStrHigh2Low1T = ""; // OK for LSB, MSB segmented
174 while( Bitstream.EOS != ( b = bsRead.readBit(false /* msbFirst */) ) ) {
175 vStrHigh2Low1T = b + vStrHigh2Low1T;
176 if(i < c1) {
177 vStrHigh2Low1C1 = b + vStrHigh2Low1C1;
178 } else if(i < c1+c2) {
179 vStrHigh2Low1C2 = b + vStrHigh2Low1C2;
180 } else {
181 vStrHigh2Low1C3 = b + vStrHigh2Low1C3;
182 }
183 i++;
184 }
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));
191 bsRead.reset();
192 }
193
194 {
195 String vStrHigh2Low3T = ""; // OK for LSB, MSB segmented
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));
200 bsRead.reset();
201 }
202
203 String vStrHigh2Low2 = "";
204 {
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));
215 bsRead.reset();
216 }
217
218 Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low1);
219 Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low2);
220
221 boolean ok = true;
222 {
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 /* outputMode */);
226 {
227 int b;
228 while( Bitstream.EOS != ( b = bsRead.readBit(false)) ) {
229 bsWrite.writeBit(false, b);
230 }
231 }
232 bsRead.reset();
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);
238 ok = ok && bR==bW;
239 }
240 Assert.assertTrue(ok);
241 }
242 {
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 /* outputMode */);
246 {
247 bsWrite.writeBits31(bitCount, bsRead.readBits31(bitCount));
248 }
249 bsRead.reset();
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);
255 ok = ok && bR==bW;
256 }
257 Assert.assertTrue(ok);
258 }
259 }
260
261 public static void main(final String args[]) throws IOException {
262 final String tstname = TestBitstream00.class.getName();
263 org.junit.runner.JUnitCore.main(tstname);
264 }
265
266}
Test basic bit operations for Bitstream.
static void main(final String args[])