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

Comparing jsr166/src/test/tck/ConcurrentSkipListSubMapTest.java (file contents):
Revision 1.11 by jsr166, Sat Nov 21 17:38:05 2009 UTC vs.
Revision 1.24 by jsr166, Wed Dec 31 19:05:42 2014 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.Collection;
10 > import java.util.Iterator;
11 > import java.util.Map;
12 > import java.util.NavigableMap;
13 > import java.util.Set;
14 > import java.util.SortedMap;
15 > import java.util.concurrent.ConcurrentNavigableMap;
16 > import java.util.concurrent.ConcurrentSkipListMap;
17 >
18 > import junit.framework.Test;
19 > import junit.framework.TestSuite;
20  
21   public class ConcurrentSkipListSubMapTest extends JSR166TestCase {
22      public static void main(String[] args) {
23 <        junit.textui.TestRunner.run (suite());
23 >        junit.textui.TestRunner.run(suite());
24      }
25      public static Test suite() {
26          return new TestSuite(ConcurrentSkipListSubMapTest.class);
27      }
28  
29      /**
30 <     * Create a map from Integers 1-5 to Strings "A"-"E".
30 >     * Returns a new map from Integers 1-5 to Strings "A"-"E".
31       */
32      private static ConcurrentNavigableMap map5() {
33          ConcurrentSkipListMap map = new ConcurrentSkipListMap();
# Line 36 | Line 45 | public class ConcurrentSkipListSubMapTes
45      }
46  
47      /**
48 <     * Create a map from Integers -5 to -1 to Strings "A"-"E".
48 >     * Returns a new map from Integers -5 to -1 to Strings "A"-"E".
49       */
50      private static ConcurrentNavigableMap dmap5() {
51          ConcurrentSkipListMap map = new ConcurrentSkipListMap();
# Line 64 | Line 73 | public class ConcurrentSkipListSubMapTes
73      }
74  
75      /**
76 <     *  clear removes all pairs
76 >     * clear removes all pairs
77       */
78      public void testClear() {
79          ConcurrentNavigableMap map = map5();
80          map.clear();
81 <        assertEquals(map.size(), 0);
81 >        assertEquals(0, map.size());
82      }
83  
75
84      /**
85 <     *  Maps with same contents are equal
85 >     * Maps with same contents are equal
86       */
87      public void testEquals() {
88          ConcurrentNavigableMap map1 = map5();
# Line 87 | Line 95 | public class ConcurrentSkipListSubMapTes
95      }
96  
97      /**
98 <     *  containsKey returns true for contained key
98 >     * containsKey returns true for contained key
99       */
100      public void testContainsKey() {
101          ConcurrentNavigableMap map = map5();
# Line 96 | Line 104 | public class ConcurrentSkipListSubMapTes
104      }
105  
106      /**
107 <     *  containsValue returns true for held values
107 >     * containsValue returns true for held values
108       */
109      public void testContainsValue() {
110          ConcurrentNavigableMap map = map5();
# Line 105 | Line 113 | public class ConcurrentSkipListSubMapTes
113      }
114  
115      /**
116 <     *  get returns the correct element at the given key,
117 <     *  or null if not present
116 >     * get returns the correct element at the given key,
117 >     * or null if not present
118       */
119      public void testGet() {
120          ConcurrentNavigableMap map = map5();
# Line 116 | Line 124 | public class ConcurrentSkipListSubMapTes
124      }
125  
126      /**
127 <     *  isEmpty is true of empty map and false for non-empty
127 >     * isEmpty is true of empty map and false for non-empty
128       */
129      public void testIsEmpty() {
130          ConcurrentNavigableMap empty = map0();
# Line 126 | Line 134 | public class ConcurrentSkipListSubMapTes
134      }
135  
136      /**
137 <     *   firstKey returns first key
137 >     * firstKey returns first key
138       */
139      public void testFirstKey() {
140          ConcurrentNavigableMap map = map5();
# Line 134 | Line 142 | public class ConcurrentSkipListSubMapTes
142      }
143  
144      /**
145 <     *   lastKey returns last key
145 >     * lastKey returns last key
146       */
147      public void testLastKey() {
148          ConcurrentNavigableMap map = map5();
149          assertEquals(five, map.lastKey());
150      }
151  
144
152      /**
153 <     *   keySet returns a Set containing all the keys
153 >     * keySet returns a Set containing all the keys
154       */
155      public void testKeySet() {
156          ConcurrentNavigableMap map = map5();
# Line 157 | Line 164 | public class ConcurrentSkipListSubMapTes
164      }
165  
166      /**
167 <     *   keySet is ordered
167 >     * keySet is ordered
168       */
169      public void testKeySetOrder() {
170          ConcurrentNavigableMap map = map5();
# Line 187 | Line 194 | public class ConcurrentSkipListSubMapTes
194      }
195  
196      /**
197 <     *  keySet.toArray returns contains all keys
197 >     * keySet.toArray returns contains all keys
198       */
199      public void testKeySetToArray() {
200          ConcurrentNavigableMap map = map5();
# Line 200 | Line 207 | public class ConcurrentSkipListSubMapTes
207      }
208  
209      /**
210 <     *  descendingkeySet.toArray returns contains all keys
210 >     * descendingkeySet.toArray returns contains all keys
211       */
212      public void testDescendingKeySetToArray() {
213          ConcurrentNavigableMap map = map5();
# Line 213 | Line 220 | public class ConcurrentSkipListSubMapTes
220      }
221  
222      /**
223 <     *  Values.toArray contains all values
223 >     * Values.toArray contains all values
224       */
225      public void testValuesToArray() {
226          ConcurrentNavigableMap map = map5();
# Line 228 | Line 235 | public class ConcurrentSkipListSubMapTes
235          assertTrue(s.contains("E"));
236      }
237  
231
238      /**
239       * entrySet contains all pairs
240       */
# Line 249 | Line 255 | public class ConcurrentSkipListSubMapTes
255      }
256  
257      /**
258 <     *   putAll  adds all key-value pairs from the given map
258 >     * putAll adds all key-value pairs from the given map
259       */
260      public void testPutAll() {
261          ConcurrentNavigableMap empty = map0();
# Line 264 | Line 270 | public class ConcurrentSkipListSubMapTes
270      }
271  
272      /**
273 <     *   putIfAbsent works when the given key is not present
273 >     * putIfAbsent works when the given key is not present
274       */
275      public void testPutIfAbsent() {
276          ConcurrentNavigableMap map = map5();
# Line 273 | Line 279 | public class ConcurrentSkipListSubMapTes
279      }
280  
281      /**
282 <     *   putIfAbsent does not add the pair if the key is already present
282 >     * putIfAbsent does not add the pair if the key is already present
283       */
284      public void testPutIfAbsent2() {
285          ConcurrentNavigableMap map = map5();
# Line 281 | Line 287 | public class ConcurrentSkipListSubMapTes
287      }
288  
289      /**
290 <     *   replace fails when the given key is not present
290 >     * replace fails when the given key is not present
291       */
292      public void testReplace() {
293          ConcurrentNavigableMap map = map5();
# Line 290 | Line 296 | public class ConcurrentSkipListSubMapTes
296      }
297  
298      /**
299 <     *   replace succeeds if the key is already present
299 >     * replace succeeds if the key is already present
300       */
301      public void testReplace2() {
302          ConcurrentNavigableMap map = map5();
# Line 298 | Line 304 | public class ConcurrentSkipListSubMapTes
304          assertEquals("Z", map.get(one));
305      }
306  
301
307      /**
308       * replace value fails when the given key not mapped to expected value
309       */
# Line 319 | Line 324 | public class ConcurrentSkipListSubMapTes
324          assertEquals("Z", map.get(one));
325      }
326  
322
327      /**
328 <     *   remove removes the correct key-value pair from the map
328 >     * remove removes the correct key-value pair from the map
329       */
330      public void testRemove() {
331          ConcurrentNavigableMap map = map5();
# Line 343 | Line 347 | public class ConcurrentSkipListSubMapTes
347          map.remove(four, "A");
348          assertEquals(4, map.size());
349          assertTrue(map.containsKey(four));
346
350      }
351  
352      /**
# Line 362 | Line 365 | public class ConcurrentSkipListSubMapTes
365  
366          Map.Entry e4 = map.lowerEntry(zero);
367          assertNull(e4);
365
368      }
369  
370      /**
# Line 381 | Line 383 | public class ConcurrentSkipListSubMapTes
383  
384          Map.Entry e4 = map.higherEntry(six);
385          assertNull(e4);
384
386      }
387  
388      /**
# Line 400 | Line 401 | public class ConcurrentSkipListSubMapTes
401  
402          Map.Entry e4 = map.floorEntry(zero);
403          assertNull(e4);
403
404      }
405  
406      /**
# Line 419 | Line 419 | public class ConcurrentSkipListSubMapTes
419  
420          Map.Entry e4 = map.ceilingEntry(six);
421          assertNull(e4);
422
422      }
423  
424      /**
# Line 477 | Line 476 | public class ConcurrentSkipListSubMapTes
476      }
477  
478      /**
479 <     *   size returns the correct values
479 >     * size returns the correct values
480       */
481      public void testSize() {
482          ConcurrentNavigableMap map = map5();
# Line 493 | Line 492 | public class ConcurrentSkipListSubMapTes
492          ConcurrentNavigableMap map = map5();
493          String s = map.toString();
494          for (int i = 1; i <= 5; ++i) {
495 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
495 >            assertTrue(s.contains(String.valueOf(i)));
496          }
497      }
498  
# Line 532 | Line 531 | public class ConcurrentSkipListSubMapTes
531          } catch (NullPointerException success) {}
532      }
533  
535
534      /**
535       * put(null,x) throws NPE
536       */
# Line 603 | Line 601 | public class ConcurrentSkipListSubMapTes
601       * A deserialized map equals original
602       */
603      public void testSerialization() throws Exception {
604 <        ConcurrentNavigableMap q = map5();
604 >        NavigableMap x = map5();
605 >        NavigableMap y = serialClone(x);
606  
607 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
608 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
609 <        out.writeObject(q);
610 <        out.close();
611 <
613 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
614 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
615 <        ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
616 <        assertEquals(q.size(), r.size());
617 <        assertTrue(q.equals(r));
618 <        assertTrue(r.equals(q));
607 >        assertNotSame(x, y);
608 >        assertEquals(x.size(), y.size());
609 >        assertEquals(x.toString(), y.toString());
610 >        assertEquals(x, y);
611 >        assertEquals(y, x);
612      }
613  
621
622
614      /**
615       * subMap returns map with keys in requested range
616       */
# Line 649 | Line 640 | public class ConcurrentSkipListSubMapTes
640          assertEquals(1, sm.size());
641          assertEquals(three, sm.firstKey());
642          assertEquals(three, sm.lastKey());
643 <        assertTrue(sm.remove(three) != null);
643 >        assertEquals("C", sm.remove(three));
644          assertTrue(sm.isEmpty());
645          assertEquals(3, map.size());
646      }
# Line 677 | Line 668 | public class ConcurrentSkipListSubMapTes
668          assertEquals(4, map.size());
669          assertEquals(0, sm.size());
670          assertTrue(sm.isEmpty());
671 <        assertTrue(sm.remove(three) == null);
671 >        assertSame(sm.remove(three), null);
672          assertEquals(4, map.size());
673      }
674  
# Line 749 | Line 740 | public class ConcurrentSkipListSubMapTes
740          SortedMap ssm = sm.tailMap(four);
741          assertEquals(four, ssm.firstKey());
742          assertEquals(five, ssm.lastKey());
743 <        assertTrue(ssm.remove(four) != null);
743 >        assertEquals("D", ssm.remove(four));
744          assertEquals(1, ssm.size());
745          assertEquals(3, sm.size());
746          assertEquals(4, map.size());
747      }
748  
749      /**
750 <     *  clear removes all pairs
750 >     * clear removes all pairs
751       */
752      public void testDescendingClear() {
753          ConcurrentNavigableMap map = dmap5();
754          map.clear();
755 <        assertEquals(map.size(), 0);
755 >        assertEquals(0, map.size());
756      }
757  
767
758      /**
759 <     *  Maps with same contents are equal
759 >     * Maps with same contents are equal
760       */
761      public void testDescendingEquals() {
762          ConcurrentNavigableMap map1 = dmap5();
# Line 779 | Line 769 | public class ConcurrentSkipListSubMapTes
769      }
770  
771      /**
772 <     *  containsKey returns true for contained key
772 >     * containsKey returns true for contained key
773       */
774      public void testDescendingContainsKey() {
775          ConcurrentNavigableMap map = dmap5();
# Line 788 | Line 778 | public class ConcurrentSkipListSubMapTes
778      }
779  
780      /**
781 <     *  containsValue returns true for held values
781 >     * containsValue returns true for held values
782       */
783      public void testDescendingContainsValue() {
784          ConcurrentNavigableMap map = dmap5();
# Line 797 | Line 787 | public class ConcurrentSkipListSubMapTes
787      }
788  
789      /**
790 <     *  get returns the correct element at the given key,
791 <     *  or null if not present
790 >     * get returns the correct element at the given key,
791 >     * or null if not present
792       */
793      public void testDescendingGet() {
794          ConcurrentNavigableMap map = dmap5();
# Line 808 | Line 798 | public class ConcurrentSkipListSubMapTes
798      }
799  
800      /**
801 <     *  isEmpty is true of empty map and false for non-empty
801 >     * isEmpty is true of empty map and false for non-empty
802       */
803      public void testDescendingIsEmpty() {
804          ConcurrentNavigableMap empty = dmap0();
# Line 818 | Line 808 | public class ConcurrentSkipListSubMapTes
808      }
809  
810      /**
811 <     *   firstKey returns first key
811 >     * firstKey returns first key
812       */
813      public void testDescendingFirstKey() {
814          ConcurrentNavigableMap map = dmap5();
# Line 826 | Line 816 | public class ConcurrentSkipListSubMapTes
816      }
817  
818      /**
819 <     *   lastKey returns last key
819 >     * lastKey returns last key
820       */
821      public void testDescendingLastKey() {
822          ConcurrentNavigableMap map = dmap5();
823          assertEquals(m5, map.lastKey());
824      }
825  
836
826      /**
827 <     *   keySet returns a Set containing all the keys
827 >     * keySet returns a Set containing all the keys
828       */
829      public void testDescendingKeySet() {
830          ConcurrentNavigableMap map = dmap5();
# Line 849 | Line 838 | public class ConcurrentSkipListSubMapTes
838      }
839  
840      /**
841 <     *   keySet is ordered
841 >     * keySet is ordered
842       */
843      public void testDescendingKeySetOrder() {
844          ConcurrentNavigableMap map = dmap5();
# Line 879 | Line 868 | public class ConcurrentSkipListSubMapTes
868      }
869  
870      /**
871 <     *  keySet.toArray returns contains all keys
871 >     * keySet.toArray returns contains all keys
872       */
873      public void testDescendingAscendingKeySetToArray() {
874          ConcurrentNavigableMap map = dmap5();
# Line 892 | Line 881 | public class ConcurrentSkipListSubMapTes
881      }
882  
883      /**
884 <     *  descendingkeySet.toArray returns contains all keys
884 >     * descendingkeySet.toArray returns contains all keys
885       */
886      public void testDescendingDescendingKeySetToArray() {
887          ConcurrentNavigableMap map = dmap5();
# Line 905 | Line 894 | public class ConcurrentSkipListSubMapTes
894      }
895  
896      /**
897 <     *  Values.toArray contains all values
897 >     * Values.toArray contains all values
898       */
899      public void testDescendingValuesToArray() {
900          ConcurrentNavigableMap map = dmap5();
# Line 920 | Line 909 | public class ConcurrentSkipListSubMapTes
909          assertTrue(s.contains("E"));
910      }
911  
923
912      /**
913       * entrySet contains all pairs
914       */
# Line 941 | Line 929 | public class ConcurrentSkipListSubMapTes
929      }
930  
931      /**
932 <     *   putAll  adds all key-value pairs from the given map
932 >     * putAll adds all key-value pairs from the given map
933       */
934      public void testDescendingPutAll() {
935          ConcurrentNavigableMap empty = dmap0();
# Line 956 | Line 944 | public class ConcurrentSkipListSubMapTes
944      }
945  
946      /**
947 <     *   putIfAbsent works when the given key is not present
947 >     * putIfAbsent works when the given key is not present
948       */
949      public void testDescendingPutIfAbsent() {
950          ConcurrentNavigableMap map = dmap5();
# Line 965 | Line 953 | public class ConcurrentSkipListSubMapTes
953      }
954  
955      /**
956 <     *   putIfAbsent does not add the pair if the key is already present
956 >     * putIfAbsent does not add the pair if the key is already present
957       */
958      public void testDescendingPutIfAbsent2() {
959          ConcurrentNavigableMap map = dmap5();
# Line 973 | Line 961 | public class ConcurrentSkipListSubMapTes
961      }
962  
963      /**
964 <     *   replace fails when the given key is not present
964 >     * replace fails when the given key is not present
965       */
966      public void testDescendingReplace() {
967          ConcurrentNavigableMap map = dmap5();
# Line 982 | Line 970 | public class ConcurrentSkipListSubMapTes
970      }
971  
972      /**
973 <     *   replace succeeds if the key is already present
973 >     * replace succeeds if the key is already present
974       */
975      public void testDescendingReplace2() {
976          ConcurrentNavigableMap map = dmap5();
# Line 990 | Line 978 | public class ConcurrentSkipListSubMapTes
978          assertEquals("Z", map.get(m1));
979      }
980  
993
981      /**
982       * replace value fails when the given key not mapped to expected value
983       */
# Line 1011 | Line 998 | public class ConcurrentSkipListSubMapTes
998          assertEquals("Z", map.get(m1));
999      }
1000  
1014
1001      /**
1002 <     *   remove removes the correct key-value pair from the map
1002 >     * remove removes the correct key-value pair from the map
1003       */
1004      public void testDescendingRemove() {
1005          ConcurrentNavigableMap map = dmap5();
# Line 1035 | Line 1021 | public class ConcurrentSkipListSubMapTes
1021          map.remove(m4, "A");
1022          assertEquals(4, map.size());
1023          assertTrue(map.containsKey(m4));
1038
1024      }
1025  
1026      /**
# Line 1054 | Line 1039 | public class ConcurrentSkipListSubMapTes
1039  
1040          Map.Entry e4 = map.lowerEntry(zero);
1041          assertNull(e4);
1057
1042      }
1043  
1044      /**
# Line 1073 | Line 1057 | public class ConcurrentSkipListSubMapTes
1057  
1058          Map.Entry e4 = map.higherEntry(m6);
1059          assertNull(e4);
1076
1060      }
1061  
1062      /**
# Line 1092 | Line 1075 | public class ConcurrentSkipListSubMapTes
1075  
1076          Map.Entry e4 = map.floorEntry(zero);
1077          assertNull(e4);
1095
1078      }
1079  
1080      /**
# Line 1111 | Line 1093 | public class ConcurrentSkipListSubMapTes
1093  
1094          Map.Entry e4 = map.ceilingEntry(m6);
1095          assertNull(e4);
1114
1096      }
1097  
1098      /**
# Line 1169 | Line 1150 | public class ConcurrentSkipListSubMapTes
1150      }
1151  
1152      /**
1153 <     *   size returns the correct values
1153 >     * size returns the correct values
1154       */
1155      public void testDescendingSize() {
1156          ConcurrentNavigableMap map = dmap5();
# Line 1185 | Line 1166 | public class ConcurrentSkipListSubMapTes
1166          ConcurrentNavigableMap map = dmap5();
1167          String s = map.toString();
1168          for (int i = 1; i <= 5; ++i) {
1169 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1169 >            assertTrue(s.contains(String.valueOf(i)));
1170          }
1171      }
1172  
1173      // Exception testDescendings
1174  
1175      /**
1176 <     * get(null) of nm1mpty map throws NPE
1176 >     * get(null) of empty map throws NPE
1177       */
1178      public void testDescendingGet_NullPointerException() {
1179          try {
# Line 1203 | Line 1184 | public class ConcurrentSkipListSubMapTes
1184      }
1185  
1186      /**
1187 <     * containsKey(null) of nm1mpty map throws NPE
1187 >     * containsKey(null) of empty map throws NPE
1188       */
1189      public void testDescendingContainsKey_NullPointerException() {
1190          try {
# Line 1224 | Line 1205 | public class ConcurrentSkipListSubMapTes
1205          } catch (NullPointerException success) {}
1206      }
1207  
1227
1208      /**
1209       * put(null,x) throws NPE
1210       */
# Line 1295 | Line 1275 | public class ConcurrentSkipListSubMapTes
1275       * A deserialized map equals original
1276       */
1277      public void testDescendingSerialization() throws Exception {
1278 <        ConcurrentNavigableMap q = dmap5();
1278 >        NavigableMap x = dmap5();
1279 >        NavigableMap y = serialClone(x);
1280  
1281 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1282 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1283 <        out.writeObject(q);
1284 <        out.close();
1285 <
1305 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1306 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1307 <        ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
1308 <        assertEquals(q.size(), r.size());
1309 <        assertTrue(q.equals(r));
1310 <        assertTrue(r.equals(q));
1281 >        assertNotSame(x, y);
1282 >        assertEquals(x.size(), y.size());
1283 >        assertEquals(x.toString(), y.toString());
1284 >        assertEquals(x, y);
1285 >        assertEquals(y, x);
1286      }
1287  
1313
1288      /**
1289       * subMap returns map with keys in requested range
1290       */
# Line 1340 | Line 1314 | public class ConcurrentSkipListSubMapTes
1314          assertEquals(1, sm.size());
1315          assertEquals(m3, sm.firstKey());
1316          assertEquals(m3, sm.lastKey());
1317 <        assertTrue(sm.remove(m3) != null);
1317 >        assertEquals("C", sm.remove(m3));
1318          assertTrue(sm.isEmpty());
1319          assertEquals(3, map.size());
1320      }
# Line 1368 | Line 1342 | public class ConcurrentSkipListSubMapTes
1342          assertEquals(4, map.size());
1343          assertEquals(0, sm.size());
1344          assertTrue(sm.isEmpty());
1345 <        assertTrue(sm.remove(m3) == null);
1345 >        assertSame(sm.remove(m3), null);
1346          assertEquals(4, map.size());
1347      }
1348  
# Line 1440 | Line 1414 | public class ConcurrentSkipListSubMapTes
1414          SortedMap ssm = sm.tailMap(m4);
1415          assertEquals(m4, ssm.firstKey());
1416          assertEquals(m5, ssm.lastKey());
1417 <        assertTrue(ssm.remove(m4) != null);
1417 >        assertEquals("D", ssm.remove(m4));
1418          assertEquals(1, ssm.size());
1419          assertEquals(3, sm.size());
1420          assertEquals(4, map.size());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines