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 |
|
*/ |
168 |
|
* add(null) throws NPE |
169 |
|
*/ |
170 |
|
public void testAddNull() { |
171 |
< |
try { |
171 |
> |
try { |
172 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
173 |
|
q.add(null); |
174 |
|
shouldThrow(); |
175 |
< |
} catch (NullPointerException success) { } |
175 |
> |
} catch (NullPointerException success) { } |
176 |
|
} |
177 |
|
|
178 |
|
/** |
204 |
|
q.add(new Object()); |
205 |
|
shouldThrow(); |
206 |
|
} |
207 |
< |
catch(ClassCastException success) {} |
207 |
> |
catch (ClassCastException success) {} |
208 |
|
} |
209 |
|
|
210 |
|
/** |
272 |
|
for (int i = 0; i < SIZE; ++i) { |
273 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
274 |
|
} |
275 |
< |
assertNull(q.pollFirst()); |
275 |
> |
assertNull(q.pollFirst()); |
276 |
|
} |
277 |
|
|
278 |
|
/** |
283 |
|
for (int i = SIZE-1; i >= 0; --i) { |
284 |
|
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
285 |
|
} |
286 |
< |
assertNull(q.pollFirst()); |
286 |
> |
assertNull(q.pollFirst()); |
287 |
|
} |
288 |
|
|
289 |
|
|
301 |
|
} |
302 |
|
assertTrue(q.isEmpty()); |
303 |
|
} |
304 |
< |
|
304 |
> |
|
305 |
|
/** |
306 |
|
* contains(x) reports true when elements added but not yet removed |
307 |
|
*/ |
377 |
|
} |
378 |
|
} |
379 |
|
|
380 |
< |
|
380 |
> |
|
381 |
|
|
382 |
|
/** |
383 |
|
* lower returns preceding element |
460 |
|
*/ |
461 |
|
public void testToArray() { |
462 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
463 |
< |
Object[] o = q.toArray(); |
463 |
> |
Object[] o = q.toArray(); |
464 |
|
Arrays.sort(o); |
465 |
< |
for(int i = 0; i < o.length; i++) |
466 |
< |
assertEquals(o[i], q.pollFirst()); |
465 |
> |
for (int i = 0; i < o.length; i++) |
466 |
> |
assertEquals(o[i], q.pollFirst()); |
467 |
|
} |
468 |
|
|
469 |
|
/** |
471 |
|
*/ |
472 |
|
public void testToArray2() { |
473 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
474 |
< |
Integer[] ints = new Integer[SIZE]; |
475 |
< |
ints = (Integer[])q.toArray(ints); |
474 |
> |
Integer[] ints = new Integer[SIZE]; |
475 |
> |
ints = (Integer[])q.toArray(ints); |
476 |
|
Arrays.sort(ints); |
477 |
< |
for(int i = 0; i < ints.length; i++) |
477 |
> |
for (int i = 0; i < ints.length; i++) |
478 |
|
assertEquals(ints[i], q.pollFirst()); |
479 |
|
} |
480 |
< |
|
480 |
> |
|
481 |
|
/** |
482 |
|
* iterator iterates through all elements |
483 |
|
*/ |
484 |
|
public void testIterator() { |
485 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
486 |
|
int i = 0; |
487 |
< |
Iterator it = q.iterator(); |
488 |
< |
while(it.hasNext()) { |
487 |
> |
Iterator it = q.iterator(); |
488 |
> |
while (it.hasNext()) { |
489 |
|
assertTrue(q.contains(it.next())); |
490 |
|
++i; |
491 |
|
} |
498 |
|
public void testEmptyIterator() { |
499 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
500 |
|
int i = 0; |
501 |
< |
Iterator it = q.iterator(); |
502 |
< |
while(it.hasNext()) { |
501 |
> |
Iterator it = q.iterator(); |
502 |
> |
while (it.hasNext()) { |
503 |
|
assertTrue(q.contains(it.next())); |
504 |
|
++i; |
505 |
|
} |
535 |
|
for (int i = 0; i < SIZE; ++i) { |
536 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
537 |
|
} |
538 |
< |
} |
538 |
> |
} |
539 |
|
|
540 |
|
/** |
541 |
< |
* A deserialized serialized set has same elements |
541 |
> |
* A deserialized serialized set has same elements |
542 |
|
*/ |
543 |
|
public void testSerialization() { |
544 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
552 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
553 |
|
ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); |
554 |
|
assertEquals(q.size(), r.size()); |
555 |
< |
while (!q.isEmpty()) |
555 |
> |
while (!q.isEmpty()) |
556 |
|
assertEquals(q.pollFirst(), r.pollFirst()); |
557 |
< |
} catch(Exception e){ |
557 |
> |
} catch (Exception e) { |
558 |
|
e.printStackTrace(); |
559 |
|
unexpectedException(); |
560 |
|
} |
686 |
|
* Subsets of subsets subdivide correctly |
687 |
|
*/ |
688 |
|
public void testRecursiveSubSets() { |
689 |
< |
int setSize = 1000; |
690 |
< |
Class cl = ConcurrentSkipListSet.class; |
689 |
> |
int setSize = 1000; |
690 |
> |
Class cl = ConcurrentSkipListSet.class; |
691 |
|
|
692 |
|
NavigableSet<Integer> set = newSet(cl); |
693 |
|
bs = new BitSet(setSize); |
706 |
|
|
707 |
|
static NavigableSet<Integer> newSet(Class cl) { |
708 |
|
NavigableSet<Integer> result = null; |
709 |
< |
try { |
709 |
> |
try { |
710 |
|
result = (NavigableSet<Integer>) cl.newInstance(); |
711 |
< |
} catch(Exception e) { |
711 |
> |
} catch (Exception e) { |
712 |
|
fail(); |
713 |
< |
} |
713 |
> |
} |
714 |
|
assertEquals(result.size(), 0); |
715 |
|
assertFalse(result.iterator().hasNext()); |
716 |
|
return result; |
733 |
|
} |
734 |
|
|
735 |
|
// Remove a bunch of entries with iterator |
736 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
736 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
737 |
|
if (rnd.nextBoolean()) { |
738 |
|
bs.clear(it.next()); |
739 |
|
it.remove(); |
758 |
|
} |
759 |
|
|
760 |
|
// Remove a bunch of entries with iterator |
761 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
761 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
762 |
|
if (rnd.nextBoolean()) { |
763 |
|
bs.clear(it.next()); |
764 |
|
it.remove(); |
774 |
|
try { |
775 |
|
set.add(element); |
776 |
|
fail(); |
777 |
< |
} catch(IllegalArgumentException e) { |
777 |
> |
} catch (IllegalArgumentException e) { |
778 |
|
// expected |
779 |
|
} |
780 |
|
} |
976 |
|
try { |
977 |
|
set.first(); |
978 |
|
fail(); |
979 |
< |
} catch(NoSuchElementException e) { |
979 |
> |
} catch (NoSuchElementException e) { |
980 |
|
// expected |
981 |
|
} |
982 |
|
try { |
983 |
|
set.last(); |
984 |
|
fail(); |
985 |
< |
} catch(NoSuchElementException e) { |
985 |
> |
} catch (NoSuchElementException e) { |
986 |
|
// expected |
987 |
|
} |
988 |
|
} |