diff options
author | Sven Gothel <[email protected]> | 2014-02-21 12:25:36 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-02-21 12:25:36 +0100 |
commit | 4447232af0d95a4348d09d4ed03fbef48394ca3a (patch) | |
tree | 22f385831ee15bb09771ed63421b276e849064f7 | |
parent | dac8d11f68ffa3a35fedeab879132c5d9aa4907c (diff) |
Bug 980: Refine Bitstream API 'signed' and 'unsigned' semantics - readUInt32(..) must return long due to EOF
-rwxr-xr-x | make/scripts/runtest.sh | 8 | ||||
-rw-r--r-- | src/java/com/jogamp/common/util/Bitstream.java | 82 | ||||
-rw-r--r-- | src/java/jogamp/common/os/elf/IOUtils.java | 6 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/BitstreamData.java | 5 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/TestBitstream00.java | 5 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/TestBitstream02.java | 6 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/TestBitstream03.java | 8 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/TestBitstream04.java | 81 |
8 files changed, 109 insertions, 92 deletions
diff --git a/make/scripts/runtest.sh b/make/scripts/runtest.sh index aa162a6..62376d1 100755 --- a/make/scripts/runtest.sh +++ b/make/scripts/runtest.sh @@ -104,10 +104,10 @@ function onetest() { #onetest com.jogamp.common.util.TestSyncRingBuffer01 $* #onetest com.jogamp.common.util.TestLFRingBuffer01 $* onetest com.jogamp.common.util.TestBitstream00 2>&1 | tee -a $LOG -#onetest com.jogamp.common.util.TestBitstream01 2>&1 | tee -a $LOG -#onetest com.jogamp.common.util.TestBitstream02 2>&1 | tee -a $LOG -#onetest com.jogamp.common.util.TestBitstream03 2>&1 | tee -a $LOG -#onetest com.jogamp.common.util.TestBitstream04 2>&1 | tee -a $LOG +onetest com.jogamp.common.util.TestBitstream01 2>&1 | tee -a $LOG +onetest com.jogamp.common.util.TestBitstream02 2>&1 | tee -a $LOG +onetest com.jogamp.common.util.TestBitstream03 2>&1 | tee -a $LOG +onetest com.jogamp.common.util.TestBitstream04 2>&1 | tee -a $LOG #onetest com.jogamp.common.net.TestUrisWithAssetHandler 2>&1 | tee -a $LOG #onetest com.jogamp.common.net.TestURIQueryProps 2>&1 | tee -a $LOG #onetest com.jogamp.common.net.AssetURLConnectionUnregisteredTest 2>&1 | tee -a $LOG diff --git a/src/java/com/jogamp/common/util/Bitstream.java b/src/java/com/jogamp/common/util/Bitstream.java index 656d3c4..550371c 100644 --- a/src/java/com/jogamp/common/util/Bitstream.java +++ b/src/java/com/jogamp/common/util/Bitstream.java @@ -1027,14 +1027,17 @@ public class Bitstream<T> { } /** - * Return incoming int8 as read via {@link #readBits31(boolean, int)}. + * Return incoming <code>uint8_t</code> as read via {@link #readBits31(boolean, int)}. + * <p> + * In case of a <code>int8_t</code> 2-complement signed value, simply cast the result to <code>byte</code> + * after checking for {@link #EOS}. + * </p> * @param msbFirst if true incoming stream bit order is MSB to LSB, otherwise LSB to MSB. - * @return {@link #EOS} or the 8bit value, which might be unsigned or 2-complement signed value. - * In the signed case, user shall cast the result to <code>byte</code>. + * @return {@link #EOS} or the 8bit unsigned value within the lower bits. * @throws IllegalStateException if not in input mode or stream closed * @throws IOException */ - public final int readInt8(final boolean msbFirst) throws IllegalStateException, IOException { + public final int readUInt8(final boolean msbFirst) throws IllegalStateException, IOException { if( 0 == bitCount && msbFirst ) { // fast path if( outputMode || null == bytes ) { @@ -1047,7 +1050,7 @@ public class Bitstream<T> { } /** - * Write the given int8 via {@link #writeBits31(boolean, int, int)}. + * Write the given 8 bits via {@link #writeBits31(boolean, int, int)}. * @param msbFirst if true incoming stream bit order is MSB to LSB, otherwise LSB to MSB. * @return {@link #EOS} or the written 8bit value. * @throws IllegalStateException if not in output mode or stream closed @@ -1066,16 +1069,19 @@ public class Bitstream<T> { } /** - * Return incoming int16 as read via {@link #readBits31(boolean, int)} + * Return incoming <code>uint16_t</code> as read via {@link #readBits31(boolean, int)} * and swap bytes if !bigEndian. + * <p> + * In case of a <code>int16_t</code> 2-complement signed value, simply cast the result to <code>short</code> + * after checking for {@link #EOS}. + * </p> * @param msbFirst if true incoming stream bit order is MSB to LSB, otherwise LSB to MSB. - * @param bigEndian if false, swap incoming bytes to little-endian, otherwise leave them as little-endian. - * @return {@link #EOS} or the 16bit value, which might be unsigned or 2-complement signed value. - * In the signed case, user shall cast the result to <code>short</code>. + * @param bigEndian if false, swap incoming bytes to little-endian, otherwise leave them as big-endian. + * @return {@link #EOS} or the 16bit unsigned value within the lower bits. * @throws IllegalStateException if not in input mode or stream closed * @throws IOException */ - public final int readInt16(final boolean msbFirst, final boolean bigEndian) throws IllegalStateException, IOException { + public final int readUInt16(final boolean msbFirst, final boolean bigEndian) throws IllegalStateException, IOException { if( 0 == bitCount && msbFirst ) { // fast path if( outputMode || null == bytes ) { @@ -1105,13 +1111,15 @@ public class Bitstream<T> { } /** - * Return incoming int16 value and swap bytes if !bigEndian. - * @param bigEndian if false, swap incoming bytes to little-endian, otherwise leave them as little-endian. - * @return the 16bit value, which might be unsigned or 2-complement signed value. - * In the signed case, user shall cast the result to <code>short</code>. + * Return incoming <code>uint16_t</code> value and swap bytes if !bigEndian. + * <p> + * In case of a <code>int16_t</code> 2-complement signed value, simply cast the result to <code>short</code>. + * </p> + * @param bigEndian if false, swap incoming bytes to little-endian, otherwise leave them as big-endian. + * @return the 16bit unsigned value within the lower bits. * @throws IndexOutOfBoundsException */ - public static final int readInt16(final boolean bigEndian, final byte[] bytes, final int offset) throws IndexOutOfBoundsException { + public static final int readUInt16(final boolean bigEndian, final byte[] bytes, final int offset) throws IndexOutOfBoundsException { checkBounds(bytes, offset, 2); final int b1 = bytes[offset]; final int b2 = bytes[offset+1]; @@ -1123,10 +1131,10 @@ public class Bitstream<T> { } /** - * Write the given int16 via {@link #writeBits31(boolean, int, int)}, + * Write the given 16 bits via {@link #writeBits31(boolean, int, int)}, * while swapping bytes if !bigEndian beforehand. * @param msbFirst if true incoming stream bit order is MSB to LSB, otherwise LSB to MSB. - * @param bigEndian if false, swap given bytes to little-endian, otherwise leave them as little-endian. + * @param bigEndian if false, swap given bytes to little-endian, otherwise leave them as big-endian. * @return {@link #EOS} or the written 16bit value. * @throws IllegalStateException if not in output mode or stream closed * @throws IOException @@ -1163,20 +1171,19 @@ public class Bitstream<T> { } /** - * Return incoming int32 as read via {@link #readBits31(boolean, int)} + * Return incoming <code>uint32_t</code> as read via {@link #readBits31(boolean, int)} * and swap bytes if !bigEndian. * <p> - * In case the returned value shall be interpreted as <code>uint32_t</code> - * utilize {@link #toUInt32Long(int)} or {@link #toUInt32Int(int)} for - * an appropriate conversion. + * In case of a <code>int32_t</code> 2-complement signed value, simply cast the result to <code>int</code> + * after checking for {@link #EOS}. * </p> * @param msbFirst if true incoming stream bit order is MSB to LSB, otherwise LSB to MSB. - * @param bigEndian if false, swap incoming bytes to little-endian, otherwise leave them as little-endian. - * @return {@link #EOS} or the 32bit value, which might be unsigned or 2-complement signed value. + * @param bigEndian if false, swap incoming bytes to little-endian, otherwise leave them as big-endian. + * @return {@link #EOS} or the 32bit unsigned value within the lower bits. * @throws IllegalStateException if not in input mode or stream closed * @throws IOException */ - public final int readInt32(final boolean msbFirst, final boolean bigEndian) throws IllegalStateException, IOException { + public final long readUInt32(final boolean msbFirst, final boolean bigEndian) throws IllegalStateException, IOException { if( 0 == bitCount && msbFirst ) { // fast path if( outputMode || null == bytes ) { @@ -1189,9 +1196,9 @@ public class Bitstream<T> { if( EOS == b4 ) { return EOS; } else if( bigEndian ) { - return b1 << 24 | b2 << 16 | b3 << 8 | b4; + return 0xffffffffL & ( b1 << 24 | b2 << 16 | b3 << 8 | b4 ); } else { - return b4 << 24 | b3 << 16 | b2 << 8 | b1; + return 0xffffffffL & ( b4 << 24 | b3 << 16 | b2 << 8 | b1 ); } } else { final int i16a = readBits31(msbFirst, 16); @@ -1199,44 +1206,41 @@ public class Bitstream<T> { if( EOS == i16b ) { return EOS; } else if( bigEndian ) { - return i16a << 16 | i16b; + return 0xffffffffL & ( i16a << 16 | i16b ); } else { final int b1 = 0xff & ( i16a >>> 8 ); final int b2 = 0xff & i16a; final int b3 = 0xff & ( i16b >>> 8 ); final int b4 = 0xff & i16b; - return b4 << 24 | b3 << 16 | b2 << 8 | b1; + return 0xffffffffL & ( b4 << 24 | b3 << 16 | b2 << 8 | b1 ); } } } /** - * Return incoming int32 as read via {@link #readBits31(boolean, int)} - * and swap bytes if !bigEndian. + * Return incoming <code>uint32_t</code> and swap bytes if !bigEndian. * <p> - * In case the returned value shall be interpreted as <code>uint32_t</code> - * utilize {@link #toUInt32Long(int)} or {@link #toUInt32Int(int)} for - * an appropriate conversion. + * In case of a <code>int32_t</code> 2-complement signed value, simply cast the result to <code>int</code>. * </p> - * @param bigEndian if false, swap incoming bytes to little-endian, otherwise leave them as little-endian. - * @return the 32bit value, which might be unsigned or 2-complement signed value. + * @param bigEndian if false, swap incoming bytes to little-endian, otherwise leave them as big-endian. + * @return the 32bit unsigned value within the lower bits. * @throws IndexOutOfBoundsException */ - public static final int readInt32(final boolean bigEndian, final byte[] bytes, final int offset) throws IndexOutOfBoundsException { + public static final long readUInt32(final boolean bigEndian, final byte[] bytes, final int offset) throws IndexOutOfBoundsException { checkBounds(bytes, offset, 4); final int b1 = bytes[offset]; final int b2 = bytes[offset+1]; final int b3 = bytes[offset+2]; final int b4 = bytes[offset+3]; if( bigEndian ) { - return b1 << 24 | b2 << 16 | b3 << 8 | b4; + return 0xffffffffL & ( b1 << 24 | b2 << 16 | b3 << 8 | b4 ); } else { - return b4 << 24 | b3 << 16 | b2 << 8 | b1; + return 0xffffffffL & ( b4 << 24 | b3 << 16 | b2 << 8 | b1 ); } } /** - * Write the given int32 via {@link #writeBits31(boolean, int, int)}, + * Write the given 32 bits via {@link #writeBits31(boolean, int, int)}, * while swapping bytes if !bigEndian beforehand. * @param msbFirst if true incoming stream bit order is MSB to LSB, otherwise LSB to MSB. * @param bigEndian if false, swap given bytes to little-endian, otherwise leave them as little-endian. diff --git a/src/java/jogamp/common/os/elf/IOUtils.java b/src/java/jogamp/common/os/elf/IOUtils.java index 08bbaa1..57b7a48 100644 --- a/src/java/jogamp/common/os/elf/IOUtils.java +++ b/src/java/jogamp/common/os/elf/IOUtils.java @@ -62,7 +62,7 @@ class IOUtils { } static int readUInt32(final byte[] in, final int offset) { - final int v = readInt32(in, offset); + final int v = Bitstream.uint32LongToInt(Bitstream.readUInt32(!Platform.isLittleEndian(), in, offset)); if( 0 > v ) { throw new IllegalArgumentException("Read uint32 value "+toHexString(v)+" > int32-max "+toHexString(MAX_INT_VALUE)); } @@ -74,10 +74,6 @@ class IOUtils { return b.asLongBuffer().get(0); */ } - static int readInt32(final byte[] in, final int offset) { - return Bitstream.readInt32(!Platform.isLittleEndian(), in, offset); - } - /** * @param sb byte source buffer to parse * @param offset offset within byte source buffer to start parsing diff --git a/src/junit/com/jogamp/common/util/BitstreamData.java b/src/junit/com/jogamp/common/util/BitstreamData.java index 5a8bd46..a434053 100644 --- a/src/junit/com/jogamp/common/util/BitstreamData.java +++ b/src/junit/com/jogamp/common/util/BitstreamData.java @@ -115,9 +115,4 @@ public class BitstreamData { final int nibbles = 0 == bitCount ? 2 : ( bitCount + 3 ) / 4; return String.format("[%0"+nibbles+"X, %s]", v, toBinaryString(v, bitCount)); } - public static String toUnsignedBinaryString(final int int32) { - final long l = Bitstream.toUInt32Long(int32); - final int i = Bitstream.toUInt32Int(int32); - return "(long)"+l+", (int)"+i+", "+toHexBinaryString(l, 32); - } } diff --git a/src/junit/com/jogamp/common/util/TestBitstream00.java b/src/junit/com/jogamp/common/util/TestBitstream00.java index 767117b..d0c5613 100644 --- a/src/junit/com/jogamp/common/util/TestBitstream00.java +++ b/src/junit/com/jogamp/common/util/TestBitstream00.java @@ -95,11 +95,12 @@ public class TestBitstream00 extends JunitTracer { @Test public void test01Uint32Conversion() { testUInt32Conversion(1, 1); + testUInt32Conversion(-2, -1); testUInt32Conversion(Integer.MAX_VALUE, Integer.MAX_VALUE); testUInt32Conversion(0xffff0000, -1); testUInt32Conversion(0xffffffff, -1); } - void testUInt32Conversion(final int int32, final int expUint32Int) { + void testUInt32Conversion(final int int32, final int expUInt32Int) { final String int32_hStr = toHexString(int32); final long l = Bitstream.toUInt32Long(int32); final String l_hStr = toHexString(l); @@ -107,7 +108,7 @@ public class TestBitstream00 extends JunitTracer { final String i_hStr = toHexString(i); System.err.printf("int32_t %012d %10s -> (long) %012d %10s, (int) %012d %10s%n", int32, int32_hStr, l, l_hStr, i, i_hStr); Assert.assertEquals(int32_hStr, l_hStr); - Assert.assertEquals(expUint32Int, i); + Assert.assertEquals(expUInt32Int, i); } @Test diff --git a/src/junit/com/jogamp/common/util/TestBitstream02.java b/src/junit/com/jogamp/common/util/TestBitstream02.java index dc7333c..b518df8 100644 --- a/src/junit/com/jogamp/common/util/TestBitstream02.java +++ b/src/junit/com/jogamp/common/util/TestBitstream02.java @@ -71,7 +71,7 @@ public class TestBitstream02 extends JunitTracer { final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb); final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, false /* outputMode */); { - final byte r8 = (byte) bs.readInt8(true /* msbFirst */); + final byte r8 = (byte) bs.readUInt8(true /* msbFirst */); System.err.println("Read8.1 "+r8+", "+toHexBinaryString(r8, 8)); Assert.assertEquals(val8, r8); } @@ -81,7 +81,7 @@ public class TestBitstream02 extends JunitTracer { bs.writeInt8(true /* msbFirst */, val8); bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush() { - final byte r8 = (byte) bs.readInt8(true /* msbFirst */); + final byte r8 = (byte) bs.readUInt8(true /* msbFirst */); System.err.println("Read8.2 "+r8+", "+toHexBinaryString(r8, 8)); Assert.assertEquals(val8, r8); } @@ -119,7 +119,7 @@ public class TestBitstream02 extends JunitTracer { bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush() final int rPre = (short) bs.readBits31(true /* msbFirst */, preBits); - final byte r8 = (byte) bs.readInt8(true /* msbFirst */); + final byte r8 = (byte) bs.readUInt8(true /* msbFirst */); System.err.println("ReadPre "+rPre+", "+toBinaryString(rPre, preBits)); System.err.println("Read8 "+r8+", "+toHexBinaryString(r8, 8)); Assert.assertEquals(val8, r8); diff --git a/src/junit/com/jogamp/common/util/TestBitstream03.java b/src/junit/com/jogamp/common/util/TestBitstream03.java index 7eb89e7..3647924 100644 --- a/src/junit/com/jogamp/common/util/TestBitstream03.java +++ b/src/junit/com/jogamp/common/util/TestBitstream03.java @@ -47,7 +47,7 @@ import org.junit.runners.MethodSorters; * Test {@link Bitstream} w/ int16 read/write access w/ semantics * as well as with aligned and unaligned access. * <ul> - * <li>{@link Bitstream#readInt16(boolean, boolean)}</li> + * <li>{@link Bitstream#readUInt16(boolean, boolean)}</li> * <li>{@link Bitstream#writeInt16(boolean, boolean, short)}</li> * </ul> */ @@ -82,7 +82,7 @@ public class TestBitstream03 extends JunitTracer { final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb); final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, false /* outputMode */); { - final short r16 = (short) bs.readInt16(true /* msbFirst */, bigEndian); + final short r16 = (short) bs.readUInt16(true /* msbFirst */, bigEndian); System.err.println("Read16.1 "+r16+", "+toHexBinaryString(r16, 16)); Assert.assertEquals(val16, r16); } @@ -92,7 +92,7 @@ public class TestBitstream03 extends JunitTracer { bs.writeInt16(true /* msbFirst */, bigEndian, val16); bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush() { - final short r16 = (short) bs.readInt16(true /* msbFirst */, bigEndian); + final short r16 = (short) bs.readUInt16(true /* msbFirst */, bigEndian); System.err.println("Read16.2 "+r16+", "+toHexBinaryString(r16, 16)); Assert.assertEquals(val16, r16); } @@ -140,7 +140,7 @@ public class TestBitstream03 extends JunitTracer { bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush() final int rPre = (short) bs.readBits31(true /* msbFirst */, preBits); - final short r16 = (short) bs.readInt16(true /* msbFirst */, bigEndian); + final short r16 = (short) bs.readUInt16(true /* msbFirst */, bigEndian); System.err.println("ReadPre "+rPre+", "+toBinaryString(rPre, preBits)); System.err.println("Read16 "+r16+", "+toHexBinaryString(r16, 16)); Assert.assertEquals(val16, r16); diff --git a/src/junit/com/jogamp/common/util/TestBitstream04.java b/src/junit/com/jogamp/common/util/TestBitstream04.java index dfc9c90..196db71 100644 --- a/src/junit/com/jogamp/common/util/TestBitstream04.java +++ b/src/junit/com/jogamp/common/util/TestBitstream04.java @@ -47,7 +47,7 @@ import org.junit.runners.MethodSorters; * Test {@link Bitstream} w/ int32 read/write access w/ semantics * as well as with aligned and unaligned access. * <ul> - * <li>{@link Bitstream#readInt32(boolean, boolean)}</li> + * <li>{@link Bitstream#readUInt32(boolean, boolean)}</li> * <li>{@link Bitstream#writeInt32(boolean, boolean, int)}</li> * </ul> */ @@ -61,32 +61,40 @@ public class TestBitstream04 extends JunitTracer { test01Int32BitsImpl(ByteOrder.LITTLE_ENDIAN); } void test01Int32BitsImpl(ByteOrder byteOrder) throws IOException { - test01Int32BitsAlignedImpl(byteOrder, 0); - test01Int32BitsAlignedImpl(byteOrder, 1); - test01Int32BitsAlignedImpl(byteOrder, -1); - test01Int32BitsAlignedImpl(byteOrder, 7); - test01Int32BitsAlignedImpl(byteOrder, 0x0fffffff); - test01Int32BitsAlignedImpl(byteOrder, Integer.MIN_VALUE); - test01Int32BitsAlignedImpl(byteOrder, Integer.MAX_VALUE); - test01Int32BitsAlignedImpl(byteOrder, 0xffffffff); + test01Int32BitsAlignedImpl(byteOrder, 0, 0); + test01Int32BitsAlignedImpl(byteOrder, 1, 1); + test01Int32BitsAlignedImpl(byteOrder, -1, -1); + test01Int32BitsAlignedImpl(byteOrder, 7, 7); + test01Int32BitsAlignedImpl(byteOrder, 0x0fffffff, 0x0fffffff); + test01Int32BitsAlignedImpl(byteOrder, Integer.MIN_VALUE, -1); + test01Int32BitsAlignedImpl(byteOrder, Integer.MAX_VALUE, Integer.MAX_VALUE); + test01Int32BitsAlignedImpl(byteOrder, 0xffffffff, -1); } - void test01Int32BitsAlignedImpl(ByteOrder byteOrder, int val32) throws IOException { + void test01Int32BitsAlignedImpl(ByteOrder byteOrder, int val32, int expUInt32Int) throws IOException { // Test with buffer defined value final ByteBuffer bb = ByteBuffer.allocate(Buffers.SIZEOF_INT); if( null != byteOrder ) { bb.order(byteOrder); } final boolean bigEndian = ByteOrder.BIG_ENDIAN == bb.order(); + final String val32_hs = toHexString(val32); System.err.println("XXX Test01Int32BitsAligned: byteOrder "+byteOrder+" (bigEndian "+bigEndian+"), value "+val32+", "+toHexBinaryString(val32, 32)); - System.err.println("XXX Test01Int32BitsAligned: "+toUnsignedBinaryString(val32)); + System.err.println("XXX Test01Int32BitsAligned: "+val32+", "+val32_hs); + bb.putInt(0, val32); final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb); final Bitstream<ByteBuffer> bs = new Bitstream<ByteBuffer>(bbs, false /* outputMode */); { - final int r32 = bs.readInt32(true /* msbFirst */, bigEndian); - System.err.println("Read32.1 "+r32+", "+toHexBinaryString(r32, 32)+", "+toUnsignedBinaryString(r32)); - Assert.assertEquals(val32, r32); + final long uint32_l = bs.readUInt32(true /* msbFirst */, bigEndian); + final int int32_l = (int)uint32_l; + final String uint32_l_hs = toHexString(uint32_l); + final int uint32_i = Bitstream.uint32LongToInt(uint32_l); + System.err.printf("Read32.1 uint32_l %012d, %10s; int32_l %012d %10s; uint32_i %012d %10s%n", + uint32_l, uint32_l_hs, int32_l, toHexString(int32_l), uint32_i, toHexString(uint32_i)); + Assert.assertEquals(val32_hs, uint32_l_hs); + Assert.assertEquals(val32, int32_l); + Assert.assertEquals(expUInt32Int, uint32_i); } // Test with written bitstream value @@ -94,9 +102,15 @@ public class TestBitstream04 extends JunitTracer { bs.writeInt32(true /* msbFirst */, bigEndian, val32); bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush() { - final int r32 = bs.readInt32(true /* msbFirst */, bigEndian); - System.err.println("Read32.2 "+r32+", "+toHexBinaryString(r32, 32)+", "+toUnsignedBinaryString(r32)); - Assert.assertEquals(val32, r32); + final long uint32_l = bs.readUInt32(true /* msbFirst */, bigEndian); + final int int32_l = (int)uint32_l; + final String uint32_l_hs = toHexString(uint32_l); + final int uint32_i = Bitstream.uint32LongToInt(uint32_l); + System.err.printf("Read32.2 uint32_l %012d, %10s; int32_l %012d %10s; uint32_i %012d %10s%n", + uint32_l, uint32_l_hs, int32_l, toHexString(int32_l), uint32_i, toHexString(uint32_i)); + Assert.assertEquals(val32_hs, uint32_l_hs); + Assert.assertEquals(val32, int32_l); + Assert.assertEquals(expUInt32Int, uint32_i); } } @@ -116,16 +130,16 @@ public class TestBitstream04 extends JunitTracer { test02Int32BitsUnalignedImpl(byteOrder, 25); } void test02Int32BitsUnalignedImpl(ByteOrder byteOrder, final int preBits) throws IOException { - test02Int32BitsUnalignedImpl(byteOrder, preBits, 0); - test02Int32BitsUnalignedImpl(byteOrder, preBits, 1); - test02Int32BitsUnalignedImpl(byteOrder, preBits, -1); - test02Int32BitsUnalignedImpl(byteOrder, preBits, 7); - test02Int32BitsUnalignedImpl(byteOrder, preBits, 0x0fffffff); - test02Int32BitsUnalignedImpl(byteOrder, preBits, Integer.MIN_VALUE); - test02Int32BitsUnalignedImpl(byteOrder, preBits, Integer.MAX_VALUE); - test02Int32BitsUnalignedImpl(byteOrder, preBits, 0xffffffff); + test02Int32BitsUnalignedImpl(byteOrder, preBits, 0, 0); + test02Int32BitsUnalignedImpl(byteOrder, preBits, 1, 1); + test02Int32BitsUnalignedImpl(byteOrder, preBits, -1, -1); + test02Int32BitsUnalignedImpl(byteOrder, preBits, 7, 7); + test02Int32BitsUnalignedImpl(byteOrder, preBits, 0x0fffffff, 0x0fffffff); + test02Int32BitsUnalignedImpl(byteOrder, preBits, Integer.MIN_VALUE, -1); + test02Int32BitsUnalignedImpl(byteOrder, preBits, Integer.MAX_VALUE, Integer.MAX_VALUE); + test02Int32BitsUnalignedImpl(byteOrder, preBits, 0xffffffff, -1); } - void test02Int32BitsUnalignedImpl(ByteOrder byteOrder, int preBits, int val32) throws IOException { + void test02Int32BitsUnalignedImpl(ByteOrder byteOrder, int preBits, int val32, int expUInt32Int) throws IOException { final int preBytes = ( preBits + 7 ) >>> 3; final int byteCount = preBytes + Buffers.SIZEOF_INT; final ByteBuffer bb = ByteBuffer.allocate(byteCount); @@ -133,8 +147,9 @@ public class TestBitstream04 extends JunitTracer { bb.order(byteOrder); } final boolean bigEndian = ByteOrder.BIG_ENDIAN == bb.order(); + final String val32_hs = toHexString(val32); System.err.println("XXX Test02Int32BitsUnaligned: byteOrder "+byteOrder+" (bigEndian "+bigEndian+"), preBits "+preBits+", value "+val32+", "+toHexBinaryString(val32, 32)); - System.err.println("XXX Test02Int32BitsUnaligned: "+toUnsignedBinaryString(val32)); + System.err.println("XXX Test02Int32BitsUnaligned: "+val32+", "+val32_hs); // Test with written bitstream value final Bitstream.ByteBufferStream bbs = new Bitstream.ByteBufferStream(bb); @@ -144,10 +159,16 @@ public class TestBitstream04 extends JunitTracer { bs.setStream(bs.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush() final int rPre = bs.readBits31(true /* msbFirst */, preBits); - final int r32 = bs.readInt32(true /* msbFirst */, bigEndian); + final long uint32_l = bs.readUInt32(true /* msbFirst */, bigEndian); + final int int32_l = (int)uint32_l; + final String uint32_l_hs = toHexString(uint32_l); + final int uint32_i = Bitstream.uint32LongToInt(uint32_l); System.err.println("ReadPre "+rPre+", "+toBinaryString(rPre, preBits)); - System.err.println("Read32 "+r32+", "+toHexBinaryString(r32, 32)+", "+toUnsignedBinaryString(r32)); - Assert.assertEquals(val32, r32); + System.err.printf("Read32 uint32_l %012d, %10s; int32_l %012d %10s; uint32_i %012d %10s%n", + uint32_l, uint32_l_hs, int32_l, toHexString(int32_l), uint32_i, toHexString(uint32_i)); + Assert.assertEquals(val32_hs, uint32_l_hs); + Assert.assertEquals(val32, int32_l); + Assert.assertEquals(expUInt32Int, uint32_i); } public static void main(String args[]) throws IOException { |