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 { |
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(); |
35 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
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 |
|
|
77 |
|
q.add(m4); |
78 |
|
q.add(m5); |
79 |
|
NavigableSet s = q.descendingSet(); |
80 |
< |
assertEquals(5, s.size()); |
80 |
> |
assertEquals(5, s.size()); |
81 |
|
return s; |
82 |
|
} |
83 |
|
|
84 |
< |
private static NavigableSet set0() { |
85 |
< |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
84 |
> |
private static NavigableSet set0() { |
85 |
> |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
86 |
|
assertTrue(set.isEmpty()); |
87 |
|
return set.tailSet(m1, true); |
88 |
|
} |
89 |
|
|
90 |
< |
private static NavigableSet dset0() { |
91 |
< |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
90 |
> |
private static NavigableSet dset0() { |
91 |
> |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
92 |
|
assertTrue(set.isEmpty()); |
93 |
|
return set; |
94 |
|
} |
95 |
< |
|
95 |
> |
|
96 |
|
/** |
97 |
|
* A new set has unbounded capacity |
98 |
|
*/ |
134 |
|
* add(null) throws NPE |
135 |
|
*/ |
136 |
|
public void testAddNull() { |
137 |
< |
try { |
137 |
> |
try { |
138 |
|
NavigableSet q = set0(); |
139 |
|
q.add(null); |
140 |
|
shouldThrow(); |
141 |
< |
} catch (NullPointerException success) { } |
141 |
> |
} catch (NullPointerException success) {} |
142 |
|
} |
143 |
|
|
144 |
|
/** |
168 |
|
q.add(new Object()); |
169 |
|
q.add(new Object()); |
170 |
|
shouldThrow(); |
171 |
< |
} |
172 |
< |
catch(ClassCastException success) {} |
171 |
> |
} catch (ClassCastException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
|
180 |
|
NavigableSet q = set0(); |
181 |
|
q.addAll(null); |
182 |
|
shouldThrow(); |
183 |
< |
} |
185 |
< |
catch (NullPointerException success) {} |
183 |
> |
} catch (NullPointerException success) {} |
184 |
|
} |
185 |
|
/** |
186 |
|
* addAll of a collection with null elements throws NPE |
191 |
|
Integer[] ints = new Integer[SIZE]; |
192 |
|
q.addAll(Arrays.asList(ints)); |
193 |
|
shouldThrow(); |
194 |
< |
} |
197 |
< |
catch (NullPointerException success) {} |
194 |
> |
} catch (NullPointerException success) {} |
195 |
|
} |
196 |
|
/** |
197 |
|
* addAll of a collection with any null elements throws NPE after |
205 |
|
ints[i] = new Integer(i+SIZE); |
206 |
|
q.addAll(Arrays.asList(ints)); |
207 |
|
shouldThrow(); |
208 |
< |
} |
212 |
< |
catch (NullPointerException success) {} |
208 |
> |
} catch (NullPointerException success) {} |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
|
* Set contains all elements of successful addAll |
213 |
|
*/ |
214 |
|
public void testAddAll5() { |
215 |
< |
try { |
216 |
< |
Integer[] empty = new Integer[0]; |
217 |
< |
Integer[] ints = new Integer[SIZE]; |
218 |
< |
for (int i = 0; i < SIZE; ++i) |
219 |
< |
ints[i] = new Integer(SIZE-1- i); |
220 |
< |
NavigableSet q = set0(); |
221 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
222 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
223 |
< |
for (int i = 0; i < SIZE; ++i) |
228 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
229 |
< |
} |
230 |
< |
finally {} |
215 |
> |
Integer[] empty = new Integer[0]; |
216 |
> |
Integer[] ints = new Integer[SIZE]; |
217 |
> |
for (int i = 0; i < SIZE; ++i) |
218 |
> |
ints[i] = new Integer(SIZE-1- i); |
219 |
> |
NavigableSet q = set0(); |
220 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
221 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
222 |
> |
for (int i = 0; i < SIZE; ++i) |
223 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
231 |
|
for (int i = 0; i < SIZE; ++i) { |
232 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
233 |
|
} |
234 |
< |
assertNull(q.pollFirst()); |
234 |
> |
assertNull(q.pollFirst()); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
248 |
|
} |
249 |
|
assertTrue(q.isEmpty()); |
250 |
|
} |
251 |
< |
|
251 |
> |
|
252 |
|
/** |
253 |
|
* contains(x) reports true when elements added but not yet removed |
254 |
|
*/ |
324 |
|
} |
325 |
|
} |
326 |
|
|
327 |
< |
|
327 |
> |
|
328 |
|
|
329 |
|
/** |
330 |
|
* lower returns preceding element |
407 |
|
*/ |
408 |
|
public void testToArray() { |
409 |
|
NavigableSet q = populatedSet(SIZE); |
410 |
< |
Object[] o = q.toArray(); |
410 |
> |
Object[] o = q.toArray(); |
411 |
|
Arrays.sort(o); |
412 |
< |
for(int i = 0; i < o.length; i++) |
413 |
< |
assertEquals(o[i], q.pollFirst()); |
412 |
> |
for (int i = 0; i < o.length; i++) |
413 |
> |
assertEquals(o[i], q.pollFirst()); |
414 |
|
} |
415 |
|
|
416 |
|
/** |
418 |
|
*/ |
419 |
|
public void testToArray2() { |
420 |
|
NavigableSet q = populatedSet(SIZE); |
421 |
< |
Integer[] ints = new Integer[SIZE]; |
422 |
< |
ints = (Integer[])q.toArray(ints); |
421 |
> |
Integer[] ints = new Integer[SIZE]; |
422 |
> |
ints = (Integer[])q.toArray(ints); |
423 |
|
Arrays.sort(ints); |
424 |
< |
for(int i = 0; i < ints.length; i++) |
424 |
> |
for (int i = 0; i < ints.length; i++) |
425 |
|
assertEquals(ints[i], q.pollFirst()); |
426 |
|
} |
427 |
< |
|
427 |
> |
|
428 |
|
/** |
429 |
|
* iterator iterates through all elements |
430 |
|
*/ |
431 |
|
public void testIterator() { |
432 |
|
NavigableSet q = populatedSet(SIZE); |
433 |
|
int i = 0; |
434 |
< |
Iterator it = q.iterator(); |
435 |
< |
while(it.hasNext()) { |
434 |
> |
Iterator it = q.iterator(); |
435 |
> |
while (it.hasNext()) { |
436 |
|
assertTrue(q.contains(it.next())); |
437 |
|
++i; |
438 |
|
} |
445 |
|
public void testEmptyIterator() { |
446 |
|
NavigableSet q = set0(); |
447 |
|
int i = 0; |
448 |
< |
Iterator it = q.iterator(); |
449 |
< |
while(it.hasNext()) { |
448 |
> |
Iterator it = q.iterator(); |
449 |
> |
while (it.hasNext()) { |
450 |
|
assertTrue(q.contains(it.next())); |
451 |
|
++i; |
452 |
|
} |
482 |
|
for (int i = 0; i < SIZE; ++i) { |
483 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
484 |
|
} |
485 |
< |
} |
485 |
> |
} |
486 |
|
|
487 |
|
/** |
488 |
< |
* A deserialized serialized set has same elements |
488 |
> |
* A deserialized serialized set has same elements |
489 |
|
*/ |
490 |
< |
public void testSerialization() { |
490 |
> |
public void testSerialization() throws Exception { |
491 |
|
NavigableSet q = populatedSet(SIZE); |
492 |
< |
try { |
493 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
494 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
495 |
< |
out.writeObject(q); |
496 |
< |
out.close(); |
497 |
< |
|
498 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
499 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
500 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
501 |
< |
assertEquals(q.size(), r.size()); |
502 |
< |
while (!q.isEmpty()) |
510 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
511 |
< |
} catch(Exception e){ |
512 |
< |
e.printStackTrace(); |
513 |
< |
unexpectedException(); |
514 |
< |
} |
492 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
493 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
494 |
> |
out.writeObject(q); |
495 |
> |
out.close(); |
496 |
> |
|
497 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
498 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
499 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
500 |
> |
assertEquals(q.size(), r.size()); |
501 |
> |
while (!q.isEmpty()) |
502 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
503 |
|
} |
504 |
|
|
505 |
|
/** |
640 |
|
* add(null) throws NPE |
641 |
|
*/ |
642 |
|
public void testDescendingAddNull() { |
643 |
< |
try { |
643 |
> |
try { |
644 |
|
NavigableSet q = dset0(); |
645 |
|
q.add(null); |
646 |
|
shouldThrow(); |
647 |
< |
} catch (NullPointerException success) { } |
647 |
> |
} catch (NullPointerException success) {} |
648 |
|
} |
649 |
|
|
650 |
|
/** |
674 |
|
q.add(new Object()); |
675 |
|
q.add(new Object()); |
676 |
|
shouldThrow(); |
677 |
< |
} |
690 |
< |
catch(ClassCastException success) {} |
677 |
> |
} catch (ClassCastException success) {} |
678 |
|
} |
679 |
|
|
680 |
|
|
686 |
|
NavigableSet q = dset0(); |
687 |
|
q.addAll(null); |
688 |
|
shouldThrow(); |
689 |
< |
} |
703 |
< |
catch (NullPointerException success) {} |
689 |
> |
} catch (NullPointerException success) {} |
690 |
|
} |
691 |
|
/** |
692 |
|
* addAll of a collection with null elements throws NPE |
697 |
|
Integer[] ints = new Integer[SIZE]; |
698 |
|
q.addAll(Arrays.asList(ints)); |
699 |
|
shouldThrow(); |
700 |
< |
} |
715 |
< |
catch (NullPointerException success) {} |
700 |
> |
} catch (NullPointerException success) {} |
701 |
|
} |
702 |
|
/** |
703 |
|
* addAll of a collection with any null elements throws NPE after |
711 |
|
ints[i] = new Integer(i+SIZE); |
712 |
|
q.addAll(Arrays.asList(ints)); |
713 |
|
shouldThrow(); |
714 |
< |
} |
730 |
< |
catch (NullPointerException success) {} |
714 |
> |
} catch (NullPointerException success) {} |
715 |
|
} |
716 |
|
|
717 |
|
/** |
718 |
|
* Set contains all elements of successful addAll |
719 |
|
*/ |
720 |
|
public void testDescendingAddAll5() { |
721 |
< |
try { |
722 |
< |
Integer[] empty = new Integer[0]; |
723 |
< |
Integer[] ints = new Integer[SIZE]; |
724 |
< |
for (int i = 0; i < SIZE; ++i) |
725 |
< |
ints[i] = new Integer(SIZE-1- i); |
726 |
< |
NavigableSet q = dset0(); |
727 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
728 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
729 |
< |
for (int i = 0; i < SIZE; ++i) |
746 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
747 |
< |
} |
748 |
< |
finally {} |
721 |
> |
Integer[] empty = new Integer[0]; |
722 |
> |
Integer[] ints = new Integer[SIZE]; |
723 |
> |
for (int i = 0; i < SIZE; ++i) |
724 |
> |
ints[i] = new Integer(SIZE-1- i); |
725 |
> |
NavigableSet q = dset0(); |
726 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
727 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
728 |
> |
for (int i = 0; i < SIZE; ++i) |
729 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
730 |
|
} |
731 |
|
|
732 |
|
/** |
737 |
|
for (int i = 0; i < SIZE; ++i) { |
738 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
739 |
|
} |
740 |
< |
assertNull(q.pollFirst()); |
740 |
> |
assertNull(q.pollFirst()); |
741 |
|
} |
742 |
|
|
743 |
|
/** |
754 |
|
} |
755 |
|
assertTrue(q.isEmpty()); |
756 |
|
} |
757 |
< |
|
757 |
> |
|
758 |
|
/** |
759 |
|
* contains(x) reports true when elements added but not yet removed |
760 |
|
*/ |
830 |
|
} |
831 |
|
} |
832 |
|
|
833 |
< |
|
833 |
> |
|
834 |
|
|
835 |
|
/** |
836 |
|
* lower returns preceding element |
913 |
|
*/ |
914 |
|
public void testDescendingToArray() { |
915 |
|
NavigableSet q = populatedSet(SIZE); |
916 |
< |
Object[] o = q.toArray(); |
916 |
> |
Object[] o = q.toArray(); |
917 |
|
Arrays.sort(o); |
918 |
< |
for(int i = 0; i < o.length; i++) |
919 |
< |
assertEquals(o[i], q.pollFirst()); |
918 |
> |
for (int i = 0; i < o.length; i++) |
919 |
> |
assertEquals(o[i], q.pollFirst()); |
920 |
|
} |
921 |
|
|
922 |
|
/** |
924 |
|
*/ |
925 |
|
public void testDescendingToArray2() { |
926 |
|
NavigableSet q = populatedSet(SIZE); |
927 |
< |
Integer[] ints = new Integer[SIZE]; |
928 |
< |
ints = (Integer[])q.toArray(ints); |
927 |
> |
Integer[] ints = new Integer[SIZE]; |
928 |
> |
ints = (Integer[])q.toArray(ints); |
929 |
|
Arrays.sort(ints); |
930 |
< |
for(int i = 0; i < ints.length; i++) |
930 |
> |
for (int i = 0; i < ints.length; i++) |
931 |
|
assertEquals(ints[i], q.pollFirst()); |
932 |
|
} |
933 |
< |
|
933 |
> |
|
934 |
|
/** |
935 |
|
* iterator iterates through all elements |
936 |
|
*/ |
937 |
|
public void testDescendingIterator() { |
938 |
|
NavigableSet q = populatedSet(SIZE); |
939 |
|
int i = 0; |
940 |
< |
Iterator it = q.iterator(); |
941 |
< |
while(it.hasNext()) { |
940 |
> |
Iterator it = q.iterator(); |
941 |
> |
while (it.hasNext()) { |
942 |
|
assertTrue(q.contains(it.next())); |
943 |
|
++i; |
944 |
|
} |
951 |
|
public void testDescendingEmptyIterator() { |
952 |
|
NavigableSet q = dset0(); |
953 |
|
int i = 0; |
954 |
< |
Iterator it = q.iterator(); |
955 |
< |
while(it.hasNext()) { |
954 |
> |
Iterator it = q.iterator(); |
955 |
> |
while (it.hasNext()) { |
956 |
|
assertTrue(q.contains(it.next())); |
957 |
|
++i; |
958 |
|
} |
988 |
|
for (int i = 0; i < SIZE; ++i) { |
989 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
990 |
|
} |
991 |
< |
} |
991 |
> |
} |
992 |
|
|
993 |
|
/** |
994 |
< |
* A deserialized serialized set has same elements |
994 |
> |
* A deserialized serialized set has same elements |
995 |
|
*/ |
996 |
< |
public void testDescendingSerialization() { |
996 |
> |
public void testDescendingSerialization() throws Exception { |
997 |
|
NavigableSet q = populatedSet(SIZE); |
998 |
< |
try { |
999 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1000 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1001 |
< |
out.writeObject(q); |
1002 |
< |
out.close(); |
1003 |
< |
|
1004 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1005 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1006 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
1007 |
< |
assertEquals(q.size(), r.size()); |
1008 |
< |
while (!q.isEmpty()) |
1028 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
1029 |
< |
} catch(Exception e){ |
1030 |
< |
e.printStackTrace(); |
1031 |
< |
unexpectedException(); |
1032 |
< |
} |
998 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
999 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1000 |
> |
out.writeObject(q); |
1001 |
> |
out.close(); |
1002 |
> |
|
1003 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1004 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1005 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
1006 |
> |
assertEquals(q.size(), r.size()); |
1007 |
> |
while (!q.isEmpty()) |
1008 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
1009 |
|
} |
1010 |
|
|
1011 |
|
/** |