GlueGen: src/junit/com/jogamp/common/util/TestBitstream01.java Source File

GlueGen v2.6.0 GlueGen, Native Binding Generator for Java™ (public API).

TestBitstream01.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.common.util;
30
31import java.io.IOException;
32import java.nio.ByteBuffer;
33
34import org.junit.Assert;
35import org.junit.Test;
36
37import com.jogamp.junit.util.SingletonJunitCase;
38import static com.jogamp.common.util.BitDemoData.*;
39
40import org.junit.FixMethodOrder;
41import org.junit.runners.MethodSorters;
42
43/**
44 * Test {@link Bitstream} w/ raw linear and bulk read/write access w/o semantics:
45 * <ul>
46 * <li>{@link Bitstream#readBit(boolean)}</li>
47 * <li>{@link Bitstream#writeBit(boolean, int)}</li>
48 * <li>{@link Bitstream#mark(int)}</li>
49 * <li>{@link Bitstream#reset()}</li>
50 * <li>{@link Bitstream#flush()}</li>
51 * <li>{@link Bitstream#readBits31(int)}</li>
52 * <li>{@link Bitstream#writeBits31(int, int)}</li>
53 * </ul>
54 */
55@FixMethodOrder(MethodSorters.NAME_ASCENDING)
56public class TestBitstream01 extends SingletonJunitCase {
57
58 Bitstream<ByteBuffer> getTestStream(final boolean msbFirstData, final boolean msbFirstWrite,
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);
63 final Bitstream.ByteBufferStream bbsTest = new Bitstream.ByteBufferStream(bbTest);
64 final Bitstream<ByteBuffer> bsTest = new Bitstream<ByteBuffer>(bbsTest, true /* outputMode */);
65 final String sTest0;
66 if( msbFirstData ) {
67 sTest0 = testStringMSB.substring(0, preBits+skipBits+postBits);
68 } else {
69 sTest0 = testStringLSB.substring(0, preBits+skipBits+postBits);
70 }
71 if( msbFirstData == msbFirstWrite ) {
72 for(int i=0; i<bitCount; i++) {
73 final int bit = Integer.valueOf(sTest0.substring(i, i+1));
74 bsTest.writeBit(msbFirstWrite, bit);
75 }
76 } else {
77 for(int i=bitCount-1; i >= 0; i--) {
78 final int bit = Integer.valueOf(sTest0.substring(i, i+1));
79 bsTest.writeBit(msbFirstWrite, bit);
80 }
81 }
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());
85 bsTest.setStream(bsTest.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
86 dumpData("TestData: ", bsTest.getSubStream(), 0, bsTest.getSubStream().limit());
87 return bsTest;
88 }
89
90 String getTestStreamResultAsString(final boolean msbFirstData, final boolean msbFirstAssemble,
91 final int preBits, final int skipBits, final int postBits) {
92 final String pre, post;
93 if( msbFirstData ) {
94 if( msbFirstAssemble ) {
95 pre = testStringMSB.substring(0, preBits);
96 post = testStringMSB.substring(preBits+skipBits, preBits+skipBits+postBits);
97 } else {
98 pre = testStringMSB.substring(postBits+skipBits, preBits+skipBits+postBits);
99 post = testStringMSB.substring(0, postBits);
100 }
101 } else {
102 if( msbFirstAssemble ) {
103 pre = testStringLSB.substring(0, preBits);
104 post = testStringLSB.substring(preBits+skipBits, preBits+skipBits+postBits);
105 } else {
106 pre = testStringMSB.substring(postBits+skipBits, preBits+skipBits+postBits);
107 post = testStringMSB.substring(0, postBits);
108 }
109 }
110 final String r = msbFirstAssemble ? pre + post : post + pre;
111 System.err.println("ResultExp: <"+pre+"> + <"+post+"> = <"+r+">");
112 return r;
113 }
114
115 @Test
116 public void test01LinearBitsMSBFirst() throws IOException {
117 testLinearBitsImpl(true /* msbFirst */);
118 }
119 @Test
120 public void test02LinearBitsLSBFirst() throws IOException {
121 testLinearBitsImpl(false /* msbFirst */);
122 }
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);
130
131 testLinearBitsImpl(msbFirst, 3, 0, 3);
132 testLinearBitsImpl(msbFirst, 8, 0, 3);
133 testLinearBitsImpl(msbFirst, 9, 0, 3);
134
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);
141
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);
148 }
149
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();
152 int i = 0;
153 while( i < count ) {
154 final int bit = input.readBit(msbFirst);
155 if( Bitstream.EOS == bit ) {
156 System.err.printf(" EOS");
157 break;
158 } else {
159 sbRead.append( ( 0 != bit ) ? '1' : '0' );
160 i++;
161 Assert.assertEquals(i+preCount, input.position());
162 if( null != copy ) {
163 copy.writeBit(msbFirst, bit);
164 Assert.assertEquals(i+preCount, copy.position());
165 }
166 }
167 }
168 Assert.assertEquals(i+preCount, input.position());
169 if( null != copy ) {
170 Assert.assertEquals(i+preCount, copy.position());
171 }
172 return sbRead.toString();
173 }
174
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);
178
179 // prepare bitstream
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);
183
184 // init copy-bitstream
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 /* outputMode */);
189
190 // read-bitstream .. and copy bits while reading
191 System.err.println("Reading bitstream: "+bsTest);
192 {
193 final String sReadPre = readBits(msbFirst, bsCopy, bsTest, 0, preBits);
194 {
195 final int skippedBits = (int) bsTest.skip(skipBits);
196 Assert.assertEquals(skipBits, skippedBits);
197 }
198 {
199 final int skippedBits = (int) bsCopy.skip(skipBits);
200 Assert.assertEquals(skipBits, skippedBits);
201 }
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());
208 }
209
210 // read copy ..
211 bsCopy.setStream(bsCopy.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
212 dumpData("Copy", bbCopy, 0, bbCopy.limit());
213 System.err.println("Reading copy-bitstream: "+bsCopy);
214 bsCopy.mark(0); // mark at beginning
215 Assert.assertEquals(0, bsCopy.position());
216 {
217 final String sReadPre1 = readBits(msbFirst, null, bsCopy, 0, preBits);
218 {
219 final int skippedBits = (int) bsCopy.skip(skipBits);
220 Assert.assertEquals(skipBits, skippedBits);
221 }
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);
226
227 bsCopy.reset();
228 final String sReadPre2 = readBits(msbFirst, null, bsCopy, 0, preBits);
229 Assert.assertEquals(sReadPre1, sReadPre2);
230 {
231 final int skippedBits = (int) bsCopy.skip(skipBits);
232 Assert.assertEquals(skipBits, skippedBits);
233 }
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());
240 }
241 }
242
243 @Test
244 public void test03BulkBits() throws IOException {
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);
251
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);
257
258 testBulkBitsImpl(0, 1, 1);
259 testBulkBitsImpl(3, 6, 4);
260
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);
267
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);
276 }
277
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);
281
282 // prepare bitstream
283 System.err.println("Prepare bitstream");
284 final Bitstream<ByteBuffer> bsTest = getTestStream(true, false, preBits, skipBits, postBits);
285 final String sTest = getTestStreamResultAsString(true, false, preBits, skipBits, postBits);
286
287 // init copy-bitstream
288 final int byteCount = ( totalBits + 7 ) / 8;
289 final ByteBuffer bbCopy = ByteBuffer.allocate(byteCount);
290 final Bitstream.ByteBufferStream bbsCopy = new Bitstream.ByteBufferStream(bbCopy);
291 final Bitstream<ByteBuffer> bsCopy = new Bitstream<ByteBuffer>(bbsCopy, true /* outputMode */);
292
293 // read-bitstream .. and copy bits while reading
294 System.err.println("Reading bitstream: "+bsTest);
295 {
296 final int readBitsPre = bsTest.readBits31(preBits);
297 Assert.assertEquals(readBitsPre, bsCopy.writeBits31(preBits, readBitsPre));
298
299 final int skippedReadBits = (int) bsTest.skip(skipBits);
300 final int skippedBitsCopy = (int) bsCopy.skip(skipBits);
301
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+">");
308
309 Assert.assertEquals(skipBits, skippedReadBits);
310 Assert.assertEquals(sTest, sRead);
311 Assert.assertEquals(totalBits, bsTest.position());
312 Assert.assertEquals(skipBits, skippedBitsCopy);
313 }
314
315 // read copy ..
316 bsCopy.setStream(bsCopy.getSubStream(), false /* outputMode */); // switch to input-mode, implies flush()
317 dumpData("Copy", bbCopy, 0, bbCopy.limit());
318 System.err.println("Reading copy-bitstream: "+bsCopy);
319 Assert.assertEquals(0, bsCopy.position());
320 {
321 final int copyBitsPre = bsCopy.readBits31(preBits);
322
323 final int skippedCopyBits = (int) bsCopy.skip(skipBits);
324
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+">");
330
331 Assert.assertEquals(skipBits, skippedCopyBits);
332 Assert.assertEquals(sTest, sCopy);
333 Assert.assertEquals(totalBits, bsCopy.position());
334 }
335 }
336
337 @Test
338 public void test05ErrorHandling() throws IOException {
339 // prepare bitstream
340 final Bitstream<ByteBuffer> bsTest = getTestStream(false, false, 0, 0, 0);
341 System.err.println("01a: "+bsTest);
342 bsTest.close();
343 System.err.println("01b: "+bsTest);
344
345 try {
346 bsTest.readBit(false);
347 } catch (final Exception e) {
348 Assert.assertNotNull(e);
349 }
350 try {
351 bsTest.writeBit(false, 1);
352 } catch (final Exception e) {
353 Assert.assertNotNull(e);
354 }
355 }
356
357 public static void main(final String args[]) throws IOException {
358 final String tstname = TestBitstream01.class.getName();
359 org.junit.runner.JUnitCore.main(tstname);
360 }
361
362}
com.jogamp.common.util.Bitstream.ByteBufferStream
Specific byte stream.
Definition: Bitstream.java:292
com.jogamp.common.util.Bitstream
Versatile Bitstream implementation supporting:
Definition: Bitstream.java:51
com.jogamp.common.util.Bitstream.skip
long skip(final long n)
It is implementation dependent, whether backward skip giving a negative number is supported or not.
Definition: Bitstream.java:952
com.jogamp.common.util.Bitstream.readBits31
int readBits31(final int n)
Return incoming bits as read via readBit(boolean) LSB-first as little-endian.
Definition: Bitstream.java:1031
com.jogamp.common.util.Bitstream.writeBits31
int writeBits31(final int n, final int bits)
Write the given bits via writeBit(boolean, int) LSB-first as little-endian.
Definition: Bitstream.java:1108
com.jogamp.common.util.Bitstream.writeBit
final int writeBit(final boolean msbFirst, final int bit)
Definition: Bitstream.java:915
com.jogamp.common.util.Bitstream.getSubStream
final T getSubStream()
Returns the currently used ByteStream's ByteStream#getStream().
Definition: Bitstream.java:686
com.jogamp.common.util.Bitstream.setStream
final void setStream(final T stream, final boolean outputMode)
Sets the underlying stream, without close()ing the previous one.
Definition: Bitstream.java:672
com.jogamp.common.util.Bitstream.readBit
final int readBit(final boolean msbFirst)
Definition: Bitstream.java:879
com.jogamp.common.util.Bitstream.position
final long position()
Returns the bit position in the stream.
Definition: Bitstream.java:826
com.jogamp.common.util.Bitstream.close
final void close()
Closing the underlying stream, implies flush().
Definition: Bitstream.java:701
com.jogamp.common.util.TestBitstream01
Test Bitstream w/ raw linear and bulk read/write access w/o semantics:
Definition: TestBitstream01.java:56
com.jogamp.common.util.TestBitstream01.main
static void main(final String args[])
Definition: TestBitstream01.java:357
com.jogamp.common.util.TestBitstream01.test01LinearBitsMSBFirst
void test01LinearBitsMSBFirst()
Definition: TestBitstream01.java:116
com.jogamp.common.util.TestBitstream01.test03BulkBits
void test03BulkBits()
Definition: TestBitstream01.java:244
com.jogamp.common.util.TestBitstream01.test02LinearBitsLSBFirst
void test02LinearBitsLSBFirst()
Definition: TestBitstream01.java:120
com.jogamp.common.util.TestBitstream01.test05ErrorHandling
void test05ErrorHandling()
Definition: TestBitstream01.java:338
com.jogamp.junit.util.SingletonJunitCase
Definition: SingletonJunitCase.java:39