JavaLib
 
Tester
 
Overview
Basic Types
User Types
Inexact Values
Sets
Iterable
Traversals
Maps
Random Choice
Range Check
Equivalence
ISame
Exceptions
Constructors
Methods
CheckFail
 
Sources
Downloads
 
TSRJ Home
TSRJ Local

Basic Types

The tester allows comparison of pairs of all primitive types, or pairs of objects that are instances of the same wrapper class. An instance if a primitive type can be compared to an instance of its corresponding wrapper class as well. The tester first converts the values to the corresponding primitive type, then uses the Java == operator to validate the equality.

Sample test cases would be:

boolean testPrimitive(Tester t){
  t.checkExpect(5, new Integer(5), "should succeed");
  t.checkExpect(5, 5, "should succeed");
  t.checkExpect(new Long(5), new Integer(5), "should fail"); 
}

The comparison of inexact numbers (of the types double, float) or their wrapper classes is handled in the tester by checkInexact method. The programmer must provide not only the pairs objects to compare, but also the relative accuracy limit (TOLERANCE) to be used in deteremining the equality of the two objects.

Inexact numbers (double and Double or float and Float) are considered the same if their values val1 and val2 satisfy the formula:

(Math.abs(val1 - val2) / (Math.max (Math.abs(val1), Math.abs(val2)))) 
    < TOLERANCE;

So, we can have two tests that compare the same objects, where one test succeeds and the other fails:

boolean testDouble(Tester t){
  t.checkExpect(0.003, 0.00298, 0.001, "should succeed");
  t.checkExpect(0.003, 0.00298, 0.01, "should fail"); 
}

If the comparison of two objects involves a comparison of inexact numbers, the test case report contains a warning, regardless whether the test succeeds or fails, regardless whether the programmer requested the inexact comparison, or failed to do so.

Please, see the section on Inexact Values for further details, especially what happens if the two values match exactly, and how the comparison with exact zero is handled.

All tests that request exact comparison, but require inexact comparison of any fields will fail.


The classes that represent big numbers BigInteger or BigDecimal can also be compared.

The comparison of String data uses Java equals method invoked by the first String.

The description of the equality testing of instances of the Canvas and the classes that implement IColor interface in the World interactive drawing packages is provided at their respective pages.

If both data values are null, the test succeeds, if one value is null and the other is not, the test fails.


Code sources

Here is the complete source code for this test suite.

You can also download the entire souce code as a zip file.

Complete test results are shown here.


last updated on Mon Apr 4 15:26:25 EDT 2011generated with DrRacket