11 |
|
|
12 |
|
public class ConcurrentSkipListSetTest 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(ConcurrentSkipListSetTest.class); |
17 |
> |
return new TestSuite(ConcurrentSkipListSetTest.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 |
|
|
30 |
|
private ConcurrentSkipListSet populatedSet(int n) { |
31 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
32 |
|
assertTrue(q.isEmpty()); |
33 |
< |
for(int i = n-1; i >= 0; i-=2) |
34 |
< |
assertTrue(q.add(new Integer(i))); |
35 |
< |
for(int i = (n & 1); i < n; i+=2) |
36 |
< |
assertTrue(q.add(new Integer(i))); |
33 |
> |
for (int i = n-1; i >= 0; i-=2) |
34 |
> |
assertTrue(q.add(new Integer(i))); |
35 |
> |
for (int i = (n & 1); i < n; i+=2) |
36 |
> |
assertTrue(q.add(new Integer(i))); |
37 |
|
assertFalse(q.isEmpty()); |
38 |
< |
assertEquals(n, q.size()); |
38 |
> |
assertEquals(n, q.size()); |
39 |
|
return q; |
40 |
|
} |
41 |
|
|
50 |
|
q.add(three); |
51 |
|
q.add(four); |
52 |
|
q.add(five); |
53 |
< |
assertEquals(5, q.size()); |
53 |
> |
assertEquals(5, q.size()); |
54 |
|
return q; |
55 |
|
} |
56 |
|
|
68 |
|
try { |
69 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null); |
70 |
|
shouldThrow(); |
71 |
< |
} |
76 |
< |
catch (NullPointerException success) {} |
71 |
> |
} catch (NullPointerException success) {} |
72 |
|
} |
73 |
|
|
74 |
|
/** |
79 |
|
Integer[] ints = new Integer[SIZE]; |
80 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
81 |
|
shouldThrow(); |
82 |
< |
} |
88 |
< |
catch (NullPointerException success) {} |
82 |
> |
} catch (NullPointerException success) {} |
83 |
|
} |
84 |
|
|
85 |
|
/** |
92 |
|
ints[i] = new Integer(i); |
93 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
94 |
|
shouldThrow(); |
95 |
< |
} |
102 |
< |
catch (NullPointerException success) {} |
95 |
> |
} catch (NullPointerException success) {} |
96 |
|
} |
97 |
|
|
98 |
|
/** |
99 |
|
* Set contains all elements of collection used to initialize |
100 |
|
*/ |
101 |
|
public void testConstructor6() { |
102 |
< |
try { |
103 |
< |
Integer[] ints = new Integer[SIZE]; |
104 |
< |
for (int i = 0; i < SIZE; ++i) |
105 |
< |
ints[i] = new Integer(i); |
106 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
107 |
< |
for (int i = 0; i < SIZE; ++i) |
115 |
< |
assertEquals(ints[i], q.pollFirst()); |
116 |
< |
} |
117 |
< |
finally {} |
102 |
> |
Integer[] ints = new Integer[SIZE]; |
103 |
> |
for (int i = 0; i < SIZE; ++i) |
104 |
> |
ints[i] = new Integer(i); |
105 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
106 |
> |
for (int i = 0; i < SIZE; ++i) |
107 |
> |
assertEquals(ints[i], q.pollFirst()); |
108 |
|
} |
109 |
|
|
110 |
|
/** |
111 |
|
* The comparator used in constructor is used |
112 |
|
*/ |
113 |
|
public void testConstructor7() { |
114 |
< |
try { |
115 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
116 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); |
117 |
< |
assertEquals(cmp, q.comparator()); |
118 |
< |
Integer[] ints = new Integer[SIZE]; |
119 |
< |
for (int i = 0; i < SIZE; ++i) |
120 |
< |
ints[i] = new Integer(i); |
121 |
< |
q.addAll(Arrays.asList(ints)); |
122 |
< |
for (int i = SIZE-1; i >= 0; --i) |
133 |
< |
assertEquals(ints[i], q.pollFirst()); |
134 |
< |
} |
135 |
< |
finally {} |
114 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
115 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); |
116 |
> |
assertEquals(cmp, q.comparator()); |
117 |
> |
Integer[] ints = new Integer[SIZE]; |
118 |
> |
for (int i = 0; i < SIZE; ++i) |
119 |
> |
ints[i] = new Integer(i); |
120 |
> |
q.addAll(Arrays.asList(ints)); |
121 |
> |
for (int i = SIZE-1; i >= 0; --i) |
122 |
> |
assertEquals(ints[i], q.pollFirst()); |
123 |
|
} |
124 |
|
|
125 |
|
/** |
155 |
|
* add(null) throws NPE |
156 |
|
*/ |
157 |
|
public void testAddNull() { |
158 |
< |
try { |
158 |
> |
try { |
159 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
160 |
|
q.add(null); |
161 |
|
shouldThrow(); |
162 |
< |
} catch (NullPointerException success) { } |
162 |
> |
} catch (NullPointerException success) {} |
163 |
|
} |
164 |
|
|
165 |
|
/** |
190 |
|
q.add(new Object()); |
191 |
|
q.add(new Object()); |
192 |
|
shouldThrow(); |
193 |
< |
} |
207 |
< |
catch(ClassCastException success) {} |
193 |
> |
} catch (ClassCastException success) {} |
194 |
|
} |
195 |
|
|
196 |
|
/** |
201 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
202 |
|
q.addAll(null); |
203 |
|
shouldThrow(); |
204 |
< |
} |
219 |
< |
catch (NullPointerException success) {} |
204 |
> |
} catch (NullPointerException success) {} |
205 |
|
} |
206 |
+ |
|
207 |
|
/** |
208 |
|
* addAll of a collection with null elements throws NPE |
209 |
|
*/ |
213 |
|
Integer[] ints = new Integer[SIZE]; |
214 |
|
q.addAll(Arrays.asList(ints)); |
215 |
|
shouldThrow(); |
216 |
< |
} |
231 |
< |
catch (NullPointerException success) {} |
216 |
> |
} catch (NullPointerException success) {} |
217 |
|
} |
218 |
+ |
|
219 |
|
/** |
220 |
|
* addAll of a collection with any null elements throws NPE after |
221 |
|
* possibly adding some elements |
228 |
|
ints[i] = new Integer(i); |
229 |
|
q.addAll(Arrays.asList(ints)); |
230 |
|
shouldThrow(); |
231 |
< |
} |
246 |
< |
catch (NullPointerException success) {} |
231 |
> |
} catch (NullPointerException success) {} |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
|
* Set contains all elements of successful addAll |
236 |
|
*/ |
237 |
|
public void testAddAll5() { |
238 |
< |
try { |
239 |
< |
Integer[] empty = new Integer[0]; |
240 |
< |
Integer[] ints = new Integer[SIZE]; |
241 |
< |
for (int i = 0; i < SIZE; ++i) |
242 |
< |
ints[i] = new Integer(SIZE-1-i); |
243 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
244 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
245 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
246 |
< |
for (int i = 0; i < SIZE; ++i) |
262 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
263 |
< |
} |
264 |
< |
finally {} |
238 |
> |
Integer[] empty = new Integer[0]; |
239 |
> |
Integer[] ints = new Integer[SIZE]; |
240 |
> |
for (int i = 0; i < SIZE; ++i) |
241 |
> |
ints[i] = new Integer(SIZE-1-i); |
242 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
243 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
244 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
245 |
> |
for (int i = 0; i < SIZE; ++i) |
246 |
> |
assertEquals(i, q.pollFirst()); |
247 |
|
} |
248 |
|
|
249 |
|
/** |
252 |
|
public void testPollFirst() { |
253 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
254 |
|
for (int i = 0; i < SIZE; ++i) { |
255 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
255 |
> |
assertEquals(i, q.pollFirst()); |
256 |
|
} |
257 |
< |
assertNull(q.pollFirst()); |
257 |
> |
assertNull(q.pollFirst()); |
258 |
|
} |
259 |
|
|
260 |
|
/** |
263 |
|
public void testPollLast() { |
264 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
265 |
|
for (int i = SIZE-1; i >= 0; --i) { |
266 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
266 |
> |
assertEquals(i, q.pollLast()); |
267 |
|
} |
268 |
< |
assertNull(q.pollFirst()); |
268 |
> |
assertNull(q.pollFirst()); |
269 |
|
} |
270 |
|
|
271 |
|
|
377 |
|
|
378 |
|
Object e4 = q.lower(zero); |
379 |
|
assertNull(e4); |
398 |
– |
|
380 |
|
} |
381 |
|
|
382 |
|
/** |
395 |
|
|
396 |
|
Object e4 = q.higher(six); |
397 |
|
assertNull(e4); |
417 |
– |
|
398 |
|
} |
399 |
|
|
400 |
|
/** |
413 |
|
|
414 |
|
Object e4 = q.floor(zero); |
415 |
|
assertNull(e4); |
436 |
– |
|
416 |
|
} |
417 |
|
|
418 |
|
/** |
431 |
|
|
432 |
|
Object e4 = q.ceiling(six); |
433 |
|
assertNull(e4); |
455 |
– |
|
434 |
|
} |
435 |
|
|
436 |
|
/** |
437 |
< |
* toArray contains all elements |
437 |
> |
* toArray contains all elements in sorted order |
438 |
|
*/ |
439 |
|
public void testToArray() { |
440 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
441 |
< |
Object[] o = q.toArray(); |
442 |
< |
Arrays.sort(o); |
443 |
< |
for(int i = 0; i < o.length; i++) |
466 |
< |
assertEquals(o[i], q.pollFirst()); |
441 |
> |
Object[] o = q.toArray(); |
442 |
> |
for (int i = 0; i < o.length; i++) |
443 |
> |
assertSame(o[i], q.pollFirst()); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
447 |
< |
* toArray(a) contains all elements |
447 |
> |
* toArray(a) contains all elements in sorted order |
448 |
|
*/ |
449 |
|
public void testToArray2() { |
450 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
451 |
< |
Integer[] ints = new Integer[SIZE]; |
452 |
< |
ints = (Integer[])q.toArray(ints); |
453 |
< |
Arrays.sort(ints); |
454 |
< |
for(int i = 0; i < ints.length; i++) |
478 |
< |
assertEquals(ints[i], q.pollFirst()); |
451 |
> |
Integer[] ints = new Integer[SIZE]; |
452 |
> |
assertSame(ints, q.toArray(ints)); |
453 |
> |
for (int i = 0; i < ints.length; i++) |
454 |
> |
assertSame(ints[i], q.pollFirst()); |
455 |
|
} |
456 |
|
|
457 |
|
/** |
460 |
|
public void testIterator() { |
461 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
462 |
|
int i = 0; |
463 |
< |
Iterator it = q.iterator(); |
464 |
< |
while(it.hasNext()) { |
463 |
> |
Iterator it = q.iterator(); |
464 |
> |
while (it.hasNext()) { |
465 |
|
assertTrue(q.contains(it.next())); |
466 |
|
++i; |
467 |
|
} |
474 |
|
public void testEmptyIterator() { |
475 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
476 |
|
int i = 0; |
477 |
< |
Iterator it = q.iterator(); |
478 |
< |
while(it.hasNext()) { |
477 |
> |
Iterator it = q.iterator(); |
478 |
> |
while (it.hasNext()) { |
479 |
|
assertTrue(q.contains(it.next())); |
480 |
|
++i; |
481 |
|
} |
485 |
|
/** |
486 |
|
* iterator.remove removes current element |
487 |
|
*/ |
488 |
< |
public void testIteratorRemove () { |
488 |
> |
public void testIteratorRemove() { |
489 |
|
final ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
490 |
|
q.add(new Integer(2)); |
491 |
|
q.add(new Integer(1)); |
516 |
|
/** |
517 |
|
* A deserialized serialized set has same elements |
518 |
|
*/ |
519 |
< |
public void testSerialization() { |
519 |
> |
public void testSerialization() throws Exception { |
520 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
521 |
< |
try { |
522 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
523 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
524 |
< |
out.writeObject(q); |
525 |
< |
out.close(); |
526 |
< |
|
527 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
528 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
529 |
< |
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
530 |
< |
assertEquals(q.size(), r.size()); |
531 |
< |
while (!q.isEmpty()) |
556 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
557 |
< |
} catch(Exception e){ |
558 |
< |
e.printStackTrace(); |
559 |
< |
unexpectedException(); |
560 |
< |
} |
521 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
522 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
523 |
> |
out.writeObject(q); |
524 |
> |
out.close(); |
525 |
> |
|
526 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
527 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
528 |
> |
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
529 |
> |
assertEquals(q.size(), r.size()); |
530 |
> |
while (!q.isEmpty()) |
531 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
532 |
|
} |
533 |
|
|
534 |
|
/** |
656 |
|
/** |
657 |
|
* Subsets of subsets subdivide correctly |
658 |
|
*/ |
659 |
< |
public void testRecursiveSubSets() { |
660 |
< |
int setSize = 1000; |
661 |
< |
Class cl = ConcurrentSkipListSet.class; |
659 |
> |
public void testRecursiveSubSets() throws Exception { |
660 |
> |
int setSize = expensiveTests ? 1000 : 100; |
661 |
> |
Class cl = ConcurrentSkipListSet.class; |
662 |
|
|
663 |
|
NavigableSet<Integer> set = newSet(cl); |
664 |
|
bs = new BitSet(setSize); |
675 |
|
0, setSize - 1, true); |
676 |
|
} |
677 |
|
|
678 |
< |
static NavigableSet<Integer> newSet(Class cl) { |
679 |
< |
NavigableSet<Integer> result = null; |
709 |
< |
try { |
710 |
< |
result = (NavigableSet<Integer>) cl.newInstance(); |
711 |
< |
} catch(Exception e) { |
712 |
< |
fail(); |
713 |
< |
} |
678 |
> |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
679 |
> |
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
680 |
|
assertEquals(result.size(), 0); |
681 |
|
assertFalse(result.iterator().hasNext()); |
682 |
|
return result; |
699 |
|
} |
700 |
|
|
701 |
|
// Remove a bunch of entries with iterator |
702 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
702 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
703 |
|
if (rnd.nextBoolean()) { |
704 |
|
bs.clear(it.next()); |
705 |
|
it.remove(); |
724 |
|
} |
725 |
|
|
726 |
|
// Remove a bunch of entries with iterator |
727 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
727 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
728 |
|
if (rnd.nextBoolean()) { |
729 |
|
bs.clear(it.next()); |
730 |
|
it.remove(); |
739 |
|
} else { |
740 |
|
try { |
741 |
|
set.add(element); |
742 |
< |
fail(); |
743 |
< |
} catch(IllegalArgumentException e) { |
778 |
< |
// expected |
779 |
< |
} |
742 |
> |
shouldThrow(); |
743 |
> |
} catch (IllegalArgumentException success) {} |
744 |
|
} |
745 |
|
} |
746 |
|
} |
873 |
|
// BitSet should support this! Test would run much faster |
874 |
|
while (element >= min) { |
875 |
|
if (bs.get(element)) |
876 |
< |
return(element); |
876 |
> |
return element; |
877 |
|
element--; |
878 |
|
} |
879 |
|
return -1; |
939 |
|
assertEq(rs.last(), -1); |
940 |
|
try { |
941 |
|
set.first(); |
942 |
< |
fail(); |
943 |
< |
} catch(NoSuchElementException e) { |
980 |
< |
// expected |
981 |
< |
} |
942 |
> |
shouldThrow(); |
943 |
> |
} catch (NoSuchElementException success) {} |
944 |
|
try { |
945 |
|
set.last(); |
946 |
< |
fail(); |
947 |
< |
} catch(NoSuchElementException e) { |
986 |
< |
// expected |
987 |
< |
} |
946 |
> |
shouldThrow(); |
947 |
> |
} catch (NoSuchElementException success) {} |
948 |
|
} |
949 |
|
} |
950 |
|
|