Tests that check whether the two given objects are equivalent according to the user-defined function object that implements the Equivalence interface.
The Equivalence interface is defined as:
public interface Equivalence {
public boolean equivalent(T t1, T t2);
}
It is assumed, but not required, that the implementation of the Equivalence interface represents a true equivalence relation, i.e. is reflexive, symmetric and transitive.
The class ExamplesEquivalence contains all test cases.
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.
Here is an example that illustrates the use of the checkEquivalent tests.
public class EquivBooks implements Equivalence {
public boolean equivalent(Book b1, Book b2) {
return b1.author.name.equals(b2.author.name);
}
}
public class ExamplesEquivalence {
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);
public Book book1 = new Book("title1", author1, 1990);
public Book book2 = new Book("title2", author2, 1990);
public Book book3 = new Book("title3", author3, 2000);
public Book book4 = new Book("title1", author4, 1990);
Equivalence equivbooks = new EquivBooks();
void testEquivalence(Tester t) {
t.checkEquivalent(book1, book3, equivbooks, "Success: same author names");
t.checkEquivalent(book1, book3, equivbooks); // no testname
t.checkEquivalent(book1, book4, equivbooks, "Should fail: different authors");
t.checkEquivalent(book1, book2, equivbooks, "Should fail: different books, authors");
}
}