Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestBitstream02.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.toBinaryString;
30import static jau.test.util.BitDemoData.toHexBinaryString;
31
32import java.io.IOException;
33import java.nio.ByteBuffer;
34
35import org.jau.io.Bitstream;
36import org.jau.io.Buffers;
37import org.junit.Assert;
38import org.junit.FixMethodOrder;
39import org.junit.Test;
40import org.junit.runners.MethodSorters;
41
42import jau.test.junit.util.JunitTracer;
43
44/**
45 * Test {@link Bitstream} w/ int8 read/write access w/ semantics
46 * as well as with aligned and unaligned access.
47 * <ul>
48 * <li>{@link Bitstream#readInt8(boolean, boolean)}</li>
49 * <li>{@link Bitstream#writeInt8(boolean, boolean, byte)}</li>
50 * </ul>
51 */
52@FixMethodOrder(MethodSorters.NAME_ASCENDING)
53public class TestBitstream02 extends JunitTracer {
54
55 @Test
56 public void test01Int8BitsAligned() throws IOException {
57 test01Int8BitsAlignedImpl((byte)0);
58 test01Int8BitsAlignedImpl((byte)1);
59 test01Int8BitsAlignedImpl((byte)7);
60 test01Int8BitsAlignedImpl(Byte.MIN_VALUE);
61 test01Int8BitsAlignedImpl(Byte.MAX_VALUE);
62 test01Int8BitsAlignedImpl((byte)0xff);
63 }
64 void test01Int8BitsAlignedImpl(final byte val8) throws IOException {
65 // Test with buffer defined value
66 final ByteBuffer bb = ByteBuffer.allocate(Buffers.SIZEOF_BYTE);
67 System.err.println("XXX Test01Int8BitsAligned: value "+val8+", "+toHexBinaryString(val8, 8));
68 bb.put(0, val8);
69
70 final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb);
71 final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, false /* outputMode */);
72 {
73 final byte r8 = (byte) bs.readUInt8();
74 System.err.println("Read8.1 "+r8+", "+toHexBinaryString(r8, 8));
75 Assert.assertEquals(val8, r8);
76 }
77
78 // Test with written bitstream value
79 bs.setStream(bs.getSubStream(), true /* outputMode */);
80 bs.writeInt8(val8);
81 bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
82 {
83 final byte r8 = (byte) bs.readUInt8();
84 System.err.println("Read8.2 "+r8+", "+toHexBinaryString(r8, 8));
85 Assert.assertEquals(val8, r8);
86 }
87 }
88
89 @Test
90 public void test02Int8BitsUnaligned() throws IOException {
91 test02Int8BitsUnalignedImpl(0);
92 test02Int8BitsUnalignedImpl(1);
93 test02Int8BitsUnalignedImpl(7);
94 test02Int8BitsUnalignedImpl(8);
95 test02Int8BitsUnalignedImpl(15);
96 test02Int8BitsUnalignedImpl(24);
97 test02Int8BitsUnalignedImpl(25);
98 }
99 void test02Int8BitsUnalignedImpl(final int preBits) throws IOException {
100 test02Int8BitsUnalignedImpl(preBits, (byte)0);
101 test02Int8BitsUnalignedImpl(preBits, (byte)1);
102 test02Int8BitsUnalignedImpl(preBits, (byte)7);
103 test02Int8BitsUnalignedImpl(preBits, Byte.MIN_VALUE);
104 test02Int8BitsUnalignedImpl(preBits, Byte.MAX_VALUE);
105 test02Int8BitsUnalignedImpl(preBits, (byte)0xff);
106 }
107 void test02Int8BitsUnalignedImpl(final int preBits, final byte val8) throws IOException {
108 final int preBytes = ( preBits + 7 ) >>> 3;
109 final int byteCount = preBytes + Buffers.SIZEOF_BYTE;
110 final ByteBuffer bb = ByteBuffer.allocate(byteCount);
111 System.err.println("XXX Test02Int8BitsUnaligned: preBits "+preBits+", value "+val8+", "+toHexBinaryString(val8, 8));
112
113 // Test with written bitstream value
114 final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb);
115 final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, true /* outputMode */);
116 bs.writeBits31(preBits, 0);
117 bs.writeInt8(val8);
118 bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
119
120 final int rPre = (short) bs.readBits31(preBits);
121 final byte r8 = (byte) bs.readUInt8();
122 System.err.println("ReadPre "+rPre+", "+toBinaryString(rPre, preBits));
123 System.err.println("Read8 "+r8+", "+toHexBinaryString(r8, 8));
124 Assert.assertEquals(val8, r8);
125 }
126
127 public static void main(final String args[]) throws IOException {
128 final String tstname = TestBitstream02.class.getName();
129 org.junit.runner.JUnitCore.main(tstname);
130 }
131
132}
Test Bitstream w/ int8 read/write access w/ semantics as well as with aligned and unaligned access.
static void main(final String args[])