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.20 by jsr166, Sat Oct 9 19:30:34 2010 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();
# Line 39 | Line 61 | public class ConcurrentSkipListMapTest e
61      public void testClear() {
62          ConcurrentSkipListMap map = map5();
63          map.clear();
64 <        assertEquals(map.size(), 0);
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 119 | Line 141 | public class ConcurrentSkipListMapTest e
141          assertEquals(five, map.lastKey());
142      }
143  
122
144      /**
145       * keySet.toArray returns contains all keys
146       */
# 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 195 | Line 216 | public class ConcurrentSkipListMapTest e
216              last = k;
217              ++count;
218          }
219 <        assertEquals(count ,5);
219 >        assertEquals(5, count);
220      }
221  
222      /**
# 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      /**
# 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      /**
# 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       */
# Line 403 | Line 423 | public class ConcurrentSkipListMapTest e
423          assertEquals("Z", map.get(one));
424      }
425  
406
426      /**
427       * remove removes the correct key-value pair from the map
428       */
# Line 505 | Line 524 | public class ConcurrentSkipListMapTest e
524       * lowerEntry, higherEntry, ceilingEntry, and floorEntry return
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());
# Line 533 | Line 552 | public class ConcurrentSkipListMapTest e
552          } catch (UnsupportedOperationException success) {}
553      }
554  
536
537
555      /**
556       * lowerKey returns preceding element
557       */
# Line 678 | 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      }
701  
# Line 688 | 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 {
692            ConcurrentSkipListMap c = map5();
710              c.get(null);
711              shouldThrow();
712          } catch (NullPointerException success) {}
# Line 699 | Line 716 | public class ConcurrentSkipListMapTest e
716       * containsKey(null) of nonempty map throws NPE
717       */
718      public void testContainsKey_NullPointerException() {
719 +        ConcurrentSkipListMap c = map5();
720          try {
703            ConcurrentSkipListMap c = map5();
721              c.containsKey(null);
722              shouldThrow();
723          } catch (NullPointerException success) {}
# Line 710 | Line 727 | public class ConcurrentSkipListMapTest e
727       * containsValue(null) throws NPE
728       */
729      public void testContainsValue_NullPointerException() {
730 +        ConcurrentSkipListMap c = new ConcurrentSkipListMap();
731          try {
714            ConcurrentSkipListMap c = new ConcurrentSkipListMap();
732              c.containsValue(null);
733              shouldThrow();
734          } catch (NullPointerException success) {}
735      }
736  
720
737      /**
738       * put(null,x) throws NPE
739       */
740      public void testPut1_NullPointerException() {
741 +        ConcurrentSkipListMap c = map5();
742          try {
726            ConcurrentSkipListMap c = map5();
743              c.put(null, "whatever");
744              shouldThrow();
745          } catch (NullPointerException success) {}
# Line 733 | Line 749 | public class ConcurrentSkipListMapTest e
749       * putIfAbsent(null, x) throws NPE
750       */
751      public void testPutIfAbsent1_NullPointerException() {
752 +        ConcurrentSkipListMap c = map5();
753          try {
737            ConcurrentSkipListMap c = map5();
754              c.putIfAbsent(null, "whatever");
755              shouldThrow();
756          } catch (NullPointerException success) {}
# Line 744 | Line 760 | public class ConcurrentSkipListMapTest e
760       * replace(null, x) throws NPE
761       */
762      public void testReplace_NullPointerException() {
763 +        ConcurrentSkipListMap c = map5();
764          try {
748            ConcurrentSkipListMap c = map5();
765              c.replace(null, "whatever");
766              shouldThrow();
767          } catch (NullPointerException success) {}
# Line 755 | Line 771 | public class ConcurrentSkipListMapTest e
771       * replace(null, x, y) throws NPE
772       */
773      public void testReplaceValue_NullPointerException() {
774 +        ConcurrentSkipListMap c = map5();
775          try {
759            ConcurrentSkipListMap c = map5();
776              c.replace(null, one, "whatever");
777              shouldThrow();
778          } catch (NullPointerException success) {}
# Line 766 | Line 782 | public class ConcurrentSkipListMapTest e
782       * remove(null) throws NPE
783       */
784      public void testRemove1_NullPointerException() {
785 +        ConcurrentSkipListMap c = new ConcurrentSkipListMap();
786 +        c.put("sadsdf", "asdads");
787          try {
770            ConcurrentSkipListMap c = new ConcurrentSkipListMap();
771            c.put("sadsdf", "asdads");
788              c.remove(null);
789              shouldThrow();
790          } catch (NullPointerException success) {}
# Line 778 | Line 794 | public class ConcurrentSkipListMapTest e
794       * remove(null, x) throws NPE
795       */
796      public void testRemove2_NullPointerException() {
797 +        ConcurrentSkipListMap c = new ConcurrentSkipListMap();
798 +        c.put("sadsdf", "asdads");
799          try {
782            ConcurrentSkipListMap c = new ConcurrentSkipListMap();
783            c.put("sadsdf", "asdads");
800              c.remove(null, "whatever");
801              shouldThrow();
802          } catch (NullPointerException success) {}
# Line 796 | Line 812 | public class ConcurrentSkipListMapTest e
812      }
813  
814      /**
815 <     * A deserialized map equals original
815 >     * A cloned map equals original
816       */
817 <    public void testSerialization() throws Exception {
818 <        ConcurrentSkipListMap q = map5();
817 >    public void testClone() {
818 >        ConcurrentSkipListMap x = map5();
819 >        ConcurrentSkipListMap y = x.clone();
820  
821 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
822 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
823 <        out.writeObject(q);
824 <        out.close();
825 <
826 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
827 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
828 <        ConcurrentSkipListMap r = (ConcurrentSkipListMap)in.readObject();
812 <        assertEquals(q.size(), r.size());
813 <        assertTrue(q.equals(r));
814 <        assertTrue(r.equals(q));
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 980 | Line 1009 | public class ConcurrentSkipListMapTest e
1009       * Submaps of submaps subdivide correctly
1010       */
1011      public void testRecursiveSubMaps() throws Exception {
1012 <        int mapSize = 1000;
1012 >        int mapSize = expensiveTests ? 1000 : 100;
1013          Class cl = ConcurrentSkipListMap.class;
1014          NavigableMap<Integer, Integer> map = newMap(cl);
1015          bs = new BitSet(mapSize);
# Line 999 | Line 1028 | public class ConcurrentSkipListMapTest e
1028  
1029      static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception {
1030          NavigableMap<Integer, Integer> result =
1031 <            (NavigableMap<Integer, Integer>) cl.newInstance();
1032 <        assertEquals(result.size(), 0);
1031 >            (NavigableMap<Integer, Integer>) cl.getConstructor().newInstance();
1032 >        assertEquals(0, result.size());
1033          assertFalse(result.keySet().iterator().hasNext());
1034          return result;
1035      }
# Line 1032 | 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 1057 | 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 {
# Line 1161 | 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 1227 | 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 1275 | 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  
1310   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines