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.12 by jsr166, Sat Nov 21 02:07:26 2009 UTC vs.
Revision 1.27 by jsr166, Sat Nov 26 05:19:17 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import junit.framework.*;
8   import java.util.*;
9 < import java.util.concurrent.*;
10 < import java.io.*;
9 > 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);
# Line 34 | Line 33 | public class ConcurrentSkipListMapTest e
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);
41 >        assertEquals(0, map.size());
42      }
43  
44      /**
# 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();
# Line 65 | Line 64 | public class ConcurrentSkipListMapTest e
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();
# Line 74 | Line 73 | public class ConcurrentSkipListMapTest e
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();
# Line 83 | Line 82 | public class ConcurrentSkipListMapTest e
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();
# Line 94 | Line 93 | public class ConcurrentSkipListMapTest e
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();
# Line 104 | Line 103 | public class ConcurrentSkipListMapTest e
103      }
104  
105      /**
106 <     *   firstKey returns first key
106 >     * firstKey returns first key
107       */
108      public void testFirstKey() {
109          ConcurrentSkipListMap map = map5();
# Line 112 | Line 111 | public class ConcurrentSkipListMapTest e
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());
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();
# Line 134 | Line 132 | public class ConcurrentSkipListMapTest e
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();
# Line 147 | Line 145 | public class ConcurrentSkipListMapTest e
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();
# Line 161 | Line 159 | public class ConcurrentSkipListMapTest e
159      }
160  
161      /**
162 <     *   keySet is ordered
162 >     * keySet is ordered
163       */
164      public void testKeySetOrder() {
165          ConcurrentSkipListMap map = map5();
# Line 176 | Line 174 | public class ConcurrentSkipListMapTest e
174              last = k;
175              ++count;
176          }
177 <        assertEquals(count ,5);
177 >        assertEquals(5, count);
178      }
179  
180      /**
# Line 195 | Line 193 | public class ConcurrentSkipListMapTest e
193              last = k;
194              ++count;
195          }
196 <        assertEquals(count ,5);
196 >        assertEquals(5, count);
197      }
198  
199      /**
200 <     *   descendingKeySet is ordered
200 >     * descendingKeySet is ordered
201       */
202      public void testDescendingKeySetOrder() {
203          ConcurrentSkipListMap map = map5();
# Line 214 | Line 212 | public class ConcurrentSkipListMapTest e
212              last = k;
213              ++count;
214          }
215 <        assertEquals(count, 5);
215 >        assertEquals(5, count);
216      }
217  
218      /**
219 <     *  descending iterator of descendingKeySet is ordered
219 >     * descending iterator of descendingKeySet is ordered
220       */
221      public void testDescendingKeySetDescendingIteratorOrder() {
222          ConcurrentSkipListMap map = map5();
# Line 233 | Line 231 | public class ConcurrentSkipListMapTest e
231              last = k;
232              ++count;
233          }
234 <        assertEquals(count, 5);
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();
# Line 305 | 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();
# Line 319 | 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();
# Line 333 | 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();
# Line 348 | Line 346 | public class ConcurrentSkipListMapTest e
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();
# Line 357 | Line 355 | public class ConcurrentSkipListMapTest e
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 365 | 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();
# Line 374 | Line 372 | public class ConcurrentSkipListMapTest e
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 382 | Line 380 | public class ConcurrentSkipListMapTest e
380          assertEquals("Z", map.get(one));
381      }
382  
385
383      /**
384       * replace value fails when the given key not mapped to expected value
385       */
# Line 403 | Line 400 | public class ConcurrentSkipListMapTest e
400          assertEquals("Z", map.get(one));
401      }
402  
406
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();
# Line 427 | Line 423 | public class ConcurrentSkipListMapTest e
423          map.remove(four, "A");
424          assertEquals(4, map.size());
425          assertTrue(map.containsKey(four));
430
426      }
427  
428      /**
# Line 446 | Line 441 | public class ConcurrentSkipListMapTest e
441  
442          Map.Entry e4 = map.lowerEntry(zero);
443          assertNull(e4);
449
444      }
445  
446      /**
# Line 465 | Line 459 | public class ConcurrentSkipListMapTest e
459  
460          Map.Entry e4 = map.higherEntry(six);
461          assertNull(e4);
468
462      }
463  
464      /**
# Line 484 | Line 477 | public class ConcurrentSkipListMapTest e
477  
478          Map.Entry e4 = map.floorEntry(zero);
479          assertNull(e4);
487
480      }
481  
482      /**
# Line 503 | Line 495 | public class ConcurrentSkipListMapTest e
495  
496          Map.Entry e4 = map.ceilingEntry(six);
497          assertNull(e4);
506
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();
510 >            shouldThrow();
511          } catch (UnsupportedOperationException success) {}
512          e = map.higherEntry(zero);
513          assertEquals(one, e.getKey());
514          try {
515              e.setValue("X");
516 <            fail();
516 >            shouldThrow();
517          } catch (UnsupportedOperationException success) {}
518          e = map.floorEntry(one);
519          assertEquals(one, e.getKey());
520          try {
521              e.setValue("X");
522 <            fail();
522 >            shouldThrow();
523          } catch (UnsupportedOperationException success) {}
524          e = map.ceilingEntry(five);
525          assertEquals(five, e.getKey());
526          try {
527              e.setValue("X");
528 <            fail();
528 >            shouldThrow();
529          } catch (UnsupportedOperationException success) {}
530      }
531  
541
542
532      /**
533       * lowerKey returns preceding element
534       */
# Line 556 | Line 545 | public class ConcurrentSkipListMapTest e
545  
546          Object e4 = q.lowerKey(zero);
547          assertNull(e4);
559
548      }
549  
550      /**
# Line 575 | Line 563 | public class ConcurrentSkipListMapTest e
563  
564          Object e4 = q.higherKey(six);
565          assertNull(e4);
578
566      }
567  
568      /**
# Line 594 | Line 581 | public class ConcurrentSkipListMapTest e
581  
582          Object e4 = q.floorKey(zero);
583          assertNull(e4);
597
584      }
585  
586      /**
# Line 613 | Line 599 | public class ConcurrentSkipListMapTest e
599  
600          Object e4 = q.ceilingKey(six);
601          assertNull(e4);
616
602      }
603  
604      /**
# Line 638 | Line 623 | public class ConcurrentSkipListMapTest e
623          try {
624              e.setValue("A");
625              shouldThrow();
626 <        } catch (Exception ok) {
642 <        }
626 >        } catch (UnsupportedOperationException success) {}
627          e = map.pollFirstEntry();
628          assertNull(e);
629      }
# Line 666 | Line 650 | public class ConcurrentSkipListMapTest e
650          try {
651              e.setValue("E");
652              shouldThrow();
653 <        } catch (Exception ok) {
670 <        }
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();
# Line 689 | 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      }
678  
# Line 703 | 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 714 | 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 725 | 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  
731
714      /**
715       * put(null,x) throws NPE
716       */
# Line 737 | 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 748 | 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 759 | 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 770 | 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 782 | 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 794 | 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");
807 <            assertFalse(c.remove("sadsdf", null));
808 <        } catch (NullPointerException e) {
809 <            fail();
810 <        }
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 <
825 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
826 <            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 <        }
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  
837
838
805      /**
806       * subMap returns map with keys in requested range
807       */
# Line 872 | 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      }
# Line 905 | 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 987 | Line 953 | public class ConcurrentSkipListMapTest e
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 999 | Line 965 | public class ConcurrentSkipListMapTest e
965      /**
966       * Submaps of submaps subdivide correctly
967       */
968 <    public void testRecursiveSubMaps() {
969 <        int mapSize = 1000;
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);
# Line 1017 | Line 983 | public class ConcurrentSkipListMapTest e
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();
1024 <        } catch (Exception e) {
1025 <            fail();
1026 <        }
1027 <        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 1086 | Line 1048 | public class ConcurrentSkipListMapTest e
1048              } else {
1049                  try {
1050                      map.put(key, 2 * key);
1051 <                    fail();
1052 <                } catch (IllegalArgumentException e) {
1091 <                    // expected
1092 <                }
1051 >                    shouldThrow();
1052 >                } catch (IllegalArgumentException success) {}
1053              }
1054          }
1055      }
# Line 1187 | 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 1218 | 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 1284 | Line 1244 | public class ConcurrentSkipListMapTest e
1244              assertEq(rs.last(),  -1);
1245              try {
1246                  map.firstKey();
1247 <                fail();
1248 <            } catch (NoSuchElementException e) {
1289 <                // expected
1290 <            }
1247 >                shouldThrow();
1248 >            } catch (NoSuchElementException success) {}
1249              try {
1250                  map.lastKey();
1251 <                fail();
1252 <            } catch (NoSuchElementException e) {
1295 <                // expected
1296 <            }
1251 >                shouldThrow();
1252 >            } catch (NoSuchElementException success) {}
1253          }
1254      }
1255  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines