--- jsr166/src/test/tck/TreeMapTest.java 2009/11/21 02:07:27 1.9 +++ jsr166/src/test/tck/TreeMapTest.java 2019/09/29 20:40:48 1.35 @@ -1,24 +1,43 @@ /* * 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.Arrays; +import java.util.BitSet; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.NavigableMap; +import java.util.NavigableSet; +import java.util.NoSuchElementException; +import java.util.Random; +import java.util.Set; +import java.util.TreeMap; + +import junit.framework.Test; public class TreeMapTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + main(suite(), args); } public static Test suite() { - return new TestSuite(TreeMapTest.class); + class Implementation implements MapImplementation { + public Class klazz() { return TreeMap.class; } + public Map emptyMap() { return new TreeMap(); } + public boolean isConcurrent() { return false; } + public boolean permitsNullKeys() { return false; } + public boolean permitsNullValues() { return true; } + public boolean supportsSetValue() { return true; } + } + return newTestSuite( + TreeMapTest.class, + MapTest.testSuite(new Implementation())); } /** - * Create a map from Integers 1-5 to Strings "A"-"E". + * Returns a new map from Integers 1-5 to Strings "A"-"E". */ private static TreeMap map5() { TreeMap map = new TreeMap(); @@ -34,16 +53,16 @@ public class TreeMapTest extends JSR166T } /** - * clear removes all pairs + * clear removes all pairs */ public void testClear() { TreeMap map = map5(); map.clear(); - assertEquals(map.size(), 0); + assertEquals(0, map.size()); } /** - * + * copy constructor creates map equal to source map */ public void testConstructFromSorted() { TreeMap map = map5(); @@ -52,7 +71,7 @@ public class TreeMapTest extends JSR166T } /** - * Maps with same contents are equal + * Maps with same contents are equal */ public void testEquals() { TreeMap map1 = map5(); @@ -65,7 +84,7 @@ public class TreeMapTest extends JSR166T } /** - * containsKey returns true for contained key + * containsKey returns true for contained key */ public void testContainsKey() { TreeMap map = map5(); @@ -74,7 +93,7 @@ public class TreeMapTest extends JSR166T } /** - * containsValue returns true for held values + * containsValue returns true for held values */ public void testContainsValue() { TreeMap map = map5(); @@ -83,8 +102,8 @@ public class TreeMapTest extends JSR166T } /** - * 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() { TreeMap map = map5(); @@ -94,7 +113,7 @@ public class TreeMapTest extends JSR166T } /** - * 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() { TreeMap empty = new TreeMap(); @@ -104,7 +123,7 @@ public class TreeMapTest extends JSR166T } /** - * firstKey returns first key + * firstKey returns first key */ public void testFirstKey() { TreeMap map = map5(); @@ -112,16 +131,15 @@ public class TreeMapTest extends JSR166T } /** - * lastKey returns last key + * lastKey returns last key */ public void testLastKey() { TreeMap map = map5(); assertEquals(five, map.lastKey()); } - /** - * keySet.toArray returns contains all keys + * keySet.toArray returns contains all keys */ public void testKeySetToArray() { TreeMap map = map5(); @@ -134,7 +152,7 @@ public class TreeMapTest extends JSR166T } /** - * descendingkeySet.toArray returns contains all keys + * descendingkeySet.toArray returns contains all keys */ public void testDescendingKeySetToArray() { TreeMap map = map5(); @@ -147,7 +165,7 @@ public class TreeMapTest extends JSR166T } /** - * keySet returns a Set containing all the keys + * keySet returns a Set containing all the keys */ public void testKeySet() { TreeMap map = map5(); @@ -161,7 +179,7 @@ public class TreeMapTest extends JSR166T } /** - * keySet is ordered + * keySet is ordered */ public void testKeySetOrder() { TreeMap map = map5(); @@ -176,7 +194,7 @@ public class TreeMapTest extends JSR166T last = k; ++count; } - assertEquals(count ,5); + assertEquals(5, count); } /** @@ -195,11 +213,11 @@ public class TreeMapTest extends JSR166T last = k; ++count; } - assertEquals(count ,5); + assertEquals(5, count); } /** - * descendingKeySet is ordered + * descendingKeySet is ordered */ public void testDescendingKeySetOrder() { TreeMap map = map5(); @@ -214,11 +232,11 @@ public class TreeMapTest extends JSR166T last = k; ++count; } - assertEquals(count, 5); + assertEquals(5, count); } /** - * descending iterator of descendingKeySet is ordered + * descending iterator of descendingKeySet is ordered */ public void testDescendingKeySetDescendingIteratorOrder() { TreeMap map = map5(); @@ -233,7 +251,7 @@ public class TreeMapTest extends JSR166T last = k; ++count; } - assertEquals(count, 5); + assertEquals(5, count); } /** @@ -289,7 +307,7 @@ public class TreeMapTest extends JSR166T } /** - * entrySet.toArray contains all entries + * entrySet.toArray contains all entries */ public void testEntrySetToArray() { TreeMap map = map5(); @@ -303,7 +321,7 @@ public class TreeMapTest extends JSR166T } /** - * descendingEntrySet.toArray contains all entries + * descendingEntrySet.toArray contains all entries */ public void testDescendingEntrySetToArray() { TreeMap map = map5(); @@ -317,7 +335,7 @@ public class TreeMapTest extends JSR166T } /** - * putAll adds all key-value pairs from the given map + * putAll adds all key-value pairs from the given map */ public void testPutAll() { TreeMap empty = new TreeMap(); @@ -332,7 +350,7 @@ public class TreeMapTest extends JSR166T } /** - * remove removes the correct key-value pair from the map + * remove removes the correct key-value pair from the map */ public void testRemove() { TreeMap map = map5(); @@ -357,7 +375,6 @@ public class TreeMapTest extends JSR166T Map.Entry e4 = map.lowerEntry(zero); assertNull(e4); - } /** @@ -376,7 +393,6 @@ public class TreeMapTest extends JSR166T Map.Entry e4 = map.higherEntry(six); assertNull(e4); - } /** @@ -395,7 +411,6 @@ public class TreeMapTest extends JSR166T Map.Entry e4 = map.floorEntry(zero); assertNull(e4); - } /** @@ -414,10 +429,8 @@ public class TreeMapTest extends JSR166T Map.Entry e4 = map.ceilingEntry(six); assertNull(e4); - } - /** * lowerKey returns preceding element */ @@ -434,7 +447,6 @@ public class TreeMapTest extends JSR166T Object e4 = q.lowerKey(zero); assertNull(e4); - } /** @@ -453,7 +465,6 @@ public class TreeMapTest extends JSR166T Object e4 = q.higherKey(six); assertNull(e4); - } /** @@ -472,7 +483,6 @@ public class TreeMapTest extends JSR166T Object e4 = q.floorKey(zero); assertNull(e4); - } /** @@ -491,7 +501,6 @@ public class TreeMapTest extends JSR166T Object e4 = q.ceilingKey(six); assertNull(e4); - } /** @@ -516,8 +525,7 @@ public class TreeMapTest extends JSR166T try { e.setValue("A"); shouldThrow(); - } catch (Exception ok) { - } + } catch (UnsupportedOperationException success) {} e = map.pollFirstEntry(); assertNull(e); } @@ -544,14 +552,13 @@ public class TreeMapTest extends JSR166T 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() { TreeMap map = map5(); @@ -567,7 +574,7 @@ public class TreeMapTest extends JSR166T TreeMap 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))); } } @@ -577,58 +584,48 @@ public class TreeMapTest extends JSR166T * get(null) of nonempty map throws NPE */ public void testGet_NullPointerException() { + TreeMap c = map5(); try { - TreeMap c = map5(); c.get(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** * containsKey(null) of nonempty map throws NPE */ public void testContainsKey_NullPointerException() { + TreeMap c = map5(); try { - TreeMap c = map5(); c.containsKey(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** * remove(null) throws NPE for nonempty map */ public void testRemove1_NullPointerException() { + TreeMap c = new TreeMap(); + c.put("sadsdf", "asdads"); try { - TreeMap c = new TreeMap(); - c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** - * A deserialized map equals original + * A deserialized/reserialized map equals original */ - public void testSerialization() { - TreeMap 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)); - TreeMap r = (TreeMap)in.readObject(); - assertEquals(q.size(), r.size()); - assertTrue(q.equals(r)); - assertTrue(r.equals(q)); - } catch (Exception e) { - e.printStackTrace(); - unexpectedException(); - } + assertNotSame(x, y); + assertEquals(x.size(), y.size()); + assertEquals(x.toString(), y.toString()); + assertEquals(x, y); + assertEquals(y, x); } /** @@ -667,7 +664,7 @@ public class TreeMapTest extends JSR166T assertEquals(1, sm.size()); assertEquals(three, sm.firstKey()); assertEquals(three, sm.lastKey()); - assertTrue(sm.remove(three) != null); + assertEquals("C", sm.remove(three)); assertTrue(sm.isEmpty()); assertEquals(3, map.size()); } @@ -700,7 +697,7 @@ public class TreeMapTest extends JSR166T assertEquals(4, map.size()); assertEquals(0, sm.size()); assertTrue(sm.isEmpty()); - assertTrue(sm.remove(three) == null); + assertSame(sm.remove(three), null); assertEquals(4, map.size()); } @@ -782,7 +779,7 @@ public class TreeMapTest extends JSR166T NavigableMap ssm = sm.tailMap(four, true); assertEquals(four, ssm.firstKey()); assertEquals(five, ssm.lastKey()); - assertTrue(ssm.remove(four) != null); + assertEquals("D", ssm.remove(four)); assertEquals(1, ssm.size()); assertEquals(3, sm.size()); assertEquals(4, map.size()); @@ -794,8 +791,8 @@ public class TreeMapTest extends JSR166T /** * Submaps of submaps subdivide correctly */ - public void testRecursiveSubMaps() { - int mapSize = 1000; + public void testRecursiveSubMaps() throws Exception { + int mapSize = expensiveTests ? 1000 : 100; Class cl = TreeMap.class; NavigableMap map = newMap(cl); bs = new BitSet(mapSize); @@ -812,14 +809,10 @@ public class TreeMapTest extends JSR166T 0, mapSize - 1, true); } - static NavigableMap newMap(Class cl) { - NavigableMap result = null; - try { - result = (NavigableMap) cl.newInstance(); - } catch (Exception e) { - fail(); - } - assertEquals(result.size(), 0); + static NavigableMap newMap(Class cl) throws Exception { + NavigableMap result + = (NavigableMap) cl.getConstructor().newInstance(); + assertEquals(0, result.size()); assertFalse(result.keySet().iterator().hasNext()); return result; } @@ -851,7 +844,7 @@ public class TreeMapTest extends JSR166T // Add entries till we're back to original size while (map.size() < size) { int key = min + rnd.nextInt(rangeSize); - assertTrue(key >= min && key<= max); + assertTrue(key >= min && key <= max); put(map, key); } } @@ -876,15 +869,13 @@ public class TreeMapTest extends JSR166T // Add entries till we're back to original size while (map.size() < size) { int key = min - 5 + rnd.nextInt(rangeSize + 10); - if (key >= min && key<= max) { + if (key >= min && key <= max) { put(map, key); } else { try { map.put(key, 2 * key); - fail(); - } catch (IllegalArgumentException e) { - // expected - } + shouldThrow(); + } catch (IllegalArgumentException success) {} } } } @@ -982,7 +973,7 @@ public class TreeMapTest extends JSR166T */ void check(NavigableMap map, final int min, final int max, final boolean ascending) { - class ReferenceSet { + class ReferenceSet { int lower(int key) { return ascending ? lowerAscending(key) : higherAscending(key); } @@ -1013,7 +1004,7 @@ public class TreeMapTest extends JSR166T // BitSet should support this! Test would run much faster while (key >= min) { if (bs.get(key)) - return(key); + return key; key--; } return -1; @@ -1048,7 +1039,7 @@ public class TreeMapTest extends JSR166T if (bsContainsI) size++; } - assertEquals(map.size(), size); + assertEquals(size, map.size()); // Test contents using contains keySet iterator int size2 = 0; @@ -1079,16 +1070,12 @@ public class TreeMapTest extends JSR166T assertEq(rs.last(), -1); try { map.firstKey(); - fail(); - } catch (NoSuchElementException e) { - // expected - } + shouldThrow(); + } catch (NoSuchElementException success) {} try { map.lastKey(); - fail(); - } catch (NoSuchElementException e) { - // expected - } + shouldThrow(); + } catch (NoSuchElementException success) {} } }