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