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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
11 |
|
|
12 |
|
public class TreeSubSetTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
junit.textui.TestRunner.run(suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
< |
return new TestSuite(TreeSubSetTest.class); |
17 |
> |
return new TestSuite(TreeSubSetTest.class); |
18 |
|
} |
19 |
|
|
20 |
< |
static class MyReverseComparator implements Comparator { |
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; |
22 |
> |
return ((Comparable)y).compareTo(x); |
23 |
|
} |
24 |
|
} |
25 |
|
|
27 |
|
* Create a set of given size containing consecutive |
28 |
|
* Integers 0 ... n. |
29 |
|
*/ |
30 |
< |
private NavigableSet populatedSet(int n) { |
31 |
< |
TreeSet q = new TreeSet(); |
30 |
> |
private NavigableSet<Integer> populatedSet(int n) { |
31 |
> |
TreeSet<Integer> q = new TreeSet<Integer>(); |
32 |
|
assertTrue(q.isEmpty()); |
33 |
|
|
34 |
< |
for(int i = n-1; i >= 0; i-=2) |
35 |
< |
assertTrue(q.add(new Integer(i))); |
36 |
< |
for(int i = (n & 1); i < n; i+=2) |
37 |
< |
assertTrue(q.add(new Integer(i))); |
34 |
> |
for (int i = n-1; i >= 0; i-=2) |
35 |
> |
assertTrue(q.add(new Integer(i))); |
36 |
> |
for (int i = (n & 1); i < n; i+=2) |
37 |
> |
assertTrue(q.add(new Integer(i))); |
38 |
|
assertTrue(q.add(new Integer(-n))); |
39 |
|
assertTrue(q.add(new Integer(n))); |
40 |
< |
NavigableSet s = q.navigableSubSet(new Integer(0), true, new Integer(n), false); |
40 |
> |
NavigableSet s = q.subSet(new Integer(0), true, new Integer(n), false); |
41 |
|
assertFalse(s.isEmpty()); |
42 |
< |
assertEquals(n, s.size()); |
42 |
> |
assertEquals(n, s.size()); |
43 |
|
return s; |
44 |
|
} |
45 |
|
|
56 |
|
q.add(five); |
57 |
|
q.add(zero); |
58 |
|
q.add(seven); |
59 |
< |
NavigableSet s = q.navigableSubSet(one, true, seven, false); |
60 |
< |
assertEquals(5, s.size()); |
59 |
> |
NavigableSet s = q.subSet(one, true, seven, false); |
60 |
> |
assertEquals(5, s.size()); |
61 |
> |
return s; |
62 |
> |
} |
63 |
> |
|
64 |
> |
private NavigableSet dset5() { |
65 |
> |
TreeSet q = new TreeSet(); |
66 |
> |
assertTrue(q.isEmpty()); |
67 |
> |
q.add(m1); |
68 |
> |
q.add(m2); |
69 |
> |
q.add(m3); |
70 |
> |
q.add(m4); |
71 |
> |
q.add(m5); |
72 |
> |
NavigableSet s = q.descendingSet(); |
73 |
> |
assertEquals(5, s.size()); |
74 |
|
return s; |
75 |
|
} |
76 |
|
|
77 |
< |
private static NavigableSet set0() { |
78 |
< |
TreeSet set = new TreeSet(); |
77 |
> |
private static NavigableSet set0() { |
78 |
> |
TreeSet set = new TreeSet(); |
79 |
|
assertTrue(set.isEmpty()); |
80 |
< |
return set.navigableTailSet(m1, false); |
80 |
> |
return set.tailSet(m1, false); |
81 |
|
} |
82 |
< |
|
82 |
> |
|
83 |
> |
private static NavigableSet dset0() { |
84 |
> |
TreeSet set = new TreeSet(); |
85 |
> |
assertTrue(set.isEmpty()); |
86 |
> |
return set; |
87 |
> |
} |
88 |
> |
|
89 |
|
/** |
90 |
|
* A new set has unbounded capacity |
91 |
|
*/ |
100 |
|
public void testEmpty() { |
101 |
|
NavigableSet q = set0(); |
102 |
|
assertTrue(q.isEmpty()); |
103 |
< |
q.add(new Integer(1)); |
103 |
> |
assertTrue(q.add(new Integer(1))); |
104 |
|
assertFalse(q.isEmpty()); |
105 |
< |
q.add(new Integer(2)); |
105 |
> |
assertTrue(q.add(new Integer(2))); |
106 |
|
q.pollFirst(); |
107 |
|
q.pollFirst(); |
108 |
|
assertTrue(q.isEmpty()); |
127 |
|
* add(null) throws NPE |
128 |
|
*/ |
129 |
|
public void testAddNull() { |
130 |
< |
try { |
130 |
> |
try { |
131 |
|
NavigableSet q = set0(); |
132 |
|
q.add(null); |
133 |
|
shouldThrow(); |
134 |
< |
} catch (NullPointerException success) { } |
134 |
> |
} catch (NullPointerException success) {} |
135 |
|
} |
136 |
|
|
137 |
|
/** |
161 |
|
q.add(new Object()); |
162 |
|
q.add(new Object()); |
163 |
|
shouldThrow(); |
164 |
< |
} |
150 |
< |
catch(ClassCastException success) {} |
164 |
> |
} catch (ClassCastException success) {} |
165 |
|
} |
166 |
|
|
167 |
|
|
173 |
|
NavigableSet q = set0(); |
174 |
|
q.addAll(null); |
175 |
|
shouldThrow(); |
176 |
< |
} |
163 |
< |
catch (NullPointerException success) {} |
176 |
> |
} catch (NullPointerException success) {} |
177 |
|
} |
178 |
+ |
|
179 |
|
/** |
180 |
|
* addAll of a collection with null elements throws NPE |
181 |
|
*/ |
185 |
|
Integer[] ints = new Integer[SIZE]; |
186 |
|
q.addAll(Arrays.asList(ints)); |
187 |
|
shouldThrow(); |
188 |
< |
} |
175 |
< |
catch (NullPointerException success) {} |
188 |
> |
} catch (NullPointerException success) {} |
189 |
|
} |
190 |
+ |
|
191 |
|
/** |
192 |
|
* addAll of a collection with any null elements throws NPE after |
193 |
|
* possibly adding some elements |
200 |
|
ints[i] = new Integer(i+SIZE); |
201 |
|
q.addAll(Arrays.asList(ints)); |
202 |
|
shouldThrow(); |
203 |
< |
} |
190 |
< |
catch (NullPointerException success) {} |
203 |
> |
} catch (NullPointerException success) {} |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
|
* Set contains all elements of successful addAll |
208 |
|
*/ |
209 |
|
public void testAddAll5() { |
210 |
< |
try { |
211 |
< |
Integer[] empty = new Integer[0]; |
212 |
< |
Integer[] ints = new Integer[SIZE]; |
213 |
< |
for (int i = 0; i < SIZE; ++i) |
214 |
< |
ints[i] = new Integer(SIZE-1- i); |
215 |
< |
NavigableSet q = set0(); |
216 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
217 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
218 |
< |
for (int i = 0; i < SIZE; ++i) |
206 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
207 |
< |
} |
208 |
< |
finally {} |
210 |
> |
Integer[] empty = new Integer[0]; |
211 |
> |
Integer[] ints = new Integer[SIZE]; |
212 |
> |
for (int i = 0; i < SIZE; ++i) |
213 |
> |
ints[i] = new Integer(SIZE-1- i); |
214 |
> |
NavigableSet q = set0(); |
215 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
216 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
217 |
> |
for (int i = 0; i < SIZE; ++i) |
218 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
219 |
|
} |
220 |
|
|
221 |
|
/** |
224 |
|
public void testPoll() { |
225 |
|
NavigableSet q = populatedSet(SIZE); |
226 |
|
for (int i = 0; i < SIZE; ++i) { |
227 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
227 |
> |
assertEquals(i, q.pollFirst()); |
228 |
|
} |
229 |
< |
assertNull(q.pollFirst()); |
229 |
> |
assertNull(q.pollFirst()); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
235 |
|
public void testRemoveElement() { |
236 |
|
NavigableSet q = populatedSet(SIZE); |
237 |
|
for (int i = 1; i < SIZE; i+=2) { |
238 |
< |
assertTrue(q.remove(new Integer(i))); |
238 |
> |
assertTrue(q.contains(i)); |
239 |
> |
assertTrue(q.remove(i)); |
240 |
> |
assertFalse(q.contains(i)); |
241 |
> |
assertTrue(q.contains(i-1)); |
242 |
|
} |
243 |
|
for (int i = 0; i < SIZE; i+=2) { |
244 |
< |
assertTrue(q.remove(new Integer(i))); |
245 |
< |
assertFalse(q.remove(new Integer(i+1))); |
244 |
> |
assertTrue(q.contains(i)); |
245 |
> |
assertTrue(q.remove(i)); |
246 |
> |
assertFalse(q.contains(i)); |
247 |
> |
assertFalse(q.remove(i+1)); |
248 |
> |
assertFalse(q.contains(i+1)); |
249 |
|
} |
250 |
|
assertTrue(q.isEmpty()); |
251 |
|
} |
252 |
< |
|
252 |
> |
|
253 |
|
/** |
254 |
|
* contains(x) reports true when elements added but not yet removed |
255 |
|
*/ |
270 |
|
q.clear(); |
271 |
|
assertTrue(q.isEmpty()); |
272 |
|
assertEquals(0, q.size()); |
273 |
< |
q.add(new Integer(1)); |
273 |
> |
assertTrue(q.add(new Integer(1))); |
274 |
|
assertFalse(q.isEmpty()); |
275 |
|
q.clear(); |
276 |
|
assertTrue(q.isEmpty()); |
325 |
|
} |
326 |
|
} |
327 |
|
|
328 |
< |
|
328 |
> |
|
329 |
|
|
330 |
|
/** |
331 |
|
* lower returns preceding element |
343 |
|
|
344 |
|
Object e4 = q.lower(zero); |
345 |
|
assertNull(e4); |
330 |
– |
|
346 |
|
} |
347 |
|
|
348 |
|
/** |
361 |
|
|
362 |
|
Object e4 = q.higher(six); |
363 |
|
assertNull(e4); |
349 |
– |
|
364 |
|
} |
365 |
|
|
366 |
|
/** |
379 |
|
|
380 |
|
Object e4 = q.floor(zero); |
381 |
|
assertNull(e4); |
368 |
– |
|
382 |
|
} |
383 |
|
|
384 |
|
/** |
397 |
|
|
398 |
|
Object e4 = q.ceiling(six); |
399 |
|
assertNull(e4); |
387 |
– |
|
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
< |
* toArray contains all elements |
403 |
> |
* toArray contains all elements in sorted order |
404 |
|
*/ |
405 |
|
public void testToArray() { |
406 |
|
NavigableSet q = populatedSet(SIZE); |
407 |
< |
Object[] o = q.toArray(); |
408 |
< |
Arrays.sort(o); |
409 |
< |
for(int i = 0; i < o.length; i++) |
398 |
< |
assertEquals(o[i], q.pollFirst()); |
407 |
> |
Object[] o = q.toArray(); |
408 |
> |
for (int i = 0; i < o.length; i++) |
409 |
> |
assertSame(o[i], q.pollFirst()); |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
< |
* toArray(a) contains all elements |
413 |
> |
* toArray(a) contains all elements in sorted order |
414 |
|
*/ |
415 |
|
public void testToArray2() { |
416 |
< |
NavigableSet q = populatedSet(SIZE); |
417 |
< |
Integer[] ints = new Integer[SIZE]; |
418 |
< |
ints = (Integer[])q.toArray(ints); |
419 |
< |
Arrays.sort(ints); |
420 |
< |
for(int i = 0; i < ints.length; i++) |
421 |
< |
assertEquals(ints[i], q.pollFirst()); |
416 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
417 |
> |
Integer[] ints = new Integer[SIZE]; |
418 |
> |
Integer[] array = q.toArray(ints); |
419 |
> |
assertSame(ints, array); |
420 |
> |
for (int i = 0; i < ints.length; i++) |
421 |
> |
assertSame(ints[i], q.pollFirst()); |
422 |
|
} |
423 |
< |
|
423 |
> |
|
424 |
|
/** |
425 |
|
* iterator iterates through all elements |
426 |
|
*/ |
427 |
|
public void testIterator() { |
428 |
|
NavigableSet q = populatedSet(SIZE); |
429 |
|
int i = 0; |
430 |
< |
Iterator it = q.iterator(); |
431 |
< |
while(it.hasNext()) { |
430 |
> |
Iterator it = q.iterator(); |
431 |
> |
while (it.hasNext()) { |
432 |
|
assertTrue(q.contains(it.next())); |
433 |
|
++i; |
434 |
|
} |
441 |
|
public void testEmptyIterator() { |
442 |
|
NavigableSet q = set0(); |
443 |
|
int i = 0; |
444 |
< |
Iterator it = q.iterator(); |
445 |
< |
while(it.hasNext()) { |
444 |
> |
Iterator it = q.iterator(); |
445 |
> |
while (it.hasNext()) { |
446 |
|
assertTrue(q.contains(it.next())); |
447 |
|
++i; |
448 |
|
} |
452 |
|
/** |
453 |
|
* iterator.remove removes current element |
454 |
|
*/ |
455 |
< |
public void testIteratorRemove () { |
455 |
> |
public void testIteratorRemove() { |
456 |
|
final NavigableSet q = set0(); |
457 |
|
q.add(new Integer(2)); |
458 |
|
q.add(new Integer(1)); |
463 |
|
it.remove(); |
464 |
|
|
465 |
|
it = q.iterator(); |
466 |
< |
assertEquals(it.next(), new Integer(2)); |
467 |
< |
assertEquals(it.next(), new Integer(3)); |
466 |
> |
assertEquals(it.next(), 2); |
467 |
> |
assertEquals(it.next(), 3); |
468 |
|
assertFalse(it.hasNext()); |
469 |
|
} |
470 |
|
|
476 |
|
NavigableSet q = populatedSet(SIZE); |
477 |
|
String s = q.toString(); |
478 |
|
for (int i = 0; i < SIZE; ++i) { |
479 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
479 |
> |
assertTrue(s.contains(String.valueOf(i))); |
480 |
|
} |
481 |
< |
} |
481 |
> |
} |
482 |
|
|
483 |
|
/** |
484 |
< |
* A deserialized serialized set has same elements |
484 |
> |
* A deserialized serialized set has same elements |
485 |
|
*/ |
486 |
< |
public void testSerialization() { |
486 |
> |
public void testSerialization() throws Exception { |
487 |
|
NavigableSet q = populatedSet(SIZE); |
488 |
< |
try { |
489 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
490 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
491 |
< |
out.writeObject(q); |
492 |
< |
out.close(); |
493 |
< |
|
494 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
495 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
496 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
497 |
< |
assertEquals(q.size(), r.size()); |
498 |
< |
while (!q.isEmpty()) |
488 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
489 |
< |
} catch(Exception e){ |
490 |
< |
e.printStackTrace(); |
491 |
< |
unexpectedException(); |
492 |
< |
} |
488 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
489 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
490 |
> |
out.writeObject(q); |
491 |
> |
out.close(); |
492 |
> |
|
493 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
494 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
495 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
496 |
> |
assertEquals(q.size(), r.size()); |
497 |
> |
while (!q.isEmpty()) |
498 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
615 |
|
assertEquals(1, ssm.size()); |
616 |
|
assertEquals(3, sm.size()); |
617 |
|
assertEquals(4, set.size()); |
618 |
+ |
} |
619 |
+ |
|
620 |
+ |
/** |
621 |
+ |
* size changes when elements added and removed |
622 |
+ |
*/ |
623 |
+ |
public void testDescendingSize() { |
624 |
+ |
NavigableSet q = populatedSet(SIZE); |
625 |
+ |
for (int i = 0; i < SIZE; ++i) { |
626 |
+ |
assertEquals(SIZE-i, q.size()); |
627 |
+ |
q.pollFirst(); |
628 |
+ |
} |
629 |
+ |
for (int i = 0; i < SIZE; ++i) { |
630 |
+ |
assertEquals(i, q.size()); |
631 |
+ |
q.add(new Integer(i)); |
632 |
+ |
} |
633 |
+ |
} |
634 |
+ |
|
635 |
+ |
/** |
636 |
+ |
* Add of comparable element succeeds |
637 |
+ |
*/ |
638 |
+ |
public void testDescendingAdd() { |
639 |
+ |
NavigableSet q = dset0(); |
640 |
+ |
assertTrue(q.add(m6)); |
641 |
+ |
} |
642 |
+ |
|
643 |
+ |
/** |
644 |
+ |
* Add of duplicate element fails |
645 |
+ |
*/ |
646 |
+ |
public void testDescendingAddDup() { |
647 |
+ |
NavigableSet q = dset0(); |
648 |
+ |
assertTrue(q.add(m6)); |
649 |
+ |
assertFalse(q.add(m6)); |
650 |
+ |
} |
651 |
+ |
|
652 |
+ |
/** |
653 |
+ |
* Add of non-Comparable throws CCE |
654 |
+ |
*/ |
655 |
+ |
public void testDescendingAddNonComparable() { |
656 |
+ |
try { |
657 |
+ |
NavigableSet q = dset0(); |
658 |
+ |
q.add(new Object()); |
659 |
+ |
q.add(new Object()); |
660 |
+ |
q.add(new Object()); |
661 |
+ |
shouldThrow(); |
662 |
+ |
} catch (ClassCastException success) {} |
663 |
+ |
} |
664 |
+ |
|
665 |
+ |
|
666 |
+ |
/** |
667 |
+ |
* addAll(null) throws NPE |
668 |
+ |
*/ |
669 |
+ |
public void testDescendingAddAll1() { |
670 |
+ |
try { |
671 |
+ |
NavigableSet q = dset0(); |
672 |
+ |
q.addAll(null); |
673 |
+ |
shouldThrow(); |
674 |
+ |
} catch (NullPointerException success) {} |
675 |
+ |
} |
676 |
+ |
|
677 |
+ |
/** |
678 |
+ |
* addAll of a collection with null elements throws NPE |
679 |
+ |
*/ |
680 |
+ |
public void testDescendingAddAll2() { |
681 |
+ |
try { |
682 |
+ |
NavigableSet q = dset0(); |
683 |
+ |
Integer[] ints = new Integer[SIZE]; |
684 |
+ |
q.addAll(Arrays.asList(ints)); |
685 |
+ |
shouldThrow(); |
686 |
+ |
} catch (NullPointerException success) {} |
687 |
+ |
} |
688 |
+ |
|
689 |
+ |
/** |
690 |
+ |
* addAll of a collection with any null elements throws NPE after |
691 |
+ |
* possibly adding some elements |
692 |
+ |
*/ |
693 |
+ |
public void testDescendingAddAll3() { |
694 |
+ |
try { |
695 |
+ |
NavigableSet q = dset0(); |
696 |
+ |
Integer[] ints = new Integer[SIZE]; |
697 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
698 |
+ |
ints[i] = new Integer(i+SIZE); |
699 |
+ |
q.addAll(Arrays.asList(ints)); |
700 |
+ |
shouldThrow(); |
701 |
+ |
} catch (NullPointerException success) {} |
702 |
+ |
} |
703 |
+ |
|
704 |
+ |
/** |
705 |
+ |
* Set contains all elements of successful addAll |
706 |
+ |
*/ |
707 |
+ |
public void testDescendingAddAll5() { |
708 |
+ |
Integer[] empty = new Integer[0]; |
709 |
+ |
Integer[] ints = new Integer[SIZE]; |
710 |
+ |
for (int i = 0; i < SIZE; ++i) |
711 |
+ |
ints[i] = new Integer(SIZE-1- i); |
712 |
+ |
NavigableSet q = dset0(); |
713 |
+ |
assertFalse(q.addAll(Arrays.asList(empty))); |
714 |
+ |
assertTrue(q.addAll(Arrays.asList(ints))); |
715 |
+ |
for (int i = 0; i < SIZE; ++i) |
716 |
+ |
assertEquals(new Integer(i), q.pollFirst()); |
717 |
+ |
} |
718 |
+ |
|
719 |
+ |
/** |
720 |
+ |
* poll succeeds unless empty |
721 |
+ |
*/ |
722 |
+ |
public void testDescendingPoll() { |
723 |
+ |
NavigableSet q = populatedSet(SIZE); |
724 |
+ |
for (int i = 0; i < SIZE; ++i) { |
725 |
+ |
assertEquals(i, q.pollFirst()); |
726 |
+ |
} |
727 |
+ |
assertNull(q.pollFirst()); |
728 |
+ |
} |
729 |
+ |
|
730 |
+ |
/** |
731 |
+ |
* remove(x) removes x and returns true if present |
732 |
+ |
*/ |
733 |
+ |
public void testDescendingRemoveElement() { |
734 |
+ |
NavigableSet q = populatedSet(SIZE); |
735 |
+ |
for (int i = 1; i < SIZE; i+=2) { |
736 |
+ |
assertTrue(q.remove(new Integer(i))); |
737 |
+ |
} |
738 |
+ |
for (int i = 0; i < SIZE; i+=2) { |
739 |
+ |
assertTrue(q.remove(new Integer(i))); |
740 |
+ |
assertFalse(q.remove(new Integer(i+1))); |
741 |
+ |
} |
742 |
+ |
assertTrue(q.isEmpty()); |
743 |
+ |
} |
744 |
+ |
|
745 |
+ |
/** |
746 |
+ |
* contains(x) reports true when elements added but not yet removed |
747 |
+ |
*/ |
748 |
+ |
public void testDescendingContains() { |
749 |
+ |
NavigableSet q = populatedSet(SIZE); |
750 |
+ |
for (int i = 0; i < SIZE; ++i) { |
751 |
+ |
assertTrue(q.contains(new Integer(i))); |
752 |
+ |
q.pollFirst(); |
753 |
+ |
assertFalse(q.contains(new Integer(i))); |
754 |
+ |
} |
755 |
+ |
} |
756 |
+ |
|
757 |
+ |
/** |
758 |
+ |
* clear removes all elements |
759 |
+ |
*/ |
760 |
+ |
public void testDescendingClear() { |
761 |
+ |
NavigableSet q = populatedSet(SIZE); |
762 |
+ |
q.clear(); |
763 |
+ |
assertTrue(q.isEmpty()); |
764 |
+ |
assertEquals(0, q.size()); |
765 |
+ |
assertTrue(q.add(new Integer(1))); |
766 |
+ |
assertFalse(q.isEmpty()); |
767 |
+ |
q.clear(); |
768 |
+ |
assertTrue(q.isEmpty()); |
769 |
+ |
} |
770 |
+ |
|
771 |
+ |
/** |
772 |
+ |
* containsAll(c) is true when c contains a subset of elements |
773 |
+ |
*/ |
774 |
+ |
public void testDescendingContainsAll() { |
775 |
+ |
NavigableSet q = populatedSet(SIZE); |
776 |
+ |
NavigableSet p = dset0(); |
777 |
+ |
for (int i = 0; i < SIZE; ++i) { |
778 |
+ |
assertTrue(q.containsAll(p)); |
779 |
+ |
assertFalse(p.containsAll(q)); |
780 |
+ |
p.add(new Integer(i)); |
781 |
+ |
} |
782 |
+ |
assertTrue(p.containsAll(q)); |
783 |
+ |
} |
784 |
+ |
|
785 |
+ |
/** |
786 |
+ |
* retainAll(c) retains only those elements of c and reports true if changed |
787 |
+ |
*/ |
788 |
+ |
public void testDescendingRetainAll() { |
789 |
+ |
NavigableSet q = populatedSet(SIZE); |
790 |
+ |
NavigableSet p = populatedSet(SIZE); |
791 |
+ |
for (int i = 0; i < SIZE; ++i) { |
792 |
+ |
boolean changed = q.retainAll(p); |
793 |
+ |
if (i == 0) |
794 |
+ |
assertFalse(changed); |
795 |
+ |
else |
796 |
+ |
assertTrue(changed); |
797 |
+ |
|
798 |
+ |
assertTrue(q.containsAll(p)); |
799 |
+ |
assertEquals(SIZE-i, q.size()); |
800 |
+ |
p.pollFirst(); |
801 |
+ |
} |
802 |
+ |
} |
803 |
+ |
|
804 |
+ |
/** |
805 |
+ |
* removeAll(c) removes only those elements of c and reports true if changed |
806 |
+ |
*/ |
807 |
+ |
public void testDescendingRemoveAll() { |
808 |
+ |
for (int i = 1; i < SIZE; ++i) { |
809 |
+ |
NavigableSet q = populatedSet(SIZE); |
810 |
+ |
NavigableSet p = populatedSet(i); |
811 |
+ |
assertTrue(q.removeAll(p)); |
812 |
+ |
assertEquals(SIZE-i, q.size()); |
813 |
+ |
for (int j = 0; j < i; ++j) { |
814 |
+ |
Integer I = (Integer)(p.pollFirst()); |
815 |
+ |
assertFalse(q.contains(I)); |
816 |
+ |
} |
817 |
+ |
} |
818 |
+ |
} |
819 |
+ |
|
820 |
+ |
|
821 |
+ |
|
822 |
+ |
/** |
823 |
+ |
* lower returns preceding element |
824 |
+ |
*/ |
825 |
+ |
public void testDescendingLower() { |
826 |
+ |
NavigableSet q = dset5(); |
827 |
+ |
Object e1 = q.lower(m3); |
828 |
+ |
assertEquals(m2, e1); |
829 |
+ |
|
830 |
+ |
Object e2 = q.lower(m6); |
831 |
+ |
assertEquals(m5, e2); |
832 |
+ |
|
833 |
+ |
Object e3 = q.lower(m1); |
834 |
+ |
assertNull(e3); |
835 |
+ |
|
836 |
+ |
Object e4 = q.lower(zero); |
837 |
+ |
assertNull(e4); |
838 |
+ |
} |
839 |
+ |
|
840 |
+ |
/** |
841 |
+ |
* higher returns next element |
842 |
+ |
*/ |
843 |
+ |
public void testDescendingHigher() { |
844 |
+ |
NavigableSet q = dset5(); |
845 |
+ |
Object e1 = q.higher(m3); |
846 |
+ |
assertEquals(m4, e1); |
847 |
+ |
|
848 |
+ |
Object e2 = q.higher(zero); |
849 |
+ |
assertEquals(m1, e2); |
850 |
+ |
|
851 |
+ |
Object e3 = q.higher(m5); |
852 |
+ |
assertNull(e3); |
853 |
+ |
|
854 |
+ |
Object e4 = q.higher(m6); |
855 |
+ |
assertNull(e4); |
856 |
+ |
} |
857 |
+ |
|
858 |
+ |
/** |
859 |
+ |
* floor returns preceding element |
860 |
+ |
*/ |
861 |
+ |
public void testDescendingFloor() { |
862 |
+ |
NavigableSet q = dset5(); |
863 |
+ |
Object e1 = q.floor(m3); |
864 |
+ |
assertEquals(m3, e1); |
865 |
+ |
|
866 |
+ |
Object e2 = q.floor(m6); |
867 |
+ |
assertEquals(m5, e2); |
868 |
+ |
|
869 |
+ |
Object e3 = q.floor(m1); |
870 |
+ |
assertEquals(m1, e3); |
871 |
+ |
|
872 |
+ |
Object e4 = q.floor(zero); |
873 |
+ |
assertNull(e4); |
874 |
+ |
} |
875 |
+ |
|
876 |
+ |
/** |
877 |
+ |
* ceiling returns next element |
878 |
+ |
*/ |
879 |
+ |
public void testDescendingCeiling() { |
880 |
+ |
NavigableSet q = dset5(); |
881 |
+ |
Object e1 = q.ceiling(m3); |
882 |
+ |
assertEquals(m3, e1); |
883 |
+ |
|
884 |
+ |
Object e2 = q.ceiling(zero); |
885 |
+ |
assertEquals(m1, e2); |
886 |
+ |
|
887 |
+ |
Object e3 = q.ceiling(m5); |
888 |
+ |
assertEquals(m5, e3); |
889 |
+ |
|
890 |
+ |
Object e4 = q.ceiling(m6); |
891 |
+ |
assertNull(e4); |
892 |
+ |
} |
893 |
+ |
|
894 |
+ |
/** |
895 |
+ |
* toArray contains all elements |
896 |
+ |
*/ |
897 |
+ |
public void testDescendingToArray() { |
898 |
+ |
NavigableSet q = populatedSet(SIZE); |
899 |
+ |
Object[] o = q.toArray(); |
900 |
+ |
Arrays.sort(o); |
901 |
+ |
for (int i = 0; i < o.length; i++) |
902 |
+ |
assertEquals(o[i], q.pollFirst()); |
903 |
+ |
} |
904 |
+ |
|
905 |
+ |
/** |
906 |
+ |
* toArray(a) contains all elements |
907 |
+ |
*/ |
908 |
+ |
public void testDescendingToArray2() { |
909 |
+ |
NavigableSet q = populatedSet(SIZE); |
910 |
+ |
Integer[] ints = new Integer[SIZE]; |
911 |
+ |
assertSame(ints, q.toArray(ints)); |
912 |
+ |
Arrays.sort(ints); |
913 |
+ |
for (int i = 0; i < ints.length; i++) |
914 |
+ |
assertEquals(ints[i], q.pollFirst()); |
915 |
+ |
} |
916 |
+ |
|
917 |
+ |
/** |
918 |
+ |
* iterator iterates through all elements |
919 |
+ |
*/ |
920 |
+ |
public void testDescendingIterator() { |
921 |
+ |
NavigableSet q = populatedSet(SIZE); |
922 |
+ |
int i = 0; |
923 |
+ |
Iterator it = q.iterator(); |
924 |
+ |
while (it.hasNext()) { |
925 |
+ |
assertTrue(q.contains(it.next())); |
926 |
+ |
++i; |
927 |
+ |
} |
928 |
+ |
assertEquals(i, SIZE); |
929 |
+ |
} |
930 |
+ |
|
931 |
+ |
/** |
932 |
+ |
* iterator of empty set has no elements |
933 |
+ |
*/ |
934 |
+ |
public void testDescendingEmptyIterator() { |
935 |
+ |
NavigableSet q = dset0(); |
936 |
+ |
int i = 0; |
937 |
+ |
Iterator it = q.iterator(); |
938 |
+ |
while (it.hasNext()) { |
939 |
+ |
assertTrue(q.contains(it.next())); |
940 |
+ |
++i; |
941 |
+ |
} |
942 |
+ |
assertEquals(i, 0); |
943 |
+ |
} |
944 |
+ |
|
945 |
+ |
/** |
946 |
+ |
* iterator.remove removes current element |
947 |
+ |
*/ |
948 |
+ |
public void testDescendingIteratorRemove() { |
949 |
+ |
final NavigableSet q = dset0(); |
950 |
+ |
q.add(new Integer(2)); |
951 |
+ |
q.add(new Integer(1)); |
952 |
+ |
q.add(new Integer(3)); |
953 |
+ |
|
954 |
+ |
Iterator it = q.iterator(); |
955 |
+ |
it.next(); |
956 |
+ |
it.remove(); |
957 |
+ |
|
958 |
+ |
it = q.iterator(); |
959 |
+ |
assertEquals(it.next(), 2); |
960 |
+ |
assertEquals(it.next(), 3); |
961 |
+ |
assertFalse(it.hasNext()); |
962 |
+ |
} |
963 |
+ |
|
964 |
+ |
|
965 |
+ |
/** |
966 |
+ |
* toString contains toStrings of elements |
967 |
+ |
*/ |
968 |
+ |
public void testDescendingToString() { |
969 |
+ |
NavigableSet q = populatedSet(SIZE); |
970 |
+ |
String s = q.toString(); |
971 |
+ |
for (int i = 0; i < SIZE; ++i) { |
972 |
+ |
assertTrue(s.contains(String.valueOf(i))); |
973 |
+ |
} |
974 |
+ |
} |
975 |
+ |
|
976 |
+ |
/** |
977 |
+ |
* A deserialized serialized set has same elements |
978 |
+ |
*/ |
979 |
+ |
public void testDescendingSerialization() throws Exception { |
980 |
+ |
NavigableSet q = populatedSet(SIZE); |
981 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
982 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
983 |
+ |
out.writeObject(q); |
984 |
+ |
out.close(); |
985 |
+ |
|
986 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
987 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
988 |
+ |
NavigableSet r = (NavigableSet)in.readObject(); |
989 |
+ |
assertEquals(q.size(), r.size()); |
990 |
+ |
while (!q.isEmpty()) |
991 |
+ |
assertEquals(q.pollFirst(), r.pollFirst()); |
992 |
+ |
} |
993 |
+ |
|
994 |
+ |
/** |
995 |
+ |
* subSet returns set with keys in requested range |
996 |
+ |
*/ |
997 |
+ |
public void testDescendingSubSetContents() { |
998 |
+ |
NavigableSet set = dset5(); |
999 |
+ |
SortedSet sm = set.subSet(m2, m4); |
1000 |
+ |
assertEquals(m2, sm.first()); |
1001 |
+ |
assertEquals(m3, sm.last()); |
1002 |
+ |
assertEquals(2, sm.size()); |
1003 |
+ |
assertFalse(sm.contains(m1)); |
1004 |
+ |
assertTrue(sm.contains(m2)); |
1005 |
+ |
assertTrue(sm.contains(m3)); |
1006 |
+ |
assertFalse(sm.contains(m4)); |
1007 |
+ |
assertFalse(sm.contains(m5)); |
1008 |
+ |
Iterator i = sm.iterator(); |
1009 |
+ |
Object k; |
1010 |
+ |
k = (Integer)(i.next()); |
1011 |
+ |
assertEquals(m2, k); |
1012 |
+ |
k = (Integer)(i.next()); |
1013 |
+ |
assertEquals(m3, k); |
1014 |
+ |
assertFalse(i.hasNext()); |
1015 |
+ |
Iterator j = sm.iterator(); |
1016 |
+ |
j.next(); |
1017 |
+ |
j.remove(); |
1018 |
+ |
assertFalse(set.contains(m2)); |
1019 |
+ |
assertEquals(4, set.size()); |
1020 |
+ |
assertEquals(1, sm.size()); |
1021 |
+ |
assertEquals(m3, sm.first()); |
1022 |
+ |
assertEquals(m3, sm.last()); |
1023 |
+ |
assertTrue(sm.remove(m3)); |
1024 |
+ |
assertTrue(sm.isEmpty()); |
1025 |
+ |
assertEquals(3, set.size()); |
1026 |
+ |
} |
1027 |
+ |
|
1028 |
+ |
public void testDescendingSubSetContents2() { |
1029 |
+ |
NavigableSet set = dset5(); |
1030 |
+ |
SortedSet sm = set.subSet(m2, m3); |
1031 |
+ |
assertEquals(1, sm.size()); |
1032 |
+ |
assertEquals(m2, sm.first()); |
1033 |
+ |
assertEquals(m2, sm.last()); |
1034 |
+ |
assertFalse(sm.contains(m1)); |
1035 |
+ |
assertTrue(sm.contains(m2)); |
1036 |
+ |
assertFalse(sm.contains(m3)); |
1037 |
+ |
assertFalse(sm.contains(m4)); |
1038 |
+ |
assertFalse(sm.contains(m5)); |
1039 |
+ |
Iterator i = sm.iterator(); |
1040 |
+ |
Object k; |
1041 |
+ |
k = (Integer)(i.next()); |
1042 |
+ |
assertEquals(m2, k); |
1043 |
+ |
assertFalse(i.hasNext()); |
1044 |
+ |
Iterator j = sm.iterator(); |
1045 |
+ |
j.next(); |
1046 |
+ |
j.remove(); |
1047 |
+ |
assertFalse(set.contains(m2)); |
1048 |
+ |
assertEquals(4, set.size()); |
1049 |
+ |
assertEquals(0, sm.size()); |
1050 |
+ |
assertTrue(sm.isEmpty()); |
1051 |
+ |
assertFalse(sm.remove(m3)); |
1052 |
+ |
assertEquals(4, set.size()); |
1053 |
+ |
} |
1054 |
+ |
|
1055 |
+ |
/** |
1056 |
+ |
* headSet returns set with keys in requested range |
1057 |
+ |
*/ |
1058 |
+ |
public void testDescendingHeadSetContents() { |
1059 |
+ |
NavigableSet set = dset5(); |
1060 |
+ |
SortedSet sm = set.headSet(m4); |
1061 |
+ |
assertTrue(sm.contains(m1)); |
1062 |
+ |
assertTrue(sm.contains(m2)); |
1063 |
+ |
assertTrue(sm.contains(m3)); |
1064 |
+ |
assertFalse(sm.contains(m4)); |
1065 |
+ |
assertFalse(sm.contains(m5)); |
1066 |
+ |
Iterator i = sm.iterator(); |
1067 |
+ |
Object k; |
1068 |
+ |
k = (Integer)(i.next()); |
1069 |
+ |
assertEquals(m1, k); |
1070 |
+ |
k = (Integer)(i.next()); |
1071 |
+ |
assertEquals(m2, k); |
1072 |
+ |
k = (Integer)(i.next()); |
1073 |
+ |
assertEquals(m3, k); |
1074 |
+ |
assertFalse(i.hasNext()); |
1075 |
+ |
sm.clear(); |
1076 |
+ |
assertTrue(sm.isEmpty()); |
1077 |
+ |
assertEquals(2, set.size()); |
1078 |
+ |
assertEquals(m4, set.first()); |
1079 |
+ |
} |
1080 |
+ |
|
1081 |
+ |
/** |
1082 |
+ |
* tailSet returns set with keys in requested range |
1083 |
+ |
*/ |
1084 |
+ |
public void testDescendingTailSetContents() { |
1085 |
+ |
NavigableSet set = dset5(); |
1086 |
+ |
SortedSet sm = set.tailSet(m2); |
1087 |
+ |
assertFalse(sm.contains(m1)); |
1088 |
+ |
assertTrue(sm.contains(m2)); |
1089 |
+ |
assertTrue(sm.contains(m3)); |
1090 |
+ |
assertTrue(sm.contains(m4)); |
1091 |
+ |
assertTrue(sm.contains(m5)); |
1092 |
+ |
Iterator i = sm.iterator(); |
1093 |
+ |
Object k; |
1094 |
+ |
k = (Integer)(i.next()); |
1095 |
+ |
assertEquals(m2, k); |
1096 |
+ |
k = (Integer)(i.next()); |
1097 |
+ |
assertEquals(m3, k); |
1098 |
+ |
k = (Integer)(i.next()); |
1099 |
+ |
assertEquals(m4, k); |
1100 |
+ |
k = (Integer)(i.next()); |
1101 |
+ |
assertEquals(m5, k); |
1102 |
+ |
assertFalse(i.hasNext()); |
1103 |
+ |
|
1104 |
+ |
SortedSet ssm = sm.tailSet(m4); |
1105 |
+ |
assertEquals(m4, ssm.first()); |
1106 |
+ |
assertEquals(m5, ssm.last()); |
1107 |
+ |
assertTrue(ssm.remove(m4)); |
1108 |
+ |
assertEquals(1, ssm.size()); |
1109 |
+ |
assertEquals(3, sm.size()); |
1110 |
+ |
assertEquals(4, set.size()); |
1111 |
|
} |
1112 |
|
|
1113 |
|
} |