| 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 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 sourcesHere 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 2011 | generated with DrRacket |