1   package baseCode.io;
2   
3   import java.io.BufferedReader;
4   import java.io.InputStream;
5   import java.io.InputStreamReader;
6   
7   import junit.framework.TestCase;
8   import baseCode.io.ByteArrayConverter;
9   import baseCode.io.StringConverter;
10  
11  //import javax.sql.rowset.serial;
12  
13  /***
14   * $Id: TestByteArrayConverter.java,v 1.3 2005/04/12 14:09:14 pavlidis Exp $
15   */
16  public class TestByteArrayConverter extends TestCase {
17     ByteArrayConverter bac;
18     StringConverter sc;
19  
20     public double x = 424542.345;
21     public double y = 25425.5652;
22     public double z = 24524523.254;
23  
24     public int a = 424542;
25     public int b = 25425;
26     public int c = 24524523;
27  
28     public char u = 'k';
29     public char v = 'i';
30     public char w = 'r';
31  
32     double[] testD = new double[] {
33           x, y, z
34     };
35  
36     int[] testI = new int[] {
37           a, b, c
38     };
39  
40     char[] testC = new char[] {
41           u, v, w
42     };
43  
44     byte[] expectedBfD = new byte[] {
45           65, 25, -23, 121, 97, 71, -82, 20, 64, -40, -44, 100, 44, 60, -98, -19, 65, 119, 99, 110, -76, 16, 98, 78
46     };
47  
48     byte[] expectedBfI = new byte[] {
49           0, 6, 122, 94, 0, 0, 99, 81, 1, 118
50     };
51  
52     byte[] expectedBfC = new byte[] {
53           0, 107, 0, 105, 0, 114
54     };
55  
56     String longDoubleString = "";
57     double[] wholeBunchOfDoubles;
58  
59     String filename = "C://Documents and Settings//Kiran Keshav//Desktop//Neuro//ross-nci60-data.txt";
60  
61     /*
62      * @see TestCase#setUp()
63      */
64     protected void setUp() throws Exception {
65        super.setUp();
66  
67        bac = new ByteArrayConverter();
68        sc = new StringConverter();
69  
70        InputStream is = TestByteArrayConverter.class.getResourceAsStream( "/data/melanoma_and_sarcomaMAS5.txt" );
71        BufferedReader br = new BufferedReader( new InputStreamReader( is ) );
72  
73        StringBuffer buf = new StringBuffer();
74        String line;
75        br.readLine(); // ditch the first row.
76        while ( ( line = br.readLine() ) != null ) {
77           buf.append( line.split( "\t", 2 )[1] + "\t" ); // so we get a very long delimited string, albeit with a
78                                                          // trailing tab.
79        }
80  
81        longDoubleString = buf.toString();
82  
83        if ( longDoubleString == null ) {
84           throw new IllegalStateException( "Couldn't setup string" );
85        }
86  
87        wholeBunchOfDoubles = sc.StringToDoubles( longDoubleString );
88        br.close();
89        is.close();
90  
91     }
92  
93     /*
94      * @see TestCase#tearDown()
95      */
96     protected void tearDown() throws Exception {
97        super.tearDown();
98        longDoubleString = null;
99        wholeBunchOfDoubles = null;
100       bac = null;
101       sc = null;
102    }
103 
104    /***
105     * 
106     *
107     */
108    public void testDoubleArrayToBytes() {
109       byte[] actualReturn = bac.doubleArrayToBytes( testD );
110       byte[] expectedValue = expectedBfD;
111       for ( int i = 0; i < expectedValue.length; i++ ) {
112          assertEquals( "return value", expectedValue[i], actualReturn[i] );
113       }
114    }
115 
116    /***
117     * 
118     *
119     */
120       public void testByteArrayToDoubles() {
121          double[] actualReturn = bac.byteArrayToDoubles( bac.doubleArrayToBytes(testD) );
122          double[] expectedValue = testD;
123          for (int i=0;i<actualReturn.length;i++){
124            assertEquals( "return value", expectedValue[i], actualReturn[i], 0);
125          }
126       }
127    /***
128     * Second Method to test the ByteArrayToDoubles. This is used for Benchmarking purposes.
129     */
130 
131    //   public void testByteArrayToDoubles() {
132    //// System.err.println(" CONVERTING BYTES TO DOUBLES ");
133    //      double[] actualReturn = f.ByteArrayToDoubles( f.DoubleArrayToBytes(g.StringToDoubles(filename)));
134    //      double[] expectedValue = actualReturn;
135    //      for (int i=0;i<actualReturn.length;i++){
136    //        //System.err.println("actualReturn:["+i+"]"+ actualReturn[i]);//new
137    // Integer(actualReturn[i]).toBinaryString(actualReturn[i]));
138    //        //assertEquals( "return value", expectedValue[i], actualReturn[i], Double.NaN);
139    //      }
140    //   }
141    /***
142     * 
143     *
144     */
145    //   public void testIntArrayToBytes() {
146    //      //System.err.println(" CONVERTING INTS TO BYTES ");
147    //      byte[] actualReturn = f.IntArrayToBytes( testI );
148    //      byte[] expectedValue = expectedBfI;
149    //      for(int i=0;i<expectedValue.length;i++){
150    //        //System.err.println("actualReturn:["+i+"]"+ actualReturn[i]);//new
151    // Integer(actualReturn[i]).toBinaryString(actualReturn[i]));
152    //       assertEquals( "return value",expectedValue[i],actualReturn[i]);
153    //      }
154    //   }
155    /***
156     * 
157     *
158     */
159    //   public void testByteArrayToInts() {
160    //      //System.err.println(" CONVERTING BYTES TO INTS");
161    //      int[] actualReturn = f.ByteArrayToInts( f.IntArrayToBytes(testI) );
162    //      int[] expectedValue = testI;
163    //      for (int i=0;i<expectedValue.length;i++){
164    //// System.err.println("actualReturn:["+i+"]"+ actualReturn[i]);//new
165    // Integer(actualReturn[i]).toBinaryString(actualReturn[i]));
166    //        assertEquals( "return value", expectedValue[i], actualReturn[i], 0);
167    //      }
168    //   }
169    /***
170     * 
171     *
172     */
173    //   public void testCharArrayToBytes() {
174    //      //System.err.println(" CONVERTING CHARS TO BYTES ");
175    //      byte[] actualReturn = f.CharArrayToBytes( testC );
176    //      byte[] expectedValue = expectedBfC;
177    //      for(int i=0;i<expectedValue.length;i++){
178    //        //System.err.println("actualReturn:["+i+"]"+ actualReturn[i]);//new
179    // Integer(actualReturn[i]).toBinaryString(actualReturn[i]));
180    //       assertEquals( "return value",expectedValue[i],actualReturn[i]);
181    //      }
182    //   }
183    /***
184     * 
185     *
186     */
187    //   public void testByteArrayToChars() {
188    //      //System.err.println(" CONVERTING BYTES TO INTS");
189    //      char[] actualReturn = f.ByteArrayToChars( f.CharArrayToBytes(testC) );
190    //      char[] expectedValue = testC;
191    //      for (int i=0;i<expectedValue.length;i++){
192    //         //System.err.println("actualReturn:["+i+"]"+ actualReturn[i]);//new
193    // Integer(actualReturn[i]).toBinaryString(actualReturn[i]));
194    //        assertEquals( "return value", expectedValue[i], actualReturn[i], 0);
195    //      }
196    //   }
197 
198    // test blob -> double[]
199    public void testByteArrayToDoubleConversionSpeed() {
200       byte[] lottaBytes = bac.doubleArrayToBytes( wholeBunchOfDoubles );
201       bac.byteArrayToDoubles( lottaBytes );
202    }
203 
204    // test string -> double[]
205    public void testStringToDoubleArrayConversionSpeed() {
206       sc.StringToDoubles( longDoubleString );
207    }
208 
209    // test double[] -> blob.
210    public void testDoubleArrayToByteArrayConversionSpeed() {
211       bac.doubleArrayToBytes( wholeBunchOfDoubles );
212    }
213 
214    // test double[] -> delimited string.
215    public void testDoubleArrayToDelimitedStringConversionSpeed() {
216       sc.DoubleArrayToString( wholeBunchOfDoubles );
217    }
218 
219 }