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