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.2 by dl, Tue Mar 22 01:30:22 2005 UTC vs.
Revision 1.13 by jsr166, Tue Dec 1 09:48:13 2009 UTC

# Line 11 | Line 11 | import java.io.*;
11  
12   public class ConcurrentSkipListSubMapTest extends JSR166TestCase {
13      public static void main(String[] args) {
14 <        junit.textui.TestRunner.run (suite());  
14 >        junit.textui.TestRunner.run (suite());
15      }
16      public static Test suite() {
17 <        return new TestSuite(ConcurrentSkipListSubMapTest.class);
17 >        return new TestSuite(ConcurrentSkipListSubMapTest.class);
18      }
19  
20      /**
21       * Create a map from Integers 1-5 to Strings "A"-"E".
22       */
23 <    private static ConcurrentNavigableMap map5() {  
24 <        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
23 >    private static ConcurrentNavigableMap map5() {
24 >        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
25          assertTrue(map.isEmpty());
26 <        map.put(zero, "Z");
27 <        map.put(one, "A");
28 <        map.put(five, "E");
29 <        map.put(three, "C");
30 <        map.put(two, "B");
31 <        map.put(four, "D");
32 <        map.put(seven, "F");
26 >        map.put(zero, "Z");
27 >        map.put(one, "A");
28 >        map.put(five, "E");
29 >        map.put(three, "C");
30 >        map.put(two, "B");
31 >        map.put(four, "D");
32 >        map.put(seven, "F");
33          assertFalse(map.isEmpty());
34          assertEquals(7, map.size());
35 <        return map.navigableSubMap(one, seven);
35 >        return map.subMap(one, true, seven, false);
36 >    }
37 >
38 >    /**
39 >     * Create a map from Integers -5 to -1 to Strings "A"-"E".
40 >     */
41 >    private static ConcurrentNavigableMap dmap5() {
42 >        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
43 >        assertTrue(map.isEmpty());
44 >        map.put(m1, "A");
45 >        map.put(m5, "E");
46 >        map.put(m3, "C");
47 >        map.put(m2, "B");
48 >        map.put(m4, "D");
49 >        assertFalse(map.isEmpty());
50 >        assertEquals(5, map.size());
51 >        return map.descendingMap();
52 >    }
53 >
54 >    private static ConcurrentNavigableMap map0() {
55 >        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
56 >        assertTrue(map.isEmpty());
57 >        return map.tailMap(one, true);
58      }
59  
60 <    private static ConcurrentNavigableMap map0() {  
61 <        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
60 >    private static ConcurrentNavigableMap dmap0() {
61 >        ConcurrentSkipListMap map = new ConcurrentSkipListMap();
62          assertTrue(map.isEmpty());
63 <        return map.navigableTailMap(one);
63 >        return map;
64      }
65  
66      /**
# Line 46 | Line 68 | public class ConcurrentSkipListSubMapTes
68       */
69      public void testClear() {
70          ConcurrentNavigableMap map = map5();
71 <        map.clear();
72 <        assertEquals(map.size(), 0);
71 >        map.clear();
72 >        assertEquals(map.size(), 0);
73      }
74  
75  
# Line 59 | Line 81 | public class ConcurrentSkipListSubMapTes
81          ConcurrentNavigableMap map2 = map5();
82          assertEquals(map1, map2);
83          assertEquals(map2, map1);
84 <        map1.clear();
84 >        map1.clear();
85          assertFalse(map1.equals(map2));
86          assertFalse(map2.equals(map1));
87      }
# Line 69 | Line 91 | public class ConcurrentSkipListSubMapTes
91       */
92      public void testContainsKey() {
93          ConcurrentNavigableMap map = map5();
94 <        assertTrue(map.containsKey(one));
94 >        assertTrue(map.containsKey(one));
95          assertFalse(map.containsKey(zero));
96      }
97  
# Line 78 | Line 100 | public class ConcurrentSkipListSubMapTes
100       */
101      public void testContainsValue() {
102          ConcurrentNavigableMap map = map5();
103 <        assertTrue(map.containsValue("A"));
103 >        assertTrue(map.containsValue("A"));
104          assertFalse(map.containsValue("Z"));
105      }
106  
# Line 88 | Line 110 | public class ConcurrentSkipListSubMapTes
110       */
111      public void testGet() {
112          ConcurrentNavigableMap map = map5();
113 <        assertEquals("A", (String)map.get(one));
113 >        assertEquals("A", (String)map.get(one));
114          ConcurrentNavigableMap empty = map0();
115          assertNull(empty.get(one));
116      }
# Line 99 | Line 121 | public class ConcurrentSkipListSubMapTes
121      public void testIsEmpty() {
122          ConcurrentNavigableMap empty = map0();
123          ConcurrentNavigableMap map = map5();
124 <        assertTrue(empty.isEmpty());
124 >        assertTrue(empty.isEmpty());
125          assertFalse(map.isEmpty());
126      }
127  
# Line 108 | Line 130 | public class ConcurrentSkipListSubMapTes
130       */
131      public void testFirstKey() {
132          ConcurrentNavigableMap map = map5();
133 <        assertEquals(one, map.firstKey());
133 >        assertEquals(one, map.firstKey());
134      }
135  
136      /**
# Line 116 | Line 138 | public class ConcurrentSkipListSubMapTes
138       */
139      public void testLastKey() {
140          ConcurrentNavigableMap map = map5();
141 <        assertEquals(five, map.lastKey());
141 >        assertEquals(five, map.lastKey());
142      }
143  
144  
# Line 125 | Line 147 | public class ConcurrentSkipListSubMapTes
147       */
148      public void testKeySet() {
149          ConcurrentNavigableMap map = map5();
150 <        Set s = map.keySet();
151 <        assertEquals(5, s.size());
152 <        assertTrue(s.contains(one));
153 <        assertTrue(s.contains(two));
154 <        assertTrue(s.contains(three));
155 <        assertTrue(s.contains(four));
156 <        assertTrue(s.contains(five));
150 >        Set s = map.keySet();
151 >        assertEquals(5, s.size());
152 >        assertTrue(s.contains(one));
153 >        assertTrue(s.contains(two));
154 >        assertTrue(s.contains(three));
155 >        assertTrue(s.contains(four));
156 >        assertTrue(s.contains(five));
157      }
158  
159      /**
# Line 139 | Line 161 | public class ConcurrentSkipListSubMapTes
161       */
162      public void testKeySetOrder() {
163          ConcurrentNavigableMap map = map5();
164 <        Set s = map.keySet();
164 >        Set s = map.keySet();
165          Iterator i = s.iterator();
166          Integer last = (Integer)i.next();
167          assertEquals(last, one);
# Line 155 | Line 177 | public class ConcurrentSkipListSubMapTes
177       */
178      public void testValues() {
179          ConcurrentNavigableMap map = map5();
180 <        Collection s = map.values();
181 <        assertEquals(5, s.size());
182 <        assertTrue(s.contains("A"));
183 <        assertTrue(s.contains("B"));
184 <        assertTrue(s.contains("C"));
185 <        assertTrue(s.contains("D"));
186 <        assertTrue(s.contains("E"));
180 >        Collection s = map.values();
181 >        assertEquals(5, s.size());
182 >        assertTrue(s.contains("A"));
183 >        assertTrue(s.contains("B"));
184 >        assertTrue(s.contains("C"));
185 >        assertTrue(s.contains("D"));
186 >        assertTrue(s.contains("E"));
187      }
188  
189      /**
190 +     *  keySet.toArray returns contains all keys
191 +     */
192 +    public void testKeySetToArray() {
193 +        ConcurrentNavigableMap map = map5();
194 +        Set s = map.keySet();
195 +        Object[] ar = s.toArray();
196 +        assertTrue(s.containsAll(Arrays.asList(ar)));
197 +        assertEquals(5, ar.length);
198 +        ar[0] = m10;
199 +        assertFalse(s.containsAll(Arrays.asList(ar)));
200 +    }
201 +
202 +    /**
203 +     *  descendingkeySet.toArray returns contains all keys
204 +     */
205 +    public void testDescendingKeySetToArray() {
206 +        ConcurrentNavigableMap map = map5();
207 +        Set s = map.descendingKeySet();
208 +        Object[] ar = s.toArray();
209 +        assertEquals(5, ar.length);
210 +        assertTrue(s.containsAll(Arrays.asList(ar)));
211 +        ar[0] = m10;
212 +        assertFalse(s.containsAll(Arrays.asList(ar)));
213 +    }
214 +
215 +    /**
216 +     *  Values.toArray contains all values
217 +     */
218 +    public void testValuesToArray() {
219 +        ConcurrentNavigableMap map = map5();
220 +        Collection v = map.values();
221 +        Object[] ar = v.toArray();
222 +        ArrayList s = new ArrayList(Arrays.asList(ar));
223 +        assertEquals(5, ar.length);
224 +        assertTrue(s.contains("A"));
225 +        assertTrue(s.contains("B"));
226 +        assertTrue(s.contains("C"));
227 +        assertTrue(s.contains("D"));
228 +        assertTrue(s.contains("E"));
229 +    }
230 +
231 +
232 +    /**
233       * entrySet contains all pairs
234       */
235      public void testEntrySet() {
236          ConcurrentNavigableMap map = map5();
237 <        Set s = map.entrySet();
238 <        assertEquals(5, s.size());
237 >        Set s = map.entrySet();
238 >        assertEquals(5, s.size());
239          Iterator it = s.iterator();
240          while (it.hasNext()) {
241              Map.Entry e = (Map.Entry) it.next();
242 <            assertTrue(
242 >            assertTrue(
243                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
244                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
245                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 189 | Line 254 | public class ConcurrentSkipListSubMapTes
254      public void testPutAll() {
255          ConcurrentNavigableMap empty = map0();
256          ConcurrentNavigableMap map = map5();
257 <        empty.putAll(map);
258 <        assertEquals(5, empty.size());
259 <        assertTrue(empty.containsKey(one));
260 <        assertTrue(empty.containsKey(two));
261 <        assertTrue(empty.containsKey(three));
262 <        assertTrue(empty.containsKey(four));
263 <        assertTrue(empty.containsKey(five));
257 >        empty.putAll(map);
258 >        assertEquals(5, empty.size());
259 >        assertTrue(empty.containsKey(one));
260 >        assertTrue(empty.containsKey(two));
261 >        assertTrue(empty.containsKey(three));
262 >        assertTrue(empty.containsKey(four));
263 >        assertTrue(empty.containsKey(five));
264      }
265  
266      /**
# Line 203 | Line 268 | public class ConcurrentSkipListSubMapTes
268       */
269      public void testPutIfAbsent() {
270          ConcurrentNavigableMap map = map5();
271 <        map.putIfAbsent(six, "Z");
271 >        map.putIfAbsent(six, "Z");
272          assertTrue(map.containsKey(six));
273      }
274  
# Line 220 | Line 285 | public class ConcurrentSkipListSubMapTes
285       */
286      public void testReplace() {
287          ConcurrentNavigableMap map = map5();
288 <        assertNull(map.replace(six, "Z"));
288 >        assertNull(map.replace(six, "Z"));
289          assertFalse(map.containsKey(six));
290      }
291  
# Line 240 | Line 305 | public class ConcurrentSkipListSubMapTes
305      public void testReplaceValue() {
306          ConcurrentNavigableMap map = map5();
307          assertEquals("A", map.get(one));
308 <        assertFalse(map.replace(one, "Z", "Z"));
308 >        assertFalse(map.replace(one, "Z", "Z"));
309          assertEquals("A", map.get(one));
310      }
311  
# Line 250 | Line 315 | public class ConcurrentSkipListSubMapTes
315      public void testReplaceValue2() {
316          ConcurrentNavigableMap map = map5();
317          assertEquals("A", map.get(one));
318 <        assertTrue(map.replace(one, "A", "Z"));
318 >        assertTrue(map.replace(one, "A", "Z"));
319          assertEquals("Z", map.get(one));
320      }
321  
# Line 260 | Line 325 | public class ConcurrentSkipListSubMapTes
325       */
326      public void testRemove() {
327          ConcurrentNavigableMap map = map5();
328 <        map.remove(five);
329 <        assertEquals(4, map.size());
330 <        assertFalse(map.containsKey(five));
328 >        map.remove(five);
329 >        assertEquals(4, map.size());
330 >        assertFalse(map.containsKey(five));
331      }
332  
333      /**
# Line 270 | Line 335 | public class ConcurrentSkipListSubMapTes
335       */
336      public void testRemove2() {
337          ConcurrentNavigableMap map = map5();
338 <        assertTrue(map.containsKey(five));
338 >        assertTrue(map.containsKey(five));
339          assertEquals("E", map.get(five));
340 <        map.remove(five, "E");
341 <        assertEquals(4, map.size());
342 <        assertFalse(map.containsKey(five));
343 <        map.remove(four, "A");
344 <        assertEquals(4, map.size());
345 <        assertTrue(map.containsKey(four));
281 <
340 >        map.remove(five, "E");
341 >        assertEquals(4, map.size());
342 >        assertFalse(map.containsKey(five));
343 >        map.remove(four, "A");
344 >        assertEquals(4, map.size());
345 >        assertTrue(map.containsKey(four));
346      }
347  
348      /**
# Line 297 | Line 361 | public class ConcurrentSkipListSubMapTes
361  
362          Map.Entry e4 = map.lowerEntry(zero);
363          assertNull(e4);
300
364      }
365  
366      /**
# Line 316 | Line 379 | public class ConcurrentSkipListSubMapTes
379  
380          Map.Entry e4 = map.higherEntry(six);
381          assertNull(e4);
319
382      }
383  
384      /**
# Line 335 | Line 397 | public class ConcurrentSkipListSubMapTes
397  
398          Map.Entry e4 = map.floorEntry(zero);
399          assertNull(e4);
338
400      }
401  
402      /**
# Line 354 | Line 415 | public class ConcurrentSkipListSubMapTes
415  
416          Map.Entry e4 = map.ceilingEntry(six);
417          assertNull(e4);
357
418      }
419  
420      /**
# Line 379 | Line 439 | public class ConcurrentSkipListSubMapTes
439          try {
440              e.setValue("A");
441              shouldThrow();
442 <        } catch (Exception ok) {
383 <        }
442 >        } catch (UnsupportedOperationException success) {}
443          e = map.pollFirstEntry();
444          assertNull(e);
445      }
# Line 407 | Line 466 | public class ConcurrentSkipListSubMapTes
466          try {
467              e.setValue("E");
468              shouldThrow();
469 <        } catch (Exception ok) {
411 <        }
469 >        } catch (UnsupportedOperationException success) {}
470          e = map.pollLastEntry();
471          assertNull(e);
472      }
# Line 419 | Line 477 | public class ConcurrentSkipListSubMapTes
477      public void testSize() {
478          ConcurrentNavigableMap map = map5();
479          ConcurrentNavigableMap empty = map0();
480 <        assertEquals(0, empty.size());
481 <        assertEquals(5, map.size());
480 >        assertEquals(0, empty.size());
481 >        assertEquals(5, map.size());
482      }
483  
484      /**
# Line 432 | Line 490 | public class ConcurrentSkipListSubMapTes
490          for (int i = 1; i <= 5; ++i) {
491              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
492          }
493 <    }        
493 >    }
494  
495      // Exception tests
496  
# Line 444 | Line 502 | public class ConcurrentSkipListSubMapTes
502              ConcurrentNavigableMap c = map5();
503              c.get(null);
504              shouldThrow();
505 <        } catch(NullPointerException e){}
505 >        } catch (NullPointerException success) {}
506      }
507  
508      /**
# Line 455 | Line 513 | public class ConcurrentSkipListSubMapTes
513              ConcurrentNavigableMap c = map5();
514              c.containsKey(null);
515              shouldThrow();
516 <        } catch(NullPointerException e){}
516 >        } catch (NullPointerException success) {}
517      }
518  
519      /**
# Line 466 | Line 524 | public class ConcurrentSkipListSubMapTes
524              ConcurrentNavigableMap c = map0();
525              c.containsValue(null);
526              shouldThrow();
527 <        } catch(NullPointerException e){}
527 >        } catch (NullPointerException success) {}
528      }
529  
530  
# Line 478 | Line 536 | public class ConcurrentSkipListSubMapTes
536              ConcurrentNavigableMap c = map5();
537              c.put(null, "whatever");
538              shouldThrow();
539 <        } catch(NullPointerException e){}
539 >        } catch (NullPointerException success) {}
540      }
541  
542      /**
# Line 489 | Line 547 | public class ConcurrentSkipListSubMapTes
547              ConcurrentNavigableMap c = map5();
548              c.putIfAbsent(null, "whatever");
549              shouldThrow();
550 <        } catch(NullPointerException e){}
550 >        } catch (NullPointerException success) {}
551      }
552  
553      /**
# Line 500 | Line 558 | public class ConcurrentSkipListSubMapTes
558              ConcurrentNavigableMap c = map5();
559              c.replace(null, "whatever");
560              shouldThrow();
561 <        } catch(NullPointerException e){}
561 >        } catch (NullPointerException success) {}
562      }
563  
564      /**
# Line 511 | Line 569 | public class ConcurrentSkipListSubMapTes
569              ConcurrentNavigableMap c = map5();
570              c.replace(null, one, "whatever");
571              shouldThrow();
572 <        } catch(NullPointerException e){}
572 >        } catch (NullPointerException success) {}
573      }
574  
575      /**
# Line 522 | Line 580 | public class ConcurrentSkipListSubMapTes
580              ConcurrentNavigableMap c = map5();
581              c.remove(null);
582              shouldThrow();
583 <        } catch(NullPointerException e){}
583 >        } catch (NullPointerException success) {}
584      }
585  
586      /**
# Line 533 | Line 591 | public class ConcurrentSkipListSubMapTes
591              ConcurrentNavigableMap c = map5();
592              c.remove(null, "whatever");
593              shouldThrow();
594 <        } catch(NullPointerException e){}
594 >        } catch (NullPointerException success) {}
595      }
596  
597      /**
598       * A deserialized map equals original
599       */
600 <    public void testSerialization() {
600 >    public void testSerialization() throws Exception {
601          ConcurrentNavigableMap q = map5();
602  
603 <        try {
604 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
605 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
606 <            out.writeObject(q);
607 <            out.close();
608 <
609 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
610 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
611 <            ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
612 <            assertEquals(q.size(), r.size());
613 <            assertTrue(q.equals(r));
556 <            assertTrue(r.equals(q));
557 <        } catch(Exception e){
558 <            e.printStackTrace();
559 <            unexpectedException();
560 <        }
603 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
604 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
605 >        out.writeObject(q);
606 >        out.close();
607 >
608 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
609 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
610 >        ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
611 >        assertEquals(q.size(), r.size());
612 >        assertTrue(q.equals(r));
613 >        assertTrue(r.equals(q));
614      }
615  
616  
# Line 591 | Line 644 | public class ConcurrentSkipListSubMapTes
644          assertEquals(1, sm.size());
645          assertEquals(three, sm.firstKey());
646          assertEquals(three, sm.lastKey());
647 <        assertTrue(sm.remove(three) != null);
647 >        assertEquals("C", sm.remove(three));
648          assertTrue(sm.isEmpty());
649          assertEquals(3, map.size());
650      }
# Line 619 | Line 672 | public class ConcurrentSkipListSubMapTes
672          assertEquals(4, map.size());
673          assertEquals(0, sm.size());
674          assertTrue(sm.isEmpty());
675 <        assertTrue(sm.remove(three) == null);
675 >        assertSame(sm.remove(three), null);
676          assertEquals(4, map.size());
677      }
678  
# Line 691 | Line 744 | public class ConcurrentSkipListSubMapTes
744          SortedMap ssm = sm.tailMap(four);
745          assertEquals(four, ssm.firstKey());
746          assertEquals(five, ssm.lastKey());
747 <        assertTrue(ssm.remove(four) != null);
747 >        assertEquals("D", ssm.remove(four));
748          assertEquals(1, ssm.size());
749          assertEquals(3, sm.size());
750          assertEquals(4, map.size());
751      }
752 <    
752 >
753 >    /**
754 >     *  clear removes all pairs
755 >     */
756 >    public void testDescendingClear() {
757 >        ConcurrentNavigableMap map = dmap5();
758 >        map.clear();
759 >        assertEquals(map.size(), 0);
760 >    }
761 >
762 >
763 >    /**
764 >     *  Maps with same contents are equal
765 >     */
766 >    public void testDescendingEquals() {
767 >        ConcurrentNavigableMap map1 = dmap5();
768 >        ConcurrentNavigableMap map2 = dmap5();
769 >        assertEquals(map1, map2);
770 >        assertEquals(map2, map1);
771 >        map1.clear();
772 >        assertFalse(map1.equals(map2));
773 >        assertFalse(map2.equals(map1));
774 >    }
775 >
776 >    /**
777 >     *  containsKey returns true for contained key
778 >     */
779 >    public void testDescendingContainsKey() {
780 >        ConcurrentNavigableMap map = dmap5();
781 >        assertTrue(map.containsKey(m1));
782 >        assertFalse(map.containsKey(zero));
783 >    }
784 >
785 >    /**
786 >     *  containsValue returns true for held values
787 >     */
788 >    public void testDescendingContainsValue() {
789 >        ConcurrentNavigableMap map = dmap5();
790 >        assertTrue(map.containsValue("A"));
791 >        assertFalse(map.containsValue("Z"));
792 >    }
793 >
794 >    /**
795 >     *  get returns the correct element at the given key,
796 >     *  or null if not present
797 >     */
798 >    public void testDescendingGet() {
799 >        ConcurrentNavigableMap map = dmap5();
800 >        assertEquals("A", (String)map.get(m1));
801 >        ConcurrentNavigableMap empty = dmap0();
802 >        assertNull(empty.get(m1));
803 >    }
804 >
805 >    /**
806 >     *  isEmpty is true of empty map and false for non-empty
807 >     */
808 >    public void testDescendingIsEmpty() {
809 >        ConcurrentNavigableMap empty = dmap0();
810 >        ConcurrentNavigableMap map = dmap5();
811 >        assertTrue(empty.isEmpty());
812 >        assertFalse(map.isEmpty());
813 >    }
814 >
815 >    /**
816 >     *   firstKey returns first key
817 >     */
818 >    public void testDescendingFirstKey() {
819 >        ConcurrentNavigableMap map = dmap5();
820 >        assertEquals(m1, map.firstKey());
821 >    }
822 >
823 >    /**
824 >     *   lastKey returns last key
825 >     */
826 >    public void testDescendingLastKey() {
827 >        ConcurrentNavigableMap map = dmap5();
828 >        assertEquals(m5, map.lastKey());
829 >    }
830 >
831 >
832 >    /**
833 >     *   keySet returns a Set containing all the keys
834 >     */
835 >    public void testDescendingKeySet() {
836 >        ConcurrentNavigableMap map = dmap5();
837 >        Set s = map.keySet();
838 >        assertEquals(5, s.size());
839 >        assertTrue(s.contains(m1));
840 >        assertTrue(s.contains(m2));
841 >        assertTrue(s.contains(m3));
842 >        assertTrue(s.contains(m4));
843 >        assertTrue(s.contains(m5));
844 >    }
845 >
846 >    /**
847 >     *   keySet is ordered
848 >     */
849 >    public void testDescendingKeySetOrder() {
850 >        ConcurrentNavigableMap map = dmap5();
851 >        Set s = map.keySet();
852 >        Iterator i = s.iterator();
853 >        Integer last = (Integer)i.next();
854 >        assertEquals(last, m1);
855 >        while (i.hasNext()) {
856 >            Integer k = (Integer)i.next();
857 >            assertTrue(last.compareTo(k) > 0);
858 >            last = k;
859 >        }
860 >    }
861 >
862 >    /**
863 >     * values collection contains all values
864 >     */
865 >    public void testDescendingValues() {
866 >        ConcurrentNavigableMap map = dmap5();
867 >        Collection s = map.values();
868 >        assertEquals(5, s.size());
869 >        assertTrue(s.contains("A"));
870 >        assertTrue(s.contains("B"));
871 >        assertTrue(s.contains("C"));
872 >        assertTrue(s.contains("D"));
873 >        assertTrue(s.contains("E"));
874 >    }
875 >
876 >    /**
877 >     *  keySet.toArray returns contains all keys
878 >     */
879 >    public void testDescendingAscendingKeySetToArray() {
880 >        ConcurrentNavigableMap map = dmap5();
881 >        Set s = map.keySet();
882 >        Object[] ar = s.toArray();
883 >        assertTrue(s.containsAll(Arrays.asList(ar)));
884 >        assertEquals(5, ar.length);
885 >        ar[0] = m10;
886 >        assertFalse(s.containsAll(Arrays.asList(ar)));
887 >    }
888 >
889 >    /**
890 >     *  descendingkeySet.toArray returns contains all keys
891 >     */
892 >    public void testDescendingDescendingKeySetToArray() {
893 >        ConcurrentNavigableMap map = dmap5();
894 >        Set s = map.descendingKeySet();
895 >        Object[] ar = s.toArray();
896 >        assertEquals(5, ar.length);
897 >        assertTrue(s.containsAll(Arrays.asList(ar)));
898 >        ar[0] = m10;
899 >        assertFalse(s.containsAll(Arrays.asList(ar)));
900 >    }
901 >
902 >    /**
903 >     *  Values.toArray contains all values
904 >     */
905 >    public void testDescendingValuesToArray() {
906 >        ConcurrentNavigableMap map = dmap5();
907 >        Collection v = map.values();
908 >        Object[] ar = v.toArray();
909 >        ArrayList s = new ArrayList(Arrays.asList(ar));
910 >        assertEquals(5, ar.length);
911 >        assertTrue(s.contains("A"));
912 >        assertTrue(s.contains("B"));
913 >        assertTrue(s.contains("C"));
914 >        assertTrue(s.contains("D"));
915 >        assertTrue(s.contains("E"));
916 >    }
917 >
918 >
919 >    /**
920 >     * entrySet contains all pairs
921 >     */
922 >    public void testDescendingEntrySet() {
923 >        ConcurrentNavigableMap map = dmap5();
924 >        Set s = map.entrySet();
925 >        assertEquals(5, s.size());
926 >        Iterator it = s.iterator();
927 >        while (it.hasNext()) {
928 >            Map.Entry e = (Map.Entry) it.next();
929 >            assertTrue(
930 >                       (e.getKey().equals(m1) && e.getValue().equals("A")) ||
931 >                       (e.getKey().equals(m2) && e.getValue().equals("B")) ||
932 >                       (e.getKey().equals(m3) && e.getValue().equals("C")) ||
933 >                       (e.getKey().equals(m4) && e.getValue().equals("D")) ||
934 >                       (e.getKey().equals(m5) && e.getValue().equals("E")));
935 >        }
936 >    }
937 >
938 >    /**
939 >     *   putAll  adds all key-value pairs from the given map
940 >     */
941 >    public void testDescendingPutAll() {
942 >        ConcurrentNavigableMap empty = dmap0();
943 >        ConcurrentNavigableMap map = dmap5();
944 >        empty.putAll(map);
945 >        assertEquals(5, empty.size());
946 >        assertTrue(empty.containsKey(m1));
947 >        assertTrue(empty.containsKey(m2));
948 >        assertTrue(empty.containsKey(m3));
949 >        assertTrue(empty.containsKey(m4));
950 >        assertTrue(empty.containsKey(m5));
951 >    }
952 >
953 >    /**
954 >     *   putIfAbsent works when the given key is not present
955 >     */
956 >    public void testDescendingPutIfAbsent() {
957 >        ConcurrentNavigableMap map = dmap5();
958 >        map.putIfAbsent(six, "Z");
959 >        assertTrue(map.containsKey(six));
960 >    }
961 >
962 >    /**
963 >     *   putIfAbsent does not add the pair if the key is already present
964 >     */
965 >    public void testDescendingPutIfAbsent2() {
966 >        ConcurrentNavigableMap map = dmap5();
967 >        assertEquals("A", map.putIfAbsent(m1, "Z"));
968 >    }
969 >
970 >    /**
971 >     *   replace fails when the given key is not present
972 >     */
973 >    public void testDescendingReplace() {
974 >        ConcurrentNavigableMap map = dmap5();
975 >        assertNull(map.replace(six, "Z"));
976 >        assertFalse(map.containsKey(six));
977 >    }
978 >
979 >    /**
980 >     *   replace succeeds if the key is already present
981 >     */
982 >    public void testDescendingReplace2() {
983 >        ConcurrentNavigableMap map = dmap5();
984 >        assertNotNull(map.replace(m1, "Z"));
985 >        assertEquals("Z", map.get(m1));
986 >    }
987 >
988 >
989 >    /**
990 >     * replace value fails when the given key not mapped to expected value
991 >     */
992 >    public void testDescendingReplaceValue() {
993 >        ConcurrentNavigableMap map = dmap5();
994 >        assertEquals("A", map.get(m1));
995 >        assertFalse(map.replace(m1, "Z", "Z"));
996 >        assertEquals("A", map.get(m1));
997 >    }
998 >
999 >    /**
1000 >     * replace value succeeds when the given key mapped to expected value
1001 >     */
1002 >    public void testDescendingReplaceValue2() {
1003 >        ConcurrentNavigableMap map = dmap5();
1004 >        assertEquals("A", map.get(m1));
1005 >        assertTrue(map.replace(m1, "A", "Z"));
1006 >        assertEquals("Z", map.get(m1));
1007 >    }
1008 >
1009 >
1010 >    /**
1011 >     *   remove removes the correct key-value pair from the map
1012 >     */
1013 >    public void testDescendingRemove() {
1014 >        ConcurrentNavigableMap map = dmap5();
1015 >        map.remove(m5);
1016 >        assertEquals(4, map.size());
1017 >        assertFalse(map.containsKey(m5));
1018 >    }
1019 >
1020 >    /**
1021 >     * remove(key,value) removes only if pair present
1022 >     */
1023 >    public void testDescendingRemove2() {
1024 >        ConcurrentNavigableMap map = dmap5();
1025 >        assertTrue(map.containsKey(m5));
1026 >        assertEquals("E", map.get(m5));
1027 >        map.remove(m5, "E");
1028 >        assertEquals(4, map.size());
1029 >        assertFalse(map.containsKey(m5));
1030 >        map.remove(m4, "A");
1031 >        assertEquals(4, map.size());
1032 >        assertTrue(map.containsKey(m4));
1033 >    }
1034 >
1035 >    /**
1036 >     * lowerEntry returns preceding entry.
1037 >     */
1038 >    public void testDescendingLowerEntry() {
1039 >        ConcurrentNavigableMap map = dmap5();
1040 >        Map.Entry e1 = map.lowerEntry(m3);
1041 >        assertEquals(m2, e1.getKey());
1042 >
1043 >        Map.Entry e2 = map.lowerEntry(m6);
1044 >        assertEquals(m5, e2.getKey());
1045 >
1046 >        Map.Entry e3 = map.lowerEntry(m1);
1047 >        assertNull(e3);
1048 >
1049 >        Map.Entry e4 = map.lowerEntry(zero);
1050 >        assertNull(e4);
1051 >    }
1052 >
1053 >    /**
1054 >     * higherEntry returns next entry.
1055 >     */
1056 >    public void testDescendingHigherEntry() {
1057 >        ConcurrentNavigableMap map = dmap5();
1058 >        Map.Entry e1 = map.higherEntry(m3);
1059 >        assertEquals(m4, e1.getKey());
1060 >
1061 >        Map.Entry e2 = map.higherEntry(zero);
1062 >        assertEquals(m1, e2.getKey());
1063 >
1064 >        Map.Entry e3 = map.higherEntry(m5);
1065 >        assertNull(e3);
1066 >
1067 >        Map.Entry e4 = map.higherEntry(m6);
1068 >        assertNull(e4);
1069 >    }
1070 >
1071 >    /**
1072 >     * floorEntry returns preceding entry.
1073 >     */
1074 >    public void testDescendingFloorEntry() {
1075 >        ConcurrentNavigableMap map = dmap5();
1076 >        Map.Entry e1 = map.floorEntry(m3);
1077 >        assertEquals(m3, e1.getKey());
1078 >
1079 >        Map.Entry e2 = map.floorEntry(m6);
1080 >        assertEquals(m5, e2.getKey());
1081 >
1082 >        Map.Entry e3 = map.floorEntry(m1);
1083 >        assertEquals(m1, e3.getKey());
1084 >
1085 >        Map.Entry e4 = map.floorEntry(zero);
1086 >        assertNull(e4);
1087 >    }
1088 >
1089 >    /**
1090 >     * ceilingEntry returns next entry.
1091 >     */
1092 >    public void testDescendingCeilingEntry() {
1093 >        ConcurrentNavigableMap map = dmap5();
1094 >        Map.Entry e1 = map.ceilingEntry(m3);
1095 >        assertEquals(m3, e1.getKey());
1096 >
1097 >        Map.Entry e2 = map.ceilingEntry(zero);
1098 >        assertEquals(m1, e2.getKey());
1099 >
1100 >        Map.Entry e3 = map.ceilingEntry(m5);
1101 >        assertEquals(m5, e3.getKey());
1102 >
1103 >        Map.Entry e4 = map.ceilingEntry(m6);
1104 >        assertNull(e4);
1105 >    }
1106 >
1107 >    /**
1108 >     * pollFirstEntry returns entries in order
1109 >     */
1110 >    public void testDescendingPollFirstEntry() {
1111 >        ConcurrentNavigableMap map = dmap5();
1112 >        Map.Entry e = map.pollFirstEntry();
1113 >        assertEquals(m1, e.getKey());
1114 >        assertEquals("A", e.getValue());
1115 >        e = map.pollFirstEntry();
1116 >        assertEquals(m2, e.getKey());
1117 >        map.put(m1, "A");
1118 >        e = map.pollFirstEntry();
1119 >        assertEquals(m1, e.getKey());
1120 >        assertEquals("A", e.getValue());
1121 >        e = map.pollFirstEntry();
1122 >        assertEquals(m3, e.getKey());
1123 >        map.remove(m4);
1124 >        e = map.pollFirstEntry();
1125 >        assertEquals(m5, e.getKey());
1126 >        try {
1127 >            e.setValue("A");
1128 >            shouldThrow();
1129 >        } catch (UnsupportedOperationException success) {}
1130 >        e = map.pollFirstEntry();
1131 >        assertNull(e);
1132 >    }
1133 >
1134 >    /**
1135 >     * pollLastEntry returns entries in order
1136 >     */
1137 >    public void testDescendingPollLastEntry() {
1138 >        ConcurrentNavigableMap map = dmap5();
1139 >        Map.Entry e = map.pollLastEntry();
1140 >        assertEquals(m5, e.getKey());
1141 >        assertEquals("E", e.getValue());
1142 >        e = map.pollLastEntry();
1143 >        assertEquals(m4, e.getKey());
1144 >        map.put(m5, "E");
1145 >        e = map.pollLastEntry();
1146 >        assertEquals(m5, e.getKey());
1147 >        assertEquals("E", e.getValue());
1148 >        e = map.pollLastEntry();
1149 >        assertEquals(m3, e.getKey());
1150 >        map.remove(m2);
1151 >        e = map.pollLastEntry();
1152 >        assertEquals(m1, e.getKey());
1153 >        try {
1154 >            e.setValue("E");
1155 >            shouldThrow();
1156 >        } catch (UnsupportedOperationException success) {}
1157 >        e = map.pollLastEntry();
1158 >        assertNull(e);
1159 >    }
1160 >
1161 >    /**
1162 >     *   size returns the correct values
1163 >     */
1164 >    public void testDescendingSize() {
1165 >        ConcurrentNavigableMap map = dmap5();
1166 >        ConcurrentNavigableMap empty = dmap0();
1167 >        assertEquals(0, empty.size());
1168 >        assertEquals(5, map.size());
1169 >    }
1170 >
1171 >    /**
1172 >     * toString contains toString of elements
1173 >     */
1174 >    public void testDescendingToString() {
1175 >        ConcurrentNavigableMap map = dmap5();
1176 >        String s = map.toString();
1177 >        for (int i = 1; i <= 5; ++i) {
1178 >            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1179 >        }
1180 >    }
1181 >
1182 >    // Exception testDescendings
1183 >
1184 >    /**
1185 >     * get(null) of nm1mpty map throws NPE
1186 >     */
1187 >    public void testDescendingGet_NullPointerException() {
1188 >        try {
1189 >            ConcurrentNavigableMap c = dmap5();
1190 >            c.get(null);
1191 >            shouldThrow();
1192 >        } catch (NullPointerException success) {}
1193 >    }
1194 >
1195 >    /**
1196 >     * containsKey(null) of nm1mpty map throws NPE
1197 >     */
1198 >    public void testDescendingContainsKey_NullPointerException() {
1199 >        try {
1200 >            ConcurrentNavigableMap c = dmap5();
1201 >            c.containsKey(null);
1202 >            shouldThrow();
1203 >        } catch (NullPointerException success) {}
1204 >    }
1205 >
1206 >    /**
1207 >     * containsValue(null) throws NPE
1208 >     */
1209 >    public void testDescendingContainsValue_NullPointerException() {
1210 >        try {
1211 >            ConcurrentNavigableMap c = dmap0();
1212 >            c.containsValue(null);
1213 >            shouldThrow();
1214 >        } catch (NullPointerException success) {}
1215 >    }
1216 >
1217 >
1218 >    /**
1219 >     * put(null,x) throws NPE
1220 >     */
1221 >    public void testDescendingPut1_NullPointerException() {
1222 >        try {
1223 >            ConcurrentNavigableMap c = dmap5();
1224 >            c.put(null, "whatever");
1225 >            shouldThrow();
1226 >        } catch (NullPointerException success) {}
1227 >    }
1228 >
1229 >    /**
1230 >     * putIfAbsent(null, x) throws NPE
1231 >     */
1232 >    public void testDescendingPutIfAbsent1_NullPointerException() {
1233 >        try {
1234 >            ConcurrentNavigableMap c = dmap5();
1235 >            c.putIfAbsent(null, "whatever");
1236 >            shouldThrow();
1237 >        } catch (NullPointerException success) {}
1238 >    }
1239 >
1240 >    /**
1241 >     * replace(null, x) throws NPE
1242 >     */
1243 >    public void testDescendingReplace_NullPointerException() {
1244 >        try {
1245 >            ConcurrentNavigableMap c = dmap5();
1246 >            c.replace(null, "whatever");
1247 >            shouldThrow();
1248 >        } catch (NullPointerException success) {}
1249 >    }
1250 >
1251 >    /**
1252 >     * replace(null, x, y) throws NPE
1253 >     */
1254 >    public void testDescendingReplaceValue_NullPointerException() {
1255 >        try {
1256 >            ConcurrentNavigableMap c = dmap5();
1257 >            c.replace(null, m1, "whatever");
1258 >            shouldThrow();
1259 >        } catch (NullPointerException success) {}
1260 >    }
1261 >
1262 >    /**
1263 >     * remove(null) throws NPE
1264 >     */
1265 >    public void testDescendingRemove1_NullPointerException() {
1266 >        try {
1267 >            ConcurrentNavigableMap c = dmap5();
1268 >            c.remove(null);
1269 >            shouldThrow();
1270 >        } catch (NullPointerException success) {}
1271 >    }
1272 >
1273 >    /**
1274 >     * remove(null, x) throws NPE
1275 >     */
1276 >    public void testDescendingRemove2_NullPointerException() {
1277 >        try {
1278 >            ConcurrentNavigableMap c = dmap5();
1279 >            c.remove(null, "whatever");
1280 >            shouldThrow();
1281 >        } catch (NullPointerException success) {}
1282 >    }
1283 >
1284 >    /**
1285 >     * A deserialized map equals original
1286 >     */
1287 >    public void testDescendingSerialization() throws Exception {
1288 >        ConcurrentNavigableMap q = dmap5();
1289 >
1290 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1291 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1292 >        out.writeObject(q);
1293 >        out.close();
1294 >
1295 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1296 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1297 >        ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
1298 >        assertEquals(q.size(), r.size());
1299 >        assertTrue(q.equals(r));
1300 >        assertTrue(r.equals(q));
1301 >    }
1302 >
1303 >
1304 >    /**
1305 >     * subMap returns map with keys in requested range
1306 >     */
1307 >    public void testDescendingSubMapContents() {
1308 >        ConcurrentNavigableMap map = dmap5();
1309 >        SortedMap sm = map.subMap(m2, m4);
1310 >        assertEquals(m2, sm.firstKey());
1311 >        assertEquals(m3, sm.lastKey());
1312 >        assertEquals(2, sm.size());
1313 >        assertFalse(sm.containsKey(m1));
1314 >        assertTrue(sm.containsKey(m2));
1315 >        assertTrue(sm.containsKey(m3));
1316 >        assertFalse(sm.containsKey(m4));
1317 >        assertFalse(sm.containsKey(m5));
1318 >        Iterator i = sm.keySet().iterator();
1319 >        Object k;
1320 >        k = (Integer)(i.next());
1321 >        assertEquals(m2, k);
1322 >        k = (Integer)(i.next());
1323 >        assertEquals(m3, k);
1324 >        assertFalse(i.hasNext());
1325 >        Iterator j = sm.keySet().iterator();
1326 >        j.next();
1327 >        j.remove();
1328 >        assertFalse(map.containsKey(m2));
1329 >        assertEquals(4, map.size());
1330 >        assertEquals(1, sm.size());
1331 >        assertEquals(m3, sm.firstKey());
1332 >        assertEquals(m3, sm.lastKey());
1333 >        assertEquals("C", sm.remove(m3));
1334 >        assertTrue(sm.isEmpty());
1335 >        assertEquals(3, map.size());
1336 >    }
1337 >
1338 >    public void testDescendingSubMapContents2() {
1339 >        ConcurrentNavigableMap map = dmap5();
1340 >        SortedMap sm = map.subMap(m2, m3);
1341 >        assertEquals(1, sm.size());
1342 >        assertEquals(m2, sm.firstKey());
1343 >        assertEquals(m2, sm.lastKey());
1344 >        assertFalse(sm.containsKey(m1));
1345 >        assertTrue(sm.containsKey(m2));
1346 >        assertFalse(sm.containsKey(m3));
1347 >        assertFalse(sm.containsKey(m4));
1348 >        assertFalse(sm.containsKey(m5));
1349 >        Iterator i = sm.keySet().iterator();
1350 >        Object k;
1351 >        k = (Integer)(i.next());
1352 >        assertEquals(m2, k);
1353 >        assertFalse(i.hasNext());
1354 >        Iterator j = sm.keySet().iterator();
1355 >        j.next();
1356 >        j.remove();
1357 >        assertFalse(map.containsKey(m2));
1358 >        assertEquals(4, map.size());
1359 >        assertEquals(0, sm.size());
1360 >        assertTrue(sm.isEmpty());
1361 >        assertSame(sm.remove(m3), null);
1362 >        assertEquals(4, map.size());
1363 >    }
1364 >
1365 >    /**
1366 >     * headMap returns map with keys in requested range
1367 >     */
1368 >    public void testDescendingHeadMapContents() {
1369 >        ConcurrentNavigableMap map = dmap5();
1370 >        SortedMap sm = map.headMap(m4);
1371 >        assertTrue(sm.containsKey(m1));
1372 >        assertTrue(sm.containsKey(m2));
1373 >        assertTrue(sm.containsKey(m3));
1374 >        assertFalse(sm.containsKey(m4));
1375 >        assertFalse(sm.containsKey(m5));
1376 >        Iterator i = sm.keySet().iterator();
1377 >        Object k;
1378 >        k = (Integer)(i.next());
1379 >        assertEquals(m1, k);
1380 >        k = (Integer)(i.next());
1381 >        assertEquals(m2, k);
1382 >        k = (Integer)(i.next());
1383 >        assertEquals(m3, k);
1384 >        assertFalse(i.hasNext());
1385 >        sm.clear();
1386 >        assertTrue(sm.isEmpty());
1387 >        assertEquals(2, map.size());
1388 >        assertEquals(m4, map.firstKey());
1389 >    }
1390 >
1391 >    /**
1392 >     * headMap returns map with keys in requested range
1393 >     */
1394 >    public void testDescendingTailMapContents() {
1395 >        ConcurrentNavigableMap map = dmap5();
1396 >        SortedMap sm = map.tailMap(m2);
1397 >        assertFalse(sm.containsKey(m1));
1398 >        assertTrue(sm.containsKey(m2));
1399 >        assertTrue(sm.containsKey(m3));
1400 >        assertTrue(sm.containsKey(m4));
1401 >        assertTrue(sm.containsKey(m5));
1402 >        Iterator i = sm.keySet().iterator();
1403 >        Object k;
1404 >        k = (Integer)(i.next());
1405 >        assertEquals(m2, k);
1406 >        k = (Integer)(i.next());
1407 >        assertEquals(m3, k);
1408 >        k = (Integer)(i.next());
1409 >        assertEquals(m4, k);
1410 >        k = (Integer)(i.next());
1411 >        assertEquals(m5, k);
1412 >        assertFalse(i.hasNext());
1413 >
1414 >        Iterator ei = sm.entrySet().iterator();
1415 >        Map.Entry e;
1416 >        e = (Map.Entry)(ei.next());
1417 >        assertEquals(m2, e.getKey());
1418 >        assertEquals("B", e.getValue());
1419 >        e = (Map.Entry)(ei.next());
1420 >        assertEquals(m3, e.getKey());
1421 >        assertEquals("C", e.getValue());
1422 >        e = (Map.Entry)(ei.next());
1423 >        assertEquals(m4, e.getKey());
1424 >        assertEquals("D", e.getValue());
1425 >        e = (Map.Entry)(ei.next());
1426 >        assertEquals(m5, e.getKey());
1427 >        assertEquals("E", e.getValue());
1428 >        assertFalse(i.hasNext());
1429 >
1430 >        SortedMap ssm = sm.tailMap(m4);
1431 >        assertEquals(m4, ssm.firstKey());
1432 >        assertEquals(m5, ssm.lastKey());
1433 >        assertEquals("D", ssm.remove(m4));
1434 >        assertEquals(1, ssm.size());
1435 >        assertEquals(3, sm.size());
1436 >        assertEquals(4, map.size());
1437 >    }
1438 >
1439   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines