diff options
Diffstat (limited to 'src/junit/com/jogamp/common/util/TestBitstream00.java')
-rw-r--r-- | src/junit/com/jogamp/common/util/TestBitstream00.java | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/junit/com/jogamp/common/util/TestBitstream00.java b/src/junit/com/jogamp/common/util/TestBitstream00.java index fa9c75d..920363b 100644 --- a/src/junit/com/jogamp/common/util/TestBitstream00.java +++ b/src/junit/com/jogamp/common/util/TestBitstream00.java @@ -129,6 +129,138 @@ public class TestBitstream00 extends SingletonJunitCase { } } + @Test + public void test10ReadWrite_13() throws UnsupportedOperationException, IllegalStateException, IOException { + // H->L : 00000011 00000010 00000001 000000110000001000000001 + // H->L rev: 10000000 01000000 11000000 100000000100000011000000 + // + // L->H : 00000001 00000010 00000011 000000010000001000000011 + // L->H rev: 11000000 01000000 10000000 110000000100000010000000 + test10ReadWrite1_31Impl(8, 8, 8, 0x030201, "000000110000001000000001"); + + // H->L: 00011 000010 00001 0001100001000001 + // L->H: 10000 010000 11000 1000001000011000 + test10ReadWrite1_31Impl(5, 6, 5, 0x1841, "0001100001000001"); + } + void test10ReadWrite1_31Impl(final int c1, final int c2, final int c3, final int v, final String vStrHigh2LowExp) + throws UnsupportedOperationException, IllegalStateException, IOException + { + // final Bitstream<ByteBuffer> source = new Bitstream<ByteBuffer>(); + final int bitCount = c1+c2+c3; + final int byteCount = ( bitCount + 7 ) / 8; + final String vStrHigh2Low0 = Bitstream.toBinString(true, v, bitCount); + System.err.printf("test10ReadWrite31 bits %d:%d:%d = %d = %d bytes%n", + c1, c2, c3, bitCount, byteCount); + System.err.printf("test10ReadWrite31 %s%n", Bitstream.toHexBinString(true, v, bitCount)); + System.err.printf("test10ReadWrite31 %s%n", Bitstream.toHexBinString(false, v, bitCount)); + Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low0); + + final ByteBuffer bbRead = ByteBuffer.allocate(byteCount); + for(int i=0; i<byteCount; i++) { + final int b = ( v >>> 8*i ) & 0xff; + bbRead.put(i, (byte) b); + System.err.printf("testBytes[%d]: %s%n", i, Bitstream.toHexBinString(true, b, 8)); + } + final Bitstream.ByteBufferStream bbsRead = new Bitstream.ByteBufferStream(bbRead); + final Bitstream<ByteBuffer> bsRead = new Bitstream<ByteBuffer>(bbsRead, false /* outputMode */); + + String vStrHigh2Low1C1 = ""; + String vStrHigh2Low1C2 = ""; + String vStrHigh2Low1C3 = ""; + String vStrHigh2Low1 = ""; + { + bsRead.mark(byteCount); + System.err.println("readBit (msbFirst false): "); + int b; + int i=0; + String vStrHigh2Low1T = ""; // OK for LSB, MSB segmented + while( Bitstream.EOS != ( b = bsRead.readBit(false /* msbFirst */) ) ) { + vStrHigh2Low1T = b + vStrHigh2Low1T; + if(i < c1) { + vStrHigh2Low1C1 = b + vStrHigh2Low1C1; + } else if(i < c1+c2) { + vStrHigh2Low1C2 = b + vStrHigh2Low1C2; + } else { + vStrHigh2Low1C3 = b + vStrHigh2Low1C3; + } + i++; + } + vStrHigh2Low1 = vStrHigh2Low1C3 + vStrHigh2Low1C2 + vStrHigh2Low1C1; + System.err.printf("readBit.1 %s, 0x%s%n", vStrHigh2Low1C1, Integer.toHexString(Integer.valueOf(vStrHigh2Low1C1, 2))); + System.err.printf("readBit.2 %s, 0x%s%n", vStrHigh2Low1C2, Integer.toHexString(Integer.valueOf(vStrHigh2Low1C2, 2))); + System.err.printf("readBit.3 %s, 0x%s%n", vStrHigh2Low1C3, Integer.toHexString(Integer.valueOf(vStrHigh2Low1C3, 2))); + System.err.printf("readBit.T %s, ok %b%n%n", vStrHigh2Low1T, vStrHigh2LowExp.equals(vStrHigh2Low1T)); + System.err.printf("readBit.X %s, ok %b%n%n", vStrHigh2Low1, vStrHigh2LowExp.equals(vStrHigh2Low1)); + bsRead.reset(); + } + + { + String vStrHigh2Low3T = ""; // OK for LSB, MSB segmented + System.err.println("readBits32: "); + final int b = bsRead.readBits31(bitCount); + vStrHigh2Low3T = Bitstream.toBinString(true, b, bitCount); + System.err.printf("readBits31.T %s, ok %b, %s%n%n", vStrHigh2Low3T, vStrHigh2LowExp.equals(vStrHigh2Low3T), Bitstream.toHexBinString(true, b, bitCount)); + bsRead.reset(); + } + + String vStrHigh2Low2 = ""; + { + System.err.println("readBits32: "); + final int bC1 = bsRead.readBits31(c1); + System.err.printf("readBits31.1 %s%n", Bitstream.toHexBinString(true, bC1, c1)); + final int bC2 = bsRead.readBits31(c2); + System.err.printf("readBits31.2 %s%n", Bitstream.toHexBinString(true, bC2, c2)); + final int bC3 = bsRead.readBits31(c3); + System.err.printf("readBits31.3 %s%n", Bitstream.toHexBinString(true, bC3, c3)); + final int b = bC3 << (c1+c2) | bC2 << c1 | bC1; + vStrHigh2Low2 = Bitstream.toBinString(true, b, bitCount); + System.err.printf("readBits31.X %s, ok %b, %s%n%n", vStrHigh2Low2, vStrHigh2LowExp.equals(vStrHigh2Low2), Bitstream.toHexBinString(true, b, bitCount)); + bsRead.reset(); + } + + Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low1); + Assert.assertEquals(vStrHigh2LowExp, vStrHigh2Low2); + + boolean ok = true; + { + final ByteBuffer bbWrite = ByteBuffer.allocate(byteCount); + final Bitstream.ByteBufferStream bbsWrite = new Bitstream.ByteBufferStream(bbWrite); + final Bitstream<ByteBuffer> bsWrite = new Bitstream<ByteBuffer>(bbsWrite, true /* outputMode */); + { + int b; + while( Bitstream.EOS != ( b = bsRead.readBit(false)) ) { + bsWrite.writeBit(false, b); + } + } + bsRead.reset(); + for(int i=0; i<byteCount; i++) { + final int bR = bbWrite.get(i); + final int bW = bbWrite.get(i); + System.err.printf("readWriteBit [%d]: read %s, write %s, ok %b%n", + i, Bitstream.toHexBinString(true, bR, 8), Bitstream.toHexBinString(true, bW, 8), bR==bW); + ok = ok && bR==bW; + } + Assert.assertTrue(ok); + } + { + final ByteBuffer bbWrite = ByteBuffer.allocate(byteCount); + final Bitstream.ByteBufferStream bbsWrite = new Bitstream.ByteBufferStream(bbWrite); + final Bitstream<ByteBuffer> bsWrite = new Bitstream<ByteBuffer>(bbsWrite, true /* outputMode */); + { + bsWrite.writeBits31(bitCount, bsRead.readBits31(bitCount)); + } + bsRead.reset(); + for(int i=0; i<byteCount; i++) { + final int bR = bbWrite.get(i); + final int bW = bbWrite.get(i); + System.err.printf("readWriteBits31[%d]: read %s, write %s, ok %b%n", + i, Bitstream.toHexBinString(true, bR, 8), Bitstream.toHexBinString(true, bW, 8), bR==bW); + ok = ok && bR==bW; + } + Assert.assertTrue(ok); + } + } + public static void main(final String args[]) throws IOException { final String tstname = TestBitstream00.class.getName(); org.junit.runner.JUnitCore.main(tstname); |