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 |
|
/** |
209 |
|
* Set contains all elements of successful addAll |
210 |
|
*/ |
211 |
|
public void testAddAll5() { |
212 |
< |
try { |
213 |
< |
Integer[] empty = new Integer[0]; |
214 |
< |
Integer[] ints = new Integer[SIZE]; |
215 |
< |
for (int i = 0; i < SIZE; ++i) |
216 |
< |
ints[i] = new Integer(SIZE-1- i); |
217 |
< |
NavigableSet q = set0(); |
218 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
219 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
220 |
< |
for (int i = 0; i < SIZE; ++i) |
225 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
226 |
< |
} |
227 |
< |
finally {} |
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) |
220 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
221 |
|
} |
222 |
|
|
223 |
|
/** |
228 |
|
for (int i = 0; i < SIZE; ++i) { |
229 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
230 |
|
} |
231 |
< |
assertNull(q.pollFirst()); |
231 |
> |
assertNull(q.pollFirst()); |
232 |
|
} |
233 |
|
|
234 |
|
/** |
404 |
|
*/ |
405 |
|
public void testToArray() { |
406 |
|
NavigableSet q = populatedSet(SIZE); |
407 |
< |
Object[] o = q.toArray(); |
407 |
> |
Object[] o = q.toArray(); |
408 |
|
Arrays.sort(o); |
409 |
< |
for (int i = 0; i < o.length; i++) |
410 |
< |
assertEquals(o[i], q.pollFirst()); |
409 |
> |
for (int i = 0; i < o.length; i++) |
410 |
> |
assertEquals(o[i], q.pollFirst()); |
411 |
|
} |
412 |
|
|
413 |
|
/** |
415 |
|
*/ |
416 |
|
public void testToArray2() { |
417 |
|
NavigableSet q = populatedSet(SIZE); |
418 |
< |
Integer[] ints = new Integer[SIZE]; |
419 |
< |
ints = (Integer[])q.toArray(ints); |
418 |
> |
Integer[] ints = new Integer[SIZE]; |
419 |
> |
ints = (Integer[])q.toArray(ints); |
420 |
|
Arrays.sort(ints); |
421 |
|
for (int i = 0; i < ints.length; i++) |
422 |
|
assertEquals(ints[i], q.pollFirst()); |
428 |
|
public void testIterator() { |
429 |
|
NavigableSet q = populatedSet(SIZE); |
430 |
|
int i = 0; |
431 |
< |
Iterator it = q.iterator(); |
431 |
> |
Iterator it = q.iterator(); |
432 |
|
while (it.hasNext()) { |
433 |
|
assertTrue(q.contains(it.next())); |
434 |
|
++i; |
442 |
|
public void testEmptyIterator() { |
443 |
|
NavigableSet q = set0(); |
444 |
|
int i = 0; |
445 |
< |
Iterator it = q.iterator(); |
445 |
> |
Iterator it = q.iterator(); |
446 |
|
while (it.hasNext()) { |
447 |
|
assertTrue(q.contains(it.next())); |
448 |
|
++i; |
484 |
|
/** |
485 |
|
* A deserialized serialized set has same elements |
486 |
|
*/ |
487 |
< |
public void testSerialization() { |
487 |
> |
public void testSerialization() throws Exception { |
488 |
|
NavigableSet q = populatedSet(SIZE); |
489 |
< |
try { |
490 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
491 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
492 |
< |
out.writeObject(q); |
493 |
< |
out.close(); |
494 |
< |
|
495 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
496 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
497 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
498 |
< |
assertEquals(q.size(), r.size()); |
499 |
< |
while (!q.isEmpty()) |
507 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
508 |
< |
} catch (Exception e){ |
509 |
< |
e.printStackTrace(); |
510 |
< |
unexpectedException(); |
511 |
< |
} |
489 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
490 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
491 |
> |
out.writeObject(q); |
492 |
> |
out.close(); |
493 |
> |
|
494 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
495 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
496 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
497 |
> |
assertEquals(q.size(), r.size()); |
498 |
> |
while (!q.isEmpty()) |
499 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
660 |
|
q.add(new Object()); |
661 |
|
q.add(new Object()); |
662 |
|
shouldThrow(); |
663 |
< |
} |
676 |
< |
catch (ClassCastException success) {} |
663 |
> |
} catch (ClassCastException success) {} |
664 |
|
} |
665 |
|
|
666 |
|
|
672 |
|
NavigableSet q = dset0(); |
673 |
|
q.addAll(null); |
674 |
|
shouldThrow(); |
675 |
< |
} |
689 |
< |
catch (NullPointerException success) {} |
675 |
> |
} catch (NullPointerException success) {} |
676 |
|
} |
677 |
|
/** |
678 |
|
* addAll of a collection with null elements throws NPE |
683 |
|
Integer[] ints = new Integer[SIZE]; |
684 |
|
q.addAll(Arrays.asList(ints)); |
685 |
|
shouldThrow(); |
686 |
< |
} |
701 |
< |
catch (NullPointerException success) {} |
686 |
> |
} catch (NullPointerException success) {} |
687 |
|
} |
688 |
|
/** |
689 |
|
* addAll of a collection with any null elements throws NPE after |
697 |
|
ints[i] = new Integer(i+SIZE); |
698 |
|
q.addAll(Arrays.asList(ints)); |
699 |
|
shouldThrow(); |
700 |
< |
} |
716 |
< |
catch (NullPointerException success) {} |
700 |
> |
} catch (NullPointerException success) {} |
701 |
|
} |
702 |
|
|
703 |
|
/** |
704 |
|
* Set contains all elements of successful addAll |
705 |
|
*/ |
706 |
|
public void testDescendingAddAll5() { |
707 |
< |
try { |
708 |
< |
Integer[] empty = new Integer[0]; |
709 |
< |
Integer[] ints = new Integer[SIZE]; |
710 |
< |
for (int i = 0; i < SIZE; ++i) |
711 |
< |
ints[i] = new Integer(SIZE-1- i); |
712 |
< |
NavigableSet q = dset0(); |
713 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
714 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
715 |
< |
for (int i = 0; i < SIZE; ++i) |
732 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
733 |
< |
} |
734 |
< |
finally {} |
707 |
> |
Integer[] empty = new Integer[0]; |
708 |
> |
Integer[] ints = new Integer[SIZE]; |
709 |
> |
for (int i = 0; i < SIZE; ++i) |
710 |
> |
ints[i] = new Integer(SIZE-1- i); |
711 |
> |
NavigableSet q = dset0(); |
712 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
713 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
714 |
> |
for (int i = 0; i < SIZE; ++i) |
715 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
716 |
|
} |
717 |
|
|
718 |
|
/** |
723 |
|
for (int i = 0; i < SIZE; ++i) { |
724 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
725 |
|
} |
726 |
< |
assertNull(q.pollFirst()); |
726 |
> |
assertNull(q.pollFirst()); |
727 |
|
} |
728 |
|
|
729 |
|
/** |
899 |
|
*/ |
900 |
|
public void testDescendingToArray() { |
901 |
|
NavigableSet q = populatedSet(SIZE); |
902 |
< |
Object[] o = q.toArray(); |
902 |
> |
Object[] o = q.toArray(); |
903 |
|
Arrays.sort(o); |
904 |
< |
for (int i = 0; i < o.length; i++) |
905 |
< |
assertEquals(o[i], q.pollFirst()); |
904 |
> |
for (int i = 0; i < o.length; i++) |
905 |
> |
assertEquals(o[i], q.pollFirst()); |
906 |
|
} |
907 |
|
|
908 |
|
/** |
910 |
|
*/ |
911 |
|
public void testDescendingToArray2() { |
912 |
|
NavigableSet q = populatedSet(SIZE); |
913 |
< |
Integer[] ints = new Integer[SIZE]; |
914 |
< |
ints = (Integer[])q.toArray(ints); |
913 |
> |
Integer[] ints = new Integer[SIZE]; |
914 |
> |
ints = (Integer[])q.toArray(ints); |
915 |
|
Arrays.sort(ints); |
916 |
|
for (int i = 0; i < ints.length; i++) |
917 |
|
assertEquals(ints[i], q.pollFirst()); |
923 |
|
public void testDescendingIterator() { |
924 |
|
NavigableSet q = populatedSet(SIZE); |
925 |
|
int i = 0; |
926 |
< |
Iterator it = q.iterator(); |
926 |
> |
Iterator it = q.iterator(); |
927 |
|
while (it.hasNext()) { |
928 |
|
assertTrue(q.contains(it.next())); |
929 |
|
++i; |
937 |
|
public void testDescendingEmptyIterator() { |
938 |
|
NavigableSet q = dset0(); |
939 |
|
int i = 0; |
940 |
< |
Iterator it = q.iterator(); |
940 |
> |
Iterator it = q.iterator(); |
941 |
|
while (it.hasNext()) { |
942 |
|
assertTrue(q.contains(it.next())); |
943 |
|
++i; |
979 |
|
/** |
980 |
|
* A deserialized serialized set has same elements |
981 |
|
*/ |
982 |
< |
public void testDescendingSerialization() { |
982 |
> |
public void testDescendingSerialization() throws Exception { |
983 |
|
NavigableSet q = populatedSet(SIZE); |
984 |
< |
try { |
985 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
986 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
987 |
< |
out.writeObject(q); |
988 |
< |
out.close(); |
989 |
< |
|
990 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
991 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
992 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
993 |
< |
assertEquals(q.size(), r.size()); |
994 |
< |
while (!q.isEmpty()) |
1014 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
1015 |
< |
} catch (Exception e){ |
1016 |
< |
e.printStackTrace(); |
1017 |
< |
unexpectedException(); |
1018 |
< |
} |
984 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
985 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
986 |
> |
out.writeObject(q); |
987 |
> |
out.close(); |
988 |
> |
|
989 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
990 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
991 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
992 |
> |
assertEquals(q.size(), r.size()); |
993 |
> |
while (!q.isEmpty()) |
994 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
995 |
|
} |
996 |
|
|
997 |
|
/** |