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); |
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) |
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) |
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()); |
57 |
|
assertEquals(5, q.size()); |
58 |
|
return q; |
59 |
|
} |
60 |
< |
|
60 |
> |
|
61 |
|
/** |
62 |
|
* A new set has unbounded capacity |
63 |
|
*/ |
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 |
|
/** |
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 |
462 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
463 |
|
Object[] o = q.toArray(); |
464 |
|
Arrays.sort(o); |
465 |
< |
for(int i = 0; i < o.length; i++) |
465 |
> |
for (int i = 0; i < o.length; i++) |
466 |
|
assertEquals(o[i], q.pollFirst()); |
467 |
|
} |
468 |
|
|
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 |
|
*/ |
485 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
486 |
|
int i = 0; |
487 |
|
Iterator it = q.iterator(); |
488 |
< |
while(it.hasNext()) { |
488 |
> |
while (it.hasNext()) { |
489 |
|
assertTrue(q.contains(it.next())); |
490 |
|
++i; |
491 |
|
} |
499 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
500 |
|
int i = 0; |
501 |
|
Iterator it = q.iterator(); |
502 |
< |
while(it.hasNext()) { |
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 |
|
} |
708 |
|
NavigableSet<Integer> result = null; |
709 |
|
try { |
710 |
|
result = (NavigableSet<Integer>) cl.newInstance(); |
711 |
< |
} catch(Exception e) { |
711 |
> |
} catch (Exception e) { |
712 |
|
fail(); |
713 |
|
} |
714 |
|
assertEquals(result.size(), 0); |
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 |
|
} |