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

Comparing jsr166/src/test/tck/TreeMapTest.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 TreeMapTest 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(TreeMapTest.class);
17 >        return new TestSuite(TreeMapTest.class);
18      }
19  
20      /**
21       * Create a map from Integers 1-5 to Strings "A"-"E".
22       */
23 <    private static TreeMap map5() {  
24 <        TreeMap map = new TreeMap();
23 >    private static TreeMap map5() {
24 >        TreeMap map = new TreeMap();
25          assertTrue(map.isEmpty());
26 <        map.put(one, "A");
27 <        map.put(five, "E");
28 <        map.put(three, "C");
29 <        map.put(two, "B");
30 <        map.put(four, "D");
26 >        map.put(one, "A");
27 >        map.put(five, "E");
28 >        map.put(three, "C");
29 >        map.put(two, "B");
30 >        map.put(four, "D");
31          assertFalse(map.isEmpty());
32          assertEquals(5, map.size());
33 <        return map;
33 >        return map;
34      }
35  
36      /**
# Line 38 | Line 38 | public class TreeMapTest extends JSR166T
38       */
39      public void testClear() {
40          TreeMap map = map5();
41 <        map.clear();
42 <        assertEquals(map.size(), 0);
41 >        map.clear();
42 >        assertEquals(map.size(), 0);
43      }
44  
45      /**
46 <     *  
46 >     *
47       */
48      public void testConstructFromSorted() {
49          TreeMap map = map5();
# Line 59 | Line 59 | public class TreeMapTest extends JSR166T
59          TreeMap map2 = map5();
60          assertEquals(map1, map2);
61          assertEquals(map2, map1);
62 <        map1.clear();
62 >        map1.clear();
63          assertFalse(map1.equals(map2));
64          assertFalse(map2.equals(map1));
65      }
# Line 69 | Line 69 | public class TreeMapTest extends JSR166T
69       */
70      public void testContainsKey() {
71          TreeMap map = map5();
72 <        assertTrue(map.containsKey(one));
72 >        assertTrue(map.containsKey(one));
73          assertFalse(map.containsKey(zero));
74      }
75  
# Line 78 | Line 78 | public class TreeMapTest extends JSR166T
78       */
79      public void testContainsValue() {
80          TreeMap map = map5();
81 <        assertTrue(map.containsValue("A"));
81 >        assertTrue(map.containsValue("A"));
82          assertFalse(map.containsValue("Z"));
83      }
84  
# Line 88 | Line 88 | public class TreeMapTest extends JSR166T
88       */
89      public void testGet() {
90          TreeMap map = map5();
91 <        assertEquals("A", (String)map.get(one));
91 >        assertEquals("A", (String)map.get(one));
92          TreeMap empty = new TreeMap();
93          assertNull(empty.get(one));
94      }
# Line 99 | Line 99 | public class TreeMapTest extends JSR166T
99      public void testIsEmpty() {
100          TreeMap empty = new TreeMap();
101          TreeMap map = map5();
102 <        assertTrue(empty.isEmpty());
102 >        assertTrue(empty.isEmpty());
103          assertFalse(map.isEmpty());
104      }
105  
# Line 108 | Line 108 | public class TreeMapTest extends JSR166T
108       */
109      public void testFirstKey() {
110          TreeMap map = map5();
111 <        assertEquals(one, map.firstKey());
111 >        assertEquals(one, map.firstKey());
112      }
113  
114      /**
# Line 116 | Line 116 | public class TreeMapTest extends JSR166T
116       */
117      public void testLastKey() {
118          TreeMap map = map5();
119 <        assertEquals(five, map.lastKey());
119 >        assertEquals(five, map.lastKey());
120      }
121  
122  
# Line 125 | Line 125 | public class TreeMapTest extends JSR166T
125       */
126      public void testKeySetToArray() {
127          TreeMap map = map5();
128 <        Set s = map.keySet();
128 >        Set s = map.keySet();
129          Object[] ar = s.toArray();
130          assertTrue(s.containsAll(Arrays.asList(ar)));
131 <        assertEquals(5, ar.length);
131 >        assertEquals(5, ar.length);
132          ar[0] = m10;
133          assertFalse(s.containsAll(Arrays.asList(ar)));
134      }
# Line 138 | Line 138 | public class TreeMapTest extends JSR166T
138       */
139      public void testDescendingKeySetToArray() {
140          TreeMap map = map5();
141 <        Set s = map.descendingKeySet();
141 >        Set s = map.descendingKeySet();
142          Object[] ar = s.toArray();
143 <        assertEquals(5, ar.length);
143 >        assertEquals(5, ar.length);
144          assertTrue(s.containsAll(Arrays.asList(ar)));
145          ar[0] = m10;
146          assertFalse(s.containsAll(Arrays.asList(ar)));
# Line 151 | Line 151 | public class TreeMapTest extends JSR166T
151       */
152      public void testKeySet() {
153          TreeMap map = map5();
154 <        Set s = map.keySet();
155 <        assertEquals(5, s.size());
156 <        assertTrue(s.contains(one));
157 <        assertTrue(s.contains(two));
158 <        assertTrue(s.contains(three));
159 <        assertTrue(s.contains(four));
160 <        assertTrue(s.contains(five));
154 >        Set s = map.keySet();
155 >        assertEquals(5, s.size());
156 >        assertTrue(s.contains(one));
157 >        assertTrue(s.contains(two));
158 >        assertTrue(s.contains(three));
159 >        assertTrue(s.contains(four));
160 >        assertTrue(s.contains(five));
161      }
162  
163      /**
# Line 165 | Line 165 | public class TreeMapTest extends JSR166T
165       */
166      public void testKeySetOrder() {
167          TreeMap map = map5();
168 <        Set s = map.keySet();
168 >        Set s = map.keySet();
169          Iterator i = s.iterator();
170          Integer last = (Integer)i.next();
171          assertEquals(last, one);
172 +        int count = 1;
173          while (i.hasNext()) {
174              Integer k = (Integer)i.next();
175              assertTrue(last.compareTo(k) < 0);
176              last = k;
177 +            ++count;
178          }
179 +        assertEquals(count ,5);
180 +    }
181 +
182 +    /**
183 +     * descending iterator of key set is inverse ordered
184 +     */
185 +    public void testKeySetDescendingIteratorOrder() {
186 +        TreeMap map = map5();
187 +        NavigableSet s = map.navigableKeySet();
188 +        Iterator i = s.descendingIterator();
189 +        Integer last = (Integer)i.next();
190 +        assertEquals(last, five);
191 +        int count = 1;
192 +        while (i.hasNext()) {
193 +            Integer k = (Integer)i.next();
194 +            assertTrue(last.compareTo(k) > 0);
195 +            last = k;
196 +            ++count;
197 +        }
198 +        assertEquals(count ,5);
199      }
200  
201      /**
# Line 181 | Line 203 | public class TreeMapTest extends JSR166T
203       */
204      public void testDescendingKeySetOrder() {
205          TreeMap map = map5();
206 <        Set s = map.descendingKeySet();
206 >        Set s = map.descendingKeySet();
207          Iterator i = s.iterator();
208          Integer last = (Integer)i.next();
209          assertEquals(last, five);
210 +        int count = 1;
211          while (i.hasNext()) {
212              Integer k = (Integer)i.next();
213              assertTrue(last.compareTo(k) > 0);
214              last = k;
215 +            ++count;
216          }
217 +        assertEquals(count, 5);
218 +    }
219 +
220 +    /**
221 +     *  descending iterator of descendingKeySet is ordered
222 +     */
223 +    public void testDescendingKeySetDescendingIteratorOrder() {
224 +        TreeMap map = map5();
225 +        NavigableSet s = map.descendingKeySet();
226 +        Iterator i = s.descendingIterator();
227 +        Integer last = (Integer)i.next();
228 +        assertEquals(last, one);
229 +        int count = 1;
230 +        while (i.hasNext()) {
231 +            Integer k = (Integer)i.next();
232 +            assertTrue(last.compareTo(k) < 0);
233 +            last = k;
234 +            ++count;
235 +        }
236 +        assertEquals(count, 5);
237      }
238  
239      /**
# Line 197 | Line 241 | public class TreeMapTest extends JSR166T
241       */
242      public void testValues() {
243          TreeMap map = map5();
244 <        Collection s = map.values();
245 <        assertEquals(5, s.size());
246 <        assertTrue(s.contains("A"));
247 <        assertTrue(s.contains("B"));
248 <        assertTrue(s.contains("C"));
249 <        assertTrue(s.contains("D"));
250 <        assertTrue(s.contains("E"));
244 >        Collection s = map.values();
245 >        assertEquals(5, s.size());
246 >        assertTrue(s.contains("A"));
247 >        assertTrue(s.contains("B"));
248 >        assertTrue(s.contains("C"));
249 >        assertTrue(s.contains("D"));
250 >        assertTrue(s.contains("E"));
251      }
252  
253      /**
# Line 211 | Line 255 | public class TreeMapTest extends JSR166T
255       */
256      public void testEntrySet() {
257          TreeMap map = map5();
258 <        Set s = map.entrySet();
259 <        assertEquals(5, s.size());
258 >        Set s = map.entrySet();
259 >        assertEquals(5, s.size());
260          Iterator it = s.iterator();
261          while (it.hasNext()) {
262              Map.Entry e = (Map.Entry) it.next();
263 <            assertTrue(
263 >            assertTrue(
264                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
265                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
266                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 230 | Line 274 | public class TreeMapTest extends JSR166T
274       */
275      public void testDescendingEntrySet() {
276          TreeMap map = map5();
277 <        Set s = map.descendingEntrySet();
278 <        assertEquals(5, s.size());
277 >        Set s = map.descendingMap().entrySet();
278 >        assertEquals(5, s.size());
279          Iterator it = s.iterator();
280          while (it.hasNext()) {
281              Map.Entry e = (Map.Entry) it.next();
282 <            assertTrue(
282 >            assertTrue(
283                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
284                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
285                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 249 | Line 293 | public class TreeMapTest extends JSR166T
293       */
294      public void testEntrySetToArray() {
295          TreeMap map = map5();
296 <        Set s = map.entrySet();
296 >        Set s = map.entrySet();
297          Object[] ar = s.toArray();
298          assertEquals(5, ar.length);
299          for (int i = 0; i < 5; ++i) {
# Line 263 | Line 307 | public class TreeMapTest extends JSR166T
307       */
308      public void testDescendingEntrySetToArray() {
309          TreeMap map = map5();
310 <        Set s = map.descendingEntrySet();
310 >        Set s = map.descendingMap().entrySet();
311          Object[] ar = s.toArray();
312          assertEquals(5, ar.length);
313          for (int i = 0; i < 5; ++i) {
# Line 278 | Line 322 | public class TreeMapTest extends JSR166T
322      public void testPutAll() {
323          TreeMap empty = new TreeMap();
324          TreeMap map = map5();
325 <        empty.putAll(map);
326 <        assertEquals(5, empty.size());
327 <        assertTrue(empty.containsKey(one));
328 <        assertTrue(empty.containsKey(two));
329 <        assertTrue(empty.containsKey(three));
330 <        assertTrue(empty.containsKey(four));
331 <        assertTrue(empty.containsKey(five));
325 >        empty.putAll(map);
326 >        assertEquals(5, empty.size());
327 >        assertTrue(empty.containsKey(one));
328 >        assertTrue(empty.containsKey(two));
329 >        assertTrue(empty.containsKey(three));
330 >        assertTrue(empty.containsKey(four));
331 >        assertTrue(empty.containsKey(five));
332      }
333  
334      /**
# Line 292 | Line 336 | public class TreeMapTest extends JSR166T
336       */
337      public void testRemove() {
338          TreeMap map = map5();
339 <        map.remove(five);
340 <        assertEquals(4, map.size());
341 <        assertFalse(map.containsKey(five));
339 >        map.remove(five);
340 >        assertEquals(4, map.size());
341 >        assertFalse(map.containsKey(five));
342      }
343  
344      /**
# Line 512 | Line 556 | public class TreeMapTest extends JSR166T
556      public void testSize() {
557          TreeMap map = map5();
558          TreeMap empty = new TreeMap();
559 <        assertEquals(0, empty.size());
560 <        assertEquals(5, map.size());
559 >        assertEquals(0, empty.size());
560 >        assertEquals(5, map.size());
561      }
562  
563      /**
# Line 525 | Line 569 | public class TreeMapTest extends JSR166T
569          for (int i = 1; i <= 5; ++i) {
570              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
571          }
572 <    }        
572 >    }
573  
574      // Exception tests
575  
# Line 537 | Line 581 | public class TreeMapTest extends JSR166T
581              TreeMap c = map5();
582              c.get(null);
583              shouldThrow();
584 <        } catch(NullPointerException e){}
584 >        } catch (NullPointerException e) {}
585      }
586  
587      /**
# Line 548 | Line 592 | public class TreeMapTest extends JSR166T
592              TreeMap c = map5();
593              c.containsKey(null);
594              shouldThrow();
595 <        } catch(NullPointerException e){}
595 >        } catch (NullPointerException e) {}
596      }
597  
598      /**
# Line 560 | Line 604 | public class TreeMapTest extends JSR166T
604              c.put("sadsdf", "asdads");
605              c.remove(null);
606              shouldThrow();
607 <        } catch(NullPointerException e){}
607 >        } catch (NullPointerException e) {}
608      }
609  
610      /**
611       * A deserialized map equals original
612       */
613 <    public void testSerialization() {
613 >    public void testSerialization() throws Exception {
614          TreeMap q = map5();
615  
616 <        try {
617 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
618 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
619 <            out.writeObject(q);
620 <            out.close();
621 <
622 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
623 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
624 <            TreeMap r = (TreeMap)in.readObject();
625 <            assertEquals(q.size(), r.size());
626 <            assertTrue(q.equals(r));
583 <            assertTrue(r.equals(q));
584 <        } catch(Exception e){
585 <            e.printStackTrace();
586 <            unexpectedException();
587 <        }
616 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
617 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
618 >        out.writeObject(q);
619 >        out.close();
620 >
621 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
622 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
623 >        TreeMap r = (TreeMap)in.readObject();
624 >        assertEquals(q.size(), r.size());
625 >        assertTrue(q.equals(r));
626 >        assertTrue(r.equals(q));
627      }
628  
629      /**
# Line 592 | Line 631 | public class TreeMapTest extends JSR166T
631       */
632      public void testSubMapContents() {
633          TreeMap map = map5();
634 <        NavigableMap sm = map.navigableSubMap(two, four);
634 >        NavigableMap sm = map.subMap(two, true, four, false);
635          assertEquals(two, sm.firstKey());
636          assertEquals(three, sm.lastKey());
637          assertEquals(2, sm.size());
# Line 614 | Line 653 | public class TreeMapTest extends JSR166T
653          k = (Integer)(r.next());
654          assertEquals(two, k);
655          assertFalse(r.hasNext());
656 <        
656 >
657          Iterator j = sm.keySet().iterator();
658          j.next();
659          j.remove();
# Line 630 | Line 669 | public class TreeMapTest extends JSR166T
669  
670      public void testSubMapContents2() {
671          TreeMap map = map5();
672 <        NavigableMap sm = map.navigableSubMap(two, three);
672 >        NavigableMap sm = map.subMap(two, true, three, false);
673          assertEquals(1, sm.size());
674          assertEquals(two, sm.firstKey());
675          assertEquals(two, sm.lastKey());
# Line 648 | Line 687 | public class TreeMapTest extends JSR166T
687          k = (Integer)(r.next());
688          assertEquals(two, k);
689          assertFalse(r.hasNext());
690 <        
690 >
691          Iterator j = sm.keySet().iterator();
692          j.next();
693          j.remove();
# Line 665 | Line 704 | public class TreeMapTest extends JSR166T
704       */
705      public void testHeadMapContents() {
706          TreeMap map = map5();
707 <        NavigableMap sm = map.navigableHeadMap(four);
707 >        NavigableMap sm = map.headMap(four, false);
708          assertTrue(sm.containsKey(one));
709          assertTrue(sm.containsKey(two));
710          assertTrue(sm.containsKey(three));
# Line 691 | Line 730 | public class TreeMapTest extends JSR166T
730       */
731      public void testTailMapContents() {
732          TreeMap map = map5();
733 <        NavigableMap sm = map.navigableTailMap(two);
733 >        NavigableMap sm = map.tailMap(two, true);
734          assertFalse(sm.containsKey(one));
735          assertTrue(sm.containsKey(two));
736          assertTrue(sm.containsKey(three));
# Line 735 | Line 774 | public class TreeMapTest extends JSR166T
774          assertEquals("E", e.getValue());
775          assertFalse(i.hasNext());
776  
777 <        NavigableMap ssm = sm.navigableTailMap(four);
777 >        NavigableMap ssm = sm.tailMap(four, true);
778          assertEquals(four, ssm.firstKey());
779          assertEquals(five, ssm.lastKey());
780          assertTrue(ssm.remove(four) != null);
# Line 743 | Line 782 | public class TreeMapTest extends JSR166T
782          assertEquals(3, sm.size());
783          assertEquals(4, map.size());
784      }
785 <    
785 >
786 >    Random rnd = new Random(666);
787 >    BitSet bs;
788 >
789 >    /**
790 >     * Submaps of submaps subdivide correctly
791 >     */
792 >    public void testRecursiveSubMaps() {
793 >        int mapSize = 1000;
794 >        Class cl = TreeMap.class;
795 >        NavigableMap<Integer, Integer> map = newMap(cl);
796 >        bs = new BitSet(mapSize);
797 >
798 >        populate(map, mapSize);
799 >        check(map,                 0, mapSize - 1, true);
800 >        check(map.descendingMap(), 0, mapSize - 1, false);
801 >
802 >        mutateMap(map, 0, mapSize - 1);
803 >        check(map,                 0, mapSize - 1, true);
804 >        check(map.descendingMap(), 0, mapSize - 1, false);
805 >
806 >        bashSubMap(map.subMap(0, true, mapSize, false),
807 >                   0, mapSize - 1, true);
808 >    }
809 >
810 >    static NavigableMap<Integer, Integer> newMap(Class cl) {
811 >        NavigableMap<Integer, Integer> result = null;
812 >        try {
813 >            result = (NavigableMap<Integer, Integer>) cl.newInstance();
814 >        } catch (Exception e) {
815 >            fail();
816 >        }
817 >        assertEquals(result.size(), 0);
818 >        assertFalse(result.keySet().iterator().hasNext());
819 >        return result;
820 >    }
821 >
822 >    void populate(NavigableMap<Integer, Integer> map, int limit) {
823 >        for (int i = 0, n = 2 * limit / 3; i < n; i++) {
824 >            int key = rnd.nextInt(limit);
825 >            put(map, key);
826 >        }
827 >    }
828 >
829 >    void mutateMap(NavigableMap<Integer, Integer> map, int min, int max) {
830 >        int size = map.size();
831 >        int rangeSize = max - min + 1;
832 >
833 >        // Remove a bunch of entries directly
834 >        for (int i = 0, n = rangeSize / 2; i < n; i++) {
835 >            remove(map, min - 5 + rnd.nextInt(rangeSize + 10));
836 >        }
837 >
838 >        // Remove a bunch of entries with iterator
839 >        for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
840 >            if (rnd.nextBoolean()) {
841 >                bs.clear(it.next());
842 >                it.remove();
843 >            }
844 >        }
845 >
846 >        // Add entries till we're back to original size
847 >        while (map.size() < size) {
848 >            int key = min + rnd.nextInt(rangeSize);
849 >            assertTrue(key >= min && key<= max);
850 >            put(map, key);
851 >        }
852 >    }
853 >
854 >    void mutateSubMap(NavigableMap<Integer, Integer> map, int min, int max) {
855 >        int size = map.size();
856 >        int rangeSize = max - min + 1;
857 >
858 >        // Remove a bunch of entries directly
859 >        for (int i = 0, n = rangeSize / 2; i < n; i++) {
860 >            remove(map, min - 5 + rnd.nextInt(rangeSize + 10));
861 >        }
862 >
863 >        // Remove a bunch of entries with iterator
864 >        for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
865 >            if (rnd.nextBoolean()) {
866 >                bs.clear(it.next());
867 >                it.remove();
868 >            }
869 >        }
870 >
871 >        // Add entries till we're back to original size
872 >        while (map.size() < size) {
873 >            int key = min - 5 + rnd.nextInt(rangeSize + 10);
874 >            if (key >= min && key<= max) {
875 >                put(map, key);
876 >            } else {
877 >                try {
878 >                    map.put(key, 2 * key);
879 >                    fail();
880 >                } catch (IllegalArgumentException e) {
881 >                    // expected
882 >                }
883 >            }
884 >        }
885 >    }
886 >
887 >    void put(NavigableMap<Integer, Integer> map, int key) {
888 >        if (map.put(key, 2 * key) == null)
889 >            bs.set(key);
890 >    }
891 >
892 >    void remove(NavigableMap<Integer, Integer> map, int key) {
893 >        if (map.remove(key) != null)
894 >            bs.clear(key);
895 >    }
896 >
897 >    void bashSubMap(NavigableMap<Integer, Integer> map,
898 >                    int min, int max, boolean ascending) {
899 >        check(map, min, max, ascending);
900 >        check(map.descendingMap(), min, max, !ascending);
901 >
902 >        mutateSubMap(map, min, max);
903 >        check(map, min, max, ascending);
904 >        check(map.descendingMap(), min, max, !ascending);
905 >
906 >        // Recurse
907 >        if (max - min < 2)
908 >            return;
909 >        int midPoint = (min + max) / 2;
910 >
911 >        // headMap - pick direction and endpoint inclusion randomly
912 >        boolean incl = rnd.nextBoolean();
913 >        NavigableMap<Integer,Integer> hm = map.headMap(midPoint, incl);
914 >        if (ascending) {
915 >            if (rnd.nextBoolean())
916 >                bashSubMap(hm, min, midPoint - (incl ? 0 : 1), true);
917 >            else
918 >                bashSubMap(hm.descendingMap(), min, midPoint - (incl ? 0 : 1),
919 >                           false);
920 >        } else {
921 >            if (rnd.nextBoolean())
922 >                bashSubMap(hm, midPoint + (incl ? 0 : 1), max, false);
923 >            else
924 >                bashSubMap(hm.descendingMap(), midPoint + (incl ? 0 : 1), max,
925 >                           true);
926 >        }
927 >
928 >        // tailMap - pick direction and endpoint inclusion randomly
929 >        incl = rnd.nextBoolean();
930 >        NavigableMap<Integer,Integer> tm = map.tailMap(midPoint,incl);
931 >        if (ascending) {
932 >            if (rnd.nextBoolean())
933 >                bashSubMap(tm, midPoint + (incl ? 0 : 1), max, true);
934 >            else
935 >                bashSubMap(tm.descendingMap(), midPoint + (incl ? 0 : 1), max,
936 >                           false);
937 >        } else {
938 >            if (rnd.nextBoolean()) {
939 >                bashSubMap(tm, min, midPoint - (incl ? 0 : 1), false);
940 >            } else {
941 >                bashSubMap(tm.descendingMap(), min, midPoint - (incl ? 0 : 1),
942 >                           true);
943 >            }
944 >        }
945 >
946 >        // subMap - pick direction and endpoint inclusion randomly
947 >        int rangeSize = max - min + 1;
948 >        int[] endpoints = new int[2];
949 >        endpoints[0] = min + rnd.nextInt(rangeSize);
950 >        endpoints[1] = min + rnd.nextInt(rangeSize);
951 >        Arrays.sort(endpoints);
952 >        boolean lowIncl = rnd.nextBoolean();
953 >        boolean highIncl = rnd.nextBoolean();
954 >        if (ascending) {
955 >            NavigableMap<Integer,Integer> sm = map.subMap(
956 >                endpoints[0], lowIncl, endpoints[1], highIncl);
957 >            if (rnd.nextBoolean())
958 >                bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1),
959 >                           endpoints[1] - (highIncl ? 0 : 1), true);
960 >            else
961 >                bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1),
962 >                           endpoints[1] - (highIncl ? 0 : 1), false);
963 >        } else {
964 >            NavigableMap<Integer,Integer> sm = map.subMap(
965 >                endpoints[1], highIncl, endpoints[0], lowIncl);
966 >            if (rnd.nextBoolean())
967 >                bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1),
968 >                           endpoints[1] - (highIncl ? 0 : 1), false);
969 >            else
970 >                bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1),
971 >                           endpoints[1] - (highIncl ? 0 : 1), true);
972 >        }
973 >    }
974 >
975 >    /**
976 >     * min and max are both inclusive.  If max < min, interval is empty.
977 >     */
978 >    void check(NavigableMap<Integer, Integer> map,
979 >                      final int min, final int max, final boolean ascending) {
980 >       class ReferenceSet {
981 >            int lower(int key) {
982 >                return ascending ? lowerAscending(key) : higherAscending(key);
983 >            }
984 >            int floor(int key) {
985 >                return ascending ? floorAscending(key) : ceilingAscending(key);
986 >            }
987 >            int ceiling(int key) {
988 >                return ascending ? ceilingAscending(key) : floorAscending(key);
989 >            }
990 >            int higher(int key) {
991 >                return ascending ? higherAscending(key) : lowerAscending(key);
992 >            }
993 >            int first() {
994 >                return ascending ? firstAscending() : lastAscending();
995 >            }
996 >            int last() {
997 >                return ascending ? lastAscending() : firstAscending();
998 >            }
999 >            int lowerAscending(int key) {
1000 >                return floorAscending(key - 1);
1001 >            }
1002 >            int floorAscending(int key) {
1003 >                if (key < min)
1004 >                    return -1;
1005 >                else if (key > max)
1006 >                    key = max;
1007 >
1008 >                // BitSet should support this! Test would run much faster
1009 >                while (key >= min) {
1010 >                    if (bs.get(key))
1011 >                        return(key);
1012 >                    key--;
1013 >                }
1014 >                return -1;
1015 >            }
1016 >            int ceilingAscending(int key) {
1017 >                if (key < min)
1018 >                    key = min;
1019 >                else if (key > max)
1020 >                    return -1;
1021 >                int result = bs.nextSetBit(key);
1022 >                return result > max ? -1 : result;
1023 >            }
1024 >            int higherAscending(int key) {
1025 >                return ceilingAscending(key + 1);
1026 >            }
1027 >            private int firstAscending() {
1028 >                int result = ceilingAscending(min);
1029 >                return result > max ? -1 : result;
1030 >            }
1031 >            private int lastAscending() {
1032 >                int result = floorAscending(max);
1033 >                return result < min ? -1 : result;
1034 >            }
1035 >        }
1036 >        ReferenceSet rs = new ReferenceSet();
1037 >
1038 >        // Test contents using containsKey
1039 >        int size = 0;
1040 >        for (int i = min; i <= max; i++) {
1041 >            boolean bsContainsI = bs.get(i);
1042 >            assertEquals(bsContainsI, map.containsKey(i));
1043 >            if (bsContainsI)
1044 >                size++;
1045 >        }
1046 >        assertEquals(map.size(), size);
1047 >
1048 >        // Test contents using contains keySet iterator
1049 >        int size2 = 0;
1050 >        int previousKey = -1;
1051 >        for (int key : map.keySet()) {
1052 >            assertTrue(bs.get(key));
1053 >            size2++;
1054 >            assertTrue(previousKey < 0 ||
1055 >                (ascending ? key - previousKey > 0 : key - previousKey < 0));
1056 >            previousKey = key;
1057 >        }
1058 >        assertEquals(size2, size);
1059 >
1060 >        // Test navigation ops
1061 >        for (int key = min - 1; key <= max + 1; key++) {
1062 >            assertEq(map.lowerKey(key), rs.lower(key));
1063 >            assertEq(map.floorKey(key), rs.floor(key));
1064 >            assertEq(map.higherKey(key), rs.higher(key));
1065 >            assertEq(map.ceilingKey(key), rs.ceiling(key));
1066 >        }
1067 >
1068 >        // Test extrema
1069 >        if (map.size() != 0) {
1070 >            assertEq(map.firstKey(), rs.first());
1071 >            assertEq(map.lastKey(), rs.last());
1072 >        } else {
1073 >            assertEq(rs.first(), -1);
1074 >            assertEq(rs.last(),  -1);
1075 >            try {
1076 >                map.firstKey();
1077 >                fail();
1078 >            } catch (NoSuchElementException e) {
1079 >                // expected
1080 >            }
1081 >            try {
1082 >                map.lastKey();
1083 >                fail();
1084 >            } catch (NoSuchElementException e) {
1085 >                // expected
1086 >            }
1087 >        }
1088 >    }
1089 >
1090 >    static void assertEq(Integer i, int j) {
1091 >        if (i == null)
1092 >            assertEquals(j, -1);
1093 >        else
1094 >            assertEquals((int) i, j);
1095 >    }
1096 >
1097 >    static boolean eq(Integer i, int j) {
1098 >        return i == null ? j == -1 : i == j;
1099 >    }
1100 >
1101   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines