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), 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.navigableSubSet(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 |
< |
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.navigableTailSet(m1); |
80 |
> |
return set.tailSet(m1, false); |
81 |
|
} |
82 |
< |
|
82 |
> |
|
83 |
> |
private static NavigableSet dset0() { |
84 |
> |
TreeSet set = new TreeSet(); |
85 |
> |
assertTrue(set.isEmpty()); |
86 |
> |
return set; |
87 |
> |
} |
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 |
|
/** |
180 |
|
* addAll of a collection with null elements throws NPE |
181 |
|
*/ |
185 |
|
Integer[] ints = new Integer[SIZE]; |
186 |
|
q.addAll(Arrays.asList(ints)); |
187 |
|
shouldThrow(); |
188 |
< |
} |
175 |
< |
catch (NullPointerException success) {} |
188 |
> |
} catch (NullPointerException success) {} |
189 |
|
} |
190 |
+ |
|
191 |
|
/** |
192 |
|
* addAll of a collection with any null elements throws NPE after |
193 |
|
* possibly adding some elements |
200 |
|
ints[i] = new Integer(i+SIZE); |
201 |
|
q.addAll(Arrays.asList(ints)); |
202 |
|
shouldThrow(); |
203 |
< |
} |
190 |
< |
catch (NullPointerException success) {} |
203 |
> |
} catch (NullPointerException success) {} |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
|
* Set contains all elements of successful addAll |
208 |
|
*/ |
209 |
|
public void testAddAll5() { |
210 |
< |
try { |
211 |
< |
Integer[] empty = new Integer[0]; |
212 |
< |
Integer[] ints = new Integer[SIZE]; |
213 |
< |
for (int i = 0; i < SIZE; ++i) |
214 |
< |
ints[i] = new Integer(SIZE-1- i); |
215 |
< |
NavigableSet q = set0(); |
216 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
217 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
218 |
< |
for (int i = 0; i < SIZE; ++i) |
206 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
207 |
< |
} |
208 |
< |
finally {} |
210 |
> |
Integer[] empty = new Integer[0]; |
211 |
> |
Integer[] ints = new Integer[SIZE]; |
212 |
> |
for (int i = 0; i < SIZE; ++i) |
213 |
> |
ints[i] = new Integer(SIZE-1- i); |
214 |
> |
NavigableSet q = set0(); |
215 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
216 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
217 |
> |
for (int i = 0; i < SIZE; ++i) |
218 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
219 |
|
} |
220 |
|
|
221 |
|
/** |
224 |
|
public void testPoll() { |
225 |
|
NavigableSet q = populatedSet(SIZE); |
226 |
|
for (int i = 0; i < SIZE; ++i) { |
227 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
227 |
> |
assertEquals(i, q.pollFirst()); |
228 |
|
} |
229 |
< |
assertNull(q.pollFirst()); |
229 |
> |
assertNull(q.pollFirst()); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
243 |
|
} |
244 |
|
assertTrue(q.isEmpty()); |
245 |
|
} |
246 |
< |
|
246 |
> |
|
247 |
|
/** |
248 |
|
* contains(x) reports true when elements added but not yet removed |
249 |
|
*/ |
264 |
|
q.clear(); |
265 |
|
assertTrue(q.isEmpty()); |
266 |
|
assertEquals(0, q.size()); |
267 |
< |
q.add(new Integer(1)); |
267 |
> |
assertTrue(q.add(new Integer(1))); |
268 |
|
assertFalse(q.isEmpty()); |
269 |
|
q.clear(); |
270 |
|
assertTrue(q.isEmpty()); |
319 |
|
} |
320 |
|
} |
321 |
|
|
322 |
< |
|
322 |
> |
|
323 |
|
|
324 |
|
/** |
325 |
|
* lower returns preceding element |
337 |
|
|
338 |
|
Object e4 = q.lower(zero); |
339 |
|
assertNull(e4); |
330 |
– |
|
340 |
|
} |
341 |
|
|
342 |
|
/** |
355 |
|
|
356 |
|
Object e4 = q.higher(six); |
357 |
|
assertNull(e4); |
349 |
– |
|
358 |
|
} |
359 |
|
|
360 |
|
/** |
373 |
|
|
374 |
|
Object e4 = q.floor(zero); |
375 |
|
assertNull(e4); |
368 |
– |
|
376 |
|
} |
377 |
|
|
378 |
|
/** |
391 |
|
|
392 |
|
Object e4 = q.ceiling(six); |
393 |
|
assertNull(e4); |
387 |
– |
|
394 |
|
} |
395 |
|
|
396 |
|
/** |
398 |
|
*/ |
399 |
|
public void testToArray() { |
400 |
|
NavigableSet q = populatedSet(SIZE); |
401 |
< |
Object[] o = q.toArray(); |
401 |
> |
Object[] o = q.toArray(); |
402 |
|
Arrays.sort(o); |
403 |
< |
for(int i = 0; i < o.length; i++) |
404 |
< |
assertEquals(o[i], q.pollFirst()); |
403 |
> |
for (int i = 0; i < o.length; i++) |
404 |
> |
assertEquals(o[i], q.pollFirst()); |
405 |
|
} |
406 |
|
|
407 |
|
/** |
409 |
|
*/ |
410 |
|
public void testToArray2() { |
411 |
|
NavigableSet q = populatedSet(SIZE); |
412 |
< |
Integer[] ints = new Integer[SIZE]; |
413 |
< |
ints = (Integer[])q.toArray(ints); |
412 |
> |
Integer[] ints = new Integer[SIZE]; |
413 |
> |
ints = (Integer[])q.toArray(ints); |
414 |
|
Arrays.sort(ints); |
415 |
< |
for(int i = 0; i < ints.length; i++) |
415 |
> |
for (int i = 0; i < ints.length; i++) |
416 |
|
assertEquals(ints[i], q.pollFirst()); |
417 |
|
} |
418 |
< |
|
418 |
> |
|
419 |
|
/** |
420 |
|
* iterator iterates through all elements |
421 |
|
*/ |
422 |
|
public void testIterator() { |
423 |
|
NavigableSet q = populatedSet(SIZE); |
424 |
|
int i = 0; |
425 |
< |
Iterator it = q.iterator(); |
426 |
< |
while(it.hasNext()) { |
425 |
> |
Iterator it = q.iterator(); |
426 |
> |
while (it.hasNext()) { |
427 |
|
assertTrue(q.contains(it.next())); |
428 |
|
++i; |
429 |
|
} |
436 |
|
public void testEmptyIterator() { |
437 |
|
NavigableSet q = set0(); |
438 |
|
int i = 0; |
439 |
< |
Iterator it = q.iterator(); |
440 |
< |
while(it.hasNext()) { |
439 |
> |
Iterator it = q.iterator(); |
440 |
> |
while (it.hasNext()) { |
441 |
|
assertTrue(q.contains(it.next())); |
442 |
|
++i; |
443 |
|
} |
447 |
|
/** |
448 |
|
* iterator.remove removes current element |
449 |
|
*/ |
450 |
< |
public void testIteratorRemove () { |
450 |
> |
public void testIteratorRemove() { |
451 |
|
final NavigableSet q = set0(); |
452 |
|
q.add(new Integer(2)); |
453 |
|
q.add(new Integer(1)); |
458 |
|
it.remove(); |
459 |
|
|
460 |
|
it = q.iterator(); |
461 |
< |
assertEquals(it.next(), new Integer(2)); |
462 |
< |
assertEquals(it.next(), new Integer(3)); |
461 |
> |
assertEquals(it.next(), 2); |
462 |
> |
assertEquals(it.next(), 3); |
463 |
|
assertFalse(it.hasNext()); |
464 |
|
} |
465 |
|
|
473 |
|
for (int i = 0; i < SIZE; ++i) { |
474 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
475 |
|
} |
476 |
< |
} |
476 |
> |
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* A deserialized serialized set has same elements |
479 |
> |
* A deserialized serialized set has same elements |
480 |
|
*/ |
481 |
< |
public void testSerialization() { |
481 |
> |
public void testSerialization() throws Exception { |
482 |
|
NavigableSet q = populatedSet(SIZE); |
483 |
< |
try { |
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()) |
488 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
489 |
< |
} catch(Exception e){ |
490 |
< |
e.printStackTrace(); |
491 |
< |
unexpectedException(); |
492 |
< |
} |
483 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
484 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
485 |
> |
out.writeObject(q); |
486 |
> |
out.close(); |
487 |
> |
|
488 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
489 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
490 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
491 |
> |
assertEquals(q.size(), r.size()); |
492 |
> |
while (!q.isEmpty()) |
493 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
610 |
|
assertEquals(1, ssm.size()); |
611 |
|
assertEquals(3, sm.size()); |
612 |
|
assertEquals(4, set.size()); |
613 |
+ |
} |
614 |
+ |
|
615 |
+ |
/** |
616 |
+ |
* size changes when elements added and removed |
617 |
+ |
*/ |
618 |
+ |
public void testDescendingSize() { |
619 |
+ |
NavigableSet q = populatedSet(SIZE); |
620 |
+ |
for (int i = 0; i < SIZE; ++i) { |
621 |
+ |
assertEquals(SIZE-i, q.size()); |
622 |
+ |
q.pollFirst(); |
623 |
+ |
} |
624 |
+ |
for (int i = 0; i < SIZE; ++i) { |
625 |
+ |
assertEquals(i, q.size()); |
626 |
+ |
q.add(new Integer(i)); |
627 |
+ |
} |
628 |
+ |
} |
629 |
+ |
|
630 |
+ |
/** |
631 |
+ |
* Add of comparable element succeeds |
632 |
+ |
*/ |
633 |
+ |
public void testDescendingAdd() { |
634 |
+ |
NavigableSet q = dset0(); |
635 |
+ |
assertTrue(q.add(m6)); |
636 |
+ |
} |
637 |
+ |
|
638 |
+ |
/** |
639 |
+ |
* Add of duplicate element fails |
640 |
+ |
*/ |
641 |
+ |
public void testDescendingAddDup() { |
642 |
+ |
NavigableSet q = dset0(); |
643 |
+ |
assertTrue(q.add(m6)); |
644 |
+ |
assertFalse(q.add(m6)); |
645 |
+ |
} |
646 |
+ |
|
647 |
+ |
/** |
648 |
+ |
* Add of non-Comparable throws CCE |
649 |
+ |
*/ |
650 |
+ |
public void testDescendingAddNonComparable() { |
651 |
+ |
try { |
652 |
+ |
NavigableSet q = dset0(); |
653 |
+ |
q.add(new Object()); |
654 |
+ |
q.add(new Object()); |
655 |
+ |
q.add(new Object()); |
656 |
+ |
shouldThrow(); |
657 |
+ |
} catch (ClassCastException success) {} |
658 |
+ |
} |
659 |
+ |
|
660 |
+ |
|
661 |
+ |
/** |
662 |
+ |
* addAll(null) throws NPE |
663 |
+ |
*/ |
664 |
+ |
public void testDescendingAddAll1() { |
665 |
+ |
try { |
666 |
+ |
NavigableSet q = dset0(); |
667 |
+ |
q.addAll(null); |
668 |
+ |
shouldThrow(); |
669 |
+ |
} catch (NullPointerException success) {} |
670 |
+ |
} |
671 |
+ |
|
672 |
+ |
/** |
673 |
+ |
* addAll of a collection with null elements throws NPE |
674 |
+ |
*/ |
675 |
+ |
public void testDescendingAddAll2() { |
676 |
+ |
try { |
677 |
+ |
NavigableSet q = dset0(); |
678 |
+ |
Integer[] ints = new Integer[SIZE]; |
679 |
+ |
q.addAll(Arrays.asList(ints)); |
680 |
+ |
shouldThrow(); |
681 |
+ |
} catch (NullPointerException success) {} |
682 |
+ |
} |
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 |
+ |
* higher returns next element |
837 |
+ |
*/ |
838 |
+ |
public void testDescendingHigher() { |
839 |
+ |
NavigableSet q = dset5(); |
840 |
+ |
Object e1 = q.higher(m3); |
841 |
+ |
assertEquals(m4, e1); |
842 |
+ |
|
843 |
+ |
Object e2 = q.higher(zero); |
844 |
+ |
assertEquals(m1, e2); |
845 |
+ |
|
846 |
+ |
Object e3 = q.higher(m5); |
847 |
+ |
assertNull(e3); |
848 |
+ |
|
849 |
+ |
Object e4 = q.higher(m6); |
850 |
+ |
assertNull(e4); |
851 |
+ |
} |
852 |
+ |
|
853 |
+ |
/** |
854 |
+ |
* floor returns preceding element |
855 |
+ |
*/ |
856 |
+ |
public void testDescendingFloor() { |
857 |
+ |
NavigableSet q = dset5(); |
858 |
+ |
Object e1 = q.floor(m3); |
859 |
+ |
assertEquals(m3, e1); |
860 |
+ |
|
861 |
+ |
Object e2 = q.floor(m6); |
862 |
+ |
assertEquals(m5, e2); |
863 |
+ |
|
864 |
+ |
Object e3 = q.floor(m1); |
865 |
+ |
assertEquals(m1, e3); |
866 |
+ |
|
867 |
+ |
Object e4 = q.floor(zero); |
868 |
+ |
assertNull(e4); |
869 |
+ |
} |
870 |
+ |
|
871 |
+ |
/** |
872 |
+ |
* ceiling returns next element |
873 |
+ |
*/ |
874 |
+ |
public void testDescendingCeiling() { |
875 |
+ |
NavigableSet q = dset5(); |
876 |
+ |
Object e1 = q.ceiling(m3); |
877 |
+ |
assertEquals(m3, e1); |
878 |
+ |
|
879 |
+ |
Object e2 = q.ceiling(zero); |
880 |
+ |
assertEquals(m1, e2); |
881 |
+ |
|
882 |
+ |
Object e3 = q.ceiling(m5); |
883 |
+ |
assertEquals(m5, e3); |
884 |
+ |
|
885 |
+ |
Object e4 = q.ceiling(m6); |
886 |
+ |
assertNull(e4); |
887 |
+ |
} |
888 |
+ |
|
889 |
+ |
/** |
890 |
+ |
* toArray contains all elements |
891 |
+ |
*/ |
892 |
+ |
public void testDescendingToArray() { |
893 |
+ |
NavigableSet q = populatedSet(SIZE); |
894 |
+ |
Object[] o = q.toArray(); |
895 |
+ |
Arrays.sort(o); |
896 |
+ |
for (int i = 0; i < o.length; i++) |
897 |
+ |
assertEquals(o[i], q.pollFirst()); |
898 |
+ |
} |
899 |
+ |
|
900 |
+ |
/** |
901 |
+ |
* toArray(a) contains all elements |
902 |
+ |
*/ |
903 |
+ |
public void testDescendingToArray2() { |
904 |
+ |
NavigableSet q = populatedSet(SIZE); |
905 |
+ |
Integer[] ints = new Integer[SIZE]; |
906 |
+ |
ints = (Integer[])q.toArray(ints); |
907 |
+ |
Arrays.sort(ints); |
908 |
+ |
for (int i = 0; i < ints.length; i++) |
909 |
+ |
assertEquals(ints[i], q.pollFirst()); |
910 |
+ |
} |
911 |
+ |
|
912 |
+ |
/** |
913 |
+ |
* iterator iterates through all elements |
914 |
+ |
*/ |
915 |
+ |
public void testDescendingIterator() { |
916 |
+ |
NavigableSet q = populatedSet(SIZE); |
917 |
+ |
int i = 0; |
918 |
+ |
Iterator it = q.iterator(); |
919 |
+ |
while (it.hasNext()) { |
920 |
+ |
assertTrue(q.contains(it.next())); |
921 |
+ |
++i; |
922 |
+ |
} |
923 |
+ |
assertEquals(i, SIZE); |
924 |
+ |
} |
925 |
+ |
|
926 |
+ |
/** |
927 |
+ |
* iterator of empty set has no elements |
928 |
+ |
*/ |
929 |
+ |
public void testDescendingEmptyIterator() { |
930 |
+ |
NavigableSet q = dset0(); |
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, 0); |
938 |
+ |
} |
939 |
+ |
|
940 |
+ |
/** |
941 |
+ |
* iterator.remove removes current element |
942 |
+ |
*/ |
943 |
+ |
public void testDescendingIteratorRemove() { |
944 |
+ |
final NavigableSet q = dset0(); |
945 |
+ |
q.add(new Integer(2)); |
946 |
+ |
q.add(new Integer(1)); |
947 |
+ |
q.add(new Integer(3)); |
948 |
+ |
|
949 |
+ |
Iterator it = q.iterator(); |
950 |
+ |
it.next(); |
951 |
+ |
it.remove(); |
952 |
+ |
|
953 |
+ |
it = q.iterator(); |
954 |
+ |
assertEquals(it.next(), 2); |
955 |
+ |
assertEquals(it.next(), 3); |
956 |
+ |
assertFalse(it.hasNext()); |
957 |
+ |
} |
958 |
+ |
|
959 |
+ |
|
960 |
+ |
/** |
961 |
+ |
* toString contains toStrings of elements |
962 |
+ |
*/ |
963 |
+ |
public void testDescendingToString() { |
964 |
+ |
NavigableSet q = populatedSet(SIZE); |
965 |
+ |
String s = q.toString(); |
966 |
+ |
for (int i = 0; i < SIZE; ++i) { |
967 |
+ |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
968 |
+ |
} |
969 |
+ |
} |
970 |
+ |
|
971 |
+ |
/** |
972 |
+ |
* A deserialized serialized set has same elements |
973 |
+ |
*/ |
974 |
+ |
public void testDescendingSerialization() throws Exception { |
975 |
+ |
NavigableSet q = populatedSet(SIZE); |
976 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
977 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
978 |
+ |
out.writeObject(q); |
979 |
+ |
out.close(); |
980 |
+ |
|
981 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
982 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
983 |
+ |
NavigableSet r = (NavigableSet)in.readObject(); |
984 |
+ |
assertEquals(q.size(), r.size()); |
985 |
+ |
while (!q.isEmpty()) |
986 |
+ |
assertEquals(q.pollFirst(), r.pollFirst()); |
987 |
+ |
} |
988 |
+ |
|
989 |
+ |
/** |
990 |
+ |
* subSet returns set with keys in requested range |
991 |
+ |
*/ |
992 |
+ |
public void testDescendingSubSetContents() { |
993 |
+ |
NavigableSet set = dset5(); |
994 |
+ |
SortedSet sm = set.subSet(m2, m4); |
995 |
+ |
assertEquals(m2, sm.first()); |
996 |
+ |
assertEquals(m3, sm.last()); |
997 |
+ |
assertEquals(2, sm.size()); |
998 |
+ |
assertFalse(sm.contains(m1)); |
999 |
+ |
assertTrue(sm.contains(m2)); |
1000 |
+ |
assertTrue(sm.contains(m3)); |
1001 |
+ |
assertFalse(sm.contains(m4)); |
1002 |
+ |
assertFalse(sm.contains(m5)); |
1003 |
+ |
Iterator i = sm.iterator(); |
1004 |
+ |
Object k; |
1005 |
+ |
k = (Integer)(i.next()); |
1006 |
+ |
assertEquals(m2, k); |
1007 |
+ |
k = (Integer)(i.next()); |
1008 |
+ |
assertEquals(m3, k); |
1009 |
+ |
assertFalse(i.hasNext()); |
1010 |
+ |
Iterator j = sm.iterator(); |
1011 |
+ |
j.next(); |
1012 |
+ |
j.remove(); |
1013 |
+ |
assertFalse(set.contains(m2)); |
1014 |
+ |
assertEquals(4, set.size()); |
1015 |
+ |
assertEquals(1, sm.size()); |
1016 |
+ |
assertEquals(m3, sm.first()); |
1017 |
+ |
assertEquals(m3, sm.last()); |
1018 |
+ |
assertTrue(sm.remove(m3)); |
1019 |
+ |
assertTrue(sm.isEmpty()); |
1020 |
+ |
assertEquals(3, set.size()); |
1021 |
+ |
} |
1022 |
+ |
|
1023 |
+ |
public void testDescendingSubSetContents2() { |
1024 |
+ |
NavigableSet set = dset5(); |
1025 |
+ |
SortedSet sm = set.subSet(m2, m3); |
1026 |
+ |
assertEquals(1, sm.size()); |
1027 |
+ |
assertEquals(m2, sm.first()); |
1028 |
+ |
assertEquals(m2, sm.last()); |
1029 |
+ |
assertFalse(sm.contains(m1)); |
1030 |
+ |
assertTrue(sm.contains(m2)); |
1031 |
+ |
assertFalse(sm.contains(m3)); |
1032 |
+ |
assertFalse(sm.contains(m4)); |
1033 |
+ |
assertFalse(sm.contains(m5)); |
1034 |
+ |
Iterator i = sm.iterator(); |
1035 |
+ |
Object k; |
1036 |
+ |
k = (Integer)(i.next()); |
1037 |
+ |
assertEquals(m2, k); |
1038 |
+ |
assertFalse(i.hasNext()); |
1039 |
+ |
Iterator j = sm.iterator(); |
1040 |
+ |
j.next(); |
1041 |
+ |
j.remove(); |
1042 |
+ |
assertFalse(set.contains(m2)); |
1043 |
+ |
assertEquals(4, set.size()); |
1044 |
+ |
assertEquals(0, sm.size()); |
1045 |
+ |
assertTrue(sm.isEmpty()); |
1046 |
+ |
assertFalse(sm.remove(m3)); |
1047 |
+ |
assertEquals(4, set.size()); |
1048 |
+ |
} |
1049 |
+ |
|
1050 |
+ |
/** |
1051 |
+ |
* headSet returns set with keys in requested range |
1052 |
+ |
*/ |
1053 |
+ |
public void testDescendingHeadSetContents() { |
1054 |
+ |
NavigableSet set = dset5(); |
1055 |
+ |
SortedSet sm = set.headSet(m4); |
1056 |
+ |
assertTrue(sm.contains(m1)); |
1057 |
+ |
assertTrue(sm.contains(m2)); |
1058 |
+ |
assertTrue(sm.contains(m3)); |
1059 |
+ |
assertFalse(sm.contains(m4)); |
1060 |
+ |
assertFalse(sm.contains(m5)); |
1061 |
+ |
Iterator i = sm.iterator(); |
1062 |
+ |
Object k; |
1063 |
+ |
k = (Integer)(i.next()); |
1064 |
+ |
assertEquals(m1, k); |
1065 |
+ |
k = (Integer)(i.next()); |
1066 |
+ |
assertEquals(m2, k); |
1067 |
+ |
k = (Integer)(i.next()); |
1068 |
+ |
assertEquals(m3, k); |
1069 |
+ |
assertFalse(i.hasNext()); |
1070 |
+ |
sm.clear(); |
1071 |
+ |
assertTrue(sm.isEmpty()); |
1072 |
+ |
assertEquals(2, set.size()); |
1073 |
+ |
assertEquals(m4, set.first()); |
1074 |
+ |
} |
1075 |
+ |
|
1076 |
+ |
/** |
1077 |
+ |
* tailSet returns set with keys in requested range |
1078 |
+ |
*/ |
1079 |
+ |
public void testDescendingTailSetContents() { |
1080 |
+ |
NavigableSet set = dset5(); |
1081 |
+ |
SortedSet sm = set.tailSet(m2); |
1082 |
+ |
assertFalse(sm.contains(m1)); |
1083 |
+ |
assertTrue(sm.contains(m2)); |
1084 |
+ |
assertTrue(sm.contains(m3)); |
1085 |
+ |
assertTrue(sm.contains(m4)); |
1086 |
+ |
assertTrue(sm.contains(m5)); |
1087 |
+ |
Iterator i = sm.iterator(); |
1088 |
+ |
Object k; |
1089 |
+ |
k = (Integer)(i.next()); |
1090 |
+ |
assertEquals(m2, k); |
1091 |
+ |
k = (Integer)(i.next()); |
1092 |
+ |
assertEquals(m3, k); |
1093 |
+ |
k = (Integer)(i.next()); |
1094 |
+ |
assertEquals(m4, k); |
1095 |
+ |
k = (Integer)(i.next()); |
1096 |
+ |
assertEquals(m5, k); |
1097 |
+ |
assertFalse(i.hasNext()); |
1098 |
+ |
|
1099 |
+ |
SortedSet ssm = sm.tailSet(m4); |
1100 |
+ |
assertEquals(m4, ssm.first()); |
1101 |
+ |
assertEquals(m5, ssm.last()); |
1102 |
+ |
assertTrue(ssm.remove(m4)); |
1103 |
+ |
assertEquals(1, ssm.size()); |
1104 |
+ |
assertEquals(3, sm.size()); |
1105 |
+ |
assertEquals(4, set.size()); |
1106 |
|
} |
1107 |
|
|
1108 |
|
} |