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

Comparing jsr166/src/test/tck/ConcurrentSkipListMapTest.java (file contents):
Revision 1.6 by dl, Wed Apr 19 15:10:54 2006 UTC vs.
Revision 1.28 by jsr166, Sat Nov 26 05:42:14 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 > import java.util.concurrent.ConcurrentSkipListMap;
10  
11   public class ConcurrentSkipListMapTest extends JSR166TestCase {
12      public static void main(String[] args) {
13 <        junit.textui.TestRunner.run (suite());  
13 >        junit.textui.TestRunner.run(suite());
14      }
15      public static Test suite() {
16 <        return new TestSuite(ConcurrentSkipListMapTest.class);
16 >        return new TestSuite(ConcurrentSkipListMapTest.class);
17      }
18  
19      /**
20       * Create a map from Integers 1-5 to Strings "A"-"E".
21       */
22 <    private static ConcurrentSkipListMap map5() {  
23 <        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
22 >    private static ConcurrentSkipListMap map5() {
23 >        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
24          assertTrue(map.isEmpty());
25 <        map.put(one, "A");
26 <        map.put(five, "E");
27 <        map.put(three, "C");
28 <        map.put(two, "B");
29 <        map.put(four, "D");
25 >        map.put(one, "A");
26 >        map.put(five, "E");
27 >        map.put(three, "C");
28 >        map.put(two, "B");
29 >        map.put(four, "D");
30          assertFalse(map.isEmpty());
31          assertEquals(5, map.size());
32 <        return map;
32 >        return map;
33      }
34  
35      /**
36 <     *  clear removes all pairs
36 >     * clear removes all pairs
37       */
38      public void testClear() {
39          ConcurrentSkipListMap map = map5();
40 <        map.clear();
41 <        assertEquals(map.size(), 0);
40 >        map.clear();
41 >        assertEquals(0, map.size());
42      }
43  
44      /**
45 <     *  
45 >     *
46       */
47      public void testConstructFromSorted() {
48          ConcurrentSkipListMap map = map5();
# Line 52 | Line 51 | public class ConcurrentSkipListMapTest e
51      }
52  
53      /**
54 <     *  Maps with same contents are equal
54 >     * Maps with same contents are equal
55       */
56      public void testEquals() {
57          ConcurrentSkipListMap map1 = map5();
58          ConcurrentSkipListMap map2 = map5();
59          assertEquals(map1, map2);
60          assertEquals(map2, map1);
61 <        map1.clear();
61 >        map1.clear();
62          assertFalse(map1.equals(map2));
63          assertFalse(map2.equals(map1));
64      }
65  
66      /**
67 <     *  containsKey returns true for contained key
67 >     * containsKey returns true for contained key
68       */
69      public void testContainsKey() {
70          ConcurrentSkipListMap map = map5();
71 <        assertTrue(map.containsKey(one));
71 >        assertTrue(map.containsKey(one));
72          assertFalse(map.containsKey(zero));
73      }
74  
75      /**
76 <     *  containsValue returns true for held values
76 >     * containsValue returns true for held values
77       */
78      public void testContainsValue() {
79          ConcurrentSkipListMap map = map5();
80 <        assertTrue(map.containsValue("A"));
80 >        assertTrue(map.containsValue("A"));
81          assertFalse(map.containsValue("Z"));
82      }
83  
84      /**
85 <     *  get returns the correct element at the given key,
86 <     *  or null if not present
85 >     * get returns the correct element at the given key,
86 >     * or null if not present
87       */
88      public void testGet() {
89          ConcurrentSkipListMap map = map5();
90 <        assertEquals("A", (String)map.get(one));
90 >        assertEquals("A", (String)map.get(one));
91          ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
92          assertNull(empty.get(one));
93      }
94  
95      /**
96 <     *  isEmpty is true of empty map and false for non-empty
96 >     * isEmpty is true of empty map and false for non-empty
97       */
98      public void testIsEmpty() {
99          ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
100          ConcurrentSkipListMap map = map5();
101 <        assertTrue(empty.isEmpty());
101 >        assertTrue(empty.isEmpty());
102          assertFalse(map.isEmpty());
103      }
104  
105      /**
106 <     *   firstKey returns first key
106 >     * firstKey returns first key
107       */
108      public void testFirstKey() {
109          ConcurrentSkipListMap map = map5();
110 <        assertEquals(one, map.firstKey());
110 >        assertEquals(one, map.firstKey());
111      }
112  
113      /**
114 <     *   lastKey returns last key
114 >     * lastKey returns last key
115       */
116      public void testLastKey() {
117          ConcurrentSkipListMap map = map5();
118 <        assertEquals(five, map.lastKey());
118 >        assertEquals(five, map.lastKey());
119      }
120  
122
121      /**
122 <     *  keySet.toArray returns contains all keys
122 >     * keySet.toArray returns contains all keys
123       */
124      public void testKeySetToArray() {
125          ConcurrentSkipListMap map = map5();
126 <        Set s = map.keySet();
126 >        Set s = map.keySet();
127          Object[] ar = s.toArray();
128          assertTrue(s.containsAll(Arrays.asList(ar)));
129 <        assertEquals(5, ar.length);
129 >        assertEquals(5, ar.length);
130          ar[0] = m10;
131          assertFalse(s.containsAll(Arrays.asList(ar)));
132      }
133  
134      /**
135 <     *  descendingkeySet.toArray returns contains all keys
135 >     * descendingkeySet.toArray returns contains all keys
136       */
137      public void testDescendingKeySetToArray() {
138          ConcurrentSkipListMap map = map5();
139 <        Set s = map.descendingKeySet();
139 >        Set s = map.descendingKeySet();
140          Object[] ar = s.toArray();
141 <        assertEquals(5, ar.length);
141 >        assertEquals(5, ar.length);
142          assertTrue(s.containsAll(Arrays.asList(ar)));
143          ar[0] = m10;
144          assertFalse(s.containsAll(Arrays.asList(ar)));
145      }
146  
147      /**
148 <     *   keySet returns a Set containing all the keys
148 >     * keySet returns a Set containing all the keys
149       */
150      public void testKeySet() {
151          ConcurrentSkipListMap map = map5();
152 <        Set s = map.keySet();
153 <        assertEquals(5, s.size());
154 <        assertTrue(s.contains(one));
155 <        assertTrue(s.contains(two));
156 <        assertTrue(s.contains(three));
157 <        assertTrue(s.contains(four));
158 <        assertTrue(s.contains(five));
152 >        Set s = map.keySet();
153 >        assertEquals(5, s.size());
154 >        assertTrue(s.contains(one));
155 >        assertTrue(s.contains(two));
156 >        assertTrue(s.contains(three));
157 >        assertTrue(s.contains(four));
158 >        assertTrue(s.contains(five));
159      }
160  
161      /**
162 <     *   keySet is ordered
162 >     * keySet is ordered
163       */
164      public void testKeySetOrder() {
165          ConcurrentSkipListMap map = map5();
166 <        Set s = map.keySet();
166 >        Set s = map.keySet();
167          Iterator i = s.iterator();
168          Integer last = (Integer)i.next();
169          assertEquals(last, one);
170 +        int count = 1;
171          while (i.hasNext()) {
172              Integer k = (Integer)i.next();
173              assertTrue(last.compareTo(k) < 0);
174              last = k;
175 +            ++count;
176 +        }
177 +        assertEquals(5, count);
178 +    }
179 +
180 +    /**
181 +     * descending iterator of key set is inverse ordered
182 +     */
183 +    public void testKeySetDescendingIteratorOrder() {
184 +        ConcurrentSkipListMap map = map5();
185 +        NavigableSet s = map.navigableKeySet();
186 +        Iterator i = s.descendingIterator();
187 +        Integer last = (Integer)i.next();
188 +        assertEquals(last, five);
189 +        int count = 1;
190 +        while (i.hasNext()) {
191 +            Integer k = (Integer)i.next();
192 +            assertTrue(last.compareTo(k) > 0);
193 +            last = k;
194 +            ++count;
195          }
196 +        assertEquals(5, count);
197      }
198  
199      /**
200 <     *   descendingKeySet is ordered
200 >     * descendingKeySet is ordered
201       */
202      public void testDescendingKeySetOrder() {
203          ConcurrentSkipListMap map = map5();
204 <        Set s = map.descendingKeySet();
204 >        Set s = map.descendingKeySet();
205          Iterator i = s.iterator();
206          Integer last = (Integer)i.next();
207          assertEquals(last, five);
208 +        int count = 1;
209          while (i.hasNext()) {
210              Integer k = (Integer)i.next();
211              assertTrue(last.compareTo(k) > 0);
212              last = k;
213 +            ++count;
214          }
215 +        assertEquals(5, count);
216      }
217  
218 +    /**
219 +     * descending iterator of descendingKeySet is ordered
220 +     */
221 +    public void testDescendingKeySetDescendingIteratorOrder() {
222 +        ConcurrentSkipListMap map = map5();
223 +        NavigableSet s = map.descendingKeySet();
224 +        Iterator i = s.descendingIterator();
225 +        Integer last = (Integer)i.next();
226 +        assertEquals(last, one);
227 +        int count = 1;
228 +        while (i.hasNext()) {
229 +            Integer k = (Integer)i.next();
230 +            assertTrue(last.compareTo(k) < 0);
231 +            last = k;
232 +            ++count;
233 +        }
234 +        assertEquals(5, count);
235 +    }
236  
237      /**
238 <     *  Values.toArray contains all values
238 >     * Values.toArray contains all values
239       */
240      public void testValuesToArray() {
241          ConcurrentSkipListMap map = map5();
242 <        Collection v = map.values();
242 >        Collection v = map.values();
243          Object[] ar = v.toArray();
244          ArrayList s = new ArrayList(Arrays.asList(ar));
245 <        assertEquals(5, ar.length);
246 <        assertTrue(s.contains("A"));
247 <        assertTrue(s.contains("B"));
248 <        assertTrue(s.contains("C"));
249 <        assertTrue(s.contains("D"));
250 <        assertTrue(s.contains("E"));
245 >        assertEquals(5, ar.length);
246 >        assertTrue(s.contains("A"));
247 >        assertTrue(s.contains("B"));
248 >        assertTrue(s.contains("C"));
249 >        assertTrue(s.contains("D"));
250 >        assertTrue(s.contains("E"));
251      }
252  
253      /**
# Line 214 | Line 255 | public class ConcurrentSkipListMapTest e
255       */
256      public void testValues() {
257          ConcurrentSkipListMap map = map5();
258 <        Collection s = map.values();
259 <        assertEquals(5, s.size());
260 <        assertTrue(s.contains("A"));
261 <        assertTrue(s.contains("B"));
262 <        assertTrue(s.contains("C"));
263 <        assertTrue(s.contains("D"));
264 <        assertTrue(s.contains("E"));
258 >        Collection s = map.values();
259 >        assertEquals(5, s.size());
260 >        assertTrue(s.contains("A"));
261 >        assertTrue(s.contains("B"));
262 >        assertTrue(s.contains("C"));
263 >        assertTrue(s.contains("D"));
264 >        assertTrue(s.contains("E"));
265      }
266  
267      /**
# Line 228 | Line 269 | public class ConcurrentSkipListMapTest e
269       */
270      public void testEntrySet() {
271          ConcurrentSkipListMap map = map5();
272 <        Set s = map.entrySet();
273 <        assertEquals(5, s.size());
272 >        Set s = map.entrySet();
273 >        assertEquals(5, s.size());
274          Iterator it = s.iterator();
275          while (it.hasNext()) {
276              Map.Entry e = (Map.Entry) it.next();
277 <            assertTrue(
277 >            assertTrue(
278                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
279                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
280                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 247 | Line 288 | public class ConcurrentSkipListMapTest e
288       */
289      public void testDescendingEntrySet() {
290          ConcurrentSkipListMap map = map5();
291 <        Set s = map.descendingMap().entrySet();
292 <        assertEquals(5, s.size());
291 >        Set s = map.descendingMap().entrySet();
292 >        assertEquals(5, s.size());
293          Iterator it = s.iterator();
294          while (it.hasNext()) {
295              Map.Entry e = (Map.Entry) it.next();
296 <            assertTrue(
296 >            assertTrue(
297                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
298                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
299                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 262 | Line 303 | public class ConcurrentSkipListMapTest e
303      }
304  
305      /**
306 <     *  entrySet.toArray contains all entries
306 >     * entrySet.toArray contains all entries
307       */
308      public void testEntrySetToArray() {
309          ConcurrentSkipListMap map = map5();
310 <        Set s = map.entrySet();
310 >        Set s = map.entrySet();
311          Object[] ar = s.toArray();
312          assertEquals(5, ar.length);
313          for (int i = 0; i < 5; ++i) {
# Line 276 | Line 317 | public class ConcurrentSkipListMapTest e
317      }
318  
319      /**
320 <     *  descendingEntrySet.toArray contains all entries
320 >     * descendingEntrySet.toArray contains all entries
321       */
322      public void testDescendingEntrySetToArray() {
323          ConcurrentSkipListMap map = map5();
324 <        Set s = map.descendingMap().entrySet();
324 >        Set s = map.descendingMap().entrySet();
325          Object[] ar = s.toArray();
326          assertEquals(5, ar.length);
327          for (int i = 0; i < 5; ++i) {
# Line 290 | Line 331 | public class ConcurrentSkipListMapTest e
331      }
332  
333      /**
334 <     *   putAll  adds all key-value pairs from the given map
334 >     * putAll adds all key-value pairs from the given map
335       */
336      public void testPutAll() {
337          ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
338          ConcurrentSkipListMap map = map5();
339 <        empty.putAll(map);
340 <        assertEquals(5, empty.size());
341 <        assertTrue(empty.containsKey(one));
342 <        assertTrue(empty.containsKey(two));
343 <        assertTrue(empty.containsKey(three));
344 <        assertTrue(empty.containsKey(four));
345 <        assertTrue(empty.containsKey(five));
339 >        empty.putAll(map);
340 >        assertEquals(5, empty.size());
341 >        assertTrue(empty.containsKey(one));
342 >        assertTrue(empty.containsKey(two));
343 >        assertTrue(empty.containsKey(three));
344 >        assertTrue(empty.containsKey(four));
345 >        assertTrue(empty.containsKey(five));
346      }
347  
348      /**
349 <     *   putIfAbsent works when the given key is not present
349 >     * putIfAbsent works when the given key is not present
350       */
351      public void testPutIfAbsent() {
352          ConcurrentSkipListMap map = map5();
353 <        map.putIfAbsent(six, "Z");
353 >        map.putIfAbsent(six, "Z");
354          assertTrue(map.containsKey(six));
355      }
356  
357      /**
358 <     *   putIfAbsent does not add the pair if the key is already present
358 >     * putIfAbsent does not add the pair if the key is already present
359       */
360      public void testPutIfAbsent2() {
361          ConcurrentSkipListMap map = map5();
# Line 322 | Line 363 | public class ConcurrentSkipListMapTest e
363      }
364  
365      /**
366 <     *   replace fails when the given key is not present
366 >     * replace fails when the given key is not present
367       */
368      public void testReplace() {
369          ConcurrentSkipListMap map = map5();
370 <        assertNull(map.replace(six, "Z"));
370 >        assertNull(map.replace(six, "Z"));
371          assertFalse(map.containsKey(six));
372      }
373  
374      /**
375 <     *   replace succeeds if the key is already present
375 >     * replace succeeds if the key is already present
376       */
377      public void testReplace2() {
378          ConcurrentSkipListMap map = map5();
# Line 339 | Line 380 | public class ConcurrentSkipListMapTest e
380          assertEquals("Z", map.get(one));
381      }
382  
342
383      /**
384       * replace value fails when the given key not mapped to expected value
385       */
386      public void testReplaceValue() {
387          ConcurrentSkipListMap map = map5();
388          assertEquals("A", map.get(one));
389 <        assertFalse(map.replace(one, "Z", "Z"));
389 >        assertFalse(map.replace(one, "Z", "Z"));
390          assertEquals("A", map.get(one));
391      }
392  
# Line 356 | Line 396 | public class ConcurrentSkipListMapTest e
396      public void testReplaceValue2() {
397          ConcurrentSkipListMap map = map5();
398          assertEquals("A", map.get(one));
399 <        assertTrue(map.replace(one, "A", "Z"));
399 >        assertTrue(map.replace(one, "A", "Z"));
400          assertEquals("Z", map.get(one));
401      }
402  
363
403      /**
404 <     *   remove removes the correct key-value pair from the map
404 >     * remove removes the correct key-value pair from the map
405       */
406      public void testRemove() {
407          ConcurrentSkipListMap map = map5();
408 <        map.remove(five);
409 <        assertEquals(4, map.size());
410 <        assertFalse(map.containsKey(five));
408 >        map.remove(five);
409 >        assertEquals(4, map.size());
410 >        assertFalse(map.containsKey(five));
411      }
412  
413      /**
# Line 376 | Line 415 | public class ConcurrentSkipListMapTest e
415       */
416      public void testRemove2() {
417          ConcurrentSkipListMap map = map5();
418 <        assertTrue(map.containsKey(five));
418 >        assertTrue(map.containsKey(five));
419          assertEquals("E", map.get(five));
420 <        map.remove(five, "E");
421 <        assertEquals(4, map.size());
422 <        assertFalse(map.containsKey(five));
423 <        map.remove(four, "A");
424 <        assertEquals(4, map.size());
425 <        assertTrue(map.containsKey(four));
387 <
420 >        map.remove(five, "E");
421 >        assertEquals(4, map.size());
422 >        assertFalse(map.containsKey(five));
423 >        map.remove(four, "A");
424 >        assertEquals(4, map.size());
425 >        assertTrue(map.containsKey(four));
426      }
427  
428      /**
# Line 403 | Line 441 | public class ConcurrentSkipListMapTest e
441  
442          Map.Entry e4 = map.lowerEntry(zero);
443          assertNull(e4);
406
444      }
445  
446      /**
# Line 422 | Line 459 | public class ConcurrentSkipListMapTest e
459  
460          Map.Entry e4 = map.higherEntry(six);
461          assertNull(e4);
425
462      }
463  
464      /**
# Line 441 | Line 477 | public class ConcurrentSkipListMapTest e
477  
478          Map.Entry e4 = map.floorEntry(zero);
479          assertNull(e4);
444
480      }
481  
482      /**
# Line 460 | Line 495 | public class ConcurrentSkipListMapTest e
495  
496          Map.Entry e4 = map.ceilingEntry(six);
497          assertNull(e4);
463
498      }
499  
500      /**
501       * lowerEntry, higherEntry, ceilingEntry, and floorEntry return
502 <     * imutable entries
502 >     * immutable entries
503       */
504 <    public void testEntryImmutablity() {
504 >    public void testEntryImmutability() {
505          ConcurrentSkipListMap map = map5();
506          Map.Entry e = map.lowerEntry(three);
507          assertEquals(two, e.getKey());
508          try {
509              e.setValue("X");
510 <            fail();
511 <        } catch(UnsupportedOperationException success) {}
510 >            shouldThrow();
511 >        } catch (UnsupportedOperationException success) {}
512          e = map.higherEntry(zero);
513          assertEquals(one, e.getKey());
514          try {
515              e.setValue("X");
516 <            fail();
517 <        } catch(UnsupportedOperationException success) {}
516 >            shouldThrow();
517 >        } catch (UnsupportedOperationException success) {}
518          e = map.floorEntry(one);
519          assertEquals(one, e.getKey());
520          try {
521              e.setValue("X");
522 <            fail();
523 <        } catch(UnsupportedOperationException success) {}
522 >            shouldThrow();
523 >        } catch (UnsupportedOperationException success) {}
524          e = map.ceilingEntry(five);
525          assertEquals(five, e.getKey());
526          try {
527              e.setValue("X");
528 <            fail();
529 <        } catch(UnsupportedOperationException success) {}
528 >            shouldThrow();
529 >        } catch (UnsupportedOperationException success) {}
530      }
531  
498
499
532      /**
533       * lowerKey returns preceding element
534       */
# Line 513 | Line 545 | public class ConcurrentSkipListMapTest e
545  
546          Object e4 = q.lowerKey(zero);
547          assertNull(e4);
516
548      }
549  
550      /**
# Line 532 | Line 563 | public class ConcurrentSkipListMapTest e
563  
564          Object e4 = q.higherKey(six);
565          assertNull(e4);
535
566      }
567  
568      /**
# Line 551 | Line 581 | public class ConcurrentSkipListMapTest e
581  
582          Object e4 = q.floorKey(zero);
583          assertNull(e4);
554
584      }
585  
586      /**
# Line 570 | Line 599 | public class ConcurrentSkipListMapTest e
599  
600          Object e4 = q.ceilingKey(six);
601          assertNull(e4);
573
602      }
603  
604      /**
# Line 595 | Line 623 | public class ConcurrentSkipListMapTest e
623          try {
624              e.setValue("A");
625              shouldThrow();
626 <        } catch (Exception ok) {
599 <        }
626 >        } catch (UnsupportedOperationException success) {}
627          e = map.pollFirstEntry();
628          assertNull(e);
629      }
# Line 623 | Line 650 | public class ConcurrentSkipListMapTest e
650          try {
651              e.setValue("E");
652              shouldThrow();
653 <        } catch (Exception ok) {
627 <        }
653 >        } catch (UnsupportedOperationException success) {}
654          e = map.pollLastEntry();
655          assertNull(e);
656      }
657  
658      /**
659 <     *   size returns the correct values
659 >     * size returns the correct values
660       */
661      public void testSize() {
662          ConcurrentSkipListMap map = map5();
663          ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
664 <        assertEquals(0, empty.size());
665 <        assertEquals(5, map.size());
664 >        assertEquals(0, empty.size());
665 >        assertEquals(5, map.size());
666      }
667  
668      /**
# Line 646 | Line 672 | public class ConcurrentSkipListMapTest e
672          ConcurrentSkipListMap map = map5();
673          String s = map.toString();
674          for (int i = 1; i <= 5; ++i) {
675 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
675 >            assertTrue(s.contains(String.valueOf(i)));
676          }
677 <    }        
677 >    }
678  
679      // Exception tests
680  
# Line 660 | Line 686 | public class ConcurrentSkipListMapTest e
686              ConcurrentSkipListMap c = map5();
687              c.get(null);
688              shouldThrow();
689 <        } catch(NullPointerException e){}
689 >        } catch (NullPointerException success) {}
690      }
691  
692      /**
# Line 671 | Line 697 | public class ConcurrentSkipListMapTest e
697              ConcurrentSkipListMap c = map5();
698              c.containsKey(null);
699              shouldThrow();
700 <        } catch(NullPointerException e){}
700 >        } catch (NullPointerException success) {}
701      }
702  
703      /**
# Line 682 | Line 708 | public class ConcurrentSkipListMapTest e
708              ConcurrentSkipListMap c = new ConcurrentSkipListMap();
709              c.containsValue(null);
710              shouldThrow();
711 <        } catch(NullPointerException e){}
711 >        } catch (NullPointerException success) {}
712      }
713  
688
714      /**
715       * put(null,x) throws NPE
716       */
# Line 694 | Line 719 | public class ConcurrentSkipListMapTest e
719              ConcurrentSkipListMap c = map5();
720              c.put(null, "whatever");
721              shouldThrow();
722 <        } catch(NullPointerException e){}
722 >        } catch (NullPointerException success) {}
723      }
724  
725      /**
# Line 705 | Line 730 | public class ConcurrentSkipListMapTest e
730              ConcurrentSkipListMap c = map5();
731              c.putIfAbsent(null, "whatever");
732              shouldThrow();
733 <        } catch(NullPointerException e){}
733 >        } catch (NullPointerException success) {}
734      }
735  
736      /**
# Line 716 | Line 741 | public class ConcurrentSkipListMapTest e
741              ConcurrentSkipListMap c = map5();
742              c.replace(null, "whatever");
743              shouldThrow();
744 <        } catch(NullPointerException e){}
744 >        } catch (NullPointerException success) {}
745      }
746  
747      /**
# Line 727 | Line 752 | public class ConcurrentSkipListMapTest e
752              ConcurrentSkipListMap c = map5();
753              c.replace(null, one, "whatever");
754              shouldThrow();
755 <        } catch(NullPointerException e){}
755 >        } catch (NullPointerException success) {}
756      }
757  
758      /**
# Line 739 | Line 764 | public class ConcurrentSkipListMapTest e
764              c.put("sadsdf", "asdads");
765              c.remove(null);
766              shouldThrow();
767 <        } catch(NullPointerException e){}
767 >        } catch (NullPointerException success) {}
768      }
769  
770      /**
# Line 751 | Line 776 | public class ConcurrentSkipListMapTest e
776              c.put("sadsdf", "asdads");
777              c.remove(null, "whatever");
778              shouldThrow();
779 <        } catch(NullPointerException e){}
779 >        } catch (NullPointerException success) {}
780      }
781  
782      /**
783       * remove(x, null) returns false
784       */
785      public void testRemove3() {
786 <        try {
787 <            ConcurrentSkipListMap c = new ConcurrentSkipListMap();
788 <            c.put("sadsdf", "asdads");
764 <            assertFalse(c.remove("sadsdf", null));
765 <        } catch(NullPointerException e){
766 <            fail();
767 <        }
786 >        ConcurrentSkipListMap c = new ConcurrentSkipListMap();
787 >        c.put("sadsdf", "asdads");
788 >        assertFalse(c.remove("sadsdf", null));
789      }
790  
791      /**
792       * A deserialized map equals original
793       */
794 <    public void testSerialization() {
795 <        ConcurrentSkipListMap q = map5();
796 <
797 <        try {
798 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
799 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
800 <            out.writeObject(q);
801 <            out.close();
802 <
782 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
783 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
784 <            ConcurrentSkipListMap r = (ConcurrentSkipListMap)in.readObject();
785 <            assertEquals(q.size(), r.size());
786 <            assertTrue(q.equals(r));
787 <            assertTrue(r.equals(q));
788 <        } catch(Exception e){
789 <            e.printStackTrace();
790 <            unexpectedException();
791 <        }
794 >    public void testSerialization() throws Exception {
795 >        NavigableMap x = map5();
796 >        NavigableMap y = serialClone(x);
797 >
798 >        assertTrue(x != y);
799 >        assertEquals(x.size(), y.size());
800 >        assertEquals(x.toString(), y.toString());
801 >        assertEquals(x, y);
802 >        assertEquals(y, x);
803      }
804  
794
795
805      /**
806       * subMap returns map with keys in requested range
807       */
808      public void testSubMapContents() {
809          ConcurrentSkipListMap map = map5();
810 <        NavigableMap sm = map.navigableSubMap(two, true, four, false);
810 >        NavigableMap sm = map.subMap(two, true, four, false);
811          assertEquals(two, sm.firstKey());
812          assertEquals(three, sm.lastKey());
813          assertEquals(2, sm.size());
# Line 820 | Line 829 | public class ConcurrentSkipListMapTest e
829          k = (Integer)(r.next());
830          assertEquals(two, k);
831          assertFalse(r.hasNext());
832 <        
832 >
833          Iterator j = sm.keySet().iterator();
834          j.next();
835          j.remove();
# Line 829 | Line 838 | public class ConcurrentSkipListMapTest e
838          assertEquals(1, sm.size());
839          assertEquals(three, sm.firstKey());
840          assertEquals(three, sm.lastKey());
841 <        assertTrue(sm.remove(three) != null);
841 >        assertEquals("C", sm.remove(three));
842          assertTrue(sm.isEmpty());
843          assertEquals(3, map.size());
844      }
845  
846      public void testSubMapContents2() {
847          ConcurrentSkipListMap map = map5();
848 <        NavigableMap sm = map.navigableSubMap(two, true, three, false);
848 >        NavigableMap sm = map.subMap(two, true, three, false);
849          assertEquals(1, sm.size());
850          assertEquals(two, sm.firstKey());
851          assertEquals(two, sm.lastKey());
# Line 854 | Line 863 | public class ConcurrentSkipListMapTest e
863          k = (Integer)(r.next());
864          assertEquals(two, k);
865          assertFalse(r.hasNext());
866 <        
866 >
867          Iterator j = sm.keySet().iterator();
868          j.next();
869          j.remove();
# Line 862 | Line 871 | public class ConcurrentSkipListMapTest e
871          assertEquals(4, map.size());
872          assertEquals(0, sm.size());
873          assertTrue(sm.isEmpty());
874 <        assertTrue(sm.remove(three) == null);
874 >        assertSame(sm.remove(three), null);
875          assertEquals(4, map.size());
876      }
877  
# Line 871 | Line 880 | public class ConcurrentSkipListMapTest e
880       */
881      public void testHeadMapContents() {
882          ConcurrentSkipListMap map = map5();
883 <        NavigableMap sm = map.navigableHeadMap(four, false);
883 >        NavigableMap sm = map.headMap(four, false);
884          assertTrue(sm.containsKey(one));
885          assertTrue(sm.containsKey(two));
886          assertTrue(sm.containsKey(three));
# Line 897 | Line 906 | public class ConcurrentSkipListMapTest e
906       */
907      public void testTailMapContents() {
908          ConcurrentSkipListMap map = map5();
909 <        NavigableMap sm = map.navigableTailMap(two, true);
909 >        NavigableMap sm = map.tailMap(two, true);
910          assertFalse(sm.containsKey(one));
911          assertTrue(sm.containsKey(two));
912          assertTrue(sm.containsKey(three));
# Line 941 | Line 950 | public class ConcurrentSkipListMapTest e
950          assertEquals("E", e.getValue());
951          assertFalse(i.hasNext());
952  
953 <        NavigableMap ssm = sm.navigableTailMap(four, true);
953 >        NavigableMap ssm = sm.tailMap(four, true);
954          assertEquals(four, ssm.firstKey());
955          assertEquals(five, ssm.lastKey());
956 <        assertTrue(ssm.remove(four) != null);
956 >        assertEquals("D", ssm.remove(four));
957          assertEquals(1, ssm.size());
958          assertEquals(3, sm.size());
959          assertEquals(4, map.size());
# Line 956 | Line 965 | public class ConcurrentSkipListMapTest e
965      /**
966       * Submaps of submaps subdivide correctly
967       */
968 <    public void testRecursiveSubMaps() {
969 <        int mapSize = 1000;
970 <        Class cl = ConcurrentSkipListMap.class;
968 >    public void testRecursiveSubMaps() throws Exception {
969 >        int mapSize = expensiveTests ? 1000 : 100;
970 >        Class cl = ConcurrentSkipListMap.class;
971          NavigableMap<Integer, Integer> map = newMap(cl);
972          bs = new BitSet(mapSize);
973  
# Line 970 | Line 979 | public class ConcurrentSkipListMapTest e
979          check(map,                 0, mapSize - 1, true);
980          check(map.descendingMap(), 0, mapSize - 1, false);
981  
982 <        bashSubMap(map.navigableSubMap(0, true, mapSize, false),
982 >        bashSubMap(map.subMap(0, true, mapSize, false),
983                     0, mapSize - 1, true);
984      }
985  
986 <    static NavigableMap<Integer, Integer> newMap(Class cl) {
987 <        NavigableMap<Integer, Integer> result = null;
988 <        try {
989 <            result = (NavigableMap<Integer, Integer>) cl.newInstance();
981 <        } catch(Exception e) {
982 <            fail();
983 <        }
984 <        assertEquals(result.size(), 0);
986 >    static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception {
987 >        NavigableMap<Integer, Integer> result =
988 >            (NavigableMap<Integer, Integer>) cl.newInstance();
989 >        assertEquals(0, result.size());
990          assertFalse(result.keySet().iterator().hasNext());
991          return result;
992      }
# Line 1003 | Line 1008 | public class ConcurrentSkipListMapTest e
1008          }
1009  
1010          // Remove a bunch of entries with iterator
1011 <        for(Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1011 >        for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1012              if (rnd.nextBoolean()) {
1013                  bs.clear(it.next());
1014                  it.remove();
# Line 1028 | Line 1033 | public class ConcurrentSkipListMapTest e
1033          }
1034  
1035          // Remove a bunch of entries with iterator
1036 <        for(Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1036 >        for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1037              if (rnd.nextBoolean()) {
1038                  bs.clear(it.next());
1039                  it.remove();
# Line 1043 | Line 1048 | public class ConcurrentSkipListMapTest e
1048              } else {
1049                  try {
1050                      map.put(key, 2 * key);
1051 <                    fail();
1052 <                } catch(IllegalArgumentException e) {
1048 <                    // expected
1049 <                }
1051 >                    shouldThrow();
1052 >                } catch (IllegalArgumentException success) {}
1053              }
1054          }
1055      }
# Line 1077 | Line 1080 | public class ConcurrentSkipListMapTest e
1080  
1081          // headMap - pick direction and endpoint inclusion randomly
1082          boolean incl = rnd.nextBoolean();
1083 <        NavigableMap<Integer,Integer> hm = map.navigableHeadMap(midPoint, incl);
1083 >        NavigableMap<Integer,Integer> hm = map.headMap(midPoint, incl);
1084          if (ascending) {
1085              if (rnd.nextBoolean())
1086                  bashSubMap(hm, min, midPoint - (incl ? 0 : 1), true);
# Line 1094 | Line 1097 | public class ConcurrentSkipListMapTest e
1097  
1098          // tailMap - pick direction and endpoint inclusion randomly
1099          incl = rnd.nextBoolean();
1100 <        NavigableMap<Integer,Integer> tm = map.navigableTailMap(midPoint,incl);
1100 >        NavigableMap<Integer,Integer> tm = map.tailMap(midPoint,incl);
1101          if (ascending) {
1102              if (rnd.nextBoolean())
1103                  bashSubMap(tm, midPoint + (incl ? 0 : 1), max, true);
# Line 1119 | Line 1122 | public class ConcurrentSkipListMapTest e
1122          boolean lowIncl = rnd.nextBoolean();
1123          boolean highIncl = rnd.nextBoolean();
1124          if (ascending) {
1125 <            NavigableMap<Integer,Integer> sm = map.navigableSubMap(
1125 >            NavigableMap<Integer,Integer> sm = map.subMap(
1126                  endpoints[0], lowIncl, endpoints[1], highIncl);
1127              if (rnd.nextBoolean())
1128                  bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1),
# Line 1128 | Line 1131 | public class ConcurrentSkipListMapTest e
1131                  bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1),
1132                             endpoints[1] - (highIncl ? 0 : 1), false);
1133          } else {
1134 <            NavigableMap<Integer,Integer> sm = map.navigableSubMap(
1134 >            NavigableMap<Integer,Integer> sm = map.subMap(
1135                  endpoints[1], highIncl, endpoints[0], lowIncl);
1136              if (rnd.nextBoolean())
1137                  bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1),
# Line 1144 | Line 1147 | public class ConcurrentSkipListMapTest e
1147       */
1148      void check(NavigableMap<Integer, Integer> map,
1149                        final int min, final int max, final boolean ascending) {
1150 <       class ReferenceSet {
1150 >        class ReferenceSet {
1151              int lower(int key) {
1152                  return ascending ? lowerAscending(key) : higherAscending(key);
1153              }
# Line 1175 | Line 1178 | public class ConcurrentSkipListMapTest e
1178                  // BitSet should support this! Test would run much faster
1179                  while (key >= min) {
1180                      if (bs.get(key))
1181 <                        return(key);
1181 >                        return key;
1182                      key--;
1183                  }
1184                  return -1;
# Line 1210 | Line 1213 | public class ConcurrentSkipListMapTest e
1213              if (bsContainsI)
1214                  size++;
1215          }
1216 <        assertEquals(map.size(), size);
1216 >        assertEquals(size, map.size());
1217  
1218          // Test contents using contains keySet iterator
1219          int size2 = 0;
# Line 1241 | Line 1244 | public class ConcurrentSkipListMapTest e
1244              assertEq(rs.last(),  -1);
1245              try {
1246                  map.firstKey();
1247 <                fail();
1248 <            } catch(NoSuchElementException e) {
1246 <                // expected
1247 <            }
1247 >                shouldThrow();
1248 >            } catch (NoSuchElementException success) {}
1249              try {
1250                  map.lastKey();
1251 <                fail();
1252 <            } catch(NoSuchElementException e) {
1252 <                // expected
1253 <            }
1251 >                shouldThrow();
1252 >            } catch (NoSuchElementException success) {}
1253          }
1254      }
1255  
# Line 1264 | Line 1263 | public class ConcurrentSkipListMapTest e
1263      static boolean eq(Integer i, int j) {
1264          return i == null ? j == -1 : i == j;
1265      }
1266 <    
1266 >
1267   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines