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 ConcurrentSkipListSubSetTest 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(ConcurrentSkipListSubSetTest.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 NavigableSet populatedSet(int n) { |
31 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
30 |
> |
private NavigableSet<Integer> populatedSet(int n) { |
31 |
> |
ConcurrentSkipListSet<Integer> q = |
32 |
> |
new ConcurrentSkipListSet<Integer>(); |
33 |
|
assertTrue(q.isEmpty()); |
34 |
|
|
35 |
|
for (int i = n-1; i >= 0; i-=2) |
97 |
|
assertEquals(0, set0().size()); |
98 |
|
} |
99 |
|
|
103 |
– |
|
100 |
|
/** |
101 |
|
* isEmpty is true before add, false after |
102 |
|
*/ |
167 |
|
} catch (ClassCastException success) {} |
168 |
|
} |
169 |
|
|
174 |
– |
|
170 |
|
/** |
171 |
|
* addAll(null) throws NPE |
172 |
|
*/ |
177 |
|
shouldThrow(); |
178 |
|
} catch (NullPointerException success) {} |
179 |
|
} |
180 |
+ |
|
181 |
|
/** |
182 |
|
* addAll of a collection with null elements throws NPE |
183 |
|
*/ |
189 |
|
shouldThrow(); |
190 |
|
} catch (NullPointerException success) {} |
191 |
|
} |
192 |
+ |
|
193 |
|
/** |
194 |
|
* addAll of a collection with any null elements throws NPE after |
195 |
|
* possibly adding some elements |
226 |
|
public void testPoll() { |
227 |
|
NavigableSet q = populatedSet(SIZE); |
228 |
|
for (int i = 0; i < SIZE; ++i) { |
229 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
229 |
> |
assertEquals(i, q.pollFirst()); |
230 |
|
} |
231 |
|
assertNull(q.pollFirst()); |
232 |
|
} |
237 |
|
public void testRemoveElement() { |
238 |
|
NavigableSet q = populatedSet(SIZE); |
239 |
|
for (int i = 1; i < SIZE; i+=2) { |
240 |
< |
assertTrue(q.remove(new Integer(i))); |
240 |
> |
assertTrue(q.contains(i)); |
241 |
> |
assertTrue(q.remove(i)); |
242 |
> |
assertFalse(q.contains(i)); |
243 |
> |
assertTrue(q.contains(i-1)); |
244 |
|
} |
245 |
|
for (int i = 0; i < SIZE; i+=2) { |
246 |
< |
assertTrue(q.remove(new Integer(i))); |
247 |
< |
assertFalse(q.remove(new Integer(i+1))); |
246 |
> |
assertTrue(q.contains(i)); |
247 |
> |
assertTrue(q.remove(i)); |
248 |
> |
assertFalse(q.contains(i)); |
249 |
> |
assertFalse(q.remove(i+1)); |
250 |
> |
assertFalse(q.contains(i+1)); |
251 |
|
} |
252 |
|
assertTrue(q.isEmpty()); |
253 |
|
} |
327 |
|
} |
328 |
|
} |
329 |
|
|
327 |
– |
|
328 |
– |
|
330 |
|
/** |
331 |
|
* lower returns preceding element |
332 |
|
*/ |
343 |
|
|
344 |
|
Object e4 = q.lower(zero); |
345 |
|
assertNull(e4); |
345 |
– |
|
346 |
|
} |
347 |
|
|
348 |
|
/** |
361 |
|
|
362 |
|
Object e4 = q.higher(six); |
363 |
|
assertNull(e4); |
364 |
– |
|
364 |
|
} |
365 |
|
|
366 |
|
/** |
379 |
|
|
380 |
|
Object e4 = q.floor(zero); |
381 |
|
assertNull(e4); |
383 |
– |
|
382 |
|
} |
383 |
|
|
384 |
|
/** |
397 |
|
|
398 |
|
Object e4 = q.ceiling(six); |
399 |
|
assertNull(e4); |
402 |
– |
|
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
< |
* toArray contains all elements |
403 |
> |
* toArray contains all elements in sorted order |
404 |
|
*/ |
405 |
|
public void testToArray() { |
406 |
|
NavigableSet q = populatedSet(SIZE); |
407 |
|
Object[] o = q.toArray(); |
411 |
– |
Arrays.sort(o); |
408 |
|
for (int i = 0; i < o.length; i++) |
409 |
< |
assertEquals(o[i], q.pollFirst()); |
409 |
> |
assertSame(o[i], q.pollFirst()); |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
< |
* toArray(a) contains all elements |
413 |
> |
* toArray(a) contains all elements in sorted order |
414 |
|
*/ |
415 |
|
public void testToArray2() { |
416 |
< |
NavigableSet q = populatedSet(SIZE); |
416 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
417 |
|
Integer[] ints = new Integer[SIZE]; |
418 |
< |
ints = (Integer[])q.toArray(ints); |
419 |
< |
Arrays.sort(ints); |
418 |
> |
Integer[] array = q.toArray(ints); |
419 |
> |
assertSame(ints, array); |
420 |
|
for (int i = 0; i < ints.length; i++) |
421 |
< |
assertEquals(ints[i], q.pollFirst()); |
421 |
> |
assertSame(ints[i], q.pollFirst()); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
452 |
|
/** |
453 |
|
* iterator.remove removes current element |
454 |
|
*/ |
455 |
< |
public void testIteratorRemove () { |
455 |
> |
public void testIteratorRemove() { |
456 |
|
final NavigableSet q = set0(); |
457 |
|
q.add(new Integer(2)); |
458 |
|
q.add(new Integer(1)); |
468 |
|
assertFalse(it.hasNext()); |
469 |
|
} |
470 |
|
|
475 |
– |
|
471 |
|
/** |
472 |
|
* toString contains toStrings of elements |
473 |
|
*/ |
475 |
|
NavigableSet q = populatedSet(SIZE); |
476 |
|
String s = q.toString(); |
477 |
|
for (int i = 0; i < SIZE; ++i) { |
478 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
478 |
> |
assertTrue(s.contains(String.valueOf(i))); |
479 |
|
} |
480 |
|
} |
481 |
|
|
672 |
|
} catch (ClassCastException success) {} |
673 |
|
} |
674 |
|
|
680 |
– |
|
675 |
|
/** |
676 |
|
* addAll(null) throws NPE |
677 |
|
*/ |
682 |
|
shouldThrow(); |
683 |
|
} catch (NullPointerException success) {} |
684 |
|
} |
685 |
+ |
|
686 |
|
/** |
687 |
|
* addAll of a collection with null elements throws NPE |
688 |
|
*/ |
694 |
|
shouldThrow(); |
695 |
|
} catch (NullPointerException success) {} |
696 |
|
} |
697 |
+ |
|
698 |
|
/** |
699 |
|
* addAll of a collection with any null elements throws NPE after |
700 |
|
* possibly adding some elements |
731 |
|
public void testDescendingPoll() { |
732 |
|
NavigableSet q = populatedSet(SIZE); |
733 |
|
for (int i = 0; i < SIZE; ++i) { |
734 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
734 |
> |
assertEquals(i, q.pollFirst()); |
735 |
|
} |
736 |
|
assertNull(q.pollFirst()); |
737 |
|
} |
826 |
|
} |
827 |
|
} |
828 |
|
|
833 |
– |
|
834 |
– |
|
829 |
|
/** |
830 |
|
* lower returns preceding element |
831 |
|
*/ |
842 |
|
|
843 |
|
Object e4 = q.lower(zero); |
844 |
|
assertNull(e4); |
851 |
– |
|
845 |
|
} |
846 |
|
|
847 |
|
/** |
860 |
|
|
861 |
|
Object e4 = q.higher(m6); |
862 |
|
assertNull(e4); |
870 |
– |
|
863 |
|
} |
864 |
|
|
865 |
|
/** |
878 |
|
|
879 |
|
Object e4 = q.floor(zero); |
880 |
|
assertNull(e4); |
889 |
– |
|
881 |
|
} |
882 |
|
|
883 |
|
/** |
896 |
|
|
897 |
|
Object e4 = q.ceiling(m6); |
898 |
|
assertNull(e4); |
908 |
– |
|
899 |
|
} |
900 |
|
|
901 |
|
/** |
915 |
|
public void testDescendingToArray2() { |
916 |
|
NavigableSet q = populatedSet(SIZE); |
917 |
|
Integer[] ints = new Integer[SIZE]; |
918 |
< |
ints = (Integer[])q.toArray(ints); |
918 |
> |
assertSame(ints, q.toArray(ints)); |
919 |
|
Arrays.sort(ints); |
920 |
|
for (int i = 0; i < ints.length; i++) |
921 |
|
assertEquals(ints[i], q.pollFirst()); |
952 |
|
/** |
953 |
|
* iterator.remove removes current element |
954 |
|
*/ |
955 |
< |
public void testDescendingIteratorRemove () { |
955 |
> |
public void testDescendingIteratorRemove() { |
956 |
|
final NavigableSet q = dset0(); |
957 |
|
q.add(new Integer(2)); |
958 |
|
q.add(new Integer(1)); |
968 |
|
assertFalse(it.hasNext()); |
969 |
|
} |
970 |
|
|
981 |
– |
|
971 |
|
/** |
972 |
|
* toString contains toStrings of elements |
973 |
|
*/ |
975 |
|
NavigableSet q = populatedSet(SIZE); |
976 |
|
String s = q.toString(); |
977 |
|
for (int i = 0; i < SIZE; ++i) { |
978 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
978 |
> |
assertTrue(s.contains(String.valueOf(i))); |
979 |
|
} |
980 |
|
} |
981 |
|
|