Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Help with invoking DRY with generics in test harness

Reply
Thread Tools

Help with invoking DRY with generics in test harness

 
 
Todd
Guest
Posts: n/a
 
      03-17-2008
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[i].length; j++ )
{
expRes = Support.roundToSigFig( expResults[i][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[i] + ": 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[i],
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[i] + ": 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Test::Harness and test values. yusuf Perl Misc 4 08-09-2006 07:00 PM
How to integrate other scripts with Test::Harness? yusuf Perl Misc 2 08-07-2006 07:54 PM
Test Harness? Troy Denkinger Ruby 10 03-10-2006 05:34 AM
Test Harness Strategies fabbl VHDL 1 04-17-2004 03:39 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments