GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestBitstream04.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/ int32 read/write access w/ semantics
48 * as well as with aligned and unaligned access.
49 * <ul>
50 * <li>{@link Bitstream#readUInt32(boolean)}</li>
51 * <li>{@link Bitstream#writeInt32(boolean, int)}</li>
52 * </ul>
53 */
54@FixMethodOrder(MethodSorters.NAME_ASCENDING)
55public class TestBitstream04 extends SingletonJunitCase {
56
57 @Test
58 public void test01Int32BitsAligned() throws IOException {
59 test01Int32BitsImpl(null);
60 test01Int32BitsImpl(ByteOrder.BIG_ENDIAN);
61 test01Int32BitsImpl(ByteOrder.LITTLE_ENDIAN);
62 }
63 void test01Int32BitsImpl(final ByteOrder byteOrder) throws IOException {
64 test01Int32BitsAlignedImpl(byteOrder, 0, 0);
65 test01Int32BitsAlignedImpl(byteOrder, 1, 1);
66 test01Int32BitsAlignedImpl(byteOrder, -1, -1);
67 test01Int32BitsAlignedImpl(byteOrder, 7, 7);
68 test01Int32BitsAlignedImpl(byteOrder, 0x0fffffff, 0x0fffffff);
69 test01Int32BitsAlignedImpl(byteOrder, Integer.MIN_VALUE, -1);
70 test01Int32BitsAlignedImpl(byteOrder, Integer.MAX_VALUE, Integer.MAX_VALUE);
71 test01Int32BitsAlignedImpl(byteOrder, 0xffffffff, -1);
72 }
73 void test01Int32BitsAlignedImpl(final ByteOrder byteOrder, final int val32, final int expUInt32Int) throws IOException {
74 // Test with buffer defined value
75 final ByteBuffer bb = ByteBuffer.allocate(Buffers.SIZEOF_INT);
76 if( null != byteOrder ) {
77 bb.order(byteOrder);
78 }
79 final boolean bigEndian = ByteOrder.BIG_ENDIAN == bb.order();
80 final String val32_hs = toHexString(val32);
81 System.err.println("XXX Test01Int32BitsAligned: byteOrder "+byteOrder+" (bigEndian "+bigEndian+"), value "+val32+", "+toHexBinaryString(val32, 32));
82 System.err.println("XXX Test01Int32BitsAligned: "+val32+", "+val32_hs);
83
84 bb.putInt(0, val32);
85 dumpData("TestData.1: ", bb, 0, 4);
86
87 final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb);
88 final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, false /* outputMode */);
89 {
90 final long uint32_l = bs.readUInt32(bigEndian);
91 final int int32_l = (int)uint32_l;
92 final String uint32_l_hs = toHexString(uint32_l);
93 final int uint32_i = Bitstream.uint32LongToInt(uint32_l);
94 System.err.printf("Read32.1 uint32_l %012d, %10s; int32_l %012d %10s; uint32_i %012d %10s%n",
95 uint32_l, uint32_l_hs, int32_l, toHexString(int32_l), uint32_i, toHexString(uint32_i));
96 Assert.assertEquals(val32_hs, uint32_l_hs);
97 Assert.assertEquals(val32, int32_l);
98 Assert.assertEquals(expUInt32Int, uint32_i);
99 }
100
101 // Test with written bitstream value
102 bs.setStream(bs.getSubStream(), true /* outputMode */);
103 bs.writeInt32(bigEndian, val32);
104 bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
105 dumpData("TestData.2: ", bb, 0, 4);
106 {
107 final long uint32_l = bs.readUInt32(bigEndian);
108 final int int32_l = (int)uint32_l;
109 final String uint32_l_hs = toHexString(uint32_l);
110 final int uint32_i = Bitstream.uint32LongToInt(uint32_l);
111 System.err.printf("Read32.2 uint32_l %012d, %10s; int32_l %012d %10s; uint32_i %012d %10s%n",
112 uint32_l, uint32_l_hs, int32_l, toHexString(int32_l), uint32_i, toHexString(uint32_i));
113 Assert.assertEquals(val32_hs, uint32_l_hs);
114 Assert.assertEquals(val32, int32_l);
115 Assert.assertEquals(expUInt32Int, uint32_i);
116 }
117 }
118
119 @Test
120 public void test02Int32BitsUnaligned() throws IOException {
121 test02Int32BitsUnalignedImpl(null);
122 test02Int32BitsUnalignedImpl(ByteOrder.BIG_ENDIAN);
123 test02Int32BitsUnalignedImpl(ByteOrder.LITTLE_ENDIAN);
124 }
125 void test02Int32BitsUnalignedImpl(final ByteOrder byteOrder) throws IOException {
126 test02Int32BitsUnalignedImpl(byteOrder, 0);
127 test02Int32BitsUnalignedImpl(byteOrder, 1);
128 test02Int32BitsUnalignedImpl(byteOrder, 7);
129 test02Int32BitsUnalignedImpl(byteOrder, 8);
130 test02Int32BitsUnalignedImpl(byteOrder, 15);
131 test02Int32BitsUnalignedImpl(byteOrder, 24);
132 test02Int32BitsUnalignedImpl(byteOrder, 25);
133 }
134 void test02Int32BitsUnalignedImpl(final ByteOrder byteOrder, final int preBits) throws IOException {
135 test02Int32BitsUnalignedImpl(byteOrder, preBits, 0, 0);
136 test02Int32BitsUnalignedImpl(byteOrder, preBits, 1, 1);
137 test02Int32BitsUnalignedImpl(byteOrder, preBits, -1, -1);
138 test02Int32BitsUnalignedImpl(byteOrder, preBits, 7, 7);
139 test02Int32BitsUnalignedImpl(byteOrder, preBits, 0x0fffffff, 0x0fffffff);
140 test02Int32BitsUnalignedImpl(byteOrder, preBits, Integer.MIN_VALUE, -1);
141 test02Int32BitsUnalignedImpl(byteOrder, preBits, Integer.MAX_VALUE, Integer.MAX_VALUE);
142 test02Int32BitsUnalignedImpl(byteOrder, preBits, 0xffffffff, -1);
143 }
144 void test02Int32BitsUnalignedImpl(final ByteOrder byteOrder, final int preBits, final int val32, final int expUInt32Int) throws IOException {
145 final int preBytes = ( preBits + 7 ) >>> 3;
146 final int byteCount = preBytes + Buffers.SIZEOF_INT;
147 final ByteBuffer bb = ByteBuffer.allocate(byteCount);
148 if( null != byteOrder ) {
149 bb.order(byteOrder);
150 }
151 final boolean bigEndian = ByteOrder.BIG_ENDIAN == bb.order();
152 final String val32_hs = toHexString(val32);
153 System.err.println("XXX Test02Int32BitsUnaligned: byteOrder "+byteOrder+" (bigEndian "+bigEndian+"), preBits "+preBits+", value "+val32+", "+toHexBinaryString(val32, 32));
154 System.err.println("XXX Test02Int32BitsUnaligned: "+val32+", "+val32_hs);
155
156 // Test with written bitstream value
157 final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb);
158 final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, true /* outputMode */);
159 bs.writeBits31(preBits, 0);
160 bs.writeInt32(bigEndian, val32);
161 bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
162
163 final int rPre = bs.readBits31(preBits);
164 final long uint32_l = bs.readUInt32(bigEndian);
165 final int int32_l = (int)uint32_l;
166 final String uint32_l_hs = toHexString(uint32_l);
167 final int uint32_i = Bitstream.uint32LongToInt(uint32_l);
168 System.err.println("ReadPre "+rPre+", "+toBinaryString(rPre, preBits));
169 System.err.printf("Read32 uint32_l %012d, %10s; int32_l %012d %10s; uint32_i %012d %10s%n",
170 uint32_l, uint32_l_hs, int32_l, toHexString(int32_l), uint32_i, toHexString(uint32_i));
171 Assert.assertEquals(val32_hs, uint32_l_hs);
172 Assert.assertEquals(val32, int32_l);
173 Assert.assertEquals(expUInt32Int, uint32_i);
174 }
175
176 public static void main(final String args[]) throws IOException {
177 final String tstname = TestBitstream04.class.getName();
178 org.junit.runner.JUnitCore.main(tstname);
179 }
180
181}
Test Bitstream w/ int32 read/write access w/ semantics as well as with aligned and unaligned access.
static void main(final String args[])