001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.codec.binary; 019 020 import static org.junit.Assert.assertEquals; 021 import static org.junit.Assert.fail; 022 023 import org.apache.commons.codec.Charsets; 024 import org.apache.commons.codec.DecoderException; 025 import org.apache.commons.codec.EncoderException; 026 import org.junit.After; 027 import org.junit.Before; 028 import org.junit.Test; 029 030 /** 031 * TestCase for BinaryCodec class. 032 * 033 * @version $Id: BinaryCodecTest.html 889935 2013-12-11 05:05:13Z ggregory $ 034 */ 035 public class BinaryCodecTest { 036 /** mask with bit zero based index 0 raised */ 037 private static final int BIT_0 = 0x01; 038 039 /** mask with bit zero based index 0 raised */ 040 private static final int BIT_1 = 0x02; 041 042 /** mask with bit zero based index 0 raised */ 043 private static final int BIT_2 = 0x04; 044 045 /** mask with bit zero based index 0 raised */ 046 private static final int BIT_3 = 0x08; 047 048 /** mask with bit zero based index 0 raised */ 049 private static final int BIT_4 = 0x10; 050 051 /** mask with bit zero based index 0 raised */ 052 private static final int BIT_5 = 0x20; 053 054 /** mask with bit zero based index 0 raised */ 055 private static final int BIT_6 = 0x40; 056 057 /** mask with bit zero based index 0 raised */ 058 private static final int BIT_7 = 0x80; 059 060 /** an instance of the binary codec */ 061 BinaryCodec instance = null; 062 063 @Before 064 public void setUp() throws Exception { 065 this.instance = new BinaryCodec(); 066 } 067 068 @After 069 public void tearDown() throws Exception { 070 this.instance = null; 071 } 072 073 // ------------------------------------------------------------------------ 074 // 075 // Test decode(Object) 076 // 077 // ------------------------------------------------------------------------ 078 /** 079 * Tests for Object decode(Object) 080 */ 081 @Test 082 public void testDecodeObjectException() { 083 try { 084 this.instance.decode(new Object()); 085 } catch (DecoderException e) { 086 // all is well. 087 return; 088 } 089 fail("Expected DecoderException"); 090 } 091 092 /** 093 * Tests for Object decode(Object) 094 */ 095 @Test 096 public void testDecodeObject() throws Exception { 097 byte[] bits; 098 // With a single raw binary 099 bits = new byte[1]; 100 assertDecodeObject(bits, "00000000"); 101 bits = new byte[1]; 102 bits[0] = BIT_0; 103 assertDecodeObject(bits, "00000001"); 104 bits = new byte[1]; 105 bits[0] = BIT_0 | BIT_1; 106 assertDecodeObject(bits, "00000011"); 107 bits = new byte[1]; 108 bits[0] = BIT_0 | BIT_1 | BIT_2; 109 assertDecodeObject(bits, "00000111"); 110 bits = new byte[1]; 111 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 112 assertDecodeObject(bits, "00001111"); 113 bits = new byte[1]; 114 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 115 assertDecodeObject(bits, "00011111"); 116 bits = new byte[1]; 117 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 118 assertDecodeObject(bits, "00111111"); 119 bits = new byte[1]; 120 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 121 assertDecodeObject(bits, "01111111"); 122 bits = new byte[1]; 123 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 124 assertDecodeObject(bits, "11111111"); 125 // With a two raw binaries 126 bits = new byte[2]; 127 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 128 assertDecodeObject(bits, "0000000011111111"); 129 bits = new byte[2]; 130 bits[1] = BIT_0; 131 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 132 assertDecodeObject(bits, "0000000111111111"); 133 bits = new byte[2]; 134 bits[1] = BIT_0 | BIT_1; 135 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 136 assertDecodeObject(bits, "0000001111111111"); 137 bits = new byte[2]; 138 bits[1] = BIT_0 | BIT_1 | BIT_2; 139 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 140 assertDecodeObject(bits, "0000011111111111"); 141 bits = new byte[2]; 142 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 143 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 144 assertDecodeObject(bits, "0000111111111111"); 145 bits = new byte[2]; 146 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 147 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 148 assertDecodeObject(bits, "0001111111111111"); 149 bits = new byte[2]; 150 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 151 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 152 assertDecodeObject(bits, "0011111111111111"); 153 bits = new byte[2]; 154 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 155 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 156 assertDecodeObject(bits, "0111111111111111"); 157 bits = new byte[2]; 158 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 159 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 160 assertDecodeObject(bits, "1111111111111111"); 161 assertDecodeObject(new byte[0], null); 162 } 163 164 // ------------------------------------------------------------------------ 165 // 166 // Test decode(byte[]) 167 // 168 // ------------------------------------------------------------------------ 169 /** 170 * Utility used to assert the encoded and decoded values. 171 * 172 * @param bits 173 * the pre-encoded data 174 * @param encodeMe 175 * data to encode and compare 176 */ 177 void assertDecodeObject(byte[] bits, String encodeMe) throws DecoderException { 178 byte[] decoded; 179 decoded = (byte[]) instance.decode(encodeMe); 180 assertEquals(new String(bits), new String(decoded)); 181 if (encodeMe == null) { 182 decoded = instance.decode((byte[]) null); 183 } else { 184 decoded = (byte[]) instance.decode((Object) encodeMe.getBytes(Charsets.UTF_8)); 185 } 186 assertEquals(new String(bits), new String(decoded)); 187 if (encodeMe == null) { 188 decoded = (byte[]) instance.decode((char[]) null); 189 } else { 190 decoded = (byte[]) instance.decode(encodeMe.toCharArray()); 191 } 192 assertEquals(new String(bits), new String(decoded)); 193 } 194 195 /* 196 * Tests for byte[] decode(byte[]) 197 */ 198 @Test 199 public void testDecodeByteArray() { 200 // With a single raw binary 201 byte[] bits = new byte[1]; 202 byte[] decoded = instance.decode("00000000".getBytes(Charsets.UTF_8)); 203 assertEquals(new String(bits), new String(decoded)); 204 bits = new byte[1]; 205 bits[0] = BIT_0; 206 decoded = instance.decode("00000001".getBytes(Charsets.UTF_8)); 207 assertEquals(new String(bits), new String(decoded)); 208 bits = new byte[1]; 209 bits[0] = BIT_0 | BIT_1; 210 decoded = instance.decode("00000011".getBytes(Charsets.UTF_8)); 211 assertEquals(new String(bits), new String(decoded)); 212 bits = new byte[1]; 213 bits[0] = BIT_0 | BIT_1 | BIT_2; 214 decoded = instance.decode("00000111".getBytes(Charsets.UTF_8)); 215 assertEquals(new String(bits), new String(decoded)); 216 bits = new byte[1]; 217 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 218 decoded = instance.decode("00001111".getBytes(Charsets.UTF_8)); 219 assertEquals(new String(bits), new String(decoded)); 220 bits = new byte[1]; 221 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 222 decoded = instance.decode("00011111".getBytes(Charsets.UTF_8)); 223 assertEquals(new String(bits), new String(decoded)); 224 bits = new byte[1]; 225 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 226 decoded = instance.decode("00111111".getBytes(Charsets.UTF_8)); 227 assertEquals(new String(bits), new String(decoded)); 228 bits = new byte[1]; 229 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 230 decoded = instance.decode("01111111".getBytes(Charsets.UTF_8)); 231 assertEquals(new String(bits), new String(decoded)); 232 bits = new byte[1]; 233 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 234 decoded = instance.decode("11111111".getBytes(Charsets.UTF_8)); 235 assertEquals(new String(bits), new String(decoded)); 236 // With a two raw binaries 237 bits = new byte[2]; 238 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 239 decoded = instance.decode("0000000011111111".getBytes(Charsets.UTF_8)); 240 assertEquals(new String(bits), new String(decoded)); 241 bits = new byte[2]; 242 bits[1] = BIT_0; 243 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 244 decoded = instance.decode("0000000111111111".getBytes(Charsets.UTF_8)); 245 assertEquals(new String(bits), new String(decoded)); 246 bits = new byte[2]; 247 bits[1] = BIT_0 | BIT_1; 248 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 249 decoded = instance.decode("0000001111111111".getBytes(Charsets.UTF_8)); 250 assertEquals(new String(bits), new String(decoded)); 251 bits = new byte[2]; 252 bits[1] = BIT_0 | BIT_1 | BIT_2; 253 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 254 decoded = instance.decode("0000011111111111".getBytes(Charsets.UTF_8)); 255 assertEquals(new String(bits), new String(decoded)); 256 bits = new byte[2]; 257 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 258 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 259 decoded = instance.decode("0000111111111111".getBytes(Charsets.UTF_8)); 260 assertEquals(new String(bits), new String(decoded)); 261 bits = new byte[2]; 262 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 263 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 264 decoded = instance.decode("0001111111111111".getBytes(Charsets.UTF_8)); 265 assertEquals(new String(bits), new String(decoded)); 266 bits = new byte[2]; 267 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 268 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 269 decoded = instance.decode("0011111111111111".getBytes(Charsets.UTF_8)); 270 assertEquals(new String(bits), new String(decoded)); 271 bits = new byte[2]; 272 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 273 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 274 decoded = instance.decode("0111111111111111".getBytes(Charsets.UTF_8)); 275 assertEquals(new String(bits), new String(decoded)); 276 bits = new byte[2]; 277 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 278 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 279 decoded = instance.decode("1111111111111111".getBytes(Charsets.UTF_8)); 280 assertEquals(new String(bits), new String(decoded)); 281 } 282 283 // ------------------------------------------------------------------------ 284 // 285 // Test toByteArray(String) 286 // 287 // ------------------------------------------------------------------------ 288 /* 289 * Tests for byte[] toByteArray(String) 290 */ 291 @Test 292 public void testToByteArrayFromString() { 293 // With a single raw binary 294 byte[] bits = new byte[1]; 295 byte[] decoded = instance.toByteArray("00000000"); 296 assertEquals(new String(bits), new String(decoded)); 297 bits = new byte[1]; 298 bits[0] = BIT_0; 299 decoded = instance.toByteArray("00000001"); 300 assertEquals(new String(bits), new String(decoded)); 301 bits = new byte[1]; 302 bits[0] = BIT_0 | BIT_1; 303 decoded = instance.toByteArray("00000011"); 304 assertEquals(new String(bits), new String(decoded)); 305 bits = new byte[1]; 306 bits[0] = BIT_0 | BIT_1 | BIT_2; 307 decoded = instance.toByteArray("00000111"); 308 assertEquals(new String(bits), new String(decoded)); 309 bits = new byte[1]; 310 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 311 decoded = instance.toByteArray("00001111"); 312 assertEquals(new String(bits), new String(decoded)); 313 bits = new byte[1]; 314 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 315 decoded = instance.toByteArray("00011111"); 316 assertEquals(new String(bits), new String(decoded)); 317 bits = new byte[1]; 318 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 319 decoded = instance.toByteArray("00111111"); 320 assertEquals(new String(bits), new String(decoded)); 321 bits = new byte[1]; 322 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 323 decoded = instance.toByteArray("01111111"); 324 assertEquals(new String(bits), new String(decoded)); 325 bits = new byte[1]; 326 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 327 decoded = instance.toByteArray("11111111"); 328 assertEquals(new String(bits), new String(decoded)); 329 // With a two raw binaries 330 bits = new byte[2]; 331 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 332 decoded = instance.toByteArray("0000000011111111"); 333 assertEquals(new String(bits), new String(decoded)); 334 bits = new byte[2]; 335 bits[1] = BIT_0; 336 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 337 decoded = instance.toByteArray("0000000111111111"); 338 assertEquals(new String(bits), new String(decoded)); 339 bits = new byte[2]; 340 bits[1] = BIT_0 | BIT_1; 341 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 342 decoded = instance.toByteArray("0000001111111111"); 343 assertEquals(new String(bits), new String(decoded)); 344 bits = new byte[2]; 345 bits[1] = BIT_0 | BIT_1 | BIT_2; 346 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 347 decoded = instance.toByteArray("0000011111111111"); 348 assertEquals(new String(bits), new String(decoded)); 349 bits = new byte[2]; 350 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 351 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 352 decoded = instance.toByteArray("0000111111111111"); 353 assertEquals(new String(bits), new String(decoded)); 354 bits = new byte[2]; 355 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 356 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 357 decoded = instance.toByteArray("0001111111111111"); 358 assertEquals(new String(bits), new String(decoded)); 359 bits = new byte[2]; 360 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 361 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 362 decoded = instance.toByteArray("0011111111111111"); 363 assertEquals(new String(bits), new String(decoded)); 364 bits = new byte[2]; 365 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 366 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 367 decoded = instance.toByteArray("0111111111111111"); 368 assertEquals(new String(bits), new String(decoded)); 369 bits = new byte[2]; 370 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 371 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 372 decoded = instance.toByteArray("1111111111111111"); 373 assertEquals(new String(bits), new String(decoded)); 374 assertEquals(0, instance.toByteArray((String) null).length); 375 } 376 377 // ------------------------------------------------------------------------ 378 // 379 // Test fromAscii(char[]) 380 // 381 // ------------------------------------------------------------------------ 382 /* 383 * Tests for byte[] fromAscii(char[]) 384 */ 385 @Test 386 public void testFromAsciiCharArray() { 387 assertEquals(0, BinaryCodec.fromAscii((char[]) null).length); 388 assertEquals(0, BinaryCodec.fromAscii(new char[0]).length); 389 // With a single raw binary 390 byte[] bits = new byte[1]; 391 byte[] decoded = BinaryCodec.fromAscii("00000000".toCharArray()); 392 assertEquals(new String(bits), new String(decoded)); 393 bits = new byte[1]; 394 bits[0] = BIT_0; 395 decoded = BinaryCodec.fromAscii("00000001".toCharArray()); 396 assertEquals(new String(bits), new String(decoded)); 397 bits = new byte[1]; 398 bits[0] = BIT_0 | BIT_1; 399 decoded = BinaryCodec.fromAscii("00000011".toCharArray()); 400 assertEquals(new String(bits), new String(decoded)); 401 bits = new byte[1]; 402 bits[0] = BIT_0 | BIT_1 | BIT_2; 403 decoded = BinaryCodec.fromAscii("00000111".toCharArray()); 404 assertEquals(new String(bits), new String(decoded)); 405 bits = new byte[1]; 406 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 407 decoded = BinaryCodec.fromAscii("00001111".toCharArray()); 408 assertEquals(new String(bits), new String(decoded)); 409 bits = new byte[1]; 410 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 411 decoded = BinaryCodec.fromAscii("00011111".toCharArray()); 412 assertEquals(new String(bits), new String(decoded)); 413 bits = new byte[1]; 414 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 415 decoded = BinaryCodec.fromAscii("00111111".toCharArray()); 416 assertEquals(new String(bits), new String(decoded)); 417 bits = new byte[1]; 418 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 419 decoded = BinaryCodec.fromAscii("01111111".toCharArray()); 420 assertEquals(new String(bits), new String(decoded)); 421 bits = new byte[1]; 422 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 423 decoded = BinaryCodec.fromAscii("11111111".toCharArray()); 424 assertEquals(new String(bits), new String(decoded)); 425 // With a two raw binaries 426 bits = new byte[2]; 427 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 428 decoded = BinaryCodec.fromAscii("0000000011111111".toCharArray()); 429 assertEquals(new String(bits), new String(decoded)); 430 bits = new byte[2]; 431 bits[1] = BIT_0; 432 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 433 decoded = BinaryCodec.fromAscii("0000000111111111".toCharArray()); 434 assertEquals(new String(bits), new String(decoded)); 435 bits = new byte[2]; 436 bits[1] = BIT_0 | BIT_1; 437 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 438 decoded = BinaryCodec.fromAscii("0000001111111111".toCharArray()); 439 assertEquals(new String(bits), new String(decoded)); 440 bits = new byte[2]; 441 bits[1] = BIT_0 | BIT_1 | BIT_2; 442 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 443 decoded = BinaryCodec.fromAscii("0000011111111111".toCharArray()); 444 assertEquals(new String(bits), new String(decoded)); 445 bits = new byte[2]; 446 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 447 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 448 decoded = BinaryCodec.fromAscii("0000111111111111".toCharArray()); 449 assertEquals(new String(bits), new String(decoded)); 450 bits = new byte[2]; 451 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 452 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 453 decoded = BinaryCodec.fromAscii("0001111111111111".toCharArray()); 454 assertEquals(new String(bits), new String(decoded)); 455 bits = new byte[2]; 456 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 457 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 458 decoded = BinaryCodec.fromAscii("0011111111111111".toCharArray()); 459 assertEquals(new String(bits), new String(decoded)); 460 bits = new byte[2]; 461 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 462 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 463 decoded = BinaryCodec.fromAscii("0111111111111111".toCharArray()); 464 assertEquals(new String(bits), new String(decoded)); 465 bits = new byte[2]; 466 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 467 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 468 decoded = BinaryCodec.fromAscii("1111111111111111".toCharArray()); 469 assertEquals(new String(bits), new String(decoded)); 470 assertEquals(0, BinaryCodec.fromAscii((char[]) null).length); 471 } 472 473 // ------------------------------------------------------------------------ 474 // 475 // Test fromAscii(byte[]) 476 // 477 // ------------------------------------------------------------------------ 478 /* 479 * Tests for byte[] fromAscii(byte[]) 480 */ 481 @Test 482 public void testFromAsciiByteArray() { 483 assertEquals(0, BinaryCodec.fromAscii((byte[]) null).length); 484 assertEquals(0, BinaryCodec.fromAscii(new byte[0]).length); 485 // With a single raw binary 486 byte[] bits = new byte[1]; 487 byte[] decoded = BinaryCodec.fromAscii("00000000".getBytes(Charsets.UTF_8)); 488 assertEquals(new String(bits), new String(decoded)); 489 bits = new byte[1]; 490 bits[0] = BIT_0; 491 decoded = BinaryCodec.fromAscii("00000001".getBytes(Charsets.UTF_8)); 492 assertEquals(new String(bits), new String(decoded)); 493 bits = new byte[1]; 494 bits[0] = BIT_0 | BIT_1; 495 decoded = BinaryCodec.fromAscii("00000011".getBytes(Charsets.UTF_8)); 496 assertEquals(new String(bits), new String(decoded)); 497 bits = new byte[1]; 498 bits[0] = BIT_0 | BIT_1 | BIT_2; 499 decoded = BinaryCodec.fromAscii("00000111".getBytes(Charsets.UTF_8)); 500 assertEquals(new String(bits), new String(decoded)); 501 bits = new byte[1]; 502 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 503 decoded = BinaryCodec.fromAscii("00001111".getBytes(Charsets.UTF_8)); 504 assertEquals(new String(bits), new String(decoded)); 505 bits = new byte[1]; 506 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 507 decoded = BinaryCodec.fromAscii("00011111".getBytes(Charsets.UTF_8)); 508 assertEquals(new String(bits), new String(decoded)); 509 bits = new byte[1]; 510 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 511 decoded = BinaryCodec.fromAscii("00111111".getBytes(Charsets.UTF_8)); 512 assertEquals(new String(bits), new String(decoded)); 513 bits = new byte[1]; 514 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 515 decoded = BinaryCodec.fromAscii("01111111".getBytes(Charsets.UTF_8)); 516 assertEquals(new String(bits), new String(decoded)); 517 bits = new byte[1]; 518 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 519 decoded = BinaryCodec.fromAscii("11111111".getBytes(Charsets.UTF_8)); 520 assertEquals(new String(bits), new String(decoded)); 521 // With a two raw binaries 522 bits = new byte[2]; 523 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 524 decoded = BinaryCodec.fromAscii("0000000011111111".getBytes(Charsets.UTF_8)); 525 assertEquals(new String(bits), new String(decoded)); 526 bits = new byte[2]; 527 bits[1] = BIT_0; 528 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 529 decoded = BinaryCodec.fromAscii("0000000111111111".getBytes(Charsets.UTF_8)); 530 assertEquals(new String(bits), new String(decoded)); 531 bits = new byte[2]; 532 bits[1] = BIT_0 | BIT_1; 533 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 534 decoded = BinaryCodec.fromAscii("0000001111111111".getBytes(Charsets.UTF_8)); 535 assertEquals(new String(bits), new String(decoded)); 536 bits = new byte[2]; 537 bits[1] = BIT_0 | BIT_1 | BIT_2; 538 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 539 decoded = BinaryCodec.fromAscii("0000011111111111".getBytes(Charsets.UTF_8)); 540 assertEquals(new String(bits), new String(decoded)); 541 bits = new byte[2]; 542 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 543 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 544 decoded = BinaryCodec.fromAscii("0000111111111111".getBytes(Charsets.UTF_8)); 545 assertEquals(new String(bits), new String(decoded)); 546 bits = new byte[2]; 547 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 548 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 549 decoded = BinaryCodec.fromAscii("0001111111111111".getBytes(Charsets.UTF_8)); 550 assertEquals(new String(bits), new String(decoded)); 551 bits = new byte[2]; 552 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 553 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 554 decoded = BinaryCodec.fromAscii("0011111111111111".getBytes(Charsets.UTF_8)); 555 assertEquals(new String(bits), new String(decoded)); 556 bits = new byte[2]; 557 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 558 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 559 decoded = BinaryCodec.fromAscii("0111111111111111".getBytes(Charsets.UTF_8)); 560 assertEquals(new String(bits), new String(decoded)); 561 bits = new byte[2]; 562 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 563 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 564 decoded = BinaryCodec.fromAscii("1111111111111111".getBytes(Charsets.UTF_8)); 565 assertEquals(new String(bits), new String(decoded)); 566 assertEquals(0, BinaryCodec.fromAscii((byte[]) null).length); 567 } 568 569 // ------------------------------------------------------------------------ 570 // 571 // Test encode(byte[]) 572 // 573 // ------------------------------------------------------------------------ 574 /* 575 * Tests for byte[] encode(byte[]) 576 */ 577 @Test 578 public void testEncodeByteArray() { 579 // With a single raw binary 580 byte[] bits = new byte[1]; 581 String l_encoded = new String(instance.encode(bits)); 582 assertEquals("00000000", l_encoded); 583 bits = new byte[1]; 584 bits[0] = BIT_0; 585 l_encoded = new String(instance.encode(bits)); 586 assertEquals("00000001", l_encoded); 587 bits = new byte[1]; 588 bits[0] = BIT_0 | BIT_1; 589 l_encoded = new String(instance.encode(bits)); 590 assertEquals("00000011", l_encoded); 591 bits = new byte[1]; 592 bits[0] = BIT_0 | BIT_1 | BIT_2; 593 l_encoded = new String(instance.encode(bits)); 594 assertEquals("00000111", l_encoded); 595 bits = new byte[1]; 596 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 597 l_encoded = new String(instance.encode(bits)); 598 assertEquals("00001111", l_encoded); 599 bits = new byte[1]; 600 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 601 l_encoded = new String(instance.encode(bits)); 602 assertEquals("00011111", l_encoded); 603 bits = new byte[1]; 604 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 605 l_encoded = new String(instance.encode(bits)); 606 assertEquals("00111111", l_encoded); 607 bits = new byte[1]; 608 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 609 l_encoded = new String(instance.encode(bits)); 610 assertEquals("01111111", l_encoded); 611 bits = new byte[1]; 612 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 613 l_encoded = new String(instance.encode(bits)); 614 assertEquals("11111111", l_encoded); 615 // With a two raw binaries 616 bits = new byte[2]; 617 l_encoded = new String(instance.encode(bits)); 618 assertEquals("0000000000000000", l_encoded); 619 bits = new byte[2]; 620 bits[0] = BIT_0; 621 l_encoded = new String(instance.encode(bits)); 622 assertEquals("0000000000000001", l_encoded); 623 bits = new byte[2]; 624 bits[0] = BIT_0 | BIT_1; 625 l_encoded = new String(instance.encode(bits)); 626 assertEquals("0000000000000011", l_encoded); 627 bits = new byte[2]; 628 bits[0] = BIT_0 | BIT_1 | BIT_2; 629 l_encoded = new String(instance.encode(bits)); 630 assertEquals("0000000000000111", l_encoded); 631 bits = new byte[2]; 632 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 633 l_encoded = new String(instance.encode(bits)); 634 assertEquals("0000000000001111", l_encoded); 635 bits = new byte[2]; 636 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 637 l_encoded = new String(instance.encode(bits)); 638 assertEquals("0000000000011111", l_encoded); 639 bits = new byte[2]; 640 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 641 l_encoded = new String(instance.encode(bits)); 642 assertEquals("0000000000111111", l_encoded); 643 bits = new byte[2]; 644 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 645 l_encoded = new String(instance.encode(bits)); 646 assertEquals("0000000001111111", l_encoded); 647 bits = new byte[2]; 648 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 649 l_encoded = new String(instance.encode(bits)); 650 assertEquals("0000000011111111", l_encoded); 651 // work on the other byte now 652 bits = new byte[2]; 653 bits[1] = BIT_0; 654 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 655 l_encoded = new String(instance.encode(bits)); 656 assertEquals("0000000111111111", l_encoded); 657 bits = new byte[2]; 658 bits[1] = BIT_0 | BIT_1; 659 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 660 l_encoded = new String(instance.encode(bits)); 661 assertEquals("0000001111111111", l_encoded); 662 bits = new byte[2]; 663 bits[1] = BIT_0 | BIT_1 | BIT_2; 664 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 665 l_encoded = new String(instance.encode(bits)); 666 assertEquals("0000011111111111", l_encoded); 667 bits = new byte[2]; 668 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 669 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 670 l_encoded = new String(instance.encode(bits)); 671 assertEquals("0000111111111111", l_encoded); 672 bits = new byte[2]; 673 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 674 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 675 l_encoded = new String(instance.encode(bits)); 676 assertEquals("0001111111111111", l_encoded); 677 bits = new byte[2]; 678 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 679 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 680 l_encoded = new String(instance.encode(bits)); 681 assertEquals("0011111111111111", l_encoded); 682 bits = new byte[2]; 683 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 684 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 685 l_encoded = new String(instance.encode(bits)); 686 assertEquals("0111111111111111", l_encoded); 687 bits = new byte[2]; 688 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 689 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 690 l_encoded = new String(instance.encode(bits)); 691 assertEquals("1111111111111111", l_encoded); 692 assertEquals(0, instance.encode((byte[]) null).length); 693 } 694 695 // ------------------------------------------------------------------------ 696 // 697 // Test toAsciiBytes 698 // 699 // ------------------------------------------------------------------------ 700 @Test 701 public void testToAsciiBytes() { 702 // With a single raw binary 703 byte[] bits = new byte[1]; 704 String l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 705 assertEquals("00000000", l_encoded); 706 bits = new byte[1]; 707 bits[0] = BIT_0; 708 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 709 assertEquals("00000001", l_encoded); 710 bits = new byte[1]; 711 bits[0] = BIT_0 | BIT_1; 712 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 713 assertEquals("00000011", l_encoded); 714 bits = new byte[1]; 715 bits[0] = BIT_0 | BIT_1 | BIT_2; 716 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 717 assertEquals("00000111", l_encoded); 718 bits = new byte[1]; 719 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 720 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 721 assertEquals("00001111", l_encoded); 722 bits = new byte[1]; 723 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 724 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 725 assertEquals("00011111", l_encoded); 726 bits = new byte[1]; 727 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 728 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 729 assertEquals("00111111", l_encoded); 730 bits = new byte[1]; 731 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 732 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 733 assertEquals("01111111", l_encoded); 734 bits = new byte[1]; 735 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 736 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 737 assertEquals("11111111", l_encoded); 738 // With a two raw binaries 739 bits = new byte[2]; 740 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 741 assertEquals("0000000000000000", l_encoded); 742 bits = new byte[2]; 743 bits[0] = BIT_0; 744 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 745 assertEquals("0000000000000001", l_encoded); 746 bits = new byte[2]; 747 bits[0] = BIT_0 | BIT_1; 748 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 749 assertEquals("0000000000000011", l_encoded); 750 bits = new byte[2]; 751 bits[0] = BIT_0 | BIT_1 | BIT_2; 752 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 753 assertEquals("0000000000000111", l_encoded); 754 bits = new byte[2]; 755 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 756 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 757 assertEquals("0000000000001111", l_encoded); 758 bits = new byte[2]; 759 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 760 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 761 assertEquals("0000000000011111", l_encoded); 762 bits = new byte[2]; 763 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 764 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 765 assertEquals("0000000000111111", l_encoded); 766 bits = new byte[2]; 767 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 768 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 769 assertEquals("0000000001111111", l_encoded); 770 bits = new byte[2]; 771 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 772 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 773 assertEquals("0000000011111111", l_encoded); 774 // work on the other byte now 775 bits = new byte[2]; 776 bits[1] = BIT_0; 777 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 778 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 779 assertEquals("0000000111111111", l_encoded); 780 bits = new byte[2]; 781 bits[1] = BIT_0 | BIT_1; 782 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 783 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 784 assertEquals("0000001111111111", l_encoded); 785 bits = new byte[2]; 786 bits[1] = BIT_0 | BIT_1 | BIT_2; 787 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 788 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 789 assertEquals("0000011111111111", l_encoded); 790 bits = new byte[2]; 791 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 792 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 793 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 794 assertEquals("0000111111111111", l_encoded); 795 bits = new byte[2]; 796 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 797 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 798 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 799 assertEquals("0001111111111111", l_encoded); 800 bits = new byte[2]; 801 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 802 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 803 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 804 assertEquals("0011111111111111", l_encoded); 805 bits = new byte[2]; 806 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 807 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 808 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 809 assertEquals("0111111111111111", l_encoded); 810 bits = new byte[2]; 811 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 812 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 813 l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); 814 assertEquals("1111111111111111", l_encoded); 815 assertEquals(0, BinaryCodec.toAsciiBytes((byte[]) null).length); 816 } 817 818 // ------------------------------------------------------------------------ 819 // 820 // Test toAsciiChars 821 // 822 // ------------------------------------------------------------------------ 823 @Test 824 public void testToAsciiChars() { 825 // With a single raw binary 826 byte[] bits = new byte[1]; 827 String l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 828 assertEquals("00000000", l_encoded); 829 bits = new byte[1]; 830 bits[0] = BIT_0; 831 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 832 assertEquals("00000001", l_encoded); 833 bits = new byte[1]; 834 bits[0] = BIT_0 | BIT_1; 835 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 836 assertEquals("00000011", l_encoded); 837 bits = new byte[1]; 838 bits[0] = BIT_0 | BIT_1 | BIT_2; 839 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 840 assertEquals("00000111", l_encoded); 841 bits = new byte[1]; 842 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 843 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 844 assertEquals("00001111", l_encoded); 845 bits = new byte[1]; 846 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 847 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 848 assertEquals("00011111", l_encoded); 849 bits = new byte[1]; 850 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 851 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 852 assertEquals("00111111", l_encoded); 853 bits = new byte[1]; 854 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 855 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 856 assertEquals("01111111", l_encoded); 857 bits = new byte[1]; 858 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 859 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 860 assertEquals("11111111", l_encoded); 861 // With a two raw binaries 862 bits = new byte[2]; 863 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 864 assertEquals("0000000000000000", l_encoded); 865 bits = new byte[2]; 866 bits[0] = BIT_0; 867 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 868 assertEquals("0000000000000001", l_encoded); 869 bits = new byte[2]; 870 bits[0] = BIT_0 | BIT_1; 871 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 872 assertEquals("0000000000000011", l_encoded); 873 bits = new byte[2]; 874 bits[0] = BIT_0 | BIT_1 | BIT_2; 875 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 876 assertEquals("0000000000000111", l_encoded); 877 bits = new byte[2]; 878 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 879 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 880 assertEquals("0000000000001111", l_encoded); 881 bits = new byte[2]; 882 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 883 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 884 assertEquals("0000000000011111", l_encoded); 885 bits = new byte[2]; 886 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 887 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 888 assertEquals("0000000000111111", l_encoded); 889 bits = new byte[2]; 890 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 891 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 892 assertEquals("0000000001111111", l_encoded); 893 bits = new byte[2]; 894 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 895 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 896 assertEquals("0000000011111111", l_encoded); 897 // work on the other byte now 898 bits = new byte[2]; 899 bits[1] = BIT_0; 900 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 901 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 902 assertEquals("0000000111111111", l_encoded); 903 bits = new byte[2]; 904 bits[1] = BIT_0 | BIT_1; 905 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 906 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 907 assertEquals("0000001111111111", l_encoded); 908 bits = new byte[2]; 909 bits[1] = BIT_0 | BIT_1 | BIT_2; 910 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 911 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 912 assertEquals("0000011111111111", l_encoded); 913 bits = new byte[2]; 914 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 915 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 916 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 917 assertEquals("0000111111111111", l_encoded); 918 bits = new byte[2]; 919 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 920 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 921 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 922 assertEquals("0001111111111111", l_encoded); 923 bits = new byte[2]; 924 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 925 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 926 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 927 assertEquals("0011111111111111", l_encoded); 928 bits = new byte[2]; 929 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 930 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 931 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 932 assertEquals("0111111111111111", l_encoded); 933 bits = new byte[2]; 934 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 935 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 936 l_encoded = new String(BinaryCodec.toAsciiChars(bits)); 937 assertEquals("1111111111111111", l_encoded); 938 assertEquals(0, BinaryCodec.toAsciiChars((byte[]) null).length); 939 } 940 941 // ------------------------------------------------------------------------ 942 // 943 // Test toAsciiString 944 // 945 // ------------------------------------------------------------------------ 946 /** 947 * Tests the toAsciiString(byte[]) method 948 */ 949 @Test 950 public void testToAsciiString() { 951 // With a single raw binary 952 byte[] bits = new byte[1]; 953 String l_encoded = BinaryCodec.toAsciiString(bits); 954 assertEquals("00000000", l_encoded); 955 bits = new byte[1]; 956 bits[0] = BIT_0; 957 l_encoded = BinaryCodec.toAsciiString(bits); 958 assertEquals("00000001", l_encoded); 959 bits = new byte[1]; 960 bits[0] = BIT_0 | BIT_1; 961 l_encoded = BinaryCodec.toAsciiString(bits); 962 assertEquals("00000011", l_encoded); 963 bits = new byte[1]; 964 bits[0] = BIT_0 | BIT_1 | BIT_2; 965 l_encoded = BinaryCodec.toAsciiString(bits); 966 assertEquals("00000111", l_encoded); 967 bits = new byte[1]; 968 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 969 l_encoded = BinaryCodec.toAsciiString(bits); 970 assertEquals("00001111", l_encoded); 971 bits = new byte[1]; 972 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 973 l_encoded = BinaryCodec.toAsciiString(bits); 974 assertEquals("00011111", l_encoded); 975 bits = new byte[1]; 976 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 977 l_encoded = BinaryCodec.toAsciiString(bits); 978 assertEquals("00111111", l_encoded); 979 bits = new byte[1]; 980 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 981 l_encoded = BinaryCodec.toAsciiString(bits); 982 assertEquals("01111111", l_encoded); 983 bits = new byte[1]; 984 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 985 l_encoded = BinaryCodec.toAsciiString(bits); 986 assertEquals("11111111", l_encoded); 987 // With a two raw binaries 988 bits = new byte[2]; 989 l_encoded = BinaryCodec.toAsciiString(bits); 990 assertEquals("0000000000000000", l_encoded); 991 bits = new byte[2]; 992 bits[0] = BIT_0; 993 l_encoded = BinaryCodec.toAsciiString(bits); 994 assertEquals("0000000000000001", l_encoded); 995 bits = new byte[2]; 996 bits[0] = BIT_0 | BIT_1; 997 l_encoded = BinaryCodec.toAsciiString(bits); 998 assertEquals("0000000000000011", l_encoded); 999 bits = new byte[2]; 1000 bits[0] = BIT_0 | BIT_1 | BIT_2; 1001 l_encoded = BinaryCodec.toAsciiString(bits); 1002 assertEquals("0000000000000111", l_encoded); 1003 bits = new byte[2]; 1004 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 1005 l_encoded = BinaryCodec.toAsciiString(bits); 1006 assertEquals("0000000000001111", l_encoded); 1007 bits = new byte[2]; 1008 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 1009 l_encoded = BinaryCodec.toAsciiString(bits); 1010 assertEquals("0000000000011111", l_encoded); 1011 bits = new byte[2]; 1012 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 1013 l_encoded = BinaryCodec.toAsciiString(bits); 1014 assertEquals("0000000000111111", l_encoded); 1015 bits = new byte[2]; 1016 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 1017 l_encoded = BinaryCodec.toAsciiString(bits); 1018 assertEquals("0000000001111111", l_encoded); 1019 bits = new byte[2]; 1020 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1021 l_encoded = BinaryCodec.toAsciiString(bits); 1022 assertEquals("0000000011111111", l_encoded); 1023 // work on the other byte now 1024 bits = new byte[2]; 1025 bits[1] = BIT_0; 1026 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1027 l_encoded = BinaryCodec.toAsciiString(bits); 1028 assertEquals("0000000111111111", l_encoded); 1029 bits = new byte[2]; 1030 bits[1] = BIT_0 | BIT_1; 1031 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1032 l_encoded = BinaryCodec.toAsciiString(bits); 1033 assertEquals("0000001111111111", l_encoded); 1034 bits = new byte[2]; 1035 bits[1] = BIT_0 | BIT_1 | BIT_2; 1036 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1037 l_encoded = BinaryCodec.toAsciiString(bits); 1038 assertEquals("0000011111111111", l_encoded); 1039 bits = new byte[2]; 1040 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 1041 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1042 l_encoded = BinaryCodec.toAsciiString(bits); 1043 assertEquals("0000111111111111", l_encoded); 1044 bits = new byte[2]; 1045 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 1046 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1047 l_encoded = BinaryCodec.toAsciiString(bits); 1048 assertEquals("0001111111111111", l_encoded); 1049 bits = new byte[2]; 1050 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 1051 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1052 l_encoded = BinaryCodec.toAsciiString(bits); 1053 assertEquals("0011111111111111", l_encoded); 1054 bits = new byte[2]; 1055 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 1056 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1057 l_encoded = BinaryCodec.toAsciiString(bits); 1058 assertEquals("0111111111111111", l_encoded); 1059 bits = new byte[2]; 1060 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1061 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1062 l_encoded = BinaryCodec.toAsciiString(bits); 1063 assertEquals("1111111111111111", l_encoded); 1064 } 1065 1066 // ------------------------------------------------------------------------ 1067 // 1068 // Test encode(Object) 1069 // 1070 // ------------------------------------------------------------------------ 1071 /* 1072 * Tests for Object encode(Object) 1073 */ 1074 @Test 1075 public void testEncodeObjectNull() throws Exception { 1076 Object obj = new byte[0]; 1077 assertEquals(0, ((char[]) instance.encode(obj)).length); 1078 } 1079 1080 /* 1081 * Tests for Object encode(Object) 1082 */ 1083 @Test 1084 public void testEncodeObjectException() { 1085 try { 1086 instance.encode(""); 1087 } catch (EncoderException e) { 1088 // all is well. 1089 return; 1090 } 1091 fail("Expected EncoderException"); 1092 } 1093 1094 /* 1095 * Tests for Object encode(Object) 1096 */ 1097 @Test 1098 public void testEncodeObject() throws Exception { 1099 // With a single raw binary 1100 byte[] bits = new byte[1]; 1101 String l_encoded = new String((char[]) instance.encode((Object) bits)); 1102 assertEquals("00000000", l_encoded); 1103 bits = new byte[1]; 1104 bits[0] = BIT_0; 1105 l_encoded = new String((char[]) instance.encode((Object) bits)); 1106 assertEquals("00000001", l_encoded); 1107 bits = new byte[1]; 1108 bits[0] = BIT_0 | BIT_1; 1109 l_encoded = new String((char[]) instance.encode((Object) bits)); 1110 assertEquals("00000011", l_encoded); 1111 bits = new byte[1]; 1112 bits[0] = BIT_0 | BIT_1 | BIT_2; 1113 l_encoded = new String((char[]) instance.encode((Object) bits)); 1114 assertEquals("00000111", l_encoded); 1115 bits = new byte[1]; 1116 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 1117 l_encoded = new String((char[]) instance.encode((Object) bits)); 1118 assertEquals("00001111", l_encoded); 1119 bits = new byte[1]; 1120 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 1121 l_encoded = new String((char[]) instance.encode((Object) bits)); 1122 assertEquals("00011111", l_encoded); 1123 bits = new byte[1]; 1124 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 1125 l_encoded = new String((char[]) instance.encode((Object) bits)); 1126 assertEquals("00111111", l_encoded); 1127 bits = new byte[1]; 1128 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 1129 l_encoded = new String((char[]) instance.encode((Object) bits)); 1130 assertEquals("01111111", l_encoded); 1131 bits = new byte[1]; 1132 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1133 l_encoded = new String((char[]) instance.encode((Object) bits)); 1134 assertEquals("11111111", l_encoded); 1135 // With a two raw binaries 1136 bits = new byte[2]; 1137 l_encoded = new String((char[]) instance.encode((Object) bits)); 1138 assertEquals("0000000000000000", l_encoded); 1139 bits = new byte[2]; 1140 bits[0] = BIT_0; 1141 l_encoded = new String((char[]) instance.encode((Object) bits)); 1142 assertEquals("0000000000000001", l_encoded); 1143 bits = new byte[2]; 1144 bits[0] = BIT_0 | BIT_1; 1145 l_encoded = new String((char[]) instance.encode((Object) bits)); 1146 assertEquals("0000000000000011", l_encoded); 1147 bits = new byte[2]; 1148 bits[0] = BIT_0 | BIT_1 | BIT_2; 1149 l_encoded = new String((char[]) instance.encode((Object) bits)); 1150 assertEquals("0000000000000111", l_encoded); 1151 bits = new byte[2]; 1152 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 1153 l_encoded = new String((char[]) instance.encode((Object) bits)); 1154 assertEquals("0000000000001111", l_encoded); 1155 bits = new byte[2]; 1156 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 1157 l_encoded = new String((char[]) instance.encode((Object) bits)); 1158 assertEquals("0000000000011111", l_encoded); 1159 bits = new byte[2]; 1160 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 1161 l_encoded = new String((char[]) instance.encode((Object) bits)); 1162 assertEquals("0000000000111111", l_encoded); 1163 bits = new byte[2]; 1164 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 1165 l_encoded = new String((char[]) instance.encode((Object) bits)); 1166 assertEquals("0000000001111111", l_encoded); 1167 bits = new byte[2]; 1168 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1169 l_encoded = new String((char[]) instance.encode((Object) bits)); 1170 assertEquals("0000000011111111", l_encoded); 1171 // work on the other byte now 1172 bits = new byte[2]; 1173 bits[1] = BIT_0; 1174 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1175 l_encoded = new String((char[]) instance.encode((Object) bits)); 1176 assertEquals("0000000111111111", l_encoded); 1177 bits = new byte[2]; 1178 bits[1] = BIT_0 | BIT_1; 1179 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1180 l_encoded = new String((char[]) instance.encode((Object) bits)); 1181 assertEquals("0000001111111111", l_encoded); 1182 bits = new byte[2]; 1183 bits[1] = BIT_0 | BIT_1 | BIT_2; 1184 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1185 l_encoded = new String((char[]) instance.encode((Object) bits)); 1186 assertEquals("0000011111111111", l_encoded); 1187 bits = new byte[2]; 1188 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3; 1189 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1190 l_encoded = new String((char[]) instance.encode((Object) bits)); 1191 assertEquals("0000111111111111", l_encoded); 1192 bits = new byte[2]; 1193 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; 1194 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1195 l_encoded = new String((char[]) instance.encode((Object) bits)); 1196 assertEquals("0001111111111111", l_encoded); 1197 bits = new byte[2]; 1198 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; 1199 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1200 l_encoded = new String((char[]) instance.encode((Object) bits)); 1201 assertEquals("0011111111111111", l_encoded); 1202 bits = new byte[2]; 1203 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; 1204 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1205 l_encoded = new String((char[]) instance.encode((Object) bits)); 1206 assertEquals("0111111111111111", l_encoded); 1207 bits = new byte[2]; 1208 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1209 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); 1210 l_encoded = new String((char[]) instance.encode((Object) bits)); 1211 assertEquals("1111111111111111", l_encoded); 1212 } 1213 }