Tests that compare all elements of two iterable objects.
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 to checkIterable (or the checkInexactIterable) method.
The class ExamplesIterables contains all test cases.
You can also download the entire souce code as a zip file.
Complete test results are shown here.
Here is an example comparing iterable objects.
public class CartPtDouble {
Double x;
Double y;
public CartPtDouble(Double x, Double y) {
this.x = x;
this.y = y;
}
public Double distTo0() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
public Double distTo(CartPtDouble that) {
return Math.sqrt((this.x - that.x) * (this.x - that.x) +
(this.y - that.y) * (this.y - that.y));
}
}
public class ExamplesIterables {
CartPtDouble cpt1 = new CartPtDouble(3.0, 4.0);
CartPtDouble cpt2 = new CartPtDouble(7.0, 7.0);
CartPtDouble cpt2a = new CartPtDouble(7.0, 6.9995); // p2a is very close to p2 but not exactly
CartPtDouble cpt3 = new CartPtDouble(15.0, 8.0);
List list1 = Arrays.asList(cpt1, cpt2, cpt3);
List list2 = Arrays.asList(cpt1, cpt2a, cpt3); // list2 is very close to list1 but not exactly
List list3 = Arrays.asList(cpt1, cpt3);
List list4 = Arrays.asList(cpt1, cpt3);
public void testIterables(Tester t) {
t.checkIterable(
list3, // 1st iterable
list4, // 2nd iterable
"Success: same lists"); // test name
t.checkIterable(
list1, // 1st iterable
list2, // 2nd iterable
"Should fail: should be checkInexactIterable"); // test name
t.checkInexactIterable(
list1, // 1st iterable
list2, // 2nd iterable
0.01, // tolerance
"Success: Test Inexact Iterable"); // test name
t.checkInexactIterable(
list1, // 1st iterable (3 elements)
list3, // 2nd iterable (2 elements)
0.01, // tolerance
"Should fail: not the same lists"); // test name
}
}