29package com.jogamp.common.util;
31import java.io.IOException;
32import java.nio.ByteBuffer;
33import java.nio.ByteOrder;
35import org.junit.Assert;
38import com.jogamp.common.nio.Buffers;
39import com.jogamp.junit.util.SingletonJunitCase;
41import static com.jogamp.common.util.BitDemoData.*;
43import org.junit.FixMethodOrder;
44import org.junit.runners.MethodSorters;
54@FixMethodOrder(MethodSorters.NAME_ASCENDING)
59 test01Int32BitsImpl(
null);
60 test01Int32BitsImpl(ByteOrder.BIG_ENDIAN);
61 test01Int32BitsImpl(ByteOrder.LITTLE_ENDIAN);
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);
73 void test01Int32BitsAlignedImpl(
final ByteOrder byteOrder,
final int val32,
final int expUInt32Int)
throws IOException {
75 final ByteBuffer bb = ByteBuffer.allocate(Buffers.SIZEOF_INT);
76 if(
null != byteOrder ) {
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);
85 dumpData(
"TestData.1: ", bb, 0, 4);
87 final Bitstream.ByteBufferStream bbs =
new Bitstream.ByteBufferStream(bb);
88 final Bitstream<ByteBuffer> bs =
new Bitstream<ByteBuffer>(bbs,
false );
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);
102 bs.setStream(bs.getSubStream(),
true );
103 bs.writeInt32(bigEndian, val32);
104 bs.setStream(bs.getSubStream(),
false );
105 dumpData(
"TestData.2: ", bb, 0, 4);
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);
121 test02Int32BitsUnalignedImpl(
null);
122 test02Int32BitsUnalignedImpl(ByteOrder.BIG_ENDIAN);
123 test02Int32BitsUnalignedImpl(ByteOrder.LITTLE_ENDIAN);
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);
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);
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 ) {
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);
157 final Bitstream.ByteBufferStream bbs =
new Bitstream.ByteBufferStream(bb);
158 final Bitstream<ByteBuffer> bs =
new Bitstream<ByteBuffer>(bbs,
true );
159 bs.writeBits31(preBits, 0);
160 bs.writeInt32(bigEndian, val32);
161 bs.setStream(bs.getSubStream(),
false );
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);
176 public static void main(
final String args[])
throws IOException {
178 org.junit.runner.JUnitCore.
main(tstname);
Test Bitstream w/ int32 read/write access w/ semantics as well as with aligned and unaligned access.
void test02Int32BitsUnaligned()
static void main(final String args[])
void test01Int32BitsAligned()