/*
 * Created on May 8, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.codehaus.activemq.util;
import junit.framework.TestCase;
import java.io.*;
/**
 * BitArrayTest
 */
public class BitArrayTest extends TestCase {
    public static void main(String[] args) {
        junit.textui.TestRunner.run(BitArrayTest.class);
    }
    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
    }
    /*
     * @see TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        super.tearDown();
    }
    /**
     * Constructor for BitArrayTest.
     * 
     * @param arg0
     */
    public BitArrayTest(String arg0) {
        super(arg0);
    }
    public void testLength() {
        BitArray ba = new BitArray();
        assertTrue(ba.length() == 0);
        for (int i = 0;i < 64;i++) {
            ba.set(i, false);
            assertTrue(ba.length() == (i + 1));
        }
    }
    public void testSet() {
        BitArray ba = new BitArray();
        assertTrue(ba.length() == 0);
        for (int i = 0;i < 64;i++) {
            if (i % 2 == 0) {
                ba.set(i, true);
                assertTrue(ba.get(i));
            }
            else {
                ba.set(i, false);
                assertTrue(!ba.get(i));
            }
        }
    }
    public void testWriteToStream() {
        try {
            for (int i =0; i < 64; i++){
                testWrite(i);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    
     void testWrite(int size) throws IOException {
        BitArray ba = new BitArray();
        for (int i = 0;i < size;i++) {
            if (i%2 == 0){
            ba.set(i, true);
            }else {
                ba.set(i,false);
            }
        }
        byte[] data = getData(ba);
        ba = new BitArray();
        setData(ba, data);
        assertTrue(ba.length() == size);
        for (int i = 0;i < size;i++) {
            if (i%2==0){
            assertTrue(ba.get(i));
            }else {
                assertTrue(!ba.get(i));
            }
        }
    }
    
    public void testTime() throws IOException {
        
        int count = 100000;
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < count; i++){
            BitArray ba = new BitArray();
            for (int j = 0;j < 64;j++) {
                if (j%2 == 0){
                ba.set(j, true);
                }else {
                    ba.set(j,false);
                }
            }
            byte[] data = getData(ba);
            ba = new BitArray();
            setData(ba, data);
        }
      
        long finishTime = System.currentTimeMillis();
        long totalTime = finishTime-startTime;
        long ps = (count * 1000)/totalTime;
        System.out.println("Time taken :" + totalTime + " for " + count + "iterations, = " + ps +" per sec.");
       
        
    }
    
    
    byte[] getData(BitArray ba) throws IOException {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(bytesOut);
        ba.writeToStream(dataOut);
        dataOut.flush();
        return bytesOut.toByteArray();
    }
    void setData(BitArray ba, byte[] data) throws IOException {
        ByteArrayInputStream bytesIn = new ByteArrayInputStream(data);
        DataInputStream dataIn = new DataInputStream(bytesIn);
        ba.readFromStream(dataIn);
    }
}
