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

User Defined Types

The equality of any two instances of user defined classes is determined by comparing the pairs of values for each field defined for that class. If the field value is an instance of another class, the comparison recursively continues the comparison of the two values. Circularity in data definitions is detected.

The comparison includes all public, protected, and private fields, both those declared in this class, and those declared in the super classes. The static, volatile, and transient fields are not included in the comparison.

If the objects that are being compared contain a field of the type double or float (or their wrapper classes), the checkInexact variant of the test method must be used (see Inexact Values).

Here is a simple example of a class that refers to another class and the tests that check the equality of their instances:

class Book{
   String title;
  Author author;
 
  Book(String author, Author author){
    this.title = title;  this.author = author;
}

class Author{
  String name;
 
  Author(String name){
    this.name = name;
  }
}

class Examples{
  Author js = new Author("John Steinbeck");
  Book tp = new Book("The Pearl", js);

  boolean testBooks(Tester t){
    return t.checkExpect(tp, new Book("The Pearl", js), "succeeds");
    return t.checkExpect(tp, new Book("The Pearl", 
        new Author("John Steinbeck"), "succeeds");
    return t.checkExpect(tp, new Book("The Girl", js), "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