Home | History | Annotate | Download | only in util
      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 
     17 package com.android.internal.util;
     18 
     19 import com.android.internal.util.BitwiseInputStream;
     20 import com.android.internal.util.BitwiseOutputStream;
     21 import com.android.internal.util.HexDump;
     22 
     23 import android.test.AndroidTestCase;
     24 import android.test.suitebuilder.annotation.SmallTest;
     25 
     26 import android.util.Log;
     27 
     28 import java.util.Random;
     29 
     30 public class BitwiseStreamsTest extends AndroidTestCase {
     31     private final static String LOG_TAG = "BitwiseStreamsTest";
     32 
     33     @SmallTest
     34     public void testOne() throws Exception {
     35         int offset = 3;
     36         byte[] inBuf = HexDump.hexStringToByteArray("FFDD");
     37         BitwiseOutputStream outStream = new BitwiseOutputStream(30);
     38         outStream.skip(offset);
     39         for (int i = 0; i < inBuf.length; i++) outStream.write(8, inBuf[i]);
     40         byte[] outBuf = outStream.toByteArray();
     41         BitwiseInputStream inStream = new BitwiseInputStream(outBuf);
     42         byte[] inBufDup = new byte[inBuf.length];
     43         inStream.skip(offset);
     44         for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = (byte)inStream.read(8);
     45         assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup));
     46     }
     47 
     48     @SmallTest
     49     public void testTwo() throws Exception {
     50         int offset = 3;
     51         byte[] inBuf = HexDump.hexStringToByteArray("11d4f29c0e9ad3c36e72584e064d9b53");
     52         BitwiseOutputStream outStream = new BitwiseOutputStream(30);
     53         outStream.skip(offset);
     54         for (int i = 0; i < inBuf.length; i++) outStream.write(8, inBuf[i]);
     55         BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray());
     56         inStream.skip(offset);
     57         byte[] inBufDup = new byte[inBuf.length];
     58         for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = (byte)inStream.read(8);
     59         assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup));
     60     }
     61 
     62     @SmallTest
     63     public void testThree() throws Exception {
     64         int offset = 4;
     65         byte[] inBuf = HexDump.hexStringToByteArray("00031040900112488ea794e0");
     66         BitwiseOutputStream outStream = new BitwiseOutputStream(30);
     67         outStream.skip(offset);
     68         for (int i = 0; i < inBuf.length; i++) outStream.write(8, inBuf[i]);
     69         BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray());
     70         inStream.skip(offset);
     71         byte[] inBufDup = new byte[inBuf.length];
     72         for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = (byte)inStream.read(8);
     73         assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup));
     74     }
     75 
     76     @SmallTest
     77     public void testFour() throws Exception {
     78         int offset = 7;
     79         byte[] inBuf = HexDump.hexStringToByteArray("00031040900112488ea794e0");
     80         BitwiseOutputStream outStream = new BitwiseOutputStream(30);
     81         outStream.skip(offset);
     82         for (int i = 0; i < inBuf.length; i++) {
     83             outStream.write(5, inBuf[i] >>> 3);
     84             outStream.write(3, inBuf[i] & 0x07);
     85         }
     86         BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray());
     87         inStream.skip(offset);
     88         byte[] inBufDup = new byte[inBuf.length];
     89         for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = (byte)inStream.read(8);
     90         assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup));
     91     }
     92 
     93     @SmallTest
     94     public void testFive() throws Exception {
     95         Random random = new Random();
     96         int iterations = 10000;
     97         int[] sizeArr = new int[iterations];
     98         int[] valueArr = new int[iterations];
     99         BitwiseOutputStream outStream = new BitwiseOutputStream(iterations * 4);
    100         for (int i = 0; i < iterations; i++) {
    101             int x = random.nextInt();
    102             int size = (x & 0x07) + 1;
    103             int value = x & (-1 >>> (32 - size));
    104             sizeArr[i] = size;
    105             valueArr[i] = value;
    106             outStream.write(size, value);
    107         }
    108         BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray());
    109         for (int i = 0; i < iterations; i++) {
    110             assertEquals(valueArr[i], inStream.read(sizeArr[i]));
    111         }
    112     }
    113 
    114     @SmallTest
    115     public void testSix() throws Exception {
    116         int num_runs = 10;
    117         long start = android.os.SystemClock.elapsedRealtime();
    118         for (int run = 0; run < num_runs; run++) {
    119             int offset = run % 8;
    120             byte[] inBuf = HexDump.hexStringToByteArray("00031040900112488ea794e0");
    121             BitwiseOutputStream outStream = new BitwiseOutputStream(30);
    122             outStream.skip(offset);
    123             for (int i = 0; i < inBuf.length; i++) {
    124                 outStream.write(5, inBuf[i] >>> 3);
    125                 outStream.write(3, inBuf[i] & 0x07);
    126             }
    127             BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray());
    128             inStream.skip(offset);
    129             byte[] inBufDup = new byte[inBuf.length];
    130             for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = (byte)inStream.read(8);
    131             assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup));
    132         }
    133         long end = android.os.SystemClock.elapsedRealtime();
    134         Log.d(LOG_TAG, "repeated encode-decode took " + (end - start) + " ms");
    135     }
    136 
    137     @SmallTest
    138     public void testExpandArray() throws Exception {
    139         Random random = new Random();
    140         int iterations = 10000;
    141         int[] sizeArr = new int[iterations];
    142         int[] valueArr = new int[iterations];
    143         BitwiseOutputStream outStream = new BitwiseOutputStream(8);
    144         for (int i = 0; i < iterations; i++) {
    145             int x = random.nextInt();
    146             int size = (x & 0x07) + 1;
    147             int value = x & (-1 >>> (32 - size));
    148             sizeArr[i] = size;
    149             valueArr[i] = value;
    150             outStream.write(size, value);
    151         }
    152         BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray());
    153         for (int i = 0; i < iterations; i++) {
    154             assertEquals(valueArr[i], inStream.read(sizeArr[i]));
    155         }
    156     }
    157 }
    158