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