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 { |
35 |
|
TreeSet q = new TreeSet(); |
36 |
|
assertTrue(q.isEmpty()); |
37 |
|
|
38 |
< |
for (int i = n-1; i >= 0; i-=2) |
39 |
< |
assertTrue(q.add(new Integer(i))); |
40 |
< |
for (int i = (n & 1); i < n; i+=2) |
41 |
< |
assertTrue(q.add(new Integer(i))); |
38 |
> |
for (int i = n-1; i >= 0; i-=2) |
39 |
> |
assertTrue(q.add(new Integer(i))); |
40 |
> |
for (int i = (n & 1); i < n; i+=2) |
41 |
> |
assertTrue(q.add(new Integer(i))); |
42 |
|
assertTrue(q.add(new Integer(-n))); |
43 |
|
assertTrue(q.add(new Integer(n))); |
44 |
|
NavigableSet s = q.subSet(new Integer(0), true, new Integer(n), false); |
45 |
|
assertFalse(s.isEmpty()); |
46 |
< |
assertEquals(n, s.size()); |
46 |
> |
assertEquals(n, s.size()); |
47 |
|
return s; |
48 |
|
} |
49 |
|
|
61 |
|
q.add(zero); |
62 |
|
q.add(seven); |
63 |
|
NavigableSet s = q.subSet(one, true, seven, false); |
64 |
< |
assertEquals(5, s.size()); |
64 |
> |
assertEquals(5, s.size()); |
65 |
|
return s; |
66 |
|
} |
67 |
|
|
74 |
|
q.add(m4); |
75 |
|
q.add(m5); |
76 |
|
NavigableSet s = q.descendingSet(); |
77 |
< |
assertEquals(5, s.size()); |
77 |
> |
assertEquals(5, s.size()); |
78 |
|
return s; |
79 |
|
} |
80 |
|
|
81 |
|
private static NavigableSet set0() { |
82 |
< |
TreeSet set = new TreeSet(); |
82 |
> |
TreeSet set = new TreeSet(); |
83 |
|
assertTrue(set.isEmpty()); |
84 |
|
return set.tailSet(m1, false); |
85 |
|
} |
86 |
|
|
87 |
|
private static NavigableSet dset0() { |
88 |
< |
TreeSet set = new TreeSet(); |
88 |
> |
TreeSet set = new TreeSet(); |
89 |
|
assertTrue(set.isEmpty()); |
90 |
|
return set; |
91 |
|
} |
131 |
|
* add(null) throws NPE |
132 |
|
*/ |
133 |
|
public void testAddNull() { |
134 |
< |
try { |
134 |
> |
try { |
135 |
|
NavigableSet q = set0(); |
136 |
|
q.add(null); |
137 |
|
shouldThrow(); |
138 |
< |
} catch (NullPointerException success) { } |
138 |
> |
} catch (NullPointerException success) {} |
139 |
|
} |
140 |
|
|
141 |
|
/** |
165 |
|
q.add(new Object()); |
166 |
|
q.add(new Object()); |
167 |
|
shouldThrow(); |
168 |
< |
} |
169 |
< |
catch (ClassCastException success) {} |
168 |
> |
} catch (ClassCastException success) {} |
169 |
|
} |
170 |
|
|
171 |
|
|
177 |
|
NavigableSet q = set0(); |
178 |
|
q.addAll(null); |
179 |
|
shouldThrow(); |
180 |
< |
} |
182 |
< |
catch (NullPointerException success) {} |
180 |
> |
} catch (NullPointerException success) {} |
181 |
|
} |
182 |
|
/** |
183 |
|
* addAll of a collection with null elements throws NPE |
188 |
|
Integer[] ints = new Integer[SIZE]; |
189 |
|
q.addAll(Arrays.asList(ints)); |
190 |
|
shouldThrow(); |
191 |
< |
} |
194 |
< |
catch (NullPointerException success) {} |
191 |
> |
} catch (NullPointerException success) {} |
192 |
|
} |
193 |
|
/** |
194 |
|
* addAll of a collection with any null elements throws NPE after |
202 |
|
ints[i] = new Integer(i+SIZE); |
203 |
|
q.addAll(Arrays.asList(ints)); |
204 |
|
shouldThrow(); |
205 |
< |
} |
209 |
< |
catch (NullPointerException success) {} |
205 |
> |
} catch (NullPointerException success) {} |
206 |
|
} |
207 |
|
|
208 |
|
/** |
231 |
|
for (int i = 0; i < SIZE; ++i) { |
232 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
233 |
|
} |
234 |
< |
assertNull(q.pollFirst()); |
234 |
> |
assertNull(q.pollFirst()); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
407 |
|
*/ |
408 |
|
public void testToArray() { |
409 |
|
NavigableSet q = populatedSet(SIZE); |
410 |
< |
Object[] o = q.toArray(); |
410 |
> |
Object[] o = q.toArray(); |
411 |
|
Arrays.sort(o); |
412 |
< |
for (int i = 0; i < o.length; i++) |
413 |
< |
assertEquals(o[i], q.pollFirst()); |
412 |
> |
for (int i = 0; i < o.length; i++) |
413 |
> |
assertEquals(o[i], q.pollFirst()); |
414 |
|
} |
415 |
|
|
416 |
|
/** |
418 |
|
*/ |
419 |
|
public void testToArray2() { |
420 |
|
NavigableSet q = populatedSet(SIZE); |
421 |
< |
Integer[] ints = new Integer[SIZE]; |
422 |
< |
ints = (Integer[])q.toArray(ints); |
421 |
> |
Integer[] ints = new Integer[SIZE]; |
422 |
> |
ints = (Integer[])q.toArray(ints); |
423 |
|
Arrays.sort(ints); |
424 |
|
for (int i = 0; i < ints.length; i++) |
425 |
|
assertEquals(ints[i], q.pollFirst()); |
431 |
|
public void testIterator() { |
432 |
|
NavigableSet q = populatedSet(SIZE); |
433 |
|
int i = 0; |
434 |
< |
Iterator it = q.iterator(); |
434 |
> |
Iterator it = q.iterator(); |
435 |
|
while (it.hasNext()) { |
436 |
|
assertTrue(q.contains(it.next())); |
437 |
|
++i; |
445 |
|
public void testEmptyIterator() { |
446 |
|
NavigableSet q = set0(); |
447 |
|
int i = 0; |
448 |
< |
Iterator it = q.iterator(); |
448 |
> |
Iterator it = q.iterator(); |
449 |
|
while (it.hasNext()) { |
450 |
|
assertTrue(q.contains(it.next())); |
451 |
|
++i; |
487 |
|
/** |
488 |
|
* A deserialized serialized set has same elements |
489 |
|
*/ |
490 |
< |
public void testSerialization() { |
490 |
> |
public void testSerialization() throws Exception { |
491 |
|
NavigableSet q = populatedSet(SIZE); |
492 |
< |
try { |
493 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
494 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
495 |
< |
out.writeObject(q); |
496 |
< |
out.close(); |
497 |
< |
|
498 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
499 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
500 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
501 |
< |
assertEquals(q.size(), r.size()); |
502 |
< |
while (!q.isEmpty()) |
507 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
508 |
< |
} catch (Exception e) { |
509 |
< |
e.printStackTrace(); |
510 |
< |
unexpectedException(); |
511 |
< |
} |
492 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
493 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
494 |
> |
out.writeObject(q); |
495 |
> |
out.close(); |
496 |
> |
|
497 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
498 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
499 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
500 |
> |
assertEquals(q.size(), r.size()); |
501 |
> |
while (!q.isEmpty()) |
502 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
503 |
|
} |
504 |
|
|
505 |
|
/** |
663 |
|
q.add(new Object()); |
664 |
|
q.add(new Object()); |
665 |
|
shouldThrow(); |
666 |
< |
} |
676 |
< |
catch (ClassCastException success) {} |
666 |
> |
} catch (ClassCastException success) {} |
667 |
|
} |
668 |
|
|
669 |
|
|
675 |
|
NavigableSet q = dset0(); |
676 |
|
q.addAll(null); |
677 |
|
shouldThrow(); |
678 |
< |
} |
689 |
< |
catch (NullPointerException success) {} |
678 |
> |
} catch (NullPointerException success) {} |
679 |
|
} |
680 |
|
/** |
681 |
|
* addAll of a collection with null elements throws NPE |
686 |
|
Integer[] ints = new Integer[SIZE]; |
687 |
|
q.addAll(Arrays.asList(ints)); |
688 |
|
shouldThrow(); |
689 |
< |
} |
701 |
< |
catch (NullPointerException success) {} |
689 |
> |
} catch (NullPointerException success) {} |
690 |
|
} |
691 |
|
/** |
692 |
|
* addAll of a collection with any null elements throws NPE after |
700 |
|
ints[i] = new Integer(i+SIZE); |
701 |
|
q.addAll(Arrays.asList(ints)); |
702 |
|
shouldThrow(); |
703 |
< |
} |
716 |
< |
catch (NullPointerException success) {} |
703 |
> |
} catch (NullPointerException success) {} |
704 |
|
} |
705 |
|
|
706 |
|
/** |
707 |
|
* Set contains all elements of successful addAll |
708 |
|
*/ |
709 |
|
public void testDescendingAddAll5() { |
710 |
< |
try { |
711 |
< |
Integer[] empty = new Integer[0]; |
712 |
< |
Integer[] ints = new Integer[SIZE]; |
713 |
< |
for (int i = 0; i < SIZE; ++i) |
714 |
< |
ints[i] = new Integer(SIZE-1- i); |
715 |
< |
NavigableSet q = dset0(); |
716 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
717 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
718 |
< |
for (int i = 0; i < SIZE; ++i) |
732 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
733 |
< |
} |
734 |
< |
finally {} |
710 |
> |
Integer[] empty = new Integer[0]; |
711 |
> |
Integer[] ints = new Integer[SIZE]; |
712 |
> |
for (int i = 0; i < SIZE; ++i) |
713 |
> |
ints[i] = new Integer(SIZE-1- i); |
714 |
> |
NavigableSet q = dset0(); |
715 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
716 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
717 |
> |
for (int i = 0; i < SIZE; ++i) |
718 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
719 |
|
} |
720 |
|
|
721 |
|
/** |
726 |
|
for (int i = 0; i < SIZE; ++i) { |
727 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
728 |
|
} |
729 |
< |
assertNull(q.pollFirst()); |
729 |
> |
assertNull(q.pollFirst()); |
730 |
|
} |
731 |
|
|
732 |
|
/** |
902 |
|
*/ |
903 |
|
public void testDescendingToArray() { |
904 |
|
NavigableSet q = populatedSet(SIZE); |
905 |
< |
Object[] o = q.toArray(); |
905 |
> |
Object[] o = q.toArray(); |
906 |
|
Arrays.sort(o); |
907 |
< |
for (int i = 0; i < o.length; i++) |
908 |
< |
assertEquals(o[i], q.pollFirst()); |
907 |
> |
for (int i = 0; i < o.length; i++) |
908 |
> |
assertEquals(o[i], q.pollFirst()); |
909 |
|
} |
910 |
|
|
911 |
|
/** |
913 |
|
*/ |
914 |
|
public void testDescendingToArray2() { |
915 |
|
NavigableSet q = populatedSet(SIZE); |
916 |
< |
Integer[] ints = new Integer[SIZE]; |
917 |
< |
ints = (Integer[])q.toArray(ints); |
916 |
> |
Integer[] ints = new Integer[SIZE]; |
917 |
> |
ints = (Integer[])q.toArray(ints); |
918 |
|
Arrays.sort(ints); |
919 |
|
for (int i = 0; i < ints.length; i++) |
920 |
|
assertEquals(ints[i], q.pollFirst()); |
926 |
|
public void testDescendingIterator() { |
927 |
|
NavigableSet q = populatedSet(SIZE); |
928 |
|
int i = 0; |
929 |
< |
Iterator it = q.iterator(); |
929 |
> |
Iterator it = q.iterator(); |
930 |
|
while (it.hasNext()) { |
931 |
|
assertTrue(q.contains(it.next())); |
932 |
|
++i; |
940 |
|
public void testDescendingEmptyIterator() { |
941 |
|
NavigableSet q = dset0(); |
942 |
|
int i = 0; |
943 |
< |
Iterator it = q.iterator(); |
943 |
> |
Iterator it = q.iterator(); |
944 |
|
while (it.hasNext()) { |
945 |
|
assertTrue(q.contains(it.next())); |
946 |
|
++i; |
982 |
|
/** |
983 |
|
* A deserialized serialized set has same elements |
984 |
|
*/ |
985 |
< |
public void testDescendingSerialization() { |
985 |
> |
public void testDescendingSerialization() throws Exception { |
986 |
|
NavigableSet q = populatedSet(SIZE); |
987 |
< |
try { |
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()) |
1014 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
1015 |
< |
} catch (Exception e) { |
1016 |
< |
e.printStackTrace(); |
1017 |
< |
unexpectedException(); |
1018 |
< |
} |
987 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
988 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
989 |
> |
out.writeObject(q); |
990 |
> |
out.close(); |
991 |
> |
|
992 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
993 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
994 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
995 |
> |
assertEquals(q.size(), r.size()); |
996 |
> |
while (!q.isEmpty()) |
997 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
998 |
|
} |
999 |
|
|
1000 |
|
/** |