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()); |
335 |
|
|
336 |
|
Object e4 = q.lower(zero); |
337 |
|
assertNull(e4); |
349 |
– |
|
338 |
|
} |
339 |
|
|
340 |
|
/** |
353 |
|
|
354 |
|
Object e4 = q.higher(six); |
355 |
|
assertNull(e4); |
368 |
– |
|
356 |
|
} |
357 |
|
|
358 |
|
/** |
371 |
|
|
372 |
|
Object e4 = q.floor(zero); |
373 |
|
assertNull(e4); |
387 |
– |
|
374 |
|
} |
375 |
|
|
376 |
|
/** |
389 |
|
|
390 |
|
Object e4 = q.ceiling(six); |
391 |
|
assertNull(e4); |
406 |
– |
|
392 |
|
} |
393 |
|
|
394 |
|
/** |
396 |
|
*/ |
397 |
|
public void testToArray() { |
398 |
|
NavigableSet q = populatedSet(SIZE); |
399 |
< |
Object[] o = q.toArray(); |
399 |
> |
Object[] o = q.toArray(); |
400 |
|
Arrays.sort(o); |
401 |
< |
for(int i = 0; i < o.length; i++) |
402 |
< |
assertEquals(o[i], q.pollFirst()); |
401 |
> |
for (int i = 0; i < o.length; i++) |
402 |
> |
assertEquals(o[i], q.pollFirst()); |
403 |
|
} |
404 |
|
|
405 |
|
/** |
407 |
|
*/ |
408 |
|
public void testToArray2() { |
409 |
|
NavigableSet q = populatedSet(SIZE); |
410 |
< |
Integer[] ints = new Integer[SIZE]; |
411 |
< |
ints = (Integer[])q.toArray(ints); |
410 |
> |
Integer[] ints = new Integer[SIZE]; |
411 |
> |
ints = (Integer[])q.toArray(ints); |
412 |
|
Arrays.sort(ints); |
413 |
< |
for(int i = 0; i < ints.length; i++) |
413 |
> |
for (int i = 0; i < ints.length; i++) |
414 |
|
assertEquals(ints[i], q.pollFirst()); |
415 |
|
} |
416 |
|
|
420 |
|
public void testIterator() { |
421 |
|
NavigableSet q = populatedSet(SIZE); |
422 |
|
int i = 0; |
423 |
< |
Iterator it = q.iterator(); |
424 |
< |
while(it.hasNext()) { |
423 |
> |
Iterator it = q.iterator(); |
424 |
> |
while (it.hasNext()) { |
425 |
|
assertTrue(q.contains(it.next())); |
426 |
|
++i; |
427 |
|
} |
434 |
|
public void testEmptyIterator() { |
435 |
|
NavigableSet q = set0(); |
436 |
|
int i = 0; |
437 |
< |
Iterator it = q.iterator(); |
438 |
< |
while(it.hasNext()) { |
437 |
> |
Iterator it = q.iterator(); |
438 |
> |
while (it.hasNext()) { |
439 |
|
assertTrue(q.contains(it.next())); |
440 |
|
++i; |
441 |
|
} |
456 |
|
it.remove(); |
457 |
|
|
458 |
|
it = q.iterator(); |
459 |
< |
assertEquals(it.next(), new Integer(2)); |
460 |
< |
assertEquals(it.next(), new Integer(3)); |
459 |
> |
assertEquals(it.next(), 2); |
460 |
> |
assertEquals(it.next(), 3); |
461 |
|
assertFalse(it.hasNext()); |
462 |
|
} |
463 |
|
|
476 |
|
/** |
477 |
|
* A deserialized serialized set has same elements |
478 |
|
*/ |
479 |
< |
public void testSerialization() { |
479 |
> |
public void testSerialization() throws Exception { |
480 |
|
NavigableSet q = populatedSet(SIZE); |
481 |
< |
try { |
482 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
483 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
484 |
< |
out.writeObject(q); |
485 |
< |
out.close(); |
486 |
< |
|
487 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
488 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
489 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
490 |
< |
assertEquals(q.size(), r.size()); |
491 |
< |
while (!q.isEmpty()) |
507 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
508 |
< |
} catch(Exception e){ |
509 |
< |
e.printStackTrace(); |
510 |
< |
unexpectedException(); |
511 |
< |
} |
481 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
482 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
483 |
> |
out.writeObject(q); |
484 |
> |
out.close(); |
485 |
> |
|
486 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
487 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
488 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
489 |
> |
assertEquals(q.size(), r.size()); |
490 |
> |
while (!q.isEmpty()) |
491 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
492 |
|
} |
493 |
|
|
494 |
|
/** |
652 |
|
q.add(new Object()); |
653 |
|
q.add(new Object()); |
654 |
|
shouldThrow(); |
655 |
< |
} |
676 |
< |
catch(ClassCastException success) {} |
655 |
> |
} catch (ClassCastException success) {} |
656 |
|
} |
657 |
|
|
658 |
|
|
664 |
|
NavigableSet q = dset0(); |
665 |
|
q.addAll(null); |
666 |
|
shouldThrow(); |
667 |
< |
} |
689 |
< |
catch (NullPointerException success) {} |
667 |
> |
} catch (NullPointerException success) {} |
668 |
|
} |
669 |
|
/** |
670 |
|
* addAll of a collection with null elements throws NPE |
675 |
|
Integer[] ints = new Integer[SIZE]; |
676 |
|
q.addAll(Arrays.asList(ints)); |
677 |
|
shouldThrow(); |
678 |
< |
} |
701 |
< |
catch (NullPointerException success) {} |
678 |
> |
} catch (NullPointerException success) {} |
679 |
|
} |
680 |
|
/** |
681 |
|
* addAll of a collection with any null elements throws NPE after |
689 |
|
ints[i] = new Integer(i+SIZE); |
690 |
|
q.addAll(Arrays.asList(ints)); |
691 |
|
shouldThrow(); |
692 |
< |
} |
716 |
< |
catch (NullPointerException success) {} |
692 |
> |
} catch (NullPointerException success) {} |
693 |
|
} |
694 |
|
|
695 |
|
/** |
696 |
|
* Set contains all elements of successful addAll |
697 |
|
*/ |
698 |
|
public void testDescendingAddAll5() { |
699 |
< |
try { |
700 |
< |
Integer[] empty = new Integer[0]; |
701 |
< |
Integer[] ints = new Integer[SIZE]; |
702 |
< |
for (int i = 0; i < SIZE; ++i) |
703 |
< |
ints[i] = new Integer(SIZE-1- i); |
704 |
< |
NavigableSet q = dset0(); |
705 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
706 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
707 |
< |
for (int i = 0; i < SIZE; ++i) |
732 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
733 |
< |
} |
734 |
< |
finally {} |
699 |
> |
Integer[] empty = new Integer[0]; |
700 |
> |
Integer[] ints = new Integer[SIZE]; |
701 |
> |
for (int i = 0; i < SIZE; ++i) |
702 |
> |
ints[i] = new Integer(SIZE-1- i); |
703 |
> |
NavigableSet q = dset0(); |
704 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
705 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
706 |
> |
for (int i = 0; i < SIZE; ++i) |
707 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
708 |
|
} |
709 |
|
|
710 |
|
/** |
713 |
|
public void testDescendingPoll() { |
714 |
|
NavigableSet q = populatedSet(SIZE); |
715 |
|
for (int i = 0; i < SIZE; ++i) { |
716 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
716 |
> |
assertEquals(i, q.pollFirst()); |
717 |
|
} |
718 |
< |
assertNull(q.pollFirst()); |
718 |
> |
assertNull(q.pollFirst()); |
719 |
|
} |
720 |
|
|
721 |
|
/** |
753 |
|
q.clear(); |
754 |
|
assertTrue(q.isEmpty()); |
755 |
|
assertEquals(0, q.size()); |
756 |
< |
q.add(new Integer(1)); |
756 |
> |
assertTrue(q.add(new Integer(1))); |
757 |
|
assertFalse(q.isEmpty()); |
758 |
|
q.clear(); |
759 |
|
assertTrue(q.isEmpty()); |
826 |
|
|
827 |
|
Object e4 = q.lower(zero); |
828 |
|
assertNull(e4); |
856 |
– |
|
829 |
|
} |
830 |
|
|
831 |
|
/** |
844 |
|
|
845 |
|
Object e4 = q.higher(m6); |
846 |
|
assertNull(e4); |
875 |
– |
|
847 |
|
} |
848 |
|
|
849 |
|
/** |
862 |
|
|
863 |
|
Object e4 = q.floor(zero); |
864 |
|
assertNull(e4); |
894 |
– |
|
865 |
|
} |
866 |
|
|
867 |
|
/** |
880 |
|
|
881 |
|
Object e4 = q.ceiling(m6); |
882 |
|
assertNull(e4); |
913 |
– |
|
883 |
|
} |
884 |
|
|
885 |
|
/** |
887 |
|
*/ |
888 |
|
public void testDescendingToArray() { |
889 |
|
NavigableSet q = populatedSet(SIZE); |
890 |
< |
Object[] o = q.toArray(); |
890 |
> |
Object[] o = q.toArray(); |
891 |
|
Arrays.sort(o); |
892 |
< |
for(int i = 0; i < o.length; i++) |
893 |
< |
assertEquals(o[i], q.pollFirst()); |
892 |
> |
for (int i = 0; i < o.length; i++) |
893 |
> |
assertEquals(o[i], q.pollFirst()); |
894 |
|
} |
895 |
|
|
896 |
|
/** |
898 |
|
*/ |
899 |
|
public void testDescendingToArray2() { |
900 |
|
NavigableSet q = populatedSet(SIZE); |
901 |
< |
Integer[] ints = new Integer[SIZE]; |
902 |
< |
ints = (Integer[])q.toArray(ints); |
901 |
> |
Integer[] ints = new Integer[SIZE]; |
902 |
> |
ints = (Integer[])q.toArray(ints); |
903 |
|
Arrays.sort(ints); |
904 |
< |
for(int i = 0; i < ints.length; i++) |
904 |
> |
for (int i = 0; i < ints.length; i++) |
905 |
|
assertEquals(ints[i], q.pollFirst()); |
906 |
|
} |
907 |
|
|
911 |
|
public void testDescendingIterator() { |
912 |
|
NavigableSet q = populatedSet(SIZE); |
913 |
|
int i = 0; |
914 |
< |
Iterator it = q.iterator(); |
915 |
< |
while(it.hasNext()) { |
914 |
> |
Iterator it = q.iterator(); |
915 |
> |
while (it.hasNext()) { |
916 |
|
assertTrue(q.contains(it.next())); |
917 |
|
++i; |
918 |
|
} |
925 |
|
public void testDescendingEmptyIterator() { |
926 |
|
NavigableSet q = dset0(); |
927 |
|
int i = 0; |
928 |
< |
Iterator it = q.iterator(); |
929 |
< |
while(it.hasNext()) { |
928 |
> |
Iterator it = q.iterator(); |
929 |
> |
while (it.hasNext()) { |
930 |
|
assertTrue(q.contains(it.next())); |
931 |
|
++i; |
932 |
|
} |
947 |
|
it.remove(); |
948 |
|
|
949 |
|
it = q.iterator(); |
950 |
< |
assertEquals(it.next(), new Integer(2)); |
951 |
< |
assertEquals(it.next(), new Integer(3)); |
950 |
> |
assertEquals(it.next(), 2); |
951 |
> |
assertEquals(it.next(), 3); |
952 |
|
assertFalse(it.hasNext()); |
953 |
|
} |
954 |
|
|
967 |
|
/** |
968 |
|
* A deserialized serialized set has same elements |
969 |
|
*/ |
970 |
< |
public void testDescendingSerialization() { |
970 |
> |
public void testDescendingSerialization() throws Exception { |
971 |
|
NavigableSet q = populatedSet(SIZE); |
972 |
< |
try { |
973 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
974 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
975 |
< |
out.writeObject(q); |
976 |
< |
out.close(); |
977 |
< |
|
978 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
979 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
980 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
981 |
< |
assertEquals(q.size(), r.size()); |
982 |
< |
while (!q.isEmpty()) |
1014 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
1015 |
< |
} catch(Exception e){ |
1016 |
< |
e.printStackTrace(); |
1017 |
< |
unexpectedException(); |
1018 |
< |
} |
972 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
973 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
974 |
> |
out.writeObject(q); |
975 |
> |
out.close(); |
976 |
> |
|
977 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
978 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
979 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
980 |
> |
assertEquals(q.size(), r.size()); |
981 |
> |
while (!q.isEmpty()) |
982 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
983 |
|
} |
984 |
|
|
985 |
|
/** |