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