--- jsr166/src/test/tck/TreeSubMapTest.java 2006/04/19 15:10:54 1.2 +++ jsr166/src/test/tck/TreeSubMapTest.java 2021/01/26 13:33:06 1.25 @@ -1,150 +1,178 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; -import java.io.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Set; +import java.util.SortedMap; +import java.util.TreeMap; + +import junit.framework.Test; +import junit.framework.TestSuite; public class TreeSubMapTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + main(suite(), args); } public static Test suite() { - return new TestSuite(TreeSubMapTest.class); + return new TestSuite(TreeSubMapTest.class); } /** - * Create a map from Integers 1-5 to Strings "A"-"E". + * Returns a new map from Items 1-5 to Strings "A"-"E". */ - private static NavigableMap map5() { - TreeMap map = new TreeMap(); + private static NavigableMap map5() { + TreeMap map = new TreeMap(); assertTrue(map.isEmpty()); - map.put(zero, "Z"); - map.put(one, "A"); - map.put(five, "E"); - map.put(three, "C"); - map.put(two, "B"); - map.put(four, "D"); - map.put(seven, "F"); + map.put(zero, "Z"); + map.put(one, "A"); + map.put(five, "E"); + map.put(three, "C"); + map.put(two, "B"); + map.put(four, "D"); + map.put(seven, "F"); assertFalse(map.isEmpty()); - assertEquals(7, map.size()); - return map.navigableSubMap(one, true, seven, false); + mustEqual(7, map.size()); + return map.subMap(one, true, seven, false); } - private static NavigableMap map0() { - TreeMap map = new TreeMap(); + private static NavigableMap map0() { + TreeMap map = new TreeMap(); assertTrue(map.isEmpty()); - return map.navigableTailMap(one, true); + return map.tailMap(one, true); } /** - * clear removes all pairs + * Returns a new map from Items -5 to -1 to Strings "A"-"E". */ - public void testClear() { - NavigableMap map = map5(); - map.clear(); - assertEquals(map.size(), 0); + private static NavigableMap dmap5() { + TreeMap map = new TreeMap(); + assertTrue(map.isEmpty()); + map.put(minusOne, "A"); + map.put(minusFive, "E"); + map.put(minusThree, "C"); + map.put(minusTwo, "B"); + map.put(minusFour, "D"); + assertFalse(map.isEmpty()); + mustEqual(5, map.size()); + return map.descendingMap(); } + private static NavigableMap dmap0() { + TreeMap map = new TreeMap(); + assertTrue(map.isEmpty()); + return map; + } /** - * Maps with same contents are equal + * clear removes all pairs + */ + public void testClear() { + NavigableMap map = map5(); + map.clear(); + mustEqual(0, map.size()); + } + + /** + * Maps with same contents are equal */ public void testEquals() { - NavigableMap map1 = map5(); - NavigableMap map2 = map5(); - assertEquals(map1, map2); - assertEquals(map2, map1); - map1.clear(); + NavigableMap map1 = map5(); + NavigableMap map2 = map5(); + mustEqual(map1, map2); + mustEqual(map2, map1); + map1.clear(); assertFalse(map1.equals(map2)); assertFalse(map2.equals(map1)); } /** - * containsKey returns true for contained key + * containsKey returns true for contained key */ public void testContainsKey() { - NavigableMap map = map5(); - assertTrue(map.containsKey(one)); + NavigableMap map = map5(); + assertTrue(map.containsKey(one)); assertFalse(map.containsKey(zero)); } /** - * containsValue returns true for held values + * containsValue returns true for held values */ public void testContainsValue() { - NavigableMap map = map5(); - assertTrue(map.containsValue("A")); + NavigableMap map = map5(); + assertTrue(map.containsValue("A")); assertFalse(map.containsValue("Z")); } /** - * get returns the correct element at the given key, - * or null if not present + * get returns the correct element at the given key, + * or null if not present */ public void testGet() { - NavigableMap map = map5(); - assertEquals("A", (String)map.get(one)); - NavigableMap empty = map0(); + NavigableMap map = map5(); + mustEqual("A", map.get(one)); + NavigableMap empty = map0(); assertNull(empty.get(one)); } /** - * isEmpty is true of empty map and false for non-empty + * isEmpty is true of empty map and false for non-empty */ public void testIsEmpty() { - NavigableMap empty = map0(); - NavigableMap map = map5(); - assertTrue(empty.isEmpty()); + NavigableMap empty = map0(); + NavigableMap map = map5(); + assertTrue(empty.isEmpty()); assertFalse(map.isEmpty()); } /** - * firstKey returns first key + * firstKey returns first key */ public void testFirstKey() { - NavigableMap map = map5(); - assertEquals(one, map.firstKey()); + NavigableMap map = map5(); + mustEqual(one, map.firstKey()); } /** - * lastKey returns last key + * lastKey returns last key */ public void testLastKey() { - NavigableMap map = map5(); - assertEquals(five, map.lastKey()); + NavigableMap map = map5(); + mustEqual(five, map.lastKey()); } - /** - * keySet returns a Set containing all the keys + * keySet returns a Set containing all the keys */ public void testKeySet() { - NavigableMap map = map5(); - Set s = map.keySet(); - assertEquals(5, s.size()); - assertTrue(s.contains(one)); - assertTrue(s.contains(two)); - assertTrue(s.contains(three)); - assertTrue(s.contains(four)); - assertTrue(s.contains(five)); + NavigableMap map = map5(); + Set s = map.keySet(); + mustEqual(5, s.size()); + mustContain(s, one); + mustContain(s, two); + mustContain(s, three); + mustContain(s, four); + mustContain(s, five); } /** - * keySet is ordered + * keySet is ordered */ public void testKeySetOrder() { - NavigableMap map = map5(); - Set s = map.keySet(); - Iterator i = s.iterator(); - Integer last = (Integer)i.next(); - assertEquals(last, one); + NavigableMap map = map5(); + Set s = map.keySet(); + Iterator i = s.iterator(); + Item last = i.next(); + mustEqual(last, one); while (i.hasNext()) { - Integer k = (Integer)i.next(); + Item k = i.next(); assertTrue(last.compareTo(k) < 0); last = k; } @@ -154,27 +182,27 @@ public class TreeSubMapTest extends JSR1 * values collection contains all values */ public void testValues() { - NavigableMap map = map5(); - Collection s = map.values(); - assertEquals(5, s.size()); - assertTrue(s.contains("A")); - assertTrue(s.contains("B")); - assertTrue(s.contains("C")); - assertTrue(s.contains("D")); - assertTrue(s.contains("E")); + NavigableMap map = map5(); + Collection s = map.values(); + mustEqual(5, s.size()); + assertTrue(s.contains("A")); + assertTrue(s.contains("B")); + assertTrue(s.contains("C")); + assertTrue(s.contains("D")); + assertTrue(s.contains("E")); } /** * entrySet contains all pairs */ public void testEntrySet() { - NavigableMap map = map5(); - Set s = map.entrySet(); - assertEquals(5, s.size()); - Iterator it = s.iterator(); + NavigableMap map = map5(); + Set> s = map.entrySet(); + mustEqual(5, s.size()); + Iterator> it = s.iterator(); while (it.hasNext()) { - Map.Entry e = (Map.Entry) it.next(); - assertTrue( + Map.Entry e = it.next(); + 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")) || @@ -184,132 +212,127 @@ public class TreeSubMapTest extends JSR1 } /** - * putAll adds all key-value pairs from the given map + * putAll adds all key-value pairs from the given map */ public void testPutAll() { - NavigableMap empty = map0(); - NavigableMap map = map5(); - empty.putAll(map); - assertEquals(5, empty.size()); - assertTrue(empty.containsKey(one)); - assertTrue(empty.containsKey(two)); - assertTrue(empty.containsKey(three)); - assertTrue(empty.containsKey(four)); - assertTrue(empty.containsKey(five)); + NavigableMap empty = map0(); + NavigableMap map = map5(); + empty.putAll(map); + mustEqual(5, empty.size()); + assertTrue(empty.containsKey(one)); + assertTrue(empty.containsKey(two)); + assertTrue(empty.containsKey(three)); + assertTrue(empty.containsKey(four)); + assertTrue(empty.containsKey(five)); } /** - * remove removes the correct key-value pair from the map + * remove removes the correct key-value pair from the map */ public void testRemove() { - NavigableMap map = map5(); - map.remove(five); - assertEquals(4, map.size()); - assertFalse(map.containsKey(five)); + NavigableMap map = map5(); + map.remove(five); + mustEqual(4, map.size()); + assertFalse(map.containsKey(five)); } /** * lowerEntry returns preceding entry. */ public void testLowerEntry() { - NavigableMap map = map5(); - Map.Entry e1 = map.lowerEntry(three); - assertEquals(two, e1.getKey()); + NavigableMap map = map5(); + Map.Entry e1 = map.lowerEntry(three); + mustEqual(two, e1.getKey()); - Map.Entry e2 = map.lowerEntry(six); - assertEquals(five, e2.getKey()); + Map.Entry e2 = map.lowerEntry(six); + mustEqual(five, e2.getKey()); - Map.Entry e3 = map.lowerEntry(one); + Map.Entry e3 = map.lowerEntry(one); assertNull(e3); - Map.Entry e4 = map.lowerEntry(zero); + Map.Entry e4 = map.lowerEntry(zero); assertNull(e4); - } /** * higherEntry returns next entry. */ public void testHigherEntry() { - NavigableMap map = map5(); - Map.Entry e1 = map.higherEntry(three); - assertEquals(four, e1.getKey()); + NavigableMap map = map5(); + Map.Entry e1 = map.higherEntry(three); + mustEqual(four, e1.getKey()); - Map.Entry e2 = map.higherEntry(zero); - assertEquals(one, e2.getKey()); + Map.Entry e2 = map.higherEntry(zero); + mustEqual(one, e2.getKey()); - Map.Entry e3 = map.higherEntry(five); + Map.Entry e3 = map.higherEntry(five); assertNull(e3); - Map.Entry e4 = map.higherEntry(six); + Map.Entry e4 = map.higherEntry(six); assertNull(e4); - } /** * floorEntry returns preceding entry. */ public void testFloorEntry() { - NavigableMap map = map5(); - Map.Entry e1 = map.floorEntry(three); - assertEquals(three, e1.getKey()); + NavigableMap map = map5(); + Map.Entry e1 = map.floorEntry(three); + mustEqual(three, e1.getKey()); - Map.Entry e2 = map.floorEntry(six); - assertEquals(five, e2.getKey()); + Map.Entry e2 = map.floorEntry(six); + mustEqual(five, e2.getKey()); - Map.Entry e3 = map.floorEntry(one); - assertEquals(one, e3.getKey()); + Map.Entry e3 = map.floorEntry(one); + mustEqual(one, e3.getKey()); - Map.Entry e4 = map.floorEntry(zero); + Map.Entry e4 = map.floorEntry(zero); assertNull(e4); - } /** * ceilingEntry returns next entry. */ public void testCeilingEntry() { - NavigableMap map = map5(); - Map.Entry e1 = map.ceilingEntry(three); - assertEquals(three, e1.getKey()); + NavigableMap map = map5(); + Map.Entry e1 = map.ceilingEntry(three); + mustEqual(three, e1.getKey()); - Map.Entry e2 = map.ceilingEntry(zero); - assertEquals(one, e2.getKey()); + Map.Entry e2 = map.ceilingEntry(zero); + mustEqual(one, e2.getKey()); - Map.Entry e3 = map.ceilingEntry(five); - assertEquals(five, e3.getKey()); + Map.Entry e3 = map.ceilingEntry(five); + mustEqual(five, e3.getKey()); - Map.Entry e4 = map.ceilingEntry(six); + Map.Entry e4 = map.ceilingEntry(six); assertNull(e4); - } /** * pollFirstEntry returns entries in order */ public void testPollFirstEntry() { - NavigableMap map = map5(); - Map.Entry e = map.pollFirstEntry(); - assertEquals(one, e.getKey()); - assertEquals("A", e.getValue()); + NavigableMap map = map5(); + Map.Entry e = map.pollFirstEntry(); + mustEqual(one, e.getKey()); + mustEqual("A", e.getValue()); e = map.pollFirstEntry(); - assertEquals(two, e.getKey()); + mustEqual(two, e.getKey()); map.put(one, "A"); e = map.pollFirstEntry(); - assertEquals(one, e.getKey()); - assertEquals("A", e.getValue()); + mustEqual(one, e.getKey()); + mustEqual("A", e.getValue()); e = map.pollFirstEntry(); - assertEquals(three, e.getKey()); + mustEqual(three, e.getKey()); map.remove(four); e = map.pollFirstEntry(); - assertEquals(five, e.getKey()); + mustEqual(five, e.getKey()); try { e.setValue("A"); shouldThrow(); - } catch (Exception ok) { - } + } catch (UnsupportedOperationException success) {} assertTrue(map.isEmpty()); - Map.Entry f = map.firstEntry(); + Map.Entry f = map.firstEntry(); assertNull(f); e = map.pollFirstEntry(); assertNull(e); @@ -319,50 +342,49 @@ public class TreeSubMapTest extends JSR1 * pollLastEntry returns entries in order */ public void testPollLastEntry() { - NavigableMap map = map5(); - Map.Entry e = map.pollLastEntry(); - assertEquals(five, e.getKey()); - assertEquals("E", e.getValue()); + NavigableMap map = map5(); + Map.Entry e = map.pollLastEntry(); + mustEqual(five, e.getKey()); + mustEqual("E", e.getValue()); e = map.pollLastEntry(); - assertEquals(four, e.getKey()); + mustEqual(four, e.getKey()); map.put(five, "E"); e = map.pollLastEntry(); - assertEquals(five, e.getKey()); - assertEquals("E", e.getValue()); + mustEqual(five, e.getKey()); + mustEqual("E", e.getValue()); e = map.pollLastEntry(); - assertEquals(three, e.getKey()); + mustEqual(three, e.getKey()); map.remove(two); e = map.pollLastEntry(); - assertEquals(one, e.getKey()); + mustEqual(one, e.getKey()); try { e.setValue("E"); shouldThrow(); - } catch (Exception ok) { - } + } catch (UnsupportedOperationException success) {} e = map.pollLastEntry(); assertNull(e); } /** - * size returns the correct values + * size returns the correct values */ public void testSize() { - NavigableMap map = map5(); - NavigableMap empty = map0(); - assertEquals(0, empty.size()); - assertEquals(5, map.size()); + NavigableMap map = map5(); + NavigableMap empty = map0(); + mustEqual(0, empty.size()); + mustEqual(5, map.size()); } /** * toString contains toString of elements */ public void testToString() { - NavigableMap map = map5(); + NavigableMap map = map5(); String s = map.toString(); for (int i = 1; i <= 5; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } - } + } // Exception tests @@ -370,206 +392,720 @@ public class TreeSubMapTest extends JSR1 * get(null) of nonempty map throws NPE */ public void testGet_NullPointerException() { + NavigableMap c = map5(); try { - NavigableMap c = map5(); c.get(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** * containsKey(null) of nonempty map throws NPE */ public void testContainsKey_NullPointerException() { + NavigableMap c = map5(); try { - NavigableMap c = map5(); c.containsKey(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** * put(null,x) throws NPE */ public void testPut1_NullPointerException() { + NavigableMap c = map5(); try { - NavigableMap c = map5(); c.put(null, "whatever"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** * remove(null) throws NPE */ public void testRemove1_NullPointerException() { + NavigableMap c = map5(); try { - NavigableMap c = map5(); c.remove(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** - * A deserialized map equals original + * A deserialized/reserialized map equals original */ - public void testSerialization() { - NavigableMap q = map5(); + public void testSerialization() throws Exception { + NavigableMap x = map5(); + NavigableMap y = serialClone(x); - try { - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - NavigableMap r = (NavigableMap)in.readObject(); - assertFalse(r.isEmpty()); - assertEquals(q.size(), r.size()); - assertTrue(q.equals(r)); - assertTrue(r.equals(q)); - } catch(Exception e){ - e.printStackTrace(); - unexpectedException(); - } + assertNotSame(x, y); + mustEqual(x.size(), y.size()); + mustEqual(x.toString(), y.toString()); + mustEqual(x, y); + mustEqual(y, x); } - - /** * subMap returns map with keys in requested range */ public void testSubMapContents() { - NavigableMap map = map5(); - SortedMap sm = map.subMap(two, four); - assertEquals(two, sm.firstKey()); - assertEquals(three, sm.lastKey()); - assertEquals(2, sm.size()); + NavigableMap map = map5(); + SortedMap sm = map.subMap(two, four); + mustEqual(two, sm.firstKey()); + mustEqual(three, sm.lastKey()); + mustEqual(2, sm.size()); assertFalse(sm.containsKey(one)); assertTrue(sm.containsKey(two)); assertTrue(sm.containsKey(three)); assertFalse(sm.containsKey(four)); assertFalse(sm.containsKey(five)); - Iterator i = sm.keySet().iterator(); - Object k; - k = (Integer)(i.next()); - assertEquals(two, k); - k = (Integer)(i.next()); - assertEquals(three, k); + Iterator i = sm.keySet().iterator(); + Item k; + k = (Item)(i.next()); + mustEqual(two, k); + k = (Item)(i.next()); + mustEqual(three, k); assertFalse(i.hasNext()); - Iterator j = sm.keySet().iterator(); + Iterator j = sm.keySet().iterator(); j.next(); j.remove(); assertFalse(map.containsKey(two)); - assertEquals(4, map.size()); - assertEquals(1, sm.size()); - assertEquals(three, sm.firstKey()); - assertEquals(three, sm.lastKey()); - assertTrue(sm.remove(three) != null); + mustEqual(4, map.size()); + mustEqual(1, sm.size()); + mustEqual(three, sm.firstKey()); + mustEqual(three, sm.lastKey()); + mustEqual("C", sm.remove(three)); assertTrue(sm.isEmpty()); - assertEquals(3, map.size()); + mustEqual(3, map.size()); } public void testSubMapContents2() { - NavigableMap map = map5(); - SortedMap sm = map.subMap(two, three); - assertEquals(1, sm.size()); - assertEquals(two, sm.firstKey()); - assertEquals(two, sm.lastKey()); + NavigableMap map = map5(); + SortedMap sm = map.subMap(two, three); + mustEqual(1, sm.size()); + mustEqual(two, sm.firstKey()); + mustEqual(two, sm.lastKey()); assertFalse(sm.containsKey(one)); assertTrue(sm.containsKey(two)); assertFalse(sm.containsKey(three)); assertFalse(sm.containsKey(four)); assertFalse(sm.containsKey(five)); - Iterator i = sm.keySet().iterator(); + Iterator i = sm.keySet().iterator(); Object k; - k = (Integer)(i.next()); - assertEquals(two, k); + k = (Item)(i.next()); + mustEqual(two, k); assertFalse(i.hasNext()); - Iterator j = sm.keySet().iterator(); + Iterator j = sm.keySet().iterator(); j.next(); j.remove(); assertFalse(map.containsKey(two)); - assertEquals(4, map.size()); - assertEquals(0, sm.size()); + mustEqual(4, map.size()); + mustEqual(0, sm.size()); assertTrue(sm.isEmpty()); - assertTrue(sm.remove(three) == null); - assertEquals(4, map.size()); + assertNull(sm.remove(three)); + mustEqual(4, map.size()); } /** * headMap returns map with keys in requested range */ public void testHeadMapContents() { - NavigableMap map = map5(); - SortedMap sm = map.headMap(four); + NavigableMap map = map5(); + SortedMap sm = map.headMap(four); assertTrue(sm.containsKey(one)); assertTrue(sm.containsKey(two)); assertTrue(sm.containsKey(three)); assertFalse(sm.containsKey(four)); assertFalse(sm.containsKey(five)); - Iterator i = sm.keySet().iterator(); - Object k; - k = (Integer)(i.next()); - assertEquals(one, k); - k = (Integer)(i.next()); - assertEquals(two, k); - k = (Integer)(i.next()); - assertEquals(three, k); + Iterator i = sm.keySet().iterator(); + Item k; + k = (Item)(i.next()); + mustEqual(one, k); + k = (Item)(i.next()); + mustEqual(two, k); + k = (Item)(i.next()); + mustEqual(three, k); assertFalse(i.hasNext()); sm.clear(); assertTrue(sm.isEmpty()); - assertEquals(2, map.size()); - assertEquals(four, map.firstKey()); + mustEqual(2, map.size()); + mustEqual(four, map.firstKey()); } /** * headMap returns map with keys in requested range */ public void testTailMapContents() { - NavigableMap map = map5(); - SortedMap sm = map.tailMap(two); + NavigableMap map = map5(); + SortedMap sm = map.tailMap(two); assertFalse(sm.containsKey(one)); assertTrue(sm.containsKey(two)); assertTrue(sm.containsKey(three)); assertTrue(sm.containsKey(four)); assertTrue(sm.containsKey(five)); - Iterator i = sm.keySet().iterator(); + Iterator i = sm.keySet().iterator(); Object k; - k = (Integer)(i.next()); - assertEquals(two, k); - k = (Integer)(i.next()); - assertEquals(three, k); - k = (Integer)(i.next()); - assertEquals(four, k); - k = (Integer)(i.next()); - assertEquals(five, k); + k = (Item)(i.next()); + mustEqual(two, k); + k = (Item)(i.next()); + mustEqual(three, k); + k = (Item)(i.next()); + mustEqual(four, k); + k = (Item)(i.next()); + mustEqual(five, k); + assertFalse(i.hasNext()); + + Iterator> ei = sm.entrySet().iterator(); + Map.Entry e; + e = ei.next(); + mustEqual(two, e.getKey()); + mustEqual("B", e.getValue()); + e = (ei.next()); + mustEqual(three, e.getKey()); + mustEqual("C", e.getValue()); + e = (ei.next()); + mustEqual(four, e.getKey()); + mustEqual("D", e.getValue()); + e = (ei.next()); + mustEqual(five, e.getKey()); + mustEqual("E", e.getValue()); + assertFalse(i.hasNext()); + + SortedMap ssm = sm.tailMap(four); + mustEqual(four, ssm.firstKey()); + mustEqual(five, ssm.lastKey()); + mustEqual("D", ssm.remove(four)); + mustEqual(1, ssm.size()); + mustEqual(3, sm.size()); + mustEqual(4, map.size()); + } + + /** + * clear removes all pairs + */ + public void testDescendingClear() { + NavigableMap map = dmap5(); + map.clear(); + mustEqual(0, map.size()); + } + + /** + * Maps with same contents are equal + */ + public void testDescendingEquals() { + NavigableMap map1 = dmap5(); + NavigableMap map2 = dmap5(); + mustEqual(map1, map2); + mustEqual(map2, map1); + map1.clear(); + assertFalse(map1.equals(map2)); + assertFalse(map2.equals(map1)); + } + + /** + * containsKey returns true for contained key + */ + public void testDescendingContainsKey() { + NavigableMap map = dmap5(); + assertTrue(map.containsKey(minusOne)); + assertFalse(map.containsKey(zero)); + } + + /** + * containsValue returns true for held values + */ + public void testDescendingContainsValue() { + NavigableMap map = dmap5(); + assertTrue(map.containsValue("A")); + assertFalse(map.containsValue("Z")); + } + + /** + * get returns the correct element at the given key, + * or null if not present + */ + public void testDescendingGet() { + NavigableMap map = dmap5(); + mustEqual("A", map.get(minusOne)); + NavigableMap empty = dmap0(); + assertNull(empty.get(minusOne)); + } + + /** + * isEmpty is true of empty map and false for non-empty + */ + public void testDescendingIsEmpty() { + NavigableMap empty = dmap0(); + NavigableMap map = dmap5(); + assertTrue(empty.isEmpty()); + assertFalse(map.isEmpty()); + } + + /** + * firstKey returns first key + */ + public void testDescendingFirstKey() { + NavigableMap map = dmap5(); + mustEqual(minusOne, map.firstKey()); + } + + /** + * lastKey returns last key + */ + public void testDescendingLastKey() { + NavigableMap map = dmap5(); + mustEqual(minusFive, map.lastKey()); + } + + /** + * keySet returns a Set containing all the keys + */ + public void testDescendingKeySet() { + NavigableMap map = dmap5(); + Set s = map.keySet(); + mustEqual(5, s.size()); + mustContain(s, minusOne); + mustContain(s, minusTwo); + mustContain(s, minusThree); + mustContain(s, minusFour); + mustContain(s, minusFive); + } + + /** + * keySet is ordered + */ + public void testDescendingKeySetOrder() { + NavigableMap map = dmap5(); + Set s = map.keySet(); + Iterator i = s.iterator(); + Item last = (Item)i.next(); + mustEqual(last, minusOne); + while (i.hasNext()) { + Item k = (Item)i.next(); + assertTrue(last.compareTo(k) > 0); + last = k; + } + } + + /** + * values collection contains all values + */ + public void testDescendingValues() { + NavigableMap map = dmap5(); + Collection s = map.values(); + mustEqual(5, s.size()); + assertTrue(s.contains("A")); + assertTrue(s.contains("B")); + assertTrue(s.contains("C")); + assertTrue(s.contains("D")); + assertTrue(s.contains("E")); + } + + /** + * keySet.toArray returns contains all keys + */ + public void testDescendingAscendingKeySetToArray() { + NavigableMap map = dmap5(); + Set s = map.keySet(); + Item[] ar = s.toArray(new Item[0]); + assertTrue(s.containsAll(Arrays.asList(ar))); + mustEqual(5, ar.length); + ar[0] = minusTen; + assertFalse(s.containsAll(Arrays.asList(ar))); + } + + /** + * descendingkeySet.toArray returns contains all keys + */ + public void testDescendingDescendingKeySetToArray() { + NavigableMap map = dmap5(); + Set s = map.descendingKeySet(); + Item[] ar = s.toArray(new Item[0]); + mustEqual(5, ar.length); + assertTrue(s.containsAll(Arrays.asList(ar))); + ar[0] = minusTen; + assertFalse(s.containsAll(Arrays.asList(ar))); + } + + /** + * Values.toArray contains all values + */ + public void testDescendingValuesToArray() { + NavigableMap map = dmap5(); + Collection v = map.values(); + String[] ar = v.toArray(new String[0]); + ArrayList s = new ArrayList(Arrays.asList(ar)); + mustEqual(5, ar.length); + assertTrue(s.contains("A")); + assertTrue(s.contains("B")); + assertTrue(s.contains("C")); + assertTrue(s.contains("D")); + assertTrue(s.contains("E")); + } + + /** + * entrySet contains all pairs + */ + public void testDescendingEntrySet() { + NavigableMap map = dmap5(); + Set> s = map.entrySet(); + mustEqual(5, s.size()); + Iterator> it = s.iterator(); + while (it.hasNext()) { + Map.Entry e = it.next(); + assertTrue( + (e.getKey().equals(minusOne) && e.getValue().equals("A")) || + (e.getKey().equals(minusTwo) && e.getValue().equals("B")) || + (e.getKey().equals(minusThree) && e.getValue().equals("C")) || + (e.getKey().equals(minusFour) && e.getValue().equals("D")) || + (e.getKey().equals(minusFive) && e.getValue().equals("E"))); + } + } + + /** + * putAll adds all key-value pairs from the given map + */ + public void testDescendingPutAll() { + NavigableMap empty = dmap0(); + NavigableMap map = dmap5(); + empty.putAll(map); + mustEqual(5, empty.size()); + assertTrue(empty.containsKey(minusOne)); + assertTrue(empty.containsKey(minusTwo)); + assertTrue(empty.containsKey(minusThree)); + assertTrue(empty.containsKey(minusFour)); + assertTrue(empty.containsKey(minusFive)); + } + + /** + * remove removes the correct key-value pair from the map + */ + public void testDescendingRemove() { + NavigableMap map = dmap5(); + map.remove(minusFive); + mustEqual(4, map.size()); + assertFalse(map.containsKey(minusFive)); + } + + /** + * lowerEntry returns preceding entry. + */ + public void testDescendingLowerEntry() { + NavigableMap map = dmap5(); + Map.Entry e1 = map.lowerEntry(minusThree); + mustEqual(minusTwo, e1.getKey()); + + Map.Entry e2 = map.lowerEntry(minusSix); + mustEqual(minusFive, e2.getKey()); + + Map.Entry e3 = map.lowerEntry(minusOne); + assertNull(e3); + + Map.Entry e4 = map.lowerEntry(zero); + assertNull(e4); + } + + /** + * higherEntry returns next entry. + */ + public void testDescendingHigherEntry() { + NavigableMap map = dmap5(); + Map.Entry e1 = map.higherEntry(minusThree); + mustEqual(minusFour, e1.getKey()); + + Map.Entry e2 = map.higherEntry(zero); + mustEqual(minusOne, e2.getKey()); + + Map.Entry e3 = map.higherEntry(minusFive); + assertNull(e3); + + Map.Entry e4 = map.higherEntry(minusSix); + assertNull(e4); + } + + /** + * floorEntry returns preceding entry. + */ + public void testDescendingFloorEntry() { + NavigableMap map = dmap5(); + Map.Entry e1 = map.floorEntry(minusThree); + mustEqual(minusThree, e1.getKey()); + + Map.Entry e2 = map.floorEntry(minusSix); + mustEqual(minusFive, e2.getKey()); + + Map.Entry e3 = map.floorEntry(minusOne); + mustEqual(minusOne, e3.getKey()); + + Map.Entry e4 = map.floorEntry(zero); + assertNull(e4); + } + + /** + * ceilingEntry returns next entry. + */ + public void testDescendingCeilingEntry() { + NavigableMap map = dmap5(); + Map.Entry e1 = map.ceilingEntry(minusThree); + mustEqual(minusThree, e1.getKey()); + + Map.Entry e2 = map.ceilingEntry(zero); + mustEqual(minusOne, e2.getKey()); + + Map.Entry e3 = map.ceilingEntry(minusFive); + mustEqual(minusFive, e3.getKey()); + + Map.Entry e4 = map.ceilingEntry(minusSix); + assertNull(e4); + } + + /** + * pollFirstEntry returns entries in order + */ + public void testDescendingPollFirstEntry() { + NavigableMap map = dmap5(); + Map.Entry e = map.pollFirstEntry(); + mustEqual(minusOne, e.getKey()); + mustEqual("A", e.getValue()); + e = map.pollFirstEntry(); + mustEqual(minusTwo, e.getKey()); + map.put(minusOne, "A"); + e = map.pollFirstEntry(); + mustEqual(minusOne, e.getKey()); + mustEqual("A", e.getValue()); + e = map.pollFirstEntry(); + mustEqual(minusThree, e.getKey()); + map.remove(minusFour); + e = map.pollFirstEntry(); + mustEqual(minusFive, e.getKey()); + try { + e.setValue("A"); + shouldThrow(); + } catch (UnsupportedOperationException success) {} + e = map.pollFirstEntry(); + assertNull(e); + } + + /** + * pollLastEntry returns entries in order + */ + public void testDescendingPollLastEntry() { + NavigableMap map = dmap5(); + Map.Entry e = map.pollLastEntry(); + mustEqual(minusFive, e.getKey()); + mustEqual("E", e.getValue()); + e = map.pollLastEntry(); + mustEqual(minusFour, e.getKey()); + map.put(minusFive, "E"); + e = map.pollLastEntry(); + mustEqual(minusFive, e.getKey()); + mustEqual("E", e.getValue()); + e = map.pollLastEntry(); + mustEqual(minusThree, e.getKey()); + map.remove(minusTwo); + e = map.pollLastEntry(); + mustEqual(minusOne, e.getKey()); + try { + e.setValue("E"); + shouldThrow(); + } catch (UnsupportedOperationException success) {} + e = map.pollLastEntry(); + assertNull(e); + } + + /** + * size returns the correct values + */ + public void testDescendingSize() { + NavigableMap map = dmap5(); + NavigableMap empty = dmap0(); + mustEqual(0, empty.size()); + mustEqual(5, map.size()); + } + + /** + * toString contains toString of elements + */ + public void testDescendingToString() { + NavigableMap map = dmap5(); + String s = map.toString(); + for (int i = 1; i <= 5; ++i) { + assertTrue(s.contains(String.valueOf(i))); + } + } + + // Exception testDescendings + + /** + * get(null) of nonempty map throws NPE + */ + public void testDescendingGet_NullPointerException() { + NavigableMap c = dmap5(); + try { + c.get(null); + shouldThrow(); + } catch (NullPointerException success) {} + } + + /** + * put(null,x) throws NPE + */ + public void testDescendingPut1_NullPointerException() { + NavigableMap c = dmap5(); + try { + c.put(null, "whatever"); + shouldThrow(); + } catch (NullPointerException success) {} + } + + /** + * A deserialized/reserialized map equals original + */ + public void testDescendingSerialization() throws Exception { + NavigableMap x = dmap5(); + NavigableMap y = serialClone(x); + + assertNotSame(x, y); + mustEqual(x.size(), y.size()); + mustEqual(x.toString(), y.toString()); + mustEqual(x, y); + mustEqual(y, x); + } + + /** + * subMap returns map with keys in requested range + */ + public void testDescendingSubMapContents() { + NavigableMap map = dmap5(); + SortedMap sm = map.subMap(minusTwo, minusFour); + mustEqual(minusTwo, sm.firstKey()); + mustEqual(minusThree, sm.lastKey()); + mustEqual(2, sm.size()); + assertFalse(sm.containsKey(minusOne)); + assertTrue(sm.containsKey(minusTwo)); + assertTrue(sm.containsKey(minusThree)); + assertFalse(sm.containsKey(minusFour)); + assertFalse(sm.containsKey(minusFive)); + Iterator i = sm.keySet().iterator(); + Item k; + k = (Item)(i.next()); + mustEqual(minusTwo, k); + k = (Item)(i.next()); + mustEqual(minusThree, k); assertFalse(i.hasNext()); + Iterator j = sm.keySet().iterator(); + j.next(); + j.remove(); + assertFalse(map.containsKey(minusTwo)); + mustEqual(4, map.size()); + mustEqual(1, sm.size()); + mustEqual(minusThree, sm.firstKey()); + mustEqual(minusThree, sm.lastKey()); + mustEqual("C", sm.remove(minusThree)); + assertTrue(sm.isEmpty()); + mustEqual(3, map.size()); + } - Iterator ei = sm.entrySet().iterator(); - Map.Entry e; - e = (Map.Entry)(ei.next()); - assertEquals(two, e.getKey()); - assertEquals("B", e.getValue()); - e = (Map.Entry)(ei.next()); - assertEquals(three, e.getKey()); - assertEquals("C", e.getValue()); - e = (Map.Entry)(ei.next()); - assertEquals(four, e.getKey()); - assertEquals("D", e.getValue()); - e = (Map.Entry)(ei.next()); - assertEquals(five, e.getKey()); - assertEquals("E", e.getValue()); + public void testDescendingSubMapContents2() { + NavigableMap map = dmap5(); + SortedMap sm = map.subMap(minusTwo, minusThree); + mustEqual(1, sm.size()); + mustEqual(minusTwo, sm.firstKey()); + mustEqual(minusTwo, sm.lastKey()); + assertFalse(sm.containsKey(minusOne)); + assertTrue(sm.containsKey(minusTwo)); + assertFalse(sm.containsKey(minusThree)); + assertFalse(sm.containsKey(minusFour)); + assertFalse(sm.containsKey(minusFive)); + Iterator i = sm.keySet().iterator(); + Item k; + k = (Item)(i.next()); + mustEqual(minusTwo, k); assertFalse(i.hasNext()); + Iterator j = sm.keySet().iterator(); + j.next(); + j.remove(); + assertFalse(map.containsKey(minusTwo)); + mustEqual(4, map.size()); + mustEqual(0, sm.size()); + assertTrue(sm.isEmpty()); + assertNull(sm.remove(minusThree)); + mustEqual(4, map.size()); + } - SortedMap ssm = sm.tailMap(four); - assertEquals(four, ssm.firstKey()); - assertEquals(five, ssm.lastKey()); - assertTrue(ssm.remove(four) != null); - assertEquals(1, ssm.size()); - assertEquals(3, sm.size()); - assertEquals(4, map.size()); + /** + * headMap returns map with keys in requested range + */ + public void testDescendingHeadMapContents() { + NavigableMap map = dmap5(); + SortedMap sm = map.headMap(minusFour); + assertTrue(sm.containsKey(minusOne)); + assertTrue(sm.containsKey(minusTwo)); + assertTrue(sm.containsKey(minusThree)); + assertFalse(sm.containsKey(minusFour)); + assertFalse(sm.containsKey(minusFive)); + Iterator i = sm.keySet().iterator(); + Item k; + k = (Item)(i.next()); + mustEqual(minusOne, k); + k = (Item)(i.next()); + mustEqual(minusTwo, k); + k = (Item)(i.next()); + mustEqual(minusThree, k); + assertFalse(i.hasNext()); + sm.clear(); + assertTrue(sm.isEmpty()); + mustEqual(2, map.size()); + mustEqual(minusFour, map.firstKey()); + } + + /** + * headMap returns map with keys in requested range + */ + public void testDescendingTailMapContents() { + NavigableMap map = dmap5(); + SortedMap sm = map.tailMap(minusTwo); + assertFalse(sm.containsKey(minusOne)); + assertTrue(sm.containsKey(minusTwo)); + assertTrue(sm.containsKey(minusThree)); + assertTrue(sm.containsKey(minusFour)); + assertTrue(sm.containsKey(minusFive)); + Iterator i = sm.keySet().iterator(); + Item k; + k = (Item)(i.next()); + mustEqual(minusTwo, k); + k = (Item)(i.next()); + mustEqual(minusThree, k); + k = (Item)(i.next()); + mustEqual(minusFour, k); + k = (Item)(i.next()); + mustEqual(minusFive, k); + assertFalse(i.hasNext()); + + Iterator> ei = sm.entrySet().iterator(); + Map.Entry e; + e = (ei.next()); + mustEqual(minusTwo, e.getKey()); + mustEqual("B", e.getValue()); + e = (ei.next()); + mustEqual(minusThree, e.getKey()); + mustEqual("C", e.getValue()); + e = (ei.next()); + mustEqual(minusFour, e.getKey()); + mustEqual("D", e.getValue()); + e = (ei.next()); + mustEqual(minusFive, e.getKey()); + mustEqual("E", e.getValue()); + assertFalse(i.hasNext()); + + SortedMap ssm = sm.tailMap(minusFour); + mustEqual(minusFour, ssm.firstKey()); + mustEqual(minusFive, ssm.lastKey()); + mustEqual("D", ssm.remove(minusFour)); + mustEqual(1, ssm.size()); + mustEqual(3, sm.size()); + mustEqual(4, map.size()); } - + }