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 |
|
|
27 |
|
* Create a set of given size containing consecutive |
28 |
|
* Integers 0 ... n. |
29 |
|
*/ |
30 |
< |
private NavigableSet populatedSet(int n) { |
31 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
30 |
> |
private NavigableSet<Integer> populatedSet(int n) { |
31 |
> |
ConcurrentSkipListSet<Integer> q = |
32 |
> |
new ConcurrentSkipListSet<Integer>(); |
33 |
|
assertTrue(q.isEmpty()); |
34 |
|
|
35 |
< |
for(int i = n-1; i >= 0; i-=2) |
36 |
< |
assertTrue(q.add(new Integer(i))); |
37 |
< |
for(int i = (n & 1); i < n; i+=2) |
38 |
< |
assertTrue(q.add(new Integer(i))); |
35 |
> |
for (int i = n-1; i >= 0; i-=2) |
36 |
> |
assertTrue(q.add(new Integer(i))); |
37 |
> |
for (int i = (n & 1); i < n; i+=2) |
38 |
> |
assertTrue(q.add(new Integer(i))); |
39 |
|
assertTrue(q.add(new Integer(-n))); |
40 |
|
assertTrue(q.add(new Integer(n))); |
41 |
|
NavigableSet s = q.subSet(new Integer(0), true, new Integer(n), false); |
42 |
|
assertFalse(s.isEmpty()); |
43 |
< |
assertEquals(n, s.size()); |
43 |
> |
assertEquals(n, s.size()); |
44 |
|
return s; |
45 |
|
} |
46 |
|
|
58 |
|
q.add(zero); |
59 |
|
q.add(seven); |
60 |
|
NavigableSet s = q.subSet(one, true, seven, false); |
61 |
< |
assertEquals(5, s.size()); |
61 |
> |
assertEquals(5, s.size()); |
62 |
|
return s; |
63 |
|
} |
64 |
|
|
74 |
|
q.add(m4); |
75 |
|
q.add(m5); |
76 |
|
NavigableSet s = q.descendingSet(); |
77 |
< |
assertEquals(5, s.size()); |
77 |
> |
assertEquals(5, s.size()); |
78 |
|
return s; |
79 |
|
} |
80 |
|
|
81 |
|
private static NavigableSet set0() { |
82 |
< |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
82 |
> |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
83 |
|
assertTrue(set.isEmpty()); |
84 |
|
return set.tailSet(m1, true); |
85 |
|
} |
86 |
|
|
87 |
|
private static NavigableSet dset0() { |
88 |
< |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
88 |
> |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
89 |
|
assertTrue(set.isEmpty()); |
90 |
|
return set; |
91 |
|
} |
131 |
|
* add(null) throws NPE |
132 |
|
*/ |
133 |
|
public void testAddNull() { |
134 |
< |
try { |
134 |
> |
try { |
135 |
|
NavigableSet q = set0(); |
136 |
|
q.add(null); |
137 |
|
shouldThrow(); |
138 |
< |
} catch (NullPointerException success) { } |
138 |
> |
} catch (NullPointerException success) {} |
139 |
|
} |
140 |
|
|
141 |
|
/** |
165 |
|
q.add(new Object()); |
166 |
|
q.add(new Object()); |
167 |
|
shouldThrow(); |
168 |
< |
} |
172 |
< |
catch(ClassCastException success) {} |
168 |
> |
} catch (ClassCastException success) {} |
169 |
|
} |
170 |
|
|
171 |
|
|
177 |
|
NavigableSet q = set0(); |
178 |
|
q.addAll(null); |
179 |
|
shouldThrow(); |
180 |
< |
} |
185 |
< |
catch (NullPointerException success) {} |
180 |
> |
} catch (NullPointerException success) {} |
181 |
|
} |
182 |
+ |
|
183 |
|
/** |
184 |
|
* addAll of a collection with null elements throws NPE |
185 |
|
*/ |
189 |
|
Integer[] ints = new Integer[SIZE]; |
190 |
|
q.addAll(Arrays.asList(ints)); |
191 |
|
shouldThrow(); |
192 |
< |
} |
197 |
< |
catch (NullPointerException success) {} |
192 |
> |
} catch (NullPointerException success) {} |
193 |
|
} |
194 |
+ |
|
195 |
|
/** |
196 |
|
* addAll of a collection with any null elements throws NPE after |
197 |
|
* possibly adding some elements |
204 |
|
ints[i] = new Integer(i+SIZE); |
205 |
|
q.addAll(Arrays.asList(ints)); |
206 |
|
shouldThrow(); |
207 |
< |
} |
212 |
< |
catch (NullPointerException success) {} |
207 |
> |
} catch (NullPointerException success) {} |
208 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
|
* Set contains all elements of successful addAll |
212 |
|
*/ |
213 |
|
public void testAddAll5() { |
214 |
< |
try { |
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) |
228 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
229 |
< |
} |
230 |
< |
finally {} |
214 |
> |
Integer[] empty = new Integer[0]; |
215 |
> |
Integer[] ints = new Integer[SIZE]; |
216 |
> |
for (int i = 0; i < SIZE; ++i) |
217 |
> |
ints[i] = new Integer(SIZE-1- i); |
218 |
> |
NavigableSet q = set0(); |
219 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
220 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
221 |
> |
for (int i = 0; i < SIZE; ++i) |
222 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
223 |
|
} |
224 |
|
|
225 |
|
/** |
228 |
|
public void testPoll() { |
229 |
|
NavigableSet q = populatedSet(SIZE); |
230 |
|
for (int i = 0; i < SIZE; ++i) { |
231 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
231 |
> |
assertEquals(i, q.pollFirst()); |
232 |
|
} |
233 |
< |
assertNull(q.pollFirst()); |
233 |
> |
assertNull(q.pollFirst()); |
234 |
|
} |
235 |
|
|
236 |
|
/** |
341 |
|
|
342 |
|
Object e4 = q.lower(zero); |
343 |
|
assertNull(e4); |
352 |
– |
|
344 |
|
} |
345 |
|
|
346 |
|
/** |
359 |
|
|
360 |
|
Object e4 = q.higher(six); |
361 |
|
assertNull(e4); |
371 |
– |
|
362 |
|
} |
363 |
|
|
364 |
|
/** |
377 |
|
|
378 |
|
Object e4 = q.floor(zero); |
379 |
|
assertNull(e4); |
390 |
– |
|
380 |
|
} |
381 |
|
|
382 |
|
/** |
395 |
|
|
396 |
|
Object e4 = q.ceiling(six); |
397 |
|
assertNull(e4); |
409 |
– |
|
398 |
|
} |
399 |
|
|
400 |
|
/** |
401 |
< |
* toArray contains all elements |
401 |
> |
* toArray contains all elements in sorted order |
402 |
|
*/ |
403 |
|
public void testToArray() { |
404 |
|
NavigableSet q = populatedSet(SIZE); |
405 |
< |
Object[] o = q.toArray(); |
406 |
< |
Arrays.sort(o); |
407 |
< |
for(int i = 0; i < o.length; i++) |
420 |
< |
assertEquals(o[i], q.pollFirst()); |
405 |
> |
Object[] o = q.toArray(); |
406 |
> |
for (int i = 0; i < o.length; i++) |
407 |
> |
assertSame(o[i], q.pollFirst()); |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
< |
* toArray(a) contains all elements |
411 |
> |
* toArray(a) contains all elements in sorted order |
412 |
|
*/ |
413 |
|
public void testToArray2() { |
414 |
< |
NavigableSet q = populatedSet(SIZE); |
415 |
< |
Integer[] ints = new Integer[SIZE]; |
416 |
< |
ints = (Integer[])q.toArray(ints); |
417 |
< |
Arrays.sort(ints); |
418 |
< |
for(int i = 0; i < ints.length; i++) |
419 |
< |
assertEquals(ints[i], q.pollFirst()); |
414 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
415 |
> |
Integer[] ints = new Integer[SIZE]; |
416 |
> |
Integer[] array = q.toArray(ints); |
417 |
> |
assertSame(ints, array); |
418 |
> |
for (int i = 0; i < ints.length; i++) |
419 |
> |
assertSame(ints[i], q.pollFirst()); |
420 |
|
} |
421 |
|
|
422 |
|
/** |
425 |
|
public void testIterator() { |
426 |
|
NavigableSet q = populatedSet(SIZE); |
427 |
|
int i = 0; |
428 |
< |
Iterator it = q.iterator(); |
429 |
< |
while(it.hasNext()) { |
428 |
> |
Iterator it = q.iterator(); |
429 |
> |
while (it.hasNext()) { |
430 |
|
assertTrue(q.contains(it.next())); |
431 |
|
++i; |
432 |
|
} |
439 |
|
public void testEmptyIterator() { |
440 |
|
NavigableSet q = set0(); |
441 |
|
int i = 0; |
442 |
< |
Iterator it = q.iterator(); |
443 |
< |
while(it.hasNext()) { |
442 |
> |
Iterator it = q.iterator(); |
443 |
> |
while (it.hasNext()) { |
444 |
|
assertTrue(q.contains(it.next())); |
445 |
|
++i; |
446 |
|
} |
450 |
|
/** |
451 |
|
* iterator.remove removes current element |
452 |
|
*/ |
453 |
< |
public void testIteratorRemove () { |
453 |
> |
public void testIteratorRemove() { |
454 |
|
final NavigableSet q = set0(); |
455 |
|
q.add(new Integer(2)); |
456 |
|
q.add(new Integer(1)); |
481 |
|
/** |
482 |
|
* A deserialized serialized set has same elements |
483 |
|
*/ |
484 |
< |
public void testSerialization() { |
484 |
> |
public void testSerialization() throws Exception { |
485 |
|
NavigableSet q = populatedSet(SIZE); |
486 |
< |
try { |
487 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
488 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
489 |
< |
out.writeObject(q); |
490 |
< |
out.close(); |
491 |
< |
|
492 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
493 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
494 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
495 |
< |
assertEquals(q.size(), r.size()); |
496 |
< |
while (!q.isEmpty()) |
510 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
511 |
< |
} catch(Exception e){ |
512 |
< |
e.printStackTrace(); |
513 |
< |
unexpectedException(); |
514 |
< |
} |
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()); |
497 |
|
} |
498 |
|
|
499 |
|
/** |
634 |
|
* add(null) throws NPE |
635 |
|
*/ |
636 |
|
public void testDescendingAddNull() { |
637 |
< |
try { |
637 |
> |
try { |
638 |
|
NavigableSet q = dset0(); |
639 |
|
q.add(null); |
640 |
|
shouldThrow(); |
641 |
< |
} catch (NullPointerException success) { } |
641 |
> |
} catch (NullPointerException success) {} |
642 |
|
} |
643 |
|
|
644 |
|
/** |
668 |
|
q.add(new Object()); |
669 |
|
q.add(new Object()); |
670 |
|
shouldThrow(); |
671 |
< |
} |
690 |
< |
catch(ClassCastException success) {} |
671 |
> |
} catch (ClassCastException success) {} |
672 |
|
} |
673 |
|
|
674 |
|
|
680 |
|
NavigableSet q = dset0(); |
681 |
|
q.addAll(null); |
682 |
|
shouldThrow(); |
683 |
< |
} |
703 |
< |
catch (NullPointerException success) {} |
683 |
> |
} catch (NullPointerException success) {} |
684 |
|
} |
685 |
+ |
|
686 |
|
/** |
687 |
|
* addAll of a collection with null elements throws NPE |
688 |
|
*/ |
692 |
|
Integer[] ints = new Integer[SIZE]; |
693 |
|
q.addAll(Arrays.asList(ints)); |
694 |
|
shouldThrow(); |
695 |
< |
} |
715 |
< |
catch (NullPointerException success) {} |
695 |
> |
} catch (NullPointerException success) {} |
696 |
|
} |
697 |
+ |
|
698 |
|
/** |
699 |
|
* addAll of a collection with any null elements throws NPE after |
700 |
|
* possibly adding some elements |
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 |
|
/** |
844 |
|
|
845 |
|
Object e4 = q.lower(zero); |
846 |
|
assertNull(e4); |
870 |
– |
|
847 |
|
} |
848 |
|
|
849 |
|
/** |
862 |
|
|
863 |
|
Object e4 = q.higher(m6); |
864 |
|
assertNull(e4); |
889 |
– |
|
865 |
|
} |
866 |
|
|
867 |
|
/** |
880 |
|
|
881 |
|
Object e4 = q.floor(zero); |
882 |
|
assertNull(e4); |
908 |
– |
|
883 |
|
} |
884 |
|
|
885 |
|
/** |
898 |
|
|
899 |
|
Object e4 = q.ceiling(m6); |
900 |
|
assertNull(e4); |
927 |
– |
|
901 |
|
} |
902 |
|
|
903 |
|
/** |
905 |
|
*/ |
906 |
|
public void testDescendingToArray() { |
907 |
|
NavigableSet q = populatedSet(SIZE); |
908 |
< |
Object[] o = q.toArray(); |
908 |
> |
Object[] o = q.toArray(); |
909 |
|
Arrays.sort(o); |
910 |
< |
for(int i = 0; i < o.length; i++) |
911 |
< |
assertEquals(o[i], q.pollFirst()); |
910 |
> |
for (int i = 0; i < o.length; i++) |
911 |
> |
assertEquals(o[i], q.pollFirst()); |
912 |
|
} |
913 |
|
|
914 |
|
/** |
916 |
|
*/ |
917 |
|
public void testDescendingToArray2() { |
918 |
|
NavigableSet q = populatedSet(SIZE); |
919 |
< |
Integer[] ints = new Integer[SIZE]; |
920 |
< |
ints = (Integer[])q.toArray(ints); |
919 |
> |
Integer[] ints = new Integer[SIZE]; |
920 |
> |
assertSame(ints, q.toArray(ints)); |
921 |
|
Arrays.sort(ints); |
922 |
< |
for(int i = 0; i < ints.length; i++) |
922 |
> |
for (int i = 0; i < ints.length; i++) |
923 |
|
assertEquals(ints[i], q.pollFirst()); |
924 |
|
} |
925 |
|
|
929 |
|
public void testDescendingIterator() { |
930 |
|
NavigableSet q = populatedSet(SIZE); |
931 |
|
int i = 0; |
932 |
< |
Iterator it = q.iterator(); |
933 |
< |
while(it.hasNext()) { |
932 |
> |
Iterator it = q.iterator(); |
933 |
> |
while (it.hasNext()) { |
934 |
|
assertTrue(q.contains(it.next())); |
935 |
|
++i; |
936 |
|
} |
943 |
|
public void testDescendingEmptyIterator() { |
944 |
|
NavigableSet q = dset0(); |
945 |
|
int i = 0; |
946 |
< |
Iterator it = q.iterator(); |
947 |
< |
while(it.hasNext()) { |
946 |
> |
Iterator it = q.iterator(); |
947 |
> |
while (it.hasNext()) { |
948 |
|
assertTrue(q.contains(it.next())); |
949 |
|
++i; |
950 |
|
} |
954 |
|
/** |
955 |
|
* iterator.remove removes current element |
956 |
|
*/ |
957 |
< |
public void testDescendingIteratorRemove () { |
957 |
> |
public void testDescendingIteratorRemove() { |
958 |
|
final NavigableSet q = dset0(); |
959 |
|
q.add(new Integer(2)); |
960 |
|
q.add(new Integer(1)); |
985 |
|
/** |
986 |
|
* A deserialized serialized set has same elements |
987 |
|
*/ |
988 |
< |
public void testDescendingSerialization() { |
988 |
> |
public void testDescendingSerialization() throws Exception { |
989 |
|
NavigableSet q = populatedSet(SIZE); |
990 |
< |
try { |
991 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
992 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
993 |
< |
out.writeObject(q); |
994 |
< |
out.close(); |
995 |
< |
|
996 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
997 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
998 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
999 |
< |
assertEquals(q.size(), r.size()); |
1000 |
< |
while (!q.isEmpty()) |
1028 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
1029 |
< |
} catch(Exception e){ |
1030 |
< |
e.printStackTrace(); |
1031 |
< |
unexpectedException(); |
1032 |
< |
} |
990 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
991 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
992 |
> |
out.writeObject(q); |
993 |
> |
out.close(); |
994 |
> |
|
995 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
996 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
997 |
> |
NavigableSet r = (NavigableSet)in.readObject(); |
998 |
> |
assertEquals(q.size(), r.size()); |
999 |
> |
while (!q.isEmpty()) |
1000 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
1001 |
|
} |
1002 |
|
|
1003 |
|
/** |