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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines