View Javadoc

    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     private RegressionTesting() { /* block instantiation */
    32     }
    33
    34     //  private String resourcePath = "";
    35
    36     public static void writeTestResult( String result, String fileName ) throws IOException {
    37
    38        BufferedWriter buf = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( new File( fileName ) ) ) );
    39        BufferedReader resultBuf = new BufferedReader( new StringReader( result ) );
    40
    41        String line = null;
    42        while ( ( line = resultBuf.readLine() ) != null ) {
    43           buf.write( line + "\n" );
    44        }
    45        buf.close();
    46        resultBuf.close();
    47     }
    48
    49     /***
    50      * @param istream
    51      * @return
    52      * @throws IOException
    53      */
    54     public static String readTestResult( InputStream istream ) throws IOException {
    55        if ( istream == null ) {
    56           throw new IllegalStateException( "Null stream" );
    57        }
    58
    59        BufferedReader buf = new BufferedReader( new InputStreamReader( istream ) );
    60        String line = "";
    61        StringBuffer testOutput = new StringBuffer( line );
    62        while ( ( line = buf.readLine() ) != null ) {
    63           testOutput.append( line + "\n" );
    64        }
    65        buf.close();
    66        return testOutput.toString();
    67     }
    68
    69     /***
    70      * @param resourceName
    71      * @return the contents of the resource as a String
    72      * @throws IOException
    73      */
    74     public static String readTestResult( String resourceName ) throws IOException {
    75        InputStream istream = RegressionTesting.class.getResourceAsStream( resourceName );
    76
    77        if ( istream == null ) return null;
    78
    79        String result = readTestResult( istream );
    80        istream.close();
    81        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     public static String readTestResultFromFile( String fileName ) throws IOException {
    91        InputStream is = new FileInputStream( fileName );
    92        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    public static boolean closeEnough( DoubleArrayList a, DoubleArrayList b, double tolerance ) {
    104       if ( a.size() != b.size() ) return false;
    105
    106       for ( int i = 0; i < a.size(); i++ ) {
    107          if ( Math.abs( a.getQuick( i ) - b.getQuick( i ) ) > tolerance ) return false;
    108       }
    109       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    public static boolean closeEnough( AbstractNamedDoubleMatrix a, AbstractNamedDoubleMatrix b, double tolerance ) {
    121       if ( a.rows() != b.rows() || a.columns() != b.columns() ) return false;
    122
    123       for ( int i = 0; i < a.rows(); i++ ) {
    124          for ( int j = 0; j < a.columns(); j++ ) {
    125             if ( Math.abs( a.getQuick( i, j ) - b.getQuick( i, j ) ) > tolerance ) return false;
    126          }
    127       }
    128       return true;
    129    }
    130
    131    public static boolean closeEnough( DoubleMatrix2D a, DoubleMatrix2D b, double tolerance ) {
    132       if ( a.rows() != b.rows() || a.columns() != b.columns() ) return false;
    133
    134       for ( int i = 0; i < a.rows(); i++ ) {
    135          for ( int j = 0; j < a.columns(); j++ ) {
    136             if ( Math.abs( a.getQuick( i, j ) - b.getQuick( i, j ) ) > tolerance ) return false;
    137          }
    138       }
    139       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    public static boolean closeEnough( Object[] a, Object[] b ) {
    150       if ( a.length != b.length ) {
    151          return false;
    152       }
    153
    154       for ( int i = 0; i < a.length; i++ ) {
    155          if ( !a[i].equals( b[i] ) ) {
    156             return false;
    157          }
    158       }
    159       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    public static boolean containsSame( Collection a, Collection b ) {
    170       if ( a.size() != b.size() ) return false;
    171
    172       if ( !a.containsAll( b ) ) return false;
    173
    174       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    public static boolean containsSame( Object[] a, Object[] b ) {
    186       if ( a.length != b.length ) return false;
    187
    188       Vector av = new Vector( a.length );
    189       Vector bv = new Vector( b.length );
    190
    191       for ( int i = 0; i < b.length; i++ ) {
    192          av.add( a[i] );
    193          bv.add( b[i] );
    194       }
    195
    196       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    public static boolean containsSame( double[] a, double[] b ) {
    208       if ( a.length != b.length ) return false;
    209
    210       Vector av = new Vector( a.length );
    211       Vector bv = new Vector( b.length );
    212       for ( int i = 0; i < b.length; i++ ) {
    213          av.add( new Double( a[i] ) );
    214          bv.add( new Double( b[i] ) );
    215       }
    216
    217       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 }