--- jsr166/src/test/tck/TreeMapTest.java 2006/04/19 15:10:54 1.3 +++ jsr166/src/test/tck/TreeMapTest.java 2009/11/16 04:57:10 1.7 @@ -11,7 +11,7 @@ import java.io.*; public class TreeMapTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run (suite()); } public static Test suite() { return new TestSuite(TreeMapTest.class); @@ -20,7 +20,7 @@ public class TreeMapTest extends JSR166T /** * Create a map from Integers 1-5 to Strings "A"-"E". */ - private static TreeMap map5() { + private static TreeMap map5() { TreeMap map = new TreeMap(); assertTrue(map.isEmpty()); map.put(one, "A"); @@ -43,7 +43,7 @@ public class TreeMapTest extends JSR166T } /** - * + * */ public void testConstructFromSorted() { TreeMap map = map5(); @@ -169,11 +169,33 @@ public class TreeMapTest extends JSR166T Iterator i = s.iterator(); Integer last = (Integer)i.next(); assertEquals(last, one); + int count = 1; while (i.hasNext()) { Integer k = (Integer)i.next(); assertTrue(last.compareTo(k) < 0); last = k; + ++count; } + assertEquals(count ,5); + } + + /** + * descending iterator of key set is inverse ordered + */ + public void testKeySetDescendingIteratorOrder() { + TreeMap map = map5(); + NavigableSet s = map.navigableKeySet(); + Iterator i = s.descendingIterator(); + Integer last = (Integer)i.next(); + assertEquals(last, five); + int count = 1; + while (i.hasNext()) { + Integer k = (Integer)i.next(); + assertTrue(last.compareTo(k) > 0); + last = k; + ++count; + } + assertEquals(count ,5); } /** @@ -185,11 +207,33 @@ public class TreeMapTest extends JSR166T Iterator i = s.iterator(); Integer last = (Integer)i.next(); assertEquals(last, five); + int count = 1; while (i.hasNext()) { Integer k = (Integer)i.next(); assertTrue(last.compareTo(k) > 0); last = k; + ++count; + } + assertEquals(count, 5); + } + + /** + * descending iterator of descendingKeySet is ordered + */ + public void testDescendingKeySetDescendingIteratorOrder() { + TreeMap map = map5(); + NavigableSet s = map.descendingKeySet(); + Iterator i = s.descendingIterator(); + Integer last = (Integer)i.next(); + assertEquals(last, one); + int count = 1; + while (i.hasNext()) { + Integer k = (Integer)i.next(); + assertTrue(last.compareTo(k) < 0); + last = k; + ++count; } + assertEquals(count, 5); } /** @@ -216,7 +260,7 @@ public class TreeMapTest extends JSR166T Iterator it = s.iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); - assertTrue( + assertTrue( (e.getKey().equals(one) && e.getValue().equals("A")) || (e.getKey().equals(two) && e.getValue().equals("B")) || (e.getKey().equals(three) && e.getValue().equals("C")) || @@ -235,7 +279,7 @@ public class TreeMapTest extends JSR166T Iterator it = s.iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); - assertTrue( + assertTrue( (e.getKey().equals(one) && e.getValue().equals("A")) || (e.getKey().equals(two) && e.getValue().equals("B")) || (e.getKey().equals(three) && e.getValue().equals("C")) || @@ -525,7 +569,7 @@ public class TreeMapTest extends JSR166T for (int i = 1; i <= 5; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } // Exception tests @@ -537,7 +581,7 @@ public class TreeMapTest extends JSR166T TreeMap c = map5(); c.get(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -548,7 +592,7 @@ public class TreeMapTest extends JSR166T TreeMap c = map5(); c.containsKey(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -560,7 +604,7 @@ public class TreeMapTest extends JSR166T c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -581,7 +625,7 @@ public class TreeMapTest extends JSR166T assertEquals(q.size(), r.size()); assertTrue(q.equals(r)); assertTrue(r.equals(q)); - } catch(Exception e){ + } catch (Exception e){ e.printStackTrace(); unexpectedException(); } @@ -592,7 +636,7 @@ public class TreeMapTest extends JSR166T */ public void testSubMapContents() { TreeMap map = map5(); - NavigableMap sm = map.navigableSubMap(two, true, four, false); + NavigableMap sm = map.subMap(two, true, four, false); assertEquals(two, sm.firstKey()); assertEquals(three, sm.lastKey()); assertEquals(2, sm.size()); @@ -614,7 +658,7 @@ public class TreeMapTest extends JSR166T k = (Integer)(r.next()); assertEquals(two, k); assertFalse(r.hasNext()); - + Iterator j = sm.keySet().iterator(); j.next(); j.remove(); @@ -630,7 +674,7 @@ public class TreeMapTest extends JSR166T public void testSubMapContents2() { TreeMap map = map5(); - NavigableMap sm = map.navigableSubMap(two, true, three, false); + NavigableMap sm = map.subMap(two, true, three, false); assertEquals(1, sm.size()); assertEquals(two, sm.firstKey()); assertEquals(two, sm.lastKey()); @@ -648,7 +692,7 @@ public class TreeMapTest extends JSR166T k = (Integer)(r.next()); assertEquals(two, k); assertFalse(r.hasNext()); - + Iterator j = sm.keySet().iterator(); j.next(); j.remove(); @@ -665,7 +709,7 @@ public class TreeMapTest extends JSR166T */ public void testHeadMapContents() { TreeMap map = map5(); - NavigableMap sm = map.navigableHeadMap(four, false); + NavigableMap sm = map.headMap(four, false); assertTrue(sm.containsKey(one)); assertTrue(sm.containsKey(two)); assertTrue(sm.containsKey(three)); @@ -691,7 +735,7 @@ public class TreeMapTest extends JSR166T */ public void testTailMapContents() { TreeMap map = map5(); - NavigableMap sm = map.navigableTailMap(two, true); + NavigableMap sm = map.tailMap(two, true); assertFalse(sm.containsKey(one)); assertTrue(sm.containsKey(two)); assertTrue(sm.containsKey(three)); @@ -735,7 +779,7 @@ public class TreeMapTest extends JSR166T assertEquals("E", e.getValue()); assertFalse(i.hasNext()); - NavigableMap ssm = sm.navigableTailMap(four, true); + NavigableMap ssm = sm.tailMap(four, true); assertEquals(four, ssm.firstKey()); assertEquals(five, ssm.lastKey()); assertTrue(ssm.remove(four) != null); @@ -764,7 +808,7 @@ public class TreeMapTest extends JSR166T check(map, 0, mapSize - 1, true); check(map.descendingMap(), 0, mapSize - 1, false); - bashSubMap(map.navigableSubMap(0, true, mapSize, false), + bashSubMap(map.subMap(0, true, mapSize, false), 0, mapSize - 1, true); } @@ -772,7 +816,7 @@ public class TreeMapTest extends JSR166T NavigableMap result = null; try { result = (NavigableMap) cl.newInstance(); - } catch(Exception e) { + } catch (Exception e) { fail(); } assertEquals(result.size(), 0); @@ -797,7 +841,7 @@ public class TreeMapTest extends JSR166T } // Remove a bunch of entries with iterator - for(Iterator it = map.keySet().iterator(); it.hasNext(); ) { + for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { if (rnd.nextBoolean()) { bs.clear(it.next()); it.remove(); @@ -822,7 +866,7 @@ public class TreeMapTest extends JSR166T } // Remove a bunch of entries with iterator - for(Iterator it = map.keySet().iterator(); it.hasNext(); ) { + for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { if (rnd.nextBoolean()) { bs.clear(it.next()); it.remove(); @@ -838,7 +882,7 @@ public class TreeMapTest extends JSR166T try { map.put(key, 2 * key); fail(); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { // expected } } @@ -871,7 +915,7 @@ public class TreeMapTest extends JSR166T // headMap - pick direction and endpoint inclusion randomly boolean incl = rnd.nextBoolean(); - NavigableMap hm = map.navigableHeadMap(midPoint, incl); + NavigableMap hm = map.headMap(midPoint, incl); if (ascending) { if (rnd.nextBoolean()) bashSubMap(hm, min, midPoint - (incl ? 0 : 1), true); @@ -888,7 +932,7 @@ public class TreeMapTest extends JSR166T // tailMap - pick direction and endpoint inclusion randomly incl = rnd.nextBoolean(); - NavigableMap tm = map.navigableTailMap(midPoint,incl); + NavigableMap tm = map.tailMap(midPoint,incl); if (ascending) { if (rnd.nextBoolean()) bashSubMap(tm, midPoint + (incl ? 0 : 1), max, true); @@ -913,7 +957,7 @@ public class TreeMapTest extends JSR166T boolean lowIncl = rnd.nextBoolean(); boolean highIncl = rnd.nextBoolean(); if (ascending) { - NavigableMap sm = map.navigableSubMap( + NavigableMap sm = map.subMap( endpoints[0], lowIncl, endpoints[1], highIncl); if (rnd.nextBoolean()) bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), @@ -922,7 +966,7 @@ public class TreeMapTest extends JSR166T bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), endpoints[1] - (highIncl ? 0 : 1), false); } else { - NavigableMap sm = map.navigableSubMap( + NavigableMap sm = map.subMap( endpoints[1], highIncl, endpoints[0], lowIncl); if (rnd.nextBoolean()) bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), @@ -1036,13 +1080,13 @@ public class TreeMapTest extends JSR166T try { map.firstKey(); fail(); - } catch(NoSuchElementException e) { + } catch (NoSuchElementException e) { // expected } try { map.lastKey(); fail(); - } catch(NoSuchElementException e) { + } catch (NoSuchElementException e) { // expected } } @@ -1058,5 +1102,5 @@ public class TreeMapTest extends JSR166T static boolean eq(Integer i, int j) { return i == null ? j == -1 : i == j; } - + }