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