ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TreeMapTest.java
(Generate patch)

Comparing jsr166/src/test/tck/TreeMapTest.java (file contents):
Revision 1.6 by jsr166, Mon Nov 2 20:28:32 2009 UTC vs.
Revision 1.22 by jsr166, Sat Nov 26 05:19:17 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import junit.framework.*;
8   import java.util.*;
9 import java.util.concurrent.*;
10 import java.io.*;
9  
10   public class TreeMapTest extends JSR166TestCase {
11      public static void main(String[] args) {
12 <        junit.textui.TestRunner.run (suite());
12 >        junit.textui.TestRunner.run(suite());
13      }
14      public static Test suite() {
15 <        return new TestSuite(TreeMapTest.class);
15 >        return new TestSuite(TreeMapTest.class);
16      }
17  
18      /**
19       * Create a map from Integers 1-5 to Strings "A"-"E".
20       */
21      private static TreeMap map5() {
22 <        TreeMap map = new TreeMap();
22 >        TreeMap map = new TreeMap();
23          assertTrue(map.isEmpty());
24 <        map.put(one, "A");
25 <        map.put(five, "E");
26 <        map.put(three, "C");
27 <        map.put(two, "B");
28 <        map.put(four, "D");
24 >        map.put(one, "A");
25 >        map.put(five, "E");
26 >        map.put(three, "C");
27 >        map.put(two, "B");
28 >        map.put(four, "D");
29          assertFalse(map.isEmpty());
30          assertEquals(5, map.size());
31 <        return map;
31 >        return map;
32      }
33  
34      /**
35 <     *  clear removes all pairs
35 >     * clear removes all pairs
36       */
37      public void testClear() {
38          TreeMap map = map5();
39 <        map.clear();
40 <        assertEquals(map.size(), 0);
39 >        map.clear();
40 >        assertEquals(0, map.size());
41      }
42  
43      /**
# Line 52 | Line 50 | public class TreeMapTest extends JSR166T
50      }
51  
52      /**
53 <     *  Maps with same contents are equal
53 >     * Maps with same contents are equal
54       */
55      public void testEquals() {
56          TreeMap map1 = map5();
57          TreeMap map2 = map5();
58          assertEquals(map1, map2);
59          assertEquals(map2, map1);
60 <        map1.clear();
60 >        map1.clear();
61          assertFalse(map1.equals(map2));
62          assertFalse(map2.equals(map1));
63      }
64  
65      /**
66 <     *  containsKey returns true for contained key
66 >     * containsKey returns true for contained key
67       */
68      public void testContainsKey() {
69          TreeMap map = map5();
70 <        assertTrue(map.containsKey(one));
70 >        assertTrue(map.containsKey(one));
71          assertFalse(map.containsKey(zero));
72      }
73  
74      /**
75 <     *  containsValue returns true for held values
75 >     * containsValue returns true for held values
76       */
77      public void testContainsValue() {
78          TreeMap map = map5();
79 <        assertTrue(map.containsValue("A"));
79 >        assertTrue(map.containsValue("A"));
80          assertFalse(map.containsValue("Z"));
81      }
82  
83      /**
84 <     *  get returns the correct element at the given key,
85 <     *  or null if not present
84 >     * get returns the correct element at the given key,
85 >     * or null if not present
86       */
87      public void testGet() {
88          TreeMap map = map5();
89 <        assertEquals("A", (String)map.get(one));
89 >        assertEquals("A", (String)map.get(one));
90          TreeMap empty = new TreeMap();
91          assertNull(empty.get(one));
92      }
93  
94      /**
95 <     *  isEmpty is true of empty map and false for non-empty
95 >     * isEmpty is true of empty map and false for non-empty
96       */
97      public void testIsEmpty() {
98          TreeMap empty = new TreeMap();
99          TreeMap map = map5();
100 <        assertTrue(empty.isEmpty());
100 >        assertTrue(empty.isEmpty());
101          assertFalse(map.isEmpty());
102      }
103  
104      /**
105 <     *   firstKey returns first key
105 >     * firstKey returns first key
106       */
107      public void testFirstKey() {
108          TreeMap map = map5();
109 <        assertEquals(one, map.firstKey());
109 >        assertEquals(one, map.firstKey());
110      }
111  
112      /**
113 <     *   lastKey returns last key
113 >     * lastKey returns last key
114       */
115      public void testLastKey() {
116          TreeMap map = map5();
117 <        assertEquals(five, map.lastKey());
117 >        assertEquals(five, map.lastKey());
118      }
119  
122
120      /**
121 <     *  keySet.toArray returns contains all keys
121 >     * keySet.toArray returns contains all keys
122       */
123      public void testKeySetToArray() {
124          TreeMap map = map5();
125 <        Set s = map.keySet();
125 >        Set s = map.keySet();
126          Object[] ar = s.toArray();
127          assertTrue(s.containsAll(Arrays.asList(ar)));
128 <        assertEquals(5, ar.length);
128 >        assertEquals(5, ar.length);
129          ar[0] = m10;
130          assertFalse(s.containsAll(Arrays.asList(ar)));
131      }
132  
133      /**
134 <     *  descendingkeySet.toArray returns contains all keys
134 >     * descendingkeySet.toArray returns contains all keys
135       */
136      public void testDescendingKeySetToArray() {
137          TreeMap map = map5();
138 <        Set s = map.descendingKeySet();
138 >        Set s = map.descendingKeySet();
139          Object[] ar = s.toArray();
140 <        assertEquals(5, ar.length);
140 >        assertEquals(5, ar.length);
141          assertTrue(s.containsAll(Arrays.asList(ar)));
142          ar[0] = m10;
143          assertFalse(s.containsAll(Arrays.asList(ar)));
144      }
145  
146      /**
147 <     *   keySet returns a Set containing all the keys
147 >     * keySet returns a Set containing all the keys
148       */
149      public void testKeySet() {
150          TreeMap map = map5();
151 <        Set s = map.keySet();
152 <        assertEquals(5, s.size());
153 <        assertTrue(s.contains(one));
154 <        assertTrue(s.contains(two));
155 <        assertTrue(s.contains(three));
156 <        assertTrue(s.contains(four));
157 <        assertTrue(s.contains(five));
151 >        Set s = map.keySet();
152 >        assertEquals(5, s.size());
153 >        assertTrue(s.contains(one));
154 >        assertTrue(s.contains(two));
155 >        assertTrue(s.contains(three));
156 >        assertTrue(s.contains(four));
157 >        assertTrue(s.contains(five));
158      }
159  
160      /**
161 <     *   keySet is ordered
161 >     * keySet is ordered
162       */
163      public void testKeySetOrder() {
164          TreeMap map = map5();
165 <        Set s = map.keySet();
165 >        Set s = map.keySet();
166          Iterator i = s.iterator();
167          Integer last = (Integer)i.next();
168          assertEquals(last, one);
# Line 176 | Line 173 | public class TreeMapTest extends JSR166T
173              last = k;
174              ++count;
175          }
176 <        assertEquals(count ,5);
176 >        assertEquals(5, count);
177      }
178  
179      /**
# Line 184 | Line 181 | public class TreeMapTest extends JSR166T
181       */
182      public void testKeySetDescendingIteratorOrder() {
183          TreeMap map = map5();
184 <        NavigableSet s = map.navigableKeySet();
184 >        NavigableSet s = map.navigableKeySet();
185          Iterator i = s.descendingIterator();
186          Integer last = (Integer)i.next();
187          assertEquals(last, five);
# Line 195 | Line 192 | public class TreeMapTest extends JSR166T
192              last = k;
193              ++count;
194          }
195 <        assertEquals(count ,5);
195 >        assertEquals(5, count);
196      }
197  
198      /**
199 <     *   descendingKeySet is ordered
199 >     * descendingKeySet is ordered
200       */
201      public void testDescendingKeySetOrder() {
202          TreeMap map = map5();
203 <        Set s = map.descendingKeySet();
203 >        Set s = map.descendingKeySet();
204          Iterator i = s.iterator();
205          Integer last = (Integer)i.next();
206          assertEquals(last, five);
# Line 214 | Line 211 | public class TreeMapTest extends JSR166T
211              last = k;
212              ++count;
213          }
214 <        assertEquals(count, 5);
214 >        assertEquals(5, count);
215      }
216  
217      /**
218 <     *  descending iterator of descendingKeySet is ordered
218 >     * descending iterator of descendingKeySet is ordered
219       */
220      public void testDescendingKeySetDescendingIteratorOrder() {
221          TreeMap map = map5();
222 <        NavigableSet s = map.descendingKeySet();
222 >        NavigableSet s = map.descendingKeySet();
223          Iterator i = s.descendingIterator();
224          Integer last = (Integer)i.next();
225          assertEquals(last, one);
# Line 233 | Line 230 | public class TreeMapTest extends JSR166T
230              last = k;
231              ++count;
232          }
233 <        assertEquals(count, 5);
233 >        assertEquals(5, count);
234      }
235  
236      /**
# Line 241 | Line 238 | public class TreeMapTest extends JSR166T
238       */
239      public void testValues() {
240          TreeMap map = map5();
241 <        Collection s = map.values();
242 <        assertEquals(5, s.size());
243 <        assertTrue(s.contains("A"));
244 <        assertTrue(s.contains("B"));
245 <        assertTrue(s.contains("C"));
246 <        assertTrue(s.contains("D"));
247 <        assertTrue(s.contains("E"));
241 >        Collection s = map.values();
242 >        assertEquals(5, s.size());
243 >        assertTrue(s.contains("A"));
244 >        assertTrue(s.contains("B"));
245 >        assertTrue(s.contains("C"));
246 >        assertTrue(s.contains("D"));
247 >        assertTrue(s.contains("E"));
248      }
249  
250      /**
# Line 255 | Line 252 | public class TreeMapTest extends JSR166T
252       */
253      public void testEntrySet() {
254          TreeMap map = map5();
255 <        Set s = map.entrySet();
256 <        assertEquals(5, s.size());
255 >        Set s = map.entrySet();
256 >        assertEquals(5, s.size());
257          Iterator it = s.iterator();
258          while (it.hasNext()) {
259              Map.Entry e = (Map.Entry) it.next();
# Line 274 | Line 271 | public class TreeMapTest extends JSR166T
271       */
272      public void testDescendingEntrySet() {
273          TreeMap map = map5();
274 <        Set s = map.descendingMap().entrySet();
275 <        assertEquals(5, s.size());
274 >        Set s = map.descendingMap().entrySet();
275 >        assertEquals(5, s.size());
276          Iterator it = s.iterator();
277          while (it.hasNext()) {
278              Map.Entry e = (Map.Entry) it.next();
# Line 289 | Line 286 | public class TreeMapTest extends JSR166T
286      }
287  
288      /**
289 <     *  entrySet.toArray contains all entries
289 >     * entrySet.toArray contains all entries
290       */
291      public void testEntrySetToArray() {
292          TreeMap map = map5();
293 <        Set s = map.entrySet();
293 >        Set s = map.entrySet();
294          Object[] ar = s.toArray();
295          assertEquals(5, ar.length);
296          for (int i = 0; i < 5; ++i) {
# Line 303 | Line 300 | public class TreeMapTest extends JSR166T
300      }
301  
302      /**
303 <     *  descendingEntrySet.toArray contains all entries
303 >     * descendingEntrySet.toArray contains all entries
304       */
305      public void testDescendingEntrySetToArray() {
306          TreeMap map = map5();
307 <        Set s = map.descendingMap().entrySet();
307 >        Set s = map.descendingMap().entrySet();
308          Object[] ar = s.toArray();
309          assertEquals(5, ar.length);
310          for (int i = 0; i < 5; ++i) {
# Line 317 | Line 314 | public class TreeMapTest extends JSR166T
314      }
315  
316      /**
317 <     *   putAll  adds all key-value pairs from the given map
317 >     * putAll adds all key-value pairs from the given map
318       */
319      public void testPutAll() {
320          TreeMap empty = new TreeMap();
321          TreeMap map = map5();
322 <        empty.putAll(map);
323 <        assertEquals(5, empty.size());
324 <        assertTrue(empty.containsKey(one));
325 <        assertTrue(empty.containsKey(two));
326 <        assertTrue(empty.containsKey(three));
327 <        assertTrue(empty.containsKey(four));
328 <        assertTrue(empty.containsKey(five));
322 >        empty.putAll(map);
323 >        assertEquals(5, empty.size());
324 >        assertTrue(empty.containsKey(one));
325 >        assertTrue(empty.containsKey(two));
326 >        assertTrue(empty.containsKey(three));
327 >        assertTrue(empty.containsKey(four));
328 >        assertTrue(empty.containsKey(five));
329      }
330  
331      /**
332 <     *   remove removes the correct key-value pair from the map
332 >     * remove removes the correct key-value pair from the map
333       */
334      public void testRemove() {
335          TreeMap map = map5();
336 <        map.remove(five);
337 <        assertEquals(4, map.size());
338 <        assertFalse(map.containsKey(five));
336 >        map.remove(five);
337 >        assertEquals(4, map.size());
338 >        assertFalse(map.containsKey(five));
339      }
340  
341      /**
# Line 357 | Line 354 | public class TreeMapTest extends JSR166T
354  
355          Map.Entry e4 = map.lowerEntry(zero);
356          assertNull(e4);
360
357      }
358  
359      /**
# Line 376 | Line 372 | public class TreeMapTest extends JSR166T
372  
373          Map.Entry e4 = map.higherEntry(six);
374          assertNull(e4);
379
375      }
376  
377      /**
# Line 395 | Line 390 | public class TreeMapTest extends JSR166T
390  
391          Map.Entry e4 = map.floorEntry(zero);
392          assertNull(e4);
398
393      }
394  
395      /**
# Line 414 | Line 408 | public class TreeMapTest extends JSR166T
408  
409          Map.Entry e4 = map.ceilingEntry(six);
410          assertNull(e4);
417
411      }
412  
420
413      /**
414       * lowerKey returns preceding element
415       */
# Line 434 | Line 426 | public class TreeMapTest extends JSR166T
426  
427          Object e4 = q.lowerKey(zero);
428          assertNull(e4);
437
429      }
430  
431      /**
# Line 453 | Line 444 | public class TreeMapTest extends JSR166T
444  
445          Object e4 = q.higherKey(six);
446          assertNull(e4);
456
447      }
448  
449      /**
# Line 472 | Line 462 | public class TreeMapTest extends JSR166T
462  
463          Object e4 = q.floorKey(zero);
464          assertNull(e4);
475
465      }
466  
467      /**
# Line 491 | Line 480 | public class TreeMapTest extends JSR166T
480  
481          Object e4 = q.ceilingKey(six);
482          assertNull(e4);
494
483      }
484  
485      /**
# Line 516 | Line 504 | public class TreeMapTest extends JSR166T
504          try {
505              e.setValue("A");
506              shouldThrow();
507 <        } catch (Exception ok) {
520 <        }
507 >        } catch (UnsupportedOperationException success) {}
508          e = map.pollFirstEntry();
509          assertNull(e);
510      }
# Line 544 | Line 531 | public class TreeMapTest extends JSR166T
531          try {
532              e.setValue("E");
533              shouldThrow();
534 <        } catch (Exception ok) {
548 <        }
534 >        } catch (UnsupportedOperationException success) {}
535          e = map.pollLastEntry();
536          assertNull(e);
537      }
538  
539      /**
540 <     *   size returns the correct values
540 >     * size returns the correct values
541       */
542      public void testSize() {
543          TreeMap map = map5();
544          TreeMap empty = new TreeMap();
545 <        assertEquals(0, empty.size());
546 <        assertEquals(5, map.size());
545 >        assertEquals(0, empty.size());
546 >        assertEquals(5, map.size());
547      }
548  
549      /**
# Line 567 | Line 553 | public class TreeMapTest extends JSR166T
553          TreeMap map = map5();
554          String s = map.toString();
555          for (int i = 1; i <= 5; ++i) {
556 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
556 >            assertTrue(s.contains(String.valueOf(i)));
557          }
558      }
559  
# Line 581 | Line 567 | public class TreeMapTest extends JSR166T
567              TreeMap c = map5();
568              c.get(null);
569              shouldThrow();
570 <        } catch(NullPointerException e){}
570 >        } catch (NullPointerException success) {}
571      }
572  
573      /**
# Line 592 | Line 578 | public class TreeMapTest extends JSR166T
578              TreeMap c = map5();
579              c.containsKey(null);
580              shouldThrow();
581 <        } catch(NullPointerException e){}
581 >        } catch (NullPointerException success) {}
582      }
583  
584      /**
# Line 604 | Line 590 | public class TreeMapTest extends JSR166T
590              c.put("sadsdf", "asdads");
591              c.remove(null);
592              shouldThrow();
593 <        } catch(NullPointerException e){}
593 >        } catch (NullPointerException success) {}
594      }
595  
596      /**
597       * A deserialized map equals original
598       */
599 <    public void testSerialization() {
600 <        TreeMap q = map5();
601 <
602 <        try {
603 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
604 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
605 <            out.writeObject(q);
606 <            out.close();
607 <
622 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
623 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
624 <            TreeMap r = (TreeMap)in.readObject();
625 <            assertEquals(q.size(), r.size());
626 <            assertTrue(q.equals(r));
627 <            assertTrue(r.equals(q));
628 <        } catch(Exception e){
629 <            e.printStackTrace();
630 <            unexpectedException();
631 <        }
599 >    public void testSerialization() throws Exception {
600 >        NavigableMap x = map5();
601 >        NavigableMap y = serialClone(x);
602 >
603 >        assertTrue(x != y);
604 >        assertEquals(x.size(), y.size());
605 >        assertEquals(x.toString(), y.toString());
606 >        assertEquals(x, y);
607 >        assertEquals(y, x);
608      }
609  
610      /**
# Line 667 | Line 643 | public class TreeMapTest extends JSR166T
643          assertEquals(1, sm.size());
644          assertEquals(three, sm.firstKey());
645          assertEquals(three, sm.lastKey());
646 <        assertTrue(sm.remove(three) != null);
646 >        assertEquals("C", sm.remove(three));
647          assertTrue(sm.isEmpty());
648          assertEquals(3, map.size());
649      }
# Line 700 | Line 676 | public class TreeMapTest extends JSR166T
676          assertEquals(4, map.size());
677          assertEquals(0, sm.size());
678          assertTrue(sm.isEmpty());
679 <        assertTrue(sm.remove(three) == null);
679 >        assertSame(sm.remove(three), null);
680          assertEquals(4, map.size());
681      }
682  
# Line 782 | Line 758 | public class TreeMapTest extends JSR166T
758          NavigableMap ssm = sm.tailMap(four, true);
759          assertEquals(four, ssm.firstKey());
760          assertEquals(five, ssm.lastKey());
761 <        assertTrue(ssm.remove(four) != null);
761 >        assertEquals("D", ssm.remove(four));
762          assertEquals(1, ssm.size());
763          assertEquals(3, sm.size());
764          assertEquals(4, map.size());
# Line 794 | Line 770 | public class TreeMapTest extends JSR166T
770      /**
771       * Submaps of submaps subdivide correctly
772       */
773 <    public void testRecursiveSubMaps() {
774 <        int mapSize = 1000;
775 <        Class cl = TreeMap.class;
773 >    public void testRecursiveSubMaps() throws Exception {
774 >        int mapSize = expensiveTests ? 1000 : 100;
775 >        Class cl = TreeMap.class;
776          NavigableMap<Integer, Integer> map = newMap(cl);
777          bs = new BitSet(mapSize);
778  
# Line 812 | Line 788 | public class TreeMapTest extends JSR166T
788                     0, mapSize - 1, true);
789      }
790  
791 <    static NavigableMap<Integer, Integer> newMap(Class cl) {
792 <        NavigableMap<Integer, Integer> result = null;
793 <        try {
794 <            result = (NavigableMap<Integer, Integer>) cl.newInstance();
819 <        } catch(Exception e) {
820 <            fail();
821 <        }
822 <        assertEquals(result.size(), 0);
791 >    static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception {
792 >        NavigableMap<Integer, Integer> result
793 >            = (NavigableMap<Integer, Integer>) cl.newInstance();
794 >        assertEquals(0, result.size());
795          assertFalse(result.keySet().iterator().hasNext());
796          return result;
797      }
# Line 841 | Line 813 | public class TreeMapTest extends JSR166T
813          }
814  
815          // Remove a bunch of entries with iterator
816 <        for(Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
816 >        for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
817              if (rnd.nextBoolean()) {
818                  bs.clear(it.next());
819                  it.remove();
# Line 866 | Line 838 | public class TreeMapTest extends JSR166T
838          }
839  
840          // Remove a bunch of entries with iterator
841 <        for(Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
841 >        for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
842              if (rnd.nextBoolean()) {
843                  bs.clear(it.next());
844                  it.remove();
# Line 881 | Line 853 | public class TreeMapTest extends JSR166T
853              } else {
854                  try {
855                      map.put(key, 2 * key);
856 <                    fail();
857 <                } catch(IllegalArgumentException e) {
886 <                    // expected
887 <                }
856 >                    shouldThrow();
857 >                } catch (IllegalArgumentException success) {}
858              }
859          }
860      }
# Line 982 | Line 952 | public class TreeMapTest extends JSR166T
952       */
953      void check(NavigableMap<Integer, Integer> map,
954                        final int min, final int max, final boolean ascending) {
955 <       class ReferenceSet {
955 >        class ReferenceSet {
956              int lower(int key) {
957                  return ascending ? lowerAscending(key) : higherAscending(key);
958              }
# Line 1013 | Line 983 | public class TreeMapTest extends JSR166T
983                  // BitSet should support this! Test would run much faster
984                  while (key >= min) {
985                      if (bs.get(key))
986 <                        return(key);
986 >                        return key;
987                      key--;
988                  }
989                  return -1;
# Line 1079 | Line 1049 | public class TreeMapTest extends JSR166T
1049              assertEq(rs.last(),  -1);
1050              try {
1051                  map.firstKey();
1052 <                fail();
1053 <            } catch(NoSuchElementException e) {
1084 <                // expected
1085 <            }
1052 >                shouldThrow();
1053 >            } catch (NoSuchElementException success) {}
1054              try {
1055                  map.lastKey();
1056 <                fail();
1057 <            } catch(NoSuchElementException e) {
1090 <                // expected
1091 <            }
1056 >                shouldThrow();
1057 >            } catch (NoSuchElementException success) {}
1058          }
1059      }
1060  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines