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