GlueGen: src/junit/com/jogamp/common/util/TestBitstream03.java Source File

GlueGen v2.6.0-rc-20250722 GlueGen, Native Binding Generator for Java™ (public API).

TestBitstream03.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;
33import java.nio.ByteOrder;
34
35import org.junit.Assert;
36import org.junit.Test;
37
38import com.jogamp.common.nio.Buffers;
39import com.jogamp.junit.util.SingletonJunitCase;
40
41import static com.jogamp.common.util.BitDemoData.*;
42
43import org.junit.FixMethodOrder;
44import org.junit.runners.MethodSorters;
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 SingletonJunitCase {
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}
com.jogamp.common.util.TestBitstream03
Test Bitstream w/ int16 read/write access w/ semantics as well as with aligned and unaligned access.
Definition: TestBitstream03.java:55
com.jogamp.common.util.TestBitstream03.main
static void main(final String args[])
Definition: TestBitstream03.java:152
com.jogamp.common.util.TestBitstream03.test02Int16BitsUnaligned
void test02Int16BitsUnaligned()
Definition: TestBitstream03.java:104
com.jogamp.common.util.TestBitstream03.test01Int16BitsAligned
void test01Int16BitsAligned()
Definition: TestBitstream03.java:58
com.jogamp.junit.util.SingletonJunitCase
Definition: SingletonJunitCase.java:39