Tests that compare two instances of a class that implements the ISame interface.
The programmer may decide that for one or more classes in his program the equality comparison should be handled in a special way (e.g. only some of the fields have to match, or thatĀ Strings should be compared in a case-insensitive manner). In this case these classes should implement the ISame interface by defining the method same that compares this object with the given one. Any two instances of a class that implements the ISame interface will be compared by applying the same method defined in that class. The remainder of the test evaluation follows the regular rules.
The class ExamplesISame 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 tests that apply user-defined same method.
For the class that represents an Author we define two authors to be same if their last names are the same:
public class Author implements ISame {
public String firstName;
public String lastName;
public Author(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public boolean same(Author that) {
return this.lastName.equals(that.lastName);
}
}
public class ExamplesISame {
public Author sk=new Author("Steven", "King");
public Author dk=new Author("Dan", "King");
public Author db=new Author("Dan", "Brown");
public Book book1=new Book("title1",sk,2000);
public Book book2=new Book("title2",db,2000);
public Book book3=new Book("title1",db,2000);
public Book book4=new Book("title1",dk,2000);
public Book book5=new Book("title2",db,2000);
void testISame(Tester t) {
t.checkExpect(this.book1, this.book4, "Success: same author last names");
t.checkExpect(this.book2, this.book5, "Success: same author first names and last names");
t.checkFail(this.book1, this.book2, "Test to fail: different books, authors");
t.checkFail(this.book3, this.book4, "Test to fail: different author last names");
t.checkFail(this.book2, this.book3, "Test to fail: different titles");
}
}