Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestBitstream04.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.dumpData;
30import static jau.test.util.BitDemoData.toBinaryString;
31import static jau.test.util.BitDemoData.toHexBinaryString;
32import static jau.test.util.BitDemoData.toHexString;
33
34import java.io.IOException;
35import java.nio.ByteBuffer;
36import java.nio.ByteOrder;
37
38import org.jau.io.Bitstream;
39import org.jau.io.Buffers;
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 {@link Bitstream} w/ int32 read/write access w/ semantics
49 * as well as with aligned and unaligned access.
50 * <ul>
51 * <li>{@link Bitstream#readUInt32(boolean)}</li>
52 * <li>{@link Bitstream#writeInt32(boolean, int)}</li>
53 * </ul>
54 */
55@FixMethodOrder(MethodSorters.NAME_ASCENDING)
56public class TestBitstream04 extends JunitTracer {
57
58 @Test
59 public void test01Int32BitsAligned() throws IOException {
60 test01Int32BitsImpl(null);
61 test01Int32BitsImpl(ByteOrder.BIG_ENDIAN);
62 test01Int32BitsImpl(ByteOrder.LITTLE_ENDIAN);
63 }
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);
73 }
74 void test01Int32BitsAlignedImpl(final ByteOrder byteOrder, final int val32, final int expUInt32Int) throws IOException {
75 // Test with buffer defined value
76 final ByteBuffer bb = ByteBuffer.allocate(Buffers.SIZEOF_INT);
77 if( null != byteOrder ) {
78 bb.order(byteOrder);
79 }
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);
84
85 bb.putInt(0, val32);
86 dumpData("TestData.1: ", bb, 0, 4);
87
88 final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb);
89 final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, false /* outputMode */);
90 {
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);
100 }
101
102 // Test with written bitstream value
103 bs.setStream(bs.getSubStream(), true /* outputMode */);
104 bs.writeInt32(bigEndian, val32);
105 bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
106 dumpData("TestData.2: ", bb, 0, 4);
107 {
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);
117 }
118 }
119
120 @Test
121 public void test02Int32BitsUnaligned() throws IOException {
122 test02Int32BitsUnalignedImpl(null);
123 test02Int32BitsUnalignedImpl(ByteOrder.BIG_ENDIAN);
124 test02Int32BitsUnalignedImpl(ByteOrder.LITTLE_ENDIAN);
125 }
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);
134 }
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);
144 }
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 ) {
150 bb.order(byteOrder);
151 }
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);
156
157 // Test with written bitstream value
158 final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb);
159 final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, true /* outputMode */);
160 bs.writeBits31(preBits, 0);
161 bs.writeInt32(bigEndian, val32);
162 bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
163
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);
175 }
176
177 public static void main(final String args[]) throws IOException {
178 final String tstname = TestBitstream04.class.getName();
179 org.junit.runner.JUnitCore.main(tstname);
180 }
181
182}
Test Bitstream w/ int32 read/write access w/ semantics as well as with aligned and unaligned access.
static void main(final String args[])