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 { |
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.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 |
|
|
57 |
|
q.add(zero); |
58 |
|
q.add(seven); |
59 |
|
NavigableSet s = q.subSet(one, true, seven, false); |
60 |
< |
assertEquals(5, s.size()); |
60 |
> |
assertEquals(5, s.size()); |
61 |
|
return s; |
62 |
|
} |
63 |
|
|
70 |
|
q.add(m4); |
71 |
|
q.add(m5); |
72 |
|
NavigableSet s = q.descendingSet(); |
73 |
< |
assertEquals(5, s.size()); |
73 |
> |
assertEquals(5, s.size()); |
74 |
|
return s; |
75 |
|
} |
76 |
|
|
77 |
|
private static NavigableSet set0() { |
78 |
< |
TreeSet set = new TreeSet(); |
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(); |
84 |
> |
TreeSet set = new TreeSet(); |
85 |
|
assertTrue(set.isEmpty()); |
86 |
|
return set; |
87 |
|
} |
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 |
< |
} |
169 |
< |
catch (ClassCastException success) {} |
164 |
> |
} catch (ClassCastException success) {} |
165 |
|
} |
166 |
|
|
167 |
|
|
173 |
|
NavigableSet q = set0(); |
174 |
|
q.addAll(null); |
175 |
|
shouldThrow(); |
176 |
< |
} |
182 |
< |
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 |
< |
} |
194 |
< |
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 |
< |
} |
209 |
< |
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) |
225 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
226 |
< |
} |
227 |
< |
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 |
|
/** |
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()); |
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++) |
418 |
|
assertEquals(ints[i], q.pollFirst()); |
424 |
|
public void testIterator() { |
425 |
|
NavigableSet q = populatedSet(SIZE); |
426 |
|
int i = 0; |
427 |
< |
Iterator it = q.iterator(); |
427 |
> |
Iterator it = q.iterator(); |
428 |
|
while (it.hasNext()) { |
429 |
|
assertTrue(q.contains(it.next())); |
430 |
|
++i; |
438 |
|
public void testEmptyIterator() { |
439 |
|
NavigableSet q = set0(); |
440 |
|
int i = 0; |
441 |
< |
Iterator it = q.iterator(); |
441 |
> |
Iterator it = q.iterator(); |
442 |
|
while (it.hasNext()) { |
443 |
|
assertTrue(q.contains(it.next())); |
444 |
|
++i; |
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 |
|
|
480 |
|
/** |
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()) |
507 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
508 |
< |
} catch (Exception e) { |
509 |
< |
e.printStackTrace(); |
510 |
< |
unexpectedException(); |
511 |
< |
} |
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 |
|
/** |
656 |
|
q.add(new Object()); |
657 |
|
q.add(new Object()); |
658 |
|
shouldThrow(); |
659 |
< |
} |
676 |
< |
catch (ClassCastException success) {} |
659 |
> |
} catch (ClassCastException success) {} |
660 |
|
} |
661 |
|
|
662 |
|
|
668 |
|
NavigableSet q = dset0(); |
669 |
|
q.addAll(null); |
670 |
|
shouldThrow(); |
671 |
< |
} |
689 |
< |
catch (NullPointerException success) {} |
671 |
> |
} catch (NullPointerException success) {} |
672 |
|
} |
673 |
|
/** |
674 |
|
* addAll of a collection with null elements throws NPE |
679 |
|
Integer[] ints = new Integer[SIZE]; |
680 |
|
q.addAll(Arrays.asList(ints)); |
681 |
|
shouldThrow(); |
682 |
< |
} |
701 |
< |
catch (NullPointerException success) {} |
682 |
> |
} catch (NullPointerException success) {} |
683 |
|
} |
684 |
|
/** |
685 |
|
* addAll of a collection with any null elements throws NPE after |
693 |
|
ints[i] = new Integer(i+SIZE); |
694 |
|
q.addAll(Arrays.asList(ints)); |
695 |
|
shouldThrow(); |
696 |
< |
} |
716 |
< |
catch (NullPointerException success) {} |
696 |
> |
} catch (NullPointerException success) {} |
697 |
|
} |
698 |
|
|
699 |
|
/** |
700 |
|
* Set contains all elements of successful addAll |
701 |
|
*/ |
702 |
|
public void testDescendingAddAll5() { |
703 |
< |
try { |
704 |
< |
Integer[] empty = new Integer[0]; |
705 |
< |
Integer[] ints = new Integer[SIZE]; |
706 |
< |
for (int i = 0; i < SIZE; ++i) |
707 |
< |
ints[i] = new Integer(SIZE-1- i); |
708 |
< |
NavigableSet q = dset0(); |
709 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
710 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
711 |
< |
for (int i = 0; i < SIZE; ++i) |
732 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
733 |
< |
} |
734 |
< |
finally {} |
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 |
|
/** |
717 |
|
public void testDescendingPoll() { |
718 |
|
NavigableSet q = populatedSet(SIZE); |
719 |
|
for (int i = 0; i < SIZE; ++i) { |
720 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
720 |
> |
assertEquals(i, q.pollFirst()); |
721 |
|
} |
722 |
< |
assertNull(q.pollFirst()); |
722 |
> |
assertNull(q.pollFirst()); |
723 |
|
} |
724 |
|
|
725 |
|
/** |
757 |
|
q.clear(); |
758 |
|
assertTrue(q.isEmpty()); |
759 |
|
assertEquals(0, q.size()); |
760 |
< |
q.add(new Integer(1)); |
760 |
> |
assertTrue(q.add(new Integer(1))); |
761 |
|
assertFalse(q.isEmpty()); |
762 |
|
q.clear(); |
763 |
|
assertTrue(q.isEmpty()); |
895 |
|
*/ |
896 |
|
public void testDescendingToArray() { |
897 |
|
NavigableSet q = populatedSet(SIZE); |
898 |
< |
Object[] o = q.toArray(); |
898 |
> |
Object[] o = q.toArray(); |
899 |
|
Arrays.sort(o); |
900 |
< |
for (int i = 0; i < o.length; i++) |
901 |
< |
assertEquals(o[i], q.pollFirst()); |
900 |
> |
for (int i = 0; i < o.length; i++) |
901 |
> |
assertEquals(o[i], q.pollFirst()); |
902 |
|
} |
903 |
|
|
904 |
|
/** |
906 |
|
*/ |
907 |
|
public void testDescendingToArray2() { |
908 |
|
NavigableSet q = populatedSet(SIZE); |
909 |
< |
Integer[] ints = new Integer[SIZE]; |
910 |
< |
ints = (Integer[])q.toArray(ints); |
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()); |
919 |
|
public void testDescendingIterator() { |
920 |
|
NavigableSet q = populatedSet(SIZE); |
921 |
|
int i = 0; |
922 |
< |
Iterator it = q.iterator(); |
922 |
> |
Iterator it = q.iterator(); |
923 |
|
while (it.hasNext()) { |
924 |
|
assertTrue(q.contains(it.next())); |
925 |
|
++i; |
933 |
|
public void testDescendingEmptyIterator() { |
934 |
|
NavigableSet q = dset0(); |
935 |
|
int i = 0; |
936 |
< |
Iterator it = q.iterator(); |
936 |
> |
Iterator it = q.iterator(); |
937 |
|
while (it.hasNext()) { |
938 |
|
assertTrue(q.contains(it.next())); |
939 |
|
++i; |
955 |
|
it.remove(); |
956 |
|
|
957 |
|
it = q.iterator(); |
958 |
< |
assertEquals(it.next(), new Integer(2)); |
959 |
< |
assertEquals(it.next(), new Integer(3)); |
958 |
> |
assertEquals(it.next(), 2); |
959 |
> |
assertEquals(it.next(), 3); |
960 |
|
assertFalse(it.hasNext()); |
961 |
|
} |
962 |
|
|
975 |
|
/** |
976 |
|
* A deserialized serialized set has same elements |
977 |
|
*/ |
978 |
< |
public void testDescendingSerialization() { |
978 |
> |
public void testDescendingSerialization() throws Exception { |
979 |
|
NavigableSet q = populatedSet(SIZE); |
980 |
< |
try { |
981 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
982 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
983 |
< |
out.writeObject(q); |
984 |
< |
out.close(); |
985 |
< |
|
986 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
987 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
988 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
989 |
< |
assertEquals(q.size(), r.size()); |
990 |
< |
while (!q.isEmpty()) |
1014 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
1015 |
< |
} catch (Exception e) { |
1016 |
< |
e.printStackTrace(); |
1017 |
< |
unexpectedException(); |
1018 |
< |
} |
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 |
|
/** |