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.35 by jsr166, Sat Feb 28 18:30:05 2015 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.*;
7 > import java.util.ArrayList;
8 > import java.util.Arrays;
9 > import java.util.BitSet;
10 > import java.util.Collection;
11 > import java.util.Iterator;
12 > import java.util.Map;
13 > import java.util.NavigableMap;
14 > import java.util.NavigableSet;
15 > import java.util.NoSuchElementException;
16 > import java.util.Random;
17 > import java.util.Set;
18 > import java.util.concurrent.ConcurrentSkipListMap;
19 >
20 > import junit.framework.Test;
21 > import junit.framework.TestSuite;
22  
23   public class ConcurrentSkipListMapTest extends JSR166TestCase {
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run (suite());  
25 >        junit.textui.TestRunner.run(suite());
26      }
27      public static Test suite() {
28 <        return new TestSuite(ConcurrentSkipListMapTest.class);
28 >        return new TestSuite(ConcurrentSkipListMapTest.class);
29      }
30  
31      /**
32 <     * Create a map from Integers 1-5 to Strings "A"-"E".
32 >     * Returns a new map from Integers 1-5 to Strings "A"-"E".
33       */
34 <    private static ConcurrentSkipListMap map5() {  
35 <        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
34 >    private static ConcurrentSkipListMap map5() {
35 >        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
36          assertTrue(map.isEmpty());
37 <        map.put(one, "A");
38 <        map.put(five, "E");
39 <        map.put(three, "C");
40 <        map.put(two, "B");
41 <        map.put(four, "D");
37 >        map.put(one, "A");
38 >        map.put(five, "E");
39 >        map.put(three, "C");
40 >        map.put(two, "B");
41 >        map.put(four, "D");
42          assertFalse(map.isEmpty());
43          assertEquals(5, map.size());
44 <        return map;
44 >        return map;
45      }
46  
47      /**
48 <     *  clear removes all pairs
48 >     * clear removes all pairs
49       */
50      public void testClear() {
51          ConcurrentSkipListMap map = map5();
52 <        map.clear();
53 <        assertEquals(map.size(), 0);
52 >        map.clear();
53 >        assertEquals(0, map.size());
54      }
55  
56      /**
57 <     *  
57 >     * copy constructor creates map equal to source map
58       */
59      public void testConstructFromSorted() {
60          ConcurrentSkipListMap map = map5();
# Line 52 | Line 63 | public class ConcurrentSkipListMapTest e
63      }
64  
65      /**
66 <     *  Maps with same contents are equal
66 >     * Maps with same contents are equal
67       */
68      public void testEquals() {
69          ConcurrentSkipListMap map1 = map5();
70          ConcurrentSkipListMap map2 = map5();
71          assertEquals(map1, map2);
72          assertEquals(map2, map1);
73 <        map1.clear();
73 >        map1.clear();
74          assertFalse(map1.equals(map2));
75          assertFalse(map2.equals(map1));
76      }
77  
78      /**
79 <     *  containsKey returns true for contained key
79 >     * containsKey returns true for contained key
80       */
81      public void testContainsKey() {
82          ConcurrentSkipListMap map = map5();
83 <        assertTrue(map.containsKey(one));
83 >        assertTrue(map.containsKey(one));
84          assertFalse(map.containsKey(zero));
85      }
86  
87      /**
88 <     *  containsValue returns true for held values
88 >     * containsValue returns true for held values
89       */
90      public void testContainsValue() {
91          ConcurrentSkipListMap map = map5();
92 <        assertTrue(map.containsValue("A"));
92 >        assertTrue(map.containsValue("A"));
93          assertFalse(map.containsValue("Z"));
94      }
95  
96      /**
97 <     *  get returns the correct element at the given key,
98 <     *  or null if not present
97 >     * get returns the correct element at the given key,
98 >     * or null if not present
99       */
100      public void testGet() {
101          ConcurrentSkipListMap map = map5();
102 <        assertEquals("A", (String)map.get(one));
102 >        assertEquals("A", (String)map.get(one));
103          ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
104          assertNull(empty.get(one));
105      }
106  
107      /**
108 <     *  isEmpty is true of empty map and false for non-empty
108 >     * isEmpty is true of empty map and false for non-empty
109       */
110      public void testIsEmpty() {
111          ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
112          ConcurrentSkipListMap map = map5();
113 <        assertTrue(empty.isEmpty());
113 >        assertTrue(empty.isEmpty());
114          assertFalse(map.isEmpty());
115      }
116  
117      /**
118 <     *   firstKey returns first key
118 >     * firstKey returns first key
119       */
120      public void testFirstKey() {
121          ConcurrentSkipListMap map = map5();
122 <        assertEquals(one, map.firstKey());
122 >        assertEquals(one, map.firstKey());
123      }
124  
125      /**
126 <     *   lastKey returns last key
126 >     * lastKey returns last key
127       */
128      public void testLastKey() {
129          ConcurrentSkipListMap map = map5();
130 <        assertEquals(five, map.lastKey());
130 >        assertEquals(five, map.lastKey());
131      }
132  
122
133      /**
134 <     *  keySet.toArray returns contains all keys
134 >     * keySet.toArray returns contains all keys
135       */
136      public void testKeySetToArray() {
137          ConcurrentSkipListMap map = map5();
138 <        Set s = map.keySet();
138 >        Set s = map.keySet();
139          Object[] ar = s.toArray();
140          assertTrue(s.containsAll(Arrays.asList(ar)));
141 <        assertEquals(5, ar.length);
141 >        assertEquals(5, ar.length);
142          ar[0] = m10;
143          assertFalse(s.containsAll(Arrays.asList(ar)));
144      }
145  
146      /**
147 <     *  descendingkeySet.toArray returns contains all keys
147 >     * descendingkeySet.toArray returns contains all keys
148       */
149      public void testDescendingKeySetToArray() {
150          ConcurrentSkipListMap map = map5();
151 <        Set s = map.descendingKeySet();
151 >        Set s = map.descendingKeySet();
152          Object[] ar = s.toArray();
153 <        assertEquals(5, ar.length);
153 >        assertEquals(5, ar.length);
154          assertTrue(s.containsAll(Arrays.asList(ar)));
155          ar[0] = m10;
156          assertFalse(s.containsAll(Arrays.asList(ar)));
157      }
158  
159      /**
160 <     *   keySet returns a Set containing all the keys
160 >     * keySet returns a Set containing all the keys
161       */
162      public void testKeySet() {
163          ConcurrentSkipListMap map = map5();
164 <        Set s = map.keySet();
165 <        assertEquals(5, s.size());
166 <        assertTrue(s.contains(one));
167 <        assertTrue(s.contains(two));
168 <        assertTrue(s.contains(three));
169 <        assertTrue(s.contains(four));
170 <        assertTrue(s.contains(five));
164 >        Set s = map.keySet();
165 >        assertEquals(5, s.size());
166 >        assertTrue(s.contains(one));
167 >        assertTrue(s.contains(two));
168 >        assertTrue(s.contains(three));
169 >        assertTrue(s.contains(four));
170 >        assertTrue(s.contains(five));
171      }
172  
173      /**
174 <     *   keySet is ordered
174 >     * keySet is ordered
175       */
176      public void testKeySetOrder() {
177          ConcurrentSkipListMap map = map5();
178 <        Set s = map.keySet();
178 >        Set s = map.keySet();
179          Iterator i = s.iterator();
180          Integer last = (Integer)i.next();
181          assertEquals(last, one);
182 +        int count = 1;
183          while (i.hasNext()) {
184              Integer k = (Integer)i.next();
185              assertTrue(last.compareTo(k) < 0);
186              last = k;
187 +            ++count;
188 +        }
189 +        assertEquals(5, count);
190 +    }
191 +
192 +    /**
193 +     * descending iterator of key set is inverse ordered
194 +     */
195 +    public void testKeySetDescendingIteratorOrder() {
196 +        ConcurrentSkipListMap map = map5();
197 +        NavigableSet s = map.navigableKeySet();
198 +        Iterator i = s.descendingIterator();
199 +        Integer last = (Integer)i.next();
200 +        assertEquals(last, five);
201 +        int count = 1;
202 +        while (i.hasNext()) {
203 +            Integer k = (Integer)i.next();
204 +            assertTrue(last.compareTo(k) > 0);
205 +            last = k;
206 +            ++count;
207          }
208 +        assertEquals(5, count);
209      }
210  
211      /**
212 <     *   descendingKeySet is ordered
212 >     * descendingKeySet is ordered
213       */
214      public void testDescendingKeySetOrder() {
215          ConcurrentSkipListMap map = map5();
216 <        Set s = map.descendingKeySet();
216 >        Set s = map.descendingKeySet();
217          Iterator i = s.iterator();
218          Integer last = (Integer)i.next();
219          assertEquals(last, five);
220 +        int count = 1;
221          while (i.hasNext()) {
222              Integer k = (Integer)i.next();
223              assertTrue(last.compareTo(k) > 0);
224              last = k;
225 +            ++count;
226          }
227 +        assertEquals(5, count);
228      }
229  
230 +    /**
231 +     * descending iterator of descendingKeySet is ordered
232 +     */
233 +    public void testDescendingKeySetDescendingIteratorOrder() {
234 +        ConcurrentSkipListMap map = map5();
235 +        NavigableSet s = map.descendingKeySet();
236 +        Iterator i = s.descendingIterator();
237 +        Integer last = (Integer)i.next();
238 +        assertEquals(last, one);
239 +        int count = 1;
240 +        while (i.hasNext()) {
241 +            Integer k = (Integer)i.next();
242 +            assertTrue(last.compareTo(k) < 0);
243 +            last = k;
244 +            ++count;
245 +        }
246 +        assertEquals(5, count);
247 +    }
248  
249      /**
250 <     *  Values.toArray contains all values
250 >     * Values.toArray contains all values
251       */
252      public void testValuesToArray() {
253          ConcurrentSkipListMap map = map5();
254 <        Collection v = map.values();
254 >        Collection v = map.values();
255          Object[] ar = v.toArray();
256          ArrayList s = new ArrayList(Arrays.asList(ar));
257 <        assertEquals(5, ar.length);
258 <        assertTrue(s.contains("A"));
259 <        assertTrue(s.contains("B"));
260 <        assertTrue(s.contains("C"));
261 <        assertTrue(s.contains("D"));
262 <        assertTrue(s.contains("E"));
257 >        assertEquals(5, ar.length);
258 >        assertTrue(s.contains("A"));
259 >        assertTrue(s.contains("B"));
260 >        assertTrue(s.contains("C"));
261 >        assertTrue(s.contains("D"));
262 >        assertTrue(s.contains("E"));
263      }
264  
265      /**
# Line 214 | Line 267 | public class ConcurrentSkipListMapTest e
267       */
268      public void testValues() {
269          ConcurrentSkipListMap map = map5();
270 <        Collection s = map.values();
271 <        assertEquals(5, s.size());
272 <        assertTrue(s.contains("A"));
273 <        assertTrue(s.contains("B"));
274 <        assertTrue(s.contains("C"));
275 <        assertTrue(s.contains("D"));
276 <        assertTrue(s.contains("E"));
270 >        Collection s = map.values();
271 >        assertEquals(5, s.size());
272 >        assertTrue(s.contains("A"));
273 >        assertTrue(s.contains("B"));
274 >        assertTrue(s.contains("C"));
275 >        assertTrue(s.contains("D"));
276 >        assertTrue(s.contains("E"));
277      }
278  
279      /**
# Line 228 | Line 281 | public class ConcurrentSkipListMapTest e
281       */
282      public void testEntrySet() {
283          ConcurrentSkipListMap map = map5();
284 <        Set s = map.entrySet();
285 <        assertEquals(5, s.size());
284 >        Set s = map.entrySet();
285 >        assertEquals(5, s.size());
286          Iterator it = s.iterator();
287          while (it.hasNext()) {
288              Map.Entry e = (Map.Entry) it.next();
289 <            assertTrue(
289 >            assertTrue(
290                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
291                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
292                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 247 | Line 300 | public class ConcurrentSkipListMapTest e
300       */
301      public void testDescendingEntrySet() {
302          ConcurrentSkipListMap map = map5();
303 <        Set s = map.descendingMap().entrySet();
304 <        assertEquals(5, s.size());
303 >        Set s = map.descendingMap().entrySet();
304 >        assertEquals(5, s.size());
305          Iterator it = s.iterator();
306          while (it.hasNext()) {
307              Map.Entry e = (Map.Entry) it.next();
308 <            assertTrue(
308 >            assertTrue(
309                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
310                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
311                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 262 | Line 315 | public class ConcurrentSkipListMapTest e
315      }
316  
317      /**
318 <     *  entrySet.toArray contains all entries
318 >     * entrySet.toArray contains all entries
319       */
320      public void testEntrySetToArray() {
321          ConcurrentSkipListMap map = map5();
322 <        Set s = map.entrySet();
322 >        Set s = map.entrySet();
323          Object[] ar = s.toArray();
324          assertEquals(5, ar.length);
325          for (int i = 0; i < 5; ++i) {
# Line 276 | Line 329 | public class ConcurrentSkipListMapTest e
329      }
330  
331      /**
332 <     *  descendingEntrySet.toArray contains all entries
332 >     * descendingEntrySet.toArray contains all entries
333       */
334      public void testDescendingEntrySetToArray() {
335          ConcurrentSkipListMap map = map5();
336 <        Set s = map.descendingMap().entrySet();
336 >        Set s = map.descendingMap().entrySet();
337          Object[] ar = s.toArray();
338          assertEquals(5, ar.length);
339          for (int i = 0; i < 5; ++i) {
# Line 290 | Line 343 | public class ConcurrentSkipListMapTest e
343      }
344  
345      /**
346 <     *   putAll  adds all key-value pairs from the given map
346 >     * putAll adds all key-value pairs from the given map
347       */
348      public void testPutAll() {
349          ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
350          ConcurrentSkipListMap map = map5();
351 <        empty.putAll(map);
352 <        assertEquals(5, empty.size());
353 <        assertTrue(empty.containsKey(one));
354 <        assertTrue(empty.containsKey(two));
355 <        assertTrue(empty.containsKey(three));
356 <        assertTrue(empty.containsKey(four));
357 <        assertTrue(empty.containsKey(five));
351 >        empty.putAll(map);
352 >        assertEquals(5, empty.size());
353 >        assertTrue(empty.containsKey(one));
354 >        assertTrue(empty.containsKey(two));
355 >        assertTrue(empty.containsKey(three));
356 >        assertTrue(empty.containsKey(four));
357 >        assertTrue(empty.containsKey(five));
358      }
359  
360      /**
361 <     *   putIfAbsent works when the given key is not present
361 >     * putIfAbsent works when the given key is not present
362       */
363      public void testPutIfAbsent() {
364          ConcurrentSkipListMap map = map5();
365 <        map.putIfAbsent(six, "Z");
365 >        map.putIfAbsent(six, "Z");
366          assertTrue(map.containsKey(six));
367      }
368  
369      /**
370 <     *   putIfAbsent does not add the pair if the key is already present
370 >     * putIfAbsent does not add the pair if the key is already present
371       */
372      public void testPutIfAbsent2() {
373          ConcurrentSkipListMap map = map5();
# Line 322 | Line 375 | public class ConcurrentSkipListMapTest e
375      }
376  
377      /**
378 <     *   replace fails when the given key is not present
378 >     * replace fails when the given key is not present
379       */
380      public void testReplace() {
381          ConcurrentSkipListMap map = map5();
382 <        assertNull(map.replace(six, "Z"));
382 >        assertNull(map.replace(six, "Z"));
383          assertFalse(map.containsKey(six));
384      }
385  
386      /**
387 <     *   replace succeeds if the key is already present
387 >     * replace succeeds if the key is already present
388       */
389      public void testReplace2() {
390          ConcurrentSkipListMap map = map5();
# Line 339 | Line 392 | public class ConcurrentSkipListMapTest e
392          assertEquals("Z", map.get(one));
393      }
394  
342
395      /**
396       * replace value fails when the given key not mapped to expected value
397       */
398      public void testReplaceValue() {
399          ConcurrentSkipListMap map = map5();
400          assertEquals("A", map.get(one));
401 <        assertFalse(map.replace(one, "Z", "Z"));
401 >        assertFalse(map.replace(one, "Z", "Z"));
402          assertEquals("A", map.get(one));
403      }
404  
# Line 356 | Line 408 | public class ConcurrentSkipListMapTest e
408      public void testReplaceValue2() {
409          ConcurrentSkipListMap map = map5();
410          assertEquals("A", map.get(one));
411 <        assertTrue(map.replace(one, "A", "Z"));
411 >        assertTrue(map.replace(one, "A", "Z"));
412          assertEquals("Z", map.get(one));
413      }
414  
363
415      /**
416 <     *   remove removes the correct key-value pair from the map
416 >     * remove removes the correct key-value pair from the map
417       */
418      public void testRemove() {
419          ConcurrentSkipListMap map = map5();
420 <        map.remove(five);
421 <        assertEquals(4, map.size());
422 <        assertFalse(map.containsKey(five));
420 >        map.remove(five);
421 >        assertEquals(4, map.size());
422 >        assertFalse(map.containsKey(five));
423      }
424  
425      /**
# Line 376 | Line 427 | public class ConcurrentSkipListMapTest e
427       */
428      public void testRemove2() {
429          ConcurrentSkipListMap map = map5();
430 <        assertTrue(map.containsKey(five));
430 >        assertTrue(map.containsKey(five));
431          assertEquals("E", map.get(five));
432 <        map.remove(five, "E");
433 <        assertEquals(4, map.size());
434 <        assertFalse(map.containsKey(five));
435 <        map.remove(four, "A");
436 <        assertEquals(4, map.size());
437 <        assertTrue(map.containsKey(four));
387 <
432 >        map.remove(five, "E");
433 >        assertEquals(4, map.size());
434 >        assertFalse(map.containsKey(five));
435 >        map.remove(four, "A");
436 >        assertEquals(4, map.size());
437 >        assertTrue(map.containsKey(four));
438      }
439  
440      /**
# Line 403 | Line 453 | public class ConcurrentSkipListMapTest e
453  
454          Map.Entry e4 = map.lowerEntry(zero);
455          assertNull(e4);
406
456      }
457  
458      /**
# Line 422 | Line 471 | public class ConcurrentSkipListMapTest e
471  
472          Map.Entry e4 = map.higherEntry(six);
473          assertNull(e4);
425
474      }
475  
476      /**
# Line 441 | Line 489 | public class ConcurrentSkipListMapTest e
489  
490          Map.Entry e4 = map.floorEntry(zero);
491          assertNull(e4);
444
492      }
493  
494      /**
# Line 460 | Line 507 | public class ConcurrentSkipListMapTest e
507  
508          Map.Entry e4 = map.ceilingEntry(six);
509          assertNull(e4);
463
510      }
511  
512      /**
513       * lowerEntry, higherEntry, ceilingEntry, and floorEntry return
514 <     * imutable entries
514 >     * immutable entries
515       */
516 <    public void testEntryImmutablity() {
516 >    public void testEntryImmutability() {
517          ConcurrentSkipListMap map = map5();
518          Map.Entry e = map.lowerEntry(three);
519          assertEquals(two, e.getKey());
520          try {
521              e.setValue("X");
522 <            fail();
523 <        } catch(UnsupportedOperationException success) {}
522 >            shouldThrow();
523 >        } catch (UnsupportedOperationException success) {}
524          e = map.higherEntry(zero);
525          assertEquals(one, e.getKey());
526          try {
527              e.setValue("X");
528 <            fail();
529 <        } catch(UnsupportedOperationException success) {}
528 >            shouldThrow();
529 >        } catch (UnsupportedOperationException success) {}
530          e = map.floorEntry(one);
531          assertEquals(one, e.getKey());
532          try {
533              e.setValue("X");
534 <            fail();
535 <        } catch(UnsupportedOperationException success) {}
534 >            shouldThrow();
535 >        } catch (UnsupportedOperationException success) {}
536          e = map.ceilingEntry(five);
537          assertEquals(five, e.getKey());
538          try {
539              e.setValue("X");
540 <            fail();
541 <        } catch(UnsupportedOperationException success) {}
540 >            shouldThrow();
541 >        } catch (UnsupportedOperationException success) {}
542      }
543  
498
499
544      /**
545       * lowerKey returns preceding element
546       */
# Line 513 | Line 557 | public class ConcurrentSkipListMapTest e
557  
558          Object e4 = q.lowerKey(zero);
559          assertNull(e4);
516
560      }
561  
562      /**
# Line 532 | Line 575 | public class ConcurrentSkipListMapTest e
575  
576          Object e4 = q.higherKey(six);
577          assertNull(e4);
535
578      }
579  
580      /**
# Line 551 | Line 593 | public class ConcurrentSkipListMapTest e
593  
594          Object e4 = q.floorKey(zero);
595          assertNull(e4);
554
596      }
597  
598      /**
# Line 570 | Line 611 | public class ConcurrentSkipListMapTest e
611  
612          Object e4 = q.ceilingKey(six);
613          assertNull(e4);
573
614      }
615  
616      /**
# Line 595 | Line 635 | public class ConcurrentSkipListMapTest e
635          try {
636              e.setValue("A");
637              shouldThrow();
638 <        } catch (Exception ok) {
599 <        }
638 >        } catch (UnsupportedOperationException success) {}
639          e = map.pollFirstEntry();
640          assertNull(e);
641      }
# Line 623 | Line 662 | public class ConcurrentSkipListMapTest e
662          try {
663              e.setValue("E");
664              shouldThrow();
665 <        } catch (Exception ok) {
627 <        }
665 >        } catch (UnsupportedOperationException success) {}
666          e = map.pollLastEntry();
667          assertNull(e);
668      }
669  
670      /**
671 <     *   size returns the correct values
671 >     * size returns the correct values
672       */
673      public void testSize() {
674          ConcurrentSkipListMap map = map5();
675          ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
676 <        assertEquals(0, empty.size());
677 <        assertEquals(5, map.size());
676 >        assertEquals(0, empty.size());
677 >        assertEquals(5, map.size());
678      }
679  
680      /**
# Line 646 | Line 684 | public class ConcurrentSkipListMapTest e
684          ConcurrentSkipListMap map = map5();
685          String s = map.toString();
686          for (int i = 1; i <= 5; ++i) {
687 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
687 >            assertTrue(s.contains(String.valueOf(i)));
688          }
689 <    }        
689 >    }
690  
691      // Exception tests
692  
# Line 656 | Line 694 | public class ConcurrentSkipListMapTest e
694       * get(null) of nonempty map throws NPE
695       */
696      public void testGet_NullPointerException() {
697 +        ConcurrentSkipListMap c = map5();
698          try {
660            ConcurrentSkipListMap c = map5();
699              c.get(null);
700              shouldThrow();
701 <        } catch(NullPointerException e){}
701 >        } catch (NullPointerException success) {}
702      }
703  
704      /**
705       * containsKey(null) of nonempty map throws NPE
706       */
707      public void testContainsKey_NullPointerException() {
708 +        ConcurrentSkipListMap c = map5();
709          try {
671            ConcurrentSkipListMap c = map5();
710              c.containsKey(null);
711              shouldThrow();
712 <        } catch(NullPointerException e){}
712 >        } catch (NullPointerException success) {}
713      }
714  
715      /**
716       * containsValue(null) throws NPE
717       */
718      public void testContainsValue_NullPointerException() {
719 +        ConcurrentSkipListMap c = new ConcurrentSkipListMap();
720          try {
682            ConcurrentSkipListMap c = new ConcurrentSkipListMap();
721              c.containsValue(null);
722              shouldThrow();
723 <        } catch(NullPointerException e){}
723 >        } catch (NullPointerException success) {}
724      }
725  
688
726      /**
727       * put(null,x) throws NPE
728       */
729      public void testPut1_NullPointerException() {
730 +        ConcurrentSkipListMap c = map5();
731          try {
694            ConcurrentSkipListMap c = map5();
732              c.put(null, "whatever");
733              shouldThrow();
734 <        } catch(NullPointerException e){}
734 >        } catch (NullPointerException success) {}
735      }
736  
737      /**
738       * putIfAbsent(null, x) throws NPE
739       */
740      public void testPutIfAbsent1_NullPointerException() {
741 +        ConcurrentSkipListMap c = map5();
742          try {
705            ConcurrentSkipListMap c = map5();
743              c.putIfAbsent(null, "whatever");
744              shouldThrow();
745 <        } catch(NullPointerException e){}
745 >        } catch (NullPointerException success) {}
746      }
747  
748      /**
749       * replace(null, x) throws NPE
750       */
751      public void testReplace_NullPointerException() {
752 +        ConcurrentSkipListMap c = map5();
753          try {
716            ConcurrentSkipListMap c = map5();
754              c.replace(null, "whatever");
755              shouldThrow();
756 <        } catch(NullPointerException e){}
756 >        } catch (NullPointerException success) {}
757      }
758  
759      /**
760       * replace(null, x, y) throws NPE
761       */
762      public void testReplaceValue_NullPointerException() {
763 +        ConcurrentSkipListMap c = map5();
764          try {
727            ConcurrentSkipListMap c = map5();
765              c.replace(null, one, "whatever");
766              shouldThrow();
767 <        } catch(NullPointerException e){}
767 >        } catch (NullPointerException success) {}
768      }
769  
770      /**
771       * remove(null) throws NPE
772       */
773      public void testRemove1_NullPointerException() {
774 +        ConcurrentSkipListMap c = new ConcurrentSkipListMap();
775 +        c.put("sadsdf", "asdads");
776          try {
738            ConcurrentSkipListMap c = new ConcurrentSkipListMap();
739            c.put("sadsdf", "asdads");
777              c.remove(null);
778              shouldThrow();
779 <        } catch(NullPointerException e){}
779 >        } catch (NullPointerException success) {}
780      }
781  
782      /**
783       * remove(null, x) throws NPE
784       */
785      public void testRemove2_NullPointerException() {
786 +        ConcurrentSkipListMap c = new ConcurrentSkipListMap();
787 +        c.put("sadsdf", "asdads");
788          try {
750            ConcurrentSkipListMap c = new ConcurrentSkipListMap();
751            c.put("sadsdf", "asdads");
789              c.remove(null, "whatever");
790              shouldThrow();
791 <        } catch(NullPointerException e){}
791 >        } catch (NullPointerException success) {}
792      }
793  
794      /**
795       * remove(x, null) returns false
796       */
797      public void testRemove3() {
798 <        try {
799 <            ConcurrentSkipListMap c = new ConcurrentSkipListMap();
800 <            c.put("sadsdf", "asdads");
764 <            assertFalse(c.remove("sadsdf", null));
765 <        } catch(NullPointerException e){
766 <            fail();
767 <        }
798 >        ConcurrentSkipListMap c = new ConcurrentSkipListMap();
799 >        c.put("sadsdf", "asdads");
800 >        assertFalse(c.remove("sadsdf", null));
801      }
802  
803      /**
804       * A deserialized map equals original
805       */
806 <    public void testSerialization() {
807 <        ConcurrentSkipListMap q = map5();
808 <
809 <        try {
810 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
811 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
812 <            out.writeObject(q);
813 <            out.close();
814 <
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 <        }
806 >    public void testSerialization() throws Exception {
807 >        NavigableMap x = map5();
808 >        NavigableMap y = serialClone(x);
809 >
810 >        assertNotSame(x, y);
811 >        assertEquals(x.size(), y.size());
812 >        assertEquals(x.toString(), y.toString());
813 >        assertEquals(x, y);
814 >        assertEquals(y, x);
815      }
816  
794
795
817      /**
818       * subMap returns map with keys in requested range
819       */
820      public void testSubMapContents() {
821          ConcurrentSkipListMap map = map5();
822 <        NavigableMap sm = map.navigableSubMap(two, true, four, false);
822 >        NavigableMap sm = map.subMap(two, true, four, false);
823          assertEquals(two, sm.firstKey());
824          assertEquals(three, sm.lastKey());
825          assertEquals(2, sm.size());
# Line 820 | Line 841 | public class ConcurrentSkipListMapTest e
841          k = (Integer)(r.next());
842          assertEquals(two, k);
843          assertFalse(r.hasNext());
844 <        
844 >
845          Iterator j = sm.keySet().iterator();
846          j.next();
847          j.remove();
# Line 829 | Line 850 | public class ConcurrentSkipListMapTest e
850          assertEquals(1, sm.size());
851          assertEquals(three, sm.firstKey());
852          assertEquals(three, sm.lastKey());
853 <        assertTrue(sm.remove(three) != null);
853 >        assertEquals("C", sm.remove(three));
854          assertTrue(sm.isEmpty());
855          assertEquals(3, map.size());
856      }
857  
858      public void testSubMapContents2() {
859          ConcurrentSkipListMap map = map5();
860 <        NavigableMap sm = map.navigableSubMap(two, true, three, false);
860 >        NavigableMap sm = map.subMap(two, true, three, false);
861          assertEquals(1, sm.size());
862          assertEquals(two, sm.firstKey());
863          assertEquals(two, sm.lastKey());
# Line 854 | Line 875 | public class ConcurrentSkipListMapTest e
875          k = (Integer)(r.next());
876          assertEquals(two, k);
877          assertFalse(r.hasNext());
878 <        
878 >
879          Iterator j = sm.keySet().iterator();
880          j.next();
881          j.remove();
# Line 862 | Line 883 | public class ConcurrentSkipListMapTest e
883          assertEquals(4, map.size());
884          assertEquals(0, sm.size());
885          assertTrue(sm.isEmpty());
886 <        assertTrue(sm.remove(three) == null);
886 >        assertSame(sm.remove(three), null);
887          assertEquals(4, map.size());
888      }
889  
# Line 871 | Line 892 | public class ConcurrentSkipListMapTest e
892       */
893      public void testHeadMapContents() {
894          ConcurrentSkipListMap map = map5();
895 <        NavigableMap sm = map.navigableHeadMap(four, false);
895 >        NavigableMap sm = map.headMap(four, false);
896          assertTrue(sm.containsKey(one));
897          assertTrue(sm.containsKey(two));
898          assertTrue(sm.containsKey(three));
# Line 897 | Line 918 | public class ConcurrentSkipListMapTest e
918       */
919      public void testTailMapContents() {
920          ConcurrentSkipListMap map = map5();
921 <        NavigableMap sm = map.navigableTailMap(two, true);
921 >        NavigableMap sm = map.tailMap(two, true);
922          assertFalse(sm.containsKey(one));
923          assertTrue(sm.containsKey(two));
924          assertTrue(sm.containsKey(three));
# Line 941 | Line 962 | public class ConcurrentSkipListMapTest e
962          assertEquals("E", e.getValue());
963          assertFalse(i.hasNext());
964  
965 <        NavigableMap ssm = sm.navigableTailMap(four, true);
965 >        NavigableMap ssm = sm.tailMap(four, true);
966          assertEquals(four, ssm.firstKey());
967          assertEquals(five, ssm.lastKey());
968 <        assertTrue(ssm.remove(four) != null);
968 >        assertEquals("D", ssm.remove(four));
969          assertEquals(1, ssm.size());
970          assertEquals(3, sm.size());
971          assertEquals(4, map.size());
# Line 956 | Line 977 | public class ConcurrentSkipListMapTest e
977      /**
978       * Submaps of submaps subdivide correctly
979       */
980 <    public void testRecursiveSubMaps() {
981 <        int mapSize = 1000;
982 <        Class cl = ConcurrentSkipListMap.class;
980 >    public void testRecursiveSubMaps() throws Exception {
981 >        int mapSize = expensiveTests ? 1000 : 100;
982 >        Class cl = ConcurrentSkipListMap.class;
983          NavigableMap<Integer, Integer> map = newMap(cl);
984          bs = new BitSet(mapSize);
985  
# Line 970 | Line 991 | public class ConcurrentSkipListMapTest e
991          check(map,                 0, mapSize - 1, true);
992          check(map.descendingMap(), 0, mapSize - 1, false);
993  
994 <        bashSubMap(map.navigableSubMap(0, true, mapSize, false),
994 >        bashSubMap(map.subMap(0, true, mapSize, false),
995                     0, mapSize - 1, true);
996      }
997  
998 <    static NavigableMap<Integer, Integer> newMap(Class cl) {
999 <        NavigableMap<Integer, Integer> result = null;
1000 <        try {
1001 <            result = (NavigableMap<Integer, Integer>) cl.newInstance();
981 <        } catch(Exception e) {
982 <            fail();
983 <        }
984 <        assertEquals(result.size(), 0);
998 >    static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception {
999 >        NavigableMap<Integer, Integer> result =
1000 >            (NavigableMap<Integer, Integer>) cl.newInstance();
1001 >        assertEquals(0, result.size());
1002          assertFalse(result.keySet().iterator().hasNext());
1003          return result;
1004      }
# Line 1003 | Line 1020 | public class ConcurrentSkipListMapTest e
1020          }
1021  
1022          // Remove a bunch of entries with iterator
1023 <        for(Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1023 >        for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1024              if (rnd.nextBoolean()) {
1025                  bs.clear(it.next());
1026                  it.remove();
# Line 1013 | Line 1030 | public class ConcurrentSkipListMapTest e
1030          // Add entries till we're back to original size
1031          while (map.size() < size) {
1032              int key = min + rnd.nextInt(rangeSize);
1033 <            assertTrue(key >= min && key<= max);
1033 >            assertTrue(key >= min && key <= max);
1034              put(map, key);
1035          }
1036      }
# Line 1028 | Line 1045 | public class ConcurrentSkipListMapTest e
1045          }
1046  
1047          // Remove a bunch of entries with iterator
1048 <        for(Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1048 >        for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1049              if (rnd.nextBoolean()) {
1050                  bs.clear(it.next());
1051                  it.remove();
# Line 1038 | Line 1055 | public class ConcurrentSkipListMapTest e
1055          // Add entries till we're back to original size
1056          while (map.size() < size) {
1057              int key = min - 5 + rnd.nextInt(rangeSize + 10);
1058 <            if (key >= min && key<= max) {
1058 >            if (key >= min && key <= max) {
1059                  put(map, key);
1060              } else {
1061                  try {
1062                      map.put(key, 2 * key);
1063 <                    fail();
1064 <                } catch(IllegalArgumentException e) {
1048 <                    // expected
1049 <                }
1063 >                    shouldThrow();
1064 >                } catch (IllegalArgumentException success) {}
1065              }
1066          }
1067      }
# Line 1077 | Line 1092 | public class ConcurrentSkipListMapTest e
1092  
1093          // headMap - pick direction and endpoint inclusion randomly
1094          boolean incl = rnd.nextBoolean();
1095 <        NavigableMap<Integer,Integer> hm = map.navigableHeadMap(midPoint, incl);
1095 >        NavigableMap<Integer,Integer> hm = map.headMap(midPoint, incl);
1096          if (ascending) {
1097              if (rnd.nextBoolean())
1098                  bashSubMap(hm, min, midPoint - (incl ? 0 : 1), true);
# Line 1094 | Line 1109 | public class ConcurrentSkipListMapTest e
1109  
1110          // tailMap - pick direction and endpoint inclusion randomly
1111          incl = rnd.nextBoolean();
1112 <        NavigableMap<Integer,Integer> tm = map.navigableTailMap(midPoint,incl);
1112 >        NavigableMap<Integer,Integer> tm = map.tailMap(midPoint,incl);
1113          if (ascending) {
1114              if (rnd.nextBoolean())
1115                  bashSubMap(tm, midPoint + (incl ? 0 : 1), max, true);
# Line 1119 | Line 1134 | public class ConcurrentSkipListMapTest e
1134          boolean lowIncl = rnd.nextBoolean();
1135          boolean highIncl = rnd.nextBoolean();
1136          if (ascending) {
1137 <            NavigableMap<Integer,Integer> sm = map.navigableSubMap(
1137 >            NavigableMap<Integer,Integer> sm = map.subMap(
1138                  endpoints[0], lowIncl, endpoints[1], highIncl);
1139              if (rnd.nextBoolean())
1140                  bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1),
# Line 1128 | Line 1143 | public class ConcurrentSkipListMapTest e
1143                  bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1),
1144                             endpoints[1] - (highIncl ? 0 : 1), false);
1145          } else {
1146 <            NavigableMap<Integer,Integer> sm = map.navigableSubMap(
1146 >            NavigableMap<Integer,Integer> sm = map.subMap(
1147                  endpoints[1], highIncl, endpoints[0], lowIncl);
1148              if (rnd.nextBoolean())
1149                  bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1),
# Line 1144 | Line 1159 | public class ConcurrentSkipListMapTest e
1159       */
1160      void check(NavigableMap<Integer, Integer> map,
1161                        final int min, final int max, final boolean ascending) {
1162 <       class ReferenceSet {
1162 >        class ReferenceSet {
1163              int lower(int key) {
1164                  return ascending ? lowerAscending(key) : higherAscending(key);
1165              }
# Line 1175 | Line 1190 | public class ConcurrentSkipListMapTest e
1190                  // BitSet should support this! Test would run much faster
1191                  while (key >= min) {
1192                      if (bs.get(key))
1193 <                        return(key);
1193 >                        return key;
1194                      key--;
1195                  }
1196                  return -1;
# Line 1210 | Line 1225 | public class ConcurrentSkipListMapTest e
1225              if (bsContainsI)
1226                  size++;
1227          }
1228 <        assertEquals(map.size(), size);
1228 >        assertEquals(size, map.size());
1229  
1230          // Test contents using contains keySet iterator
1231          int size2 = 0;
# Line 1241 | Line 1256 | public class ConcurrentSkipListMapTest e
1256              assertEq(rs.last(),  -1);
1257              try {
1258                  map.firstKey();
1259 <                fail();
1260 <            } catch(NoSuchElementException e) {
1246 <                // expected
1247 <            }
1259 >                shouldThrow();
1260 >            } catch (NoSuchElementException success) {}
1261              try {
1262                  map.lastKey();
1263 <                fail();
1264 <            } catch(NoSuchElementException e) {
1252 <                // expected
1253 <            }
1263 >                shouldThrow();
1264 >            } catch (NoSuchElementException success) {}
1265          }
1266      }
1267  
# Line 1264 | Line 1275 | public class ConcurrentSkipListMapTest e
1275      static boolean eq(Integer i, int j) {
1276          return i == null ? j == -1 : i == j;
1277      }
1278 <    
1278 >
1279   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines