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
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
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();
76 while ( ( line = br.readLine() ) != null ) {
77 buf.append( line.split( "\t", 2 )[1] + "\t" );
78
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
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
132
133
134
135
136
137
138
139
140
141 /***
142 *
143 *
144 */
145
146
147
148
149
150
151
152
153
154
155 /***
156 *
157 *
158 */
159
160
161
162
163
164
165
166
167
168
169 /***
170 *
171 *
172 */
173
174
175
176
177
178
179
180
181
182
183 /***
184 *
185 *
186 */
187
188
189
190
191
192
193
194
195
196
197
198
199 public void testByteArrayToDoubleConversionSpeed() {
200 byte[] lottaBytes = bac.doubleArrayToBytes( wholeBunchOfDoubles );
201 bac.byteArrayToDoubles( lottaBytes );
202 }
203
204
205 public void testStringToDoubleArrayConversionSpeed() {
206 sc.StringToDoubles( longDoubleString );
207 }
208
209
210 public void testDoubleArrayToByteArrayConversionSpeed() {
211 bac.doubleArrayToBytes( wholeBunchOfDoubles );
212 }
213
214
215 public void testDoubleArrayToDelimitedStringConversionSpeed() {
216 sc.DoubleArrayToString( wholeBunchOfDoubles );
217 }
218
219 }