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.10 by jsr166, Sat Nov 21 10:25:05 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));
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 419 | Line 484 | public class ConcurrentSkipListSubMapTes
484      public void testSize() {
485          ConcurrentNavigableMap map = map5();
486          ConcurrentNavigableMap empty = map0();
487 <        assertEquals(0, empty.size());
488 <        assertEquals(5, map.size());
487 >        assertEquals(0, empty.size());
488 >        assertEquals(5, map.size());
489      }
490  
491      /**
# Line 432 | Line 497 | public class ConcurrentSkipListSubMapTes
497          for (int i = 1; i <= 5; ++i) {
498              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
499          }
500 <    }        
500 >    }
501  
502      // Exception tests
503  
# Line 444 | Line 509 | public class ConcurrentSkipListSubMapTes
509              ConcurrentNavigableMap c = map5();
510              c.get(null);
511              shouldThrow();
512 <        } catch(NullPointerException e){}
512 >        } catch (NullPointerException e) {}
513      }
514  
515      /**
# Line 455 | Line 520 | public class ConcurrentSkipListSubMapTes
520              ConcurrentNavigableMap c = map5();
521              c.containsKey(null);
522              shouldThrow();
523 <        } catch(NullPointerException e){}
523 >        } catch (NullPointerException e) {}
524      }
525  
526      /**
# Line 466 | Line 531 | public class ConcurrentSkipListSubMapTes
531              ConcurrentNavigableMap c = map0();
532              c.containsValue(null);
533              shouldThrow();
534 <        } catch(NullPointerException e){}
534 >        } catch (NullPointerException e) {}
535      }
536  
537  
# Line 478 | Line 543 | public class ConcurrentSkipListSubMapTes
543              ConcurrentNavigableMap c = map5();
544              c.put(null, "whatever");
545              shouldThrow();
546 <        } catch(NullPointerException e){}
546 >        } catch (NullPointerException e) {}
547      }
548  
549      /**
# Line 489 | Line 554 | public class ConcurrentSkipListSubMapTes
554              ConcurrentNavigableMap c = map5();
555              c.putIfAbsent(null, "whatever");
556              shouldThrow();
557 <        } catch(NullPointerException e){}
557 >        } catch (NullPointerException e) {}
558      }
559  
560      /**
# Line 500 | Line 565 | public class ConcurrentSkipListSubMapTes
565              ConcurrentNavigableMap c = map5();
566              c.replace(null, "whatever");
567              shouldThrow();
568 <        } catch(NullPointerException e){}
568 >        } catch (NullPointerException e) {}
569      }
570  
571      /**
# Line 511 | Line 576 | public class ConcurrentSkipListSubMapTes
576              ConcurrentNavigableMap c = map5();
577              c.replace(null, one, "whatever");
578              shouldThrow();
579 <        } catch(NullPointerException e){}
579 >        } catch (NullPointerException e) {}
580      }
581  
582      /**
# Line 522 | Line 587 | public class ConcurrentSkipListSubMapTes
587              ConcurrentNavigableMap c = map5();
588              c.remove(null);
589              shouldThrow();
590 <        } catch(NullPointerException e){}
590 >        } catch (NullPointerException e) {}
591      }
592  
593      /**
# Line 533 | Line 598 | public class ConcurrentSkipListSubMapTes
598              ConcurrentNavigableMap c = map5();
599              c.remove(null, "whatever");
600              shouldThrow();
601 <        } catch(NullPointerException e){}
601 >        } catch (NullPointerException e) {}
602      }
603  
604      /**
605       * A deserialized map equals original
606       */
607 <    public void testSerialization() {
607 >    public void testSerialization() throws Exception {
608          ConcurrentNavigableMap q = map5();
609  
610 <        try {
611 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
612 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
613 <            out.writeObject(q);
614 <            out.close();
615 <
616 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
617 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
618 <            ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
619 <            assertEquals(q.size(), r.size());
620 <            assertTrue(q.equals(r));
556 <            assertTrue(r.equals(q));
557 <        } catch(Exception e){
558 <            e.printStackTrace();
559 <            unexpectedException();
560 <        }
610 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
611 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
612 >        out.writeObject(q);
613 >        out.close();
614 >
615 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
616 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
617 >        ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
618 >        assertEquals(q.size(), r.size());
619 >        assertTrue(q.equals(r));
620 >        assertTrue(r.equals(q));
621      }
622  
623  
# Line 696 | Line 756 | public class ConcurrentSkipListSubMapTes
756          assertEquals(3, sm.size());
757          assertEquals(4, map.size());
758      }
759 <    
759 >
760 >    /**
761 >     *  clear removes all pairs
762 >     */
763 >    public void testDescendingClear() {
764 >        ConcurrentNavigableMap map = dmap5();
765 >        map.clear();
766 >        assertEquals(map.size(), 0);
767 >    }
768 >
769 >
770 >    /**
771 >     *  Maps with same contents are equal
772 >     */
773 >    public void testDescendingEquals() {
774 >        ConcurrentNavigableMap map1 = dmap5();
775 >        ConcurrentNavigableMap map2 = dmap5();
776 >        assertEquals(map1, map2);
777 >        assertEquals(map2, map1);
778 >        map1.clear();
779 >        assertFalse(map1.equals(map2));
780 >        assertFalse(map2.equals(map1));
781 >    }
782 >
783 >    /**
784 >     *  containsKey returns true for contained key
785 >     */
786 >    public void testDescendingContainsKey() {
787 >        ConcurrentNavigableMap map = dmap5();
788 >        assertTrue(map.containsKey(m1));
789 >        assertFalse(map.containsKey(zero));
790 >    }
791 >
792 >    /**
793 >     *  containsValue returns true for held values
794 >     */
795 >    public void testDescendingContainsValue() {
796 >        ConcurrentNavigableMap map = dmap5();
797 >        assertTrue(map.containsValue("A"));
798 >        assertFalse(map.containsValue("Z"));
799 >    }
800 >
801 >    /**
802 >     *  get returns the correct element at the given key,
803 >     *  or null if not present
804 >     */
805 >    public void testDescendingGet() {
806 >        ConcurrentNavigableMap map = dmap5();
807 >        assertEquals("A", (String)map.get(m1));
808 >        ConcurrentNavigableMap empty = dmap0();
809 >        assertNull(empty.get(m1));
810 >    }
811 >
812 >    /**
813 >     *  isEmpty is true of empty map and false for non-empty
814 >     */
815 >    public void testDescendingIsEmpty() {
816 >        ConcurrentNavigableMap empty = dmap0();
817 >        ConcurrentNavigableMap map = dmap5();
818 >        assertTrue(empty.isEmpty());
819 >        assertFalse(map.isEmpty());
820 >    }
821 >
822 >    /**
823 >     *   firstKey returns first key
824 >     */
825 >    public void testDescendingFirstKey() {
826 >        ConcurrentNavigableMap map = dmap5();
827 >        assertEquals(m1, map.firstKey());
828 >    }
829 >
830 >    /**
831 >     *   lastKey returns last key
832 >     */
833 >    public void testDescendingLastKey() {
834 >        ConcurrentNavigableMap map = dmap5();
835 >        assertEquals(m5, map.lastKey());
836 >    }
837 >
838 >
839 >    /**
840 >     *   keySet returns a Set containing all the keys
841 >     */
842 >    public void testDescendingKeySet() {
843 >        ConcurrentNavigableMap map = dmap5();
844 >        Set s = map.keySet();
845 >        assertEquals(5, s.size());
846 >        assertTrue(s.contains(m1));
847 >        assertTrue(s.contains(m2));
848 >        assertTrue(s.contains(m3));
849 >        assertTrue(s.contains(m4));
850 >        assertTrue(s.contains(m5));
851 >    }
852 >
853 >    /**
854 >     *   keySet is ordered
855 >     */
856 >    public void testDescendingKeySetOrder() {
857 >        ConcurrentNavigableMap map = dmap5();
858 >        Set s = map.keySet();
859 >        Iterator i = s.iterator();
860 >        Integer last = (Integer)i.next();
861 >        assertEquals(last, m1);
862 >        while (i.hasNext()) {
863 >            Integer k = (Integer)i.next();
864 >            assertTrue(last.compareTo(k) > 0);
865 >            last = k;
866 >        }
867 >    }
868 >
869 >    /**
870 >     * values collection contains all values
871 >     */
872 >    public void testDescendingValues() {
873 >        ConcurrentNavigableMap map = dmap5();
874 >        Collection s = map.values();
875 >        assertEquals(5, s.size());
876 >        assertTrue(s.contains("A"));
877 >        assertTrue(s.contains("B"));
878 >        assertTrue(s.contains("C"));
879 >        assertTrue(s.contains("D"));
880 >        assertTrue(s.contains("E"));
881 >    }
882 >
883 >    /**
884 >     *  keySet.toArray returns contains all keys
885 >     */
886 >    public void testDescendingAscendingKeySetToArray() {
887 >        ConcurrentNavigableMap map = dmap5();
888 >        Set s = map.keySet();
889 >        Object[] ar = s.toArray();
890 >        assertTrue(s.containsAll(Arrays.asList(ar)));
891 >        assertEquals(5, ar.length);
892 >        ar[0] = m10;
893 >        assertFalse(s.containsAll(Arrays.asList(ar)));
894 >    }
895 >
896 >    /**
897 >     *  descendingkeySet.toArray returns contains all keys
898 >     */
899 >    public void testDescendingDescendingKeySetToArray() {
900 >        ConcurrentNavigableMap map = dmap5();
901 >        Set s = map.descendingKeySet();
902 >        Object[] ar = s.toArray();
903 >        assertEquals(5, ar.length);
904 >        assertTrue(s.containsAll(Arrays.asList(ar)));
905 >        ar[0] = m10;
906 >        assertFalse(s.containsAll(Arrays.asList(ar)));
907 >    }
908 >
909 >    /**
910 >     *  Values.toArray contains all values
911 >     */
912 >    public void testDescendingValuesToArray() {
913 >        ConcurrentNavigableMap map = dmap5();
914 >        Collection v = map.values();
915 >        Object[] ar = v.toArray();
916 >        ArrayList s = new ArrayList(Arrays.asList(ar));
917 >        assertEquals(5, ar.length);
918 >        assertTrue(s.contains("A"));
919 >        assertTrue(s.contains("B"));
920 >        assertTrue(s.contains("C"));
921 >        assertTrue(s.contains("D"));
922 >        assertTrue(s.contains("E"));
923 >    }
924 >
925 >
926 >    /**
927 >     * entrySet contains all pairs
928 >     */
929 >    public void testDescendingEntrySet() {
930 >        ConcurrentNavigableMap map = dmap5();
931 >        Set s = map.entrySet();
932 >        assertEquals(5, s.size());
933 >        Iterator it = s.iterator();
934 >        while (it.hasNext()) {
935 >            Map.Entry e = (Map.Entry) it.next();
936 >            assertTrue(
937 >                       (e.getKey().equals(m1) && e.getValue().equals("A")) ||
938 >                       (e.getKey().equals(m2) && e.getValue().equals("B")) ||
939 >                       (e.getKey().equals(m3) && e.getValue().equals("C")) ||
940 >                       (e.getKey().equals(m4) && e.getValue().equals("D")) ||
941 >                       (e.getKey().equals(m5) && e.getValue().equals("E")));
942 >        }
943 >    }
944 >
945 >    /**
946 >     *   putAll  adds all key-value pairs from the given map
947 >     */
948 >    public void testDescendingPutAll() {
949 >        ConcurrentNavigableMap empty = dmap0();
950 >        ConcurrentNavigableMap map = dmap5();
951 >        empty.putAll(map);
952 >        assertEquals(5, empty.size());
953 >        assertTrue(empty.containsKey(m1));
954 >        assertTrue(empty.containsKey(m2));
955 >        assertTrue(empty.containsKey(m3));
956 >        assertTrue(empty.containsKey(m4));
957 >        assertTrue(empty.containsKey(m5));
958 >    }
959 >
960 >    /**
961 >     *   putIfAbsent works when the given key is not present
962 >     */
963 >    public void testDescendingPutIfAbsent() {
964 >        ConcurrentNavigableMap map = dmap5();
965 >        map.putIfAbsent(six, "Z");
966 >        assertTrue(map.containsKey(six));
967 >    }
968 >
969 >    /**
970 >     *   putIfAbsent does not add the pair if the key is already present
971 >     */
972 >    public void testDescendingPutIfAbsent2() {
973 >        ConcurrentNavigableMap map = dmap5();
974 >        assertEquals("A", map.putIfAbsent(m1, "Z"));
975 >    }
976 >
977 >    /**
978 >     *   replace fails when the given key is not present
979 >     */
980 >    public void testDescendingReplace() {
981 >        ConcurrentNavigableMap map = dmap5();
982 >        assertNull(map.replace(six, "Z"));
983 >        assertFalse(map.containsKey(six));
984 >    }
985 >
986 >    /**
987 >     *   replace succeeds if the key is already present
988 >     */
989 >    public void testDescendingReplace2() {
990 >        ConcurrentNavigableMap map = dmap5();
991 >        assertNotNull(map.replace(m1, "Z"));
992 >        assertEquals("Z", map.get(m1));
993 >    }
994 >
995 >
996 >    /**
997 >     * replace value fails when the given key not mapped to expected value
998 >     */
999 >    public void testDescendingReplaceValue() {
1000 >        ConcurrentNavigableMap map = dmap5();
1001 >        assertEquals("A", map.get(m1));
1002 >        assertFalse(map.replace(m1, "Z", "Z"));
1003 >        assertEquals("A", map.get(m1));
1004 >    }
1005 >
1006 >    /**
1007 >     * replace value succeeds when the given key mapped to expected value
1008 >     */
1009 >    public void testDescendingReplaceValue2() {
1010 >        ConcurrentNavigableMap map = dmap5();
1011 >        assertEquals("A", map.get(m1));
1012 >        assertTrue(map.replace(m1, "A", "Z"));
1013 >        assertEquals("Z", map.get(m1));
1014 >    }
1015 >
1016 >
1017 >    /**
1018 >     *   remove removes the correct key-value pair from the map
1019 >     */
1020 >    public void testDescendingRemove() {
1021 >        ConcurrentNavigableMap map = dmap5();
1022 >        map.remove(m5);
1023 >        assertEquals(4, map.size());
1024 >        assertFalse(map.containsKey(m5));
1025 >    }
1026 >
1027 >    /**
1028 >     * remove(key,value) removes only if pair present
1029 >     */
1030 >    public void testDescendingRemove2() {
1031 >        ConcurrentNavigableMap map = dmap5();
1032 >        assertTrue(map.containsKey(m5));
1033 >        assertEquals("E", map.get(m5));
1034 >        map.remove(m5, "E");
1035 >        assertEquals(4, map.size());
1036 >        assertFalse(map.containsKey(m5));
1037 >        map.remove(m4, "A");
1038 >        assertEquals(4, map.size());
1039 >        assertTrue(map.containsKey(m4));
1040 >
1041 >    }
1042 >
1043 >    /**
1044 >     * lowerEntry returns preceding entry.
1045 >     */
1046 >    public void testDescendingLowerEntry() {
1047 >        ConcurrentNavigableMap map = dmap5();
1048 >        Map.Entry e1 = map.lowerEntry(m3);
1049 >        assertEquals(m2, e1.getKey());
1050 >
1051 >        Map.Entry e2 = map.lowerEntry(m6);
1052 >        assertEquals(m5, e2.getKey());
1053 >
1054 >        Map.Entry e3 = map.lowerEntry(m1);
1055 >        assertNull(e3);
1056 >
1057 >        Map.Entry e4 = map.lowerEntry(zero);
1058 >        assertNull(e4);
1059 >
1060 >    }
1061 >
1062 >    /**
1063 >     * higherEntry returns next entry.
1064 >     */
1065 >    public void testDescendingHigherEntry() {
1066 >        ConcurrentNavigableMap map = dmap5();
1067 >        Map.Entry e1 = map.higherEntry(m3);
1068 >        assertEquals(m4, e1.getKey());
1069 >
1070 >        Map.Entry e2 = map.higherEntry(zero);
1071 >        assertEquals(m1, e2.getKey());
1072 >
1073 >        Map.Entry e3 = map.higherEntry(m5);
1074 >        assertNull(e3);
1075 >
1076 >        Map.Entry e4 = map.higherEntry(m6);
1077 >        assertNull(e4);
1078 >
1079 >    }
1080 >
1081 >    /**
1082 >     * floorEntry returns preceding entry.
1083 >     */
1084 >    public void testDescendingFloorEntry() {
1085 >        ConcurrentNavigableMap map = dmap5();
1086 >        Map.Entry e1 = map.floorEntry(m3);
1087 >        assertEquals(m3, e1.getKey());
1088 >
1089 >        Map.Entry e2 = map.floorEntry(m6);
1090 >        assertEquals(m5, e2.getKey());
1091 >
1092 >        Map.Entry e3 = map.floorEntry(m1);
1093 >        assertEquals(m1, e3.getKey());
1094 >
1095 >        Map.Entry e4 = map.floorEntry(zero);
1096 >        assertNull(e4);
1097 >
1098 >    }
1099 >
1100 >    /**
1101 >     * ceilingEntry returns next entry.
1102 >     */
1103 >    public void testDescendingCeilingEntry() {
1104 >        ConcurrentNavigableMap map = dmap5();
1105 >        Map.Entry e1 = map.ceilingEntry(m3);
1106 >        assertEquals(m3, e1.getKey());
1107 >
1108 >        Map.Entry e2 = map.ceilingEntry(zero);
1109 >        assertEquals(m1, e2.getKey());
1110 >
1111 >        Map.Entry e3 = map.ceilingEntry(m5);
1112 >        assertEquals(m5, e3.getKey());
1113 >
1114 >        Map.Entry e4 = map.ceilingEntry(m6);
1115 >        assertNull(e4);
1116 >
1117 >    }
1118 >
1119 >    /**
1120 >     * pollFirstEntry returns entries in order
1121 >     */
1122 >    public void testDescendingPollFirstEntry() {
1123 >        ConcurrentNavigableMap map = dmap5();
1124 >        Map.Entry e = map.pollFirstEntry();
1125 >        assertEquals(m1, e.getKey());
1126 >        assertEquals("A", e.getValue());
1127 >        e = map.pollFirstEntry();
1128 >        assertEquals(m2, e.getKey());
1129 >        map.put(m1, "A");
1130 >        e = map.pollFirstEntry();
1131 >        assertEquals(m1, e.getKey());
1132 >        assertEquals("A", e.getValue());
1133 >        e = map.pollFirstEntry();
1134 >        assertEquals(m3, e.getKey());
1135 >        map.remove(m4);
1136 >        e = map.pollFirstEntry();
1137 >        assertEquals(m5, e.getKey());
1138 >        try {
1139 >            e.setValue("A");
1140 >            shouldThrow();
1141 >        } catch (Exception ok) {
1142 >        }
1143 >        e = map.pollFirstEntry();
1144 >        assertNull(e);
1145 >    }
1146 >
1147 >    /**
1148 >     * pollLastEntry returns entries in order
1149 >     */
1150 >    public void testDescendingPollLastEntry() {
1151 >        ConcurrentNavigableMap map = dmap5();
1152 >        Map.Entry e = map.pollLastEntry();
1153 >        assertEquals(m5, e.getKey());
1154 >        assertEquals("E", e.getValue());
1155 >        e = map.pollLastEntry();
1156 >        assertEquals(m4, e.getKey());
1157 >        map.put(m5, "E");
1158 >        e = map.pollLastEntry();
1159 >        assertEquals(m5, e.getKey());
1160 >        assertEquals("E", e.getValue());
1161 >        e = map.pollLastEntry();
1162 >        assertEquals(m3, e.getKey());
1163 >        map.remove(m2);
1164 >        e = map.pollLastEntry();
1165 >        assertEquals(m1, e.getKey());
1166 >        try {
1167 >            e.setValue("E");
1168 >            shouldThrow();
1169 >        } catch (Exception ok) {
1170 >        }
1171 >        e = map.pollLastEntry();
1172 >        assertNull(e);
1173 >    }
1174 >
1175 >    /**
1176 >     *   size returns the correct values
1177 >     */
1178 >    public void testDescendingSize() {
1179 >        ConcurrentNavigableMap map = dmap5();
1180 >        ConcurrentNavigableMap empty = dmap0();
1181 >        assertEquals(0, empty.size());
1182 >        assertEquals(5, map.size());
1183 >    }
1184 >
1185 >    /**
1186 >     * toString contains toString of elements
1187 >     */
1188 >    public void testDescendingToString() {
1189 >        ConcurrentNavigableMap map = dmap5();
1190 >        String s = map.toString();
1191 >        for (int i = 1; i <= 5; ++i) {
1192 >            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1193 >        }
1194 >    }
1195 >
1196 >    // Exception testDescendings
1197 >
1198 >    /**
1199 >     * get(null) of nm1mpty map throws NPE
1200 >     */
1201 >    public void testDescendingGet_NullPointerException() {
1202 >        try {
1203 >            ConcurrentNavigableMap c = dmap5();
1204 >            c.get(null);
1205 >            shouldThrow();
1206 >        } catch (NullPointerException e) {}
1207 >    }
1208 >
1209 >    /**
1210 >     * containsKey(null) of nm1mpty map throws NPE
1211 >     */
1212 >    public void testDescendingContainsKey_NullPointerException() {
1213 >        try {
1214 >            ConcurrentNavigableMap c = dmap5();
1215 >            c.containsKey(null);
1216 >            shouldThrow();
1217 >        } catch (NullPointerException e) {}
1218 >    }
1219 >
1220 >    /**
1221 >     * containsValue(null) throws NPE
1222 >     */
1223 >    public void testDescendingContainsValue_NullPointerException() {
1224 >        try {
1225 >            ConcurrentNavigableMap c = dmap0();
1226 >            c.containsValue(null);
1227 >            shouldThrow();
1228 >        } catch (NullPointerException e) {}
1229 >    }
1230 >
1231 >
1232 >    /**
1233 >     * put(null,x) throws NPE
1234 >     */
1235 >    public void testDescendingPut1_NullPointerException() {
1236 >        try {
1237 >            ConcurrentNavigableMap c = dmap5();
1238 >            c.put(null, "whatever");
1239 >            shouldThrow();
1240 >        } catch (NullPointerException e) {}
1241 >    }
1242 >
1243 >    /**
1244 >     * putIfAbsent(null, x) throws NPE
1245 >     */
1246 >    public void testDescendingPutIfAbsent1_NullPointerException() {
1247 >        try {
1248 >            ConcurrentNavigableMap c = dmap5();
1249 >            c.putIfAbsent(null, "whatever");
1250 >            shouldThrow();
1251 >        } catch (NullPointerException e) {}
1252 >    }
1253 >
1254 >    /**
1255 >     * replace(null, x) throws NPE
1256 >     */
1257 >    public void testDescendingReplace_NullPointerException() {
1258 >        try {
1259 >            ConcurrentNavigableMap c = dmap5();
1260 >            c.replace(null, "whatever");
1261 >            shouldThrow();
1262 >        } catch (NullPointerException e) {}
1263 >    }
1264 >
1265 >    /**
1266 >     * replace(null, x, y) throws NPE
1267 >     */
1268 >    public void testDescendingReplaceValue_NullPointerException() {
1269 >        try {
1270 >            ConcurrentNavigableMap c = dmap5();
1271 >            c.replace(null, m1, "whatever");
1272 >            shouldThrow();
1273 >        } catch (NullPointerException e) {}
1274 >    }
1275 >
1276 >    /**
1277 >     * remove(null) throws NPE
1278 >     */
1279 >    public void testDescendingRemove1_NullPointerException() {
1280 >        try {
1281 >            ConcurrentNavigableMap c = dmap5();
1282 >            c.remove(null);
1283 >            shouldThrow();
1284 >        } catch (NullPointerException e) {}
1285 >    }
1286 >
1287 >    /**
1288 >     * remove(null, x) throws NPE
1289 >     */
1290 >    public void testDescendingRemove2_NullPointerException() {
1291 >        try {
1292 >            ConcurrentNavigableMap c = dmap5();
1293 >            c.remove(null, "whatever");
1294 >            shouldThrow();
1295 >        } catch (NullPointerException e) {}
1296 >    }
1297 >
1298 >    /**
1299 >     * A deserialized map equals original
1300 >     */
1301 >    public void testDescendingSerialization() throws Exception {
1302 >        ConcurrentNavigableMap q = dmap5();
1303 >
1304 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1305 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1306 >        out.writeObject(q);
1307 >        out.close();
1308 >
1309 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1310 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1311 >        ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
1312 >        assertEquals(q.size(), r.size());
1313 >        assertTrue(q.equals(r));
1314 >        assertTrue(r.equals(q));
1315 >    }
1316 >
1317 >
1318 >    /**
1319 >     * subMap returns map with keys in requested range
1320 >     */
1321 >    public void testDescendingSubMapContents() {
1322 >        ConcurrentNavigableMap map = dmap5();
1323 >        SortedMap sm = map.subMap(m2, m4);
1324 >        assertEquals(m2, sm.firstKey());
1325 >        assertEquals(m3, sm.lastKey());
1326 >        assertEquals(2, sm.size());
1327 >        assertFalse(sm.containsKey(m1));
1328 >        assertTrue(sm.containsKey(m2));
1329 >        assertTrue(sm.containsKey(m3));
1330 >        assertFalse(sm.containsKey(m4));
1331 >        assertFalse(sm.containsKey(m5));
1332 >        Iterator i = sm.keySet().iterator();
1333 >        Object k;
1334 >        k = (Integer)(i.next());
1335 >        assertEquals(m2, k);
1336 >        k = (Integer)(i.next());
1337 >        assertEquals(m3, k);
1338 >        assertFalse(i.hasNext());
1339 >        Iterator j = sm.keySet().iterator();
1340 >        j.next();
1341 >        j.remove();
1342 >        assertFalse(map.containsKey(m2));
1343 >        assertEquals(4, map.size());
1344 >        assertEquals(1, sm.size());
1345 >        assertEquals(m3, sm.firstKey());
1346 >        assertEquals(m3, sm.lastKey());
1347 >        assertTrue(sm.remove(m3) != null);
1348 >        assertTrue(sm.isEmpty());
1349 >        assertEquals(3, map.size());
1350 >    }
1351 >
1352 >    public void testDescendingSubMapContents2() {
1353 >        ConcurrentNavigableMap map = dmap5();
1354 >        SortedMap sm = map.subMap(m2, m3);
1355 >        assertEquals(1, sm.size());
1356 >        assertEquals(m2, sm.firstKey());
1357 >        assertEquals(m2, sm.lastKey());
1358 >        assertFalse(sm.containsKey(m1));
1359 >        assertTrue(sm.containsKey(m2));
1360 >        assertFalse(sm.containsKey(m3));
1361 >        assertFalse(sm.containsKey(m4));
1362 >        assertFalse(sm.containsKey(m5));
1363 >        Iterator i = sm.keySet().iterator();
1364 >        Object k;
1365 >        k = (Integer)(i.next());
1366 >        assertEquals(m2, k);
1367 >        assertFalse(i.hasNext());
1368 >        Iterator j = sm.keySet().iterator();
1369 >        j.next();
1370 >        j.remove();
1371 >        assertFalse(map.containsKey(m2));
1372 >        assertEquals(4, map.size());
1373 >        assertEquals(0, sm.size());
1374 >        assertTrue(sm.isEmpty());
1375 >        assertTrue(sm.remove(m3) == null);
1376 >        assertEquals(4, map.size());
1377 >    }
1378 >
1379 >    /**
1380 >     * headMap returns map with keys in requested range
1381 >     */
1382 >    public void testDescendingHeadMapContents() {
1383 >        ConcurrentNavigableMap map = dmap5();
1384 >        SortedMap sm = map.headMap(m4);
1385 >        assertTrue(sm.containsKey(m1));
1386 >        assertTrue(sm.containsKey(m2));
1387 >        assertTrue(sm.containsKey(m3));
1388 >        assertFalse(sm.containsKey(m4));
1389 >        assertFalse(sm.containsKey(m5));
1390 >        Iterator i = sm.keySet().iterator();
1391 >        Object k;
1392 >        k = (Integer)(i.next());
1393 >        assertEquals(m1, k);
1394 >        k = (Integer)(i.next());
1395 >        assertEquals(m2, k);
1396 >        k = (Integer)(i.next());
1397 >        assertEquals(m3, k);
1398 >        assertFalse(i.hasNext());
1399 >        sm.clear();
1400 >        assertTrue(sm.isEmpty());
1401 >        assertEquals(2, map.size());
1402 >        assertEquals(m4, map.firstKey());
1403 >    }
1404 >
1405 >    /**
1406 >     * headMap returns map with keys in requested range
1407 >     */
1408 >    public void testDescendingTailMapContents() {
1409 >        ConcurrentNavigableMap map = dmap5();
1410 >        SortedMap sm = map.tailMap(m2);
1411 >        assertFalse(sm.containsKey(m1));
1412 >        assertTrue(sm.containsKey(m2));
1413 >        assertTrue(sm.containsKey(m3));
1414 >        assertTrue(sm.containsKey(m4));
1415 >        assertTrue(sm.containsKey(m5));
1416 >        Iterator i = sm.keySet().iterator();
1417 >        Object k;
1418 >        k = (Integer)(i.next());
1419 >        assertEquals(m2, k);
1420 >        k = (Integer)(i.next());
1421 >        assertEquals(m3, k);
1422 >        k = (Integer)(i.next());
1423 >        assertEquals(m4, k);
1424 >        k = (Integer)(i.next());
1425 >        assertEquals(m5, k);
1426 >        assertFalse(i.hasNext());
1427 >
1428 >        Iterator ei = sm.entrySet().iterator();
1429 >        Map.Entry e;
1430 >        e = (Map.Entry)(ei.next());
1431 >        assertEquals(m2, e.getKey());
1432 >        assertEquals("B", e.getValue());
1433 >        e = (Map.Entry)(ei.next());
1434 >        assertEquals(m3, e.getKey());
1435 >        assertEquals("C", e.getValue());
1436 >        e = (Map.Entry)(ei.next());
1437 >        assertEquals(m4, e.getKey());
1438 >        assertEquals("D", e.getValue());
1439 >        e = (Map.Entry)(ei.next());
1440 >        assertEquals(m5, e.getKey());
1441 >        assertEquals("E", e.getValue());
1442 >        assertFalse(i.hasNext());
1443 >
1444 >        SortedMap ssm = sm.tailMap(m4);
1445 >        assertEquals(m4, ssm.firstKey());
1446 >        assertEquals(m5, ssm.lastKey());
1447 >        assertTrue(ssm.remove(m4) != null);
1448 >        assertEquals(1, ssm.size());
1449 >        assertEquals(3, sm.size());
1450 >        assertEquals(4, map.size());
1451 >    }
1452 >
1453   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines