Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestBitstream03.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;
32
33import java.io.IOException;
34import java.nio.ByteBuffer;
35import java.nio.ByteOrder;
36
37import org.jau.io.Bitstream;
38import org.jau.io.Buffers;
39import org.junit.Assert;
40import org.junit.FixMethodOrder;
41import org.junit.Test;
42import org.junit.runners.MethodSorters;
43
44import jau.test.junit.util.JunitTracer;
45
46/**
47 * Test {@link Bitstream} w/ int16 read/write access w/ semantics
48 * as well as with aligned and unaligned access.
49 * <ul>
50 * <li>{@link Bitstream#readUInt16(boolean)}</li>
51 * <li>{@link Bitstream#writeInt16(boolean, short)}</li>
52 * </ul>
53 */
54@FixMethodOrder(MethodSorters.NAME_ASCENDING)
55public class TestBitstream03 extends JunitTracer {
56
57 @Test
58 public void test01Int16BitsAligned() throws IOException {
59 test01Int16BitsImpl(null);
60 test01Int16BitsImpl(ByteOrder.BIG_ENDIAN);
61 test01Int16BitsImpl(ByteOrder.LITTLE_ENDIAN);
62 }
63 void test01Int16BitsImpl(final ByteOrder byteOrder) throws IOException {
64 test01Int16BitsAlignedImpl(byteOrder, (short)0);
65 test01Int16BitsAlignedImpl(byteOrder, (short)1);
66 test01Int16BitsAlignedImpl(byteOrder, (short)7);
67 test01Int16BitsAlignedImpl(byteOrder, (short)0x0fff);
68 test01Int16BitsAlignedImpl(byteOrder, Short.MIN_VALUE);
69 test01Int16BitsAlignedImpl(byteOrder, Short.MAX_VALUE);
70 test01Int16BitsAlignedImpl(byteOrder, (short)0xffff);
71 }
72 void test01Int16BitsAlignedImpl(final ByteOrder byteOrder, final short val16) throws IOException {
73 // Test with buffer defined value
74 final ByteBuffer bb = ByteBuffer.allocate(Buffers.SIZEOF_SHORT);
75 if( null != byteOrder ) {
76 bb.order(byteOrder);
77 }
78 final boolean bigEndian = ByteOrder.BIG_ENDIAN == bb.order();
79 System.err.println("XXX Test01Int16BitsAligned: byteOrder "+byteOrder+" (bigEndian "+bigEndian+"), value "+val16+", "+toHexBinaryString(val16, 16));
80 bb.putShort(0, val16);
81 dumpData("TestData.1: ", bb, 0, 2);
82
83 final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb);
84 final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, false /* outputMode */);
85 {
86 final short r16 = (short) bs.readUInt16(bigEndian);
87 System.err.println("Read16.1 "+r16+", "+toHexBinaryString(r16, 16));
88 Assert.assertEquals(val16, r16);
89 }
90
91 // Test with written bitstream value
92 bs.setStream(bs.getSubStream(), true /* outputMode */);
93 bs.writeInt16(bigEndian, val16);
94 bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
95 dumpData("TestData.2: ", bb, 0, 2);
96 {
97 final short r16 = (short) bs.readUInt16(bigEndian);
98 System.err.println("Read16.2 "+r16+", "+toHexBinaryString(r16, 16));
99 Assert.assertEquals(val16, r16);
100 }
101 }
102
103 @Test
104 public void test02Int16BitsUnaligned() throws IOException {
105 test02Int16BitsUnalignedImpl(null);
106 test02Int16BitsUnalignedImpl(ByteOrder.BIG_ENDIAN);
107 test02Int16BitsUnalignedImpl(ByteOrder.LITTLE_ENDIAN);
108 }
109 void test02Int16BitsUnalignedImpl(final ByteOrder byteOrder) throws IOException {
110 test02Int16BitsUnalignedImpl(byteOrder, 0);
111 test02Int16BitsUnalignedImpl(byteOrder, 1);
112 test02Int16BitsUnalignedImpl(byteOrder, 7);
113 test02Int16BitsUnalignedImpl(byteOrder, 8);
114 test02Int16BitsUnalignedImpl(byteOrder, 15);
115 test02Int16BitsUnalignedImpl(byteOrder, 24);
116 test02Int16BitsUnalignedImpl(byteOrder, 25);
117 }
118 void test02Int16BitsUnalignedImpl(final ByteOrder byteOrder, final int preBits) throws IOException {
119 test02Int16BitsUnalignedImpl(byteOrder, preBits, (short)0);
120 test02Int16BitsUnalignedImpl(byteOrder, preBits, (short)1);
121 test02Int16BitsUnalignedImpl(byteOrder, preBits, (short)7);
122 test02Int16BitsUnalignedImpl(byteOrder, preBits, (short)0x0fff);
123 test02Int16BitsUnalignedImpl(byteOrder, preBits, Short.MIN_VALUE);
124 test02Int16BitsUnalignedImpl(byteOrder, preBits, Short.MAX_VALUE);
125 test02Int16BitsUnalignedImpl(byteOrder, preBits, (short)0xffff);
126 }
127 void test02Int16BitsUnalignedImpl(final ByteOrder byteOrder, final int preBits, final short val16) throws IOException {
128 final int preBytes = ( preBits + 7 ) >>> 3;
129 final int byteCount = preBytes + Buffers.SIZEOF_SHORT;
130 final ByteBuffer bb = ByteBuffer.allocate(byteCount);
131 if( null != byteOrder ) {
132 bb.order(byteOrder);
133 }
134 final boolean bigEndian = ByteOrder.BIG_ENDIAN == bb.order();
135 System.err.println("XXX Test02Int16BitsUnaligned: byteOrder "+byteOrder+" (bigEndian "+bigEndian+"), preBits "+preBits+", value "+val16+", "+toHexBinaryString(val16, 16));
136
137 // Test with written bitstream value
138 final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb);
139 final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, true /* outputMode */);
140 bs.writeBits31(preBits, 0);
141 bs.writeInt16(bigEndian, val16);
142 bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
143 dumpData("TestData.1: ", bb, 0, byteCount);
144
145 final int rPre = (short) bs.readBits31(preBits);
146 final short r16 = (short) bs.readUInt16(bigEndian);
147 System.err.println("ReadPre "+rPre+", "+toBinaryString(rPre, preBits));
148 System.err.println("Read16 "+r16+", "+toHexBinaryString(r16, 16));
149 Assert.assertEquals(val16, r16);
150 }
151
152 public static void main(final String args[]) throws IOException {
153 final String tstname = TestBitstream03.class.getName();
154 org.junit.runner.JUnitCore.main(tstname);
155 }
156
157}
Test Bitstream w/ int16 read/write access w/ semantics as well as with aligned and unaligned access.
static void main(final String args[])