JavaLib
 
Tester
 
Overview
Basic Types
User Types
Inexact Values
Sets
Iterable
Traversals
Maps
Random Choice
Range Check
Equivalence
ISame
Exceptions
Constructors
Methods
CheckFail
 
Sources
Downloads
 
TSRJ Home
TSRJ Local

Sets

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. (As both are Iterable, the tester will traverse over their elements and match each pair using the regular extensional equality comparison.)

For comparing elements of a set we use the Java equals method when comparing two objects, as this is what is used to determine duplicates in a Set.

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.


Here is an example comparing a TreeSet and a HashSet. The first test compares two HashSets with the same data, the second test fails, because an instance of a HashSet and an instance of a TreeSet do not contain the same data in the same iterable order. The third test succeeds, as the HashSet and the TreeSet represent the same set of data:

...
Song song1a = new Song("title1", 4);   
Song song1b = this.song1;   

HashSet<Song> songSet1a = 
  new HashSet<Song>(Arrays.asList(this.song1a, this.song2, this.song3));

HashSet<Song> songSet1b = 
  new HashSet<Song>(Arrays.asList(this.song1b, this.song3, this.song2));

TreeSet<Song> songSet1c = 
  new TreeSet<Song>(Arrays.asList(this.song1b, this.song3, this.song2));

The test cases would be:
void testSets(Tester t){
  t.checkExpect(songSet1a, songSet1b, "should succeed");
  t.checkExpect(songSet1a, songSet1c, "should fail");
  t.checkSet(songSet1a, songSet1c, "should succeed");
}

Code sources

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.


last updated on Mon Apr 4 15:26:25 EDT 2011generated with DrRacket