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.navigableSubSet(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.navigableSubSet(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.navigableTailSet(m1); |
90 |
> |
return set; |
91 |
|
} |
92 |
< |
|
92 |
> |
|
93 |
|
/** |
94 |
|
* A new set has unbounded capacity |
95 |
|
*/ |
131 |
|
* add(null) throws NPE |
132 |
|
*/ |
133 |
|
public void testAddNull() { |
134 |
< |
try { |
134 |
> |
try { |
135 |
|
NavigableSet q = set0(); |
136 |
|
q.add(null); |
137 |
|
shouldThrow(); |
138 |
< |
} catch (NullPointerException success) { } |
138 |
> |
} catch (NullPointerException success) {} |
139 |
|
} |
140 |
|
|
141 |
|
/** |
165 |
|
q.add(new Object()); |
166 |
|
q.add(new Object()); |
167 |
|
shouldThrow(); |
168 |
< |
} |
150 |
< |
catch(ClassCastException success) {} |
168 |
> |
} catch (ClassCastException success) {} |
169 |
|
} |
170 |
|
|
171 |
|
|
177 |
|
NavigableSet q = set0(); |
178 |
|
q.addAll(null); |
179 |
|
shouldThrow(); |
180 |
< |
} |
163 |
< |
catch (NullPointerException success) {} |
180 |
> |
} catch (NullPointerException success) {} |
181 |
|
} |
182 |
+ |
|
183 |
|
/** |
184 |
|
* addAll of a collection with null elements throws NPE |
185 |
|
*/ |
189 |
|
Integer[] ints = new Integer[SIZE]; |
190 |
|
q.addAll(Arrays.asList(ints)); |
191 |
|
shouldThrow(); |
192 |
< |
} |
175 |
< |
catch (NullPointerException success) {} |
192 |
> |
} catch (NullPointerException success) {} |
193 |
|
} |
194 |
+ |
|
195 |
|
/** |
196 |
|
* addAll of a collection with any null elements throws NPE after |
197 |
|
* possibly adding some elements |
204 |
|
ints[i] = new Integer(i+SIZE); |
205 |
|
q.addAll(Arrays.asList(ints)); |
206 |
|
shouldThrow(); |
207 |
< |
} |
190 |
< |
catch (NullPointerException success) {} |
207 |
> |
} catch (NullPointerException success) {} |
208 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
|
* Set contains all elements of successful addAll |
212 |
|
*/ |
213 |
|
public void testAddAll5() { |
214 |
< |
try { |
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) |
206 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
207 |
< |
} |
208 |
< |
finally {} |
214 |
> |
Integer[] empty = new Integer[0]; |
215 |
> |
Integer[] ints = new Integer[SIZE]; |
216 |
> |
for (int i = 0; i < SIZE; ++i) |
217 |
> |
ints[i] = new Integer(SIZE-1- i); |
218 |
> |
NavigableSet q = set0(); |
219 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
220 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
221 |
> |
for (int i = 0; i < SIZE; ++i) |
222 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
223 |
|
} |
224 |
|
|
225 |
|
/** |
228 |
|
public void testPoll() { |
229 |
|
NavigableSet q = populatedSet(SIZE); |
230 |
|
for (int i = 0; i < SIZE; ++i) { |
231 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
231 |
> |
assertEquals(i, q.pollFirst()); |
232 |
|
} |
233 |
< |
assertNull(q.pollFirst()); |
233 |
> |
assertNull(q.pollFirst()); |
234 |
|
} |
235 |
|
|
236 |
|
/** |
247 |
|
} |
248 |
|
assertTrue(q.isEmpty()); |
249 |
|
} |
250 |
< |
|
250 |
> |
|
251 |
|
/** |
252 |
|
* contains(x) reports true when elements added but not yet removed |
253 |
|
*/ |
323 |
|
} |
324 |
|
} |
325 |
|
|
326 |
< |
|
326 |
> |
|
327 |
|
|
328 |
|
/** |
329 |
|
* lower returns preceding element |
341 |
|
|
342 |
|
Object e4 = q.lower(zero); |
343 |
|
assertNull(e4); |
330 |
– |
|
344 |
|
} |
345 |
|
|
346 |
|
/** |
359 |
|
|
360 |
|
Object e4 = q.higher(six); |
361 |
|
assertNull(e4); |
349 |
– |
|
362 |
|
} |
363 |
|
|
364 |
|
/** |
377 |
|
|
378 |
|
Object e4 = q.floor(zero); |
379 |
|
assertNull(e4); |
368 |
– |
|
380 |
|
} |
381 |
|
|
382 |
|
/** |
395 |
|
|
396 |
|
Object e4 = q.ceiling(six); |
397 |
|
assertNull(e4); |
387 |
– |
|
398 |
|
} |
399 |
|
|
400 |
|
/** |
401 |
< |
* toArray contains all elements |
401 |
> |
* toArray contains all elements in sorted order |
402 |
|
*/ |
403 |
|
public void testToArray() { |
404 |
|
NavigableSet q = populatedSet(SIZE); |
405 |
< |
Object[] o = q.toArray(); |
406 |
< |
Arrays.sort(o); |
407 |
< |
for(int i = 0; i < o.length; i++) |
398 |
< |
assertEquals(o[i], q.pollFirst()); |
405 |
> |
Object[] o = q.toArray(); |
406 |
> |
for (int i = 0; i < o.length; i++) |
407 |
> |
assertSame(o[i], q.pollFirst()); |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
< |
* toArray(a) contains all elements |
411 |
> |
* toArray(a) contains all elements in sorted order |
412 |
|
*/ |
413 |
|
public void testToArray2() { |
414 |
< |
NavigableSet q = populatedSet(SIZE); |
415 |
< |
Integer[] ints = new Integer[SIZE]; |
416 |
< |
ints = (Integer[])q.toArray(ints); |
417 |
< |
Arrays.sort(ints); |
418 |
< |
for(int i = 0; i < ints.length; i++) |
419 |
< |
assertEquals(ints[i], q.pollFirst()); |
414 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
415 |
> |
Integer[] ints = new Integer[SIZE]; |
416 |
> |
Integer[] array = q.toArray(ints); |
417 |
> |
assertSame(ints, array); |
418 |
> |
for (int i = 0; i < ints.length; i++) |
419 |
> |
assertSame(ints[i], q.pollFirst()); |
420 |
|
} |
421 |
< |
|
421 |
> |
|
422 |
|
/** |
423 |
|
* iterator iterates through all elements |
424 |
|
*/ |
425 |
|
public void testIterator() { |
426 |
|
NavigableSet q = populatedSet(SIZE); |
427 |
|
int i = 0; |
428 |
< |
Iterator it = q.iterator(); |
429 |
< |
while(it.hasNext()) { |
428 |
> |
Iterator it = q.iterator(); |
429 |
> |
while (it.hasNext()) { |
430 |
|
assertTrue(q.contains(it.next())); |
431 |
|
++i; |
432 |
|
} |
439 |
|
public void testEmptyIterator() { |
440 |
|
NavigableSet q = set0(); |
441 |
|
int i = 0; |
442 |
< |
Iterator it = q.iterator(); |
443 |
< |
while(it.hasNext()) { |
442 |
> |
Iterator it = q.iterator(); |
443 |
> |
while (it.hasNext()) { |
444 |
|
assertTrue(q.contains(it.next())); |
445 |
|
++i; |
446 |
|
} |
450 |
|
/** |
451 |
|
* iterator.remove removes current element |
452 |
|
*/ |
453 |
< |
public void testIteratorRemove () { |
453 |
> |
public void testIteratorRemove() { |
454 |
|
final NavigableSet q = set0(); |
455 |
|
q.add(new Integer(2)); |
456 |
|
q.add(new Integer(1)); |
476 |
|
for (int i = 0; i < SIZE; ++i) { |
477 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
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() { |
484 |
> |
public void testSerialization() throws Exception { |
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(); |
495 |
< |
assertEquals(q.size(), r.size()); |
496 |
< |
while (!q.isEmpty()) |
488 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
489 |
< |
} catch(Exception e){ |
490 |
< |
e.printStackTrace(); |
491 |
< |
unexpectedException(); |
492 |
< |
} |
486 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
487 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
488 |
> |
out.writeObject(q); |
489 |
> |
out.close(); |
490 |
> |
|
491 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
492 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
493 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
494 |
> |
assertEquals(q.size(), r.size()); |
495 |
> |
while (!q.isEmpty()) |
496 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
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(null) throws NPE |
635 |
+ |
*/ |
636 |
+ |
public void testDescendingAddNull() { |
637 |
+ |
try { |
638 |
+ |
NavigableSet q = dset0(); |
639 |
+ |
q.add(null); |
640 |
+ |
shouldThrow(); |
641 |
+ |
} catch (NullPointerException success) {} |
642 |
+ |
} |
643 |
+ |
|
644 |
+ |
/** |
645 |
+ |
* Add of comparable element succeeds |
646 |
+ |
*/ |
647 |
+ |
public void testDescendingAdd() { |
648 |
+ |
NavigableSet q = dset0(); |
649 |
+ |
assertTrue(q.add(m6)); |
650 |
+ |
} |
651 |
+ |
|
652 |
+ |
/** |
653 |
+ |
* Add of duplicate element fails |
654 |
+ |
*/ |
655 |
+ |
public void testDescendingAddDup() { |
656 |
+ |
NavigableSet q = dset0(); |
657 |
+ |
assertTrue(q.add(m6)); |
658 |
+ |
assertFalse(q.add(m6)); |
659 |
+ |
} |
660 |
+ |
|
661 |
+ |
/** |
662 |
+ |
* Add of non-Comparable throws CCE |
663 |
+ |
*/ |
664 |
+ |
public void testDescendingAddNonComparable() { |
665 |
+ |
try { |
666 |
+ |
NavigableSet q = dset0(); |
667 |
+ |
q.add(new Object()); |
668 |
+ |
q.add(new Object()); |
669 |
+ |
q.add(new Object()); |
670 |
+ |
shouldThrow(); |
671 |
+ |
} catch (ClassCastException success) {} |
672 |
+ |
} |
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 |
+ |
|
831 |
+ |
/** |
832 |
+ |
* lower returns preceding element |
833 |
+ |
*/ |
834 |
+ |
public void testDescendingLower() { |
835 |
+ |
NavigableSet q = dset5(); |
836 |
+ |
Object e1 = q.lower(m3); |
837 |
+ |
assertEquals(m2, e1); |
838 |
+ |
|
839 |
+ |
Object e2 = q.lower(m6); |
840 |
+ |
assertEquals(m5, e2); |
841 |
+ |
|
842 |
+ |
Object e3 = q.lower(m1); |
843 |
+ |
assertNull(e3); |
844 |
+ |
|
845 |
+ |
Object e4 = q.lower(zero); |
846 |
+ |
assertNull(e4); |
847 |
+ |
} |
848 |
+ |
|
849 |
+ |
/** |
850 |
+ |
* higher returns next element |
851 |
+ |
*/ |
852 |
+ |
public void testDescendingHigher() { |
853 |
+ |
NavigableSet q = dset5(); |
854 |
+ |
Object e1 = q.higher(m3); |
855 |
+ |
assertEquals(m4, e1); |
856 |
+ |
|
857 |
+ |
Object e2 = q.higher(zero); |
858 |
+ |
assertEquals(m1, e2); |
859 |
+ |
|
860 |
+ |
Object e3 = q.higher(m5); |
861 |
+ |
assertNull(e3); |
862 |
+ |
|
863 |
+ |
Object e4 = q.higher(m6); |
864 |
+ |
assertNull(e4); |
865 |
+ |
} |
866 |
+ |
|
867 |
+ |
/** |
868 |
+ |
* floor returns preceding element |
869 |
+ |
*/ |
870 |
+ |
public void testDescendingFloor() { |
871 |
+ |
NavigableSet q = dset5(); |
872 |
+ |
Object e1 = q.floor(m3); |
873 |
+ |
assertEquals(m3, e1); |
874 |
+ |
|
875 |
+ |
Object e2 = q.floor(m6); |
876 |
+ |
assertEquals(m5, e2); |
877 |
+ |
|
878 |
+ |
Object e3 = q.floor(m1); |
879 |
+ |
assertEquals(m1, e3); |
880 |
+ |
|
881 |
+ |
Object e4 = q.floor(zero); |
882 |
+ |
assertNull(e4); |
883 |
+ |
} |
884 |
+ |
|
885 |
+ |
/** |
886 |
+ |
* ceiling returns next element |
887 |
+ |
*/ |
888 |
+ |
public void testDescendingCeiling() { |
889 |
+ |
NavigableSet q = dset5(); |
890 |
+ |
Object e1 = q.ceiling(m3); |
891 |
+ |
assertEquals(m3, e1); |
892 |
+ |
|
893 |
+ |
Object e2 = q.ceiling(zero); |
894 |
+ |
assertEquals(m1, e2); |
895 |
+ |
|
896 |
+ |
Object e3 = q.ceiling(m5); |
897 |
+ |
assertEquals(m5, e3); |
898 |
+ |
|
899 |
+ |
Object e4 = q.ceiling(m6); |
900 |
+ |
assertNull(e4); |
901 |
+ |
} |
902 |
+ |
|
903 |
+ |
/** |
904 |
+ |
* toArray contains all elements |
905 |
+ |
*/ |
906 |
+ |
public void testDescendingToArray() { |
907 |
+ |
NavigableSet q = populatedSet(SIZE); |
908 |
+ |
Object[] o = q.toArray(); |
909 |
+ |
Arrays.sort(o); |
910 |
+ |
for (int i = 0; i < o.length; i++) |
911 |
+ |
assertEquals(o[i], q.pollFirst()); |
912 |
+ |
} |
913 |
+ |
|
914 |
+ |
/** |
915 |
+ |
* toArray(a) contains all elements |
916 |
+ |
*/ |
917 |
+ |
public void testDescendingToArray2() { |
918 |
+ |
NavigableSet q = populatedSet(SIZE); |
919 |
+ |
Integer[] ints = new Integer[SIZE]; |
920 |
+ |
assertSame(ints, q.toArray(ints)); |
921 |
+ |
Arrays.sort(ints); |
922 |
+ |
for (int i = 0; i < ints.length; i++) |
923 |
+ |
assertEquals(ints[i], q.pollFirst()); |
924 |
+ |
} |
925 |
+ |
|
926 |
+ |
/** |
927 |
+ |
* iterator iterates through all elements |
928 |
+ |
*/ |
929 |
+ |
public void testDescendingIterator() { |
930 |
+ |
NavigableSet q = populatedSet(SIZE); |
931 |
+ |
int i = 0; |
932 |
+ |
Iterator it = q.iterator(); |
933 |
+ |
while (it.hasNext()) { |
934 |
+ |
assertTrue(q.contains(it.next())); |
935 |
+ |
++i; |
936 |
+ |
} |
937 |
+ |
assertEquals(i, SIZE); |
938 |
+ |
} |
939 |
+ |
|
940 |
+ |
/** |
941 |
+ |
* iterator of empty set has no elements |
942 |
+ |
*/ |
943 |
+ |
public void testDescendingEmptyIterator() { |
944 |
+ |
NavigableSet q = dset0(); |
945 |
+ |
int i = 0; |
946 |
+ |
Iterator it = q.iterator(); |
947 |
+ |
while (it.hasNext()) { |
948 |
+ |
assertTrue(q.contains(it.next())); |
949 |
+ |
++i; |
950 |
+ |
} |
951 |
+ |
assertEquals(i, 0); |
952 |
+ |
} |
953 |
+ |
|
954 |
+ |
/** |
955 |
+ |
* iterator.remove removes current element |
956 |
+ |
*/ |
957 |
+ |
public void testDescendingIteratorRemove() { |
958 |
+ |
final NavigableSet q = dset0(); |
959 |
+ |
q.add(new Integer(2)); |
960 |
+ |
q.add(new Integer(1)); |
961 |
+ |
q.add(new Integer(3)); |
962 |
+ |
|
963 |
+ |
Iterator it = q.iterator(); |
964 |
+ |
it.next(); |
965 |
+ |
it.remove(); |
966 |
+ |
|
967 |
+ |
it = q.iterator(); |
968 |
+ |
assertEquals(it.next(), new Integer(2)); |
969 |
+ |
assertEquals(it.next(), new Integer(3)); |
970 |
+ |
assertFalse(it.hasNext()); |
971 |
+ |
} |
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.indexOf(String.valueOf(i)) >= 0); |
982 |
+ |
} |
983 |
+ |
} |
984 |
+ |
|
985 |
+ |
/** |
986 |
+ |
* A deserialized serialized set has same elements |
987 |
+ |
*/ |
988 |
+ |
public void testDescendingSerialization() throws Exception { |
989 |
+ |
NavigableSet q = populatedSet(SIZE); |
990 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
991 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
992 |
+ |
out.writeObject(q); |
993 |
+ |
out.close(); |
994 |
+ |
|
995 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
996 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
997 |
+ |
NavigableSet r = (NavigableSet)in.readObject(); |
998 |
+ |
assertEquals(q.size(), r.size()); |
999 |
+ |
while (!q.isEmpty()) |
1000 |
+ |
assertEquals(q.pollFirst(), r.pollFirst()); |
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 |
|
} |