KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > codec > binary > BinaryCodecTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.codec.binary;
18
19 import junit.framework.TestCase;
20 import org.apache.commons.codec.DecoderException;
21 import org.apache.commons.codec.EncoderException;
22
23 /**
24  * TestCase for BinaryCodec class.
25  *
26  * @author Apache Software Foundation
27  * @version $Id: BinaryCodecTest.java,v 1.1 2004/03/29 23:04:41 ggregory Exp $
28  */

29 public class BinaryCodecTest extends TestCase {
30     /** mask with bit zero based index 0 raised */
31     private static final int BIT_0 = 0x01;
32
33     /** mask with bit zero based index 0 raised */
34     private static final int BIT_1 = 0x02;
35
36     /** mask with bit zero based index 0 raised */
37     private static final int BIT_2 = 0x04;
38
39     /** mask with bit zero based index 0 raised */
40     private static final int BIT_3 = 0x08;
41
42     /** mask with bit zero based index 0 raised */
43     private static final int BIT_4 = 0x10;
44
45     /** mask with bit zero based index 0 raised */
46     private static final int BIT_5 = 0x20;
47
48     /** mask with bit zero based index 0 raised */
49     private static final int BIT_6 = 0x40;
50
51     /** mask with bit zero based index 0 raised */
52     private static final int BIT_7 = 0x80;
53
54     /** an instance of the binary codec */
55     BinaryCodec instance = null;
56
57     /*
58      * @see TestCase#setUp()
59      */

60     protected void setUp() throws Exception JavaDoc {
61         super.setUp();
62         this.instance = new BinaryCodec();
63     }
64
65     /*
66      * @see TestCase#tearDown()
67      */

68     protected void tearDown() throws Exception JavaDoc {
69         super.tearDown();
70         this.instance = null;
71     }
72
73     /**
74      * Constructor for BinaryTest.
75      *
76      * @param arg0
77      */

78     public BinaryCodecTest(String JavaDoc arg0) {
79         super(arg0);
80     }
81
82     // ------------------------------------------------------------------------
83
//
84
// Test decode(Object)
85
//
86
// ------------------------------------------------------------------------
87
/**
88      * Tests for Object decode(Object)
89      */

90     public void testDecodeObjectException() {
91         try {
92             this.instance.decode(new Object JavaDoc());
93         } catch (DecoderException e) {
94             // all is well.
95
return;
96         }
97         fail("Expected DecoderException");
98     }
99
100     /**
101      * Tests for Object decode(Object)
102      */

103     public void testDecodeObject() throws Exception JavaDoc {
104         byte[] bits;
105         // With a single raw binary
106
bits = new byte[1];
107         assertDecodeObject(bits, "00000000");
108         bits = new byte[1];
109         bits[0] = BIT_0;
110         assertDecodeObject(bits, "00000001");
111         bits = new byte[1];
112         bits[0] = BIT_0 | BIT_1;
113         assertDecodeObject(bits, "00000011");
114         bits = new byte[1];
115         bits[0] = BIT_0 | BIT_1 | BIT_2;
116         assertDecodeObject(bits, "00000111");
117         bits = new byte[1];
118         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
119         assertDecodeObject(bits, "00001111");
120         bits = new byte[1];
121         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
122         assertDecodeObject(bits, "00011111");
123         bits = new byte[1];
124         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
125         assertDecodeObject(bits, "00111111");
126         bits = new byte[1];
127         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
128         assertDecodeObject(bits, "01111111");
129         bits = new byte[1];
130         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
131         assertDecodeObject(bits, "11111111");
132         // With a two raw binaries
133
bits = new byte[2];
134         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
135         assertDecodeObject(bits, "0000000011111111");
136         bits = new byte[2];
137         bits[1] = BIT_0;
138         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
139         assertDecodeObject(bits, "0000000111111111");
140         bits = new byte[2];
141         bits[1] = BIT_0 | BIT_1;
142         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
143         assertDecodeObject(bits, "0000001111111111");
144         bits = new byte[2];
145         bits[1] = BIT_0 | BIT_1 | BIT_2;
146         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
147         assertDecodeObject(bits, "0000011111111111");
148         bits = new byte[2];
149         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
150         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
151         assertDecodeObject(bits, "0000111111111111");
152         bits = new byte[2];
153         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
154         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
155         assertDecodeObject(bits, "0001111111111111");
156         bits = new byte[2];
157         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
158         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
159         assertDecodeObject(bits, "0011111111111111");
160         bits = new byte[2];
161         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
162         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
163         assertDecodeObject(bits, "0111111111111111");
164         bits = new byte[2];
165         bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
166         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
167         assertDecodeObject(bits, "1111111111111111");
168         assertDecodeObject(new byte[0], null);
169     }
170
171     // ------------------------------------------------------------------------
172
//
173
// Test decode(byte[])
174
//
175
// ------------------------------------------------------------------------
176
/**
177      * Utility used to assert the encoded and decoded values.
178      *
179      * @param bits
180      * the pre-encoded data
181      * @param encodeMe
182      * data to encode and compare
183      */

184     void assertDecodeObject(byte[] bits, String JavaDoc encodeMe) throws DecoderException {
185         byte[] decoded;
186         decoded = (byte[]) instance.decode(encodeMe);
187         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
188         if (encodeMe == null) {
189             decoded = instance.decode((byte[]) null);
190         } else {
191             decoded = (byte[]) instance.decode((Object JavaDoc) encodeMe.getBytes());
192         }
193         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
194         if (encodeMe == null) {
195             decoded = (byte[]) instance.decode((char[]) null);
196         } else {
197             decoded = (byte[]) instance.decode(encodeMe.toCharArray());
198         }
199         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
200     }
201
202     /*
203      * Tests for byte[] decode(byte[])
204      */

205     public void testDecodebyteArray() {
206         // With a single raw binary
207
byte[] bits = new byte[1];
208         byte[] decoded = instance.decode("00000000".getBytes());
209         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
210         bits = new byte[1];
211         bits[0] = BIT_0;
212         decoded = instance.decode("00000001".getBytes());
213         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
214         bits = new byte[1];
215         bits[0] = BIT_0 | BIT_1;
216         decoded = instance.decode("00000011".getBytes());
217         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
218         bits = new byte[1];
219         bits[0] = BIT_0 | BIT_1 | BIT_2;
220         decoded = instance.decode("00000111".getBytes());
221         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
222         bits = new byte[1];
223         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
224         decoded = instance.decode("00001111".getBytes());
225         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
226         bits = new byte[1];
227         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
228         decoded = instance.decode("00011111".getBytes());
229         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
230         bits = new byte[1];
231         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
232         decoded = instance.decode("00111111".getBytes());
233         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
234         bits = new byte[1];
235         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
236         decoded = instance.decode("01111111".getBytes());
237         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
238         bits = new byte[1];
239         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
240         decoded = instance.decode("11111111".getBytes());
241         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
242         // With a two raw binaries
243
bits = new byte[2];
244         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
245         decoded = instance.decode("0000000011111111".getBytes());
246         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
247         bits = new byte[2];
248         bits[1] = BIT_0;
249         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
250         decoded = instance.decode("0000000111111111".getBytes());
251         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
252         bits = new byte[2];
253         bits[1] = BIT_0 | BIT_1;
254         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
255         decoded = instance.decode("0000001111111111".getBytes());
256         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
257         bits = new byte[2];
258         bits[1] = BIT_0 | BIT_1 | BIT_2;
259         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
260         decoded = instance.decode("0000011111111111".getBytes());
261         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
262         bits = new byte[2];
263         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
264         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
265         decoded = instance.decode("0000111111111111".getBytes());
266         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
267         bits = new byte[2];
268         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
269         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
270         decoded = instance.decode("0001111111111111".getBytes());
271         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
272         bits = new byte[2];
273         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
274         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
275         decoded = instance.decode("0011111111111111".getBytes());
276         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
277         bits = new byte[2];
278         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
279         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
280         decoded = instance.decode("0111111111111111".getBytes());
281         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
282         bits = new byte[2];
283         bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
284         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
285         decoded = instance.decode("1111111111111111".getBytes());
286         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
287     }
288
289     // ------------------------------------------------------------------------
290
//
291
// Test toByteArray(String)
292
//
293
// ------------------------------------------------------------------------
294
/*
295      * Tests for byte[] toByteArray(String)
296      */

297     public void testToByteArrayFromString() {
298         // With a single raw binary
299
byte[] bits = new byte[1];
300         byte[] decoded = instance.toByteArray("00000000");
301         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
302         bits = new byte[1];
303         bits[0] = BIT_0;
304         decoded = instance.toByteArray("00000001");
305         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
306         bits = new byte[1];
307         bits[0] = BIT_0 | BIT_1;
308         decoded = instance.toByteArray("00000011");
309         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
310         bits = new byte[1];
311         bits[0] = BIT_0 | BIT_1 | BIT_2;
312         decoded = instance.toByteArray("00000111");
313         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
314         bits = new byte[1];
315         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
316         decoded = instance.toByteArray("00001111");
317         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
318         bits = new byte[1];
319         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
320         decoded = instance.toByteArray("00011111");
321         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
322         bits = new byte[1];
323         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
324         decoded = instance.toByteArray("00111111");
325         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
326         bits = new byte[1];
327         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
328         decoded = instance.toByteArray("01111111");
329         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
330         bits = new byte[1];
331         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
332         decoded = instance.toByteArray("11111111");
333         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
334         // With a two raw binaries
335
bits = new byte[2];
336         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
337         decoded = instance.toByteArray("0000000011111111");
338         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
339         bits = new byte[2];
340         bits[1] = BIT_0;
341         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
342         decoded = instance.toByteArray("0000000111111111");
343         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
344         bits = new byte[2];
345         bits[1] = BIT_0 | BIT_1;
346         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
347         decoded = instance.toByteArray("0000001111111111");
348         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
349         bits = new byte[2];
350         bits[1] = BIT_0 | BIT_1 | BIT_2;
351         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
352         decoded = instance.toByteArray("0000011111111111");
353         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
354         bits = new byte[2];
355         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
356         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
357         decoded = instance.toByteArray("0000111111111111");
358         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
359         bits = new byte[2];
360         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
361         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
362         decoded = instance.toByteArray("0001111111111111");
363         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
364         bits = new byte[2];
365         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
366         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
367         decoded = instance.toByteArray("0011111111111111");
368         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
369         bits = new byte[2];
370         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
371         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
372         decoded = instance.toByteArray("0111111111111111");
373         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
374         bits = new byte[2];
375         bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
376         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
377         decoded = instance.toByteArray("1111111111111111");
378         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
379         assertEquals(0, instance.toByteArray((String JavaDoc) null).length);
380     }
381
382     // ------------------------------------------------------------------------
383
//
384
// Test fromAscii(char[])
385
//
386
// ------------------------------------------------------------------------
387
/*
388      * Tests for byte[] fromAscii(char[])
389      */

390     public void testFromAsciicharArray() {
391         // With a single raw binary
392
byte[] bits = new byte[1];
393         byte[] decoded = BinaryCodec.fromAscii("00000000".toCharArray());
394         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
395         bits = new byte[1];
396         bits[0] = BIT_0;
397         decoded = BinaryCodec.fromAscii("00000001".toCharArray());
398         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
399         bits = new byte[1];
400         bits[0] = BIT_0 | BIT_1;
401         decoded = BinaryCodec.fromAscii("00000011".toCharArray());
402         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
403         bits = new byte[1];
404         bits[0] = BIT_0 | BIT_1 | BIT_2;
405         decoded = BinaryCodec.fromAscii("00000111".toCharArray());
406         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
407         bits = new byte[1];
408         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
409         decoded = BinaryCodec.fromAscii("00001111".toCharArray());
410         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
411         bits = new byte[1];
412         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
413         decoded = BinaryCodec.fromAscii("00011111".toCharArray());
414         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
415         bits = new byte[1];
416         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
417         decoded = BinaryCodec.fromAscii("00111111".toCharArray());
418         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
419         bits = new byte[1];
420         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
421         decoded = BinaryCodec.fromAscii("01111111".toCharArray());
422         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
423         bits = new byte[1];
424         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
425         decoded = BinaryCodec.fromAscii("11111111".toCharArray());
426         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
427         // With a two raw binaries
428
bits = new byte[2];
429         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
430         decoded = BinaryCodec.fromAscii("0000000011111111".toCharArray());
431         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
432         bits = new byte[2];
433         bits[1] = BIT_0;
434         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
435         decoded = BinaryCodec.fromAscii("0000000111111111".toCharArray());
436         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
437         bits = new byte[2];
438         bits[1] = BIT_0 | BIT_1;
439         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
440         decoded = BinaryCodec.fromAscii("0000001111111111".toCharArray());
441         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
442         bits = new byte[2];
443         bits[1] = BIT_0 | BIT_1 | BIT_2;
444         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
445         decoded = BinaryCodec.fromAscii("0000011111111111".toCharArray());
446         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
447         bits = new byte[2];
448         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
449         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
450         decoded = BinaryCodec.fromAscii("0000111111111111".toCharArray());
451         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
452         bits = new byte[2];
453         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
454         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
455         decoded = BinaryCodec.fromAscii("0001111111111111".toCharArray());
456         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
457         bits = new byte[2];
458         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
459         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
460         decoded = BinaryCodec.fromAscii("0011111111111111".toCharArray());
461         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
462         bits = new byte[2];
463         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
464         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
465         decoded = BinaryCodec.fromAscii("0111111111111111".toCharArray());
466         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
467         bits = new byte[2];
468         bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
469         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
470         decoded = BinaryCodec.fromAscii("1111111111111111".toCharArray());
471         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
472         assertEquals(0, BinaryCodec.fromAscii((char[]) null).length);
473     }
474
475     // ------------------------------------------------------------------------
476
//
477
// Test fromAscii(byte[])
478
//
479
// ------------------------------------------------------------------------
480
/*
481      * Tests for byte[] fromAscii(byte[])
482      */

483     public void testFromAsciibyteArray() {
484         // With a single raw binary
485
byte[] bits = new byte[1];
486         byte[] decoded = BinaryCodec.fromAscii("00000000".getBytes());
487         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
488         bits = new byte[1];
489         bits[0] = BIT_0;
490         decoded = BinaryCodec.fromAscii("00000001".getBytes());
491         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
492         bits = new byte[1];
493         bits[0] = BIT_0 | BIT_1;
494         decoded = BinaryCodec.fromAscii("00000011".getBytes());
495         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
496         bits = new byte[1];
497         bits[0] = BIT_0 | BIT_1 | BIT_2;
498         decoded = BinaryCodec.fromAscii("00000111".getBytes());
499         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
500         bits = new byte[1];
501         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
502         decoded = BinaryCodec.fromAscii("00001111".getBytes());
503         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
504         bits = new byte[1];
505         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
506         decoded = BinaryCodec.fromAscii("00011111".getBytes());
507         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
508         bits = new byte[1];
509         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
510         decoded = BinaryCodec.fromAscii("00111111".getBytes());
511         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
512         bits = new byte[1];
513         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
514         decoded = BinaryCodec.fromAscii("01111111".getBytes());
515         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
516         bits = new byte[1];
517         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
518         decoded = BinaryCodec.fromAscii("11111111".getBytes());
519         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
520         // With a two raw binaries
521
bits = new byte[2];
522         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
523         decoded = BinaryCodec.fromAscii("0000000011111111".getBytes());
524         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
525         bits = new byte[2];
526         bits[1] = BIT_0;
527         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
528         decoded = BinaryCodec.fromAscii("0000000111111111".getBytes());
529         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
530         bits = new byte[2];
531         bits[1] = BIT_0 | BIT_1;
532         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
533         decoded = BinaryCodec.fromAscii("0000001111111111".getBytes());
534         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
535         bits = new byte[2];
536         bits[1] = BIT_0 | BIT_1 | BIT_2;
537         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
538         decoded = BinaryCodec.fromAscii("0000011111111111".getBytes());
539         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
540         bits = new byte[2];
541         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
542         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
543         decoded = BinaryCodec.fromAscii("0000111111111111".getBytes());
544         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
545         bits = new byte[2];
546         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
547         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
548         decoded = BinaryCodec.fromAscii("0001111111111111".getBytes());
549         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
550         bits = new byte[2];
551         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
552         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
553         decoded = BinaryCodec.fromAscii("0011111111111111".getBytes());
554         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
555         bits = new byte[2];
556         bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
557         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
558         decoded = BinaryCodec.fromAscii("0111111111111111".getBytes());
559         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
560         bits = new byte[2];
561         bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
562         bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
563         decoded = BinaryCodec.fromAscii("1111111111111111".getBytes());
564         assertEquals(new String JavaDoc(bits), new String JavaDoc(decoded));
565         assertEquals(0, BinaryCodec.fromAscii((byte[]) null).length);
566     }
567
568     // ------------------------------------------------------------------------
569
//
570
// Test encode(byte[])
571
//
572
// ------------------------------------------------------------------------
573
/*
574      * Tests for byte[] encode(byte[])
575      */

576     public void testEncodebyteArray() {
577         // With a single raw binary
578
byte[] bits = new byte[1];
579         String JavaDoc l_encoded = new String JavaDoc(instance.encode(bits));
580         assertEquals("00000000", l_encoded);
581         bits = new byte[1];
582         bits[0] = BIT_0;
583         l_encoded = new String JavaDoc(instance.encode(bits));
584         assertEquals("00000001", l_encoded);
585         bits = new byte[1];
586         bits[0] = BIT_0 | BIT_1;
587         l_encoded = new String JavaDoc(instance.encode(bits));
588         assertEquals("00000011", l_encoded);
589         bits = new byte[1];
590         bits[0] = BIT_0 | BIT_1 | BIT_2;
591         l_encoded = new String JavaDoc(instance.encode(bits));
592         assertEquals("00000111", l_encoded);
593         bits = new byte[1];
594         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
595         l_encoded = new String JavaDoc(instance.encode(bits));
596         assertEquals("00001111", l_encoded);
597         bits = new byte[1];
598         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
599         l_encoded = new String JavaDoc(instance.encode(bits));
600         assertEquals("00011111", l_encoded);
601         bits = new byte[1];
602         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
603         l_encoded = new String JavaDoc(instance.encode(bits));
604         assertEquals("00111111", l_encoded);
605         bits = new byte[1];
606         bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;