1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
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); |
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 |
|
|
27 |
|
* Create a set of given size containing consecutive |
28 |
|
* Integers 0 ... n. |
29 |
|
*/ |
30 |
< |
private ConcurrentSkipListSet populatedSet(int n) { |
31 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
30 |
> |
private ConcurrentSkipListSet<Integer> populatedSet(int n) { |
31 |
> |
ConcurrentSkipListSet<Integer> q = |
32 |
> |
new ConcurrentSkipListSet<Integer>(); |
33 |
|
assertTrue(q.isEmpty()); |
34 |
|
for (int i = n-1; i >= 0; i-=2) |
35 |
|
assertTrue(q.add(new Integer(i))); |
112 |
|
* The comparator used in constructor is used |
113 |
|
*/ |
114 |
|
public void testConstructor7() { |
115 |
< |
try { |
116 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
117 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); |
118 |
< |
assertEquals(cmp, q.comparator()); |
119 |
< |
Integer[] ints = new Integer[SIZE]; |
120 |
< |
for (int i = 0; i < SIZE; ++i) |
121 |
< |
ints[i] = new Integer(i); |
122 |
< |
q.addAll(Arrays.asList(ints)); |
123 |
< |
for (int i = SIZE-1; i >= 0; --i) |
127 |
< |
assertEquals(ints[i], q.pollFirst()); |
128 |
< |
} |
129 |
< |
finally {} |
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) |
123 |
> |
assertEquals(ints[i], q.pollFirst()); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
204 |
|
shouldThrow(); |
205 |
|
} catch (NullPointerException success) {} |
206 |
|
} |
207 |
+ |
|
208 |
|
/** |
209 |
|
* addAll of a collection with null elements throws NPE |
210 |
|
*/ |
216 |
|
shouldThrow(); |
217 |
|
} catch (NullPointerException success) {} |
218 |
|
} |
219 |
+ |
|
220 |
|
/** |
221 |
|
* addAll of a collection with any null elements throws NPE after |
222 |
|
* possibly adding some elements |
236 |
|
* Set contains all elements of successful addAll |
237 |
|
*/ |
238 |
|
public void testAddAll5() { |
239 |
< |
try { |
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) |
252 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
253 |
< |
} |
254 |
< |
finally {} |
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) |
247 |
> |
assertEquals(i, q.pollFirst()); |
248 |
|
} |
249 |
|
|
250 |
|
/** |
253 |
|
public void testPollFirst() { |
254 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
255 |
|
for (int i = 0; i < SIZE; ++i) { |
256 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
256 |
> |
assertEquals(i, q.pollFirst()); |
257 |
|
} |
258 |
|
assertNull(q.pollFirst()); |
259 |
|
} |
264 |
|
public void testPollLast() { |
265 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
266 |
|
for (int i = SIZE-1; i >= 0; --i) { |
267 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
267 |
> |
assertEquals(i, q.pollLast()); |
268 |
|
} |
269 |
|
assertNull(q.pollFirst()); |
270 |
|
} |
276 |
|
public void testRemoveElement() { |
277 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
278 |
|
for (int i = 1; i < SIZE; i+=2) { |
279 |
< |
assertTrue(q.remove(new Integer(i))); |
279 |
> |
assertTrue(q.contains(i)); |
280 |
> |
assertTrue(q.remove(i)); |
281 |
> |
assertFalse(q.contains(i)); |
282 |
> |
assertTrue(q.contains(i-1)); |
283 |
|
} |
284 |
|
for (int i = 0; i < SIZE; i+=2) { |
285 |
< |
assertTrue(q.remove(new Integer(i))); |
286 |
< |
assertFalse(q.remove(new Integer(i+1))); |
285 |
> |
assertTrue(q.contains(i)); |
286 |
> |
assertTrue(q.remove(i)); |
287 |
> |
assertFalse(q.contains(i)); |
288 |
> |
assertFalse(q.remove(i+1)); |
289 |
> |
assertFalse(q.contains(i+1)); |
290 |
|
} |
291 |
|
assertTrue(q.isEmpty()); |
292 |
|
} |
384 |
|
|
385 |
|
Object e4 = q.lower(zero); |
386 |
|
assertNull(e4); |
388 |
– |
|
387 |
|
} |
388 |
|
|
389 |
|
/** |
402 |
|
|
403 |
|
Object e4 = q.higher(six); |
404 |
|
assertNull(e4); |
407 |
– |
|
405 |
|
} |
406 |
|
|
407 |
|
/** |
420 |
|
|
421 |
|
Object e4 = q.floor(zero); |
422 |
|
assertNull(e4); |
426 |
– |
|
423 |
|
} |
424 |
|
|
425 |
|
/** |
438 |
|
|
439 |
|
Object e4 = q.ceiling(six); |
440 |
|
assertNull(e4); |
445 |
– |
|
441 |
|
} |
442 |
|
|
443 |
|
/** |
444 |
< |
* toArray contains all elements |
444 |
> |
* toArray contains all elements in sorted order |
445 |
|
*/ |
446 |
|
public void testToArray() { |
447 |
|
ConcurrentSkipListSet q = populatedSet(SIZE); |
448 |
|
Object[] o = q.toArray(); |
454 |
– |
Arrays.sort(o); |
449 |
|
for (int i = 0; i < o.length; i++) |
450 |
< |
assertEquals(o[i], q.pollFirst()); |
450 |
> |
assertSame(o[i], q.pollFirst()); |
451 |
|
} |
452 |
|
|
453 |
|
/** |
454 |
< |
* toArray(a) contains all elements |
454 |
> |
* toArray(a) contains all elements in sorted order |
455 |
|
*/ |
456 |
|
public void testToArray2() { |
457 |
< |
ConcurrentSkipListSet q = populatedSet(SIZE); |
457 |
> |
ConcurrentSkipListSet<Integer> q = populatedSet(SIZE); |
458 |
|
Integer[] ints = new Integer[SIZE]; |
459 |
< |
ints = (Integer[])q.toArray(ints); |
460 |
< |
Arrays.sort(ints); |
459 |
> |
Integer[] array = q.toArray(ints); |
460 |
> |
assertSame(ints, array); |
461 |
|
for (int i = 0; i < ints.length; i++) |
462 |
< |
assertEquals(ints[i], q.pollFirst()); |
462 |
> |
assertSame(ints[i], q.pollFirst()); |
463 |
|
} |
464 |
|
|
465 |
|
/** |
493 |
|
/** |
494 |
|
* iterator.remove removes current element |
495 |
|
*/ |
496 |
< |
public void testIteratorRemove () { |
496 |
> |
public void testIteratorRemove() { |
497 |
|
final ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
498 |
|
q.add(new Integer(2)); |
499 |
|
q.add(new Integer(1)); |
664 |
|
/** |
665 |
|
* Subsets of subsets subdivide correctly |
666 |
|
*/ |
667 |
< |
public void testRecursiveSubSets() { |
668 |
< |
int setSize = 1000; |
667 |
> |
public void testRecursiveSubSets() throws Exception { |
668 |
> |
int setSize = expensiveTests ? 1000 : 100; |
669 |
|
Class cl = ConcurrentSkipListSet.class; |
670 |
|
|
671 |
|
NavigableSet<Integer> set = newSet(cl); |
683 |
|
0, setSize - 1, true); |
684 |
|
} |
685 |
|
|
686 |
< |
static NavigableSet<Integer> newSet(Class cl) { |
687 |
< |
NavigableSet<Integer> result = null; |
694 |
< |
try { |
695 |
< |
result = (NavigableSet<Integer>) cl.newInstance(); |
696 |
< |
} catch (Exception e) { |
697 |
< |
fail(); |
698 |
< |
} |
686 |
> |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
687 |
> |
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
688 |
|
assertEquals(result.size(), 0); |
689 |
|
assertFalse(result.iterator().hasNext()); |
690 |
|
return result; |
747 |
|
} else { |
748 |
|
try { |
749 |
|
set.add(element); |
750 |
< |
fail(); |
751 |
< |
} catch (IllegalArgumentException e) { |
763 |
< |
// expected |
764 |
< |
} |
750 |
> |
shouldThrow(); |
751 |
> |
} catch (IllegalArgumentException success) {} |
752 |
|
} |
753 |
|
} |
754 |
|
} |
881 |
|
// BitSet should support this! Test would run much faster |
882 |
|
while (element >= min) { |
883 |
|
if (bs.get(element)) |
884 |
< |
return(element); |
884 |
> |
return element; |
885 |
|
element--; |
886 |
|
} |
887 |
|
return -1; |
947 |
|
assertEq(rs.last(), -1); |
948 |
|
try { |
949 |
|
set.first(); |
950 |
< |
fail(); |
951 |
< |
} catch (NoSuchElementException e) { |
965 |
< |
// expected |
966 |
< |
} |
950 |
> |
shouldThrow(); |
951 |
> |
} catch (NoSuchElementException success) {} |
952 |
|
try { |
953 |
|
set.last(); |
954 |
< |
fail(); |
955 |
< |
} catch (NoSuchElementException e) { |
971 |
< |
// expected |
972 |
< |
} |
954 |
> |
shouldThrow(); |
955 |
> |
} catch (NoSuchElementException success) {} |
956 |
|
} |
957 |
|
} |
958 |
|
|