GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestBitstream02.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;
33
34import org.junit.Assert;
35import org.junit.Test;
36
37import com.jogamp.common.nio.Buffers;
38import com.jogamp.junit.util.SingletonJunitCase;
39
40import static com.jogamp.common.util.BitDemoData.*;
41
42import org.junit.FixMethodOrder;
43import org.junit.runners.MethodSorters;
44
45/**
46 * Test {@link Bitstream} w/ int8 read/write access w/ semantics
47 * as well as with aligned and unaligned access.
48 * <ul>
49 * <li>{@link Bitstream#readInt8(boolean, boolean)}</li>
50 * <li>{@link Bitstream#writeInt8(boolean, boolean, byte)}</li>
51 * </ul>
52 */
53@FixMethodOrder(MethodSorters.NAME_ASCENDING)
54public class TestBitstream02 extends SingletonJunitCase {
55
56 @Test
57 public void test01Int8BitsAligned() throws IOException {
58 test01Int8BitsAlignedImpl((byte)0);
59 test01Int8BitsAlignedImpl((byte)1);
60 test01Int8BitsAlignedImpl((byte)7);
61 test01Int8BitsAlignedImpl(Byte.MIN_VALUE);
62 test01Int8BitsAlignedImpl(Byte.MAX_VALUE);
63 test01Int8BitsAlignedImpl((byte)0xff);
64 }
65 void test01Int8BitsAlignedImpl(final byte val8) throws IOException {
66 // Test with buffer defined value
67 final ByteBuffer bb = ByteBuffer.allocate(Buffers.SIZEOF_BYTE);
68 System.err.println("XXX Test01Int8BitsAligned: value "+val8+", "+toHexBinaryString(val8, 8));
69 bb.put(0, val8);
70
71 final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb);
72 final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, false /* outputMode */);
73 {
74 final byte r8 = (byte) bs.readUInt8();
75 System.err.println("Read8.1 "+r8+", "+toHexBinaryString(r8, 8));
76 Assert.assertEquals(val8, r8);
77 }
78
79 // Test with written bitstream value
80 bs.setStream(bs.getSubStream(), true /* outputMode */);
81 bs.writeInt8(val8);
82 bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
83 {
84 final byte r8 = (byte) bs.readUInt8();
85 System.err.println("Read8.2 "+r8+", "+toHexBinaryString(r8, 8));
86 Assert.assertEquals(val8, r8);
87 }
88 }
89
90 @Test
91 public void test02Int8BitsUnaligned() throws IOException {
92 test02Int8BitsUnalignedImpl(0);
93 test02Int8BitsUnalignedImpl(1);
94 test02Int8BitsUnalignedImpl(7);
95 test02Int8BitsUnalignedImpl(8);
96 test02Int8BitsUnalignedImpl(15);
97 test02Int8BitsUnalignedImpl(24);
98 test02Int8BitsUnalignedImpl(25);
99 }
100 void test02Int8BitsUnalignedImpl(final int preBits) throws IOException {
101 test02Int8BitsUnalignedImpl(preBits, (byte)0);
102 test02Int8BitsUnalignedImpl(preBits, (byte)1);
103 test02Int8BitsUnalignedImpl(preBits, (byte)7);
104 test02Int8BitsUnalignedImpl(preBits, Byte.MIN_VALUE);
105 test02Int8BitsUnalignedImpl(preBits, Byte.MAX_VALUE);
106 test02Int8BitsUnalignedImpl(preBits, (byte)0xff);
107 }
108 void test02Int8BitsUnalignedImpl(final int preBits, final byte val8) throws IOException {
109 final int preBytes = ( preBits + 7 ) >>> 3;
110 final int byteCount = preBytes + Buffers.SIZEOF_BYTE;
111 final ByteBuffer bb = ByteBuffer.allocate(byteCount);
112 System.err.println("XXX Test02Int8BitsUnaligned: preBits "+preBits+", value "+val8+", "+toHexBinaryString(val8, 8));
113
114 // Test with written bitstream value
115 final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb);
116 final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, true /* outputMode */);
117 bs.writeBits31(preBits, 0);
118 bs.writeInt8(val8);
119 bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
120
121 final int rPre = (short) bs.readBits31(preBits);
122 final byte r8 = (byte) bs.readUInt8();
123 System.err.println("ReadPre "+rPre+", "+toBinaryString(rPre, preBits));
124 System.err.println("Read8 "+r8+", "+toHexBinaryString(r8, 8));
125 Assert.assertEquals(val8, r8);
126 }
127
128 public static void main(final String args[]) throws IOException {
129 final String tstname = TestBitstream02.class.getName();
130 org.junit.runner.JUnitCore.main(tstname);
131 }
132
133}
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:70
static final int SIZEOF_BYTE
Definition: Buffers.java:77
Versatile Bitstream implementation supporting:
Definition: Bitstream.java:51
final int writeInt8(final byte int8)
Write the given 8 bits via writeBits31(int, int).
final int readUInt8()
Return incoming uint8_t as read via readBits31(int).
final T getSubStream()
Returns the currently used ByteStream's ByteStream#getStream().
Definition: Bitstream.java:686
final void setStream(final T stream, final boolean outputMode)
Sets the underlying stream, without close()ing the previous one.
Definition: Bitstream.java:672
Test Bitstream w/ int8 read/write access w/ semantics as well as with aligned and unaligned access.
static void main(final String args[])