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 |
|
/** |
162 |
|
* add(null) throws NPE |
163 |
|
*/ |
164 |
|
public void testAddNull() { |
165 |
< |
try { |
165 |
> |
try { |
166 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
167 |
|
q.add(null); |
168 |
|
shouldThrow(); |
169 |
< |
} catch (NullPointerException success) { } |
169 |
> |
} catch (NullPointerException success) {} |
170 |
|
} |
171 |
|
|
172 |
|
/** |
197 |
|
q.add(new Object()); |
198 |
|
q.add(new Object()); |
199 |
|
shouldThrow(); |
200 |
< |
} |
207 |
< |
catch(ClassCastException success) {} |
200 |
> |
} catch (ClassCastException success) {} |
201 |
|
} |
202 |
|
|
203 |
|
/** |
208 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
209 |
|
q.addAll(null); |
210 |
|
shouldThrow(); |
211 |
< |
} |
219 |
< |
catch (NullPointerException success) {} |
211 |
> |
} catch (NullPointerException success) {} |
212 |
|
} |
213 |
|
/** |
214 |
|
* addAll of a collection with null elements throws NPE |
219 |
|
Integer[] ints = new Integer[SIZE]; |
220 |
|
q.addAll(Arrays.asList(ints)); |
221 |
|
shouldThrow(); |
222 |
< |
} |
231 |
< |
catch (NullPointerException success) {} |
222 |
> |
} catch (NullPointerException success) {} |
223 |
|
} |
224 |
|
/** |
225 |
|
* addAll of a collection with any null elements throws NPE after |
233 |
|
ints[i] = new Integer(i); |
234 |
|
q.addAll(Arrays.asList(ints)); |
235 |
|
shouldThrow(); |
236 |
< |
} |
246 |
< |
catch (NullPointerException success) {} |
236 |
> |
} catch (NullPointerException success) {} |
237 |
|
} |
238 |
|
|
239 |
|
/** |
262 |
|
for (int i = 0; i < SIZE; ++i) { |
263 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
264 |
|
} |
265 |
< |
assertNull(q.pollFirst()); |
265 |
> |
assertNull(q.pollFirst()); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
273 |
|
for (int i = SIZE-1; i >= 0; --i) { |
274 |
|
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
275 |
|
} |
276 |
< |
assertNull(q.pollFirst()); |
276 |
> |
assertNull(q.pollFirst()); |
277 |
|
} |
278 |
|
|
279 |
|
|
291 |
|
} |
292 |
|
assertTrue(q.isEmpty()); |
293 |
|
} |
294 |
< |
|
294 |
> |
|
295 |
|
/** |
296 |
|
* contains(x) reports true when elements added but not yet removed |
297 |
|
*/ |
367 |
|
} |
368 |
|
} |
369 |
|
|
370 |
< |
|
370 |
> |
|
371 |
|
|
372 |
|
/** |
373 |
|
* lower returns preceding element |
450 |
|
*/ |
451 |
|
public void testToArray() { |
452 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
453 |
< |
Object[] o = q.toArray(); |
453 |
> |
Object[] o = q.toArray(); |
454 |
|
Arrays.sort(o); |
455 |
< |
for(int i = 0; i < o.length; i++) |
456 |
< |
assertEquals(o[i], q.pollFirst()); |
455 |
> |
for (int i = 0; i < o.length; i++) |
456 |
> |
assertEquals(o[i], q.pollFirst()); |
457 |
|
} |
458 |
|
|
459 |
|
/** |
461 |
|
*/ |
462 |
|
public void testToArray2() { |
463 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
464 |
< |
Integer[] ints = new Integer[SIZE]; |
465 |
< |
ints = (Integer[])q.toArray(ints); |
464 |
> |
Integer[] ints = new Integer[SIZE]; |
465 |
> |
ints = (Integer[])q.toArray(ints); |
466 |
|
Arrays.sort(ints); |
467 |
< |
for(int i = 0; i < ints.length; i++) |
467 |
> |
for (int i = 0; i < ints.length; i++) |
468 |
|
assertEquals(ints[i], q.pollFirst()); |
469 |
|
} |
470 |
< |
|
470 |
> |
|
471 |
|
/** |
472 |
|
* iterator iterates through all elements |
473 |
|
*/ |
474 |
|
public void testIterator() { |
475 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
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 |
|
} |
488 |
|
public void testEmptyIterator() { |
489 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
490 |
|
int i = 0; |
491 |
< |
Iterator it = q.iterator(); |
492 |
< |
while(it.hasNext()) { |
491 |
> |
Iterator it = q.iterator(); |
492 |
> |
while (it.hasNext()) { |
493 |
|
assertTrue(q.contains(it.next())); |
494 |
|
++i; |
495 |
|
} |
525 |
|
for (int i = 0; i < SIZE; ++i) { |
526 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
527 |
|
} |
528 |
< |
} |
528 |
> |
} |
529 |
|
|
530 |
|
/** |
531 |
< |
* A deserialized serialized set has same elements |
531 |
> |
* A deserialized serialized set has same elements |
532 |
|
*/ |
533 |
< |
public void testSerialization() { |
533 |
> |
public void testSerialization() throws Exception { |
534 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
535 |
< |
try { |
536 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
537 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
538 |
< |
out.writeObject(q); |
539 |
< |
out.close(); |
540 |
< |
|
541 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
542 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
543 |
< |
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
544 |
< |
assertEquals(q.size(), r.size()); |
545 |
< |
while (!q.isEmpty()) |
556 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
557 |
< |
} catch(Exception e){ |
558 |
< |
e.printStackTrace(); |
559 |
< |
unexpectedException(); |
560 |
< |
} |
535 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
536 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
537 |
> |
out.writeObject(q); |
538 |
> |
out.close(); |
539 |
> |
|
540 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
541 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
542 |
> |
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
543 |
> |
assertEquals(q.size(), r.size()); |
544 |
> |
while (!q.isEmpty()) |
545 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
546 |
|
} |
547 |
|
|
548 |
|
/** |
671 |
|
* Subsets of subsets subdivide correctly |
672 |
|
*/ |
673 |
|
public void testRecursiveSubSets() { |
674 |
< |
int setSize = 1000; |
675 |
< |
Class cl = ConcurrentSkipListSet.class; |
674 |
> |
int setSize = 1000; |
675 |
> |
Class cl = ConcurrentSkipListSet.class; |
676 |
|
|
677 |
|
NavigableSet<Integer> set = newSet(cl); |
678 |
|
bs = new BitSet(setSize); |
691 |
|
|
692 |
|
static NavigableSet<Integer> newSet(Class cl) { |
693 |
|
NavigableSet<Integer> result = null; |
694 |
< |
try { |
694 |
> |
try { |
695 |
|
result = (NavigableSet<Integer>) cl.newInstance(); |
696 |
< |
} catch(Exception e) { |
696 |
> |
} catch (Exception e) { |
697 |
|
fail(); |
698 |
< |
} |
698 |
> |
} |
699 |
|
assertEquals(result.size(), 0); |
700 |
|
assertFalse(result.iterator().hasNext()); |
701 |
|
return result; |
718 |
|
} |
719 |
|
|
720 |
|
// Remove a bunch of entries with iterator |
721 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
721 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
722 |
|
if (rnd.nextBoolean()) { |
723 |
|
bs.clear(it.next()); |
724 |
|
it.remove(); |
743 |
|
} |
744 |
|
|
745 |
|
// Remove a bunch of entries with iterator |
746 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
746 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
747 |
|
if (rnd.nextBoolean()) { |
748 |
|
bs.clear(it.next()); |
749 |
|
it.remove(); |
759 |
|
try { |
760 |
|
set.add(element); |
761 |
|
fail(); |
762 |
< |
} catch(IllegalArgumentException e) { |
762 |
> |
} catch (IllegalArgumentException e) { |
763 |
|
// expected |
764 |
|
} |
765 |
|
} |
961 |
|
try { |
962 |
|
set.first(); |
963 |
|
fail(); |
964 |
< |
} catch(NoSuchElementException e) { |
964 |
> |
} catch (NoSuchElementException e) { |
965 |
|
// expected |
966 |
|
} |
967 |
|
try { |
968 |
|
set.last(); |
969 |
|
fail(); |
970 |
< |
} catch(NoSuchElementException e) { |
970 |
> |
} catch (NoSuchElementException e) { |
971 |
|
// expected |
972 |
|
} |
973 |
|
} |