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 { |
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(); |
34 |
|
private ConcurrentSkipListSet populatedSet(int n) { |
35 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
36 |
|
assertTrue(q.isEmpty()); |
37 |
< |
for(int i = n-1; i >= 0; i-=2) |
38 |
< |
assertTrue(q.add(new Integer(i))); |
39 |
< |
for(int i = (n & 1); i < n; i+=2) |
40 |
< |
assertTrue(q.add(new Integer(i))); |
37 |
> |
for (int i = n-1; i >= 0; i-=2) |
38 |
> |
assertTrue(q.add(new Integer(i))); |
39 |
> |
for (int i = (n & 1); i < n; i+=2) |
40 |
> |
assertTrue(q.add(new Integer(i))); |
41 |
|
assertFalse(q.isEmpty()); |
42 |
< |
assertEquals(n, q.size()); |
42 |
> |
assertEquals(n, q.size()); |
43 |
|
return q; |
44 |
|
} |
45 |
|
|
54 |
|
q.add(three); |
55 |
|
q.add(four); |
56 |
|
q.add(five); |
57 |
< |
assertEquals(5, q.size()); |
57 |
> |
assertEquals(5, q.size()); |
58 |
|
return q; |
59 |
|
} |
60 |
< |
|
60 |
> |
|
61 |
|
/** |
62 |
|
* A new set has unbounded capacity |
63 |
|
*/ |
72 |
|
try { |
73 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null); |
74 |
|
shouldThrow(); |
75 |
< |
} |
76 |
< |
catch (NullPointerException success) {} |
75 |
> |
} catch (NullPointerException success) {} |
76 |
|
} |
77 |
|
|
78 |
|
/** |
83 |
|
Integer[] ints = new Integer[SIZE]; |
84 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
85 |
|
shouldThrow(); |
86 |
< |
} |
88 |
< |
catch (NullPointerException success) {} |
86 |
> |
} catch (NullPointerException success) {} |
87 |
|
} |
88 |
|
|
89 |
|
/** |
96 |
|
ints[i] = new Integer(i); |
97 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
98 |
|
shouldThrow(); |
99 |
< |
} |
102 |
< |
catch (NullPointerException success) {} |
99 |
> |
} catch (NullPointerException success) {} |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
|
* Set contains all elements of collection used to initialize |
104 |
|
*/ |
105 |
|
public void testConstructor6() { |
106 |
< |
try { |
107 |
< |
Integer[] ints = new Integer[SIZE]; |
108 |
< |
for (int i = 0; i < SIZE; ++i) |
109 |
< |
ints[i] = new Integer(i); |
110 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
111 |
< |
for (int i = 0; i < SIZE; ++i) |
115 |
< |
assertEquals(ints[i], q.pollFirst()); |
116 |
< |
} |
117 |
< |
finally {} |
106 |
> |
Integer[] ints = new Integer[SIZE]; |
107 |
> |
for (int i = 0; i < SIZE; ++i) |
108 |
> |
ints[i] = new Integer(i); |
109 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); |
110 |
> |
for (int i = 0; i < SIZE; ++i) |
111 |
> |
assertEquals(ints[i], q.pollFirst()); |
112 |
|
} |
113 |
|
|
114 |
|
/** |
115 |
|
* The comparator used in constructor is used |
116 |
|
*/ |
117 |
|
public void testConstructor7() { |
118 |
< |
try { |
119 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
120 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); |
121 |
< |
assertEquals(cmp, q.comparator()); |
122 |
< |
Integer[] ints = new Integer[SIZE]; |
123 |
< |
for (int i = 0; i < SIZE; ++i) |
124 |
< |
ints[i] = new Integer(i); |
125 |
< |
q.addAll(Arrays.asList(ints)); |
126 |
< |
for (int i = SIZE-1; i >= 0; --i) |
133 |
< |
assertEquals(ints[i], q.pollFirst()); |
134 |
< |
} |
135 |
< |
finally {} |
118 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
119 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); |
120 |
> |
assertEquals(cmp, q.comparator()); |
121 |
> |
Integer[] ints = new Integer[SIZE]; |
122 |
> |
for (int i = 0; i < SIZE; ++i) |
123 |
> |
ints[i] = new Integer(i); |
124 |
> |
q.addAll(Arrays.asList(ints)); |
125 |
> |
for (int i = SIZE-1; i >= 0; --i) |
126 |
> |
assertEquals(ints[i], q.pollFirst()); |
127 |
|
} |
128 |
|
|
129 |
|
/** |
159 |
|
* add(null) throws NPE |
160 |
|
*/ |
161 |
|
public void testAddNull() { |
162 |
< |
try { |
162 |
> |
try { |
163 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
164 |
|
q.add(null); |
165 |
|
shouldThrow(); |
166 |
< |
} catch (NullPointerException success) { } |
166 |
> |
} catch (NullPointerException success) {} |
167 |
|
} |
168 |
|
|
169 |
|
/** |
194 |
|
q.add(new Object()); |
195 |
|
q.add(new Object()); |
196 |
|
shouldThrow(); |
197 |
< |
} |
207 |
< |
catch(ClassCastException success) {} |
197 |
> |
} catch (ClassCastException success) {} |
198 |
|
} |
199 |
|
|
200 |
|
/** |
205 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
206 |
|
q.addAll(null); |
207 |
|
shouldThrow(); |
208 |
< |
} |
219 |
< |
catch (NullPointerException success) {} |
208 |
> |
} catch (NullPointerException success) {} |
209 |
|
} |
210 |
|
/** |
211 |
|
* addAll of a collection with null elements throws NPE |
216 |
|
Integer[] ints = new Integer[SIZE]; |
217 |
|
q.addAll(Arrays.asList(ints)); |
218 |
|
shouldThrow(); |
219 |
< |
} |
231 |
< |
catch (NullPointerException success) {} |
219 |
> |
} catch (NullPointerException success) {} |
220 |
|
} |
221 |
|
/** |
222 |
|
* addAll of a collection with any null elements throws NPE after |
230 |
|
ints[i] = new Integer(i); |
231 |
|
q.addAll(Arrays.asList(ints)); |
232 |
|
shouldThrow(); |
233 |
< |
} |
246 |
< |
catch (NullPointerException success) {} |
233 |
> |
} catch (NullPointerException success) {} |
234 |
|
} |
235 |
|
|
236 |
|
/** |
237 |
|
* Set contains all elements of successful addAll |
238 |
|
*/ |
239 |
|
public void testAddAll5() { |
240 |
< |
try { |
241 |
< |
Integer[] empty = new Integer[0]; |
242 |
< |
Integer[] ints = new Integer[SIZE]; |
243 |
< |
for (int i = 0; i < SIZE; ++i) |
244 |
< |
ints[i] = new Integer(SIZE-1-i); |
245 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
246 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
247 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
248 |
< |
for (int i = 0; i < SIZE; ++i) |
262 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
263 |
< |
} |
264 |
< |
finally {} |
240 |
> |
Integer[] empty = new Integer[0]; |
241 |
> |
Integer[] ints = new Integer[SIZE]; |
242 |
> |
for (int i = 0; i < SIZE; ++i) |
243 |
> |
ints[i] = new Integer(SIZE-1-i); |
244 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
245 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
246 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
247 |
> |
for (int i = 0; i < SIZE; ++i) |
248 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
249 |
|
} |
250 |
|
|
251 |
|
/** |
256 |
|
for (int i = 0; i < SIZE; ++i) { |
257 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
258 |
|
} |
259 |
< |
assertNull(q.pollFirst()); |
259 |
> |
assertNull(q.pollFirst()); |
260 |
|
} |
261 |
|
|
262 |
|
/** |
267 |
|
for (int i = SIZE-1; i >= 0; --i) { |
268 |
|
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
269 |
|
} |
270 |
< |
assertNull(q.pollFirst()); |
270 |
> |
assertNull(q.pollFirst()); |
271 |
|
} |
272 |
|
|
273 |
|
|
285 |
|
} |
286 |
|
assertTrue(q.isEmpty()); |
287 |
|
} |
288 |
< |
|
288 |
> |
|
289 |
|
/** |
290 |
|
* contains(x) reports true when elements added but not yet removed |
291 |
|
*/ |
361 |
|
} |
362 |
|
} |
363 |
|
|
364 |
< |
|
364 |
> |
|
365 |
|
|
366 |
|
/** |
367 |
|
* lower returns preceding element |
444 |
|
*/ |
445 |
|
public void testToArray() { |
446 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
447 |
< |
Object[] o = q.toArray(); |
447 |
> |
Object[] o = q.toArray(); |
448 |
|
Arrays.sort(o); |
449 |
< |
for(int i = 0; i < o.length; i++) |
450 |
< |
assertEquals(o[i], q.pollFirst()); |
449 |
> |
for (int i = 0; i < o.length; i++) |
450 |
> |
assertEquals(o[i], q.pollFirst()); |
451 |
|
} |
452 |
|
|
453 |
|
/** |
455 |
|
*/ |
456 |
|
public void testToArray2() { |
457 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
458 |
< |
Integer[] ints = new Integer[SIZE]; |
459 |
< |
ints = (Integer[])q.toArray(ints); |
458 |
> |
Integer[] ints = new Integer[SIZE]; |
459 |
> |
ints = (Integer[])q.toArray(ints); |
460 |
|
Arrays.sort(ints); |
461 |
< |
for(int i = 0; i < ints.length; i++) |
461 |
> |
for (int i = 0; i < ints.length; i++) |
462 |
|
assertEquals(ints[i], q.pollFirst()); |
463 |
|
} |
464 |
< |
|
464 |
> |
|
465 |
|
/** |
466 |
|
* iterator iterates through all elements |
467 |
|
*/ |
468 |
|
public void testIterator() { |
469 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
470 |
|
int i = 0; |
471 |
< |
Iterator it = q.iterator(); |
472 |
< |
while(it.hasNext()) { |
471 |
> |
Iterator it = q.iterator(); |
472 |
> |
while (it.hasNext()) { |
473 |
|
assertTrue(q.contains(it.next())); |
474 |
|
++i; |
475 |
|
} |
482 |
|
public void testEmptyIterator() { |
483 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
484 |
|
int i = 0; |
485 |
< |
Iterator it = q.iterator(); |
486 |
< |
while(it.hasNext()) { |
485 |
> |
Iterator it = q.iterator(); |
486 |
> |
while (it.hasNext()) { |
487 |
|
assertTrue(q.contains(it.next())); |
488 |
|
++i; |
489 |
|
} |
519 |
|
for (int i = 0; i < SIZE; ++i) { |
520 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
521 |
|
} |
522 |
< |
} |
522 |
> |
} |
523 |
|
|
524 |
|
/** |
525 |
< |
* A deserialized serialized set has same elements |
525 |
> |
* A deserialized serialized set has same elements |
526 |
|
*/ |
527 |
< |
public void testSerialization() { |
527 |
> |
public void testSerialization() throws Exception { |
528 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
529 |
< |
try { |
530 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
531 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
532 |
< |
out.writeObject(q); |
533 |
< |
out.close(); |
534 |
< |
|
535 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
536 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
537 |
< |
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
538 |
< |
assertEquals(q.size(), r.size()); |
539 |
< |
while (!q.isEmpty()) |
556 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
557 |
< |
} catch(Exception e){ |
558 |
< |
e.printStackTrace(); |
559 |
< |
unexpectedException(); |
560 |
< |
} |
529 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
530 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
531 |
> |
out.writeObject(q); |
532 |
> |
out.close(); |
533 |
> |
|
534 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
535 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
536 |
> |
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
537 |
> |
assertEquals(q.size(), r.size()); |
538 |
> |
while (!q.isEmpty()) |
539 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
540 |
|
} |
541 |
|
|
542 |
|
/** |
665 |
|
* Subsets of subsets subdivide correctly |
666 |
|
*/ |
667 |
|
public void testRecursiveSubSets() { |
668 |
< |
int setSize = 1000; |
669 |
< |
Class cl = ConcurrentSkipListSet.class; |
668 |
> |
int setSize = 1000; |
669 |
> |
Class cl = ConcurrentSkipListSet.class; |
670 |
|
|
671 |
|
NavigableSet<Integer> set = newSet(cl); |
672 |
|
bs = new BitSet(setSize); |
685 |
|
|
686 |
|
static NavigableSet<Integer> newSet(Class cl) { |
687 |
|
NavigableSet<Integer> result = null; |
688 |
< |
try { |
688 |
> |
try { |
689 |
|
result = (NavigableSet<Integer>) cl.newInstance(); |
690 |
< |
} catch(Exception e) { |
690 |
> |
} catch (Exception e) { |
691 |
|
fail(); |
692 |
< |
} |
692 |
> |
} |
693 |
|
assertEquals(result.size(), 0); |
694 |
|
assertFalse(result.iterator().hasNext()); |
695 |
|
return result; |
712 |
|
} |
713 |
|
|
714 |
|
// Remove a bunch of entries with iterator |
715 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
715 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
716 |
|
if (rnd.nextBoolean()) { |
717 |
|
bs.clear(it.next()); |
718 |
|
it.remove(); |
737 |
|
} |
738 |
|
|
739 |
|
// Remove a bunch of entries with iterator |
740 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
740 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
741 |
|
if (rnd.nextBoolean()) { |
742 |
|
bs.clear(it.next()); |
743 |
|
it.remove(); |
753 |
|
try { |
754 |
|
set.add(element); |
755 |
|
fail(); |
756 |
< |
} catch(IllegalArgumentException e) { |
756 |
> |
} catch (IllegalArgumentException e) { |
757 |
|
// expected |
758 |
|
} |
759 |
|
} |
955 |
|
try { |
956 |
|
set.first(); |
957 |
|
fail(); |
958 |
< |
} catch(NoSuchElementException e) { |
958 |
> |
} catch (NoSuchElementException e) { |
959 |
|
// expected |
960 |
|
} |
961 |
|
try { |
962 |
|
set.last(); |
963 |
|
fail(); |
964 |
< |
} catch(NoSuchElementException e) { |
964 |
> |
} catch (NoSuchElementException e) { |
965 |
|
// expected |
966 |
|
} |
967 |
|
} |