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.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 |
< |
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, true); |
84 |
> |
} |
85 |
> |
|
86 |
> |
private static NavigableSet dset0() { |
87 |
> |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
88 |
|
assertTrue(set.isEmpty()); |
89 |
< |
return set.navigableTailSet(m1, true); |
89 |
> |
return set; |
90 |
|
} |
91 |
< |
|
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 |
|
* addAll of a collection with null elements throws NPE |
187 |
|
Integer[] ints = new Integer[SIZE]; |
188 |
|
q.addAll(Arrays.asList(ints)); |
189 |
|
shouldThrow(); |
190 |
< |
} |
175 |
< |
catch (NullPointerException success) {} |
190 |
> |
} catch (NullPointerException success) {} |
191 |
|
} |
192 |
|
/** |
193 |
|
* addAll of a collection with any null elements throws NPE after |
201 |
|
ints[i] = new Integer(i+SIZE); |
202 |
|
q.addAll(Arrays.asList(ints)); |
203 |
|
shouldThrow(); |
204 |
< |
} |
190 |
< |
catch (NullPointerException success) {} |
204 |
> |
} catch (NullPointerException success) {} |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
|
* Set contains all elements of successful addAll |
209 |
|
*/ |
210 |
|
public void testAddAll5() { |
211 |
< |
try { |
212 |
< |
Integer[] empty = new Integer[0]; |
213 |
< |
Integer[] ints = new Integer[SIZE]; |
214 |
< |
for (int i = 0; i < SIZE; ++i) |
215 |
< |
ints[i] = new Integer(SIZE-1- i); |
216 |
< |
NavigableSet q = set0(); |
217 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
218 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
219 |
< |
for (int i = 0; i < SIZE; ++i) |
206 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
207 |
< |
} |
208 |
< |
finally {} |
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) |
219 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
220 |
|
} |
221 |
|
|
222 |
|
/** |
225 |
|
public void testPoll() { |
226 |
|
NavigableSet q = populatedSet(SIZE); |
227 |
|
for (int i = 0; i < SIZE; ++i) { |
228 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
228 |
> |
assertEquals(i, q.pollFirst()); |
229 |
|
} |
230 |
< |
assertNull(q.pollFirst()); |
230 |
> |
assertNull(q.pollFirst()); |
231 |
|
} |
232 |
|
|
233 |
|
/** |
244 |
|
} |
245 |
|
assertTrue(q.isEmpty()); |
246 |
|
} |
247 |
< |
|
247 |
> |
|
248 |
|
/** |
249 |
|
* contains(x) reports true when elements added but not yet removed |
250 |
|
*/ |
320 |
|
} |
321 |
|
} |
322 |
|
|
323 |
< |
|
323 |
> |
|
324 |
|
|
325 |
|
/** |
326 |
|
* lower returns preceding element |
338 |
|
|
339 |
|
Object e4 = q.lower(zero); |
340 |
|
assertNull(e4); |
330 |
– |
|
341 |
|
} |
342 |
|
|
343 |
|
/** |
356 |
|
|
357 |
|
Object e4 = q.higher(six); |
358 |
|
assertNull(e4); |
349 |
– |
|
359 |
|
} |
360 |
|
|
361 |
|
/** |
374 |
|
|
375 |
|
Object e4 = q.floor(zero); |
376 |
|
assertNull(e4); |
368 |
– |
|
377 |
|
} |
378 |
|
|
379 |
|
/** |
392 |
|
|
393 |
|
Object e4 = q.ceiling(six); |
394 |
|
assertNull(e4); |
387 |
– |
|
395 |
|
} |
396 |
|
|
397 |
|
/** |
399 |
|
*/ |
400 |
|
public void testToArray() { |
401 |
|
NavigableSet q = populatedSet(SIZE); |
402 |
< |
Object[] o = q.toArray(); |
402 |
> |
Object[] o = q.toArray(); |
403 |
|
Arrays.sort(o); |
404 |
< |
for(int i = 0; i < o.length; i++) |
405 |
< |
assertEquals(o[i], q.pollFirst()); |
404 |
> |
for (int i = 0; i < o.length; i++) |
405 |
> |
assertEquals(o[i], q.pollFirst()); |
406 |
|
} |
407 |
|
|
408 |
|
/** |
410 |
|
*/ |
411 |
|
public void testToArray2() { |
412 |
|
NavigableSet q = populatedSet(SIZE); |
413 |
< |
Integer[] ints = new Integer[SIZE]; |
414 |
< |
ints = (Integer[])q.toArray(ints); |
413 |
> |
Integer[] ints = new Integer[SIZE]; |
414 |
> |
ints = (Integer[])q.toArray(ints); |
415 |
|
Arrays.sort(ints); |
416 |
< |
for(int i = 0; i < ints.length; i++) |
416 |
> |
for (int i = 0; i < ints.length; i++) |
417 |
|
assertEquals(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 |
|
} |
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 |
+ |
* addAll of a collection with null elements throws NPE |
685 |
+ |
*/ |
686 |
+ |
public void testDescendingAddAll2() { |
687 |
+ |
try { |
688 |
+ |
NavigableSet q = dset0(); |
689 |
+ |
Integer[] ints = new Integer[SIZE]; |
690 |
+ |
q.addAll(Arrays.asList(ints)); |
691 |
+ |
shouldThrow(); |
692 |
+ |
} catch (NullPointerException success) {} |
693 |
+ |
} |
694 |
+ |
/** |
695 |
+ |
* addAll of a collection with any null elements throws NPE after |
696 |
+ |
* possibly adding some elements |
697 |
+ |
*/ |
698 |
+ |
public void testDescendingAddAll3() { |
699 |
+ |
try { |
700 |
+ |
NavigableSet q = dset0(); |
701 |
+ |
Integer[] ints = new Integer[SIZE]; |
702 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
703 |
+ |
ints[i] = new Integer(i+SIZE); |
704 |
+ |
q.addAll(Arrays.asList(ints)); |
705 |
+ |
shouldThrow(); |
706 |
+ |
} catch (NullPointerException success) {} |
707 |
+ |
} |
708 |
+ |
|
709 |
+ |
/** |
710 |
+ |
* Set contains all elements of successful addAll |
711 |
+ |
*/ |
712 |
+ |
public void testDescendingAddAll5() { |
713 |
+ |
Integer[] empty = new Integer[0]; |
714 |
+ |
Integer[] ints = new Integer[SIZE]; |
715 |
+ |
for (int i = 0; i < SIZE; ++i) |
716 |
+ |
ints[i] = new Integer(SIZE-1- i); |
717 |
+ |
NavigableSet q = dset0(); |
718 |
+ |
assertFalse(q.addAll(Arrays.asList(empty))); |
719 |
+ |
assertTrue(q.addAll(Arrays.asList(ints))); |
720 |
+ |
for (int i = 0; i < SIZE; ++i) |
721 |
+ |
assertEquals(new Integer(i), q.pollFirst()); |
722 |
+ |
} |
723 |
+ |
|
724 |
+ |
/** |
725 |
+ |
* poll succeeds unless empty |
726 |
+ |
*/ |
727 |
+ |
public void testDescendingPoll() { |
728 |
+ |
NavigableSet q = populatedSet(SIZE); |
729 |
+ |
for (int i = 0; i < SIZE; ++i) { |
730 |
+ |
assertEquals(i, q.pollFirst()); |
731 |
+ |
} |
732 |
+ |
assertNull(q.pollFirst()); |
733 |
+ |
} |
734 |
+ |
|
735 |
+ |
/** |
736 |
+ |
* remove(x) removes x and returns true if present |
737 |
+ |
*/ |
738 |
+ |
public void testDescendingRemoveElement() { |
739 |
+ |
NavigableSet q = populatedSet(SIZE); |
740 |
+ |
for (int i = 1; i < SIZE; i+=2) { |
741 |
+ |
assertTrue(q.remove(new Integer(i))); |
742 |
+ |
} |
743 |
+ |
for (int i = 0; i < SIZE; i+=2) { |
744 |
+ |
assertTrue(q.remove(new Integer(i))); |
745 |
+ |
assertFalse(q.remove(new Integer(i+1))); |
746 |
+ |
} |
747 |
+ |
assertTrue(q.isEmpty()); |
748 |
+ |
} |
749 |
+ |
|
750 |
+ |
/** |
751 |
+ |
* contains(x) reports true when elements added but not yet removed |
752 |
+ |
*/ |
753 |
+ |
public void testDescendingContains() { |
754 |
+ |
NavigableSet q = populatedSet(SIZE); |
755 |
+ |
for (int i = 0; i < SIZE; ++i) { |
756 |
+ |
assertTrue(q.contains(new Integer(i))); |
757 |
+ |
q.pollFirst(); |
758 |
+ |
assertFalse(q.contains(new Integer(i))); |
759 |
+ |
} |
760 |
+ |
} |
761 |
+ |
|
762 |
+ |
/** |
763 |
+ |
* clear removes all elements |
764 |
+ |
*/ |
765 |
+ |
public void testDescendingClear() { |
766 |
+ |
NavigableSet q = populatedSet(SIZE); |
767 |
+ |
q.clear(); |
768 |
+ |
assertTrue(q.isEmpty()); |
769 |
+ |
assertEquals(0, q.size()); |
770 |
+ |
q.add(new Integer(1)); |
771 |
+ |
assertFalse(q.isEmpty()); |
772 |
+ |
q.clear(); |
773 |
+ |
assertTrue(q.isEmpty()); |
774 |
+ |
} |
775 |
+ |
|
776 |
+ |
/** |
777 |
+ |
* containsAll(c) is true when c contains a subset of elements |
778 |
+ |
*/ |
779 |
+ |
public void testDescendingContainsAll() { |
780 |
+ |
NavigableSet q = populatedSet(SIZE); |
781 |
+ |
NavigableSet p = dset0(); |
782 |
+ |
for (int i = 0; i < SIZE; ++i) { |
783 |
+ |
assertTrue(q.containsAll(p)); |
784 |
+ |
assertFalse(p.containsAll(q)); |
785 |
+ |
p.add(new Integer(i)); |
786 |
+ |
} |
787 |
+ |
assertTrue(p.containsAll(q)); |
788 |
+ |
} |
789 |
+ |
|
790 |
+ |
/** |
791 |
+ |
* retainAll(c) retains only those elements of c and reports true if changed |
792 |
+ |
*/ |
793 |
+ |
public void testDescendingRetainAll() { |
794 |
+ |
NavigableSet q = populatedSet(SIZE); |
795 |
+ |
NavigableSet p = populatedSet(SIZE); |
796 |
+ |
for (int i = 0; i < SIZE; ++i) { |
797 |
+ |
boolean changed = q.retainAll(p); |
798 |
+ |
if (i == 0) |
799 |
+ |
assertFalse(changed); |
800 |
+ |
else |
801 |
+ |
assertTrue(changed); |
802 |
+ |
|
803 |
+ |
assertTrue(q.containsAll(p)); |
804 |
+ |
assertEquals(SIZE-i, q.size()); |
805 |
+ |
p.pollFirst(); |
806 |
+ |
} |
807 |
+ |
} |
808 |
+ |
|
809 |
+ |
/** |
810 |
+ |
* removeAll(c) removes only those elements of c and reports true if changed |
811 |
+ |
*/ |
812 |
+ |
public void testDescendingRemoveAll() { |
813 |
+ |
for (int i = 1; i < SIZE; ++i) { |
814 |
+ |
NavigableSet q = populatedSet(SIZE); |
815 |
+ |
NavigableSet p = populatedSet(i); |
816 |
+ |
assertTrue(q.removeAll(p)); |
817 |
+ |
assertEquals(SIZE-i, q.size()); |
818 |
+ |
for (int j = 0; j < i; ++j) { |
819 |
+ |
Integer I = (Integer)(p.pollFirst()); |
820 |
+ |
assertFalse(q.contains(I)); |
821 |
+ |
} |
822 |
+ |
} |
823 |
+ |
} |
824 |
+ |
|
825 |
+ |
|
826 |
+ |
|
827 |
+ |
/** |
828 |
+ |
* lower returns preceding element |
829 |
+ |
*/ |
830 |
+ |
public void testDescendingLower() { |
831 |
+ |
NavigableSet q = dset5(); |
832 |
+ |
Object e1 = q.lower(m3); |
833 |
+ |
assertEquals(m2, e1); |
834 |
+ |
|
835 |
+ |
Object e2 = q.lower(m6); |
836 |
+ |
assertEquals(m5, e2); |
837 |
+ |
|
838 |
+ |
Object e3 = q.lower(m1); |
839 |
+ |
assertNull(e3); |
840 |
+ |
|
841 |
+ |
Object e4 = q.lower(zero); |
842 |
+ |
assertNull(e4); |
843 |
+ |
} |
844 |
+ |
|
845 |
+ |
/** |
846 |
+ |
* higher returns next element |
847 |
+ |
*/ |
848 |
+ |
public void testDescendingHigher() { |
849 |
+ |
NavigableSet q = dset5(); |
850 |
+ |
Object e1 = q.higher(m3); |
851 |
+ |
assertEquals(m4, e1); |
852 |
+ |
|
853 |
+ |
Object e2 = q.higher(zero); |
854 |
+ |
assertEquals(m1, e2); |
855 |
+ |
|
856 |
+ |
Object e3 = q.higher(m5); |
857 |
+ |
assertNull(e3); |
858 |
+ |
|
859 |
+ |
Object e4 = q.higher(m6); |
860 |
+ |
assertNull(e4); |
861 |
+ |
} |
862 |
+ |
|
863 |
+ |
/** |
864 |
+ |
* floor returns preceding element |
865 |
+ |
*/ |
866 |
+ |
public void testDescendingFloor() { |
867 |
+ |
NavigableSet q = dset5(); |
868 |
+ |
Object e1 = q.floor(m3); |
869 |
+ |
assertEquals(m3, e1); |
870 |
+ |
|
871 |
+ |
Object e2 = q.floor(m6); |
872 |
+ |
assertEquals(m5, e2); |
873 |
+ |
|
874 |
+ |
Object e3 = q.floor(m1); |
875 |
+ |
assertEquals(m1, e3); |
876 |
+ |
|
877 |
+ |
Object e4 = q.floor(zero); |
878 |
+ |
assertNull(e4); |
879 |
+ |
} |
880 |
+ |
|
881 |
+ |
/** |
882 |
+ |
* ceiling returns next element |
883 |
+ |
*/ |
884 |
+ |
public void testDescendingCeiling() { |
885 |
+ |
NavigableSet q = dset5(); |
886 |
+ |
Object e1 = q.ceiling(m3); |
887 |
+ |
assertEquals(m3, e1); |
888 |
+ |
|
889 |
+ |
Object e2 = q.ceiling(zero); |
890 |
+ |
assertEquals(m1, e2); |
891 |
+ |
|
892 |
+ |
Object e3 = q.ceiling(m5); |
893 |
+ |
assertEquals(m5, e3); |
894 |
+ |
|
895 |
+ |
Object e4 = q.ceiling(m6); |
896 |
+ |
assertNull(e4); |
897 |
+ |
} |
898 |
+ |
|
899 |
+ |
/** |
900 |
+ |
* toArray contains all elements |
901 |
+ |
*/ |
902 |
+ |
public void testDescendingToArray() { |
903 |
+ |
NavigableSet q = populatedSet(SIZE); |
904 |
+ |
Object[] o = q.toArray(); |
905 |
+ |
Arrays.sort(o); |
906 |
+ |
for (int i = 0; i < o.length; i++) |
907 |
+ |
assertEquals(o[i], q.pollFirst()); |
908 |
+ |
} |
909 |
+ |
|
910 |
+ |
/** |
911 |
+ |
* toArray(a) contains all elements |
912 |
+ |
*/ |
913 |
+ |
public void testDescendingToArray2() { |
914 |
+ |
NavigableSet q = populatedSet(SIZE); |
915 |
+ |
Integer[] ints = new Integer[SIZE]; |
916 |
+ |
ints = (Integer[])q.toArray(ints); |
917 |
+ |
Arrays.sort(ints); |
918 |
+ |
for (int i = 0; i < ints.length; i++) |
919 |
+ |
assertEquals(ints[i], q.pollFirst()); |
920 |
+ |
} |
921 |
+ |
|
922 |
+ |
/** |
923 |
+ |
* iterator iterates through all elements |
924 |
+ |
*/ |
925 |
+ |
public void testDescendingIterator() { |
926 |
+ |
NavigableSet q = populatedSet(SIZE); |
927 |
+ |
int i = 0; |
928 |
+ |
Iterator it = q.iterator(); |
929 |
+ |
while (it.hasNext()) { |
930 |
+ |
assertTrue(q.contains(it.next())); |
931 |
+ |
++i; |
932 |
+ |
} |
933 |
+ |
assertEquals(i, SIZE); |
934 |
+ |
} |
935 |
+ |
|
936 |
+ |
/** |
937 |
+ |
* iterator of empty set has no elements |
938 |
+ |
*/ |
939 |
+ |
public void testDescendingEmptyIterator() { |
940 |
+ |
NavigableSet q = dset0(); |
941 |
+ |
int i = 0; |
942 |
+ |
Iterator it = q.iterator(); |
943 |
+ |
while (it.hasNext()) { |
944 |
+ |
assertTrue(q.contains(it.next())); |
945 |
+ |
++i; |
946 |
+ |
} |
947 |
+ |
assertEquals(i, 0); |
948 |
+ |
} |
949 |
+ |
|
950 |
+ |
/** |
951 |
+ |
* iterator.remove removes current element |
952 |
+ |
*/ |
953 |
+ |
public void testDescendingIteratorRemove () { |
954 |
+ |
final NavigableSet q = dset0(); |
955 |
+ |
q.add(new Integer(2)); |
956 |
+ |
q.add(new Integer(1)); |
957 |
+ |
q.add(new Integer(3)); |
958 |
+ |
|
959 |
+ |
Iterator it = q.iterator(); |
960 |
+ |
it.next(); |
961 |
+ |
it.remove(); |
962 |
+ |
|
963 |
+ |
it = q.iterator(); |
964 |
+ |
assertEquals(it.next(), new Integer(2)); |
965 |
+ |
assertEquals(it.next(), new Integer(3)); |
966 |
+ |
assertFalse(it.hasNext()); |
967 |
+ |
} |
968 |
+ |
|
969 |
+ |
|
970 |
+ |
/** |
971 |
+ |
* toString contains toStrings of elements |
972 |
+ |
*/ |
973 |
+ |
public void testDescendingToString() { |
974 |
+ |
NavigableSet q = populatedSet(SIZE); |
975 |
+ |
String s = q.toString(); |
976 |
+ |
for (int i = 0; i < SIZE; ++i) { |
977 |
+ |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
978 |
+ |
} |
979 |
+ |
} |
980 |
+ |
|
981 |
+ |
/** |
982 |
+ |
* A deserialized serialized set has same elements |
983 |
+ |
*/ |
984 |
+ |
public void testDescendingSerialization() throws Exception { |
985 |
+ |
NavigableSet q = populatedSet(SIZE); |
986 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
987 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
988 |
+ |
out.writeObject(q); |
989 |
+ |
out.close(); |
990 |
+ |
|
991 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
992 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
993 |
+ |
NavigableSet r = (NavigableSet)in.readObject(); |
994 |
+ |
assertEquals(q.size(), r.size()); |
995 |
+ |
while (!q.isEmpty()) |
996 |
+ |
assertEquals(q.pollFirst(), r.pollFirst()); |
997 |
+ |
} |
998 |
+ |
|
999 |
+ |
/** |
1000 |
+ |
* subSet returns set with keys in requested range |
1001 |
+ |
*/ |
1002 |
+ |
public void testDescendingSubSetContents() { |
1003 |
+ |
NavigableSet set = dset5(); |
1004 |
+ |
SortedSet sm = set.subSet(m2, m4); |
1005 |
+ |
assertEquals(m2, sm.first()); |
1006 |
+ |
assertEquals(m3, sm.last()); |
1007 |
+ |
assertEquals(2, sm.size()); |
1008 |
+ |
assertFalse(sm.contains(m1)); |
1009 |
+ |
assertTrue(sm.contains(m2)); |
1010 |
+ |
assertTrue(sm.contains(m3)); |
1011 |
+ |
assertFalse(sm.contains(m4)); |
1012 |
+ |
assertFalse(sm.contains(m5)); |
1013 |
+ |
Iterator i = sm.iterator(); |
1014 |
+ |
Object k; |
1015 |
+ |
k = (Integer)(i.next()); |
1016 |
+ |
assertEquals(m2, k); |
1017 |
+ |
k = (Integer)(i.next()); |
1018 |
+ |
assertEquals(m3, k); |
1019 |
+ |
assertFalse(i.hasNext()); |
1020 |
+ |
Iterator j = sm.iterator(); |
1021 |
+ |
j.next(); |
1022 |
+ |
j.remove(); |
1023 |
+ |
assertFalse(set.contains(m2)); |
1024 |
+ |
assertEquals(4, set.size()); |
1025 |
+ |
assertEquals(1, sm.size()); |
1026 |
+ |
assertEquals(m3, sm.first()); |
1027 |
+ |
assertEquals(m3, sm.last()); |
1028 |
+ |
assertTrue(sm.remove(m3)); |
1029 |
+ |
assertTrue(sm.isEmpty()); |
1030 |
+ |
assertEquals(3, set.size()); |
1031 |
+ |
} |
1032 |
+ |
|
1033 |
+ |
public void testDescendingSubSetContents2() { |
1034 |
+ |
NavigableSet set = dset5(); |
1035 |
+ |
SortedSet sm = set.subSet(m2, m3); |
1036 |
+ |
assertEquals(1, sm.size()); |
1037 |
+ |
assertEquals(m2, sm.first()); |
1038 |
+ |
assertEquals(m2, sm.last()); |
1039 |
+ |
assertFalse(sm.contains(m1)); |
1040 |
+ |
assertTrue(sm.contains(m2)); |
1041 |
+ |
assertFalse(sm.contains(m3)); |
1042 |
+ |
assertFalse(sm.contains(m4)); |
1043 |
+ |
assertFalse(sm.contains(m5)); |
1044 |
+ |
Iterator i = sm.iterator(); |
1045 |
+ |
Object k; |
1046 |
+ |
k = (Integer)(i.next()); |
1047 |
+ |
assertEquals(m2, k); |
1048 |
+ |
assertFalse(i.hasNext()); |
1049 |
+ |
Iterator j = sm.iterator(); |
1050 |
+ |
j.next(); |
1051 |
+ |
j.remove(); |
1052 |
+ |
assertFalse(set.contains(m2)); |
1053 |
+ |
assertEquals(4, set.size()); |
1054 |
+ |
assertEquals(0, sm.size()); |
1055 |
+ |
assertTrue(sm.isEmpty()); |
1056 |
+ |
assertFalse(sm.remove(m3)); |
1057 |
+ |
assertEquals(4, set.size()); |
1058 |
+ |
} |
1059 |
+ |
|
1060 |
+ |
/** |
1061 |
+ |
* headSet returns set with keys in requested range |
1062 |
+ |
*/ |
1063 |
+ |
public void testDescendingHeadSetContents() { |
1064 |
+ |
NavigableSet set = dset5(); |
1065 |
+ |
SortedSet sm = set.headSet(m4); |
1066 |
+ |
assertTrue(sm.contains(m1)); |
1067 |
+ |
assertTrue(sm.contains(m2)); |
1068 |
+ |
assertTrue(sm.contains(m3)); |
1069 |
+ |
assertFalse(sm.contains(m4)); |
1070 |
+ |
assertFalse(sm.contains(m5)); |
1071 |
+ |
Iterator i = sm.iterator(); |
1072 |
+ |
Object k; |
1073 |
+ |
k = (Integer)(i.next()); |
1074 |
+ |
assertEquals(m1, k); |
1075 |
+ |
k = (Integer)(i.next()); |
1076 |
+ |
assertEquals(m2, k); |
1077 |
+ |
k = (Integer)(i.next()); |
1078 |
+ |
assertEquals(m3, k); |
1079 |
+ |
assertFalse(i.hasNext()); |
1080 |
+ |
sm.clear(); |
1081 |
+ |
assertTrue(sm.isEmpty()); |
1082 |
+ |
assertEquals(2, set.size()); |
1083 |
+ |
assertEquals(m4, set.first()); |
1084 |
+ |
} |
1085 |
+ |
|
1086 |
+ |
/** |
1087 |
+ |
* tailSet returns set with keys in requested range |
1088 |
+ |
*/ |
1089 |
+ |
public void testDescendingTailSetContents() { |
1090 |
+ |
NavigableSet set = dset5(); |
1091 |
+ |
SortedSet sm = set.tailSet(m2); |
1092 |
+ |
assertFalse(sm.contains(m1)); |
1093 |
+ |
assertTrue(sm.contains(m2)); |
1094 |
+ |
assertTrue(sm.contains(m3)); |
1095 |
+ |
assertTrue(sm.contains(m4)); |
1096 |
+ |
assertTrue(sm.contains(m5)); |
1097 |
+ |
Iterator i = sm.iterator(); |
1098 |
+ |
Object k; |
1099 |
+ |
k = (Integer)(i.next()); |
1100 |
+ |
assertEquals(m2, k); |
1101 |
+ |
k = (Integer)(i.next()); |
1102 |
+ |
assertEquals(m3, k); |
1103 |
+ |
k = (Integer)(i.next()); |
1104 |
+ |
assertEquals(m4, k); |
1105 |
+ |
k = (Integer)(i.next()); |
1106 |
+ |
assertEquals(m5, k); |
1107 |
+ |
assertFalse(i.hasNext()); |
1108 |
+ |
|
1109 |
+ |
SortedSet ssm = sm.tailSet(m4); |
1110 |
+ |
assertEquals(m4, ssm.first()); |
1111 |
+ |
assertEquals(m5, ssm.last()); |
1112 |
+ |
assertTrue(ssm.remove(m4)); |
1113 |
+ |
assertEquals(1, ssm.size()); |
1114 |
+ |
assertEquals(3, sm.size()); |
1115 |
+ |
assertEquals(4, set.size()); |
1116 |
|
} |
1117 |
|
|
1118 |
|
} |