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