29package com.jogamp.common.util;
31import java.io.IOException;
32import java.nio.ByteBuffer;
34import org.junit.Assert;
37import com.jogamp.junit.util.SingletonJunitCase;
38import static com.jogamp.common.util.BitDemoData.*;
40import org.junit.FixMethodOrder;
41import org.junit.runners.MethodSorters;
55@FixMethodOrder(MethodSorters.NAME_ASCENDING)
59 final int preBits,
final int skipBits,
final int postBits)
throws IOException {
60 final int bitCount = preBits+skipBits+postBits;
61 final int byteCount = ( bitCount + 7 ) / 8;
62 final ByteBuffer bbTest = ByteBuffer.allocate(byteCount);
67 sTest0 = testStringMSB.substring(0, preBits+skipBits+postBits);
69 sTest0 = testStringLSB.substring(0, preBits+skipBits+postBits);
71 if( msbFirstData == msbFirstWrite ) {
72 for(
int i=0; i<bitCount; i++) {
73 final int bit = Integer.valueOf(sTest0.substring(i, i+1));
77 for(
int i=bitCount-1; i >= 0; i--) {
78 final int bit = Integer.valueOf(sTest0.substring(i, i+1));
82 System.err.printf(
"TestData: msbFirst[data %b, write %b], bits[pre %d, skip %d, post %d = %d]: <%s>%n",
83 msbFirstData, msbFirstWrite, preBits, skipBits, postBits, bitCount, sTest0);
84 Assert.assertEquals(preBits+skipBits+postBits, bsTest.
position());
90 String getTestStreamResultAsString(
final boolean msbFirstData,
final boolean msbFirstAssemble,
91 final int preBits,
final int skipBits,
final int postBits) {
92 final String pre, post;
94 if( msbFirstAssemble ) {
95 pre = testStringMSB.substring(0, preBits);
96 post = testStringMSB.substring(preBits+skipBits, preBits+skipBits+postBits);
98 pre = testStringMSB.substring(postBits+skipBits, preBits+skipBits+postBits);
99 post = testStringMSB.substring(0, postBits);
102 if( msbFirstAssemble ) {
103 pre = testStringLSB.substring(0, preBits);
104 post = testStringLSB.substring(preBits+skipBits, preBits+skipBits+postBits);
106 pre = testStringMSB.substring(postBits+skipBits, preBits+skipBits+postBits);
107 post = testStringMSB.substring(0, postBits);
110 final String r = msbFirstAssemble ? pre + post : post + pre;
111 System.err.println(
"ResultExp: <"+pre+
"> + <"+post+
"> = <"+r+
">");
117 testLinearBitsImpl(
true );
121 testLinearBitsImpl(
false );
123 void testLinearBitsImpl(
final boolean msbFirst)
throws IOException {
124 testLinearBitsImpl(msbFirst, 0, 0, 1);
125 testLinearBitsImpl(msbFirst, 0, 0, 3);
126 testLinearBitsImpl(msbFirst, 0, 0, 8);
127 testLinearBitsImpl(msbFirst, 0, 0, 10);
128 testLinearBitsImpl(msbFirst, 0, 0, 30);
129 testLinearBitsImpl(msbFirst, 0, 0, 32);
131 testLinearBitsImpl(msbFirst, 3, 0, 3);
132 testLinearBitsImpl(msbFirst, 8, 0, 3);
133 testLinearBitsImpl(msbFirst, 9, 0, 3);
135 testLinearBitsImpl(msbFirst, 0, 1, 1);
136 testLinearBitsImpl(msbFirst, 0, 1, 3);
137 testLinearBitsImpl(msbFirst, 0, 2, 8);
138 testLinearBitsImpl(msbFirst, 0, 8, 10);
139 testLinearBitsImpl(msbFirst, 0, 12, 20);
140 testLinearBitsImpl(msbFirst, 0, 23, 9);
142 testLinearBitsImpl(msbFirst, 1, 1, 1);
143 testLinearBitsImpl(msbFirst, 2, 1, 3);
144 testLinearBitsImpl(msbFirst, 7, 2, 8);
145 testLinearBitsImpl(msbFirst, 8, 8, 8);
146 testLinearBitsImpl(msbFirst, 15, 12, 5);
147 testLinearBitsImpl(msbFirst, 16, 11, 5);
150 String readBits(
final boolean msbFirst,
final Bitstream<?> copy,
final Bitstream<?> input,
final int preCount,
final int count)
throws IOException {
151 final StringBuilder sbRead =
new StringBuilder();
154 final int bit = input.readBit(msbFirst);
155 if( Bitstream.EOS == bit ) {
156 System.err.printf(
" EOS");
159 sbRead.append( ( 0 != bit ) ?
'1' :
'0' );
161 Assert.assertEquals(i+preCount, input.position());
163 copy.writeBit(msbFirst, bit);
164 Assert.assertEquals(i+preCount, copy.position());
168 Assert.assertEquals(i+preCount, input.position());
170 Assert.assertEquals(i+preCount, copy.position());
172 return sbRead.toString();
175 void testLinearBitsImpl(
final boolean msbFirst,
final int preBits,
final int skipBits,
final int postBits)
throws IOException {
176 final int totalBits = preBits+skipBits+postBits;
177 System.err.println(
"XXX TestLinearBits: msbFirst "+msbFirst+
", preBits "+preBits+
", skipBits "+skipBits+
", postBits "+postBits+
", totalBits "+totalBits);
180 System.err.println(
"Prepare bitstream");
181 final Bitstream<ByteBuffer> bsTest = getTestStream(msbFirst, msbFirst, preBits, skipBits, postBits);
182 final String sTest = getTestStreamResultAsString(msbFirst,
true, preBits, skipBits, postBits);
185 final int byteCount = ( totalBits + 7 ) / 8;
186 final ByteBuffer bbCopy = ByteBuffer.allocate(byteCount);
187 final Bitstream.ByteBufferStream bbsCopy =
new Bitstream.ByteBufferStream(bbCopy);
188 final Bitstream<ByteBuffer> bsCopy =
new Bitstream<ByteBuffer>(bbsCopy,
true );
191 System.err.println(
"Reading bitstream: "+bsTest);
193 final String sReadPre = readBits(msbFirst, bsCopy, bsTest, 0, preBits);
195 final int skippedBits = (int) bsTest.skip(skipBits);
196 Assert.assertEquals(skipBits, skippedBits);
199 final int skippedBits = (int) bsCopy.skip(skipBits);
200 Assert.assertEquals(skipBits, skippedBits);
202 final String sReadPost = readBits(msbFirst, bsCopy, bsTest, preBits+skipBits, postBits);
203 final String sRead = sReadPre + sReadPost;
204 System.err.println(
"Read.Test: <"+sReadPre+
"> + <"+sReadPost+
"> = <"+sRead+
">");
205 Assert.assertEquals(sTest, sRead);
206 Assert.assertEquals(totalBits, bsTest.position());
207 Assert.assertEquals(totalBits, bsCopy.position());
211 bsCopy.setStream(bsCopy.getSubStream(),
false );
212 dumpData(
"Copy", bbCopy, 0, bbCopy.limit());
213 System.err.println(
"Reading copy-bitstream: "+bsCopy);
215 Assert.assertEquals(0, bsCopy.position());
217 final String sReadPre1 = readBits(msbFirst,
null, bsCopy, 0, preBits);
219 final int skippedBits = (int) bsCopy.skip(skipBits);
220 Assert.assertEquals(skipBits, skippedBits);
222 final String sReadPost1 = readBits(msbFirst,
null, bsCopy, preBits+skipBits, postBits);
223 final String sRead1 = sReadPre1 + sReadPost1;
224 System.err.println(
"Read.Copy.1: <"+sReadPre1+
"> + <"+sReadPost1+
"> = <"+sRead1+
">");
225 Assert.assertEquals(sTest, sRead1);
228 final String sReadPre2 = readBits(msbFirst,
null, bsCopy, 0, preBits);
229 Assert.assertEquals(sReadPre1, sReadPre2);
231 final int skippedBits = (int) bsCopy.skip(skipBits);
232 Assert.assertEquals(skipBits, skippedBits);
234 final String sReadPost2 = readBits(msbFirst,
null, bsCopy, preBits+skipBits, postBits);
235 Assert.assertEquals(sReadPost1, sReadPost2);
236 final String sRead2 = sReadPre2 + sReadPost2;
237 System.err.println(
"Read.Copy.2: <"+sReadPre2+
"> + <"+sReadPost2+
"> = <"+sRead2+
">");
238 Assert.assertEquals(sTest, sRead2);
239 Assert.assertEquals(totalBits, bsCopy.position());
245 testBulkBitsImpl(0, 0, 1);
246 testBulkBitsImpl(0, 0, 3);
247 testBulkBitsImpl(0, 0, 8);
248 testBulkBitsImpl(0, 0, 10);
249 testBulkBitsImpl(0, 0, 30);
250 testBulkBitsImpl(0, 0, 31);
252 testBulkBitsImpl(3, 0, 3);
253 testBulkBitsImpl(8, 0, 3);
254 testBulkBitsImpl(9, 0, 3);
255 testBulkBitsImpl(5, 0, 6);
256 testBulkBitsImpl(5, 0, 8);
258 testBulkBitsImpl(0, 1, 1);
259 testBulkBitsImpl(3, 6, 4);
261 testBulkBitsImpl(0, 1, 3);
262 testBulkBitsImpl(0, 2, 8);
263 testBulkBitsImpl(0, 8, 10);
264 testBulkBitsImpl(0, 12, 20);
265 testBulkBitsImpl(0, 23, 9);
266 testBulkBitsImpl(0, 1, 31);
268 testBulkBitsImpl(1, 1, 1);
269 testBulkBitsImpl(2, 1, 3);
270 testBulkBitsImpl(7, 2, 8);
271 testBulkBitsImpl(8, 8, 8);
272 testBulkBitsImpl(15, 12, 5);
273 testBulkBitsImpl(16, 11, 5);
274 testBulkBitsImpl(5, 6, 5);
275 testBulkBitsImpl(5, 6, 8);
278 void testBulkBitsImpl(
final int preBits,
final int skipBits,
final int postBits)
throws IOException {
279 final int totalBits = preBits+skipBits+postBits;
280 System.err.println(
"XXX TestBulkBits: preBits "+preBits+
", skipBits "+skipBits+
", postBits "+postBits+
", totalBits "+totalBits);
283 System.err.println(
"Prepare bitstream");
285 final String sTest = getTestStreamResultAsString(
true,
false, preBits, skipBits, postBits);
288 final int byteCount = ( totalBits + 7 ) / 8;
289 final ByteBuffer bbCopy = ByteBuffer.allocate(byteCount);
294 System.err.println(
"Reading bitstream: "+bsTest);
296 final int readBitsPre = bsTest.
readBits31(preBits);
297 Assert.assertEquals(readBitsPre, bsCopy.
writeBits31(preBits, readBitsPre));
299 final int skippedReadBits = (int) bsTest.
skip(skipBits);
300 final int skippedBitsCopy = (int) bsCopy.
skip(skipBits);
302 final int readBitsPost = bsTest.
readBits31(postBits);
303 Assert.assertEquals(readBitsPost, bsCopy.
writeBits31(postBits, readBitsPost));
304 final String sReadPreLo = toBinaryString(readBitsPre, preBits);
305 final String sReadPostHi = toBinaryString(readBitsPost, postBits);
306 final String sRead = sReadPostHi + sReadPreLo;
307 System.err.println(
"Read.Test: <"+sReadPreLo+
"> + <"+sReadPostHi+
"> = <"+sRead+
">");
309 Assert.assertEquals(skipBits, skippedReadBits);
310 Assert.assertEquals(sTest, sRead);
311 Assert.assertEquals(totalBits, bsTest.
position());
312 Assert.assertEquals(skipBits, skippedBitsCopy);
317 dumpData(
"Copy", bbCopy, 0, bbCopy.limit());
318 System.err.println(
"Reading copy-bitstream: "+bsCopy);
319 Assert.assertEquals(0, bsCopy.
position());
321 final int copyBitsPre = bsCopy.
readBits31(preBits);
323 final int skippedCopyBits = (int) bsCopy.
skip(skipBits);
325 final int copyBitsPost = bsCopy.
readBits31(postBits);
326 final String sCopyPreLo = toBinaryString(copyBitsPre, preBits);
327 final String sCopyPostHi = toBinaryString(copyBitsPost, postBits);
328 final String sCopy = sCopyPostHi + sCopyPreLo;
329 System.err.println(
"Copy.Test: <"+sCopyPreLo+
"> + <"+sCopyPostHi+
"> = <"+sCopy+
">");
331 Assert.assertEquals(skipBits, skippedCopyBits);
332 Assert.assertEquals(sTest, sCopy);
333 Assert.assertEquals(totalBits, bsCopy.
position());
341 System.err.println(
"01a: "+bsTest);
343 System.err.println(
"01b: "+bsTest);
347 }
catch (
final Exception e) {
348 Assert.assertNotNull(e);
352 }
catch (
final Exception e) {
353 Assert.assertNotNull(e);
357 public static void main(
final String args[])
throws IOException {
359 org.junit.runner.JUnitCore.
main(tstname);
Versatile Bitstream implementation supporting:
long skip(final long n)
It is implementation dependent, whether backward skip giving a negative number is supported or not.
int readBits31(final int n)
Return incoming bits as read via readBit(boolean) LSB-first as little-endian.
int writeBits31(final int n, final int bits)
Write the given bits via writeBit(boolean, int) LSB-first as little-endian.
final int writeBit(final boolean msbFirst, final int bit)
final T getSubStream()
Returns the currently used ByteStream's ByteStream#getStream().
final void setStream(final T stream, final boolean outputMode)
Sets the underlying stream, without close()ing the previous one.
final int readBit(final boolean msbFirst)
final long position()
Returns the bit position in the stream.
final void close()
Closing the underlying stream, implies flush().
Test Bitstream w/ raw linear and bulk read/write access w/o semantics:
static void main(final String args[])
void test01LinearBitsMSBFirst()
void test02LinearBitsLSBFirst()
void test05ErrorHandling()