Help with invoking DRY with generics in test harness

Discussion in 'Java' started by Todd, Mar 17, 2008.

  1. Todd

    Todd Guest

    Hello,

    I am trying to create a test harness for tests of numbers that can be
    used for scalar and array tests. I am trying to also apply the DRY
    (Don't Repeat Yourself) axiom within the test harness. As such, I
    thought that generics would be the answer.

    Here is what I have come up with:
    public class TestHarness <T>
    {
    public <T extends Number> T[] testArray( int i )
    {
    return null;
    }

    public <T extends Number> boolean batchTest( String methodName,
    Accuracy sigFig,
    String[] messages,
    T[][] expResults )
    {
    System.out.println( "\n" + methodName + " test"
    + " ~ " + sigFig + " significant
    figures" );

    int numTested = 0;
    int numPassed = 0;
    int numFailed = 0;
    boolean testsPassed = true;
    for( int i = 0; testsPassed && i < expResults.length; i++ )
    {
    T[] result = testArray( i );

    boolean passed = true;
    double expRes = Double.NaN;
    double res = Double.NaN;
    for( int j = 0; passed && j < expResults.length; j++ )
    {
    expRes = Support.roundToSigFig( expResults[j],
    sigFig );
    res = Support.roundToSigFig( result[j], sigFig );

    passed = (expRes == res);
    }

    numTested++;
    if( passed )
    {
    numPassed++;
    }
    else if( !passed )
    {
    numFailed++;

    System.out.print( (i + 1) + "/" + expResults.length
    + " " + messages + ": failed
    " );
    System.out.print( expRes + " != " + res );
    System.out.flush();
    }

    testsPassed &= passed;

    } // for( int i = 0; testsPassed && i < expResults.length; i+
    + )

    System.out.print( numTested + " tests performed; " );
    System.out.print( numPassed + " tests passed; " );
    System.out.println( numFailed + " tests failed" );

    System.out.println( methodName + ": " +
    (testsPassed ? "passed" :
    "failed") );

    return( testsPassed );

    }


    public <T extends Number> T testScalar( int i )
    {
    return null;
    }

    public <T extends Number> boolean batchTest( String methodName,
    Accuracy sigFig,
    String[] messages,
    T[] expResults )
    {
    System.out.println( "\n" + methodName + " test"
    + " ~ " + sigFig + " significant
    figures" );

    int numTested = 0;
    int numPassed = 0;
    int numFailed = 0;
    boolean testsPassed = true;
    for( int i = 0; testsPassed && i < expResults.length; i++ )
    {
    T result = testScalar( i );

    double expRes = Support.roundToSigFig( expResults,
    sigFig );
    double res = Support.roundToSigFig( result, sigFig );
    boolean passed = (expRes == res);

    numTested++;
    if( passed )
    {
    numPassed++;
    }
    else if( !passed )
    {
    numFailed++;

    System.out.print( (i + 1) + "/" + expResults.length
    + " " + messages + ": failed
    " );
    System.out.print( expRes + " != " + res );
    System.out.flush();
    }

    testsPassed &= passed;

    } // for( int i = 0; testsPassed && i < expResults.length; i+
    + )

    System.out.print( numTested + " tests performed; " );
    System.out.print( numPassed + " tests passed; " );
    System.out.println( numFailed + " tests failed" );

    System.out.println( methodName + ": " +
    (testsPassed ? "passed" :
    "failed") );

    return( testsPassed );

    }
    }


    This does not compile due to the errors:
    TestHarness.java:33: type parameters of <T>T[] cannot be determined;
    no unique maximal instance exists for type variable T with upper
    bounds T,java.lang.Number
    T[] result = testArray( i );
    TestHarness.java:95: type parameters of <T>T cannot be determined; no
    unique maximal instance exists for type variable T with upper bounds
    T,java.lang.Number
    T result = testScalar( i );


    I am still working on my understanding of generics, the limitations
    and what I can expect to do with it. I understand that I can not
    instantiate an object of type T as the compiler doesn't know what T
    is. So, how to I capture objects of type T being returned from
    methods?

    I would appreciate any ideas as to how to eliminate the duplication in
    this class, and especially on how to get it to compile.

    Thanks,
    Todd
     
    Todd, Mar 17, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fabbl

    Test Harness Strategies

    fabbl, Apr 17, 2004, in forum: VHDL
    Replies:
    1
    Views:
    1,056
    Jim Lewis
    Apr 17, 2004
  2. Troy Denkinger

    Test Harness?

    Troy Denkinger, Mar 9, 2006, in forum: Ruby
    Replies:
    10
    Views:
    240
    Troy Denkinger
    Mar 10, 2006
  3. yusuf
    Replies:
    2
    Views:
    140
    yusuf
    Aug 7, 2006
  4. yusuf

    Test::Harness and test values.

    yusuf, Aug 7, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    114
    yusuf
    Aug 9, 2006
  5. Test harness for scripts?

    , May 16, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    111
    Sisyphus
    May 17, 2007
Loading...

Share This Page