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.1 by dl, Tue Dec 28 16:15:59 2004 UTC vs.
Revision 1.27 by dl, Tue Jan 26 13:33:05 2021 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 >        main(suite(), args);
24      }
25      public static Test suite() {
26 <        return new TestSuite(ConcurrentSkipListSubMapTest.class);
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 Items 1-5 to Strings "A"-"E".
31       */
32 <    private static ConcurrentNavigableMap map5() {  
33 <        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
32 >    private static ConcurrentNavigableMap<Item,String> map5() {
33 >        ConcurrentSkipListMap<Item,String>map = new ConcurrentSkipListMap<Item,String>();
34          assertTrue(map.isEmpty());
35 <        map.put(zero, "Z");
36 <        map.put(one, "A");
37 <        map.put(five, "E");
38 <        map.put(three, "C");
39 <        map.put(two, "B");
40 <        map.put(four, "D");
41 <        map.put(seven, "F");
35 >        map.put(zero, "Z");
36 >        map.put(one, "A");
37 >        map.put(five, "E");
38 >        map.put(three, "C");
39 >        map.put(two, "B");
40 >        map.put(four, "D");
41 >        map.put(seven, "F");
42          assertFalse(map.isEmpty());
43 <        assertEquals(7, map.size());
44 <        return map.subMap(one, seven);
43 >        mustEqual(7, map.size());
44 >        return map.subMap(one, true, seven, false);
45 >    }
46 >
47 >    /**
48 >     * Returns a new map from Items -5 to -1 to Strings "A"-"E".
49 >     */
50 >    private static ConcurrentNavigableMap<Item,String> dmap5() {
51 >        ConcurrentSkipListMap<Item,String>map = new ConcurrentSkipListMap<Item,String>();
52 >        assertTrue(map.isEmpty());
53 >        map.put(minusOne, "A");
54 >        map.put(minusFive, "E");
55 >        map.put(minusThree, "C");
56 >        map.put(minusTwo, "B");
57 >        map.put(minusFour, "D");
58 >        assertFalse(map.isEmpty());
59 >        mustEqual(5, map.size());
60 >        return map.descendingMap();
61 >    }
62 >
63 >    private static ConcurrentNavigableMap<Item,String> map0() {
64 >        ConcurrentSkipListMap<Item,String>map = new ConcurrentSkipListMap<Item,String>();
65 >        assertTrue(map.isEmpty());
66 >        return map.tailMap(one, true);
67      }
68  
69 <    private static ConcurrentNavigableMap map0() {  
70 <        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
69 >    private static ConcurrentNavigableMap<Item,String> dmap0() {
70 >        ConcurrentSkipListMap<Item,String>map = new ConcurrentSkipListMap<Item,String>();
71          assertTrue(map.isEmpty());
72 <        return map.tailMap(one);
72 >        return map;
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);
79 >        ConcurrentNavigableMap<Item,String> map = map5();
80 >        map.clear();
81 >        mustEqual(0, map.size());
82      }
83  
53
84      /**
85 <     *  Maps with same contents are equal
85 >     * Maps with same contents are equal
86       */
87      public void testEquals() {
88 <        ConcurrentNavigableMap map1 = map5();
89 <        ConcurrentNavigableMap map2 = map5();
90 <        assertEquals(map1, map2);
91 <        assertEquals(map2, map1);
92 <        map1.clear();
88 >        ConcurrentNavigableMap<Item,String> map1 = map5();
89 >        ConcurrentNavigableMap<Item,String> map2 = map5();
90 >        mustEqual(map1, map2);
91 >        mustEqual(map2, map1);
92 >        map1.clear();
93          assertFalse(map1.equals(map2));
94          assertFalse(map2.equals(map1));
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();
102 <        assertTrue(map.containsKey(one));
101 >        ConcurrentNavigableMap<Item,String> map = map5();
102 >        assertTrue(map.containsKey(one));
103          assertFalse(map.containsKey(zero));
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();
111 <        assertTrue(map.containsValue("A"));
110 >        ConcurrentNavigableMap<Item,String> map = map5();
111 >        assertTrue(map.containsValue("A"));
112          assertFalse(map.containsValue("Z"));
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();
121 <        assertEquals("A", (String)map.get(one));
122 <        ConcurrentNavigableMap empty = map0();
120 >        ConcurrentNavigableMap<Item,String> map = map5();
121 >        mustEqual("A", map.get(one));
122 >        ConcurrentNavigableMap<Item,String> empty = map0();
123          assertNull(empty.get(one));
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();
131 <        ConcurrentNavigableMap map = map5();
132 <        assertTrue(empty.isEmpty());
130 >        ConcurrentNavigableMap<Item,String> empty = map0();
131 >        ConcurrentNavigableMap<Item,String> map = map5();
132 >        assertTrue(empty.isEmpty());
133          assertFalse(map.isEmpty());
134      }
135  
136      /**
137 <     *   firstKey returns first key
137 >     * firstKey returns first key
138       */
139      public void testFirstKey() {
140 <        ConcurrentNavigableMap map = map5();
141 <        assertEquals(one, map.firstKey());
140 >        ConcurrentNavigableMap<Item,String> map = map5();
141 >        mustEqual(one, map.firstKey());
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());
148 >        ConcurrentNavigableMap<Item,String> map = map5();
149 >        mustEqual(five, map.lastKey());
150      }
151  
122
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();
157 <        Set s = map.keySet();
158 <        assertEquals(5, s.size());
159 <        assertTrue(s.contains(one));
160 <        assertTrue(s.contains(two));
161 <        assertTrue(s.contains(three));
162 <        assertTrue(s.contains(four));
163 <        assertTrue(s.contains(five));
156 >        ConcurrentNavigableMap<Item,String> map = map5();
157 >        Set<Item> s = map.keySet();
158 >        mustEqual(5, s.size());
159 >        mustContain(s, one);
160 >        mustContain(s, two);
161 >        mustContain(s, three);
162 >        mustContain(s, four);
163 >        mustContain(s, five);
164      }
165  
166      /**
167 <     *   keySet is ordered
167 >     * keySet is ordered
168       */
169      public void testKeySetOrder() {
170 <        ConcurrentNavigableMap map = map5();
171 <        Set s = map.keySet();
172 <        Iterator i = s.iterator();
173 <        Integer last = (Integer)i.next();
174 <        assertEquals(last, one);
170 >        ConcurrentNavigableMap<Item,String> map = map5();
171 >        Set<Item> s = map.keySet();
172 >        Iterator<? extends Item> i = s.iterator();
173 >        Item last = i.next();
174 >        mustEqual(last, one);
175          while (i.hasNext()) {
176 <            Integer k = (Integer)i.next();
176 >            Item k = i.next();
177              assertTrue(last.compareTo(k) < 0);
178              last = k;
179          }
# Line 154 | Line 183 | public class ConcurrentSkipListSubMapTes
183       * values collection contains all values
184       */
185      public void testValues() {
186 <        ConcurrentNavigableMap map = map5();
187 <        Collection s = map.values();
188 <        assertEquals(5, s.size());
189 <        assertTrue(s.contains("A"));
190 <        assertTrue(s.contains("B"));
191 <        assertTrue(s.contains("C"));
192 <        assertTrue(s.contains("D"));
193 <        assertTrue(s.contains("E"));
186 >        ConcurrentNavigableMap<Item,String> map = map5();
187 >        Collection<String> s = map.values();
188 >        mustEqual(5, s.size());
189 >        assertTrue(s.contains("A"));
190 >        assertTrue(s.contains("B"));
191 >        assertTrue(s.contains("C"));
192 >        assertTrue(s.contains("D"));
193 >        assertTrue(s.contains("E"));
194 >    }
195 >
196 >    /**
197 >     * keySet.toArray returns contains all keys
198 >     */
199 >    public void testKeySetToArray() {
200 >        ConcurrentNavigableMap<Item,String> map = map5();
201 >        Set<Item> s = map.keySet();
202 >        Object[] ar = s.toArray();
203 >        assertTrue(s.containsAll(Arrays.asList(ar)));
204 >        mustEqual(5, ar.length);
205 >        ar[0] = minusTen;
206 >        assertFalse(s.containsAll(Arrays.asList(ar)));
207 >    }
208 >
209 >    /**
210 >     * descendingkeySet.toArray returns contains all keys
211 >     */
212 >    public void testDescendingKeySetToArray() {
213 >        ConcurrentNavigableMap<Item,String> map = map5();
214 >        Set<Item> s = map.descendingKeySet();
215 >        Item[] ar = s.toArray(new Item[0]);
216 >        mustEqual(5, ar.length);
217 >        assertTrue(s.containsAll(Arrays.asList(ar)));
218 >        ar[0] = minusTen;
219 >        assertFalse(s.containsAll(Arrays.asList(ar)));
220 >    }
221 >
222 >    /**
223 >     * Values.toArray contains all values
224 >     */
225 >    public void testValuesToArray() {
226 >        ConcurrentNavigableMap<Item,String> map = map5();
227 >        Collection<String> v = map.values();
228 >        String[] ar = v.toArray(new String[0]);
229 >        ArrayList<String> s = new ArrayList<String>(Arrays.asList(ar));
230 >        mustEqual(5, ar.length);
231 >        assertTrue(s.contains("A"));
232 >        assertTrue(s.contains("B"));
233 >        assertTrue(s.contains("C"));
234 >        assertTrue(s.contains("D"));
235 >        assertTrue(s.contains("E"));
236      }
237  
238      /**
239       * entrySet contains all pairs
240       */
241      public void testEntrySet() {
242 <        ConcurrentNavigableMap map = map5();
243 <        Set s = map.entrySet();
244 <        assertEquals(5, s.size());
245 <        Iterator it = s.iterator();
242 >        ConcurrentNavigableMap<Item,String> map = map5();
243 >        Set<Map.Entry<Item,String>> s = map.entrySet();
244 >        mustEqual(5, s.size());
245 >        Iterator<Map.Entry<Item,String>> it = s.iterator();
246          while (it.hasNext()) {
247 <            Map.Entry e = (Map.Entry) it.next();
248 <            assertTrue(
247 >            Map.Entry<Item,String> e = it.next();
248 >            assertTrue(
249                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
250                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
251                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 184 | 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();
262 <        ConcurrentNavigableMap map = map5();
263 <        empty.putAll(map);
264 <        assertEquals(5, empty.size());
265 <        assertTrue(empty.containsKey(one));
266 <        assertTrue(empty.containsKey(two));
267 <        assertTrue(empty.containsKey(three));
268 <        assertTrue(empty.containsKey(four));
269 <        assertTrue(empty.containsKey(five));
261 >        ConcurrentNavigableMap<Item,String> empty = map0();
262 >        ConcurrentNavigableMap<Item,String> map = map5();
263 >        empty.putAll(map);
264 >        mustEqual(5, empty.size());
265 >        assertTrue(empty.containsKey(one));
266 >        assertTrue(empty.containsKey(two));
267 >        assertTrue(empty.containsKey(three));
268 >        assertTrue(empty.containsKey(four));
269 >        assertTrue(empty.containsKey(five));
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();
277 <        map.putIfAbsent(six, "Z");
276 >        ConcurrentNavigableMap<Item,String> map = map5();
277 >        map.putIfAbsent(six, "Z");
278          assertTrue(map.containsKey(six));
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();
286 <        assertEquals("A", map.putIfAbsent(one, "Z"));
285 >        ConcurrentNavigableMap<Item,String> map = map5();
286 >        mustEqual("A", map.putIfAbsent(one, "Z"));
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();
294 <        assertNull(map.replace(six, "Z"));
293 >        ConcurrentNavigableMap<Item,String> map = map5();
294 >        assertNull(map.replace(six, "Z"));
295          assertFalse(map.containsKey(six));
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();
302 >        ConcurrentNavigableMap<Item,String> map = map5();
303          assertNotNull(map.replace(one, "Z"));
304 <        assertEquals("Z", map.get(one));
304 >        mustEqual("Z", map.get(one));
305      }
306  
236
307      /**
308       * replace value fails when the given key not mapped to expected value
309       */
310      public void testReplaceValue() {
311 <        ConcurrentNavigableMap map = map5();
312 <        assertEquals("A", map.get(one));
313 <        assertFalse(map.replace(one, "Z", "Z"));
314 <        assertEquals("A", map.get(one));
311 >        ConcurrentNavigableMap<Item,String> map = map5();
312 >        mustEqual("A", map.get(one));
313 >        assertFalse(map.replace(one, "Z", "Z"));
314 >        mustEqual("A", map.get(one));
315      }
316  
317      /**
318       * replace value succeeds when the given key mapped to expected value
319       */
320      public void testReplaceValue2() {
321 <        ConcurrentNavigableMap map = map5();
322 <        assertEquals("A", map.get(one));
323 <        assertTrue(map.replace(one, "A", "Z"));
324 <        assertEquals("Z", map.get(one));
321 >        ConcurrentNavigableMap<Item,String> map = map5();
322 >        mustEqual("A", map.get(one));
323 >        assertTrue(map.replace(one, "A", "Z"));
324 >        mustEqual("Z", map.get(one));
325      }
326  
257
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();
332 <        map.remove(five);
333 <        assertEquals(4, map.size());
334 <        assertFalse(map.containsKey(five));
331 >        ConcurrentNavigableMap<Item,String> map = map5();
332 >        map.remove(five);
333 >        mustEqual(4, map.size());
334 >        assertFalse(map.containsKey(five));
335      }
336  
337      /**
338       * remove(key,value) removes only if pair present
339       */
340      public void testRemove2() {
341 <        ConcurrentNavigableMap map = map5();
342 <        assertTrue(map.containsKey(five));
343 <        assertEquals("E", map.get(five));
344 <        map.remove(five, "E");
345 <        assertEquals(4, map.size());
346 <        assertFalse(map.containsKey(five));
347 <        map.remove(four, "A");
348 <        assertEquals(4, map.size());
349 <        assertTrue(map.containsKey(four));
281 <
341 >        ConcurrentNavigableMap<Item,String> map = map5();
342 >        assertTrue(map.containsKey(five));
343 >        mustEqual("E", map.get(five));
344 >        map.remove(five, "E");
345 >        mustEqual(4, map.size());
346 >        assertFalse(map.containsKey(five));
347 >        map.remove(four, "A");
348 >        mustEqual(4, map.size());
349 >        assertTrue(map.containsKey(four));
350      }
351  
352      /**
353       * lowerEntry returns preceding entry.
354       */
355      public void testLowerEntry() {
356 <        ConcurrentNavigableMap map = map5();
357 <        Map.Entry e1 = map.lowerEntry(three);
358 <        assertEquals(two, e1.getKey());
356 >        ConcurrentNavigableMap<Item,String> map = map5();
357 >        Map.Entry<Item,String> e1 = map.lowerEntry(three);
358 >        mustEqual(two, e1.getKey());
359  
360 <        Map.Entry e2 = map.lowerEntry(six);
361 <        assertEquals(five, e2.getKey());
360 >        Map.Entry<Item,String> e2 = map.lowerEntry(six);
361 >        mustEqual(five, e2.getKey());
362  
363 <        Map.Entry e3 = map.lowerEntry(one);
363 >        Map.Entry<Item,String> e3 = map.lowerEntry(one);
364          assertNull(e3);
365  
366 <        Map.Entry e4 = map.lowerEntry(zero);
366 >        Map.Entry<Item,String> e4 = map.lowerEntry(zero);
367          assertNull(e4);
300
368      }
369  
370      /**
371       * higherEntry returns next entry.
372       */
373      public void testHigherEntry() {
374 <        ConcurrentNavigableMap map = map5();
375 <        Map.Entry e1 = map.higherEntry(three);
376 <        assertEquals(four, e1.getKey());
374 >        ConcurrentNavigableMap<Item,String> map = map5();
375 >        Map.Entry<Item,String> e1 = map.higherEntry(three);
376 >        mustEqual(four, e1.getKey());
377  
378 <        Map.Entry e2 = map.higherEntry(zero);
379 <        assertEquals(one, e2.getKey());
378 >        Map.Entry<Item,String> e2 = map.higherEntry(zero);
379 >        mustEqual(one, e2.getKey());
380  
381 <        Map.Entry e3 = map.higherEntry(five);
381 >        Map.Entry<Item,String> e3 = map.higherEntry(five);
382          assertNull(e3);
383  
384 <        Map.Entry e4 = map.higherEntry(six);
384 >        Map.Entry<Item,String> e4 = map.higherEntry(six);
385          assertNull(e4);
319
386      }
387  
388      /**
389       * floorEntry returns preceding entry.
390       */
391      public void testFloorEntry() {
392 <        ConcurrentNavigableMap map = map5();
393 <        Map.Entry e1 = map.floorEntry(three);
394 <        assertEquals(three, e1.getKey());
392 >        ConcurrentNavigableMap<Item,String> map = map5();
393 >        Map.Entry<Item,String> e1 = map.floorEntry(three);
394 >        mustEqual(three, e1.getKey());
395  
396 <        Map.Entry e2 = map.floorEntry(six);
397 <        assertEquals(five, e2.getKey());
396 >        Map.Entry<Item,String> e2 = map.floorEntry(six);
397 >        mustEqual(five, e2.getKey());
398  
399 <        Map.Entry e3 = map.floorEntry(one);
400 <        assertEquals(one, e3.getKey());
399 >        Map.Entry<Item,String> e3 = map.floorEntry(one);
400 >        mustEqual(one, e3.getKey());
401  
402 <        Map.Entry e4 = map.floorEntry(zero);
402 >        Map.Entry<Item,String> e4 = map.floorEntry(zero);
403          assertNull(e4);
338
404      }
405  
406      /**
407       * ceilingEntry returns next entry.
408       */
409      public void testCeilingEntry() {
410 <        ConcurrentNavigableMap map = map5();
411 <        Map.Entry e1 = map.ceilingEntry(three);
412 <        assertEquals(three, e1.getKey());
410 >        ConcurrentNavigableMap<Item,String> map = map5();
411 >        Map.Entry<Item,String> e1 = map.ceilingEntry(three);
412 >        mustEqual(three, e1.getKey());
413  
414 <        Map.Entry e2 = map.ceilingEntry(zero);
415 <        assertEquals(one, e2.getKey());
414 >        Map.Entry<Item,String> e2 = map.ceilingEntry(zero);
415 >        mustEqual(one, e2.getKey());
416  
417 <        Map.Entry e3 = map.ceilingEntry(five);
418 <        assertEquals(five, e3.getKey());
417 >        Map.Entry<Item,String> e3 = map.ceilingEntry(five);
418 >        mustEqual(five, e3.getKey());
419  
420 <        Map.Entry e4 = map.ceilingEntry(six);
420 >        Map.Entry<Item,String> e4 = map.ceilingEntry(six);
421          assertNull(e4);
357
422      }
423  
424      /**
425       * pollFirstEntry returns entries in order
426       */
427      public void testPollFirstEntry() {
428 <        ConcurrentNavigableMap map = map5();
429 <        Map.Entry e = map.pollFirstEntry();
430 <        assertEquals(one, e.getKey());
431 <        assertEquals("A", e.getValue());
428 >        ConcurrentNavigableMap<Item,String> map = map5();
429 >        Map.Entry<Item,String> e = map.pollFirstEntry();
430 >        mustEqual(one, e.getKey());
431 >        mustEqual("A", e.getValue());
432          e = map.pollFirstEntry();
433 <        assertEquals(two, e.getKey());
433 >        mustEqual(two, e.getKey());
434          map.put(one, "A");
435          e = map.pollFirstEntry();
436 <        assertEquals(one, e.getKey());
437 <        assertEquals("A", e.getValue());
436 >        mustEqual(one, e.getKey());
437 >        mustEqual("A", e.getValue());
438          e = map.pollFirstEntry();
439 <        assertEquals(three, e.getKey());
439 >        mustEqual(three, e.getKey());
440          map.remove(four);
441          e = map.pollFirstEntry();
442 <        assertEquals(five, e.getKey());
442 >        mustEqual(five, e.getKey());
443          try {
444              e.setValue("A");
445              shouldThrow();
446 <        } catch (Exception ok) {
383 <        }
446 >        } catch (UnsupportedOperationException success) {}
447          e = map.pollFirstEntry();
448          assertNull(e);
449      }
# Line 389 | Line 452 | public class ConcurrentSkipListSubMapTes
452       * pollLastEntry returns entries in order
453       */
454      public void testPollLastEntry() {
455 <        ConcurrentNavigableMap map = map5();
456 <        Map.Entry e = map.pollLastEntry();
457 <        assertEquals(five, e.getKey());
458 <        assertEquals("E", e.getValue());
455 >        ConcurrentNavigableMap<Item,String> map = map5();
456 >        Map.Entry<Item,String> e = map.pollLastEntry();
457 >        mustEqual(five, e.getKey());
458 >        mustEqual("E", e.getValue());
459          e = map.pollLastEntry();
460 <        assertEquals(four, e.getKey());
460 >        mustEqual(four, e.getKey());
461          map.put(five, "E");
462          e = map.pollLastEntry();
463 <        assertEquals(five, e.getKey());
464 <        assertEquals("E", e.getValue());
463 >        mustEqual(five, e.getKey());
464 >        mustEqual("E", e.getValue());
465          e = map.pollLastEntry();
466 <        assertEquals(three, e.getKey());
466 >        mustEqual(three, e.getKey());
467          map.remove(two);
468          e = map.pollLastEntry();
469 <        assertEquals(one, e.getKey());
469 >        mustEqual(one, e.getKey());
470          try {
471              e.setValue("E");
472              shouldThrow();
473 <        } catch (Exception ok) {
411 <        }
473 >        } catch (UnsupportedOperationException success) {}
474          e = map.pollLastEntry();
475          assertNull(e);
476      }
477  
478      /**
479 <     *   size returns the correct values
479 >     * size returns the correct values
480       */
481      public void testSize() {
482 <        ConcurrentNavigableMap map = map5();
483 <        ConcurrentNavigableMap empty = map0();
484 <        assertEquals(0, empty.size());
485 <        assertEquals(5, map.size());
482 >        ConcurrentNavigableMap<Item,String> map = map5();
483 >        ConcurrentNavigableMap<Item,String> empty = map0();
484 >        mustEqual(0, empty.size());
485 >        mustEqual(5, map.size());
486      }
487  
488      /**
489       * toString contains toString of elements
490       */
491      public void testToString() {
492 <        ConcurrentNavigableMap map = map5();
492 >        ConcurrentNavigableMap<Item,String> 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 <    }        
497 >    }
498  
499      // Exception tests
500  
# Line 441 | Line 503 | public class ConcurrentSkipListSubMapTes
503       */
504      public void testGet_NullPointerException() {
505          try {
506 <            ConcurrentNavigableMap c = map5();
506 >            ConcurrentNavigableMap<Item,String> c = map5();
507              c.get(null);
508              shouldThrow();
509 <        } catch(NullPointerException e){}
509 >        } catch (NullPointerException success) {}
510      }
511  
512      /**
# Line 452 | Line 514 | public class ConcurrentSkipListSubMapTes
514       */
515      public void testContainsKey_NullPointerException() {
516          try {
517 <            ConcurrentNavigableMap c = map5();
517 >            ConcurrentNavigableMap<Item,String> c = map5();
518              c.containsKey(null);
519              shouldThrow();
520 <        } catch(NullPointerException e){}
520 >        } catch (NullPointerException success) {}
521      }
522  
523      /**
# Line 463 | Line 525 | public class ConcurrentSkipListSubMapTes
525       */
526      public void testContainsValue_NullPointerException() {
527          try {
528 <            ConcurrentNavigableMap c = map0();
528 >            ConcurrentNavigableMap<Item,String> c = map0();
529              c.containsValue(null);
530              shouldThrow();
531 <        } catch(NullPointerException e){}
531 >        } catch (NullPointerException success) {}
532      }
533  
472
534      /**
535       * put(null,x) throws NPE
536       */
537      public void testPut1_NullPointerException() {
538          try {
539 <            ConcurrentNavigableMap c = map5();
539 >            ConcurrentNavigableMap<Item,String> c = map5();
540              c.put(null, "whatever");
541              shouldThrow();
542 <        } catch(NullPointerException e){}
542 >        } catch (NullPointerException success) {}
543      }
544  
545      /**
# Line 486 | Line 547 | public class ConcurrentSkipListSubMapTes
547       */
548      public void testPutIfAbsent1_NullPointerException() {
549          try {
550 <            ConcurrentNavigableMap c = map5();
550 >            ConcurrentNavigableMap<Item,String> c = map5();
551              c.putIfAbsent(null, "whatever");
552              shouldThrow();
553 <        } catch(NullPointerException e){}
553 >        } catch (NullPointerException success) {}
554      }
555  
556      /**
# Line 497 | Line 558 | public class ConcurrentSkipListSubMapTes
558       */
559      public void testReplace_NullPointerException() {
560          try {
561 <            ConcurrentNavigableMap c = map5();
562 <            c.replace(null, "whatever");
561 >            ConcurrentNavigableMap<Item,String> c = map5();
562 >            c.replace(null, "A");
563              shouldThrow();
564 <        } catch(NullPointerException e){}
564 >        } catch (NullPointerException success) {}
565      }
566  
567      /**
# Line 508 | Line 569 | public class ConcurrentSkipListSubMapTes
569       */
570      public void testReplaceValue_NullPointerException() {
571          try {
572 <            ConcurrentNavigableMap c = map5();
573 <            c.replace(null, one, "whatever");
572 >            ConcurrentNavigableMap<Item,String> c = map5();
573 >            c.replace(null, "A", "B");
574              shouldThrow();
575 <        } catch(NullPointerException e){}
575 >        } catch (NullPointerException success) {}
576      }
577  
578      /**
# Line 519 | Line 580 | public class ConcurrentSkipListSubMapTes
580       */
581      public void testRemove1_NullPointerException() {
582          try {
583 <            ConcurrentNavigableMap c = map5();
583 >            ConcurrentNavigableMap<Item,String> c = map5();
584              c.remove(null);
585              shouldThrow();
586 <        } catch(NullPointerException e){}
586 >        } catch (NullPointerException success) {}
587      }
588  
589      /**
# Line 530 | Line 591 | public class ConcurrentSkipListSubMapTes
591       */
592      public void testRemove2_NullPointerException() {
593          try {
594 <            ConcurrentNavigableMap c = map5();
594 >            ConcurrentNavigableMap<Item,String> c = map5();
595              c.remove(null, "whatever");
596              shouldThrow();
597 <        } catch(NullPointerException e){}
597 >        } catch (NullPointerException success) {}
598      }
599  
600      /**
601 <     * A deserialized map equals original
601 >     * A deserialized/reserialized map equals original
602       */
603 <    public void testSerialization() {
604 <        ConcurrentNavigableMap q = map5();
603 >    public void testSerialization() throws Exception {
604 >        NavigableMap<Item,String> x = map5();
605 >        NavigableMap<Item,String> y = serialClone(x);
606  
607 <        try {
608 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
609 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
610 <            out.writeObject(q);
611 <            out.close();
550 <
551 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
552 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
553 <            ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
554 <            assertEquals(q.size(), r.size());
555 <            assertTrue(q.equals(r));
556 <            assertTrue(r.equals(q));
557 <        } catch(Exception e){
558 <            e.printStackTrace();
559 <            unexpectedException();
560 <        }
607 >        assertNotSame(x, y);
608 >        mustEqual(x.size(), y.size());
609 >        mustEqual(x.toString(), y.toString());
610 >        mustEqual(x, y);
611 >        mustEqual(y, x);
612      }
613  
563
564
614      /**
615       * subMap returns map with keys in requested range
616       */
617      public void testSubMapContents() {
618 <        ConcurrentNavigableMap map = map5();
619 <        SortedMap sm = map.subMap(two, four);
620 <        assertEquals(two, sm.firstKey());
621 <        assertEquals(three, sm.lastKey());
622 <        assertEquals(2, sm.size());
618 >        ConcurrentNavigableMap<Item,String> map = map5();
619 >        SortedMap<Item,String> sm = map.subMap(two, four);
620 >        mustEqual(two, sm.firstKey());
621 >        mustEqual(three, sm.lastKey());
622 >        mustEqual(2, sm.size());
623          assertFalse(sm.containsKey(one));
624          assertTrue(sm.containsKey(two));
625          assertTrue(sm.containsKey(three));
626          assertFalse(sm.containsKey(four));
627          assertFalse(sm.containsKey(five));
628 <        Iterator i = sm.keySet().iterator();
629 <        Object k;
630 <        k = (Integer)(i.next());
631 <        assertEquals(two, k);
632 <        k = (Integer)(i.next());
633 <        assertEquals(three, k);
628 >        Iterator<? extends Item> i = sm.keySet().iterator();
629 >        Item k;
630 >        k = (Item)(i.next());
631 >        mustEqual(two, k);
632 >        k = (Item)(i.next());
633 >        mustEqual(three, k);
634          assertFalse(i.hasNext());
635 <        Iterator j = sm.keySet().iterator();
635 >        Iterator<? extends Item> j = sm.keySet().iterator();
636          j.next();
637          j.remove();
638          assertFalse(map.containsKey(two));
639 <        assertEquals(4, map.size());
640 <        assertEquals(1, sm.size());
641 <        assertEquals(three, sm.firstKey());
642 <        assertEquals(three, sm.lastKey());
643 <        assertTrue(sm.remove(three) != null);
639 >        mustEqual(4, map.size());
640 >        mustEqual(1, sm.size());
641 >        mustEqual(three, sm.firstKey());
642 >        mustEqual(three, sm.lastKey());
643 >        mustEqual("C", sm.remove(three));
644          assertTrue(sm.isEmpty());
645 <        assertEquals(3, map.size());
645 >        mustEqual(3, map.size());
646      }
647  
648      public void testSubMapContents2() {
649 <        ConcurrentNavigableMap map = map5();
650 <        SortedMap sm = map.subMap(two, three);
651 <        assertEquals(1, sm.size());
652 <        assertEquals(two, sm.firstKey());
653 <        assertEquals(two, sm.lastKey());
649 >        ConcurrentNavigableMap<Item,String> map = map5();
650 >        SortedMap<Item,String> sm = map.subMap(two, three);
651 >        mustEqual(1, sm.size());
652 >        mustEqual(two, sm.firstKey());
653 >        mustEqual(two, sm.lastKey());
654          assertFalse(sm.containsKey(one));
655          assertTrue(sm.containsKey(two));
656          assertFalse(sm.containsKey(three));
657          assertFalse(sm.containsKey(four));
658          assertFalse(sm.containsKey(five));
659 <        Iterator i = sm.keySet().iterator();
660 <        Object k;
661 <        k = (Integer)(i.next());
662 <        assertEquals(two, k);
659 >        Iterator<? extends Item> i = sm.keySet().iterator();
660 >        Item k;
661 >        k = (Item)(i.next());
662 >        mustEqual(two, k);
663          assertFalse(i.hasNext());
664 <        Iterator j = sm.keySet().iterator();
664 >        Iterator<? extends Item> j = sm.keySet().iterator();
665          j.next();
666          j.remove();
667          assertFalse(map.containsKey(two));
668 <        assertEquals(4, map.size());
669 <        assertEquals(0, sm.size());
668 >        mustEqual(4, map.size());
669 >        mustEqual(0, sm.size());
670          assertTrue(sm.isEmpty());
671 <        assertTrue(sm.remove(three) == null);
672 <        assertEquals(4, map.size());
671 >        assertSame(sm.remove(three), null);
672 >        mustEqual(4, map.size());
673      }
674  
675      /**
676       * headMap returns map with keys in requested range
677       */
678      public void testHeadMapContents() {
679 <        ConcurrentNavigableMap map = map5();
680 <        SortedMap sm = map.headMap(four);
679 >        ConcurrentNavigableMap<Item,String> map = map5();
680 >        SortedMap<Item,String> sm = map.headMap(four);
681          assertTrue(sm.containsKey(one));
682          assertTrue(sm.containsKey(two));
683          assertTrue(sm.containsKey(three));
684          assertFalse(sm.containsKey(four));
685          assertFalse(sm.containsKey(five));
686 <        Iterator i = sm.keySet().iterator();
686 >        Iterator<? extends Item> i = sm.keySet().iterator();
687          Object k;
688 <        k = (Integer)(i.next());
689 <        assertEquals(one, k);
690 <        k = (Integer)(i.next());
691 <        assertEquals(two, k);
692 <        k = (Integer)(i.next());
693 <        assertEquals(three, k);
688 >        k = (Item)(i.next());
689 >        mustEqual(one, k);
690 >        k = (Item)(i.next());
691 >        mustEqual(two, k);
692 >        k = (Item)(i.next());
693 >        mustEqual(three, k);
694          assertFalse(i.hasNext());
695          sm.clear();
696          assertTrue(sm.isEmpty());
697 <        assertEquals(2, map.size());
698 <        assertEquals(four, map.firstKey());
697 >        mustEqual(2, map.size());
698 >        mustEqual(four, map.firstKey());
699      }
700  
701      /**
702       * headMap returns map with keys in requested range
703       */
704      public void testTailMapContents() {
705 <        ConcurrentNavigableMap map = map5();
706 <        SortedMap sm = map.tailMap(two);
705 >        ConcurrentNavigableMap<Item,String> map = map5();
706 >        SortedMap<Item,String> sm = map.tailMap(two);
707          assertFalse(sm.containsKey(one));
708          assertTrue(sm.containsKey(two));
709          assertTrue(sm.containsKey(three));
710          assertTrue(sm.containsKey(four));
711          assertTrue(sm.containsKey(five));
712 <        Iterator i = sm.keySet().iterator();
713 <        Object k;
714 <        k = (Integer)(i.next());
715 <        assertEquals(two, k);
716 <        k = (Integer)(i.next());
717 <        assertEquals(three, k);
718 <        k = (Integer)(i.next());
719 <        assertEquals(four, k);
720 <        k = (Integer)(i.next());
721 <        assertEquals(five, k);
712 >        Iterator<Item> i = sm.keySet().iterator();
713 >        Item k = i.next();
714 >        mustEqual(two, k);
715 >        k = i.next();
716 >        mustEqual(three, k);
717 >        k = i.next();
718 >        mustEqual(four, k);
719 >        k = i.next();
720 >        mustEqual(five, k);
721 >        assertFalse(i.hasNext());
722 >
723 >        Iterator<Map.Entry<Item,String>> ei = sm.entrySet().iterator();
724 >        Map.Entry<Item,String> e;
725 >        e = (ei.next());
726 >        mustEqual(two, e.getKey());
727 >        mustEqual("B", e.getValue());
728 >        e = (ei.next());
729 >        mustEqual(three, e.getKey());
730 >        mustEqual("C", e.getValue());
731 >        e = (ei.next());
732 >        mustEqual(four, e.getKey());
733 >        mustEqual("D", e.getValue());
734 >        e = (ei.next());
735 >        mustEqual(five, e.getKey());
736 >        mustEqual("E", e.getValue());
737 >        assertFalse(i.hasNext());
738 >
739 >        SortedMap<Item,String> ssm = sm.tailMap(four);
740 >        mustEqual(four, ssm.firstKey());
741 >        mustEqual(five, ssm.lastKey());
742 >        mustEqual("D", ssm.remove(four));
743 >        mustEqual(1, ssm.size());
744 >        mustEqual(3, sm.size());
745 >        mustEqual(4, map.size());
746 >    }
747 >
748 >    /**
749 >     * clear removes all pairs
750 >     */
751 >    public void testDescendingClear() {
752 >        ConcurrentNavigableMap<Item,String> map = dmap5();
753 >        map.clear();
754 >        mustEqual(0, map.size());
755 >    }
756 >
757 >    /**
758 >     * Maps with same contents are equal
759 >     */
760 >    public void testDescendingEquals() {
761 >        ConcurrentNavigableMap<Item,String> map1 = dmap5();
762 >        ConcurrentNavigableMap<Item,String> map2 = dmap5();
763 >        mustEqual(map1, map2);
764 >        mustEqual(map2, map1);
765 >        map1.clear();
766 >        assertFalse(map1.equals(map2));
767 >        assertFalse(map2.equals(map1));
768 >    }
769 >
770 >    /**
771 >     * containsKey returns true for contained key
772 >     */
773 >    public void testDescendingContainsKey() {
774 >        ConcurrentNavigableMap<Item,String> map = dmap5();
775 >        assertTrue(map.containsKey(minusOne));
776 >        assertFalse(map.containsKey(zero));
777 >    }
778 >
779 >    /**
780 >     * containsValue returns true for held values
781 >     */
782 >    public void testDescendingContainsValue() {
783 >        ConcurrentNavigableMap<Item,String> map = dmap5();
784 >        assertTrue(map.containsValue("A"));
785 >        assertFalse(map.containsValue("Z"));
786 >    }
787 >
788 >    /**
789 >     * get returns the correct element at the given key,
790 >     * or null if not present
791 >     */
792 >    public void testDescendingGet() {
793 >        ConcurrentNavigableMap<Item,String> map = dmap5();
794 >        mustEqual("A", map.get(minusOne));
795 >        ConcurrentNavigableMap<Item,String> empty = dmap0();
796 >        assertNull(empty.get(minusOne));
797 >    }
798 >
799 >    /**
800 >     * isEmpty is true of empty map and false for non-empty
801 >     */
802 >    public void testDescendingIsEmpty() {
803 >        ConcurrentNavigableMap<Item,String> empty = dmap0();
804 >        ConcurrentNavigableMap<Item,String> map = dmap5();
805 >        assertTrue(empty.isEmpty());
806 >        assertFalse(map.isEmpty());
807 >    }
808 >
809 >    /**
810 >     * firstKey returns first key
811 >     */
812 >    public void testDescendingFirstKey() {
813 >        ConcurrentNavigableMap<Item,String> map = dmap5();
814 >        mustEqual(minusOne, map.firstKey());
815 >    }
816 >
817 >    /**
818 >     * lastKey returns last key
819 >     */
820 >    public void testDescendingLastKey() {
821 >        ConcurrentNavigableMap<Item,String> map = dmap5();
822 >        mustEqual(minusFive, map.lastKey());
823 >    }
824 >
825 >    /**
826 >     * keySet returns a Set containing all the keys
827 >     */
828 >    public void testDescendingKeySet() {
829 >        ConcurrentNavigableMap<Item,String> map = dmap5();
830 >        Set<Item> s = map.keySet();
831 >        mustEqual(5, s.size());
832 >        mustContain(s, minusOne);
833 >        mustContain(s, minusTwo);
834 >        mustContain(s, minusThree);
835 >        mustContain(s, minusFour);
836 >        mustContain(s, minusFive);
837 >    }
838 >
839 >    /**
840 >     * keySet is ordered
841 >     */
842 >    public void testDescendingKeySetOrder() {
843 >        ConcurrentNavigableMap<Item,String> map = dmap5();
844 >        Set<Item> s = map.keySet();
845 >        Iterator<? extends Item> i = s.iterator();
846 >        Item last = i.next();
847 >        mustEqual(last, minusOne);
848 >        while (i.hasNext()) {
849 >            Item k = i.next();
850 >            assertTrue(last.compareTo(k) > 0);
851 >            last = k;
852 >        }
853 >    }
854 >
855 >    /**
856 >     * values collection contains all values
857 >     */
858 >    public void testDescendingValues() {
859 >        ConcurrentNavigableMap<Item,String> map = dmap5();
860 >        Collection<String> s = map.values();
861 >        mustEqual(5, s.size());
862 >        assertTrue(s.contains("A"));
863 >        assertTrue(s.contains("B"));
864 >        assertTrue(s.contains("C"));
865 >        assertTrue(s.contains("D"));
866 >        assertTrue(s.contains("E"));
867 >    }
868 >
869 >    /**
870 >     * keySet.toArray returns contains all keys
871 >     */
872 >    public void testDescendingAscendingKeySetToArray() {
873 >        ConcurrentNavigableMap<Item,String> map = dmap5();
874 >        Set<Item> s = map.keySet();
875 >        Item[] ar = s.toArray(new Item[0]);
876 >        assertTrue(s.containsAll(Arrays.asList(ar)));
877 >        mustEqual(5, ar.length);
878 >        ar[0] = minusTen;
879 >        assertFalse(s.containsAll(Arrays.asList(ar)));
880 >    }
881 >
882 >    /**
883 >     * descendingkeySet.toArray returns contains all keys
884 >     */
885 >    public void testDescendingDescendingKeySetToArray() {
886 >        ConcurrentNavigableMap<Item,String> map = dmap5();
887 >        Set<Item> s = map.descendingKeySet();
888 >        Item[] ar = s.toArray(new Item[0]);
889 >        mustEqual(5, ar.length);
890 >        assertTrue(s.containsAll(Arrays.asList(ar)));
891 >        ar[0] = minusTen;
892 >        assertFalse(s.containsAll(Arrays.asList(ar)));
893 >    }
894 >
895 >    /**
896 >     * Values.toArray contains all values
897 >     */
898 >    public void testDescendingValuesToArray() {
899 >        ConcurrentNavigableMap<Item,String> map = dmap5();
900 >        Collection<String> v = map.values();
901 >        String[] ar = v.toArray(new String[0]);
902 >        ArrayList<String> s = new ArrayList<String>(Arrays.asList(ar));
903 >        mustEqual(5, ar.length);
904 >        assertTrue(s.contains("A"));
905 >        assertTrue(s.contains("B"));
906 >        assertTrue(s.contains("C"));
907 >        assertTrue(s.contains("D"));
908 >        assertTrue(s.contains("E"));
909 >    }
910 >
911 >    /**
912 >     * entrySet contains all pairs
913 >     */
914 >    public void testDescendingEntrySet() {
915 >        ConcurrentNavigableMap<Item,String> map = dmap5();
916 >        Set<Map.Entry<Item,String>> s = map.entrySet();
917 >        mustEqual(5, s.size());
918 >        Iterator<Map.Entry<Item,String>> it = s.iterator();
919 >        while (it.hasNext()) {
920 >            Map.Entry<Item,String> e = it.next();
921 >            assertTrue(
922 >                       (e.getKey().equals(minusOne) && e.getValue().equals("A")) ||
923 >                       (e.getKey().equals(minusTwo) && e.getValue().equals("B")) ||
924 >                       (e.getKey().equals(minusThree) && e.getValue().equals("C")) ||
925 >                       (e.getKey().equals(minusFour) && e.getValue().equals("D")) ||
926 >                       (e.getKey().equals(minusFive) && e.getValue().equals("E")));
927 >        }
928 >    }
929 >
930 >    /**
931 >     * putAll adds all key-value pairs from the given map
932 >     */
933 >    public void testDescendingPutAll() {
934 >        ConcurrentNavigableMap<Item,String> empty = dmap0();
935 >        ConcurrentNavigableMap<Item,String> map = dmap5();
936 >        empty.putAll(map);
937 >        mustEqual(5, empty.size());
938 >        assertTrue(empty.containsKey(minusOne));
939 >        assertTrue(empty.containsKey(minusTwo));
940 >        assertTrue(empty.containsKey(minusThree));
941 >        assertTrue(empty.containsKey(minusFour));
942 >        assertTrue(empty.containsKey(minusFive));
943 >    }
944 >
945 >    /**
946 >     * putIfAbsent works when the given key is not present
947 >     */
948 >    public void testDescendingPutIfAbsent() {
949 >        ConcurrentNavigableMap<Item,String> map = dmap5();
950 >        map.putIfAbsent(six, "Z");
951 >        assertTrue(map.containsKey(six));
952 >    }
953 >
954 >    /**
955 >     * putIfAbsent does not add the pair if the key is already present
956 >     */
957 >    public void testDescendingPutIfAbsent2() {
958 >        ConcurrentNavigableMap<Item,String> map = dmap5();
959 >        mustEqual("A", map.putIfAbsent(minusOne, "Z"));
960 >    }
961 >
962 >    /**
963 >     * replace fails when the given key is not present
964 >     */
965 >    public void testDescendingReplace() {
966 >        ConcurrentNavigableMap<Item,String> map = dmap5();
967 >        assertNull(map.replace(six, "Z"));
968 >        assertFalse(map.containsKey(six));
969 >    }
970 >
971 >    /**
972 >     * replace succeeds if the key is already present
973 >     */
974 >    public void testDescendingReplace2() {
975 >        ConcurrentNavigableMap<Item,String> map = dmap5();
976 >        assertNotNull(map.replace(minusOne, "Z"));
977 >        mustEqual("Z", map.get(minusOne));
978 >    }
979 >
980 >    /**
981 >     * replace value fails when the given key not mapped to expected value
982 >     */
983 >    public void testDescendingReplaceValue() {
984 >        ConcurrentNavigableMap<Item,String> map = dmap5();
985 >        mustEqual("A", map.get(minusOne));
986 >        assertFalse(map.replace(minusOne, "Z", "Z"));
987 >        mustEqual("A", map.get(minusOne));
988 >    }
989 >
990 >    /**
991 >     * replace value succeeds when the given key mapped to expected value
992 >     */
993 >    public void testDescendingReplaceValue2() {
994 >        ConcurrentNavigableMap<Item,String> map = dmap5();
995 >        mustEqual("A", map.get(minusOne));
996 >        assertTrue(map.replace(minusOne, "A", "Z"));
997 >        mustEqual("Z", map.get(minusOne));
998 >    }
999 >
1000 >    /**
1001 >     * remove removes the correct key-value pair from the map
1002 >     */
1003 >    public void testDescendingRemove() {
1004 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1005 >        map.remove(minusFive);
1006 >        mustEqual(4, map.size());
1007 >        assertFalse(map.containsKey(minusFive));
1008 >    }
1009 >
1010 >    /**
1011 >     * remove(key,value) removes only if pair present
1012 >     */
1013 >    public void testDescendingRemove2() {
1014 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1015 >        assertTrue(map.containsKey(minusFive));
1016 >        mustEqual("E", map.get(minusFive));
1017 >        map.remove(minusFive, "E");
1018 >        mustEqual(4, map.size());
1019 >        assertFalse(map.containsKey(minusFive));
1020 >        map.remove(minusFour, "A");
1021 >        mustEqual(4, map.size());
1022 >        assertTrue(map.containsKey(minusFour));
1023 >    }
1024 >
1025 >    /**
1026 >     * lowerEntry returns preceding entry.
1027 >     */
1028 >    public void testDescendingLowerEntry() {
1029 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1030 >        Map.Entry<Item,String> e1 = map.lowerEntry(minusThree);
1031 >        mustEqual(minusTwo, e1.getKey());
1032 >
1033 >        Map.Entry<Item,String> e2 = map.lowerEntry(minusSix);
1034 >        mustEqual(minusFive, e2.getKey());
1035 >
1036 >        Map.Entry<Item,String> e3 = map.lowerEntry(minusOne);
1037 >        assertNull(e3);
1038 >
1039 >        Map.Entry<Item,String> e4 = map.lowerEntry(zero);
1040 >        assertNull(e4);
1041 >    }
1042 >
1043 >    /**
1044 >     * higherEntry returns next entry.
1045 >     */
1046 >    public void testDescendingHigherEntry() {
1047 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1048 >        Map.Entry<Item,String> e1 = map.higherEntry(minusThree);
1049 >        mustEqual(minusFour, e1.getKey());
1050 >
1051 >        Map.Entry<Item,String> e2 = map.higherEntry(zero);
1052 >        mustEqual(minusOne, e2.getKey());
1053 >
1054 >        Map.Entry<Item,String> e3 = map.higherEntry(minusFive);
1055 >        assertNull(e3);
1056 >
1057 >        Map.Entry<Item,String> e4 = map.higherEntry(minusSix);
1058 >        assertNull(e4);
1059 >    }
1060 >
1061 >    /**
1062 >     * floorEntry returns preceding entry.
1063 >     */
1064 >    public void testDescendingFloorEntry() {
1065 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1066 >        Map.Entry<Item,String> e1 = map.floorEntry(minusThree);
1067 >        mustEqual(minusThree, e1.getKey());
1068 >
1069 >        Map.Entry<Item,String> e2 = map.floorEntry(minusSix);
1070 >        mustEqual(minusFive, e2.getKey());
1071 >
1072 >        Map.Entry<Item,String> e3 = map.floorEntry(minusOne);
1073 >        mustEqual(minusOne, e3.getKey());
1074 >
1075 >        Map.Entry<Item,String> e4 = map.floorEntry(zero);
1076 >        assertNull(e4);
1077 >    }
1078 >
1079 >    /**
1080 >     * ceilingEntry returns next entry.
1081 >     */
1082 >    public void testDescendingCeilingEntry() {
1083 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1084 >        Map.Entry<Item,String> e1 = map.ceilingEntry(minusThree);
1085 >        mustEqual(minusThree, e1.getKey());
1086 >
1087 >        Map.Entry<Item,String> e2 = map.ceilingEntry(zero);
1088 >        mustEqual(minusOne, e2.getKey());
1089 >
1090 >        Map.Entry<Item,String> e3 = map.ceilingEntry(minusFive);
1091 >        mustEqual(minusFive, e3.getKey());
1092 >
1093 >        Map.Entry<Item,String> e4 = map.ceilingEntry(minusSix);
1094 >        assertNull(e4);
1095 >    }
1096 >
1097 >    /**
1098 >     * pollFirstEntry returns entries in order
1099 >     */
1100 >    public void testDescendingPollFirstEntry() {
1101 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1102 >        Map.Entry<Item,String> e = map.pollFirstEntry();
1103 >        mustEqual(minusOne, e.getKey());
1104 >        mustEqual("A", e.getValue());
1105 >        e = map.pollFirstEntry();
1106 >        mustEqual(minusTwo, e.getKey());
1107 >        map.put(minusOne, "A");
1108 >        e = map.pollFirstEntry();
1109 >        mustEqual(minusOne, e.getKey());
1110 >        mustEqual("A", e.getValue());
1111 >        e = map.pollFirstEntry();
1112 >        mustEqual(minusThree, e.getKey());
1113 >        map.remove(minusFour);
1114 >        e = map.pollFirstEntry();
1115 >        mustEqual(minusFive, e.getKey());
1116 >        try {
1117 >            e.setValue("A");
1118 >            shouldThrow();
1119 >        } catch (UnsupportedOperationException success) {}
1120 >        e = map.pollFirstEntry();
1121 >        assertNull(e);
1122 >    }
1123 >
1124 >    /**
1125 >     * pollLastEntry returns entries in order
1126 >     */
1127 >    public void testDescendingPollLastEntry() {
1128 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1129 >        Map.Entry<Item,String> e = map.pollLastEntry();
1130 >        mustEqual(minusFive, e.getKey());
1131 >        mustEqual("E", e.getValue());
1132 >        e = map.pollLastEntry();
1133 >        mustEqual(minusFour, e.getKey());
1134 >        map.put(minusFive, "E");
1135 >        e = map.pollLastEntry();
1136 >        mustEqual(minusFive, e.getKey());
1137 >        mustEqual("E", e.getValue());
1138 >        e = map.pollLastEntry();
1139 >        mustEqual(minusThree, e.getKey());
1140 >        map.remove(minusTwo);
1141 >        e = map.pollLastEntry();
1142 >        mustEqual(minusOne, e.getKey());
1143 >        try {
1144 >            e.setValue("E");
1145 >            shouldThrow();
1146 >        } catch (UnsupportedOperationException success) {}
1147 >        e = map.pollLastEntry();
1148 >        assertNull(e);
1149 >    }
1150 >
1151 >    /**
1152 >     * size returns the correct values
1153 >     */
1154 >    public void testDescendingSize() {
1155 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1156 >        ConcurrentNavigableMap<Item,String> empty = dmap0();
1157 >        mustEqual(0, empty.size());
1158 >        mustEqual(5, map.size());
1159 >    }
1160 >
1161 >    /**
1162 >     * toString contains toString of elements
1163 >     */
1164 >    public void testDescendingToString() {
1165 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1166 >        String s = map.toString();
1167 >        for (int i = 1; i <= 5; ++i) {
1168 >            assertTrue(s.contains(String.valueOf(i)));
1169 >        }
1170 >    }
1171 >
1172 >    // Exception testDescendings
1173 >
1174 >    /**
1175 >     * get(null) of empty map throws NPE
1176 >     */
1177 >    public void testDescendingGet_NullPointerException() {
1178 >        try {
1179 >            ConcurrentNavigableMap<Item,String> c = dmap5();
1180 >            c.get(null);
1181 >            shouldThrow();
1182 >        } catch (NullPointerException success) {}
1183 >    }
1184 >
1185 >    /**
1186 >     * containsKey(null) of empty map throws NPE
1187 >     */
1188 >    public void testDescendingContainsKey_NullPointerException() {
1189 >        try {
1190 >            ConcurrentNavigableMap<Item,String> c = dmap5();
1191 >            c.containsKey(null);
1192 >            shouldThrow();
1193 >        } catch (NullPointerException success) {}
1194 >    }
1195 >
1196 >    /**
1197 >     * containsValue(null) throws NPE
1198 >     */
1199 >    public void testDescendingContainsValue_NullPointerException() {
1200 >        try {
1201 >            ConcurrentNavigableMap<Item,String> c = dmap0();
1202 >            c.containsValue(null);
1203 >            shouldThrow();
1204 >        } catch (NullPointerException success) {}
1205 >    }
1206 >
1207 >    /**
1208 >     * put(null,x) throws NPE
1209 >     */
1210 >    public void testDescendingPut1_NullPointerException() {
1211 >        try {
1212 >            ConcurrentNavigableMap<Item,String> c = dmap5();
1213 >            c.put(null, "whatever");
1214 >            shouldThrow();
1215 >        } catch (NullPointerException success) {}
1216 >    }
1217 >
1218 >    /**
1219 >     * putIfAbsent(null, x) throws NPE
1220 >     */
1221 >    public void testDescendingPutIfAbsent1_NullPointerException() {
1222 >        try {
1223 >            ConcurrentNavigableMap<Item,String> c = dmap5();
1224 >            c.putIfAbsent(null, "whatever");
1225 >            shouldThrow();
1226 >        } catch (NullPointerException success) {}
1227 >    }
1228 >
1229 >    /**
1230 >     * replace(null, x) throws NPE
1231 >     */
1232 >    public void testDescendingReplace_NullPointerException() {
1233 >        try {
1234 >            ConcurrentNavigableMap<Item,String> c = dmap5();
1235 >            c.replace(null, "whatever");
1236 >            shouldThrow();
1237 >        } catch (NullPointerException success) {}
1238 >    }
1239 >
1240 >    /**
1241 >     * replace(null, x, y) throws NPE
1242 >     */
1243 >    public void testDescendingReplaceValue_NullPointerException() {
1244 >        try {
1245 >            ConcurrentNavigableMap<Item,String> c = dmap5();
1246 >            c.replace(null, "A", "B");
1247 >            shouldThrow();
1248 >        } catch (NullPointerException success) {}
1249 >    }
1250 >
1251 >    /**
1252 >     * remove(null) throws NPE
1253 >     */
1254 >    public void testDescendingRemove1_NullPointerException() {
1255 >        try {
1256 >            ConcurrentNavigableMap<Item,String> c = dmap5();
1257 >            c.remove(null);
1258 >            shouldThrow();
1259 >        } catch (NullPointerException success) {}
1260 >    }
1261 >
1262 >    /**
1263 >     * remove(null, x) throws NPE
1264 >     */
1265 >    public void testDescendingRemove2_NullPointerException() {
1266 >        try {
1267 >            ConcurrentNavigableMap<Item,String> c = dmap5();
1268 >            c.remove(null, "whatever");
1269 >            shouldThrow();
1270 >        } catch (NullPointerException success) {}
1271 >    }
1272 >
1273 >    /**
1274 >     * A deserialized/reserialized map equals original
1275 >     */
1276 >    public void testDescendingSerialization() throws Exception {
1277 >        NavigableMap<Item,String> x = dmap5();
1278 >        NavigableMap<Item,String> y = serialClone(x);
1279 >
1280 >        assertNotSame(x, y);
1281 >        mustEqual(x.size(), y.size());
1282 >        mustEqual(x.toString(), y.toString());
1283 >        mustEqual(x, y);
1284 >        mustEqual(y, x);
1285 >    }
1286 >
1287 >    /**
1288 >     * subMap returns map with keys in requested range
1289 >     */
1290 >    public void testDescendingSubMapContents() {
1291 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1292 >        SortedMap<Item,String> sm = map.subMap(minusTwo, minusFour);
1293 >        mustEqual(minusTwo, sm.firstKey());
1294 >        mustEqual(minusThree, sm.lastKey());
1295 >        mustEqual(2, sm.size());
1296 >        assertFalse(sm.containsKey(minusOne));
1297 >        assertTrue(sm.containsKey(minusTwo));
1298 >        assertTrue(sm.containsKey(minusThree));
1299 >        assertFalse(sm.containsKey(minusFour));
1300 >        assertFalse(sm.containsKey(minusFive));
1301 >        Iterator<? extends Item> i = sm.keySet().iterator();
1302 >        Item k;
1303 >        k = (Item)(i.next());
1304 >        mustEqual(minusTwo, k);
1305 >        k = (Item)(i.next());
1306 >        mustEqual(minusThree, k);
1307 >        assertFalse(i.hasNext());
1308 >        Iterator<? extends Item> j = sm.keySet().iterator();
1309 >        j.next();
1310 >        j.remove();
1311 >        assertFalse(map.containsKey(minusTwo));
1312 >        mustEqual(4, map.size());
1313 >        mustEqual(1, sm.size());
1314 >        mustEqual(minusThree, sm.firstKey());
1315 >        mustEqual(minusThree, sm.lastKey());
1316 >        mustEqual("C", sm.remove(minusThree));
1317 >        assertTrue(sm.isEmpty());
1318 >        mustEqual(3, map.size());
1319 >    }
1320 >
1321 >    public void testDescendingSubMapContents2() {
1322 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1323 >        SortedMap<Item,String> sm = map.subMap(minusTwo, minusThree);
1324 >        mustEqual(1, sm.size());
1325 >        mustEqual(minusTwo, sm.firstKey());
1326 >        mustEqual(minusTwo, sm.lastKey());
1327 >        assertFalse(sm.containsKey(minusOne));
1328 >        assertTrue(sm.containsKey(minusTwo));
1329 >        assertFalse(sm.containsKey(minusThree));
1330 >        assertFalse(sm.containsKey(minusFour));
1331 >        assertFalse(sm.containsKey(minusFive));
1332 >        Iterator<? extends Item> i = sm.keySet().iterator();
1333 >        Item k;
1334 >        k = (Item)(i.next());
1335 >        mustEqual(minusTwo, k);
1336          assertFalse(i.hasNext());
1337 +        Iterator<? extends Item> j = sm.keySet().iterator();
1338 +        j.next();
1339 +        j.remove();
1340 +        assertFalse(map.containsKey(minusTwo));
1341 +        mustEqual(4, map.size());
1342 +        mustEqual(0, sm.size());
1343 +        assertTrue(sm.isEmpty());
1344 +        assertSame(sm.remove(minusThree), null);
1345 +        mustEqual(4, map.size());
1346 +    }
1347  
1348 <        Iterator ei = sm.entrySet().iterator();
1349 <        Map.Entry e;
1350 <        e = (Map.Entry)(ei.next());
1351 <        assertEquals(two, e.getKey());
1352 <        assertEquals("B", e.getValue());
1353 <        e = (Map.Entry)(ei.next());
1354 <        assertEquals(three, e.getKey());
1355 <        assertEquals("C", e.getValue());
1356 <        e = (Map.Entry)(ei.next());
1357 <        assertEquals(four, e.getKey());
1358 <        assertEquals("D", e.getValue());
1359 <        e = (Map.Entry)(ei.next());
1360 <        assertEquals(five, e.getKey());
1361 <        assertEquals("E", e.getValue());
1348 >    /**
1349 >     * headMap returns map with keys in requested range
1350 >     */
1351 >    public void testDescendingHeadMapContents() {
1352 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1353 >        SortedMap<Item,String> sm = map.headMap(minusFour);
1354 >        assertTrue(sm.containsKey(minusOne));
1355 >        assertTrue(sm.containsKey(minusTwo));
1356 >        assertTrue(sm.containsKey(minusThree));
1357 >        assertFalse(sm.containsKey(minusFour));
1358 >        assertFalse(sm.containsKey(minusFive));
1359 >        Iterator<? extends Item> i = sm.keySet().iterator();
1360 >        Item k;
1361 >        k = (Item)(i.next());
1362 >        mustEqual(minusOne, k);
1363 >        k = (Item)(i.next());
1364 >        mustEqual(minusTwo, k);
1365 >        k = (Item)(i.next());
1366 >        mustEqual(minusThree, k);
1367 >        assertFalse(i.hasNext());
1368 >        sm.clear();
1369 >        assertTrue(sm.isEmpty());
1370 >        mustEqual(2, map.size());
1371 >        mustEqual(minusFour, map.firstKey());
1372 >    }
1373 >
1374 >    /**
1375 >     * headMap returns map with keys in requested range
1376 >     */
1377 >    public void testDescendingTailMapContents() {
1378 >        ConcurrentNavigableMap<Item,String> map = dmap5();
1379 >        SortedMap<Item,String> sm = map.tailMap(minusTwo);
1380 >        assertFalse(sm.containsKey(minusOne));
1381 >        assertTrue(sm.containsKey(minusTwo));
1382 >        assertTrue(sm.containsKey(minusThree));
1383 >        assertTrue(sm.containsKey(minusFour));
1384 >        assertTrue(sm.containsKey(minusFive));
1385 >        Iterator<? extends Item> i = sm.keySet().iterator();
1386 >        Item k = i.next();
1387 >        mustEqual(minusTwo, k);
1388 >        k = (i.next());
1389 >        mustEqual(minusThree, k);
1390 >        k = (i.next());
1391 >        mustEqual(minusFour, k);
1392 >        k = (i.next());
1393 >        mustEqual(minusFive, k);
1394          assertFalse(i.hasNext());
1395  
1396 <        SortedMap ssm = sm.tailMap(four);
1397 <        assertEquals(four, ssm.firstKey());
1398 <        assertEquals(five, ssm.lastKey());
1399 <        assertTrue(ssm.remove(four) != null);
1400 <        assertEquals(1, ssm.size());
1401 <        assertEquals(3, sm.size());
1402 <        assertEquals(4, map.size());
1396 >        Iterator<Map.Entry<Item,String>> ei = sm.entrySet().iterator();
1397 >        Map.Entry<Item,String> e;
1398 >        e = (ei.next());
1399 >        mustEqual(minusTwo, e.getKey());
1400 >        mustEqual("B", e.getValue());
1401 >        e = (ei.next());
1402 >        mustEqual(minusThree, e.getKey());
1403 >        mustEqual("C", e.getValue());
1404 >        e = (ei.next());
1405 >        mustEqual(minusFour, e.getKey());
1406 >        mustEqual("D", e.getValue());
1407 >        e = (ei.next());
1408 >        mustEqual(minusFive, e.getKey());
1409 >        mustEqual("E", e.getValue());
1410 >        assertFalse(i.hasNext());
1411 >
1412 >        SortedMap<Item,String> ssm = sm.tailMap(minusFour);
1413 >        mustEqual(minusFour, ssm.firstKey());
1414 >        mustEqual(minusFive, ssm.lastKey());
1415 >        mustEqual("D", ssm.remove(minusFour));
1416 >        mustEqual(1, ssm.size());
1417 >        mustEqual(3, sm.size());
1418 >        mustEqual(4, map.size());
1419      }
1420 <    
1420 >
1421   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines