1   /*
2    * Created on May 8, 2004
3    *
4    * To change the template for this generated file go to
5    * Window - Preferences - Java - Code Generation - Code and Comments
6    */
7   package org.codehaus.activemq.util;
8   import junit.framework.TestCase;
9   import java.io.*;
10  /***
11   * BitArrayTest
12   */
13  public class BitArrayTest extends TestCase {
14      public static void main(String[] args) {
15          junit.textui.TestRunner.run(BitArrayTest.class);
16      }
17      /*
18       * @see TestCase#setUp()
19       */
20      protected void setUp() throws Exception {
21          super.setUp();
22      }
23      /*
24       * @see TestCase#tearDown()
25       */
26      protected void tearDown() throws Exception {
27          super.tearDown();
28      }
29      /***
30       * Constructor for BitArrayTest.
31       * 
32       * @param arg0
33       */
34      public BitArrayTest(String arg0) {
35          super(arg0);
36      }
37      public void testLength() {
38          BitArray ba = new BitArray();
39          assertTrue(ba.length() == 0);
40          for (int i = 0;i < 64;i++) {
41              ba.set(i, false);
42              assertTrue(ba.length() == (i + 1));
43          }
44      }
45      public void testSet() {
46          BitArray ba = new BitArray();
47          assertTrue(ba.length() == 0);
48          for (int i = 0;i < 64;i++) {
49              if (i % 2 == 0) {
50                  ba.set(i, true);
51                  assertTrue(ba.get(i));
52              }
53              else {
54                  ba.set(i, false);
55                  assertTrue(!ba.get(i));
56              }
57          }
58      }
59      public void testWriteToStream() {
60          try {
61              for (int i =0; i < 64; i++){
62                  testWrite(i);
63              }
64          }
65          catch (IOException e) {
66              e.printStackTrace();
67          }
68      }
69      
70       void testWrite(int size) throws IOException {
71          BitArray ba = new BitArray();
72          for (int i = 0;i < size;i++) {
73              if (i%2 == 0){
74              ba.set(i, true);
75              }else {
76                  ba.set(i,false);
77              }
78          }
79          byte[] data = getData(ba);
80          ba = new BitArray();
81          setData(ba, data);
82          assertTrue(ba.length() == size);
83          for (int i = 0;i < size;i++) {
84              if (i%2==0){
85              assertTrue(ba.get(i));
86              }else {
87                  assertTrue(!ba.get(i));
88              }
89          }
90      }
91      
92      public void testTime() throws IOException {
93          
94          int count = 100000;
95          long startTime = System.currentTimeMillis();
96          for (int i = 0; i < count; i++){
97              BitArray ba = new BitArray();
98              for (int j = 0;j < 64;j++) {
99                  if (j%2 == 0){
100                 ba.set(j, true);
101                 }else {
102                     ba.set(j,false);
103                 }
104             }
105             byte[] data = getData(ba);
106             ba = new BitArray();
107             setData(ba, data);
108         }
109       
110         long finishTime = System.currentTimeMillis();
111         long totalTime = finishTime-startTime;
112         long ps = (count * 1000)/totalTime;
113         System.out.println("Time taken :" + totalTime + " for " + count + "iterations, = " + ps +" per sec.");
114        
115         
116     }
117     
118     
119     byte[] getData(BitArray ba) throws IOException {
120         ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
121         DataOutputStream dataOut = new DataOutputStream(bytesOut);
122         ba.writeToStream(dataOut);
123         dataOut.flush();
124         return bytesOut.toByteArray();
125     }
126     void setData(BitArray ba, byte[] data) throws IOException {
127         ByteArrayInputStream bytesIn = new ByteArrayInputStream(data);
128         DataInputStream dataIn = new DataInputStream(bytesIn);
129         ba.readFromStream(dataIn);
130     }
131 }