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 |
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.remove(new Integer(i))); |
284 |
} |
285 |
for (int i = 0; i < SIZE; i+=2) { |
286 |
assertTrue(q.remove(new Integer(i))); |
287 |
assertFalse(q.remove(new Integer(i+1))); |
288 |
} |
289 |
assertTrue(q.isEmpty()); |
290 |
} |
291 |
|
292 |
/** |
293 |
* contains(x) reports true when elements added but not yet removed |
294 |
*/ |
295 |
public void testContains() { |
296 |
TreeSet q = populatedSet(SIZE); |
297 |
for (int i = 0; i < SIZE; ++i) { |
298 |
assertTrue(q.contains(new Integer(i))); |
299 |
q.pollFirst(); |
300 |
assertFalse(q.contains(new Integer(i))); |
301 |
} |
302 |
} |
303 |
|
304 |
/** |
305 |
* clear removes all elements |
306 |
*/ |
307 |
public void testClear() { |
308 |
TreeSet q = populatedSet(SIZE); |
309 |
q.clear(); |
310 |
assertTrue(q.isEmpty()); |
311 |
assertEquals(0, q.size()); |
312 |
q.add(new Integer(1)); |
313 |
assertFalse(q.isEmpty()); |
314 |
q.clear(); |
315 |
assertTrue(q.isEmpty()); |
316 |
} |
317 |
|
318 |
/** |
319 |
* containsAll(c) is true when c contains a subset of elements |
320 |
*/ |
321 |
public void testContainsAll() { |
322 |
TreeSet q = populatedSet(SIZE); |
323 |
TreeSet p = new TreeSet(); |
324 |
for (int i = 0; i < SIZE; ++i) { |
325 |
assertTrue(q.containsAll(p)); |
326 |
assertFalse(p.containsAll(q)); |
327 |
p.add(new Integer(i)); |
328 |
} |
329 |
assertTrue(p.containsAll(q)); |
330 |
} |
331 |
|
332 |
/** |
333 |
* retainAll(c) retains only those elements of c and reports true if changed |
334 |
*/ |
335 |
public void testRetainAll() { |
336 |
TreeSet q = populatedSet(SIZE); |
337 |
TreeSet p = populatedSet(SIZE); |
338 |
for (int i = 0; i < SIZE; ++i) { |
339 |
boolean changed = q.retainAll(p); |
340 |
if (i == 0) |
341 |
assertFalse(changed); |
342 |
else |
343 |
assertTrue(changed); |
344 |
|
345 |
assertTrue(q.containsAll(p)); |
346 |
assertEquals(SIZE-i, q.size()); |
347 |
p.pollFirst(); |
348 |
} |
349 |
} |
350 |
|
351 |
/** |
352 |
* removeAll(c) removes only those elements of c and reports true if changed |
353 |
*/ |
354 |
public void testRemoveAll() { |
355 |
for (int i = 1; i < SIZE; ++i) { |
356 |
TreeSet q = populatedSet(SIZE); |
357 |
TreeSet p = populatedSet(i); |
358 |
assertTrue(q.removeAll(p)); |
359 |
assertEquals(SIZE-i, q.size()); |
360 |
for (int j = 0; j < i; ++j) { |
361 |
Integer I = (Integer)(p.pollFirst()); |
362 |
assertFalse(q.contains(I)); |
363 |
} |
364 |
} |
365 |
} |
366 |
|
367 |
|
368 |
|
369 |
/** |
370 |
* lower returns preceding element |
371 |
*/ |
372 |
public void testLower() { |
373 |
TreeSet q = set5(); |
374 |
Object e1 = q.lower(three); |
375 |
assertEquals(two, e1); |
376 |
|
377 |
Object e2 = q.lower(six); |
378 |
assertEquals(five, e2); |
379 |
|
380 |
Object e3 = q.lower(one); |
381 |
assertNull(e3); |
382 |
|
383 |
Object e4 = q.lower(zero); |
384 |
assertNull(e4); |
385 |
} |
386 |
|
387 |
/** |
388 |
* higher returns next element |
389 |
*/ |
390 |
public void testHigher() { |
391 |
TreeSet q = set5(); |
392 |
Object e1 = q.higher(three); |
393 |
assertEquals(four, e1); |
394 |
|
395 |
Object e2 = q.higher(zero); |
396 |
assertEquals(one, e2); |
397 |
|
398 |
Object e3 = q.higher(five); |
399 |
assertNull(e3); |
400 |
|
401 |
Object e4 = q.higher(six); |
402 |
assertNull(e4); |
403 |
} |
404 |
|
405 |
/** |
406 |
* floor returns preceding element |
407 |
*/ |
408 |
public void testFloor() { |
409 |
TreeSet q = set5(); |
410 |
Object e1 = q.floor(three); |
411 |
assertEquals(three, e1); |
412 |
|
413 |
Object e2 = q.floor(six); |
414 |
assertEquals(five, e2); |
415 |
|
416 |
Object e3 = q.floor(one); |
417 |
assertEquals(one, e3); |
418 |
|
419 |
Object e4 = q.floor(zero); |
420 |
assertNull(e4); |
421 |
} |
422 |
|
423 |
/** |
424 |
* ceiling returns next element |
425 |
*/ |
426 |
public void testCeiling() { |
427 |
TreeSet q = set5(); |
428 |
Object e1 = q.ceiling(three); |
429 |
assertEquals(three, e1); |
430 |
|
431 |
Object e2 = q.ceiling(zero); |
432 |
assertEquals(one, e2); |
433 |
|
434 |
Object e3 = q.ceiling(five); |
435 |
assertEquals(five, e3); |
436 |
|
437 |
Object e4 = q.ceiling(six); |
438 |
assertNull(e4); |
439 |
} |
440 |
|
441 |
/** |
442 |
* toArray contains all elements in sorted order |
443 |
*/ |
444 |
public void testToArray() { |
445 |
TreeSet q = populatedSet(SIZE); |
446 |
Object[] o = q.toArray(); |
447 |
for (int i = 0; i < o.length; i++) |
448 |
assertSame(o[i], q.pollFirst()); |
449 |
} |
450 |
|
451 |
/** |
452 |
* toArray(a) contains all elements in sorted order |
453 |
*/ |
454 |
public void testToArray2() { |
455 |
TreeSet<Integer> q = populatedSet(SIZE); |
456 |
Integer[] ints = new Integer[SIZE]; |
457 |
Integer[] array = q.toArray(ints); |
458 |
assertSame(ints, array); |
459 |
for (int i = 0; i < ints.length; i++) |
460 |
assertSame(ints[i], q.pollFirst()); |
461 |
} |
462 |
|
463 |
/** |
464 |
* iterator iterates through all elements |
465 |
*/ |
466 |
public void testIterator() { |
467 |
TreeSet q = populatedSet(SIZE); |
468 |
int i = 0; |
469 |
Iterator it = q.iterator(); |
470 |
while (it.hasNext()) { |
471 |
assertTrue(q.contains(it.next())); |
472 |
++i; |
473 |
} |
474 |
assertEquals(i, SIZE); |
475 |
} |
476 |
|
477 |
/** |
478 |
* iterator of empty set has no elements |
479 |
*/ |
480 |
public void testEmptyIterator() { |
481 |
TreeSet q = new TreeSet(); |
482 |
int i = 0; |
483 |
Iterator it = q.iterator(); |
484 |
while (it.hasNext()) { |
485 |
assertTrue(q.contains(it.next())); |
486 |
++i; |
487 |
} |
488 |
assertEquals(i, 0); |
489 |
} |
490 |
|
491 |
/** |
492 |
* iterator.remove removes current element |
493 |
*/ |
494 |
public void testIteratorRemove() { |
495 |
final TreeSet q = new TreeSet(); |
496 |
q.add(new Integer(2)); |
497 |
q.add(new Integer(1)); |
498 |
q.add(new Integer(3)); |
499 |
|
500 |
Iterator it = q.iterator(); |
501 |
it.next(); |
502 |
it.remove(); |
503 |
|
504 |
it = q.iterator(); |
505 |
assertEquals(it.next(), new Integer(2)); |
506 |
assertEquals(it.next(), new Integer(3)); |
507 |
assertFalse(it.hasNext()); |
508 |
} |
509 |
|
510 |
|
511 |
/** |
512 |
* toString contains toStrings of elements |
513 |
*/ |
514 |
public void testToString() { |
515 |
TreeSet q = populatedSet(SIZE); |
516 |
String s = q.toString(); |
517 |
for (int i = 0; i < SIZE; ++i) { |
518 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
519 |
} |
520 |
} |
521 |
|
522 |
/** |
523 |
* A deserialized serialized set has same elements |
524 |
*/ |
525 |
public void testSerialization() throws Exception { |
526 |
TreeSet q = populatedSet(SIZE); |
527 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
528 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
529 |
out.writeObject(q); |
530 |
out.close(); |
531 |
|
532 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
533 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
534 |
TreeSet r = (TreeSet)in.readObject(); |
535 |
assertEquals(q.size(), r.size()); |
536 |
while (!q.isEmpty()) |
537 |
assertEquals(q.pollFirst(), r.pollFirst()); |
538 |
} |
539 |
|
540 |
/** |
541 |
* subSet returns set with keys in requested range |
542 |
*/ |
543 |
public void testSubSetContents() { |
544 |
TreeSet set = set5(); |
545 |
SortedSet sm = set.subSet(two, four); |
546 |
assertEquals(two, sm.first()); |
547 |
assertEquals(three, sm.last()); |
548 |
assertEquals(2, sm.size()); |
549 |
assertFalse(sm.contains(one)); |
550 |
assertTrue(sm.contains(two)); |
551 |
assertTrue(sm.contains(three)); |
552 |
assertFalse(sm.contains(four)); |
553 |
assertFalse(sm.contains(five)); |
554 |
Iterator i = sm.iterator(); |
555 |
Object k; |
556 |
k = (Integer)(i.next()); |
557 |
assertEquals(two, k); |
558 |
k = (Integer)(i.next()); |
559 |
assertEquals(three, k); |
560 |
assertFalse(i.hasNext()); |
561 |
Iterator j = sm.iterator(); |
562 |
j.next(); |
563 |
j.remove(); |
564 |
assertFalse(set.contains(two)); |
565 |
assertEquals(4, set.size()); |
566 |
assertEquals(1, sm.size()); |
567 |
assertEquals(three, sm.first()); |
568 |
assertEquals(three, sm.last()); |
569 |
assertTrue(sm.remove(three)); |
570 |
assertTrue(sm.isEmpty()); |
571 |
assertEquals(3, set.size()); |
572 |
} |
573 |
|
574 |
public void testSubSetContents2() { |
575 |
TreeSet set = set5(); |
576 |
SortedSet sm = set.subSet(two, three); |
577 |
assertEquals(1, sm.size()); |
578 |
assertEquals(two, sm.first()); |
579 |
assertEquals(two, sm.last()); |
580 |
assertFalse(sm.contains(one)); |
581 |
assertTrue(sm.contains(two)); |
582 |
assertFalse(sm.contains(three)); |
583 |
assertFalse(sm.contains(four)); |
584 |
assertFalse(sm.contains(five)); |
585 |
Iterator i = sm.iterator(); |
586 |
Object k; |
587 |
k = (Integer)(i.next()); |
588 |
assertEquals(two, k); |
589 |
assertFalse(i.hasNext()); |
590 |
Iterator j = sm.iterator(); |
591 |
j.next(); |
592 |
j.remove(); |
593 |
assertFalse(set.contains(two)); |
594 |
assertEquals(4, set.size()); |
595 |
assertEquals(0, sm.size()); |
596 |
assertTrue(sm.isEmpty()); |
597 |
assertFalse(sm.remove(three)); |
598 |
assertEquals(4, set.size()); |
599 |
} |
600 |
|
601 |
/** |
602 |
* headSet returns set with keys in requested range |
603 |
*/ |
604 |
public void testHeadSetContents() { |
605 |
TreeSet set = set5(); |
606 |
SortedSet sm = set.headSet(four); |
607 |
assertTrue(sm.contains(one)); |
608 |
assertTrue(sm.contains(two)); |
609 |
assertTrue(sm.contains(three)); |
610 |
assertFalse(sm.contains(four)); |
611 |
assertFalse(sm.contains(five)); |
612 |
Iterator i = sm.iterator(); |
613 |
Object k; |
614 |
k = (Integer)(i.next()); |
615 |
assertEquals(one, k); |
616 |
k = (Integer)(i.next()); |
617 |
assertEquals(two, k); |
618 |
k = (Integer)(i.next()); |
619 |
assertEquals(three, k); |
620 |
assertFalse(i.hasNext()); |
621 |
sm.clear(); |
622 |
assertTrue(sm.isEmpty()); |
623 |
assertEquals(2, set.size()); |
624 |
assertEquals(four, set.first()); |
625 |
} |
626 |
|
627 |
/** |
628 |
* tailSet returns set with keys in requested range |
629 |
*/ |
630 |
public void testTailSetContents() { |
631 |
TreeSet set = set5(); |
632 |
SortedSet sm = set.tailSet(two); |
633 |
assertFalse(sm.contains(one)); |
634 |
assertTrue(sm.contains(two)); |
635 |
assertTrue(sm.contains(three)); |
636 |
assertTrue(sm.contains(four)); |
637 |
assertTrue(sm.contains(five)); |
638 |
Iterator i = sm.iterator(); |
639 |
Object k; |
640 |
k = (Integer)(i.next()); |
641 |
assertEquals(two, k); |
642 |
k = (Integer)(i.next()); |
643 |
assertEquals(three, k); |
644 |
k = (Integer)(i.next()); |
645 |
assertEquals(four, k); |
646 |
k = (Integer)(i.next()); |
647 |
assertEquals(five, k); |
648 |
assertFalse(i.hasNext()); |
649 |
|
650 |
SortedSet ssm = sm.tailSet(four); |
651 |
assertEquals(four, ssm.first()); |
652 |
assertEquals(five, ssm.last()); |
653 |
assertTrue(ssm.remove(four)); |
654 |
assertEquals(1, ssm.size()); |
655 |
assertEquals(3, sm.size()); |
656 |
assertEquals(4, set.size()); |
657 |
} |
658 |
|
659 |
Random rnd = new Random(666); |
660 |
BitSet bs; |
661 |
|
662 |
/** |
663 |
* Subsets of subsets subdivide correctly |
664 |
*/ |
665 |
public void testRecursiveSubSets() throws Exception { |
666 |
int setSize = expensiveTests ? 1000 : 100; |
667 |
Class cl = TreeSet.class; |
668 |
|
669 |
NavigableSet<Integer> set = newSet(cl); |
670 |
bs = new BitSet(setSize); |
671 |
|
672 |
populate(set, setSize); |
673 |
check(set, 0, setSize - 1, true); |
674 |
check(set.descendingSet(), 0, setSize - 1, false); |
675 |
|
676 |
mutateSet(set, 0, setSize - 1); |
677 |
check(set, 0, setSize - 1, true); |
678 |
check(set.descendingSet(), 0, setSize - 1, false); |
679 |
|
680 |
bashSubSet(set.subSet(0, true, setSize, false), |
681 |
0, setSize - 1, true); |
682 |
} |
683 |
|
684 |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
685 |
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
686 |
assertEquals(result.size(), 0); |
687 |
assertFalse(result.iterator().hasNext()); |
688 |
return result; |
689 |
} |
690 |
|
691 |
void populate(NavigableSet<Integer> set, int limit) { |
692 |
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
693 |
int element = rnd.nextInt(limit); |
694 |
put(set, element); |
695 |
} |
696 |
} |
697 |
|
698 |
void mutateSet(NavigableSet<Integer> set, int min, int max) { |
699 |
int size = set.size(); |
700 |
int rangeSize = max - min + 1; |
701 |
|
702 |
// Remove a bunch of entries directly |
703 |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
704 |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
705 |
} |
706 |
|
707 |
// Remove a bunch of entries with iterator |
708 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
709 |
if (rnd.nextBoolean()) { |
710 |
bs.clear(it.next()); |
711 |
it.remove(); |
712 |
} |
713 |
} |
714 |
|
715 |
// Add entries till we're back to original size |
716 |
while (set.size() < size) { |
717 |
int element = min + rnd.nextInt(rangeSize); |
718 |
assertTrue(element >= min && element<= max); |
719 |
put(set, element); |
720 |
} |
721 |
} |
722 |
|
723 |
void mutateSubSet(NavigableSet<Integer> set, int min, int max) { |
724 |
int size = set.size(); |
725 |
int rangeSize = max - min + 1; |
726 |
|
727 |
// Remove a bunch of entries directly |
728 |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
729 |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
730 |
} |
731 |
|
732 |
// Remove a bunch of entries with iterator |
733 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
734 |
if (rnd.nextBoolean()) { |
735 |
bs.clear(it.next()); |
736 |
it.remove(); |
737 |
} |
738 |
} |
739 |
|
740 |
// Add entries till we're back to original size |
741 |
while (set.size() < size) { |
742 |
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
743 |
if (element >= min && element<= max) { |
744 |
put(set, element); |
745 |
} else { |
746 |
try { |
747 |
set.add(element); |
748 |
shouldThrow(); |
749 |
} catch (IllegalArgumentException success) {} |
750 |
} |
751 |
} |
752 |
} |
753 |
|
754 |
void put(NavigableSet<Integer> set, int element) { |
755 |
if (set.add(element)) |
756 |
bs.set(element); |
757 |
} |
758 |
|
759 |
void remove(NavigableSet<Integer> set, int element) { |
760 |
if (set.remove(element)) |
761 |
bs.clear(element); |
762 |
} |
763 |
|
764 |
void bashSubSet(NavigableSet<Integer> set, |
765 |
int min, int max, boolean ascending) { |
766 |
check(set, min, max, ascending); |
767 |
check(set.descendingSet(), min, max, !ascending); |
768 |
|
769 |
mutateSubSet(set, min, max); |
770 |
check(set, min, max, ascending); |
771 |
check(set.descendingSet(), min, max, !ascending); |
772 |
|
773 |
// Recurse |
774 |
if (max - min < 2) |
775 |
return; |
776 |
int midPoint = (min + max) / 2; |
777 |
|
778 |
// headSet - pick direction and endpoint inclusion randomly |
779 |
boolean incl = rnd.nextBoolean(); |
780 |
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
781 |
if (ascending) { |
782 |
if (rnd.nextBoolean()) |
783 |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
784 |
else |
785 |
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
786 |
false); |
787 |
} else { |
788 |
if (rnd.nextBoolean()) |
789 |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false); |
790 |
else |
791 |
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
792 |
true); |
793 |
} |
794 |
|
795 |
// tailSet - pick direction and endpoint inclusion randomly |
796 |
incl = rnd.nextBoolean(); |
797 |
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
798 |
if (ascending) { |
799 |
if (rnd.nextBoolean()) |
800 |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
801 |
else |
802 |
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
803 |
false); |
804 |
} else { |
805 |
if (rnd.nextBoolean()) { |
806 |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false); |
807 |
} else { |
808 |
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
809 |
true); |
810 |
} |
811 |
} |
812 |
|
813 |
// subSet - pick direction and endpoint inclusion randomly |
814 |
int rangeSize = max - min + 1; |
815 |
int[] endpoints = new int[2]; |
816 |
endpoints[0] = min + rnd.nextInt(rangeSize); |
817 |
endpoints[1] = min + rnd.nextInt(rangeSize); |
818 |
Arrays.sort(endpoints); |
819 |
boolean lowIncl = rnd.nextBoolean(); |
820 |
boolean highIncl = rnd.nextBoolean(); |
821 |
if (ascending) { |
822 |
NavigableSet<Integer> sm = set.subSet( |
823 |
endpoints[0], lowIncl, endpoints[1], highIncl); |
824 |
if (rnd.nextBoolean()) |
825 |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
826 |
endpoints[1] - (highIncl ? 0 : 1), true); |
827 |
else |
828 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
829 |
endpoints[1] - (highIncl ? 0 : 1), false); |
830 |
} else { |
831 |
NavigableSet<Integer> sm = set.subSet( |
832 |
endpoints[1], highIncl, endpoints[0], lowIncl); |
833 |
if (rnd.nextBoolean()) |
834 |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
835 |
endpoints[1] - (highIncl ? 0 : 1), false); |
836 |
else |
837 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
838 |
endpoints[1] - (highIncl ? 0 : 1), true); |
839 |
} |
840 |
} |
841 |
|
842 |
/** |
843 |
* min and max are both inclusive. If max < min, interval is empty. |
844 |
*/ |
845 |
void check(NavigableSet<Integer> set, |
846 |
final int min, final int max, final boolean ascending) { |
847 |
class ReferenceSet { |
848 |
int lower(int element) { |
849 |
return ascending ? |
850 |
lowerAscending(element) : higherAscending(element); |
851 |
} |
852 |
int floor(int element) { |
853 |
return ascending ? |
854 |
floorAscending(element) : ceilingAscending(element); |
855 |
} |
856 |
int ceiling(int element) { |
857 |
return ascending ? |
858 |
ceilingAscending(element) : floorAscending(element); |
859 |
} |
860 |
int higher(int element) { |
861 |
return ascending ? |
862 |
higherAscending(element) : lowerAscending(element); |
863 |
} |
864 |
int first() { |
865 |
return ascending ? firstAscending() : lastAscending(); |
866 |
} |
867 |
int last() { |
868 |
return ascending ? lastAscending() : firstAscending(); |
869 |
} |
870 |
int lowerAscending(int element) { |
871 |
return floorAscending(element - 1); |
872 |
} |
873 |
int floorAscending(int element) { |
874 |
if (element < min) |
875 |
return -1; |
876 |
else if (element > max) |
877 |
element = max; |
878 |
|
879 |
// BitSet should support this! Test would run much faster |
880 |
while (element >= min) { |
881 |
if (bs.get(element)) |
882 |
return element; |
883 |
element--; |
884 |
} |
885 |
return -1; |
886 |
} |
887 |
int ceilingAscending(int element) { |
888 |
if (element < min) |
889 |
element = min; |
890 |
else if (element > max) |
891 |
return -1; |
892 |
int result = bs.nextSetBit(element); |
893 |
return result > max ? -1 : result; |
894 |
} |
895 |
int higherAscending(int element) { |
896 |
return ceilingAscending(element + 1); |
897 |
} |
898 |
private int firstAscending() { |
899 |
int result = ceilingAscending(min); |
900 |
return result > max ? -1 : result; |
901 |
} |
902 |
private int lastAscending() { |
903 |
int result = floorAscending(max); |
904 |
return result < min ? -1 : result; |
905 |
} |
906 |
} |
907 |
ReferenceSet rs = new ReferenceSet(); |
908 |
|
909 |
// Test contents using containsElement |
910 |
int size = 0; |
911 |
for (int i = min; i <= max; i++) { |
912 |
boolean bsContainsI = bs.get(i); |
913 |
assertEquals(bsContainsI, set.contains(i)); |
914 |
if (bsContainsI) |
915 |
size++; |
916 |
} |
917 |
assertEquals(set.size(), size); |
918 |
|
919 |
// Test contents using contains elementSet iterator |
920 |
int size2 = 0; |
921 |
int previousElement = -1; |
922 |
for (int element : set) { |
923 |
assertTrue(bs.get(element)); |
924 |
size2++; |
925 |
assertTrue(previousElement < 0 || (ascending ? |
926 |
element - previousElement > 0 : element - previousElement < 0)); |
927 |
previousElement = element; |
928 |
} |
929 |
assertEquals(size2, size); |
930 |
|
931 |
// Test navigation ops |
932 |
for (int element = min - 1; element <= max + 1; element++) { |
933 |
assertEq(set.lower(element), rs.lower(element)); |
934 |
assertEq(set.floor(element), rs.floor(element)); |
935 |
assertEq(set.higher(element), rs.higher(element)); |
936 |
assertEq(set.ceiling(element), rs.ceiling(element)); |
937 |
} |
938 |
|
939 |
// Test extrema |
940 |
if (set.size() != 0) { |
941 |
assertEq(set.first(), rs.first()); |
942 |
assertEq(set.last(), rs.last()); |
943 |
} else { |
944 |
assertEq(rs.first(), -1); |
945 |
assertEq(rs.last(), -1); |
946 |
try { |
947 |
set.first(); |
948 |
shouldThrow(); |
949 |
} catch (NoSuchElementException success) {} |
950 |
try { |
951 |
set.last(); |
952 |
shouldThrow(); |
953 |
} catch (NoSuchElementException success) {} |
954 |
} |
955 |
} |
956 |
|
957 |
static void assertEq(Integer i, int j) { |
958 |
if (i == null) |
959 |
assertEquals(j, -1); |
960 |
else |
961 |
assertEquals((int) i, j); |
962 |
} |
963 |
|
964 |
static boolean eq(Integer i, int j) { |
965 |
return i == null ? j == -1 : i == j; |
966 |
} |
967 |
|
968 |
} |