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 |
/** |
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 |
443 |
*/ |
444 |
public void testToArray() { |
445 |
TreeSet q = populatedSet(SIZE); |
446 |
Object[] o = q.toArray(); |
447 |
Arrays.sort(o); |
448 |
for (int i = 0; i < o.length; i++) |
449 |
assertEquals(o[i], q.pollFirst()); |
450 |
} |
451 |
|
452 |
/** |
453 |
* toArray(a) contains all elements |
454 |
*/ |
455 |
public void testToArray2() { |
456 |
TreeSet q = populatedSet(SIZE); |
457 |
Integer[] ints = new Integer[SIZE]; |
458 |
ints = (Integer[])q.toArray(ints); |
459 |
Arrays.sort(ints); |
460 |
for (int i = 0; i < ints.length; i++) |
461 |
assertEquals(ints[i], q.pollFirst()); |
462 |
} |
463 |
|
464 |
/** |
465 |
* iterator iterates through all elements |
466 |
*/ |
467 |
public void testIterator() { |
468 |
TreeSet q = populatedSet(SIZE); |
469 |
int i = 0; |
470 |
Iterator it = q.iterator(); |
471 |
while (it.hasNext()) { |
472 |
assertTrue(q.contains(it.next())); |
473 |
++i; |
474 |
} |
475 |
assertEquals(i, SIZE); |
476 |
} |
477 |
|
478 |
/** |
479 |
* iterator of empty set has no elements |
480 |
*/ |
481 |
public void testEmptyIterator() { |
482 |
TreeSet q = new TreeSet(); |
483 |
int i = 0; |
484 |
Iterator it = q.iterator(); |
485 |
while (it.hasNext()) { |
486 |
assertTrue(q.contains(it.next())); |
487 |
++i; |
488 |
} |
489 |
assertEquals(i, 0); |
490 |
} |
491 |
|
492 |
/** |
493 |
* iterator.remove removes current element |
494 |
*/ |
495 |
public void testIteratorRemove() { |
496 |
final TreeSet q = new TreeSet(); |
497 |
q.add(new Integer(2)); |
498 |
q.add(new Integer(1)); |
499 |
q.add(new Integer(3)); |
500 |
|
501 |
Iterator it = q.iterator(); |
502 |
it.next(); |
503 |
it.remove(); |
504 |
|
505 |
it = q.iterator(); |
506 |
assertEquals(it.next(), new Integer(2)); |
507 |
assertEquals(it.next(), new Integer(3)); |
508 |
assertFalse(it.hasNext()); |
509 |
} |
510 |
|
511 |
|
512 |
/** |
513 |
* toString contains toStrings of elements |
514 |
*/ |
515 |
public void testToString() { |
516 |
TreeSet q = populatedSet(SIZE); |
517 |
String s = q.toString(); |
518 |
for (int i = 0; i < SIZE; ++i) { |
519 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
520 |
} |
521 |
} |
522 |
|
523 |
/** |
524 |
* A deserialized serialized set has same elements |
525 |
*/ |
526 |
public void testSerialization() throws Exception { |
527 |
TreeSet q = populatedSet(SIZE); |
528 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
529 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
530 |
out.writeObject(q); |
531 |
out.close(); |
532 |
|
533 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
534 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
535 |
TreeSet r = (TreeSet)in.readObject(); |
536 |
assertEquals(q.size(), r.size()); |
537 |
while (!q.isEmpty()) |
538 |
assertEquals(q.pollFirst(), r.pollFirst()); |
539 |
} |
540 |
|
541 |
/** |
542 |
* subSet returns set with keys in requested range |
543 |
*/ |
544 |
public void testSubSetContents() { |
545 |
TreeSet set = set5(); |
546 |
SortedSet sm = set.subSet(two, four); |
547 |
assertEquals(two, sm.first()); |
548 |
assertEquals(three, sm.last()); |
549 |
assertEquals(2, sm.size()); |
550 |
assertFalse(sm.contains(one)); |
551 |
assertTrue(sm.contains(two)); |
552 |
assertTrue(sm.contains(three)); |
553 |
assertFalse(sm.contains(four)); |
554 |
assertFalse(sm.contains(five)); |
555 |
Iterator i = sm.iterator(); |
556 |
Object k; |
557 |
k = (Integer)(i.next()); |
558 |
assertEquals(two, k); |
559 |
k = (Integer)(i.next()); |
560 |
assertEquals(three, k); |
561 |
assertFalse(i.hasNext()); |
562 |
Iterator j = sm.iterator(); |
563 |
j.next(); |
564 |
j.remove(); |
565 |
assertFalse(set.contains(two)); |
566 |
assertEquals(4, set.size()); |
567 |
assertEquals(1, sm.size()); |
568 |
assertEquals(three, sm.first()); |
569 |
assertEquals(three, sm.last()); |
570 |
assertTrue(sm.remove(three)); |
571 |
assertTrue(sm.isEmpty()); |
572 |
assertEquals(3, set.size()); |
573 |
} |
574 |
|
575 |
public void testSubSetContents2() { |
576 |
TreeSet set = set5(); |
577 |
SortedSet sm = set.subSet(two, three); |
578 |
assertEquals(1, sm.size()); |
579 |
assertEquals(two, sm.first()); |
580 |
assertEquals(two, sm.last()); |
581 |
assertFalse(sm.contains(one)); |
582 |
assertTrue(sm.contains(two)); |
583 |
assertFalse(sm.contains(three)); |
584 |
assertFalse(sm.contains(four)); |
585 |
assertFalse(sm.contains(five)); |
586 |
Iterator i = sm.iterator(); |
587 |
Object k; |
588 |
k = (Integer)(i.next()); |
589 |
assertEquals(two, k); |
590 |
assertFalse(i.hasNext()); |
591 |
Iterator j = sm.iterator(); |
592 |
j.next(); |
593 |
j.remove(); |
594 |
assertFalse(set.contains(two)); |
595 |
assertEquals(4, set.size()); |
596 |
assertEquals(0, sm.size()); |
597 |
assertTrue(sm.isEmpty()); |
598 |
assertFalse(sm.remove(three)); |
599 |
assertEquals(4, set.size()); |
600 |
} |
601 |
|
602 |
/** |
603 |
* headSet returns set with keys in requested range |
604 |
*/ |
605 |
public void testHeadSetContents() { |
606 |
TreeSet set = set5(); |
607 |
SortedSet sm = set.headSet(four); |
608 |
assertTrue(sm.contains(one)); |
609 |
assertTrue(sm.contains(two)); |
610 |
assertTrue(sm.contains(three)); |
611 |
assertFalse(sm.contains(four)); |
612 |
assertFalse(sm.contains(five)); |
613 |
Iterator i = sm.iterator(); |
614 |
Object k; |
615 |
k = (Integer)(i.next()); |
616 |
assertEquals(one, k); |
617 |
k = (Integer)(i.next()); |
618 |
assertEquals(two, k); |
619 |
k = (Integer)(i.next()); |
620 |
assertEquals(three, k); |
621 |
assertFalse(i.hasNext()); |
622 |
sm.clear(); |
623 |
assertTrue(sm.isEmpty()); |
624 |
assertEquals(2, set.size()); |
625 |
assertEquals(four, set.first()); |
626 |
} |
627 |
|
628 |
/** |
629 |
* tailSet returns set with keys in requested range |
630 |
*/ |
631 |
public void testTailSetContents() { |
632 |
TreeSet set = set5(); |
633 |
SortedSet sm = set.tailSet(two); |
634 |
assertFalse(sm.contains(one)); |
635 |
assertTrue(sm.contains(two)); |
636 |
assertTrue(sm.contains(three)); |
637 |
assertTrue(sm.contains(four)); |
638 |
assertTrue(sm.contains(five)); |
639 |
Iterator i = sm.iterator(); |
640 |
Object k; |
641 |
k = (Integer)(i.next()); |
642 |
assertEquals(two, k); |
643 |
k = (Integer)(i.next()); |
644 |
assertEquals(three, k); |
645 |
k = (Integer)(i.next()); |
646 |
assertEquals(four, k); |
647 |
k = (Integer)(i.next()); |
648 |
assertEquals(five, k); |
649 |
assertFalse(i.hasNext()); |
650 |
|
651 |
SortedSet ssm = sm.tailSet(four); |
652 |
assertEquals(four, ssm.first()); |
653 |
assertEquals(five, ssm.last()); |
654 |
assertTrue(ssm.remove(four)); |
655 |
assertEquals(1, ssm.size()); |
656 |
assertEquals(3, sm.size()); |
657 |
assertEquals(4, set.size()); |
658 |
} |
659 |
|
660 |
Random rnd = new Random(666); |
661 |
BitSet bs; |
662 |
|
663 |
/** |
664 |
* Subsets of subsets subdivide correctly |
665 |
*/ |
666 |
public void testRecursiveSubSets() throws Exception { |
667 |
int setSize = 1000; |
668 |
Class cl = TreeSet.class; |
669 |
|
670 |
NavigableSet<Integer> set = newSet(cl); |
671 |
bs = new BitSet(setSize); |
672 |
|
673 |
populate(set, setSize); |
674 |
check(set, 0, setSize - 1, true); |
675 |
check(set.descendingSet(), 0, setSize - 1, false); |
676 |
|
677 |
mutateSet(set, 0, setSize - 1); |
678 |
check(set, 0, setSize - 1, true); |
679 |
check(set.descendingSet(), 0, setSize - 1, false); |
680 |
|
681 |
bashSubSet(set.subSet(0, true, setSize, false), |
682 |
0, setSize - 1, true); |
683 |
} |
684 |
|
685 |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
686 |
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
687 |
assertEquals(result.size(), 0); |
688 |
assertFalse(result.iterator().hasNext()); |
689 |
return result; |
690 |
} |
691 |
|
692 |
void populate(NavigableSet<Integer> set, int limit) { |
693 |
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
694 |
int element = rnd.nextInt(limit); |
695 |
put(set, element); |
696 |
} |
697 |
} |
698 |
|
699 |
void mutateSet(NavigableSet<Integer> set, int min, int max) { |
700 |
int size = set.size(); |
701 |
int rangeSize = max - min + 1; |
702 |
|
703 |
// Remove a bunch of entries directly |
704 |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
705 |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
706 |
} |
707 |
|
708 |
// Remove a bunch of entries with iterator |
709 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
710 |
if (rnd.nextBoolean()) { |
711 |
bs.clear(it.next()); |
712 |
it.remove(); |
713 |
} |
714 |
} |
715 |
|
716 |
// Add entries till we're back to original size |
717 |
while (set.size() < size) { |
718 |
int element = min + rnd.nextInt(rangeSize); |
719 |
assertTrue(element >= min && element<= max); |
720 |
put(set, element); |
721 |
} |
722 |
} |
723 |
|
724 |
void mutateSubSet(NavigableSet<Integer> set, int min, int max) { |
725 |
int size = set.size(); |
726 |
int rangeSize = max - min + 1; |
727 |
|
728 |
// Remove a bunch of entries directly |
729 |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
730 |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
731 |
} |
732 |
|
733 |
// Remove a bunch of entries with iterator |
734 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
735 |
if (rnd.nextBoolean()) { |
736 |
bs.clear(it.next()); |
737 |
it.remove(); |
738 |
} |
739 |
} |
740 |
|
741 |
// Add entries till we're back to original size |
742 |
while (set.size() < size) { |
743 |
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
744 |
if (element >= min && element<= max) { |
745 |
put(set, element); |
746 |
} else { |
747 |
try { |
748 |
set.add(element); |
749 |
shouldThrow(); |
750 |
} catch (IllegalArgumentException success) {} |
751 |
} |
752 |
} |
753 |
} |
754 |
|
755 |
void put(NavigableSet<Integer> set, int element) { |
756 |
if (set.add(element)) |
757 |
bs.set(element); |
758 |
} |
759 |
|
760 |
void remove(NavigableSet<Integer> set, int element) { |
761 |
if (set.remove(element)) |
762 |
bs.clear(element); |
763 |
} |
764 |
|
765 |
void bashSubSet(NavigableSet<Integer> set, |
766 |
int min, int max, boolean ascending) { |
767 |
check(set, min, max, ascending); |
768 |
check(set.descendingSet(), min, max, !ascending); |
769 |
|
770 |
mutateSubSet(set, min, max); |
771 |
check(set, min, max, ascending); |
772 |
check(set.descendingSet(), min, max, !ascending); |
773 |
|
774 |
// Recurse |
775 |
if (max - min < 2) |
776 |
return; |
777 |
int midPoint = (min + max) / 2; |
778 |
|
779 |
// headSet - pick direction and endpoint inclusion randomly |
780 |
boolean incl = rnd.nextBoolean(); |
781 |
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
782 |
if (ascending) { |
783 |
if (rnd.nextBoolean()) |
784 |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
785 |
else |
786 |
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
787 |
false); |
788 |
} else { |
789 |
if (rnd.nextBoolean()) |
790 |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false); |
791 |
else |
792 |
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
793 |
true); |
794 |
} |
795 |
|
796 |
// tailSet - pick direction and endpoint inclusion randomly |
797 |
incl = rnd.nextBoolean(); |
798 |
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
799 |
if (ascending) { |
800 |
if (rnd.nextBoolean()) |
801 |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
802 |
else |
803 |
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
804 |
false); |
805 |
} else { |
806 |
if (rnd.nextBoolean()) { |
807 |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false); |
808 |
} else { |
809 |
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
810 |
true); |
811 |
} |
812 |
} |
813 |
|
814 |
// subSet - pick direction and endpoint inclusion randomly |
815 |
int rangeSize = max - min + 1; |
816 |
int[] endpoints = new int[2]; |
817 |
endpoints[0] = min + rnd.nextInt(rangeSize); |
818 |
endpoints[1] = min + rnd.nextInt(rangeSize); |
819 |
Arrays.sort(endpoints); |
820 |
boolean lowIncl = rnd.nextBoolean(); |
821 |
boolean highIncl = rnd.nextBoolean(); |
822 |
if (ascending) { |
823 |
NavigableSet<Integer> sm = set.subSet( |
824 |
endpoints[0], lowIncl, endpoints[1], highIncl); |
825 |
if (rnd.nextBoolean()) |
826 |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
827 |
endpoints[1] - (highIncl ? 0 : 1), true); |
828 |
else |
829 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
830 |
endpoints[1] - (highIncl ? 0 : 1), false); |
831 |
} else { |
832 |
NavigableSet<Integer> sm = set.subSet( |
833 |
endpoints[1], highIncl, endpoints[0], lowIncl); |
834 |
if (rnd.nextBoolean()) |
835 |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
836 |
endpoints[1] - (highIncl ? 0 : 1), false); |
837 |
else |
838 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
839 |
endpoints[1] - (highIncl ? 0 : 1), true); |
840 |
} |
841 |
} |
842 |
|
843 |
/** |
844 |
* min and max are both inclusive. If max < min, interval is empty. |
845 |
*/ |
846 |
void check(NavigableSet<Integer> set, |
847 |
final int min, final int max, final boolean ascending) { |
848 |
class ReferenceSet { |
849 |
int lower(int element) { |
850 |
return ascending ? |
851 |
lowerAscending(element) : higherAscending(element); |
852 |
} |
853 |
int floor(int element) { |
854 |
return ascending ? |
855 |
floorAscending(element) : ceilingAscending(element); |
856 |
} |
857 |
int ceiling(int element) { |
858 |
return ascending ? |
859 |
ceilingAscending(element) : floorAscending(element); |
860 |
} |
861 |
int higher(int element) { |
862 |
return ascending ? |
863 |
higherAscending(element) : lowerAscending(element); |
864 |
} |
865 |
int first() { |
866 |
return ascending ? firstAscending() : lastAscending(); |
867 |
} |
868 |
int last() { |
869 |
return ascending ? lastAscending() : firstAscending(); |
870 |
} |
871 |
int lowerAscending(int element) { |
872 |
return floorAscending(element - 1); |
873 |
} |
874 |
int floorAscending(int element) { |
875 |
if (element < min) |
876 |
return -1; |
877 |
else if (element > max) |
878 |
element = max; |
879 |
|
880 |
// BitSet should support this! Test would run much faster |
881 |
while (element >= min) { |
882 |
if (bs.get(element)) |
883 |
return(element); |
884 |
element--; |
885 |
} |
886 |
return -1; |
887 |
} |
888 |
int ceilingAscending(int element) { |
889 |
if (element < min) |
890 |
element = min; |
891 |
else if (element > max) |
892 |
return -1; |
893 |
int result = bs.nextSetBit(element); |
894 |
return result > max ? -1 : result; |
895 |
} |
896 |
int higherAscending(int element) { |
897 |
return ceilingAscending(element + 1); |
898 |
} |
899 |
private int firstAscending() { |
900 |
int result = ceilingAscending(min); |
901 |
return result > max ? -1 : result; |
902 |
} |
903 |
private int lastAscending() { |
904 |
int result = floorAscending(max); |
905 |
return result < min ? -1 : result; |
906 |
} |
907 |
} |
908 |
ReferenceSet rs = new ReferenceSet(); |
909 |
|
910 |
// Test contents using containsElement |
911 |
int size = 0; |
912 |
for (int i = min; i <= max; i++) { |
913 |
boolean bsContainsI = bs.get(i); |
914 |
assertEquals(bsContainsI, set.contains(i)); |
915 |
if (bsContainsI) |
916 |
size++; |
917 |
} |
918 |
assertEquals(set.size(), size); |
919 |
|
920 |
// Test contents using contains elementSet iterator |
921 |
int size2 = 0; |
922 |
int previousElement = -1; |
923 |
for (int element : set) { |
924 |
assertTrue(bs.get(element)); |
925 |
size2++; |
926 |
assertTrue(previousElement < 0 || (ascending ? |
927 |
element - previousElement > 0 : element - previousElement < 0)); |
928 |
previousElement = element; |
929 |
} |
930 |
assertEquals(size2, size); |
931 |
|
932 |
// Test navigation ops |
933 |
for (int element = min - 1; element <= max + 1; element++) { |
934 |
assertEq(set.lower(element), rs.lower(element)); |
935 |
assertEq(set.floor(element), rs.floor(element)); |
936 |
assertEq(set.higher(element), rs.higher(element)); |
937 |
assertEq(set.ceiling(element), rs.ceiling(element)); |
938 |
} |
939 |
|
940 |
// Test extrema |
941 |
if (set.size() != 0) { |
942 |
assertEq(set.first(), rs.first()); |
943 |
assertEq(set.last(), rs.last()); |
944 |
} else { |
945 |
assertEq(rs.first(), -1); |
946 |
assertEq(rs.last(), -1); |
947 |
try { |
948 |
set.first(); |
949 |
shouldThrow(); |
950 |
} catch (NoSuchElementException success) {} |
951 |
try { |
952 |
set.last(); |
953 |
shouldThrow(); |
954 |
} catch (NoSuchElementException success) {} |
955 |
} |
956 |
} |
957 |
|
958 |
static void assertEq(Integer i, int j) { |
959 |
if (i == null) |
960 |
assertEquals(j, -1); |
961 |
else |
962 |
assertEquals((int) i, j); |
963 |
} |
964 |
|
965 |
static boolean eq(Integer i, int j) { |
966 |
return i == null ? j == -1 : i == j; |
967 |
} |
968 |
|
969 |
} |