SE450: Comment on testing iterators [4/5] Previous pageContentsNext page

How to test
InventorySet.iterator(), where we don't know the order...

 + Create a local HashSet<Video> and an InventorySet.
 + Put some videos in the InventorySet.
 + Put corresponding Videos in the HashSet.  Use Video, not Record,
   since you cannot create records directly.  You could also use
   strings such as "Title1 (2000) : Director1 [10,0,0]"
 + Now call InventorySet.iterator() and go through the elements.
   For each record returned do the following:
     ++ Make sure an equivalent video is in the local HashSet
     ++ Remove the equivalent video from the HashSet
 + Once you're done with the iterator, make sure the local HashSet is
   empty.

To test InventorySet.iterator(), where we do know the order ...

 + Create class that implements Comparator.  When called, the
   arguments will be Records.  For example, the following class will
   compare Records based on the number owned:

   Class NumOwnedComparator implements java.util.Comparator {
     public int compare (Object o1, Object o2) {
       Record r1 = (Record)o1;
       Record r2 = (Record)o2;
       return r2.numOwned() - r1.numOwned();
     }
   }

   or use the following in Java 1.5 to get rid of "unchecked
   conversion" warnings:

   class NumOwnedComparator implements java.util.Comparator<Record> {
     public int compare (Record r1, Record r2) {
       return r2.numOwned() - r1.numOwned();
     }
   }

 + Continue as before, but this time use a List to keep the videos in
   expected order.

If you want some more reading about Comparators/Comparable see:
Making Java Objects Comparable
by Budi Kurniawan
http://www.onjava.com/pub/a/onjava/2003/03/12/java_comp.html

Previous pageContentsNext page