1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
10 |
< |
import java.io.*; |
7 |
> |
import java.util.Arrays; |
8 |
> |
import java.util.BitSet; |
9 |
> |
import java.util.Collection; |
10 |
> |
import java.util.Iterator; |
11 |
> |
import java.util.Map; |
12 |
> |
import java.util.NavigableMap; |
13 |
> |
import java.util.NavigableSet; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.Random; |
16 |
> |
import java.util.Set; |
17 |
> |
import java.util.TreeMap; |
18 |
> |
|
19 |
> |
import junit.framework.Test; |
20 |
> |
import junit.framework.TestSuite; |
21 |
|
|
22 |
|
public class TreeMapTest extends JSR166TestCase { |
23 |
|
public static void main(String[] args) { |
24 |
< |
junit.textui.TestRunner.run (suite()); |
24 |
> |
junit.textui.TestRunner.run(suite()); |
25 |
|
} |
26 |
|
public static Test suite() { |
27 |
< |
return new TestSuite(TreeMapTest.class); |
27 |
> |
return new TestSuite(TreeMapTest.class); |
28 |
|
} |
29 |
|
|
30 |
|
/** |
31 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
31 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
32 |
|
*/ |
33 |
|
private static TreeMap map5() { |
34 |
< |
TreeMap map = new TreeMap(); |
34 |
> |
TreeMap map = new TreeMap(); |
35 |
|
assertTrue(map.isEmpty()); |
36 |
< |
map.put(one, "A"); |
37 |
< |
map.put(five, "E"); |
38 |
< |
map.put(three, "C"); |
39 |
< |
map.put(two, "B"); |
40 |
< |
map.put(four, "D"); |
36 |
> |
map.put(one, "A"); |
37 |
> |
map.put(five, "E"); |
38 |
> |
map.put(three, "C"); |
39 |
> |
map.put(two, "B"); |
40 |
> |
map.put(four, "D"); |
41 |
|
assertFalse(map.isEmpty()); |
42 |
|
assertEquals(5, map.size()); |
43 |
< |
return map; |
43 |
> |
return map; |
44 |
|
} |
45 |
|
|
46 |
|
/** |
47 |
< |
* clear removes all pairs |
47 |
> |
* clear removes all pairs |
48 |
|
*/ |
49 |
|
public void testClear() { |
50 |
|
TreeMap map = map5(); |
51 |
< |
map.clear(); |
52 |
< |
assertEquals(map.size(), 0); |
51 |
> |
map.clear(); |
52 |
> |
assertEquals(0, map.size()); |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
< |
* |
56 |
> |
* copy constructor creates map equal to source map |
57 |
|
*/ |
58 |
|
public void testConstructFromSorted() { |
59 |
|
TreeMap map = map5(); |
62 |
|
} |
63 |
|
|
64 |
|
/** |
65 |
< |
* Maps with same contents are equal |
65 |
> |
* Maps with same contents are equal |
66 |
|
*/ |
67 |
|
public void testEquals() { |
68 |
|
TreeMap map1 = map5(); |
69 |
|
TreeMap map2 = map5(); |
70 |
|
assertEquals(map1, map2); |
71 |
|
assertEquals(map2, map1); |
72 |
< |
map1.clear(); |
72 |
> |
map1.clear(); |
73 |
|
assertFalse(map1.equals(map2)); |
74 |
|
assertFalse(map2.equals(map1)); |
75 |
|
} |
76 |
|
|
77 |
|
/** |
78 |
< |
* containsKey returns true for contained key |
78 |
> |
* containsKey returns true for contained key |
79 |
|
*/ |
80 |
|
public void testContainsKey() { |
81 |
|
TreeMap map = map5(); |
82 |
< |
assertTrue(map.containsKey(one)); |
82 |
> |
assertTrue(map.containsKey(one)); |
83 |
|
assertFalse(map.containsKey(zero)); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
< |
* containsValue returns true for held values |
87 |
> |
* containsValue returns true for held values |
88 |
|
*/ |
89 |
|
public void testContainsValue() { |
90 |
|
TreeMap map = map5(); |
91 |
< |
assertTrue(map.containsValue("A")); |
91 |
> |
assertTrue(map.containsValue("A")); |
92 |
|
assertFalse(map.containsValue("Z")); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
< |
* get returns the correct element at the given key, |
97 |
< |
* or null if not present |
96 |
> |
* get returns the correct element at the given key, |
97 |
> |
* or null if not present |
98 |
|
*/ |
99 |
|
public void testGet() { |
100 |
|
TreeMap map = map5(); |
101 |
< |
assertEquals("A", (String)map.get(one)); |
101 |
> |
assertEquals("A", (String)map.get(one)); |
102 |
|
TreeMap empty = new TreeMap(); |
103 |
|
assertNull(empty.get(one)); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
107 |
< |
* isEmpty is true of empty map and false for non-empty |
107 |
> |
* isEmpty is true of empty map and false for non-empty |
108 |
|
*/ |
109 |
|
public void testIsEmpty() { |
110 |
|
TreeMap empty = new TreeMap(); |
111 |
|
TreeMap map = map5(); |
112 |
< |
assertTrue(empty.isEmpty()); |
112 |
> |
assertTrue(empty.isEmpty()); |
113 |
|
assertFalse(map.isEmpty()); |
114 |
|
} |
115 |
|
|
116 |
|
/** |
117 |
< |
* firstKey returns first key |
117 |
> |
* firstKey returns first key |
118 |
|
*/ |
119 |
|
public void testFirstKey() { |
120 |
|
TreeMap map = map5(); |
121 |
< |
assertEquals(one, map.firstKey()); |
121 |
> |
assertEquals(one, map.firstKey()); |
122 |
|
} |
123 |
|
|
124 |
|
/** |
125 |
< |
* lastKey returns last key |
125 |
> |
* lastKey returns last key |
126 |
|
*/ |
127 |
|
public void testLastKey() { |
128 |
|
TreeMap map = map5(); |
129 |
< |
assertEquals(five, map.lastKey()); |
129 |
> |
assertEquals(five, map.lastKey()); |
130 |
|
} |
131 |
|
|
122 |
– |
|
132 |
|
/** |
133 |
< |
* keySet.toArray returns contains all keys |
133 |
> |
* keySet.toArray returns contains all keys |
134 |
|
*/ |
135 |
|
public void testKeySetToArray() { |
136 |
|
TreeMap map = map5(); |
137 |
< |
Set s = map.keySet(); |
137 |
> |
Set s = map.keySet(); |
138 |
|
Object[] ar = s.toArray(); |
139 |
|
assertTrue(s.containsAll(Arrays.asList(ar))); |
140 |
< |
assertEquals(5, ar.length); |
140 |
> |
assertEquals(5, ar.length); |
141 |
|
ar[0] = m10; |
142 |
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
< |
* descendingkeySet.toArray returns contains all keys |
146 |
> |
* descendingkeySet.toArray returns contains all keys |
147 |
|
*/ |
148 |
|
public void testDescendingKeySetToArray() { |
149 |
|
TreeMap map = map5(); |
150 |
< |
Set s = map.descendingKeySet(); |
150 |
> |
Set s = map.descendingKeySet(); |
151 |
|
Object[] ar = s.toArray(); |
152 |
< |
assertEquals(5, ar.length); |
152 |
> |
assertEquals(5, ar.length); |
153 |
|
assertTrue(s.containsAll(Arrays.asList(ar))); |
154 |
|
ar[0] = m10; |
155 |
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
156 |
|
} |
157 |
|
|
158 |
|
/** |
159 |
< |
* keySet returns a Set containing all the keys |
159 |
> |
* keySet returns a Set containing all the keys |
160 |
|
*/ |
161 |
|
public void testKeySet() { |
162 |
|
TreeMap map = map5(); |
163 |
< |
Set s = map.keySet(); |
164 |
< |
assertEquals(5, s.size()); |
165 |
< |
assertTrue(s.contains(one)); |
166 |
< |
assertTrue(s.contains(two)); |
167 |
< |
assertTrue(s.contains(three)); |
168 |
< |
assertTrue(s.contains(four)); |
169 |
< |
assertTrue(s.contains(five)); |
163 |
> |
Set s = map.keySet(); |
164 |
> |
assertEquals(5, s.size()); |
165 |
> |
assertTrue(s.contains(one)); |
166 |
> |
assertTrue(s.contains(two)); |
167 |
> |
assertTrue(s.contains(three)); |
168 |
> |
assertTrue(s.contains(four)); |
169 |
> |
assertTrue(s.contains(five)); |
170 |
|
} |
171 |
|
|
172 |
|
/** |
173 |
< |
* keySet is ordered |
173 |
> |
* keySet is ordered |
174 |
|
*/ |
175 |
|
public void testKeySetOrder() { |
176 |
|
TreeMap map = map5(); |
177 |
< |
Set s = map.keySet(); |
177 |
> |
Set s = map.keySet(); |
178 |
|
Iterator i = s.iterator(); |
179 |
|
Integer last = (Integer)i.next(); |
180 |
|
assertEquals(last, one); |
185 |
|
last = k; |
186 |
|
++count; |
187 |
|
} |
188 |
< |
assertEquals(count ,5); |
188 |
> |
assertEquals(5, count); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
193 |
|
*/ |
194 |
|
public void testKeySetDescendingIteratorOrder() { |
195 |
|
TreeMap map = map5(); |
196 |
< |
NavigableSet s = map.navigableKeySet(); |
196 |
> |
NavigableSet s = map.navigableKeySet(); |
197 |
|
Iterator i = s.descendingIterator(); |
198 |
|
Integer last = (Integer)i.next(); |
199 |
|
assertEquals(last, five); |
204 |
|
last = k; |
205 |
|
++count; |
206 |
|
} |
207 |
< |
assertEquals(count ,5); |
207 |
> |
assertEquals(5, count); |
208 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
< |
* descendingKeySet is ordered |
211 |
> |
* descendingKeySet is ordered |
212 |
|
*/ |
213 |
|
public void testDescendingKeySetOrder() { |
214 |
|
TreeMap map = map5(); |
215 |
< |
Set s = map.descendingKeySet(); |
215 |
> |
Set s = map.descendingKeySet(); |
216 |
|
Iterator i = s.iterator(); |
217 |
|
Integer last = (Integer)i.next(); |
218 |
|
assertEquals(last, five); |
223 |
|
last = k; |
224 |
|
++count; |
225 |
|
} |
226 |
< |
assertEquals(count, 5); |
226 |
> |
assertEquals(5, count); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
< |
* descending iterator of descendingKeySet is ordered |
230 |
> |
* descending iterator of descendingKeySet is ordered |
231 |
|
*/ |
232 |
|
public void testDescendingKeySetDescendingIteratorOrder() { |
233 |
|
TreeMap map = map5(); |
234 |
< |
NavigableSet s = map.descendingKeySet(); |
234 |
> |
NavigableSet s = map.descendingKeySet(); |
235 |
|
Iterator i = s.descendingIterator(); |
236 |
|
Integer last = (Integer)i.next(); |
237 |
|
assertEquals(last, one); |
242 |
|
last = k; |
243 |
|
++count; |
244 |
|
} |
245 |
< |
assertEquals(count, 5); |
245 |
> |
assertEquals(5, count); |
246 |
|
} |
247 |
|
|
248 |
|
/** |
250 |
|
*/ |
251 |
|
public void testValues() { |
252 |
|
TreeMap map = map5(); |
253 |
< |
Collection s = map.values(); |
254 |
< |
assertEquals(5, s.size()); |
255 |
< |
assertTrue(s.contains("A")); |
256 |
< |
assertTrue(s.contains("B")); |
257 |
< |
assertTrue(s.contains("C")); |
258 |
< |
assertTrue(s.contains("D")); |
259 |
< |
assertTrue(s.contains("E")); |
253 |
> |
Collection s = map.values(); |
254 |
> |
assertEquals(5, s.size()); |
255 |
> |
assertTrue(s.contains("A")); |
256 |
> |
assertTrue(s.contains("B")); |
257 |
> |
assertTrue(s.contains("C")); |
258 |
> |
assertTrue(s.contains("D")); |
259 |
> |
assertTrue(s.contains("E")); |
260 |
|
} |
261 |
|
|
262 |
|
/** |
264 |
|
*/ |
265 |
|
public void testEntrySet() { |
266 |
|
TreeMap map = map5(); |
267 |
< |
Set s = map.entrySet(); |
268 |
< |
assertEquals(5, s.size()); |
267 |
> |
Set s = map.entrySet(); |
268 |
> |
assertEquals(5, s.size()); |
269 |
|
Iterator it = s.iterator(); |
270 |
|
while (it.hasNext()) { |
271 |
|
Map.Entry e = (Map.Entry) it.next(); |
283 |
|
*/ |
284 |
|
public void testDescendingEntrySet() { |
285 |
|
TreeMap map = map5(); |
286 |
< |
Set s = map.descendingMap().entrySet(); |
287 |
< |
assertEquals(5, s.size()); |
286 |
> |
Set s = map.descendingMap().entrySet(); |
287 |
> |
assertEquals(5, s.size()); |
288 |
|
Iterator it = s.iterator(); |
289 |
|
while (it.hasNext()) { |
290 |
|
Map.Entry e = (Map.Entry) it.next(); |
298 |
|
} |
299 |
|
|
300 |
|
/** |
301 |
< |
* entrySet.toArray contains all entries |
301 |
> |
* entrySet.toArray contains all entries |
302 |
|
*/ |
303 |
|
public void testEntrySetToArray() { |
304 |
|
TreeMap map = map5(); |
305 |
< |
Set s = map.entrySet(); |
305 |
> |
Set s = map.entrySet(); |
306 |
|
Object[] ar = s.toArray(); |
307 |
|
assertEquals(5, ar.length); |
308 |
|
for (int i = 0; i < 5; ++i) { |
312 |
|
} |
313 |
|
|
314 |
|
/** |
315 |
< |
* descendingEntrySet.toArray contains all entries |
315 |
> |
* descendingEntrySet.toArray contains all entries |
316 |
|
*/ |
317 |
|
public void testDescendingEntrySetToArray() { |
318 |
|
TreeMap map = map5(); |
319 |
< |
Set s = map.descendingMap().entrySet(); |
319 |
> |
Set s = map.descendingMap().entrySet(); |
320 |
|
Object[] ar = s.toArray(); |
321 |
|
assertEquals(5, ar.length); |
322 |
|
for (int i = 0; i < 5; ++i) { |
326 |
|
} |
327 |
|
|
328 |
|
/** |
329 |
< |
* putAll adds all key-value pairs from the given map |
329 |
> |
* putAll adds all key-value pairs from the given map |
330 |
|
*/ |
331 |
|
public void testPutAll() { |
332 |
|
TreeMap empty = new TreeMap(); |
333 |
|
TreeMap map = map5(); |
334 |
< |
empty.putAll(map); |
335 |
< |
assertEquals(5, empty.size()); |
336 |
< |
assertTrue(empty.containsKey(one)); |
337 |
< |
assertTrue(empty.containsKey(two)); |
338 |
< |
assertTrue(empty.containsKey(three)); |
339 |
< |
assertTrue(empty.containsKey(four)); |
340 |
< |
assertTrue(empty.containsKey(five)); |
334 |
> |
empty.putAll(map); |
335 |
> |
assertEquals(5, empty.size()); |
336 |
> |
assertTrue(empty.containsKey(one)); |
337 |
> |
assertTrue(empty.containsKey(two)); |
338 |
> |
assertTrue(empty.containsKey(three)); |
339 |
> |
assertTrue(empty.containsKey(four)); |
340 |
> |
assertTrue(empty.containsKey(five)); |
341 |
|
} |
342 |
|
|
343 |
|
/** |
344 |
< |
* remove removes the correct key-value pair from the map |
344 |
> |
* remove removes the correct key-value pair from the map |
345 |
|
*/ |
346 |
|
public void testRemove() { |
347 |
|
TreeMap map = map5(); |
348 |
< |
map.remove(five); |
349 |
< |
assertEquals(4, map.size()); |
350 |
< |
assertFalse(map.containsKey(five)); |
348 |
> |
map.remove(five); |
349 |
> |
assertEquals(4, map.size()); |
350 |
> |
assertFalse(map.containsKey(five)); |
351 |
|
} |
352 |
|
|
353 |
|
/** |
366 |
|
|
367 |
|
Map.Entry e4 = map.lowerEntry(zero); |
368 |
|
assertNull(e4); |
360 |
– |
|
369 |
|
} |
370 |
|
|
371 |
|
/** |
384 |
|
|
385 |
|
Map.Entry e4 = map.higherEntry(six); |
386 |
|
assertNull(e4); |
379 |
– |
|
387 |
|
} |
388 |
|
|
389 |
|
/** |
402 |
|
|
403 |
|
Map.Entry e4 = map.floorEntry(zero); |
404 |
|
assertNull(e4); |
398 |
– |
|
405 |
|
} |
406 |
|
|
407 |
|
/** |
420 |
|
|
421 |
|
Map.Entry e4 = map.ceilingEntry(six); |
422 |
|
assertNull(e4); |
417 |
– |
|
423 |
|
} |
424 |
|
|
420 |
– |
|
425 |
|
/** |
426 |
|
* lowerKey returns preceding element |
427 |
|
*/ |
438 |
|
|
439 |
|
Object e4 = q.lowerKey(zero); |
440 |
|
assertNull(e4); |
437 |
– |
|
441 |
|
} |
442 |
|
|
443 |
|
/** |
456 |
|
|
457 |
|
Object e4 = q.higherKey(six); |
458 |
|
assertNull(e4); |
456 |
– |
|
459 |
|
} |
460 |
|
|
461 |
|
/** |
474 |
|
|
475 |
|
Object e4 = q.floorKey(zero); |
476 |
|
assertNull(e4); |
475 |
– |
|
477 |
|
} |
478 |
|
|
479 |
|
/** |
492 |
|
|
493 |
|
Object e4 = q.ceilingKey(six); |
494 |
|
assertNull(e4); |
494 |
– |
|
495 |
|
} |
496 |
|
|
497 |
|
/** |
516 |
|
try { |
517 |
|
e.setValue("A"); |
518 |
|
shouldThrow(); |
519 |
< |
} catch (Exception ok) { |
520 |
< |
} |
519 |
> |
} catch (UnsupportedOperationException success) {} |
520 |
|
e = map.pollFirstEntry(); |
521 |
|
assertNull(e); |
522 |
|
} |
543 |
|
try { |
544 |
|
e.setValue("E"); |
545 |
|
shouldThrow(); |
546 |
< |
} catch (Exception ok) { |
548 |
< |
} |
546 |
> |
} catch (UnsupportedOperationException success) {} |
547 |
|
e = map.pollLastEntry(); |
548 |
|
assertNull(e); |
549 |
|
} |
550 |
|
|
551 |
|
/** |
552 |
< |
* size returns the correct values |
552 |
> |
* size returns the correct values |
553 |
|
*/ |
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 |
|
/** |
565 |
|
TreeMap map = map5(); |
566 |
|
String s = map.toString(); |
567 |
|
for (int i = 1; i <= 5; ++i) { |
568 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
568 |
> |
assertTrue(s.contains(String.valueOf(i))); |
569 |
|
} |
570 |
|
} |
571 |
|
|
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() { |
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 |
< |
|
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)); |
627 |
< |
assertTrue(r.equals(q)); |
628 |
< |
} catch(Exception e){ |
629 |
< |
e.printStackTrace(); |
630 |
< |
unexpectedException(); |
631 |
< |
} |
611 |
> |
public void testSerialization() throws Exception { |
612 |
> |
NavigableMap x = map5(); |
613 |
> |
NavigableMap y = serialClone(x); |
614 |
> |
|
615 |
> |
assertNotSame(x, y); |
616 |
> |
assertEquals(x.size(), y.size()); |
617 |
> |
assertEquals(x.toString(), y.toString()); |
618 |
> |
assertEquals(x, y); |
619 |
> |
assertEquals(y, x); |
620 |
|
} |
621 |
|
|
622 |
|
/** |
655 |
|
assertEquals(1, sm.size()); |
656 |
|
assertEquals(three, sm.firstKey()); |
657 |
|
assertEquals(three, sm.lastKey()); |
658 |
< |
assertTrue(sm.remove(three) != null); |
658 |
> |
assertEquals("C", sm.remove(three)); |
659 |
|
assertTrue(sm.isEmpty()); |
660 |
|
assertEquals(3, map.size()); |
661 |
|
} |
688 |
|
assertEquals(4, map.size()); |
689 |
|
assertEquals(0, sm.size()); |
690 |
|
assertTrue(sm.isEmpty()); |
691 |
< |
assertTrue(sm.remove(three) == null); |
691 |
> |
assertSame(sm.remove(three), null); |
692 |
|
assertEquals(4, map.size()); |
693 |
|
} |
694 |
|
|
770 |
|
NavigableMap ssm = sm.tailMap(four, true); |
771 |
|
assertEquals(four, ssm.firstKey()); |
772 |
|
assertEquals(five, ssm.lastKey()); |
773 |
< |
assertTrue(ssm.remove(four) != null); |
773 |
> |
assertEquals("D", ssm.remove(four)); |
774 |
|
assertEquals(1, ssm.size()); |
775 |
|
assertEquals(3, sm.size()); |
776 |
|
assertEquals(4, map.size()); |
782 |
|
/** |
783 |
|
* Submaps of submaps subdivide correctly |
784 |
|
*/ |
785 |
< |
public void testRecursiveSubMaps() { |
786 |
< |
int mapSize = 1000; |
787 |
< |
Class cl = TreeMap.class; |
785 |
> |
public void testRecursiveSubMaps() throws Exception { |
786 |
> |
int mapSize = expensiveTests ? 1000 : 100; |
787 |
> |
Class cl = TreeMap.class; |
788 |
|
NavigableMap<Integer, Integer> map = newMap(cl); |
789 |
|
bs = new BitSet(mapSize); |
790 |
|
|
800 |
|
0, mapSize - 1, true); |
801 |
|
} |
802 |
|
|
803 |
< |
static NavigableMap<Integer, Integer> newMap(Class cl) { |
804 |
< |
NavigableMap<Integer, Integer> result = null; |
805 |
< |
try { |
806 |
< |
result = (NavigableMap<Integer, Integer>) cl.newInstance(); |
819 |
< |
} catch(Exception e) { |
820 |
< |
fail(); |
821 |
< |
} |
822 |
< |
assertEquals(result.size(), 0); |
803 |
> |
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception { |
804 |
> |
NavigableMap<Integer, Integer> result |
805 |
> |
= (NavigableMap<Integer, Integer>) cl.newInstance(); |
806 |
> |
assertEquals(0, result.size()); |
807 |
|
assertFalse(result.keySet().iterator().hasNext()); |
808 |
|
return result; |
809 |
|
} |
825 |
|
} |
826 |
|
|
827 |
|
// Remove a bunch of entries with iterator |
828 |
< |
for(Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
828 |
> |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
829 |
|
if (rnd.nextBoolean()) { |
830 |
|
bs.clear(it.next()); |
831 |
|
it.remove(); |
850 |
|
} |
851 |
|
|
852 |
|
// Remove a bunch of entries with iterator |
853 |
< |
for(Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
853 |
> |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
854 |
|
if (rnd.nextBoolean()) { |
855 |
|
bs.clear(it.next()); |
856 |
|
it.remove(); |
865 |
|
} else { |
866 |
|
try { |
867 |
|
map.put(key, 2 * key); |
868 |
< |
fail(); |
869 |
< |
} catch(IllegalArgumentException e) { |
886 |
< |
// expected |
887 |
< |
} |
868 |
> |
shouldThrow(); |
869 |
> |
} catch (IllegalArgumentException success) {} |
870 |
|
} |
871 |
|
} |
872 |
|
} |
964 |
|
*/ |
965 |
|
void check(NavigableMap<Integer, Integer> map, |
966 |
|
final int min, final int max, final boolean ascending) { |
967 |
< |
class ReferenceSet { |
967 |
> |
class ReferenceSet { |
968 |
|
int lower(int key) { |
969 |
|
return ascending ? lowerAscending(key) : higherAscending(key); |
970 |
|
} |
995 |
|
// BitSet should support this! Test would run much faster |
996 |
|
while (key >= min) { |
997 |
|
if (bs.get(key)) |
998 |
< |
return(key); |
998 |
> |
return key; |
999 |
|
key--; |
1000 |
|
} |
1001 |
|
return -1; |
1030 |
|
if (bsContainsI) |
1031 |
|
size++; |
1032 |
|
} |
1033 |
< |
assertEquals(map.size(), size); |
1033 |
> |
assertEquals(size, map.size()); |
1034 |
|
|
1035 |
|
// Test contents using contains keySet iterator |
1036 |
|
int size2 = 0; |
1061 |
|
assertEq(rs.last(), -1); |
1062 |
|
try { |
1063 |
|
map.firstKey(); |
1064 |
< |
fail(); |
1065 |
< |
} catch(NoSuchElementException e) { |
1084 |
< |
// expected |
1085 |
< |
} |
1064 |
> |
shouldThrow(); |
1065 |
> |
} catch (NoSuchElementException success) {} |
1066 |
|
try { |
1067 |
|
map.lastKey(); |
1068 |
< |
fail(); |
1069 |
< |
} catch(NoSuchElementException e) { |
1090 |
< |
// expected |
1091 |
< |
} |
1068 |
> |
shouldThrow(); |
1069 |
> |
} catch (NoSuchElementException success) {} |
1070 |
|
} |
1071 |
|
} |
1072 |
|
|