--- jsr166/src/test/tck/TreeMapTest.java 2009/11/21 02:07:27 1.9 +++ jsr166/src/test/tck/TreeMapTest.java 2013/01/02 07:41:07 1.26 @@ -1,24 +1,22 @@ /* * 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.*; 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); } /** - * 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 +32,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 +50,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 +63,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 +72,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 +81,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 +92,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 +102,7 @@ public class TreeMapTest extends JSR166T } /** - * firstKey returns first key + * firstKey returns first key */ public void testFirstKey() { TreeMap map = map5(); @@ -112,16 +110,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 +131,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 +144,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 +158,7 @@ public class TreeMapTest extends JSR166T } /** - * keySet is ordered + * keySet is ordered */ public void testKeySetOrder() { TreeMap map = map5(); @@ -176,7 +173,7 @@ public class TreeMapTest extends JSR166T last = k; ++count; } - assertEquals(count ,5); + assertEquals(5, count); } /** @@ -195,11 +192,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 +211,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 +230,7 @@ public class TreeMapTest extends JSR166T last = k; ++count; } - assertEquals(count, 5); + assertEquals(5, count); } /** @@ -289,7 +286,7 @@ public class TreeMapTest extends JSR166T } /** - * entrySet.toArray contains all entries + * entrySet.toArray contains all entries */ public void testEntrySetToArray() { TreeMap map = map5(); @@ -303,7 +300,7 @@ public class TreeMapTest extends JSR166T } /** - * descendingEntrySet.toArray contains all entries + * descendingEntrySet.toArray contains all entries */ public void testDescendingEntrySetToArray() { TreeMap map = map5(); @@ -317,7 +314,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 +329,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 +354,6 @@ public class TreeMapTest extends JSR166T Map.Entry e4 = map.lowerEntry(zero); assertNull(e4); - } /** @@ -376,7 +372,6 @@ public class TreeMapTest extends JSR166T Map.Entry e4 = map.higherEntry(six); assertNull(e4); - } /** @@ -395,7 +390,6 @@ public class TreeMapTest extends JSR166T Map.Entry e4 = map.floorEntry(zero); assertNull(e4); - } /** @@ -414,10 +408,8 @@ public class TreeMapTest extends JSR166T Map.Entry e4 = map.ceilingEntry(six); assertNull(e4); - } - /** * lowerKey returns preceding element */ @@ -434,7 +426,6 @@ public class TreeMapTest extends JSR166T Object e4 = q.lowerKey(zero); assertNull(e4); - } /** @@ -453,7 +444,6 @@ public class TreeMapTest extends JSR166T Object e4 = q.higherKey(six); assertNull(e4); - } /** @@ -472,7 +462,6 @@ public class TreeMapTest extends JSR166T Object e4 = q.floorKey(zero); assertNull(e4); - } /** @@ -491,7 +480,6 @@ public class TreeMapTest extends JSR166T Object e4 = q.ceilingKey(six); assertNull(e4); - } /** @@ -516,8 +504,7 @@ public class TreeMapTest extends JSR166T try { e.setValue("A"); shouldThrow(); - } catch (Exception ok) { - } + } catch (UnsupportedOperationException success) {} e = map.pollFirstEntry(); assertNull(e); } @@ -544,14 +531,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 +553,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))); } } @@ -581,7 +567,7 @@ public class TreeMapTest extends JSR166T TreeMap c = map5(); c.get(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -592,7 +578,7 @@ public class TreeMapTest extends JSR166T TreeMap c = map5(); c.containsKey(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -604,31 +590,21 @@ public class TreeMapTest extends JSR166T c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** * A deserialized map equals original */ - public void testSerialization() { - TreeMap q = map5(); - - 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(); - } + public void testSerialization() throws Exception { + NavigableMap x = map5(); + NavigableMap y = serialClone(x); + + assertTrue(x != y); + assertEquals(x.size(), y.size()); + assertEquals(x.toString(), y.toString()); + assertEquals(x, y); + assertEquals(y, x); } /** @@ -667,7 +643,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 +676,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 +758,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 +770,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 +788,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.newInstance(); + assertEquals(0, result.size()); assertFalse(result.keySet().iterator().hasNext()); return result; } @@ -881,10 +853,8 @@ public class TreeMapTest extends JSR166T } else { try { map.put(key, 2 * key); - fail(); - } catch (IllegalArgumentException e) { - // expected - } + shouldThrow(); + } catch (IllegalArgumentException success) {} } } } @@ -982,7 +952,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 +983,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 +1018,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 +1049,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) {} } }