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.8 by dl, Sat Apr 19 18:45:18 2008 UTC vs.
Revision 1.41 by jsr166, Wed Aug 23 05:33:00 2017 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  
22   public class ConcurrentSkipListMapTest extends JSR166TestCase {
23      public static void main(String[] args) {
24 <        junit.textui.TestRunner.run (suite());  
24 >        main(suite(), args);
25      }
26      public static Test suite() {
27 <        return new TestSuite(ConcurrentSkipListMapTest.class);
27 >        class Implementation implements MapImplementation {
28 >            public Class<?> klazz() { return ConcurrentSkipListMap.class; }
29 >            public Map emptyMap() { return new ConcurrentSkipListMap(); }
30 >            public Object makeKey(int i) { return i; }
31 >            public Object makeValue(int i) { return i; }
32 >            public boolean isConcurrent() { return true; }
33 >            public boolean permitsNullKeys() { return false; }
34 >            public boolean permitsNullValues() { return false; }
35 >            public boolean supportsSetValue() { return false; }
36 >        }
37 >        return newTestSuite(
38 >            ConcurrentSkipListMapTest.class,
39 >            MapTest.testSuite(new Implementation()));
40      }
41  
42      /**
43 <     * Create a map from Integers 1-5 to Strings "A"-"E".
43 >     * Returns a new map from Integers 1-5 to Strings "A"-"E".
44       */
45 <    private static ConcurrentSkipListMap map5() {  
46 <        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
45 >    private static ConcurrentSkipListMap map5() {
46 >        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
47          assertTrue(map.isEmpty());
48 <        map.put(one, "A");
49 <        map.put(five, "E");
50 <        map.put(three, "C");
51 <        map.put(two, "B");
52 <        map.put(four, "D");
48 >        map.put(one, "A");
49 >        map.put(five, "E");
50 >        map.put(three, "C");
51 >        map.put(two, "B");
52 >        map.put(four, "D");
53          assertFalse(map.isEmpty());
54          assertEquals(5, map.size());
55 <        return map;
55 >        return map;
56      }
57  
58      /**
59 <     *  clear removes all pairs
59 >     * clear removes all pairs
60       */
61      public void testClear() {
62          ConcurrentSkipListMap map = map5();
63 <        map.clear();
64 <        assertEquals(map.size(), 0);
63 >        map.clear();
64 >        assertEquals(0, map.size());
65      }
66  
67      /**
68 <     *  
68 >     * copy constructor creates map equal to source map
69       */
70      public void testConstructFromSorted() {
71          ConcurrentSkipListMap map = map5();
# Line 52 | Line 74 | public class ConcurrentSkipListMapTest e
74      }
75  
76      /**
77 <     *  Maps with same contents are equal
77 >     * Maps with same contents are equal
78       */
79      public void testEquals() {
80          ConcurrentSkipListMap map1 = map5();
81          ConcurrentSkipListMap map2 = map5();
82          assertEquals(map1, map2);
83          assertEquals(map2, map1);
84 <        map1.clear();
84 >        map1.clear();
85          assertFalse(map1.equals(map2));
86          assertFalse(map2.equals(map1));
87      }
88  
89      /**
90 <     *  containsKey returns true for contained key
90 >     * containsKey returns true for contained key
91       */
92      public void testContainsKey() {
93          ConcurrentSkipListMap map = map5();
94 <        assertTrue(map.containsKey(one));
94 >        assertTrue(map.containsKey(one));
95          assertFalse(map.containsKey(zero));
96      }
97  
98      /**
99 <     *  containsValue returns true for held values
99 >     * containsValue returns true for held values
100       */
101      public void testContainsValue() {
102          ConcurrentSkipListMap map = map5();
103 <        assertTrue(map.containsValue("A"));
103 >        assertTrue(map.containsValue("A"));
104          assertFalse(map.containsValue("Z"));
105      }
106  
107      /**
108 <     *  get returns the correct element at the given key,
109 <     *  or null if not present
108 >     * get returns the correct element at the given key,
109 >     * or null if not present
110       */
111      public void testGet() {
112          ConcurrentSkipListMap map = map5();
113 <        assertEquals("A", (String)map.get(one));
113 >        assertEquals("A", (String)map.get(one));
114          ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
115          assertNull(empty.get(one));
116      }
117  
118      /**
119 <     *  isEmpty is true of empty map and false for non-empty
119 >     * isEmpty is true of empty map and false for non-empty
120       */
121      public void testIsEmpty() {
122          ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
123          ConcurrentSkipListMap map = map5();
124 <        assertTrue(empty.isEmpty());
124 >        assertTrue(empty.isEmpty());
125          assertFalse(map.isEmpty());
126      }
127  
128      /**
129 <     *   firstKey returns first key
129 >     * firstKey returns first key
130       */
131      public void testFirstKey() {
132          ConcurrentSkipListMap map = map5();
133 <        assertEquals(one, map.firstKey());
133 >        assertEquals(one, map.firstKey());
134      }
135  
136      /**
137 <     *   lastKey returns last key
137 >     * lastKey returns last key
138       */
139      public void testLastKey() {
140          ConcurrentSkipListMap map = map5();
141 <        assertEquals(five, map.lastKey());
141 >        assertEquals(five, map.lastKey());
142      }
143  
122
144      /**
145 <     *  keySet.toArray returns contains all keys
145 >     * keySet.toArray returns contains all keys
146       */
147      public void testKeySetToArray() {
148          ConcurrentSkipListMap map = map5();
149 <        Set s = map.keySet();
149 >        Set s = map.keySet();
150          Object[] ar = s.toArray();
151          assertTrue(s.containsAll(Arrays.asList(ar)));
152 <        assertEquals(5, ar.length);
152 >        assertEquals(5, ar.length);
153          ar[0] = m10;
154          assertFalse(s.containsAll(Arrays.asList(ar)));
155      }
156  
157      /**
158 <     *  descendingkeySet.toArray returns contains all keys
158 >     * descendingkeySet.toArray returns contains all keys
159       */
160      public void testDescendingKeySetToArray() {
161          ConcurrentSkipListMap map = map5();
162 <        Set s = map.descendingKeySet();
162 >        Set s = map.descendingKeySet();
163          Object[] ar = s.toArray();
164 <        assertEquals(5, ar.length);
164 >        assertEquals(5, ar.length);
165          assertTrue(s.containsAll(Arrays.asList(ar)));
166          ar[0] = m10;
167          assertFalse(s.containsAll(Arrays.asList(ar)));
168      }
169  
170      /**
171 <     *   keySet returns a Set containing all the keys
171 >     * keySet returns a Set containing all the keys
172       */
173      public void testKeySet() {
174          ConcurrentSkipListMap map = map5();
175 <        Set s = map.keySet();
176 <        assertEquals(5, s.size());
177 <        assertTrue(s.contains(one));
178 <        assertTrue(s.contains(two));
179 <        assertTrue(s.contains(three));
180 <        assertTrue(s.contains(four));
181 <        assertTrue(s.contains(five));
175 >        Set s = map.keySet();
176 >        assertEquals(5, s.size());
177 >        assertTrue(s.contains(one));
178 >        assertTrue(s.contains(two));
179 >        assertTrue(s.contains(three));
180 >        assertTrue(s.contains(four));
181 >        assertTrue(s.contains(five));
182      }
183  
184      /**
185 <     *   keySet is ordered
185 >     * keySet is ordered
186       */
187      public void testKeySetOrder() {
188          ConcurrentSkipListMap map = map5();
189 <        Set s = map.keySet();
189 >        Set s = map.keySet();
190          Iterator i = s.iterator();
191          Integer last = (Integer)i.next();
192          assertEquals(last, one);
# Line 176 | Line 197 | public class ConcurrentSkipListMapTest e
197              last = k;
198              ++count;
199          }
200 <        assertEquals(count ,5);
200 >        assertEquals(5, count);
201      }
202  
203      /**
# Line 184 | Line 205 | public class ConcurrentSkipListMapTest e
205       */
206      public void testKeySetDescendingIteratorOrder() {
207          ConcurrentSkipListMap map = map5();
208 <        NavigableSet s = map.navigableKeySet();
208 >        NavigableSet s = map.navigableKeySet();
209          Iterator i = s.descendingIterator();
210          Integer last = (Integer)i.next();
211          assertEquals(last, five);
# Line 195 | Line 216 | public class ConcurrentSkipListMapTest e
216              last = k;
217              ++count;
218          }
219 <        assertEquals(count ,5);
219 >        assertEquals(5, count);
220      }
221  
222      /**
223 <     *   descendingKeySet is ordered
223 >     * descendingKeySet is ordered
224       */
225      public void testDescendingKeySetOrder() {
226          ConcurrentSkipListMap map = map5();
227 <        Set s = map.descendingKeySet();
227 >        Set s = map.descendingKeySet();
228          Iterator i = s.iterator();
229          Integer last = (Integer)i.next();
230          assertEquals(last, five);
# Line 214 | Line 235 | public class ConcurrentSkipListMapTest e
235              last = k;
236              ++count;
237          }
238 <        assertEquals(count, 5);
238 >        assertEquals(5, count);
239      }
240  
241      /**
242 <     *  descending iterator of descendingKeySet is ordered
242 >     * descending iterator of descendingKeySet is ordered
243       */
244      public void testDescendingKeySetDescendingIteratorOrder() {
245          ConcurrentSkipListMap map = map5();
246 <        NavigableSet s = map.descendingKeySet();
246 >        NavigableSet s = map.descendingKeySet();
247          Iterator i = s.descendingIterator();
248          Integer last = (Integer)i.next();
249          assertEquals(last, one);
# Line 233 | Line 254 | public class ConcurrentSkipListMapTest e
254              last = k;
255              ++count;
256          }
257 <        assertEquals(count, 5);
257 >        assertEquals(5, count);
258      }
259  
260      /**
261 <     *  Values.toArray contains all values
261 >     * Values.toArray contains all values
262       */
263      public void testValuesToArray() {
264          ConcurrentSkipListMap map = map5();
265 <        Collection v = map.values();
265 >        Collection v = map.values();
266          Object[] ar = v.toArray();
267          ArrayList s = new ArrayList(Arrays.asList(ar));
268 <        assertEquals(5, ar.length);
269 <        assertTrue(s.contains("A"));
270 <        assertTrue(s.contains("B"));
271 <        assertTrue(s.contains("C"));
272 <        assertTrue(s.contains("D"));
273 <        assertTrue(s.contains("E"));
268 >        assertEquals(5, ar.length);
269 >        assertTrue(s.contains("A"));
270 >        assertTrue(s.contains("B"));
271 >        assertTrue(s.contains("C"));
272 >        assertTrue(s.contains("D"));
273 >        assertTrue(s.contains("E"));
274      }
275  
276      /**
# Line 257 | Line 278 | public class ConcurrentSkipListMapTest e
278       */
279      public void testValues() {
280          ConcurrentSkipListMap map = map5();
281 <        Collection s = map.values();
282 <        assertEquals(5, s.size());
283 <        assertTrue(s.contains("A"));
284 <        assertTrue(s.contains("B"));
285 <        assertTrue(s.contains("C"));
286 <        assertTrue(s.contains("D"));
287 <        assertTrue(s.contains("E"));
281 >        Collection s = map.values();
282 >        assertEquals(5, s.size());
283 >        assertTrue(s.contains("A"));
284 >        assertTrue(s.contains("B"));
285 >        assertTrue(s.contains("C"));
286 >        assertTrue(s.contains("D"));
287 >        assertTrue(s.contains("E"));
288      }
289  
290      /**
# Line 271 | Line 292 | public class ConcurrentSkipListMapTest e
292       */
293      public void testEntrySet() {
294          ConcurrentSkipListMap map = map5();
295 <        Set s = map.entrySet();
296 <        assertEquals(5, s.size());
295 >        Set s = map.entrySet();
296 >        assertEquals(5, s.size());
297          Iterator it = s.iterator();
298          while (it.hasNext()) {
299              Map.Entry e = (Map.Entry) it.next();
300 <            assertTrue(
300 >            assertTrue(
301                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
302                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
303                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 290 | Line 311 | public class ConcurrentSkipListMapTest e
311       */
312      public void testDescendingEntrySet() {
313          ConcurrentSkipListMap map = map5();
314 <        Set s = map.descendingMap().entrySet();
315 <        assertEquals(5, s.size());
314 >        Set s = map.descendingMap().entrySet();
315 >        assertEquals(5, s.size());
316          Iterator it = s.iterator();
317          while (it.hasNext()) {
318              Map.Entry e = (Map.Entry) it.next();
319 <            assertTrue(
319 >            assertTrue(
320                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
321                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
322                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 305 | Line 326 | public class ConcurrentSkipListMapTest e
326      }
327  
328      /**
329 <     *  entrySet.toArray contains all entries
329 >     * entrySet.toArray contains all entries
330       */
331      public void testEntrySetToArray() {
332          ConcurrentSkipListMap map = map5();
333 <        Set s = map.entrySet();
333 >        Set s = map.entrySet();
334          Object[] ar = s.toArray();
335          assertEquals(5, ar.length);
336          for (int i = 0; i < 5; ++i) {
# Line 319 | Line 340 | public class ConcurrentSkipListMapTest e
340      }
341  
342      /**
343 <     *  descendingEntrySet.toArray contains all entries
343 >     * descendingEntrySet.toArray contains all entries
344       */
345      public void testDescendingEntrySetToArray() {
346          ConcurrentSkipListMap map = map5();
347 <        Set s = map.descendingMap().entrySet();
347 >        Set s = map.descendingMap().entrySet();
348          Object[] ar = s.toArray();
349          assertEquals(5, ar.length);
350          for (int i = 0; i < 5; ++i) {
# Line 333 | Line 354 | public class ConcurrentSkipListMapTest e
354      }
355  
356      /**
357 <     *   putAll  adds all key-value pairs from the given map
357 >     * putAll adds all key-value pairs from the given map
358       */
359      public void testPutAll() {
360          ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
361          ConcurrentSkipListMap map = map5();
362 <        empty.putAll(map);
363 <        assertEquals(5, empty.size());
364 <        assertTrue(empty.containsKey(one));
365 <        assertTrue(empty.containsKey(two));
366 <        assertTrue(empty.containsKey(three));
367 <        assertTrue(empty.containsKey(four));
368 <        assertTrue(empty.containsKey(five));
362 >        empty.putAll(map);
363 >        assertEquals(5, empty.size());
364 >        assertTrue(empty.containsKey(one));
365 >        assertTrue(empty.containsKey(two));
366 >        assertTrue(empty.containsKey(three));
367 >        assertTrue(empty.containsKey(four));
368 >        assertTrue(empty.containsKey(five));
369      }
370  
371      /**
372 <     *   putIfAbsent works when the given key is not present
372 >     * putIfAbsent works when the given key is not present
373       */
374      public void testPutIfAbsent() {
375          ConcurrentSkipListMap map = map5();
376 <        map.putIfAbsent(six, "Z");
376 >        map.putIfAbsent(six, "Z");
377          assertTrue(map.containsKey(six));
378      }
379  
380      /**
381 <     *   putIfAbsent does not add the pair if the key is already present
381 >     * putIfAbsent does not add the pair if the key is already present
382       */
383      public void testPutIfAbsent2() {
384          ConcurrentSkipListMap map = map5();
# Line 365 | Line 386 | public class ConcurrentSkipListMapTest e
386      }
387  
388      /**
389 <     *   replace fails when the given key is not present
389 >     * replace fails when the given key is not present
390       */
391      public void testReplace() {
392          ConcurrentSkipListMap map = map5();
393 <        assertNull(map.replace(six, "Z"));
393 >        assertNull(map.replace(six, "Z"));
394          assertFalse(map.containsKey(six));
395      }
396  
397      /**
398 <     *   replace succeeds if the key is already present
398 >     * replace succeeds if the key is already present
399       */
400      public void testReplace2() {
401          ConcurrentSkipListMap map = map5();
# Line 382 | Line 403 | public class ConcurrentSkipListMapTest e
403          assertEquals("Z", map.get(one));
404      }
405  
385
406      /**
407       * replace value fails when the given key not mapped to expected value
408       */
409      public void testReplaceValue() {
410          ConcurrentSkipListMap map = map5();
411          assertEquals("A", map.get(one));
412 <        assertFalse(map.replace(one, "Z", "Z"));
412 >        assertFalse(map.replace(one, "Z", "Z"));
413          assertEquals("A", map.get(one));
414      }
415  
# Line 399 | Line 419 | public class ConcurrentSkipListMapTest e
419      public void testReplaceValue2() {
420          ConcurrentSkipListMap map = map5();
421          assertEquals("A", map.get(one));
422 <        assertTrue(map.replace(one, "A", "Z"));
422 >        assertTrue(map.replace(one, "A", "Z"));
423          assertEquals("Z", map.get(one));
424      }
425  
406
426      /**
427 <     *   remove removes the correct key-value pair from the map
427 >     * remove removes the correct key-value pair from the map
428       */
429      public void testRemove() {
430          ConcurrentSkipListMap map = map5();
431 <        map.remove(five);
432 <        assertEquals(4, map.size());
433 <        assertFalse(map.containsKey(five));
431 >        map.remove(five);
432 >        assertEquals(4, map.size());
433 >        assertFalse(map.containsKey(five));
434      }
435  
436      /**
# Line 419 | Line 438 | public class ConcurrentSkipListMapTest e
438       */
439      public void testRemove2() {
440          ConcurrentSkipListMap map = map5();
441 <        assertTrue(map.containsKey(five));
441 >        assertTrue(map.containsKey(five));
442          assertEquals("E", map.get(five));
443 <        map.remove(five, "E");
444 <        assertEquals(4, map.size());
445 <        assertFalse(map.containsKey(five));
446 <        map.remove(four, "A");
447 <        assertEquals(4, map.size());
448 <        assertTrue(map.containsKey(four));
430 <
443 >        map.remove(five, "E");
444 >        assertEquals(4, map.size());
445 >        assertFalse(map.containsKey(five));
446 >        map.remove(four, "A");
447 >        assertEquals(4, map.size());
448 >        assertTrue(map.containsKey(four));
449      }
450  
451      /**
# Line 446 | Line 464 | public class ConcurrentSkipListMapTest e
464  
465          Map.Entry e4 = map.lowerEntry(zero);
466          assertNull(e4);
449
467      }
468  
469      /**
# Line 465 | Line 482 | public class ConcurrentSkipListMapTest e
482  
483          Map.Entry e4 = map.higherEntry(six);
484          assertNull(e4);
468
485      }
486  
487      /**
# Line 484 | Line 500 | public class ConcurrentSkipListMapTest e
500  
501          Map.Entry e4 = map.floorEntry(zero);
502          assertNull(e4);
487
503      }
504  
505      /**
# Line 503 | Line 518 | public class ConcurrentSkipListMapTest e
518  
519          Map.Entry e4 = map.ceilingEntry(six);
520          assertNull(e4);
506
521      }
522  
523      /**
524       * lowerEntry, higherEntry, ceilingEntry, and floorEntry return
525 <     * imutable entries
525 >     * immutable entries
526       */
527 <    public void testEntryImmutablity() {
527 >    public void testEntryImmutability() {
528          ConcurrentSkipListMap map = map5();
529          Map.Entry e = map.lowerEntry(three);
530          assertEquals(two, e.getKey());
531          try {
532              e.setValue("X");
533 <            fail();
534 <        } catch(UnsupportedOperationException success) {}
533 >            shouldThrow();
534 >        } catch (UnsupportedOperationException success) {}
535          e = map.higherEntry(zero);
536          assertEquals(one, e.getKey());
537          try {
538              e.setValue("X");
539 <            fail();
540 <        } catch(UnsupportedOperationException success) {}
539 >            shouldThrow();
540 >        } catch (UnsupportedOperationException success) {}
541          e = map.floorEntry(one);
542          assertEquals(one, e.getKey());
543          try {
544              e.setValue("X");
545 <            fail();
546 <        } catch(UnsupportedOperationException success) {}
545 >            shouldThrow();
546 >        } catch (UnsupportedOperationException success) {}
547          e = map.ceilingEntry(five);
548          assertEquals(five, e.getKey());
549          try {
550              e.setValue("X");
551 <            fail();
552 <        } catch(UnsupportedOperationException success) {}
551 >            shouldThrow();
552 >        } catch (UnsupportedOperationException success) {}
553      }
554  
541
542
555      /**
556       * lowerKey returns preceding element
557       */
# Line 556 | Line 568 | public class ConcurrentSkipListMapTest e
568  
569          Object e4 = q.lowerKey(zero);
570          assertNull(e4);
559
571      }
572  
573      /**
# Line 575 | Line 586 | public class ConcurrentSkipListMapTest e
586  
587          Object e4 = q.higherKey(six);
588          assertNull(e4);
578
589      }
590  
591      /**
# Line 594 | Line 604 | public class ConcurrentSkipListMapTest e
604  
605          Object e4 = q.floorKey(zero);
606          assertNull(e4);
597
607      }
608  
609      /**
# Line 613 | Line 622 | public class ConcurrentSkipListMapTest e
622  
623          Object e4 = q.ceilingKey(six);
624          assertNull(e4);
616
625      }
626  
627      /**
# Line 638 | Line 646 | public class ConcurrentSkipListMapTest e
646          try {
647              e.setValue("A");
648              shouldThrow();
649 <        } catch (Exception ok) {
642 <        }
649 >        } catch (UnsupportedOperationException success) {}
650          e = map.pollFirstEntry();
651          assertNull(e);
652      }
# Line 666 | Line 673 | public class ConcurrentSkipListMapTest e
673          try {
674              e.setValue("E");
675              shouldThrow();
676 <        } catch (Exception ok) {
670 <        }
676 >        } catch (UnsupportedOperationException success) {}
677          e = map.pollLastEntry();
678          assertNull(e);
679      }
680  
681      /**
682 <     *   size returns the correct values
682 >     * size returns the correct values
683       */
684      public void testSize() {
685          ConcurrentSkipListMap map = map5();
686          ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
687 <        assertEquals(0, empty.size());
688 <        assertEquals(5, map.size());
687 >        assertEquals(0, empty.size());
688 >        assertEquals(5, map.size());
689      }
690  
691      /**
# Line 689 | Line 695 | public class ConcurrentSkipListMapTest e
695          ConcurrentSkipListMap map = map5();
696          String s = map.toString();
697          for (int i = 1; i <= 5; ++i) {
698 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
698 >            assertTrue(s.contains(String.valueOf(i)));
699          }
700 <    }        
700 >    }
701  
702      // Exception tests
703  
# Line 699 | Line 705 | public class ConcurrentSkipListMapTest e
705       * get(null) of nonempty map throws NPE
706       */
707      public void testGet_NullPointerException() {
708 +        ConcurrentSkipListMap c = map5();
709          try {
703            ConcurrentSkipListMap c = map5();
710              c.get(null);
711              shouldThrow();
712 <        } catch(NullPointerException e){}
712 >        } catch (NullPointerException success) {}
713      }
714  
715      /**
716       * containsKey(null) of nonempty map throws NPE
717       */
718      public void testContainsKey_NullPointerException() {
719 +        ConcurrentSkipListMap c = map5();
720          try {
714            ConcurrentSkipListMap c = map5();
721              c.containsKey(null);
722              shouldThrow();
723 <        } catch(NullPointerException e){}
723 >        } catch (NullPointerException success) {}
724      }
725  
726      /**
727       * containsValue(null) throws NPE
728       */
729      public void testContainsValue_NullPointerException() {
730 +        ConcurrentSkipListMap c = new ConcurrentSkipListMap();
731          try {
725            ConcurrentSkipListMap c = new ConcurrentSkipListMap();
732              c.containsValue(null);
733              shouldThrow();
734 <        } catch(NullPointerException e){}
734 >        } catch (NullPointerException success) {}
735      }
736  
731
737      /**
738       * put(null,x) throws NPE
739       */
740      public void testPut1_NullPointerException() {
741 +        ConcurrentSkipListMap c = map5();
742          try {
737            ConcurrentSkipListMap c = map5();
743              c.put(null, "whatever");
744              shouldThrow();
745 <        } catch(NullPointerException e){}
745 >        } catch (NullPointerException success) {}
746      }
747  
748      /**
749       * putIfAbsent(null, x) throws NPE
750       */
751      public void testPutIfAbsent1_NullPointerException() {
752 +        ConcurrentSkipListMap c = map5();
753          try {
748            ConcurrentSkipListMap c = map5();
754              c.putIfAbsent(null, "whatever");
755              shouldThrow();
756 <        } catch(NullPointerException e){}
756 >        } catch (NullPointerException success) {}
757      }
758  
759      /**
760       * replace(null, x) throws NPE
761       */
762      public void testReplace_NullPointerException() {
763 +        ConcurrentSkipListMap c = map5();
764          try {
759            ConcurrentSkipListMap c = map5();
765              c.replace(null, "whatever");
766              shouldThrow();
767 <        } catch(NullPointerException e){}
767 >        } catch (NullPointerException success) {}
768      }
769  
770      /**
771       * replace(null, x, y) throws NPE
772       */
773      public void testReplaceValue_NullPointerException() {
774 +        ConcurrentSkipListMap c = map5();
775          try {
770            ConcurrentSkipListMap c = map5();
776              c.replace(null, one, "whatever");
777              shouldThrow();
778 <        } catch(NullPointerException e){}
778 >        } catch (NullPointerException success) {}
779      }
780  
781      /**
782       * remove(null) throws NPE
783       */
784      public void testRemove1_NullPointerException() {
785 +        ConcurrentSkipListMap c = new ConcurrentSkipListMap();
786 +        c.put("sadsdf", "asdads");
787          try {
781            ConcurrentSkipListMap c = new ConcurrentSkipListMap();
782            c.put("sadsdf", "asdads");
788              c.remove(null);
789              shouldThrow();
790 <        } catch(NullPointerException e){}
790 >        } catch (NullPointerException success) {}
791      }
792  
793      /**
794       * remove(null, x) throws NPE
795       */
796      public void testRemove2_NullPointerException() {
797 +        ConcurrentSkipListMap c = new ConcurrentSkipListMap();
798 +        c.put("sadsdf", "asdads");
799          try {
793            ConcurrentSkipListMap c = new ConcurrentSkipListMap();
794            c.put("sadsdf", "asdads");
800              c.remove(null, "whatever");
801              shouldThrow();
802 <        } catch(NullPointerException e){}
802 >        } catch (NullPointerException success) {}
803      }
804  
805      /**
806       * remove(x, null) returns false
807       */
808      public void testRemove3() {
809 <        try {
810 <            ConcurrentSkipListMap c = new ConcurrentSkipListMap();
811 <            c.put("sadsdf", "asdads");
807 <            assertFalse(c.remove("sadsdf", null));
808 <        } catch(NullPointerException e){
809 <            fail();
810 <        }
809 >        ConcurrentSkipListMap c = new ConcurrentSkipListMap();
810 >        c.put("sadsdf", "asdads");
811 >        assertFalse(c.remove("sadsdf", null));
812      }
813  
814      /**
815 <     * A deserialized map equals original
815 >     * A cloned map equals original
816       */
817 <    public void testSerialization() {
818 <        ConcurrentSkipListMap q = map5();
817 >    public void testClone() {
818 >        ConcurrentSkipListMap x = map5();
819 >        ConcurrentSkipListMap y = x.clone();
820  
821 <        try {
822 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
823 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
824 <            out.writeObject(q);
825 <            out.close();
826 <
827 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
828 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
827 <            ConcurrentSkipListMap r = (ConcurrentSkipListMap)in.readObject();
828 <            assertEquals(q.size(), r.size());
829 <            assertTrue(q.equals(r));
830 <            assertTrue(r.equals(q));
831 <        } catch(Exception e){
832 <            e.printStackTrace();
833 <            unexpectedException();
834 <        }
821 >        assertNotSame(x, y);
822 >        assertEquals(x.size(), y.size());
823 >        assertEquals(x.toString(), y.toString());
824 >        assertEquals(x, y);
825 >        assertEquals(y, x);
826 >        y.clear();
827 >        assertTrue(y.isEmpty());
828 >        assertFalse(x.equals(y));
829      }
830  
831 +    /**
832 +     * A deserialized/reserialized map equals original
833 +     */
834 +    public void testSerialization() throws Exception {
835 +        NavigableMap x = map5();
836 +        NavigableMap y = serialClone(x);
837  
838 +        assertNotSame(x, y);
839 +        assertEquals(x.size(), y.size());
840 +        assertEquals(x.toString(), y.toString());
841 +        assertEquals(x, y);
842 +        assertEquals(y, x);
843 +        y.clear();
844 +        assertTrue(y.isEmpty());
845 +        assertFalse(x.equals(y));
846 +    }
847  
848      /**
849       * subMap returns map with keys in requested range
# Line 863 | Line 872 | public class ConcurrentSkipListMapTest e
872          k = (Integer)(r.next());
873          assertEquals(two, k);
874          assertFalse(r.hasNext());
875 <        
875 >
876          Iterator j = sm.keySet().iterator();
877          j.next();
878          j.remove();
# Line 872 | Line 881 | public class ConcurrentSkipListMapTest e
881          assertEquals(1, sm.size());
882          assertEquals(three, sm.firstKey());
883          assertEquals(three, sm.lastKey());
884 <        assertTrue(sm.remove(three) != null);
884 >        assertEquals("C", sm.remove(three));
885          assertTrue(sm.isEmpty());
886          assertEquals(3, map.size());
887      }
# Line 897 | Line 906 | public class ConcurrentSkipListMapTest e
906          k = (Integer)(r.next());
907          assertEquals(two, k);
908          assertFalse(r.hasNext());
909 <        
909 >
910          Iterator j = sm.keySet().iterator();
911          j.next();
912          j.remove();
# Line 905 | Line 914 | public class ConcurrentSkipListMapTest e
914          assertEquals(4, map.size());
915          assertEquals(0, sm.size());
916          assertTrue(sm.isEmpty());
917 <        assertTrue(sm.remove(three) == null);
917 >        assertSame(sm.remove(three), null);
918          assertEquals(4, map.size());
919      }
920  
# Line 987 | Line 996 | public class ConcurrentSkipListMapTest e
996          NavigableMap ssm = sm.tailMap(four, true);
997          assertEquals(four, ssm.firstKey());
998          assertEquals(five, ssm.lastKey());
999 <        assertTrue(ssm.remove(four) != null);
999 >        assertEquals("D", ssm.remove(four));
1000          assertEquals(1, ssm.size());
1001          assertEquals(3, sm.size());
1002          assertEquals(4, map.size());
# Line 999 | Line 1008 | public class ConcurrentSkipListMapTest e
1008      /**
1009       * Submaps of submaps subdivide correctly
1010       */
1011 <    public void testRecursiveSubMaps() {
1012 <        int mapSize = 1000;
1013 <        Class cl = ConcurrentSkipListMap.class;
1011 >    public void testRecursiveSubMaps() throws Exception {
1012 >        int mapSize = expensiveTests ? 1000 : 100;
1013 >        Class cl = ConcurrentSkipListMap.class;
1014          NavigableMap<Integer, Integer> map = newMap(cl);
1015          bs = new BitSet(mapSize);
1016  
# Line 1017 | Line 1026 | public class ConcurrentSkipListMapTest e
1026                     0, mapSize - 1, true);
1027      }
1028  
1029 <    static NavigableMap<Integer, Integer> newMap(Class cl) {
1030 <        NavigableMap<Integer, Integer> result = null;
1031 <        try {
1032 <            result = (NavigableMap<Integer, Integer>) cl.newInstance();
1024 <        } catch(Exception e) {
1025 <            fail();
1026 <        }
1027 <        assertEquals(result.size(), 0);
1029 >    static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception {
1030 >        NavigableMap<Integer, Integer> result =
1031 >            (NavigableMap<Integer, Integer>) cl.getConstructor().newInstance();
1032 >        assertEquals(0, result.size());
1033          assertFalse(result.keySet().iterator().hasNext());
1034          return result;
1035      }
# Line 1046 | Line 1051 | public class ConcurrentSkipListMapTest e
1051          }
1052  
1053          // Remove a bunch of entries with iterator
1054 <        for(Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1054 >        for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1055              if (rnd.nextBoolean()) {
1056                  bs.clear(it.next());
1057                  it.remove();
# Line 1056 | Line 1061 | public class ConcurrentSkipListMapTest e
1061          // Add entries till we're back to original size
1062          while (map.size() < size) {
1063              int key = min + rnd.nextInt(rangeSize);
1064 <            assertTrue(key >= min && key<= max);
1064 >            assertTrue(key >= min && key <= max);
1065              put(map, key);
1066          }
1067      }
# Line 1071 | Line 1076 | public class ConcurrentSkipListMapTest e
1076          }
1077  
1078          // Remove a bunch of entries with iterator
1079 <        for(Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1079 >        for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1080              if (rnd.nextBoolean()) {
1081                  bs.clear(it.next());
1082                  it.remove();
# Line 1081 | Line 1086 | public class ConcurrentSkipListMapTest e
1086          // Add entries till we're back to original size
1087          while (map.size() < size) {
1088              int key = min - 5 + rnd.nextInt(rangeSize + 10);
1089 <            if (key >= min && key<= max) {
1089 >            if (key >= min && key <= max) {
1090                  put(map, key);
1091              } else {
1092                  try {
1093                      map.put(key, 2 * key);
1094 <                    fail();
1095 <                } catch(IllegalArgumentException e) {
1091 <                    // expected
1092 <                }
1094 >                    shouldThrow();
1095 >                } catch (IllegalArgumentException success) {}
1096              }
1097          }
1098      }
# Line 1187 | Line 1190 | public class ConcurrentSkipListMapTest e
1190       */
1191      void check(NavigableMap<Integer, Integer> map,
1192                        final int min, final int max, final boolean ascending) {
1193 <       class ReferenceSet {
1193 >        class ReferenceSet {
1194              int lower(int key) {
1195                  return ascending ? lowerAscending(key) : higherAscending(key);
1196              }
# Line 1218 | Line 1221 | public class ConcurrentSkipListMapTest e
1221                  // BitSet should support this! Test would run much faster
1222                  while (key >= min) {
1223                      if (bs.get(key))
1224 <                        return(key);
1224 >                        return key;
1225                      key--;
1226                  }
1227                  return -1;
# Line 1253 | Line 1256 | public class ConcurrentSkipListMapTest e
1256              if (bsContainsI)
1257                  size++;
1258          }
1259 <        assertEquals(map.size(), size);
1259 >        assertEquals(size, map.size());
1260  
1261          // Test contents using contains keySet iterator
1262          int size2 = 0;
# Line 1284 | Line 1287 | public class ConcurrentSkipListMapTest e
1287              assertEq(rs.last(),  -1);
1288              try {
1289                  map.firstKey();
1290 <                fail();
1291 <            } catch(NoSuchElementException e) {
1289 <                // expected
1290 <            }
1290 >                shouldThrow();
1291 >            } catch (NoSuchElementException success) {}
1292              try {
1293                  map.lastKey();
1294 <                fail();
1295 <            } catch(NoSuchElementException e) {
1295 <                // expected
1296 <            }
1294 >                shouldThrow();
1295 >            } catch (NoSuchElementException success) {}
1296          }
1297      }
1298  
# Line 1305 | Line 1304 | public class ConcurrentSkipListMapTest e
1304      }
1305  
1306      static boolean eq(Integer i, int j) {
1307 <        return i == null ? j == -1 : i == j;
1307 >        return (i == null) ? j == -1 : i == j;
1308      }
1309 <    
1309 >
1310   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines