1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
10 |
< |
import java.io.*; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.Comparator; |
10 |
> |
import java.util.Iterator; |
11 |
> |
import java.util.NavigableSet; |
12 |
> |
import java.util.SortedSet; |
13 |
> |
import java.util.TreeSet; |
14 |
|
|
15 |
|
public class TreeSubSetTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(TreeSubSetTest.class); |
30 |
|
* Create a set of given size containing consecutive |
31 |
|
* Integers 0 ... n. |
32 |
|
*/ |
33 |
< |
private NavigableSet populatedSet(int n) { |
34 |
< |
TreeSet q = new TreeSet(); |
33 |
> |
private NavigableSet<Integer> populatedSet(int n) { |
34 |
> |
TreeSet<Integer> q = new TreeSet<Integer>(); |
35 |
|
assertTrue(q.isEmpty()); |
36 |
|
|
37 |
|
for (int i = n-1; i >= 0; i-=2) |
96 |
|
assertEquals(0, set0().size()); |
97 |
|
} |
98 |
|
|
96 |
– |
|
99 |
|
/** |
100 |
|
* isEmpty is true before add, false after |
101 |
|
*/ |
166 |
|
} catch (ClassCastException success) {} |
167 |
|
} |
168 |
|
|
167 |
– |
|
169 |
|
/** |
170 |
|
* addAll(null) throws NPE |
171 |
|
*/ |
176 |
|
shouldThrow(); |
177 |
|
} catch (NullPointerException success) {} |
178 |
|
} |
179 |
+ |
|
180 |
|
/** |
181 |
|
* addAll of a collection with null elements throws NPE |
182 |
|
*/ |
188 |
|
shouldThrow(); |
189 |
|
} catch (NullPointerException success) {} |
190 |
|
} |
191 |
+ |
|
192 |
|
/** |
193 |
|
* addAll of a collection with any null elements throws NPE after |
194 |
|
* possibly adding some elements |
236 |
|
public void testRemoveElement() { |
237 |
|
NavigableSet q = populatedSet(SIZE); |
238 |
|
for (int i = 1; i < SIZE; i+=2) { |
239 |
< |
assertTrue(q.remove(new Integer(i))); |
239 |
> |
assertTrue(q.contains(i)); |
240 |
> |
assertTrue(q.remove(i)); |
241 |
> |
assertFalse(q.contains(i)); |
242 |
> |
assertTrue(q.contains(i-1)); |
243 |
|
} |
244 |
|
for (int i = 0; i < SIZE; i+=2) { |
245 |
< |
assertTrue(q.remove(new Integer(i))); |
246 |
< |
assertFalse(q.remove(new Integer(i+1))); |
245 |
> |
assertTrue(q.contains(i)); |
246 |
> |
assertTrue(q.remove(i)); |
247 |
> |
assertFalse(q.contains(i)); |
248 |
> |
assertFalse(q.remove(i+1)); |
249 |
> |
assertFalse(q.contains(i+1)); |
250 |
|
} |
251 |
|
assertTrue(q.isEmpty()); |
252 |
|
} |
326 |
|
} |
327 |
|
} |
328 |
|
|
320 |
– |
|
321 |
– |
|
329 |
|
/** |
330 |
|
* lower returns preceding element |
331 |
|
*/ |
342 |
|
|
343 |
|
Object e4 = q.lower(zero); |
344 |
|
assertNull(e4); |
338 |
– |
|
345 |
|
} |
346 |
|
|
347 |
|
/** |
360 |
|
|
361 |
|
Object e4 = q.higher(six); |
362 |
|
assertNull(e4); |
357 |
– |
|
363 |
|
} |
364 |
|
|
365 |
|
/** |
378 |
|
|
379 |
|
Object e4 = q.floor(zero); |
380 |
|
assertNull(e4); |
376 |
– |
|
381 |
|
} |
382 |
|
|
383 |
|
/** |
396 |
|
|
397 |
|
Object e4 = q.ceiling(six); |
398 |
|
assertNull(e4); |
395 |
– |
|
399 |
|
} |
400 |
|
|
401 |
|
/** |
402 |
< |
* toArray contains all elements |
402 |
> |
* toArray contains all elements in sorted order |
403 |
|
*/ |
404 |
|
public void testToArray() { |
405 |
|
NavigableSet q = populatedSet(SIZE); |
406 |
|
Object[] o = q.toArray(); |
404 |
– |
Arrays.sort(o); |
407 |
|
for (int i = 0; i < o.length; i++) |
408 |
< |
assertEquals(o[i], q.pollFirst()); |
408 |
> |
assertSame(o[i], q.pollFirst()); |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
< |
* toArray(a) contains all elements |
412 |
> |
* toArray(a) contains all elements in sorted order |
413 |
|
*/ |
414 |
|
public void testToArray2() { |
415 |
< |
NavigableSet q = populatedSet(SIZE); |
415 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
416 |
|
Integer[] ints = new Integer[SIZE]; |
417 |
< |
ints = (Integer[])q.toArray(ints); |
418 |
< |
Arrays.sort(ints); |
417 |
> |
Integer[] array = q.toArray(ints); |
418 |
> |
assertSame(ints, array); |
419 |
|
for (int i = 0; i < ints.length; i++) |
420 |
< |
assertEquals(ints[i], q.pollFirst()); |
420 |
> |
assertSame(ints[i], q.pollFirst()); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
451 |
|
/** |
452 |
|
* iterator.remove removes current element |
453 |
|
*/ |
454 |
< |
public void testIteratorRemove () { |
454 |
> |
public void testIteratorRemove() { |
455 |
|
final NavigableSet q = set0(); |
456 |
|
q.add(new Integer(2)); |
457 |
|
q.add(new Integer(1)); |
467 |
|
assertFalse(it.hasNext()); |
468 |
|
} |
469 |
|
|
468 |
– |
|
470 |
|
/** |
471 |
|
* toString contains toStrings of elements |
472 |
|
*/ |
474 |
|
NavigableSet q = populatedSet(SIZE); |
475 |
|
String s = q.toString(); |
476 |
|
for (int i = 0; i < SIZE; ++i) { |
477 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
477 |
> |
assertTrue(s.contains(String.valueOf(i))); |
478 |
|
} |
479 |
|
} |
480 |
|
|
482 |
|
* A deserialized serialized set has same elements |
483 |
|
*/ |
484 |
|
public void testSerialization() throws Exception { |
485 |
< |
NavigableSet q = populatedSet(SIZE); |
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()) |
496 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
485 |
> |
NavigableSet x = populatedSet(SIZE); |
486 |
> |
NavigableSet y = serialClone(x); |
487 |
> |
|
488 |
> |
assertTrue(x != y); |
489 |
> |
assertEquals(x.size(), y.size()); |
490 |
> |
assertEquals(x, y); |
491 |
> |
assertEquals(y, x); |
492 |
> |
while (!x.isEmpty()) { |
493 |
> |
assertFalse(y.isEmpty()); |
494 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
495 |
> |
} |
496 |
> |
assertTrue(y.isEmpty()); |
497 |
|
} |
498 |
|
|
499 |
|
/** |
660 |
|
} catch (ClassCastException success) {} |
661 |
|
} |
662 |
|
|
662 |
– |
|
663 |
|
/** |
664 |
|
* addAll(null) throws NPE |
665 |
|
*/ |
670 |
|
shouldThrow(); |
671 |
|
} catch (NullPointerException success) {} |
672 |
|
} |
673 |
+ |
|
674 |
|
/** |
675 |
|
* addAll of a collection with null elements throws NPE |
676 |
|
*/ |
682 |
|
shouldThrow(); |
683 |
|
} catch (NullPointerException success) {} |
684 |
|
} |
685 |
+ |
|
686 |
|
/** |
687 |
|
* addAll of a collection with any null elements throws NPE after |
688 |
|
* possibly adding some elements |
814 |
|
} |
815 |
|
} |
816 |
|
|
815 |
– |
|
816 |
– |
|
817 |
|
/** |
818 |
|
* lower returns preceding element |
819 |
|
*/ |
830 |
|
|
831 |
|
Object e4 = q.lower(zero); |
832 |
|
assertNull(e4); |
833 |
– |
|
833 |
|
} |
834 |
|
|
835 |
|
/** |
848 |
|
|
849 |
|
Object e4 = q.higher(m6); |
850 |
|
assertNull(e4); |
852 |
– |
|
851 |
|
} |
852 |
|
|
853 |
|
/** |
866 |
|
|
867 |
|
Object e4 = q.floor(zero); |
868 |
|
assertNull(e4); |
871 |
– |
|
869 |
|
} |
870 |
|
|
871 |
|
/** |
884 |
|
|
885 |
|
Object e4 = q.ceiling(m6); |
886 |
|
assertNull(e4); |
890 |
– |
|
887 |
|
} |
888 |
|
|
889 |
|
/** |
903 |
|
public void testDescendingToArray2() { |
904 |
|
NavigableSet q = populatedSet(SIZE); |
905 |
|
Integer[] ints = new Integer[SIZE]; |
906 |
< |
ints = (Integer[])q.toArray(ints); |
906 |
> |
assertSame(ints, q.toArray(ints)); |
907 |
|
Arrays.sort(ints); |
908 |
|
for (int i = 0; i < ints.length; i++) |
909 |
|
assertEquals(ints[i], q.pollFirst()); |
940 |
|
/** |
941 |
|
* iterator.remove removes current element |
942 |
|
*/ |
943 |
< |
public void testDescendingIteratorRemove () { |
943 |
> |
public void testDescendingIteratorRemove() { |
944 |
|
final NavigableSet q = dset0(); |
945 |
|
q.add(new Integer(2)); |
946 |
|
q.add(new Integer(1)); |
956 |
|
assertFalse(it.hasNext()); |
957 |
|
} |
958 |
|
|
963 |
– |
|
959 |
|
/** |
960 |
|
* toString contains toStrings of elements |
961 |
|
*/ |
963 |
|
NavigableSet q = populatedSet(SIZE); |
964 |
|
String s = q.toString(); |
965 |
|
for (int i = 0; i < SIZE; ++i) { |
966 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
966 |
> |
assertTrue(s.contains(String.valueOf(i))); |
967 |
|
} |
968 |
|
} |
969 |
|
|
971 |
|
* A deserialized serialized set has same elements |
972 |
|
*/ |
973 |
|
public void testDescendingSerialization() throws Exception { |
974 |
< |
NavigableSet q = populatedSet(SIZE); |
975 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
976 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
977 |
< |
out.writeObject(q); |
978 |
< |
out.close(); |
979 |
< |
|
980 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
981 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
982 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
983 |
< |
assertEquals(q.size(), r.size()); |
984 |
< |
while (!q.isEmpty()) |
985 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
974 |
> |
NavigableSet x = dset5(); |
975 |
> |
NavigableSet y = serialClone(x); |
976 |
> |
|
977 |
> |
assertTrue(x != y); |
978 |
> |
assertEquals(x.size(), y.size()); |
979 |
> |
assertEquals(x.toString(), y.toString()); |
980 |
> |
assertEquals(x, y); |
981 |
> |
assertEquals(y, x); |
982 |
> |
while (!x.isEmpty()) { |
983 |
> |
assertFalse(y.isEmpty()); |
984 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
985 |
> |
} |
986 |
> |
assertTrue(y.isEmpty()); |
987 |
|
} |
988 |
|
|
989 |
|
/** |