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 |
|
/** |
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(); |
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 |
|
} |
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 |
|
|
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 |
|
|
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 |
|
} |
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 |
|
|
108 |
|
*/ |
109 |
|
public void testFirstKey() { |
110 |
|
TreeMap map = map5(); |
111 |
< |
assertEquals(one, map.firstKey()); |
111 |
> |
assertEquals(one, map.firstKey()); |
112 |
|
} |
113 |
|
|
114 |
|
/** |
116 |
|
*/ |
117 |
|
public void testLastKey() { |
118 |
|
TreeMap map = map5(); |
119 |
< |
assertEquals(five, map.lastKey()); |
119 |
> |
assertEquals(five, map.lastKey()); |
120 |
|
} |
121 |
|
|
122 |
|
|
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 |
|
} |
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))); |
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 |
|
/** |
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 |
|
/** |
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 |
|
/** |
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 |
|
/** |
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")) || |
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")) || |
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) { |
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) { |
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 |
|
/** |
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 |
|
/** |
516 |
|
try { |
517 |
|
e.setValue("A"); |
518 |
|
shouldThrow(); |
519 |
< |
} catch (Exception ok) { |
476 |
< |
} |
519 |
> |
} catch (UnsupportedOperationException success) {} |
520 |
|
e = map.pollFirstEntry(); |
521 |
|
assertNull(e); |
522 |
|
} |
543 |
|
try { |
544 |
|
e.setValue("E"); |
545 |
|
shouldThrow(); |
546 |
< |
} catch (Exception ok) { |
504 |
< |
} |
546 |
> |
} catch (UnsupportedOperationException success) {} |
547 |
|
e = map.pollLastEntry(); |
548 |
|
assertNull(e); |
549 |
|
} |
554 |
|
public void testSize() { |
555 |
|
TreeMap map = map5(); |
556 |
|
TreeMap empty = new TreeMap(); |
557 |
< |
assertEquals(0, empty.size()); |
558 |
< |
assertEquals(5, map.size()); |
557 |
> |
assertEquals(0, empty.size()); |
558 |
> |
assertEquals(5, map.size()); |
559 |
|
} |
560 |
|
|
561 |
|
/** |
567 |
|
for (int i = 1; i <= 5; ++i) { |
568 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
569 |
|
} |
570 |
< |
} |
570 |
> |
} |
571 |
|
|
572 |
|
// Exception tests |
573 |
|
|
579 |
|
TreeMap c = map5(); |
580 |
|
c.get(null); |
581 |
|
shouldThrow(); |
582 |
< |
} catch(NullPointerException e){} |
582 |
> |
} catch (NullPointerException success) {} |
583 |
|
} |
584 |
|
|
585 |
|
/** |
590 |
|
TreeMap c = map5(); |
591 |
|
c.containsKey(null); |
592 |
|
shouldThrow(); |
593 |
< |
} catch(NullPointerException e){} |
593 |
> |
} catch (NullPointerException success) {} |
594 |
|
} |
595 |
|
|
596 |
|
/** |
602 |
|
c.put("sadsdf", "asdads"); |
603 |
|
c.remove(null); |
604 |
|
shouldThrow(); |
605 |
< |
} catch(NullPointerException e){} |
605 |
> |
} catch (NullPointerException success) {} |
606 |
|
} |
607 |
|
|
608 |
|
/** |
609 |
|
* A deserialized map equals original |
610 |
|
*/ |
611 |
< |
public void testSerialization() { |
611 |
> |
public void testSerialization() throws Exception { |
612 |
|
TreeMap q = map5(); |
613 |
|
|
614 |
< |
try { |
615 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
616 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
617 |
< |
out.writeObject(q); |
618 |
< |
out.close(); |
619 |
< |
|
620 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
621 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
622 |
< |
TreeMap r = (TreeMap)in.readObject(); |
623 |
< |
assertEquals(q.size(), r.size()); |
624 |
< |
assertTrue(q.equals(r)); |
583 |
< |
assertTrue(r.equals(q)); |
584 |
< |
} catch(Exception e){ |
585 |
< |
e.printStackTrace(); |
586 |
< |
unexpectedException(); |
587 |
< |
} |
614 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
615 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
616 |
> |
out.writeObject(q); |
617 |
> |
out.close(); |
618 |
> |
|
619 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
620 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
621 |
> |
TreeMap r = (TreeMap)in.readObject(); |
622 |
> |
assertEquals(q.size(), r.size()); |
623 |
> |
assertTrue(q.equals(r)); |
624 |
> |
assertTrue(r.equals(q)); |
625 |
|
} |
626 |
|
|
627 |
|
/** |
629 |
|
*/ |
630 |
|
public void testSubMapContents() { |
631 |
|
TreeMap map = map5(); |
632 |
< |
NavigableMap sm = map.subMap(two, four); |
632 |
> |
NavigableMap sm = map.subMap(two, true, four, false); |
633 |
|
assertEquals(two, sm.firstKey()); |
634 |
|
assertEquals(three, sm.lastKey()); |
635 |
|
assertEquals(2, sm.size()); |
651 |
|
k = (Integer)(r.next()); |
652 |
|
assertEquals(two, k); |
653 |
|
assertFalse(r.hasNext()); |
654 |
< |
|
654 |
> |
|
655 |
|
Iterator j = sm.keySet().iterator(); |
656 |
|
j.next(); |
657 |
|
j.remove(); |
660 |
|
assertEquals(1, sm.size()); |
661 |
|
assertEquals(three, sm.firstKey()); |
662 |
|
assertEquals(three, sm.lastKey()); |
663 |
< |
assertTrue(sm.remove(three) != null); |
663 |
> |
assertEquals("C", sm.remove(three)); |
664 |
|
assertTrue(sm.isEmpty()); |
665 |
|
assertEquals(3, map.size()); |
666 |
|
} |
667 |
|
|
668 |
|
public void testSubMapContents2() { |
669 |
|
TreeMap map = map5(); |
670 |
< |
NavigableMap sm = map.subMap(two, three); |
670 |
> |
NavigableMap sm = map.subMap(two, true, three, false); |
671 |
|
assertEquals(1, sm.size()); |
672 |
|
assertEquals(two, sm.firstKey()); |
673 |
|
assertEquals(two, sm.lastKey()); |
685 |
|
k = (Integer)(r.next()); |
686 |
|
assertEquals(two, k); |
687 |
|
assertFalse(r.hasNext()); |
688 |
< |
|
688 |
> |
|
689 |
|
Iterator j = sm.keySet().iterator(); |
690 |
|
j.next(); |
691 |
|
j.remove(); |
693 |
|
assertEquals(4, map.size()); |
694 |
|
assertEquals(0, sm.size()); |
695 |
|
assertTrue(sm.isEmpty()); |
696 |
< |
assertTrue(sm.remove(three) == null); |
696 |
> |
assertSame(sm.remove(three), null); |
697 |
|
assertEquals(4, map.size()); |
698 |
|
} |
699 |
|
|
702 |
|
*/ |
703 |
|
public void testHeadMapContents() { |
704 |
|
TreeMap map = map5(); |
705 |
< |
NavigableMap sm = map.headMap(four); |
705 |
> |
NavigableMap sm = map.headMap(four, false); |
706 |
|
assertTrue(sm.containsKey(one)); |
707 |
|
assertTrue(sm.containsKey(two)); |
708 |
|
assertTrue(sm.containsKey(three)); |
728 |
|
*/ |
729 |
|
public void testTailMapContents() { |
730 |
|
TreeMap map = map5(); |
731 |
< |
NavigableMap sm = map.tailMap(two); |
731 |
> |
NavigableMap sm = map.tailMap(two, true); |
732 |
|
assertFalse(sm.containsKey(one)); |
733 |
|
assertTrue(sm.containsKey(two)); |
734 |
|
assertTrue(sm.containsKey(three)); |
772 |
|
assertEquals("E", e.getValue()); |
773 |
|
assertFalse(i.hasNext()); |
774 |
|
|
775 |
< |
NavigableMap ssm = sm.tailMap(four); |
775 |
> |
NavigableMap ssm = sm.tailMap(four, true); |
776 |
|
assertEquals(four, ssm.firstKey()); |
777 |
|
assertEquals(five, ssm.lastKey()); |
778 |
< |
assertTrue(ssm.remove(four) != null); |
778 |
> |
assertEquals("D", ssm.remove(four)); |
779 |
|
assertEquals(1, ssm.size()); |
780 |
|
assertEquals(3, sm.size()); |
781 |
|
assertEquals(4, map.size()); |
782 |
|
} |
783 |
< |
|
783 |
> |
|
784 |
> |
Random rnd = new Random(666); |
785 |
> |
BitSet bs; |
786 |
> |
|
787 |
> |
/** |
788 |
> |
* Submaps of submaps subdivide correctly |
789 |
> |
*/ |
790 |
> |
public void testRecursiveSubMaps() throws Exception { |
791 |
> |
int mapSize = 1000; |
792 |
> |
Class cl = TreeMap.class; |
793 |
> |
NavigableMap<Integer, Integer> map = newMap(cl); |
794 |
> |
bs = new BitSet(mapSize); |
795 |
> |
|
796 |
> |
populate(map, mapSize); |
797 |
> |
check(map, 0, mapSize - 1, true); |
798 |
> |
check(map.descendingMap(), 0, mapSize - 1, false); |
799 |
> |
|
800 |
> |
mutateMap(map, 0, mapSize - 1); |
801 |
> |
check(map, 0, mapSize - 1, true); |
802 |
> |
check(map.descendingMap(), 0, mapSize - 1, false); |
803 |
> |
|
804 |
> |
bashSubMap(map.subMap(0, true, mapSize, false), |
805 |
> |
0, mapSize - 1, true); |
806 |
> |
} |
807 |
> |
|
808 |
> |
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception { |
809 |
> |
NavigableMap<Integer, Integer> result |
810 |
> |
= (NavigableMap<Integer, Integer>) cl.newInstance(); |
811 |
> |
assertEquals(result.size(), 0); |
812 |
> |
assertFalse(result.keySet().iterator().hasNext()); |
813 |
> |
return result; |
814 |
> |
} |
815 |
> |
|
816 |
> |
void populate(NavigableMap<Integer, Integer> map, int limit) { |
817 |
> |
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
818 |
> |
int key = rnd.nextInt(limit); |
819 |
> |
put(map, key); |
820 |
> |
} |
821 |
> |
} |
822 |
> |
|
823 |
> |
void mutateMap(NavigableMap<Integer, Integer> map, int min, int max) { |
824 |
> |
int size = map.size(); |
825 |
> |
int rangeSize = max - min + 1; |
826 |
> |
|
827 |
> |
// Remove a bunch of entries directly |
828 |
> |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
829 |
> |
remove(map, min - 5 + rnd.nextInt(rangeSize + 10)); |
830 |
> |
} |
831 |
> |
|
832 |
> |
// Remove a bunch of entries with iterator |
833 |
> |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
834 |
> |
if (rnd.nextBoolean()) { |
835 |
> |
bs.clear(it.next()); |
836 |
> |
it.remove(); |
837 |
> |
} |
838 |
> |
} |
839 |
> |
|
840 |
> |
// Add entries till we're back to original size |
841 |
> |
while (map.size() < size) { |
842 |
> |
int key = min + rnd.nextInt(rangeSize); |
843 |
> |
assertTrue(key >= min && key<= max); |
844 |
> |
put(map, key); |
845 |
> |
} |
846 |
> |
} |
847 |
> |
|
848 |
> |
void mutateSubMap(NavigableMap<Integer, Integer> map, int min, int max) { |
849 |
> |
int size = map.size(); |
850 |
> |
int rangeSize = max - min + 1; |
851 |
> |
|
852 |
> |
// Remove a bunch of entries directly |
853 |
> |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
854 |
> |
remove(map, min - 5 + rnd.nextInt(rangeSize + 10)); |
855 |
> |
} |
856 |
> |
|
857 |
> |
// Remove a bunch of entries with iterator |
858 |
> |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
859 |
> |
if (rnd.nextBoolean()) { |
860 |
> |
bs.clear(it.next()); |
861 |
> |
it.remove(); |
862 |
> |
} |
863 |
> |
} |
864 |
> |
|
865 |
> |
// Add entries till we're back to original size |
866 |
> |
while (map.size() < size) { |
867 |
> |
int key = min - 5 + rnd.nextInt(rangeSize + 10); |
868 |
> |
if (key >= min && key<= max) { |
869 |
> |
put(map, key); |
870 |
> |
} else { |
871 |
> |
try { |
872 |
> |
map.put(key, 2 * key); |
873 |
> |
shouldThrow(); |
874 |
> |
} catch (IllegalArgumentException success) {} |
875 |
> |
} |
876 |
> |
} |
877 |
> |
} |
878 |
> |
|
879 |
> |
void put(NavigableMap<Integer, Integer> map, int key) { |
880 |
> |
if (map.put(key, 2 * key) == null) |
881 |
> |
bs.set(key); |
882 |
> |
} |
883 |
> |
|
884 |
> |
void remove(NavigableMap<Integer, Integer> map, int key) { |
885 |
> |
if (map.remove(key) != null) |
886 |
> |
bs.clear(key); |
887 |
> |
} |
888 |
> |
|
889 |
> |
void bashSubMap(NavigableMap<Integer, Integer> map, |
890 |
> |
int min, int max, boolean ascending) { |
891 |
> |
check(map, min, max, ascending); |
892 |
> |
check(map.descendingMap(), min, max, !ascending); |
893 |
> |
|
894 |
> |
mutateSubMap(map, min, max); |
895 |
> |
check(map, min, max, ascending); |
896 |
> |
check(map.descendingMap(), min, max, !ascending); |
897 |
> |
|
898 |
> |
// Recurse |
899 |
> |
if (max - min < 2) |
900 |
> |
return; |
901 |
> |
int midPoint = (min + max) / 2; |
902 |
> |
|
903 |
> |
// headMap - pick direction and endpoint inclusion randomly |
904 |
> |
boolean incl = rnd.nextBoolean(); |
905 |
> |
NavigableMap<Integer,Integer> hm = map.headMap(midPoint, incl); |
906 |
> |
if (ascending) { |
907 |
> |
if (rnd.nextBoolean()) |
908 |
> |
bashSubMap(hm, min, midPoint - (incl ? 0 : 1), true); |
909 |
> |
else |
910 |
> |
bashSubMap(hm.descendingMap(), min, midPoint - (incl ? 0 : 1), |
911 |
> |
false); |
912 |
> |
} else { |
913 |
> |
if (rnd.nextBoolean()) |
914 |
> |
bashSubMap(hm, midPoint + (incl ? 0 : 1), max, false); |
915 |
> |
else |
916 |
> |
bashSubMap(hm.descendingMap(), midPoint + (incl ? 0 : 1), max, |
917 |
> |
true); |
918 |
> |
} |
919 |
> |
|
920 |
> |
// tailMap - pick direction and endpoint inclusion randomly |
921 |
> |
incl = rnd.nextBoolean(); |
922 |
> |
NavigableMap<Integer,Integer> tm = map.tailMap(midPoint,incl); |
923 |
> |
if (ascending) { |
924 |
> |
if (rnd.nextBoolean()) |
925 |
> |
bashSubMap(tm, midPoint + (incl ? 0 : 1), max, true); |
926 |
> |
else |
927 |
> |
bashSubMap(tm.descendingMap(), midPoint + (incl ? 0 : 1), max, |
928 |
> |
false); |
929 |
> |
} else { |
930 |
> |
if (rnd.nextBoolean()) { |
931 |
> |
bashSubMap(tm, min, midPoint - (incl ? 0 : 1), false); |
932 |
> |
} else { |
933 |
> |
bashSubMap(tm.descendingMap(), min, midPoint - (incl ? 0 : 1), |
934 |
> |
true); |
935 |
> |
} |
936 |
> |
} |
937 |
> |
|
938 |
> |
// subMap - pick direction and endpoint inclusion randomly |
939 |
> |
int rangeSize = max - min + 1; |
940 |
> |
int[] endpoints = new int[2]; |
941 |
> |
endpoints[0] = min + rnd.nextInt(rangeSize); |
942 |
> |
endpoints[1] = min + rnd.nextInt(rangeSize); |
943 |
> |
Arrays.sort(endpoints); |
944 |
> |
boolean lowIncl = rnd.nextBoolean(); |
945 |
> |
boolean highIncl = rnd.nextBoolean(); |
946 |
> |
if (ascending) { |
947 |
> |
NavigableMap<Integer,Integer> sm = map.subMap( |
948 |
> |
endpoints[0], lowIncl, endpoints[1], highIncl); |
949 |
> |
if (rnd.nextBoolean()) |
950 |
> |
bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), |
951 |
> |
endpoints[1] - (highIncl ? 0 : 1), true); |
952 |
> |
else |
953 |
> |
bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), |
954 |
> |
endpoints[1] - (highIncl ? 0 : 1), false); |
955 |
> |
} else { |
956 |
> |
NavigableMap<Integer,Integer> sm = map.subMap( |
957 |
> |
endpoints[1], highIncl, endpoints[0], lowIncl); |
958 |
> |
if (rnd.nextBoolean()) |
959 |
> |
bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), |
960 |
> |
endpoints[1] - (highIncl ? 0 : 1), false); |
961 |
> |
else |
962 |
> |
bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), |
963 |
> |
endpoints[1] - (highIncl ? 0 : 1), true); |
964 |
> |
} |
965 |
> |
} |
966 |
> |
|
967 |
> |
/** |
968 |
> |
* min and max are both inclusive. If max < min, interval is empty. |
969 |
> |
*/ |
970 |
> |
void check(NavigableMap<Integer, Integer> map, |
971 |
> |
final int min, final int max, final boolean ascending) { |
972 |
> |
class ReferenceSet { |
973 |
> |
int lower(int key) { |
974 |
> |
return ascending ? lowerAscending(key) : higherAscending(key); |
975 |
> |
} |
976 |
> |
int floor(int key) { |
977 |
> |
return ascending ? floorAscending(key) : ceilingAscending(key); |
978 |
> |
} |
979 |
> |
int ceiling(int key) { |
980 |
> |
return ascending ? ceilingAscending(key) : floorAscending(key); |
981 |
> |
} |
982 |
> |
int higher(int key) { |
983 |
> |
return ascending ? higherAscending(key) : lowerAscending(key); |
984 |
> |
} |
985 |
> |
int first() { |
986 |
> |
return ascending ? firstAscending() : lastAscending(); |
987 |
> |
} |
988 |
> |
int last() { |
989 |
> |
return ascending ? lastAscending() : firstAscending(); |
990 |
> |
} |
991 |
> |
int lowerAscending(int key) { |
992 |
> |
return floorAscending(key - 1); |
993 |
> |
} |
994 |
> |
int floorAscending(int key) { |
995 |
> |
if (key < min) |
996 |
> |
return -1; |
997 |
> |
else if (key > max) |
998 |
> |
key = max; |
999 |
> |
|
1000 |
> |
// BitSet should support this! Test would run much faster |
1001 |
> |
while (key >= min) { |
1002 |
> |
if (bs.get(key)) |
1003 |
> |
return(key); |
1004 |
> |
key--; |
1005 |
> |
} |
1006 |
> |
return -1; |
1007 |
> |
} |
1008 |
> |
int ceilingAscending(int key) { |
1009 |
> |
if (key < min) |
1010 |
> |
key = min; |
1011 |
> |
else if (key > max) |
1012 |
> |
return -1; |
1013 |
> |
int result = bs.nextSetBit(key); |
1014 |
> |
return result > max ? -1 : result; |
1015 |
> |
} |
1016 |
> |
int higherAscending(int key) { |
1017 |
> |
return ceilingAscending(key + 1); |
1018 |
> |
} |
1019 |
> |
private int firstAscending() { |
1020 |
> |
int result = ceilingAscending(min); |
1021 |
> |
return result > max ? -1 : result; |
1022 |
> |
} |
1023 |
> |
private int lastAscending() { |
1024 |
> |
int result = floorAscending(max); |
1025 |
> |
return result < min ? -1 : result; |
1026 |
> |
} |
1027 |
> |
} |
1028 |
> |
ReferenceSet rs = new ReferenceSet(); |
1029 |
> |
|
1030 |
> |
// Test contents using containsKey |
1031 |
> |
int size = 0; |
1032 |
> |
for (int i = min; i <= max; i++) { |
1033 |
> |
boolean bsContainsI = bs.get(i); |
1034 |
> |
assertEquals(bsContainsI, map.containsKey(i)); |
1035 |
> |
if (bsContainsI) |
1036 |
> |
size++; |
1037 |
> |
} |
1038 |
> |
assertEquals(map.size(), size); |
1039 |
> |
|
1040 |
> |
// Test contents using contains keySet iterator |
1041 |
> |
int size2 = 0; |
1042 |
> |
int previousKey = -1; |
1043 |
> |
for (int key : map.keySet()) { |
1044 |
> |
assertTrue(bs.get(key)); |
1045 |
> |
size2++; |
1046 |
> |
assertTrue(previousKey < 0 || |
1047 |
> |
(ascending ? key - previousKey > 0 : key - previousKey < 0)); |
1048 |
> |
previousKey = key; |
1049 |
> |
} |
1050 |
> |
assertEquals(size2, size); |
1051 |
> |
|
1052 |
> |
// Test navigation ops |
1053 |
> |
for (int key = min - 1; key <= max + 1; key++) { |
1054 |
> |
assertEq(map.lowerKey(key), rs.lower(key)); |
1055 |
> |
assertEq(map.floorKey(key), rs.floor(key)); |
1056 |
> |
assertEq(map.higherKey(key), rs.higher(key)); |
1057 |
> |
assertEq(map.ceilingKey(key), rs.ceiling(key)); |
1058 |
> |
} |
1059 |
> |
|
1060 |
> |
// Test extrema |
1061 |
> |
if (map.size() != 0) { |
1062 |
> |
assertEq(map.firstKey(), rs.first()); |
1063 |
> |
assertEq(map.lastKey(), rs.last()); |
1064 |
> |
} else { |
1065 |
> |
assertEq(rs.first(), -1); |
1066 |
> |
assertEq(rs.last(), -1); |
1067 |
> |
try { |
1068 |
> |
map.firstKey(); |
1069 |
> |
shouldThrow(); |
1070 |
> |
} catch (NoSuchElementException success) {} |
1071 |
> |
try { |
1072 |
> |
map.lastKey(); |
1073 |
> |
shouldThrow(); |
1074 |
> |
} catch (NoSuchElementException success) {} |
1075 |
> |
} |
1076 |
> |
} |
1077 |
> |
|
1078 |
> |
static void assertEq(Integer i, int j) { |
1079 |
> |
if (i == null) |
1080 |
> |
assertEquals(j, -1); |
1081 |
> |
else |
1082 |
> |
assertEquals((int) i, j); |
1083 |
> |
} |
1084 |
> |
|
1085 |
> |
static boolean eq(Integer i, int j) { |
1086 |
> |
return i == null ? j == -1 : i == j; |
1087 |
> |
} |
1088 |
> |
|
1089 |
|
} |