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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
import java.util.Arrays; |
8 |
import java.util.BitSet; |
9 |
import java.util.Collection; |
10 |
import java.util.Comparator; |
11 |
import java.util.Iterator; |
12 |
import java.util.NavigableSet; |
13 |
import java.util.NoSuchElementException; |
14 |
import java.util.Random; |
15 |
import java.util.Set; |
16 |
import java.util.SortedSet; |
17 |
import java.util.TreeSet; |
18 |
|
19 |
import junit.framework.Test; |
20 |
import junit.framework.TestSuite; |
21 |
|
22 |
public class TreeSetTest extends JSR166TestCase { |
23 |
public static void main(String[] args) { |
24 |
main(suite(), args); |
25 |
} |
26 |
public static Test suite() { |
27 |
return new TestSuite(TreeSetTest.class); |
28 |
} |
29 |
|
30 |
static class MyReverseComparator implements Comparator { |
31 |
public int compare(Object x, Object y) { |
32 |
return ((Comparable)y).compareTo(x); |
33 |
} |
34 |
} |
35 |
|
36 |
/** |
37 |
* The number of elements to place in collections, arrays, etc. |
38 |
*/ |
39 |
static final int SIZE = 20; |
40 |
|
41 |
/** |
42 |
* Returns a new set of given size containing consecutive |
43 |
* Integers 0 ... n. |
44 |
*/ |
45 |
private TreeSet<Integer> populatedSet(int n) { |
46 |
TreeSet<Integer> q = new TreeSet<Integer>(); |
47 |
assertTrue(q.isEmpty()); |
48 |
for (int i = n-1; i >= 0; i -= 2) |
49 |
assertTrue(q.add(new Integer(i))); |
50 |
for (int i = (n & 1); i < n; i += 2) |
51 |
assertTrue(q.add(new Integer(i))); |
52 |
assertFalse(q.isEmpty()); |
53 |
assertEquals(n, q.size()); |
54 |
return q; |
55 |
} |
56 |
|
57 |
/** |
58 |
* Returns a new set of first 5 ints. |
59 |
*/ |
60 |
private TreeSet set5() { |
61 |
TreeSet q = new TreeSet(); |
62 |
assertTrue(q.isEmpty()); |
63 |
q.add(one); |
64 |
q.add(two); |
65 |
q.add(three); |
66 |
q.add(four); |
67 |
q.add(five); |
68 |
assertEquals(5, q.size()); |
69 |
return q; |
70 |
} |
71 |
|
72 |
/** |
73 |
* A new set has unbounded capacity |
74 |
*/ |
75 |
public void testConstructor1() { |
76 |
assertEquals(0, new TreeSet().size()); |
77 |
} |
78 |
|
79 |
/** |
80 |
* Initializing from null Collection throws NPE |
81 |
*/ |
82 |
public void testConstructor3() { |
83 |
try { |
84 |
new TreeSet((Collection)null); |
85 |
shouldThrow(); |
86 |
} catch (NullPointerException success) {} |
87 |
} |
88 |
|
89 |
/** |
90 |
* Initializing from Collection of null elements throws NPE |
91 |
*/ |
92 |
public void testConstructor4() { |
93 |
try { |
94 |
new TreeSet(Arrays.asList(new Integer[SIZE])); |
95 |
shouldThrow(); |
96 |
} catch (NullPointerException success) {} |
97 |
} |
98 |
|
99 |
/** |
100 |
* Initializing from Collection with some null elements throws NPE |
101 |
*/ |
102 |
public void testConstructor5() { |
103 |
Integer[] ints = new Integer[SIZE]; |
104 |
for (int i = 0; i < SIZE - 1; ++i) |
105 |
ints[i] = new Integer(i); |
106 |
try { |
107 |
new TreeSet(Arrays.asList(ints)); |
108 |
shouldThrow(); |
109 |
} catch (NullPointerException success) {} |
110 |
} |
111 |
|
112 |
/** |
113 |
* Set contains all elements of collection used to initialize |
114 |
*/ |
115 |
public void testConstructor6() { |
116 |
Integer[] ints = new Integer[SIZE]; |
117 |
for (int i = 0; i < SIZE; ++i) |
118 |
ints[i] = new Integer(i); |
119 |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
120 |
for (int i = 0; i < SIZE; ++i) |
121 |
assertEquals(ints[i], q.pollFirst()); |
122 |
} |
123 |
|
124 |
/** |
125 |
* The comparator used in constructor is used |
126 |
*/ |
127 |
public void testConstructor7() { |
128 |
MyReverseComparator cmp = new MyReverseComparator(); |
129 |
TreeSet q = new TreeSet(cmp); |
130 |
assertEquals(cmp, q.comparator()); |
131 |
Integer[] ints = new Integer[SIZE]; |
132 |
for (int i = 0; i < SIZE; ++i) |
133 |
ints[i] = new Integer(i); |
134 |
q.addAll(Arrays.asList(ints)); |
135 |
for (int i = SIZE - 1; i >= 0; --i) |
136 |
assertEquals(ints[i], q.pollFirst()); |
137 |
} |
138 |
|
139 |
/** |
140 |
* isEmpty is true before add, false after |
141 |
*/ |
142 |
public void testEmpty() { |
143 |
TreeSet q = new TreeSet(); |
144 |
assertTrue(q.isEmpty()); |
145 |
q.add(new Integer(1)); |
146 |
assertFalse(q.isEmpty()); |
147 |
q.add(new Integer(2)); |
148 |
q.pollFirst(); |
149 |
q.pollFirst(); |
150 |
assertTrue(q.isEmpty()); |
151 |
} |
152 |
|
153 |
/** |
154 |
* size changes when elements added and removed |
155 |
*/ |
156 |
public void testSize() { |
157 |
TreeSet q = populatedSet(SIZE); |
158 |
for (int i = 0; i < SIZE; ++i) { |
159 |
assertEquals(SIZE - i, q.size()); |
160 |
q.pollFirst(); |
161 |
} |
162 |
for (int i = 0; i < SIZE; ++i) { |
163 |
assertEquals(i, q.size()); |
164 |
q.add(new Integer(i)); |
165 |
} |
166 |
} |
167 |
|
168 |
/** |
169 |
* add(null) throws NPE if nonempty |
170 |
*/ |
171 |
public void testAddNull() { |
172 |
TreeSet q = populatedSet(SIZE); |
173 |
try { |
174 |
q.add(null); |
175 |
shouldThrow(); |
176 |
} catch (NullPointerException success) {} |
177 |
} |
178 |
|
179 |
/** |
180 |
* Add of comparable element succeeds |
181 |
*/ |
182 |
public void testAdd() { |
183 |
TreeSet q = new TreeSet(); |
184 |
assertTrue(q.add(zero)); |
185 |
assertTrue(q.add(one)); |
186 |
} |
187 |
|
188 |
/** |
189 |
* Add of duplicate element fails |
190 |
*/ |
191 |
public void testAddDup() { |
192 |
TreeSet q = new TreeSet(); |
193 |
assertTrue(q.add(zero)); |
194 |
assertFalse(q.add(zero)); |
195 |
} |
196 |
|
197 |
/** |
198 |
* Add of non-Comparable throws CCE |
199 |
*/ |
200 |
public void testAddNonComparable() { |
201 |
TreeSet q = new TreeSet(); |
202 |
try { |
203 |
q.add(new Object()); |
204 |
q.add(new Object()); |
205 |
shouldThrow(); |
206 |
} catch (ClassCastException success) {} |
207 |
} |
208 |
|
209 |
/** |
210 |
* addAll(null) throws NPE |
211 |
*/ |
212 |
public void testAddAll1() { |
213 |
TreeSet q = new TreeSet(); |
214 |
try { |
215 |
q.addAll(null); |
216 |
shouldThrow(); |
217 |
} catch (NullPointerException success) {} |
218 |
} |
219 |
|
220 |
/** |
221 |
* addAll of a collection with null elements throws NPE |
222 |
*/ |
223 |
public void testAddAll2() { |
224 |
TreeSet q = new TreeSet(); |
225 |
Integer[] ints = new Integer[SIZE]; |
226 |
try { |
227 |
q.addAll(Arrays.asList(ints)); |
228 |
shouldThrow(); |
229 |
} catch (NullPointerException success) {} |
230 |
} |
231 |
|
232 |
/** |
233 |
* addAll of a collection with any null elements throws NPE after |
234 |
* possibly adding some elements |
235 |
*/ |
236 |
public void testAddAll3() { |
237 |
TreeSet q = new TreeSet(); |
238 |
Integer[] ints = new Integer[SIZE]; |
239 |
for (int i = 0; i < SIZE - 1; ++i) |
240 |
ints[i] = new Integer(i); |
241 |
try { |
242 |
q.addAll(Arrays.asList(ints)); |
243 |
shouldThrow(); |
244 |
} catch (NullPointerException success) {} |
245 |
} |
246 |
|
247 |
/** |
248 |
* Set contains all elements of successful addAll |
249 |
*/ |
250 |
public void testAddAll5() { |
251 |
Integer[] empty = new Integer[0]; |
252 |
Integer[] ints = new Integer[SIZE]; |
253 |
for (int i = 0; i < SIZE; ++i) |
254 |
ints[i] = new Integer(SIZE - 1 - i); |
255 |
TreeSet q = new TreeSet(); |
256 |
assertFalse(q.addAll(Arrays.asList(empty))); |
257 |
assertTrue(q.addAll(Arrays.asList(ints))); |
258 |
for (int i = 0; i < SIZE; ++i) |
259 |
assertEquals(new Integer(i), q.pollFirst()); |
260 |
} |
261 |
|
262 |
/** |
263 |
* pollFirst succeeds unless empty |
264 |
*/ |
265 |
public void testPollFirst() { |
266 |
TreeSet q = populatedSet(SIZE); |
267 |
for (int i = 0; i < SIZE; ++i) { |
268 |
assertEquals(i, q.pollFirst()); |
269 |
} |
270 |
assertNull(q.pollFirst()); |
271 |
} |
272 |
|
273 |
/** |
274 |
* pollLast succeeds unless empty |
275 |
*/ |
276 |
public void testPollLast() { |
277 |
TreeSet q = populatedSet(SIZE); |
278 |
for (int i = SIZE - 1; i >= 0; --i) { |
279 |
assertEquals(i, q.pollLast()); |
280 |
} |
281 |
assertNull(q.pollFirst()); |
282 |
} |
283 |
|
284 |
/** |
285 |
* remove(x) removes x and returns true if present |
286 |
*/ |
287 |
public void testRemoveElement() { |
288 |
TreeSet q = populatedSet(SIZE); |
289 |
for (int i = 1; i < SIZE; i += 2) { |
290 |
assertTrue(q.contains(i)); |
291 |
assertTrue(q.remove(i)); |
292 |
assertFalse(q.contains(i)); |
293 |
assertTrue(q.contains(i-1)); |
294 |
} |
295 |
for (int i = 0; i < SIZE; i += 2) { |
296 |
assertTrue(q.contains(i)); |
297 |
assertTrue(q.remove(i)); |
298 |
assertFalse(q.contains(i)); |
299 |
assertFalse(q.remove(i+1)); |
300 |
assertFalse(q.contains(i+1)); |
301 |
} |
302 |
assertTrue(q.isEmpty()); |
303 |
} |
304 |
|
305 |
/** |
306 |
* contains(x) reports true when elements added but not yet removed |
307 |
*/ |
308 |
public void testContains() { |
309 |
TreeSet q = populatedSet(SIZE); |
310 |
for (int i = 0; i < SIZE; ++i) { |
311 |
assertTrue(q.contains(new Integer(i))); |
312 |
q.pollFirst(); |
313 |
assertFalse(q.contains(new Integer(i))); |
314 |
} |
315 |
} |
316 |
|
317 |
/** |
318 |
* clear removes all elements |
319 |
*/ |
320 |
public void testClear() { |
321 |
TreeSet q = populatedSet(SIZE); |
322 |
q.clear(); |
323 |
assertTrue(q.isEmpty()); |
324 |
assertEquals(0, q.size()); |
325 |
q.add(new Integer(1)); |
326 |
assertFalse(q.isEmpty()); |
327 |
q.clear(); |
328 |
assertTrue(q.isEmpty()); |
329 |
} |
330 |
|
331 |
/** |
332 |
* containsAll(c) is true when c contains a subset of elements |
333 |
*/ |
334 |
public void testContainsAll() { |
335 |
TreeSet q = populatedSet(SIZE); |
336 |
TreeSet p = new TreeSet(); |
337 |
for (int i = 0; i < SIZE; ++i) { |
338 |
assertTrue(q.containsAll(p)); |
339 |
assertFalse(p.containsAll(q)); |
340 |
p.add(new Integer(i)); |
341 |
} |
342 |
assertTrue(p.containsAll(q)); |
343 |
} |
344 |
|
345 |
/** |
346 |
* retainAll(c) retains only those elements of c and reports true if changed |
347 |
*/ |
348 |
public void testRetainAll() { |
349 |
TreeSet q = populatedSet(SIZE); |
350 |
TreeSet p = populatedSet(SIZE); |
351 |
for (int i = 0; i < SIZE; ++i) { |
352 |
boolean changed = q.retainAll(p); |
353 |
if (i == 0) |
354 |
assertFalse(changed); |
355 |
else |
356 |
assertTrue(changed); |
357 |
|
358 |
assertTrue(q.containsAll(p)); |
359 |
assertEquals(SIZE - i, q.size()); |
360 |
p.pollFirst(); |
361 |
} |
362 |
} |
363 |
|
364 |
/** |
365 |
* removeAll(c) removes only those elements of c and reports true if changed |
366 |
*/ |
367 |
public void testRemoveAll() { |
368 |
for (int i = 1; i < SIZE; ++i) { |
369 |
TreeSet q = populatedSet(SIZE); |
370 |
TreeSet p = populatedSet(i); |
371 |
assertTrue(q.removeAll(p)); |
372 |
assertEquals(SIZE - i, q.size()); |
373 |
for (int j = 0; j < i; ++j) { |
374 |
Integer x = (Integer)(p.pollFirst()); |
375 |
assertFalse(q.contains(x)); |
376 |
} |
377 |
} |
378 |
} |
379 |
|
380 |
/** |
381 |
* lower returns preceding element |
382 |
*/ |
383 |
public void testLower() { |
384 |
TreeSet q = set5(); |
385 |
Object e1 = q.lower(three); |
386 |
assertEquals(two, e1); |
387 |
|
388 |
Object e2 = q.lower(six); |
389 |
assertEquals(five, e2); |
390 |
|
391 |
Object e3 = q.lower(one); |
392 |
assertNull(e3); |
393 |
|
394 |
Object e4 = q.lower(zero); |
395 |
assertNull(e4); |
396 |
} |
397 |
|
398 |
/** |
399 |
* higher returns next element |
400 |
*/ |
401 |
public void testHigher() { |
402 |
TreeSet q = set5(); |
403 |
Object e1 = q.higher(three); |
404 |
assertEquals(four, e1); |
405 |
|
406 |
Object e2 = q.higher(zero); |
407 |
assertEquals(one, e2); |
408 |
|
409 |
Object e3 = q.higher(five); |
410 |
assertNull(e3); |
411 |
|
412 |
Object e4 = q.higher(six); |
413 |
assertNull(e4); |
414 |
} |
415 |
|
416 |
/** |
417 |
* floor returns preceding element |
418 |
*/ |
419 |
public void testFloor() { |
420 |
TreeSet q = set5(); |
421 |
Object e1 = q.floor(three); |
422 |
assertEquals(three, e1); |
423 |
|
424 |
Object e2 = q.floor(six); |
425 |
assertEquals(five, e2); |
426 |
|
427 |
Object e3 = q.floor(one); |
428 |
assertEquals(one, e3); |
429 |
|
430 |
Object e4 = q.floor(zero); |
431 |
assertNull(e4); |
432 |
} |
433 |
|
434 |
/** |
435 |
* ceiling returns next element |
436 |
*/ |
437 |
public void testCeiling() { |
438 |
TreeSet q = set5(); |
439 |
Object e1 = q.ceiling(three); |
440 |
assertEquals(three, e1); |
441 |
|
442 |
Object e2 = q.ceiling(zero); |
443 |
assertEquals(one, e2); |
444 |
|
445 |
Object e3 = q.ceiling(five); |
446 |
assertEquals(five, e3); |
447 |
|
448 |
Object e4 = q.ceiling(six); |
449 |
assertNull(e4); |
450 |
} |
451 |
|
452 |
/** |
453 |
* toArray contains all elements in sorted order |
454 |
*/ |
455 |
public void testToArray() { |
456 |
TreeSet q = populatedSet(SIZE); |
457 |
Object[] o = q.toArray(); |
458 |
for (int i = 0; i < o.length; i++) |
459 |
assertSame(o[i], q.pollFirst()); |
460 |
} |
461 |
|
462 |
/** |
463 |
* toArray(a) contains all elements in sorted order |
464 |
*/ |
465 |
public void testToArray2() { |
466 |
TreeSet<Integer> q = populatedSet(SIZE); |
467 |
Integer[] ints = new Integer[SIZE]; |
468 |
Integer[] array = q.toArray(ints); |
469 |
assertSame(ints, array); |
470 |
for (int i = 0; i < ints.length; i++) |
471 |
assertSame(ints[i], q.pollFirst()); |
472 |
} |
473 |
|
474 |
/** |
475 |
* iterator iterates through all elements |
476 |
*/ |
477 |
public void testIterator() { |
478 |
TreeSet q = populatedSet(SIZE); |
479 |
Iterator it = q.iterator(); |
480 |
int i; |
481 |
for (i = 0; it.hasNext(); i++) |
482 |
assertTrue(q.contains(it.next())); |
483 |
assertEquals(i, SIZE); |
484 |
assertIteratorExhausted(it); |
485 |
} |
486 |
|
487 |
/** |
488 |
* iterator of empty set has no elements |
489 |
*/ |
490 |
public void testEmptyIterator() { |
491 |
assertIteratorExhausted(new TreeSet().iterator()); |
492 |
} |
493 |
|
494 |
/** |
495 |
* iterator.remove removes current element |
496 |
*/ |
497 |
public void testIteratorRemove() { |
498 |
final TreeSet q = new TreeSet(); |
499 |
q.add(new Integer(2)); |
500 |
q.add(new Integer(1)); |
501 |
q.add(new Integer(3)); |
502 |
|
503 |
Iterator it = q.iterator(); |
504 |
it.next(); |
505 |
it.remove(); |
506 |
|
507 |
it = q.iterator(); |
508 |
assertEquals(it.next(), new Integer(2)); |
509 |
assertEquals(it.next(), new Integer(3)); |
510 |
assertFalse(it.hasNext()); |
511 |
} |
512 |
|
513 |
/** |
514 |
* toString contains toStrings of elements |
515 |
*/ |
516 |
public void testToString() { |
517 |
TreeSet q = populatedSet(SIZE); |
518 |
String s = q.toString(); |
519 |
for (int i = 0; i < SIZE; ++i) { |
520 |
assertTrue(s.contains(String.valueOf(i))); |
521 |
} |
522 |
} |
523 |
|
524 |
/** |
525 |
* A deserialized serialized set has same elements |
526 |
*/ |
527 |
public void testSerialization() throws Exception { |
528 |
NavigableSet x = populatedSet(SIZE); |
529 |
NavigableSet y = serialClone(x); |
530 |
|
531 |
assertNotSame(x, y); |
532 |
assertEquals(x.size(), y.size()); |
533 |
assertEquals(x, y); |
534 |
assertEquals(y, x); |
535 |
while (!x.isEmpty()) { |
536 |
assertFalse(y.isEmpty()); |
537 |
assertEquals(x.pollFirst(), y.pollFirst()); |
538 |
} |
539 |
assertTrue(y.isEmpty()); |
540 |
} |
541 |
|
542 |
/** |
543 |
* subSet returns set with keys in requested range |
544 |
*/ |
545 |
public void testSubSetContents() { |
546 |
TreeSet set = set5(); |
547 |
SortedSet sm = set.subSet(two, four); |
548 |
assertEquals(two, sm.first()); |
549 |
assertEquals(three, sm.last()); |
550 |
assertEquals(2, sm.size()); |
551 |
assertFalse(sm.contains(one)); |
552 |
assertTrue(sm.contains(two)); |
553 |
assertTrue(sm.contains(three)); |
554 |
assertFalse(sm.contains(four)); |
555 |
assertFalse(sm.contains(five)); |
556 |
Iterator i = sm.iterator(); |
557 |
Object k; |
558 |
k = (Integer)(i.next()); |
559 |
assertEquals(two, k); |
560 |
k = (Integer)(i.next()); |
561 |
assertEquals(three, k); |
562 |
assertFalse(i.hasNext()); |
563 |
Iterator j = sm.iterator(); |
564 |
j.next(); |
565 |
j.remove(); |
566 |
assertFalse(set.contains(two)); |
567 |
assertEquals(4, set.size()); |
568 |
assertEquals(1, sm.size()); |
569 |
assertEquals(three, sm.first()); |
570 |
assertEquals(three, sm.last()); |
571 |
assertTrue(sm.remove(three)); |
572 |
assertTrue(sm.isEmpty()); |
573 |
assertEquals(3, set.size()); |
574 |
} |
575 |
|
576 |
public void testSubSetContents2() { |
577 |
TreeSet set = set5(); |
578 |
SortedSet sm = set.subSet(two, three); |
579 |
assertEquals(1, sm.size()); |
580 |
assertEquals(two, sm.first()); |
581 |
assertEquals(two, sm.last()); |
582 |
assertFalse(sm.contains(one)); |
583 |
assertTrue(sm.contains(two)); |
584 |
assertFalse(sm.contains(three)); |
585 |
assertFalse(sm.contains(four)); |
586 |
assertFalse(sm.contains(five)); |
587 |
Iterator i = sm.iterator(); |
588 |
Object k; |
589 |
k = (Integer)(i.next()); |
590 |
assertEquals(two, k); |
591 |
assertFalse(i.hasNext()); |
592 |
Iterator j = sm.iterator(); |
593 |
j.next(); |
594 |
j.remove(); |
595 |
assertFalse(set.contains(two)); |
596 |
assertEquals(4, set.size()); |
597 |
assertEquals(0, sm.size()); |
598 |
assertTrue(sm.isEmpty()); |
599 |
assertFalse(sm.remove(three)); |
600 |
assertEquals(4, set.size()); |
601 |
} |
602 |
|
603 |
/** |
604 |
* headSet returns set with keys in requested range |
605 |
*/ |
606 |
public void testHeadSetContents() { |
607 |
TreeSet set = set5(); |
608 |
SortedSet sm = set.headSet(four); |
609 |
assertTrue(sm.contains(one)); |
610 |
assertTrue(sm.contains(two)); |
611 |
assertTrue(sm.contains(three)); |
612 |
assertFalse(sm.contains(four)); |
613 |
assertFalse(sm.contains(five)); |
614 |
Iterator i = sm.iterator(); |
615 |
Object k; |
616 |
k = (Integer)(i.next()); |
617 |
assertEquals(one, k); |
618 |
k = (Integer)(i.next()); |
619 |
assertEquals(two, k); |
620 |
k = (Integer)(i.next()); |
621 |
assertEquals(three, k); |
622 |
assertFalse(i.hasNext()); |
623 |
sm.clear(); |
624 |
assertTrue(sm.isEmpty()); |
625 |
assertEquals(2, set.size()); |
626 |
assertEquals(four, set.first()); |
627 |
} |
628 |
|
629 |
/** |
630 |
* tailSet returns set with keys in requested range |
631 |
*/ |
632 |
public void testTailSetContents() { |
633 |
TreeSet set = set5(); |
634 |
SortedSet sm = set.tailSet(two); |
635 |
assertFalse(sm.contains(one)); |
636 |
assertTrue(sm.contains(two)); |
637 |
assertTrue(sm.contains(three)); |
638 |
assertTrue(sm.contains(four)); |
639 |
assertTrue(sm.contains(five)); |
640 |
Iterator i = sm.iterator(); |
641 |
Object k; |
642 |
k = (Integer)(i.next()); |
643 |
assertEquals(two, k); |
644 |
k = (Integer)(i.next()); |
645 |
assertEquals(three, k); |
646 |
k = (Integer)(i.next()); |
647 |
assertEquals(four, k); |
648 |
k = (Integer)(i.next()); |
649 |
assertEquals(five, k); |
650 |
assertFalse(i.hasNext()); |
651 |
|
652 |
SortedSet ssm = sm.tailSet(four); |
653 |
assertEquals(four, ssm.first()); |
654 |
assertEquals(five, ssm.last()); |
655 |
assertTrue(ssm.remove(four)); |
656 |
assertEquals(1, ssm.size()); |
657 |
assertEquals(3, sm.size()); |
658 |
assertEquals(4, set.size()); |
659 |
} |
660 |
|
661 |
Random rnd = new Random(666); |
662 |
BitSet bs; |
663 |
|
664 |
/** |
665 |
* Subsets of subsets subdivide correctly |
666 |
*/ |
667 |
public void testRecursiveSubSets() throws Exception { |
668 |
int setSize = expensiveTests ? 1000 : 100; |
669 |
Class cl = TreeSet.class; |
670 |
|
671 |
NavigableSet<Integer> set = newSet(cl); |
672 |
bs = new BitSet(setSize); |
673 |
|
674 |
populate(set, setSize); |
675 |
check(set, 0, setSize - 1, true); |
676 |
check(set.descendingSet(), 0, setSize - 1, false); |
677 |
|
678 |
mutateSet(set, 0, setSize - 1); |
679 |
check(set, 0, setSize - 1, true); |
680 |
check(set.descendingSet(), 0, setSize - 1, false); |
681 |
|
682 |
bashSubSet(set.subSet(0, true, setSize, false), |
683 |
0, setSize - 1, true); |
684 |
} |
685 |
|
686 |
/** |
687 |
* addAll is idempotent |
688 |
*/ |
689 |
public void testAddAll_idempotent() throws Exception { |
690 |
Set x = populatedSet(SIZE); |
691 |
Set y = new TreeSet(x); |
692 |
y.addAll(x); |
693 |
assertEquals(x, y); |
694 |
assertEquals(y, x); |
695 |
} |
696 |
|
697 |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
698 |
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
699 |
assertEquals(0, result.size()); |
700 |
assertFalse(result.iterator().hasNext()); |
701 |
return result; |
702 |
} |
703 |
|
704 |
void populate(NavigableSet<Integer> set, int limit) { |
705 |
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
706 |
int element = rnd.nextInt(limit); |
707 |
put(set, element); |
708 |
} |
709 |
} |
710 |
|
711 |
void mutateSet(NavigableSet<Integer> set, int min, int max) { |
712 |
int size = set.size(); |
713 |
int rangeSize = max - min + 1; |
714 |
|
715 |
// Remove a bunch of entries directly |
716 |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
717 |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
718 |
} |
719 |
|
720 |
// Remove a bunch of entries with iterator |
721 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
722 |
if (rnd.nextBoolean()) { |
723 |
bs.clear(it.next()); |
724 |
it.remove(); |
725 |
} |
726 |
} |
727 |
|
728 |
// Add entries till we're back to original size |
729 |
while (set.size() < size) { |
730 |
int element = min + rnd.nextInt(rangeSize); |
731 |
assertTrue(element >= min && element <= max); |
732 |
put(set, element); |
733 |
} |
734 |
} |
735 |
|
736 |
void mutateSubSet(NavigableSet<Integer> set, int min, int max) { |
737 |
int size = set.size(); |
738 |
int rangeSize = max - min + 1; |
739 |
|
740 |
// Remove a bunch of entries directly |
741 |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
742 |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
743 |
} |
744 |
|
745 |
// Remove a bunch of entries with iterator |
746 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
747 |
if (rnd.nextBoolean()) { |
748 |
bs.clear(it.next()); |
749 |
it.remove(); |
750 |
} |
751 |
} |
752 |
|
753 |
// Add entries till we're back to original size |
754 |
while (set.size() < size) { |
755 |
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
756 |
if (element >= min && element <= max) { |
757 |
put(set, element); |
758 |
} else { |
759 |
try { |
760 |
set.add(element); |
761 |
shouldThrow(); |
762 |
} catch (IllegalArgumentException success) {} |
763 |
} |
764 |
} |
765 |
} |
766 |
|
767 |
void put(NavigableSet<Integer> set, int element) { |
768 |
if (set.add(element)) |
769 |
bs.set(element); |
770 |
} |
771 |
|
772 |
void remove(NavigableSet<Integer> set, int element) { |
773 |
if (set.remove(element)) |
774 |
bs.clear(element); |
775 |
} |
776 |
|
777 |
void bashSubSet(NavigableSet<Integer> set, |
778 |
int min, int max, boolean ascending) { |
779 |
check(set, min, max, ascending); |
780 |
check(set.descendingSet(), min, max, !ascending); |
781 |
|
782 |
mutateSubSet(set, min, max); |
783 |
check(set, min, max, ascending); |
784 |
check(set.descendingSet(), min, max, !ascending); |
785 |
|
786 |
// Recurse |
787 |
if (max - min < 2) |
788 |
return; |
789 |
int midPoint = (min + max) / 2; |
790 |
|
791 |
// headSet - pick direction and endpoint inclusion randomly |
792 |
boolean incl = rnd.nextBoolean(); |
793 |
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
794 |
if (ascending) { |
795 |
if (rnd.nextBoolean()) |
796 |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
797 |
else |
798 |
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
799 |
false); |
800 |
} else { |
801 |
if (rnd.nextBoolean()) |
802 |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false); |
803 |
else |
804 |
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
805 |
true); |
806 |
} |
807 |
|
808 |
// tailSet - pick direction and endpoint inclusion randomly |
809 |
incl = rnd.nextBoolean(); |
810 |
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
811 |
if (ascending) { |
812 |
if (rnd.nextBoolean()) |
813 |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
814 |
else |
815 |
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
816 |
false); |
817 |
} else { |
818 |
if (rnd.nextBoolean()) { |
819 |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false); |
820 |
} else { |
821 |
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
822 |
true); |
823 |
} |
824 |
} |
825 |
|
826 |
// subSet - pick direction and endpoint inclusion randomly |
827 |
int rangeSize = max - min + 1; |
828 |
int[] endpoints = new int[2]; |
829 |
endpoints[0] = min + rnd.nextInt(rangeSize); |
830 |
endpoints[1] = min + rnd.nextInt(rangeSize); |
831 |
Arrays.sort(endpoints); |
832 |
boolean lowIncl = rnd.nextBoolean(); |
833 |
boolean highIncl = rnd.nextBoolean(); |
834 |
if (ascending) { |
835 |
NavigableSet<Integer> sm = set.subSet( |
836 |
endpoints[0], lowIncl, endpoints[1], highIncl); |
837 |
if (rnd.nextBoolean()) |
838 |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
839 |
endpoints[1] - (highIncl ? 0 : 1), true); |
840 |
else |
841 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
842 |
endpoints[1] - (highIncl ? 0 : 1), false); |
843 |
} else { |
844 |
NavigableSet<Integer> sm = set.subSet( |
845 |
endpoints[1], highIncl, endpoints[0], lowIncl); |
846 |
if (rnd.nextBoolean()) |
847 |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
848 |
endpoints[1] - (highIncl ? 0 : 1), false); |
849 |
else |
850 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
851 |
endpoints[1] - (highIncl ? 0 : 1), true); |
852 |
} |
853 |
} |
854 |
|
855 |
/** |
856 |
* min and max are both inclusive. If max < min, interval is empty. |
857 |
*/ |
858 |
void check(NavigableSet<Integer> set, |
859 |
final int min, final int max, final boolean ascending) { |
860 |
class ReferenceSet { |
861 |
int lower(int element) { |
862 |
return ascending ? |
863 |
lowerAscending(element) : higherAscending(element); |
864 |
} |
865 |
int floor(int element) { |
866 |
return ascending ? |
867 |
floorAscending(element) : ceilingAscending(element); |
868 |
} |
869 |
int ceiling(int element) { |
870 |
return ascending ? |
871 |
ceilingAscending(element) : floorAscending(element); |
872 |
} |
873 |
int higher(int element) { |
874 |
return ascending ? |
875 |
higherAscending(element) : lowerAscending(element); |
876 |
} |
877 |
int first() { |
878 |
return ascending ? firstAscending() : lastAscending(); |
879 |
} |
880 |
int last() { |
881 |
return ascending ? lastAscending() : firstAscending(); |
882 |
} |
883 |
int lowerAscending(int element) { |
884 |
return floorAscending(element - 1); |
885 |
} |
886 |
int floorAscending(int element) { |
887 |
if (element < min) |
888 |
return -1; |
889 |
else if (element > max) |
890 |
element = max; |
891 |
|
892 |
// BitSet should support this! Test would run much faster |
893 |
while (element >= min) { |
894 |
if (bs.get(element)) |
895 |
return element; |
896 |
element--; |
897 |
} |
898 |
return -1; |
899 |
} |
900 |
int ceilingAscending(int element) { |
901 |
if (element < min) |
902 |
element = min; |
903 |
else if (element > max) |
904 |
return -1; |
905 |
int result = bs.nextSetBit(element); |
906 |
return (result > max) ? -1 : result; |
907 |
} |
908 |
int higherAscending(int element) { |
909 |
return ceilingAscending(element + 1); |
910 |
} |
911 |
private int firstAscending() { |
912 |
int result = ceilingAscending(min); |
913 |
return (result > max) ? -1 : result; |
914 |
} |
915 |
private int lastAscending() { |
916 |
int result = floorAscending(max); |
917 |
return (result < min) ? -1 : result; |
918 |
} |
919 |
} |
920 |
ReferenceSet rs = new ReferenceSet(); |
921 |
|
922 |
// Test contents using containsElement |
923 |
int size = 0; |
924 |
for (int i = min; i <= max; i++) { |
925 |
boolean bsContainsI = bs.get(i); |
926 |
assertEquals(bsContainsI, set.contains(i)); |
927 |
if (bsContainsI) |
928 |
size++; |
929 |
} |
930 |
assertEquals(size, set.size()); |
931 |
|
932 |
// Test contents using contains elementSet iterator |
933 |
int size2 = 0; |
934 |
int previousElement = -1; |
935 |
for (int element : set) { |
936 |
assertTrue(bs.get(element)); |
937 |
size2++; |
938 |
assertTrue(previousElement < 0 || (ascending ? |
939 |
element - previousElement > 0 : element - previousElement < 0)); |
940 |
previousElement = element; |
941 |
} |
942 |
assertEquals(size2, size); |
943 |
|
944 |
// Test navigation ops |
945 |
for (int element = min - 1; element <= max + 1; element++) { |
946 |
assertEq(set.lower(element), rs.lower(element)); |
947 |
assertEq(set.floor(element), rs.floor(element)); |
948 |
assertEq(set.higher(element), rs.higher(element)); |
949 |
assertEq(set.ceiling(element), rs.ceiling(element)); |
950 |
} |
951 |
|
952 |
// Test extrema |
953 |
if (set.size() != 0) { |
954 |
assertEq(set.first(), rs.first()); |
955 |
assertEq(set.last(), rs.last()); |
956 |
} else { |
957 |
assertEq(rs.first(), -1); |
958 |
assertEq(rs.last(), -1); |
959 |
try { |
960 |
set.first(); |
961 |
shouldThrow(); |
962 |
} catch (NoSuchElementException success) {} |
963 |
try { |
964 |
set.last(); |
965 |
shouldThrow(); |
966 |
} catch (NoSuchElementException success) {} |
967 |
} |
968 |
} |
969 |
|
970 |
static void assertEq(Integer i, int j) { |
971 |
if (i == null) |
972 |
assertEquals(j, -1); |
973 |
else |
974 |
assertEquals((int) i, j); |
975 |
} |
976 |
|
977 |
static boolean eq(Integer i, int j) { |
978 |
return (i == null) ? j == -1 : i == j; |
979 |
} |
980 |
|
981 |
} |