11 |
|
|
12 |
|
public class ConcurrentSkipListSubSetTest 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(ConcurrentSkipListSubSetTest.class); |
17 |
> |
return new TestSuite(ConcurrentSkipListSubSetTest.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 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
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 |
|
|
73 |
|
q.add(m4); |
74 |
|
q.add(m5); |
75 |
|
NavigableSet s = q.descendingSet(); |
76 |
< |
assertEquals(5, s.size()); |
76 |
> |
assertEquals(5, s.size()); |
77 |
|
return s; |
78 |
|
} |
79 |
|
|
80 |
|
private static NavigableSet set0() { |
81 |
< |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
81 |
> |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
82 |
|
assertTrue(set.isEmpty()); |
83 |
|
return set.tailSet(m1, true); |
84 |
|
} |
85 |
|
|
86 |
|
private static NavigableSet dset0() { |
87 |
< |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
87 |
> |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
88 |
|
assertTrue(set.isEmpty()); |
89 |
|
return set; |
90 |
|
} |
130 |
|
* add(null) throws NPE |
131 |
|
*/ |
132 |
|
public void testAddNull() { |
133 |
< |
try { |
133 |
> |
try { |
134 |
|
NavigableSet q = set0(); |
135 |
|
q.add(null); |
136 |
|
shouldThrow(); |
137 |
< |
} catch (NullPointerException success) { } |
137 |
> |
} catch (NullPointerException success) {} |
138 |
|
} |
139 |
|
|
140 |
|
/** |
164 |
|
q.add(new Object()); |
165 |
|
q.add(new Object()); |
166 |
|
shouldThrow(); |
167 |
< |
} |
172 |
< |
catch (ClassCastException success) {} |
167 |
> |
} catch (ClassCastException success) {} |
168 |
|
} |
169 |
|
|
170 |
|
|
176 |
|
NavigableSet q = set0(); |
177 |
|
q.addAll(null); |
178 |
|
shouldThrow(); |
179 |
< |
} |
185 |
< |
catch (NullPointerException success) {} |
179 |
> |
} catch (NullPointerException success) {} |
180 |
|
} |
181 |
|
/** |
182 |
|
* addAll of a collection with null elements throws NPE |
187 |
|
Integer[] ints = new Integer[SIZE]; |
188 |
|
q.addAll(Arrays.asList(ints)); |
189 |
|
shouldThrow(); |
190 |
< |
} |
197 |
< |
catch (NullPointerException success) {} |
190 |
> |
} catch (NullPointerException success) {} |
191 |
|
} |
192 |
|
/** |
193 |
|
* addAll of a collection with any null elements throws NPE after |
201 |
|
ints[i] = new Integer(i+SIZE); |
202 |
|
q.addAll(Arrays.asList(ints)); |
203 |
|
shouldThrow(); |
204 |
< |
} |
212 |
< |
catch (NullPointerException success) {} |
204 |
> |
} catch (NullPointerException success) {} |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
|
* Set contains all elements of successful addAll |
209 |
|
*/ |
210 |
|
public void testAddAll5() { |
211 |
< |
try { |
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) |
228 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
229 |
< |
} |
230 |
< |
finally {} |
211 |
> |
Integer[] empty = new Integer[0]; |
212 |
> |
Integer[] ints = new Integer[SIZE]; |
213 |
> |
for (int i = 0; i < SIZE; ++i) |
214 |
> |
ints[i] = new Integer(SIZE-1- i); |
215 |
> |
NavigableSet q = set0(); |
216 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
217 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
218 |
> |
for (int i = 0; i < SIZE; ++i) |
219 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
220 |
|
} |
221 |
|
|
222 |
|
/** |
225 |
|
public void testPoll() { |
226 |
|
NavigableSet q = populatedSet(SIZE); |
227 |
|
for (int i = 0; i < SIZE; ++i) { |
228 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
228 |
> |
assertEquals(i, q.pollFirst()); |
229 |
|
} |
230 |
< |
assertNull(q.pollFirst()); |
230 |
> |
assertNull(q.pollFirst()); |
231 |
|
} |
232 |
|
|
233 |
|
/** |
403 |
|
*/ |
404 |
|
public void testToArray() { |
405 |
|
NavigableSet q = populatedSet(SIZE); |
406 |
< |
Object[] o = q.toArray(); |
406 |
> |
Object[] o = q.toArray(); |
407 |
|
Arrays.sort(o); |
408 |
< |
for (int i = 0; i < o.length; i++) |
409 |
< |
assertEquals(o[i], q.pollFirst()); |
408 |
> |
for (int i = 0; i < o.length; i++) |
409 |
> |
assertEquals(o[i], q.pollFirst()); |
410 |
|
} |
411 |
|
|
412 |
|
/** |
414 |
|
*/ |
415 |
|
public void testToArray2() { |
416 |
|
NavigableSet q = populatedSet(SIZE); |
417 |
< |
Integer[] ints = new Integer[SIZE]; |
418 |
< |
ints = (Integer[])q.toArray(ints); |
417 |
> |
Integer[] ints = new Integer[SIZE]; |
418 |
> |
ints = (Integer[])q.toArray(ints); |
419 |
|
Arrays.sort(ints); |
420 |
|
for (int i = 0; i < ints.length; i++) |
421 |
|
assertEquals(ints[i], q.pollFirst()); |
427 |
|
public void testIterator() { |
428 |
|
NavigableSet q = populatedSet(SIZE); |
429 |
|
int i = 0; |
430 |
< |
Iterator it = q.iterator(); |
430 |
> |
Iterator it = q.iterator(); |
431 |
|
while (it.hasNext()) { |
432 |
|
assertTrue(q.contains(it.next())); |
433 |
|
++i; |
441 |
|
public void testEmptyIterator() { |
442 |
|
NavigableSet q = set0(); |
443 |
|
int i = 0; |
444 |
< |
Iterator it = q.iterator(); |
444 |
> |
Iterator it = q.iterator(); |
445 |
|
while (it.hasNext()) { |
446 |
|
assertTrue(q.contains(it.next())); |
447 |
|
++i; |
483 |
|
/** |
484 |
|
* A deserialized serialized set has same elements |
485 |
|
*/ |
486 |
< |
public void testSerialization() { |
486 |
> |
public void testSerialization() throws Exception { |
487 |
|
NavigableSet q = populatedSet(SIZE); |
488 |
< |
try { |
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()) |
510 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
511 |
< |
} catch (Exception e) { |
512 |
< |
e.printStackTrace(); |
513 |
< |
unexpectedException(); |
514 |
< |
} |
488 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
489 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
490 |
> |
out.writeObject(q); |
491 |
> |
out.close(); |
492 |
> |
|
493 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
494 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
495 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
496 |
> |
assertEquals(q.size(), r.size()); |
497 |
> |
while (!q.isEmpty()) |
498 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
636 |
|
* add(null) throws NPE |
637 |
|
*/ |
638 |
|
public void testDescendingAddNull() { |
639 |
< |
try { |
639 |
> |
try { |
640 |
|
NavigableSet q = dset0(); |
641 |
|
q.add(null); |
642 |
|
shouldThrow(); |
643 |
< |
} catch (NullPointerException success) { } |
643 |
> |
} catch (NullPointerException success) {} |
644 |
|
} |
645 |
|
|
646 |
|
/** |
670 |
|
q.add(new Object()); |
671 |
|
q.add(new Object()); |
672 |
|
shouldThrow(); |
673 |
< |
} |
690 |
< |
catch (ClassCastException success) {} |
673 |
> |
} catch (ClassCastException success) {} |
674 |
|
} |
675 |
|
|
676 |
|
|
682 |
|
NavigableSet q = dset0(); |
683 |
|
q.addAll(null); |
684 |
|
shouldThrow(); |
685 |
< |
} |
703 |
< |
catch (NullPointerException success) {} |
685 |
> |
} catch (NullPointerException success) {} |
686 |
|
} |
687 |
|
/** |
688 |
|
* addAll of a collection with null elements throws NPE |
693 |
|
Integer[] ints = new Integer[SIZE]; |
694 |
|
q.addAll(Arrays.asList(ints)); |
695 |
|
shouldThrow(); |
696 |
< |
} |
715 |
< |
catch (NullPointerException success) {} |
696 |
> |
} catch (NullPointerException success) {} |
697 |
|
} |
698 |
|
/** |
699 |
|
* addAll of a collection with any null elements throws NPE after |
707 |
|
ints[i] = new Integer(i+SIZE); |
708 |
|
q.addAll(Arrays.asList(ints)); |
709 |
|
shouldThrow(); |
710 |
< |
} |
730 |
< |
catch (NullPointerException success) {} |
710 |
> |
} catch (NullPointerException success) {} |
711 |
|
} |
712 |
|
|
713 |
|
/** |
714 |
|
* Set contains all elements of successful addAll |
715 |
|
*/ |
716 |
|
public void testDescendingAddAll5() { |
717 |
< |
try { |
718 |
< |
Integer[] empty = new Integer[0]; |
719 |
< |
Integer[] ints = new Integer[SIZE]; |
720 |
< |
for (int i = 0; i < SIZE; ++i) |
721 |
< |
ints[i] = new Integer(SIZE-1- i); |
722 |
< |
NavigableSet q = dset0(); |
723 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
724 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
725 |
< |
for (int i = 0; i < SIZE; ++i) |
746 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
747 |
< |
} |
748 |
< |
finally {} |
717 |
> |
Integer[] empty = new Integer[0]; |
718 |
> |
Integer[] ints = new Integer[SIZE]; |
719 |
> |
for (int i = 0; i < SIZE; ++i) |
720 |
> |
ints[i] = new Integer(SIZE-1- i); |
721 |
> |
NavigableSet q = dset0(); |
722 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
723 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
724 |
> |
for (int i = 0; i < SIZE; ++i) |
725 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
726 |
|
} |
727 |
|
|
728 |
|
/** |
731 |
|
public void testDescendingPoll() { |
732 |
|
NavigableSet q = populatedSet(SIZE); |
733 |
|
for (int i = 0; i < SIZE; ++i) { |
734 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
734 |
> |
assertEquals(i, q.pollFirst()); |
735 |
|
} |
736 |
< |
assertNull(q.pollFirst()); |
736 |
> |
assertNull(q.pollFirst()); |
737 |
|
} |
738 |
|
|
739 |
|
/** |
909 |
|
*/ |
910 |
|
public void testDescendingToArray() { |
911 |
|
NavigableSet q = populatedSet(SIZE); |
912 |
< |
Object[] o = q.toArray(); |
912 |
> |
Object[] o = q.toArray(); |
913 |
|
Arrays.sort(o); |
914 |
< |
for (int i = 0; i < o.length; i++) |
915 |
< |
assertEquals(o[i], q.pollFirst()); |
914 |
> |
for (int i = 0; i < o.length; i++) |
915 |
> |
assertEquals(o[i], q.pollFirst()); |
916 |
|
} |
917 |
|
|
918 |
|
/** |
920 |
|
*/ |
921 |
|
public void testDescendingToArray2() { |
922 |
|
NavigableSet q = populatedSet(SIZE); |
923 |
< |
Integer[] ints = new Integer[SIZE]; |
924 |
< |
ints = (Integer[])q.toArray(ints); |
923 |
> |
Integer[] ints = new Integer[SIZE]; |
924 |
> |
ints = (Integer[])q.toArray(ints); |
925 |
|
Arrays.sort(ints); |
926 |
|
for (int i = 0; i < ints.length; i++) |
927 |
|
assertEquals(ints[i], q.pollFirst()); |
933 |
|
public void testDescendingIterator() { |
934 |
|
NavigableSet q = populatedSet(SIZE); |
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; |
947 |
|
public void testDescendingEmptyIterator() { |
948 |
|
NavigableSet q = dset0(); |
949 |
|
int i = 0; |
950 |
< |
Iterator it = q.iterator(); |
950 |
> |
Iterator it = q.iterator(); |
951 |
|
while (it.hasNext()) { |
952 |
|
assertTrue(q.contains(it.next())); |
953 |
|
++i; |
989 |
|
/** |
990 |
|
* A deserialized serialized set has same elements |
991 |
|
*/ |
992 |
< |
public void testDescendingSerialization() { |
992 |
> |
public void testDescendingSerialization() throws Exception { |
993 |
|
NavigableSet q = populatedSet(SIZE); |
994 |
< |
try { |
995 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
996 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
997 |
< |
out.writeObject(q); |
998 |
< |
out.close(); |
999 |
< |
|
1000 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1001 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1002 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
1003 |
< |
assertEquals(q.size(), r.size()); |
1004 |
< |
while (!q.isEmpty()) |
1028 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
1029 |
< |
} catch (Exception e) { |
1030 |
< |
e.printStackTrace(); |
1031 |
< |
unexpectedException(); |
1032 |
< |
} |
994 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
995 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
996 |
> |
out.writeObject(q); |
997 |
> |
out.close(); |
998 |
> |
|
999 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1000 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1001 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
1002 |
> |
assertEquals(q.size(), r.size()); |
1003 |
> |
while (!q.isEmpty()) |
1004 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
1005 |
|
} |
1006 |
|
|
1007 |
|
/** |