diff options
author | Sven Gothel <[email protected]> | 2014-02-20 17:42:36 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-02-20 17:42:36 +0100 |
commit | d4d337be925a28b8701ac335c2b5cc6e390cabc9 (patch) | |
tree | 97b3f7bea859838635f91875fba99413c4d2a516 /src/junit/com/jogamp/common/util/TestBitstream00.java | |
parent | 86bdae8ce26d291c0096ed500581239dd2a87125 (diff) |
Bug 890: Adding versatile Bitstream implementation
We already have several locations where bitstream operations are required and
partially implemented (JPEG decoder, media parsing, ..)
as well as endian related conversion (elf parser, ..).
Create a versatile Bitstream class allowing:
- Utilize I/O operations on I/O streams, buffers and arrays
- Consider MSBfirst / LSBfirst mode
- Linear bit R/W operations
- Bulk R/W operations w/ endian related type conversion
- Allow mark/reset and switching streams and input/output mode
- Optimized operations
Complete set of unit tests included, covering hopefully all cases.
Diffstat (limited to 'src/junit/com/jogamp/common/util/TestBitstream00.java')
-rw-r--r-- | src/junit/com/jogamp/common/util/TestBitstream00.java | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/junit/com/jogamp/common/util/TestBitstream00.java b/src/junit/com/jogamp/common/util/TestBitstream00.java new file mode 100644 index 0000000..ef1fd45 --- /dev/null +++ b/src/junit/com/jogamp/common/util/TestBitstream00.java @@ -0,0 +1,115 @@ +/** + * Copyright 2014 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.common.util; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.IntBuffer; +import java.nio.LongBuffer; + +import org.junit.Test; + +import com.jogamp.common.nio.Buffers; +import com.jogamp.common.os.Platform; +import static com.jogamp.common.util.BitstreamData.*; +import com.jogamp.junit.util.JunitTracer; + +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +/** + * Test basic bit operations for {@link Bitstream} + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestBitstream00 extends JunitTracer { + + @Test + public void test00ShowByteOrder() { + int i_ff = 0xff; + byte b_ff = (byte)i_ff; + System.err.println("i_ff "+i_ff+", "+toHexBinaryString(i_ff, 8)); + System.err.println("b_ff "+b_ff+", "+toHexBinaryString(0xff & b_ff, 8)); + + System.err.println("Platform.LITTLE_ENDIAN: "+Platform.isLittleEndian()); + showOrderImpl(null); + showOrderImpl(ByteOrder.BIG_ENDIAN); + showOrderImpl(ByteOrder.LITTLE_ENDIAN); + + dumpData("tstMSB.whole", testBytesMSB, 0, testBytesMSB.length); + dumpData("tstLSB.pbyte", testBytesLSB_revByte, 0, testBytesLSB_revByte.length); + dumpData("tstLSB.whole", testBytesLSB, 0, testBytesLSB.length); + } + void showOrderImpl(ByteOrder byteOrder) { + final ByteBuffer bb_long = ByteBuffer.allocate(Buffers.SIZEOF_LONG); + if( null != byteOrder ) { + bb_long.order(byteOrder); + } + System.err.println("Order: "+byteOrder+" -> "+bb_long.order()); + final LongBuffer lb = bb_long.asLongBuffer(); + lb.put(0, 0x0807060504030201L); + dumpData("long."+byteOrder, bb_long, 0, bb_long.capacity()); + + final ByteBuffer bb_int = ByteBuffer.allocate(Buffers.SIZEOF_INT); + if( null != byteOrder ) { + bb_int.order(byteOrder); + } + final IntBuffer ib = bb_int.asIntBuffer(); + ib.put(0, 0x04030201); + dumpData("long."+byteOrder, bb_int, 0, bb_int.capacity()); + + dumpData("tstMSB.whole", testBytesMSB, 0, testBytesMSB.length); + dumpData("tstLSB.pbyte", testBytesLSB_revByte, 0, testBytesLSB_revByte.length); + dumpData("tstLSB.whole", testBytesLSB, 0, testBytesLSB.length); + } + + @Test + public void test01ShiftSigned() { + shiftSigned(0xA0000000); // negative w/ '1010' top-nibble + shiftSigned(-1); + } + void shiftSigned(final int i0) { + System.err.printf("i0 %012d, %s%n", i0, toHexBinaryString(i0, 32)); + { + int im = i0; + for(int i=0; i<32; i++) { + final int bitA = ( 0 != ( i0 & ( 1 << i ) ) ) ? 1 : 0; + final int bitB = im & 0x01; + System.err.printf("[%02d]: bit[%d, %d], im %012d, %s%n", i, bitA, bitB, im, toHexBinaryString(im, 32)); + im = im >>> 1; + } + } + } + + public static void main(String args[]) throws IOException { + String tstname = TestBitstream00.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } + +} |