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 |
|
|
31 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
32 |
|
assertTrue(q.isEmpty()); |
33 |
|
|
34 |
< |
for(int i = n-1; i >= 0; i-=2) |
35 |
< |
assertTrue(q.add(new Integer(i))); |
36 |
< |
for(int i = (n & 1); i < n; i+=2) |
37 |
< |
assertTrue(q.add(new Integer(i))); |
34 |
> |
for (int i = n-1; i >= 0; i-=2) |
35 |
> |
assertTrue(q.add(new Integer(i))); |
36 |
> |
for (int i = (n & 1); i < n; i+=2) |
37 |
> |
assertTrue(q.add(new Integer(i))); |
38 |
|
assertTrue(q.add(new Integer(-n))); |
39 |
|
assertTrue(q.add(new Integer(n))); |
40 |
< |
NavigableSet s = q.subSet(new Integer(0), new Integer(n)); |
40 |
> |
NavigableSet s = q.subSet(new Integer(0), true, new Integer(n), false); |
41 |
|
assertFalse(s.isEmpty()); |
42 |
< |
assertEquals(n, s.size()); |
42 |
> |
assertEquals(n, s.size()); |
43 |
|
return s; |
44 |
|
} |
45 |
|
|
56 |
|
q.add(five); |
57 |
|
q.add(zero); |
58 |
|
q.add(seven); |
59 |
< |
NavigableSet s = q.subSet(one, seven); |
60 |
< |
assertEquals(5, s.size()); |
59 |
> |
NavigableSet s = q.subSet(one, true, seven, false); |
60 |
> |
assertEquals(5, s.size()); |
61 |
|
return s; |
62 |
|
} |
63 |
|
|
64 |
< |
private static NavigableSet set0() { |
65 |
< |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
64 |
> |
/** |
65 |
> |
* Create set of first 5 negative ints |
66 |
> |
*/ |
67 |
> |
private NavigableSet dset5() { |
68 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
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 |
> |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
82 |
|
assertTrue(set.isEmpty()); |
83 |
< |
return set.tailSet(m1); |
83 |
> |
return set.tailSet(m1, true); |
84 |
|
} |
85 |
< |
|
85 |
> |
|
86 |
> |
private static NavigableSet dset0() { |
87 |
> |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
88 |
> |
assertTrue(set.isEmpty()); |
89 |
> |
return set; |
90 |
> |
} |
91 |
> |
|
92 |
|
/** |
93 |
|
* A new set has unbounded capacity |
94 |
|
*/ |
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 |
|
|
170 |
|
|
176 |
|
NavigableSet q = set0(); |
177 |
|
q.addAll(null); |
178 |
|
shouldThrow(); |
179 |
< |
} |
163 |
< |
catch (NullPointerException success) {} |
179 |
> |
} catch (NullPointerException success) {} |
180 |
|
} |
181 |
+ |
|
182 |
|
/** |
183 |
|
* addAll of a collection with null elements throws NPE |
184 |
|
*/ |
188 |
|
Integer[] ints = new Integer[SIZE]; |
189 |
|
q.addAll(Arrays.asList(ints)); |
190 |
|
shouldThrow(); |
191 |
< |
} |
175 |
< |
catch (NullPointerException success) {} |
191 |
> |
} catch (NullPointerException success) {} |
192 |
|
} |
193 |
+ |
|
194 |
|
/** |
195 |
|
* addAll of a collection with any null elements throws NPE after |
196 |
|
* possibly adding some elements |
203 |
|
ints[i] = new Integer(i+SIZE); |
204 |
|
q.addAll(Arrays.asList(ints)); |
205 |
|
shouldThrow(); |
206 |
< |
} |
190 |
< |
catch (NullPointerException success) {} |
206 |
> |
} catch (NullPointerException success) {} |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
|
* Set contains all elements of successful addAll |
211 |
|
*/ |
212 |
|
public void testAddAll5() { |
213 |
< |
try { |
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) |
206 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
207 |
< |
} |
208 |
< |
finally {} |
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) |
221 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
222 |
|
} |
223 |
|
|
224 |
|
/** |
227 |
|
public void testPoll() { |
228 |
|
NavigableSet q = populatedSet(SIZE); |
229 |
|
for (int i = 0; i < SIZE; ++i) { |
230 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
230 |
> |
assertEquals(i, q.pollFirst()); |
231 |
|
} |
232 |
< |
assertNull(q.pollFirst()); |
232 |
> |
assertNull(q.pollFirst()); |
233 |
|
} |
234 |
|
|
235 |
|
/** |
246 |
|
} |
247 |
|
assertTrue(q.isEmpty()); |
248 |
|
} |
249 |
< |
|
249 |
> |
|
250 |
|
/** |
251 |
|
* contains(x) reports true when elements added but not yet removed |
252 |
|
*/ |
322 |
|
} |
323 |
|
} |
324 |
|
|
325 |
< |
|
325 |
> |
|
326 |
|
|
327 |
|
/** |
328 |
|
* lower returns preceding element |
340 |
|
|
341 |
|
Object e4 = q.lower(zero); |
342 |
|
assertNull(e4); |
330 |
– |
|
343 |
|
} |
344 |
|
|
345 |
|
/** |
358 |
|
|
359 |
|
Object e4 = q.higher(six); |
360 |
|
assertNull(e4); |
349 |
– |
|
361 |
|
} |
362 |
|
|
363 |
|
/** |
376 |
|
|
377 |
|
Object e4 = q.floor(zero); |
378 |
|
assertNull(e4); |
368 |
– |
|
379 |
|
} |
380 |
|
|
381 |
|
/** |
394 |
|
|
395 |
|
Object e4 = q.ceiling(six); |
396 |
|
assertNull(e4); |
387 |
– |
|
397 |
|
} |
398 |
|
|
399 |
|
/** |
400 |
< |
* toArray contains all elements |
400 |
> |
* toArray contains all elements in sorted order |
401 |
|
*/ |
402 |
|
public void testToArray() { |
403 |
|
NavigableSet q = populatedSet(SIZE); |
404 |
< |
Object[] o = q.toArray(); |
405 |
< |
Arrays.sort(o); |
406 |
< |
for(int i = 0; i < o.length; i++) |
398 |
< |
assertEquals(o[i], q.pollFirst()); |
404 |
> |
Object[] o = q.toArray(); |
405 |
> |
for (int i = 0; i < o.length; i++) |
406 |
> |
assertSame(o[i], q.pollFirst()); |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
< |
* toArray(a) contains all elements |
410 |
> |
* toArray(a) contains all elements in sorted order |
411 |
|
*/ |
412 |
|
public void testToArray2() { |
413 |
|
NavigableSet q = populatedSet(SIZE); |
414 |
< |
Integer[] ints = new Integer[SIZE]; |
415 |
< |
ints = (Integer[])q.toArray(ints); |
416 |
< |
Arrays.sort(ints); |
417 |
< |
for(int i = 0; i < ints.length; i++) |
410 |
< |
assertEquals(ints[i], q.pollFirst()); |
414 |
> |
Integer[] ints = new Integer[SIZE]; |
415 |
> |
assertSame(ints, q.toArray(ints)); |
416 |
> |
for (int i = 0; i < ints.length; i++) |
417 |
> |
assertSame(ints[i], q.pollFirst()); |
418 |
|
} |
419 |
< |
|
419 |
> |
|
420 |
|
/** |
421 |
|
* iterator iterates through all elements |
422 |
|
*/ |
423 |
|
public void testIterator() { |
424 |
|
NavigableSet q = populatedSet(SIZE); |
425 |
|
int i = 0; |
426 |
< |
Iterator it = q.iterator(); |
427 |
< |
while(it.hasNext()) { |
426 |
> |
Iterator it = q.iterator(); |
427 |
> |
while (it.hasNext()) { |
428 |
|
assertTrue(q.contains(it.next())); |
429 |
|
++i; |
430 |
|
} |
437 |
|
public void testEmptyIterator() { |
438 |
|
NavigableSet q = set0(); |
439 |
|
int i = 0; |
440 |
< |
Iterator it = q.iterator(); |
441 |
< |
while(it.hasNext()) { |
440 |
> |
Iterator it = q.iterator(); |
441 |
> |
while (it.hasNext()) { |
442 |
|
assertTrue(q.contains(it.next())); |
443 |
|
++i; |
444 |
|
} |
448 |
|
/** |
449 |
|
* iterator.remove removes current element |
450 |
|
*/ |
451 |
< |
public void testIteratorRemove () { |
451 |
> |
public void testIteratorRemove() { |
452 |
|
final NavigableSet q = set0(); |
453 |
|
q.add(new Integer(2)); |
454 |
|
q.add(new Integer(1)); |
474 |
|
for (int i = 0; i < SIZE; ++i) { |
475 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
476 |
|
} |
477 |
< |
} |
477 |
> |
} |
478 |
|
|
479 |
|
/** |
480 |
< |
* A deserialized serialized set has same elements |
480 |
> |
* A deserialized serialized set has same elements |
481 |
|
*/ |
482 |
< |
public void testSerialization() { |
482 |
> |
public void testSerialization() throws Exception { |
483 |
|
NavigableSet q = populatedSet(SIZE); |
484 |
< |
try { |
485 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
486 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
487 |
< |
out.writeObject(q); |
488 |
< |
out.close(); |
489 |
< |
|
490 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
491 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
492 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
493 |
< |
assertEquals(q.size(), r.size()); |
494 |
< |
while (!q.isEmpty()) |
488 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
489 |
< |
} catch(Exception e){ |
490 |
< |
e.printStackTrace(); |
491 |
< |
unexpectedException(); |
492 |
< |
} |
484 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
485 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
486 |
> |
out.writeObject(q); |
487 |
> |
out.close(); |
488 |
> |
|
489 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
490 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
491 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
492 |
> |
assertEquals(q.size(), r.size()); |
493 |
> |
while (!q.isEmpty()) |
494 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
495 |
|
} |
496 |
|
|
497 |
|
/** |
611 |
|
assertEquals(1, ssm.size()); |
612 |
|
assertEquals(3, sm.size()); |
613 |
|
assertEquals(4, set.size()); |
614 |
+ |
} |
615 |
+ |
|
616 |
+ |
/** |
617 |
+ |
* size changes when elements added and removed |
618 |
+ |
*/ |
619 |
+ |
public void testDescendingSize() { |
620 |
+ |
NavigableSet q = populatedSet(SIZE); |
621 |
+ |
for (int i = 0; i < SIZE; ++i) { |
622 |
+ |
assertEquals(SIZE-i, q.size()); |
623 |
+ |
q.pollFirst(); |
624 |
+ |
} |
625 |
+ |
for (int i = 0; i < SIZE; ++i) { |
626 |
+ |
assertEquals(i, q.size()); |
627 |
+ |
q.add(new Integer(i)); |
628 |
+ |
} |
629 |
+ |
} |
630 |
+ |
|
631 |
+ |
/** |
632 |
+ |
* add(null) throws NPE |
633 |
+ |
*/ |
634 |
+ |
public void testDescendingAddNull() { |
635 |
+ |
try { |
636 |
+ |
NavigableSet q = dset0(); |
637 |
+ |
q.add(null); |
638 |
+ |
shouldThrow(); |
639 |
+ |
} catch (NullPointerException success) {} |
640 |
+ |
} |
641 |
+ |
|
642 |
+ |
/** |
643 |
+ |
* Add of comparable element succeeds |
644 |
+ |
*/ |
645 |
+ |
public void testDescendingAdd() { |
646 |
+ |
NavigableSet q = dset0(); |
647 |
+ |
assertTrue(q.add(m6)); |
648 |
+ |
} |
649 |
+ |
|
650 |
+ |
/** |
651 |
+ |
* Add of duplicate element fails |
652 |
+ |
*/ |
653 |
+ |
public void testDescendingAddDup() { |
654 |
+ |
NavigableSet q = dset0(); |
655 |
+ |
assertTrue(q.add(m6)); |
656 |
+ |
assertFalse(q.add(m6)); |
657 |
+ |
} |
658 |
+ |
|
659 |
+ |
/** |
660 |
+ |
* Add of non-Comparable throws CCE |
661 |
+ |
*/ |
662 |
+ |
public void testDescendingAddNonComparable() { |
663 |
+ |
try { |
664 |
+ |
NavigableSet q = dset0(); |
665 |
+ |
q.add(new Object()); |
666 |
+ |
q.add(new Object()); |
667 |
+ |
q.add(new Object()); |
668 |
+ |
shouldThrow(); |
669 |
+ |
} catch (ClassCastException success) {} |
670 |
+ |
} |
671 |
+ |
|
672 |
+ |
|
673 |
+ |
/** |
674 |
+ |
* addAll(null) throws NPE |
675 |
+ |
*/ |
676 |
+ |
public void testDescendingAddAll1() { |
677 |
+ |
try { |
678 |
+ |
NavigableSet q = dset0(); |
679 |
+ |
q.addAll(null); |
680 |
+ |
shouldThrow(); |
681 |
+ |
} catch (NullPointerException success) {} |
682 |
+ |
} |
683 |
+ |
|
684 |
+ |
/** |
685 |
+ |
* addAll of a collection with null elements throws NPE |
686 |
+ |
*/ |
687 |
+ |
public void testDescendingAddAll2() { |
688 |
+ |
try { |
689 |
+ |
NavigableSet q = dset0(); |
690 |
+ |
Integer[] ints = new Integer[SIZE]; |
691 |
+ |
q.addAll(Arrays.asList(ints)); |
692 |
+ |
shouldThrow(); |
693 |
+ |
} catch (NullPointerException success) {} |
694 |
+ |
} |
695 |
+ |
|
696 |
+ |
/** |
697 |
+ |
* addAll of a collection with any null elements throws NPE after |
698 |
+ |
* possibly adding some elements |
699 |
+ |
*/ |
700 |
+ |
public void testDescendingAddAll3() { |
701 |
+ |
try { |
702 |
+ |
NavigableSet q = dset0(); |
703 |
+ |
Integer[] ints = new Integer[SIZE]; |
704 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
705 |
+ |
ints[i] = new Integer(i+SIZE); |
706 |
+ |
q.addAll(Arrays.asList(ints)); |
707 |
+ |
shouldThrow(); |
708 |
+ |
} catch (NullPointerException success) {} |
709 |
+ |
} |
710 |
+ |
|
711 |
+ |
/** |
712 |
+ |
* Set contains all elements of successful addAll |
713 |
+ |
*/ |
714 |
+ |
public void testDescendingAddAll5() { |
715 |
+ |
Integer[] empty = new Integer[0]; |
716 |
+ |
Integer[] ints = new Integer[SIZE]; |
717 |
+ |
for (int i = 0; i < SIZE; ++i) |
718 |
+ |
ints[i] = new Integer(SIZE-1- i); |
719 |
+ |
NavigableSet q = dset0(); |
720 |
+ |
assertFalse(q.addAll(Arrays.asList(empty))); |
721 |
+ |
assertTrue(q.addAll(Arrays.asList(ints))); |
722 |
+ |
for (int i = 0; i < SIZE; ++i) |
723 |
+ |
assertEquals(new Integer(i), q.pollFirst()); |
724 |
+ |
} |
725 |
+ |
|
726 |
+ |
/** |
727 |
+ |
* poll succeeds unless empty |
728 |
+ |
*/ |
729 |
+ |
public void testDescendingPoll() { |
730 |
+ |
NavigableSet q = populatedSet(SIZE); |
731 |
+ |
for (int i = 0; i < SIZE; ++i) { |
732 |
+ |
assertEquals(i, q.pollFirst()); |
733 |
+ |
} |
734 |
+ |
assertNull(q.pollFirst()); |
735 |
+ |
} |
736 |
+ |
|
737 |
+ |
/** |
738 |
+ |
* remove(x) removes x and returns true if present |
739 |
+ |
*/ |
740 |
+ |
public void testDescendingRemoveElement() { |
741 |
+ |
NavigableSet q = populatedSet(SIZE); |
742 |
+ |
for (int i = 1; i < SIZE; i+=2) { |
743 |
+ |
assertTrue(q.remove(new Integer(i))); |
744 |
+ |
} |
745 |
+ |
for (int i = 0; i < SIZE; i+=2) { |
746 |
+ |
assertTrue(q.remove(new Integer(i))); |
747 |
+ |
assertFalse(q.remove(new Integer(i+1))); |
748 |
+ |
} |
749 |
+ |
assertTrue(q.isEmpty()); |
750 |
+ |
} |
751 |
+ |
|
752 |
+ |
/** |
753 |
+ |
* contains(x) reports true when elements added but not yet removed |
754 |
+ |
*/ |
755 |
+ |
public void testDescendingContains() { |
756 |
+ |
NavigableSet q = populatedSet(SIZE); |
757 |
+ |
for (int i = 0; i < SIZE; ++i) { |
758 |
+ |
assertTrue(q.contains(new Integer(i))); |
759 |
+ |
q.pollFirst(); |
760 |
+ |
assertFalse(q.contains(new Integer(i))); |
761 |
+ |
} |
762 |
+ |
} |
763 |
+ |
|
764 |
+ |
/** |
765 |
+ |
* clear removes all elements |
766 |
+ |
*/ |
767 |
+ |
public void testDescendingClear() { |
768 |
+ |
NavigableSet q = populatedSet(SIZE); |
769 |
+ |
q.clear(); |
770 |
+ |
assertTrue(q.isEmpty()); |
771 |
+ |
assertEquals(0, q.size()); |
772 |
+ |
q.add(new Integer(1)); |
773 |
+ |
assertFalse(q.isEmpty()); |
774 |
+ |
q.clear(); |
775 |
+ |
assertTrue(q.isEmpty()); |
776 |
+ |
} |
777 |
+ |
|
778 |
+ |
/** |
779 |
+ |
* containsAll(c) is true when c contains a subset of elements |
780 |
+ |
*/ |
781 |
+ |
public void testDescendingContainsAll() { |
782 |
+ |
NavigableSet q = populatedSet(SIZE); |
783 |
+ |
NavigableSet p = dset0(); |
784 |
+ |
for (int i = 0; i < SIZE; ++i) { |
785 |
+ |
assertTrue(q.containsAll(p)); |
786 |
+ |
assertFalse(p.containsAll(q)); |
787 |
+ |
p.add(new Integer(i)); |
788 |
+ |
} |
789 |
+ |
assertTrue(p.containsAll(q)); |
790 |
+ |
} |
791 |
+ |
|
792 |
+ |
/** |
793 |
+ |
* retainAll(c) retains only those elements of c and reports true if changed |
794 |
+ |
*/ |
795 |
+ |
public void testDescendingRetainAll() { |
796 |
+ |
NavigableSet q = populatedSet(SIZE); |
797 |
+ |
NavigableSet p = populatedSet(SIZE); |
798 |
+ |
for (int i = 0; i < SIZE; ++i) { |
799 |
+ |
boolean changed = q.retainAll(p); |
800 |
+ |
if (i == 0) |
801 |
+ |
assertFalse(changed); |
802 |
+ |
else |
803 |
+ |
assertTrue(changed); |
804 |
+ |
|
805 |
+ |
assertTrue(q.containsAll(p)); |
806 |
+ |
assertEquals(SIZE-i, q.size()); |
807 |
+ |
p.pollFirst(); |
808 |
+ |
} |
809 |
+ |
} |
810 |
+ |
|
811 |
+ |
/** |
812 |
+ |
* removeAll(c) removes only those elements of c and reports true if changed |
813 |
+ |
*/ |
814 |
+ |
public void testDescendingRemoveAll() { |
815 |
+ |
for (int i = 1; i < SIZE; ++i) { |
816 |
+ |
NavigableSet q = populatedSet(SIZE); |
817 |
+ |
NavigableSet p = populatedSet(i); |
818 |
+ |
assertTrue(q.removeAll(p)); |
819 |
+ |
assertEquals(SIZE-i, q.size()); |
820 |
+ |
for (int j = 0; j < i; ++j) { |
821 |
+ |
Integer I = (Integer)(p.pollFirst()); |
822 |
+ |
assertFalse(q.contains(I)); |
823 |
+ |
} |
824 |
+ |
} |
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 |
+ |
/** |
973 |
+ |
* toString contains toStrings of elements |
974 |
+ |
*/ |
975 |
+ |
public void testDescendingToString() { |
976 |
+ |
NavigableSet q = populatedSet(SIZE); |
977 |
+ |
String s = q.toString(); |
978 |
+ |
for (int i = 0; i < SIZE; ++i) { |
979 |
+ |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
980 |
+ |
} |
981 |
+ |
} |
982 |
+ |
|
983 |
+ |
/** |
984 |
+ |
* A deserialized serialized set has same elements |
985 |
+ |
*/ |
986 |
+ |
public void testDescendingSerialization() throws Exception { |
987 |
+ |
NavigableSet q = populatedSet(SIZE); |
988 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
989 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
990 |
+ |
out.writeObject(q); |
991 |
+ |
out.close(); |
992 |
+ |
|
993 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
994 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
995 |
+ |
NavigableSet r = (NavigableSet)in.readObject(); |
996 |
+ |
assertEquals(q.size(), r.size()); |
997 |
+ |
while (!q.isEmpty()) |
998 |
+ |
assertEquals(q.pollFirst(), r.pollFirst()); |
999 |
+ |
} |
1000 |
+ |
|
1001 |
+ |
/** |
1002 |
+ |
* subSet returns set with keys in requested range |
1003 |
+ |
*/ |
1004 |
+ |
public void testDescendingSubSetContents() { |
1005 |
+ |
NavigableSet set = dset5(); |
1006 |
+ |
SortedSet sm = set.subSet(m2, m4); |
1007 |
+ |
assertEquals(m2, sm.first()); |
1008 |
+ |
assertEquals(m3, sm.last()); |
1009 |
+ |
assertEquals(2, sm.size()); |
1010 |
+ |
assertFalse(sm.contains(m1)); |
1011 |
+ |
assertTrue(sm.contains(m2)); |
1012 |
+ |
assertTrue(sm.contains(m3)); |
1013 |
+ |
assertFalse(sm.contains(m4)); |
1014 |
+ |
assertFalse(sm.contains(m5)); |
1015 |
+ |
Iterator i = sm.iterator(); |
1016 |
+ |
Object k; |
1017 |
+ |
k = (Integer)(i.next()); |
1018 |
+ |
assertEquals(m2, k); |
1019 |
+ |
k = (Integer)(i.next()); |
1020 |
+ |
assertEquals(m3, k); |
1021 |
+ |
assertFalse(i.hasNext()); |
1022 |
+ |
Iterator j = sm.iterator(); |
1023 |
+ |
j.next(); |
1024 |
+ |
j.remove(); |
1025 |
+ |
assertFalse(set.contains(m2)); |
1026 |
+ |
assertEquals(4, set.size()); |
1027 |
+ |
assertEquals(1, sm.size()); |
1028 |
+ |
assertEquals(m3, sm.first()); |
1029 |
+ |
assertEquals(m3, sm.last()); |
1030 |
+ |
assertTrue(sm.remove(m3)); |
1031 |
+ |
assertTrue(sm.isEmpty()); |
1032 |
+ |
assertEquals(3, set.size()); |
1033 |
+ |
} |
1034 |
+ |
|
1035 |
+ |
public void testDescendingSubSetContents2() { |
1036 |
+ |
NavigableSet set = dset5(); |
1037 |
+ |
SortedSet sm = set.subSet(m2, m3); |
1038 |
+ |
assertEquals(1, sm.size()); |
1039 |
+ |
assertEquals(m2, sm.first()); |
1040 |
+ |
assertEquals(m2, sm.last()); |
1041 |
+ |
assertFalse(sm.contains(m1)); |
1042 |
+ |
assertTrue(sm.contains(m2)); |
1043 |
+ |
assertFalse(sm.contains(m3)); |
1044 |
+ |
assertFalse(sm.contains(m4)); |
1045 |
+ |
assertFalse(sm.contains(m5)); |
1046 |
+ |
Iterator i = sm.iterator(); |
1047 |
+ |
Object k; |
1048 |
+ |
k = (Integer)(i.next()); |
1049 |
+ |
assertEquals(m2, k); |
1050 |
+ |
assertFalse(i.hasNext()); |
1051 |
+ |
Iterator j = sm.iterator(); |
1052 |
+ |
j.next(); |
1053 |
+ |
j.remove(); |
1054 |
+ |
assertFalse(set.contains(m2)); |
1055 |
+ |
assertEquals(4, set.size()); |
1056 |
+ |
assertEquals(0, sm.size()); |
1057 |
+ |
assertTrue(sm.isEmpty()); |
1058 |
+ |
assertFalse(sm.remove(m3)); |
1059 |
+ |
assertEquals(4, set.size()); |
1060 |
+ |
} |
1061 |
+ |
|
1062 |
+ |
/** |
1063 |
+ |
* headSet returns set with keys in requested range |
1064 |
+ |
*/ |
1065 |
+ |
public void testDescendingHeadSetContents() { |
1066 |
+ |
NavigableSet set = dset5(); |
1067 |
+ |
SortedSet sm = set.headSet(m4); |
1068 |
+ |
assertTrue(sm.contains(m1)); |
1069 |
+ |
assertTrue(sm.contains(m2)); |
1070 |
+ |
assertTrue(sm.contains(m3)); |
1071 |
+ |
assertFalse(sm.contains(m4)); |
1072 |
+ |
assertFalse(sm.contains(m5)); |
1073 |
+ |
Iterator i = sm.iterator(); |
1074 |
+ |
Object k; |
1075 |
+ |
k = (Integer)(i.next()); |
1076 |
+ |
assertEquals(m1, k); |
1077 |
+ |
k = (Integer)(i.next()); |
1078 |
+ |
assertEquals(m2, k); |
1079 |
+ |
k = (Integer)(i.next()); |
1080 |
+ |
assertEquals(m3, k); |
1081 |
+ |
assertFalse(i.hasNext()); |
1082 |
+ |
sm.clear(); |
1083 |
+ |
assertTrue(sm.isEmpty()); |
1084 |
+ |
assertEquals(2, set.size()); |
1085 |
+ |
assertEquals(m4, set.first()); |
1086 |
+ |
} |
1087 |
+ |
|
1088 |
+ |
/** |
1089 |
+ |
* tailSet returns set with keys in requested range |
1090 |
+ |
*/ |
1091 |
+ |
public void testDescendingTailSetContents() { |
1092 |
+ |
NavigableSet set = dset5(); |
1093 |
+ |
SortedSet sm = set.tailSet(m2); |
1094 |
+ |
assertFalse(sm.contains(m1)); |
1095 |
+ |
assertTrue(sm.contains(m2)); |
1096 |
+ |
assertTrue(sm.contains(m3)); |
1097 |
+ |
assertTrue(sm.contains(m4)); |
1098 |
+ |
assertTrue(sm.contains(m5)); |
1099 |
+ |
Iterator i = sm.iterator(); |
1100 |
+ |
Object k; |
1101 |
+ |
k = (Integer)(i.next()); |
1102 |
+ |
assertEquals(m2, k); |
1103 |
+ |
k = (Integer)(i.next()); |
1104 |
+ |
assertEquals(m3, k); |
1105 |
+ |
k = (Integer)(i.next()); |
1106 |
+ |
assertEquals(m4, k); |
1107 |
+ |
k = (Integer)(i.next()); |
1108 |
+ |
assertEquals(m5, k); |
1109 |
+ |
assertFalse(i.hasNext()); |
1110 |
+ |
|
1111 |
+ |
SortedSet ssm = sm.tailSet(m4); |
1112 |
+ |
assertEquals(m4, ssm.first()); |
1113 |
+ |
assertEquals(m5, ssm.last()); |
1114 |
+ |
assertTrue(ssm.remove(m4)); |
1115 |
+ |
assertEquals(1, ssm.size()); |
1116 |
+ |
assertEquals(3, sm.size()); |
1117 |
+ |
assertEquals(4, set.size()); |
1118 |
|
} |
1119 |
|
|
1120 |
|
} |