Clover coverage report - baseCode - 0.2.5 Coverage timestamp: Tue Apr 12 2005 11:31:58 EDT FRAMES NO FRAMES |
file stats: | LOC: | 234 |  | Methods: | 12
| NCLOC: | 114 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
RegressionTesting.java | 37.5% | 38% | 41.7% | 38.2% |

1 |  |
     package baseCode.util;
2 |  |
3 |  |
     import java.io.BufferedReader;
4 |  |
     import java.io.BufferedWriter;
5 |  |
     import java.io.File;
6 |  |
     import java.io.FileInputStream;
7 |  |
     import java.io.FileOutputStream;
8 |  |
     import java.io.IOException;
9 |  |
     import java.io.InputStream;
10 |  |
     import java.io.InputStreamReader;
11 |  |
     import java.io.OutputStreamWriter;
12 |  |
     import java.io.StringReader;
13 |  |
     import java.util.Collection;
14 |  |
     import java.util.Vector;
15 |  |
16 |  |
     import baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix;
17 |  |
     import cern.colt.list.DoubleArrayList;
18 |  |
     import cern.colt.matrix.DoubleMatrix2D;
19 |  |
20 |  |
     /**
21 |  |
      * Tools to help make regression testing easier.
22 |  |
      * <hr>
23 |  |
      * <p>
24 |  |
      * Copyright (c) 2004 Columbia University
25 |  |
      *
26 |  |
      * @author pavlidis
27 |  |
      * @version $Id: RegressionTesting.java,v 1.17 2005/02/23 23:08:53 pavlidis Exp $
28 |  |
      */
29 |  |
     public class RegressionTesting {
30 |  |
31 | 0 |
        private RegressionTesting() { /* block instantiation */
32 |  |
        }
33 |  |
34 |  |
        //  private String resourcePath = "";
35 |  |
36 | 0 |
        public static void writeTestResult( String result, String fileName ) throws IOException {
37 |  |
38 | 0 |
           BufferedWriter buf = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( new File( fileName ) ) ) );
39 | 0 |
           BufferedReader resultBuf = new BufferedReader( new StringReader( result ) );
40 |  |
41 | 0 |
           String line = null;
42 | 0 |
           while ( ( line = resultBuf.readLine() ) != null ) {
43 | 0 |
              buf.write( line + "\n" );
44 |  |
           }
45 | 0 |
           buf.close();
46 | 0 |
           resultBuf.close();
47 |  |
        }
48 |  |
49 |  |
        /**
50 |  |
         * @param istream
51 |  |
         * @return
52 |  |
         * @throws IOException
53 |  |
         */
54 | 7 |
        public static String readTestResult( InputStream istream ) throws IOException {
55 | 7 |
           if ( istream == null ) {
56 | 0 |
              throw new IllegalStateException( "Null stream" );
57 |  |
           }
58 |  |
59 | 7 |
           BufferedReader buf = new BufferedReader( new InputStreamReader( istream ) );
60 | 7 |
           String line = "";
61 | 7 |
           StringBuffer testOutput = new StringBuffer( line );
62 | ? |
           while ( ( line = buf.readLine() ) != null ) {
63 | 1852 |
              testOutput.append( line + "\n" );
64 |  |
           }
65 | 7 |
           buf.close();
66 | 7 |
           return testOutput.toString();
67 |  |
        }
68 |  |
69 |  |
        /**
70 |  |
         * @param resourceName
71 |  |
         * @return the contents of the resource as a String
72 |  |
         * @throws IOException
73 |  |
         */
74 | 2 |
        public static String readTestResult( String resourceName ) throws IOException {
75 | 2 |
           InputStream istream = RegressionTesting.class.getResourceAsStream( resourceName );
76 |  |
77 | 0 |
           if ( istream == null ) return null;
78 |  |
79 | 2 |
           String result = readTestResult( istream );
80 | 2 |
           istream.close();
81 | 2 |
           return result;
82 |  |
83 |  |
        }
84 |  |
85 |  |
        /**
86 |  |
         * @throws IOException
87 |  |
         * @param fileName - the full path of the file to be read.
88 |  |
         * @return
89 |  |
         */
90 | 0 |
        public static String readTestResultFromFile( String fileName ) throws IOException {
91 | 0 |
           InputStream is = new FileInputStream( fileName );
92 | 0 |
           return readTestResult( is );
93 |  |
        }
94 |  |
95 |  |
        /**
96 |  |
         * Test whether two DoubleArrayLists are 'close enough' to call equal.
97 |  |
         *
98 |  |
         * @param a
99 |  |
         * @param b
100 |  |
         * @param tolerance
101 |  |
         * @return
102 |  |
         */
103 | 11 |
        public static boolean closeEnough( DoubleArrayList a, DoubleArrayList b, double tolerance ) {
104 | 0 |
           if ( a.size() != b.size() ) return false;
105 |  |
106 | 11 |
           for ( int i = 0; i < a.size(); i++ ) {
107 | 0 |
              if ( Math.abs( a.getQuick( i ) - b.getQuick( i ) ) > tolerance ) return false;
108 |  |
           }
109 | 11 |
           return true;
110 |  |
        }
111 |  |
112 |  |
        /**
113 |  |
         * Test whether two AbstractNamedDoubleMatrix are 'close enough' to call equal.
114 |  |
         *
115 |  |
         * @param a
116 |  |
         * @param b
117 |  |
         * @param tolerance
118 |  |
         * @return try if all the values in both matrices are within 'tolerance' of each other.
119 |  |
         */
