BitwiseStreamsTest.java revision e564b19ed2f6b8b5667648254bc6c6859bb536e7
1/* 2 * Copyright (C) 2006 The Android Open Source Project 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 17package com.android.unit_tests; 18 19import com.android.internal.util.BitwiseInputStream; 20import com.android.internal.util.BitwiseOutputStream; 21import com.android.internal.util.HexDump; 22 23import android.test.AndroidTestCase; 24import android.test.suitebuilder.annotation.SmallTest; 25 26import android.util.Log; 27 28public class BitwiseStreamsTest extends AndroidTestCase { 29 private final static String LOG_TAG = "BitwiseStreamsTest"; 30 31 @SmallTest 32 public void testOne() throws Exception { 33 int offset = 3; 34 byte[] inBuf = HexDump.hexStringToByteArray("FFDD"); 35 BitwiseOutputStream outStream = new BitwiseOutputStream(30); 36 outStream.skip(offset); 37 for (int i = 0; i < inBuf.length; i++) outStream.write(8, inBuf[i]); 38 byte[] outBuf = outStream.toByteArray(); 39 BitwiseInputStream inStream = new BitwiseInputStream(outBuf); 40 byte[] inBufDup = new byte[inBuf.length]; 41 inStream.skip(offset); 42 for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = inStream.read(8); 43 assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup)); 44 } 45 46 @SmallTest 47 public void testTwo() throws Exception { 48 int offset = 3; 49 byte[] inBuf = HexDump.hexStringToByteArray("11d4f29c0e9ad3c36e72584e064d9b53"); 50 BitwiseOutputStream outStream = new BitwiseOutputStream(30); 51 outStream.skip(offset); 52 for (int i = 0; i < inBuf.length; i++) outStream.write(8, inBuf[i]); 53 BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray()); 54 inStream.skip(offset); 55 byte[] inBufDup = new byte[inBuf.length]; 56 for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = inStream.read(8); 57 assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup)); 58 } 59 60 @SmallTest 61 public void testThree() throws Exception { 62 int offset = 4; 63 byte[] inBuf = HexDump.hexStringToByteArray("00031040900112488ea794e0"); 64 BitwiseOutputStream outStream = new BitwiseOutputStream(30); 65 outStream.skip(offset); 66 for (int i = 0; i < inBuf.length; i++) outStream.write(8, inBuf[i]); 67 BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray()); 68 inStream.skip(offset); 69 byte[] inBufDup = new byte[inBuf.length]; 70 for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = inStream.read(8); 71 assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup)); 72 } 73 74 @SmallTest 75 public void testFour() throws Exception { 76 int offset = 7; 77 byte[] inBuf = HexDump.hexStringToByteArray("00031040900112488ea794e0"); 78 BitwiseOutputStream outStream = new BitwiseOutputStream(30); 79 outStream.skip(offset); 80 for (int i = 0; i < inBuf.length; i++) { 81 outStream.write(5, inBuf[i] >>> 3); 82 outStream.write(3, inBuf[i] & 0x07); 83 } 84 BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray()); 85 inStream.skip(offset); 86 byte[] inBufDup = new byte[inBuf.length]; 87 for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = inStream.read(8); 88 assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup)); 89 } 90 91 @SmallTest 92 public void testFive() throws Exception { 93 int num_runs = 10; 94 long start = android.os.SystemClock.elapsedRealtime(); 95 for (int run = 0; run < num_runs; run++) { 96 int offset = run % 8; 97 byte[] inBuf = HexDump.hexStringToByteArray("00031040900112488ea794e0"); 98 BitwiseOutputStream outStream = new BitwiseOutputStream(30); 99 outStream.skip(offset); 100 for (int i = 0; i < inBuf.length; i++) { 101 outStream.write(5, inBuf[i] >>> 3); 102 outStream.write(3, inBuf[i] & 0x07); 103 } 104 BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray()); 105 inStream.skip(offset); 106 byte[] inBufDup = new byte[inBuf.length]; 107 for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = inStream.read(8); 108 assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup)); 109 } 110 long end = android.os.SystemClock.elapsedRealtime(); 111 Log.d(LOG_TAG, "repeated encode-decode took " + (end - start) + " ms"); 112 } 113} 114