| Equivalence | |||||||||||||||||||||||||||
The tester checks whether the two given objects are equivalent according to the
user-defined function object that implements the
The
public interface Equivalence<T>{
public boolean equivalent(T t1, T t2);
}
It is assumed, but not required, that the implementation of the
The following example illustrates the use of the For the class that represents a
public class EquivBooks implements Equivalence<Book>{
// two books are equivalent if the author's names match
public boolean equivalent(Book b1, Book b2){
return b1.author.name.equals(b2.author.name);
}
}
We now run the following tests:
// sample books
public Author author1=new Author("author1",40);
public Author author2=new Author("author2",55);
public Author author3=new Author("author1",66);
public Author author4=new Author("author4",26);
// sample authors
public Book book1=new Book("title1",author1,4000);
public Book book2=new Book("title2",author2,4000);
public Book book3=new Book("title3",author3,3001);
public Book book4=new Book("title1",author4,4000);
// instance of the Equivalence
Equivalence equbooks = new EquivBooks();
void testEquivalence(Tester t){
t.checkEquivalent(this.book1, this.book2, equbooks,
"fails: different books, authors");
t.checkEquivalent(this.book1, this.book3, equbooks,
"should succeed - same author names");
t.checkEquivalent(this.book1, this.book4, equbooks,
"fails: different authors.");
t.checkEquivalent(this.book1, this.book3, equbooks); // no testname
}
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 |