120 | 1 |
        public static boolean closeEnough( AbstractNamedDoubleMatrix a, AbstractNamedDoubleMatrix b, double tolerance ) {
121 | 0 |
           if ( a.rows() != b.rows() || a.columns() != b.columns() ) return false;
122 |  |
123 | 1 |
           for ( int i = 0; i < a.rows(); i++ ) {
124 | 30 |
              for ( int j = 0; j < a.columns(); j++ ) {
125 | 0 |
                 if ( Math.abs( a.getQuick( i, j ) - b.getQuick( i, j ) ) > tolerance ) return false;
126 |  |
              }
127 |  |
           }
128 | 1 |
           return true;
129 |  |
        }
130 |  |
131 | 1 |
        public static boolean closeEnough( DoubleMatrix2D a, DoubleMatrix2D b, double tolerance ) {
132 | 0 |
           if ( a.rows() != b.rows() || a.columns() != b.columns() ) return false;
133 |  |
134 | 1 |
           for ( int i = 0; i < a.rows(); i++ ) {
135 | 2 |
              for ( int j = 0; j < a.columns(); j++ ) {
136 | 0 |
                 if ( Math.abs( a.getQuick( i, j ) - b.getQuick( i, j ) ) > tolerance ) return false;
137 |  |
              }
138 |  |
           }
139 | 1 |
           return true;
140 |  |
        }
141 |  |
142 |  |
        /**
143 |  |
         * Test whether two object arrays are the same.
144 |  |
         *
145 |  |
         * @param a
146 |  |
         * @param b
147 |  |
         * @return
148 |  |
         */
149 | 0 |
        public static boolean closeEnough( Object[] a, Object[] b ) {
150 | 0 |
           if ( a.length != b.length ) {
151 | 0 |
              return false;
152 |  |
           }
153 |  |
154 | 0 |
           for ( int i = 0; i < a.length; i++ ) {
155 | 0 |
              if ( !a[i].equals( b[i] ) ) {
156 | 0 |
                 return false;
157 |  |
              }
158 |  |
           }
159 | 0 |
           return true;
160 |  |
        }
161 |  |
162 |  |
        /**
163 |  |
         * Test whether two collections contain the same items.
164 |  |
         *
165 |  |
         * @param a
166 |  |
         * @param b
167 |  |
         * @return
168 |  |
         */
169 | 0 |
        public static boolean containsSame( Collection a, Collection b ) {
170 | 0 |
           if ( a.size() != b.size() ) return false;
171 |  |
172 | 0 |
           if ( !a.containsAll( b ) ) return false;
173 |  |
174 | 0 |
           return true;
175 |  |
        }
176 |  |
177 |  |
        /**
178 |  |
         * Test whether two object arrays contain the same items. The arrays are treated as Sets - repeats are not
179 |  |
         * considered.
180 |  |
         *
181 |  |
         * @param a
182 |  |
         * @param b
183 |  |
         * @return
184 |  |
         */
185 | 0 |
        public static boolean containsSame( Object[] a, Object[] b ) {
186 | 0 |
           if ( a.length != b.length ) return false;
187 |  |
188 | 0 |
           Vector av = new Vector( a.length );
189 | 0 |
           Vector bv = new Vector( b.length );
190 |  |
191 | 0 |
           for ( int i = 0; i < b.length; i++ ) {
192 | 0 |
              av.add( a[i] );
193 | 0 |
              bv.add( b[i] );
194 |  |
           }
195 |  |
196 | 0 |
           return av.containsAll( bv );
197 |  |
198 |  |
        }
199 |  |
200 |  |
        /**
201 |  |
         * Test whether two double arrays contain the same items in any order (tolerance is ZERO)
202 |  |
         *
203 |  |
         * @param a
204 |  |
         * @param b
205 |  |
         * @return
206 |  |
         */
207 | 0 |
        public static boolean containsSame( double[] a, double[] b ) {
208 | 0 |
           if ( a.length != b.length ) return false;
209 |  |
210 | 0 |
           Vector av = new Vector( a.length );
211 | 0 |
           Vector bv = new Vector( b.length );
212 | 0 |
           for ( int i = 0; i < b.length; i++ ) {
213 | 0 |
              av.add( new Double( a[i] ) );
214 | 0 |
              bv.add( new Double( b[i] ) );
215 |  |
           }
216 |  |
217 | 0 |
           return av.containsAll( bv );
218 |  |
        }
219 |  |
220 |  |
        /**
221 |  |
         * Convenience for using Stuart D. Gathman's Diff.
222 |  |
         *
223 |  |
         * @param expected String
224 |  |
         * @param actual String
225 |  |
         * @return String edit list
226 |  |
         */
227 |  |
        /**
228 |  |
         * public static String regress( String expected, String actual ) { Diff diff = new Diff( new Object[] {expected} ,
229 |  |
         * new Object[] {actual} ); Diff.change script = diff.diff_2( false ); DiffPrint.Base p = new DiffPrint.UnifiedPrint(
230 |  |
         * new Object[] {expected} , new Object[] {actual} ); StringWriter wtr = new StringWriter(); p.setOutput( wtr );
231 |  |
         * p.print_script( script ); return wtr.toString(); }
232 |  |
         */
233 |  |
234 |  |
     }