T
Todd
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
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