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

Iterable

The tester has a special way of comparing elements of a collection that implements the java.lang.Iterable interface. Because all classes in the Java Collections Framework that implement the java.lang.Iterable interface contain no additional relevant data, the checkExpect and the checkInexact methods use the provided Iterator to traverse over the two collections that are being compared, matching them in the order generated by the iterator.

However, if the user implements the java.lang.Iterable interface for her own class, the checkExpect (or the checkInexact) method still examines the data within the class by comparing the values of the corresponding fields. To invoke the comparison of the data generated by the iterator, the user needs to use the checkIterable (or the checkInexactIterable) method.


A compelling example of this scenario is when we wish to compare two recursively-built binary search trees, the first time verifying the shape of the tree, the second time wanting to see that both contain the same collection od elements, even if their shapes may differ. So, the following two trees will fail the first test, but pass the second one, assuming the iterator traverses the tree inorder:

  tree1:                    tree2:

     7                        4
   /   \                    /   \
  4     9                  *     9
 / \   / \                      / \
*   * *   *                    7   *
                              / \
                             *   *
The test cases would be:
void testTrees(Tester t){
  t.checkExpect(tree1, tree2, "should fail");
  t.checkIterable(tree1, tree2, "should succeed");
}

Two Arrays are similarly compared by making sure they contain the same number and types of items and every pair of corresponding items passes the equality test.


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:34:55 EDT 2011generated with DrRacket