Tests that include comparison of HashSet and TreeSet objects.
The tester has a special way of comparing elements of a collection that implements the java.lang.Set interface. If the classes of the two objects match, the tester compares their sizes, then makes sure that every element in the first set appears in the second set. So, two objects of the type TreeSet or two objects of the type HashSet are compared by matching their size, then making sure every object in one set appears in the other. However, an object of the type TreeSet when compared to an object of the type HashSet will not be compared as sets.
If the user implements the java.lang.Set interface for his own class, or wishes to compare any (other) two objects that are instances of classes that implement the Set interface she can invoke the comparison of the data as Sets, by using the checkSet method. There is no inexact variant of checkSet, because the set elements are again compared using the equals method.
The class ExamplesSets 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 comparing TreeSet and HashSet objects.
public class Song {
protected String title;
private int time;
public Song(String title, int time) {
this.title=title;
this.time=time;
}
}
public class ExamplesSets {
Song song1 = new Song("title1", 4);
Song song1a = new Song("title1", 4);
Song song2 = new Song("title2", 2);
Song song3 = new Song("title3", 2);
HashSet songSet1a = new HashSet(Arrays.asList(this.song1a, this.song2, this.song3));
HashSet songSet1b = new HashSet(Arrays.asList(this.song3, this.song1a, this.song2));
HashSet songSet1c = new HashSet(Arrays.asList(this.song3, this.song1, this.song2));
HashSet songSet2a = new HashSet(Arrays.asList(this.song3));
TreeSet songSet4a = new TreeSet(Arrays.asList(this.song1a));
TreeSet songSet4b = new TreeSet(Arrays.asList(this.song1a));
TreeSet songSet5a = new TreeSet(Arrays.asList(this.song3));
public void testSets(Tester t) {
t.checkSet(songSet1a, songSet1b, "Success: HashSet same lists differently ordered");
t.checkSet(songSet4a, songSet4b, "Success: TreeSet same lists");
t.checkSet(songSet2a, songSet5a, "Success: Comparing HashSet and TreeSet");
t.checkSet(songSet1a, songSet1c, "Should fail: HashSet similar elements but different lists");
t.checkSet(songSet4a, songSet5a, "Should fail: TreeSet different lists");
}
}