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