29import static jau.test.util.BitDemoData.*;
31import java.io.IOException;
32import java.nio.ByteBuffer;
34import org.jau.io.Bitstream;
35import org.junit.Assert;
37import org.junit.FixMethodOrder;
38import org.junit.runners.MethodSorters;
40import jau.test.junit.util.JunitTracer;
54@FixMethodOrder(MethodSorters.NAME_ASCENDING)
57 Bitstream<ByteBuffer> getTestStream(
final boolean msbFirstData,
final boolean msbFirstWrite,
58 final int preBits,
final int skipBits,
final int postBits)
throws IOException {
59 final int bitCount = preBits+skipBits+postBits;
60 final int byteCount = ( bitCount + 7 ) / 8;
61 final ByteBuffer bbTest = ByteBuffer.allocate(byteCount);
62 final Bitstream.ByteBufferStream bbsTest =
new Bitstream.ByteBufferStream(bbTest);
63 final Bitstream<ByteBuffer> bsTest =
new Bitstream<ByteBuffer>(bbsTest,
true );
66 sTest0 = testStringMSB.substring(0, preBits+skipBits+postBits);
68 sTest0 = testStringLSB.substring(0, preBits+skipBits+postBits);
70 if( msbFirstData == msbFirstWrite ) {
71 for(
int i=0; i<bitCount; i++) {
72 final int bit = Integer.valueOf(sTest0.substring(i, i+1));
73 bsTest.writeBit(msbFirstWrite, bit);
76 for(
int i=bitCount-1; i >= 0; i--) {
77 final int bit = Integer.valueOf(sTest0.substring(i, i+1));
78 bsTest.writeBit(msbFirstWrite, bit);
81 System.err.printf(
"TestData: msbFirst[data %b, write %b], bits[pre %d, skip %d, post %d = %d]: <%s>%n",
82 msbFirstData, msbFirstWrite, preBits, skipBits, postBits, bitCount, sTest0);
83 Assert.assertEquals(preBits+skipBits+postBits, bsTest.position());
84 bsTest.setStream(bsTest.getSubStream(),
false );
85 dumpData(
"TestData: ", bsTest.getSubStream(), 0, bsTest.getSubStream().limit());
89 String getTestStreamResultAsString(
final boolean msbFirstData,
final boolean msbFirstAssemble,
90 final int preBits,
final int skipBits,
final int postBits) {
91 final String pre, post;
93 if( msbFirstAssemble ) {
94 pre = testStringMSB.substring(0, preBits);
95 post = testStringMSB.substring(preBits+skipBits, preBits+skipBits+postBits);
97 pre = testStringMSB.substring(postBits+skipBits, preBits+skipBits+postBits);
98 post = testStringMSB.substring(0, postBits);
101 if( msbFirstAssemble ) {
102 pre = testStringLSB.substring(0, preBits);
103 post = testStringLSB.substring(preBits+skipBits, preBits+skipBits+postBits);
105 pre = testStringMSB.substring(postBits+skipBits, preBits+skipBits+postBits);
106 post = testStringMSB.substring(0, postBits);
109 final String r = msbFirstAssemble ? pre + post : post + pre;
110 System.err.println(
"ResultExp: <"+pre+
"> + <"+post+
"> = <"+r+
">");
116 testLinearBitsImpl(
true );
120 testLinearBitsImpl(
false );
122 void testLinearBitsImpl(
final boolean msbFirst)
throws IOException {
123 testLinearBitsImpl(msbFirst, 0, 0, 1);
124 testLinearBitsImpl(msbFirst, 0, 0, 3);
125 testLinearBitsImpl(msbFirst, 0, 0, 8);
126 testLinearBitsImpl(msbFirst, 0, 0, 10);
127 testLinearBitsImpl(msbFirst, 0, 0, 30);
128 testLinearBitsImpl(msbFirst, 0, 0, 32);
130 testLinearBitsImpl(msbFirst, 3, 0, 3);
131 testLinearBitsImpl(msbFirst, 8, 0, 3);
132 testLinearBitsImpl(msbFirst, 9, 0, 3);
134 testLinearBitsImpl(msbFirst, 0, 1, 1);
135 testLinearBitsImpl(msbFirst, 0, 1, 3);
136 testLinearBitsImpl(msbFirst, 0, 2, 8);
137 testLinearBitsImpl(msbFirst, 0, 8, 10);
138 testLinearBitsImpl(msbFirst, 0, 12, 20);
139 testLinearBitsImpl(msbFirst, 0, 23, 9);
141 testLinearBitsImpl(msbFirst, 1, 1, 1);
142 testLinearBitsImpl(msbFirst, 2, 1, 3);
143 testLinearBitsImpl(msbFirst, 7, 2, 8);
144 testLinearBitsImpl(msbFirst, 8, 8, 8);
145 testLinearBitsImpl(msbFirst, 15, 12, 5);
146 testLinearBitsImpl(msbFirst, 16, 11, 5);
149 String readBits(
final boolean msbFirst,
final Bitstream<?> copy,
final Bitstream<?> input,
final int preCount,
final int count)
throws IOException {
150 final StringBuilder sbRead =
new StringBuilder();
153 final int bit = input.readBit(msbFirst);
154 if( Bitstream.EOS == bit ) {
155 System.err.printf(
" EOS");
158 sbRead.append( ( 0 != bit ) ?
'1' :
'0' );
160 Assert.assertEquals(i+preCount, input.position());
162 copy.writeBit(msbFirst, bit);
163 Assert.assertEquals(i+preCount, copy.position());
167 Assert.assertEquals(i+preCount, input.position());
169 Assert.assertEquals(i+preCount, copy.position());
171 return sbRead.toString();
174 void testLinearBitsImpl(
final boolean msbFirst,
final int preBits,
final int skipBits,
final int postBits)
throws IOException {
175 final int totalBits = preBits+skipBits+postBits;
176 System.err.println(
"XXX TestLinearBits: msbFirst "+msbFirst+
", preBits "+preBits+
", skipBits "+skipBits+
", postBits "+postBits+
", totalBits "+totalBits);
179 System.err.println(
"Prepare bitstream");
180 final Bitstream<ByteBuffer> bsTest = getTestStream(msbFirst, msbFirst, preBits, skipBits, postBits);
181 final String sTest = getTestStreamResultAsString(msbFirst,
true, preBits, skipBits, postBits);
184 final int byteCount = ( totalBits + 7 ) / 8;
185 final ByteBuffer bbCopy = ByteBuffer.allocate(byteCount);
186 final Bitstream.ByteBufferStream bbsCopy =
new Bitstream.ByteBufferStream(bbCopy);
187 final Bitstream<ByteBuffer> bsCopy =
new Bitstream<ByteBuffer>(bbsCopy,
true );
190 System.err.println(
"Reading bitstream: "+bsTest);
192 final String sReadPre = readBits(msbFirst, bsCopy, bsTest, 0, preBits);
194 final int skippedBits = (int) bsTest.skip(skipBits);
195 Assert.assertEquals(skipBits, skippedBits);
198 final int skippedBits = (int) bsCopy.skip(skipBits);
199 Assert.assertEquals(skipBits, skippedBits);
201 final String sReadPost = readBits(msbFirst, bsCopy, bsTest, preBits+skipBits, postBits);
202 final String sRead = sReadPre + sReadPost;
203 System.err.println(
"Read.Test: <"+sReadPre+
"> + <"+sReadPost+
"> = <"+sRead+
">");
204 Assert.assertEquals(sTest, sRead);
205 Assert.assertEquals(totalBits, bsTest.position());
206 Assert.assertEquals(totalBits, bsCopy.position());
210 bsCopy.setStream(bsCopy.getSubStream(),
false );
211 dumpData(
"Copy", bbCopy, 0, bbCopy.limit());
212 System.err.println(
"Reading copy-bitstream: "+bsCopy);
214 Assert.assertEquals(0, bsCopy.position());
216 final String sReadPre1 = readBits(msbFirst,
null, bsCopy, 0, preBits);
218 final int skippedBits = (int) bsCopy.skip(skipBits);
219 Assert.assertEquals(skipBits, skippedBits);
221 final String sReadPost1 = readBits(msbFirst,
null, bsCopy, preBits+skipBits, postBits);
222 final String sRead1 = sReadPre1 + sReadPost1;
223 System.err.println(
"Read.Copy.1: <"+sReadPre1+
"> + <"+sReadPost1+
"> = <"+sRead1+
">");
224 Assert.assertEquals(sTest, sRead1);
227 final String sReadPre2 = readBits(msbFirst,
null, bsCopy, 0, preBits);
228 Assert.assertEquals(sReadPre1, sReadPre2);
230 final int skippedBits = (int) bsCopy.skip(skipBits);
231 Assert.assertEquals(skipBits, skippedBits);
233 final String sReadPost2 = readBits(msbFirst,
null, bsCopy, preBits+skipBits, postBits);
234 Assert.assertEquals(sReadPost1, sReadPost2);
235 final String sRead2 = sReadPre2 + sReadPost2;
236 System.err.println(
"Read.Copy.2: <"+sReadPre2+
"> + <"+sReadPost2+
"> = <"+sRead2+
">");
237 Assert.assertEquals(sTest, sRead2);
238 Assert.assertEquals(totalBits, bsCopy.position());
244 testBulkBitsImpl(0, 0, 1);
245 testBulkBitsImpl(0, 0, 3);
246 testBulkBitsImpl(0, 0, 8);
247 testBulkBitsImpl(0, 0, 10);
248 testBulkBitsImpl(0, 0, 30);
249 testBulkBitsImpl(0, 0, 31);
251 testBulkBitsImpl(3, 0, 3);
252 testBulkBitsImpl(8, 0, 3);
253 testBulkBitsImpl(9, 0, 3);
254 testBulkBitsImpl(5, 0, 6);
255 testBulkBitsImpl(5, 0, 8);
257 testBulkBitsImpl(0, 1, 1);
258 testBulkBitsImpl(3, 6, 4);
260 testBulkBitsImpl(0, 1, 3);
261 testBulkBitsImpl(0, 2, 8);
262 testBulkBitsImpl(0, 8, 10);
263 testBulkBitsImpl(0, 12, 20);
264 testBulkBitsImpl(0, 23, 9);
265 testBulkBitsImpl(0, 1, 31);
267 testBulkBitsImpl(1, 1, 1);
268 testBulkBitsImpl(2, 1, 3);
269 testBulkBitsImpl(7, 2, 8);
270 testBulkBitsImpl(8, 8, 8);
271 testBulkBitsImpl(15, 12, 5);
272 testBulkBitsImpl(16, 11, 5);
273 testBulkBitsImpl(5, 6, 5);
274 testBulkBitsImpl(5, 6, 8);
277 void testBulkBitsImpl(
final int preBits,
final int skipBits,
final int postBits)
throws IOException {
278 final int totalBits = preBits+skipBits+postBits;
279 System.err.println(
"XXX TestBulkBits: preBits "+preBits+
", skipBits "+skipBits+
", postBits "+postBits+
", totalBits "+totalBits);
282 System.err.println(
"Prepare bitstream");
283 final Bitstream<ByteBuffer> bsTest = getTestStream(
true,
false, preBits, skipBits, postBits);
284 final String sTest = getTestStreamResultAsString(
true,
false, preBits, skipBits, postBits);
287 final int byteCount = ( totalBits + 7 ) / 8;
288 final ByteBuffer bbCopy = ByteBuffer.allocate(byteCount);
289 final Bitstream.ByteBufferStream bbsCopy =
new Bitstream.ByteBufferStream(bbCopy);
290 final Bitstream<ByteBuffer> bsCopy =
new Bitstream<ByteBuffer>(bbsCopy,
true );
293 System.err.println(
"Reading bitstream: "+bsTest);
295 final int readBitsPre = bsTest.readBits31(preBits);
296 Assert.assertEquals(readBitsPre, bsCopy.writeBits31(preBits, readBitsPre));
298 final int skippedReadBits = (int) bsTest.skip(skipBits);
299 final int skippedBitsCopy = (int) bsCopy.skip(skipBits);
301 final int readBitsPost = bsTest.readBits31(postBits);
302 Assert.assertEquals(readBitsPost, bsCopy.writeBits31(postBits, readBitsPost));
303 final String sReadPreLo = toBinaryString(readBitsPre, preBits);
304 final String sReadPostHi = toBinaryString(readBitsPost, postBits);
305 final String sRead = sReadPostHi + sReadPreLo;
306 System.err.println(
"Read.Test: <"+sReadPreLo+
"> + <"+sReadPostHi+
"> = <"+sRead+
">");
308 Assert.assertEquals(skipBits, skippedReadBits);
309 Assert.assertEquals(sTest, sRead);
310 Assert.assertEquals(totalBits, bsTest.position());
311 Assert.assertEquals(skipBits, skippedBitsCopy);
315 bsCopy.setStream(bsCopy.getSubStream(),
false );
316 dumpData(
"Copy", bbCopy, 0, bbCopy.limit());
317 System.err.println(
"Reading copy-bitstream: "+bsCopy);
318 Assert.assertEquals(0, bsCopy.position());
320 final int copyBitsPre = bsCopy.readBits31(preBits);
322 final int skippedCopyBits = (int) bsCopy.skip(skipBits);
324 final int copyBitsPost = bsCopy.readBits31(postBits);
325 final String sCopyPreLo = toBinaryString(copyBitsPre, preBits);
326 final String sCopyPostHi = toBinaryString(copyBitsPost, postBits);
327 final String sCopy = sCopyPostHi + sCopyPreLo;
328 System.err.println(
"Copy.Test: <"+sCopyPreLo+
"> + <"+sCopyPostHi+
"> = <"+sCopy+
">");
330 Assert.assertEquals(skipBits, skippedCopyBits);
331 Assert.assertEquals(sTest, sCopy);
332 Assert.assertEquals(totalBits, bsCopy.position());
339 final Bitstream<ByteBuffer> bsTest = getTestStream(
false,
false, 0, 0, 0);
340 System.err.println(
"01a: "+bsTest);
342 System.err.println(
"01b: "+bsTest);
345 bsTest.readBit(
false);
346 }
catch (
final Exception e) {
347 Assert.assertNotNull(e);
350 bsTest.writeBit(
false, 1);
351 }
catch (
final Exception e) {
352 Assert.assertNotNull(e);
356 public static void main(
final String args[])
throws IOException {
358 org.junit.runner.JUnitCore.
main(tstname);
Test Bitstream w/ raw linear and bulk read/write access w/o semantics:
void test02LinearBitsLSBFirst()
static void main(final String args[])
void test01LinearBitsMSBFirst()
void test05ErrorHandling()