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.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
10 |
< |
import java.io.*; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.BitSet; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Comparator; |
12 |
> |
import java.util.Iterator; |
13 |
> |
import java.util.NavigableSet; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.Random; |
16 |
> |
import java.util.Set; |
17 |
> |
import java.util.SortedSet; |
18 |
> |
import java.util.concurrent.ConcurrentSkipListSet; |
19 |
|
|
20 |
|
public class ConcurrentSkipListSubSetTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
32 |
|
} |
33 |
|
|
34 |
|
/** |
35 |
< |
* Create a set of given size containing consecutive |
35 |
> |
* Returns a new set of given size containing consecutive |
36 |
|
* Integers 0 ... n. |
37 |
|
*/ |
38 |
< |
private NavigableSet populatedSet(int n) { |
39 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
38 |
> |
private NavigableSet<Integer> populatedSet(int n) { |
39 |
> |
ConcurrentSkipListSet<Integer> q = |
40 |
> |
new ConcurrentSkipListSet<Integer>(); |
41 |
|
assertTrue(q.isEmpty()); |
42 |
|
|
43 |
|
for (int i = n-1; i >= 0; i-=2) |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
< |
* Create set of first 5 ints |
56 |
> |
* Returns a new set of first 5 ints. |
57 |
|
*/ |
58 |
|
private NavigableSet set5() { |
59 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
71 |
|
} |
72 |
|
|
73 |
|
/** |
74 |
< |
* Create set of first 5 negative ints |
74 |
> |
* Returns a new set of first 5 negative ints. |
75 |
|
*/ |
76 |
|
private NavigableSet dset5() { |
77 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
105 |
|
assertEquals(0, set0().size()); |
106 |
|
} |
107 |
|
|
99 |
– |
|
108 |
|
/** |
109 |
|
* isEmpty is true before add, false after |
110 |
|
*/ |
175 |
|
} catch (ClassCastException success) {} |
176 |
|
} |
177 |
|
|
170 |
– |
|
178 |
|
/** |
179 |
|
* addAll(null) throws NPE |
180 |
|
*/ |
185 |
|
shouldThrow(); |
186 |
|
} catch (NullPointerException success) {} |
187 |
|
} |
188 |
+ |
|
189 |
|
/** |
190 |
|
* addAll of a collection with null elements throws NPE |
191 |
|
*/ |
197 |
|
shouldThrow(); |
198 |
|
} catch (NullPointerException success) {} |
199 |
|
} |
200 |
+ |
|
201 |
|
/** |
202 |
|
* addAll of a collection with any null elements throws NPE after |
203 |
|
* possibly adding some elements |
245 |
|
public void testRemoveElement() { |
246 |
|
NavigableSet q = populatedSet(SIZE); |
247 |
|
for (int i = 1; i < SIZE; i+=2) { |
248 |
< |
assertTrue(q.remove(new Integer(i))); |
248 |
> |
assertTrue(q.contains(i)); |
249 |
> |
assertTrue(q.remove(i)); |
250 |
> |
assertFalse(q.contains(i)); |
251 |
> |
assertTrue(q.contains(i-1)); |
252 |
|
} |
253 |
|
for (int i = 0; i < SIZE; i+=2) { |
254 |
< |
assertTrue(q.remove(new Integer(i))); |
255 |
< |
assertFalse(q.remove(new Integer(i+1))); |
254 |
> |
assertTrue(q.contains(i)); |
255 |
> |
assertTrue(q.remove(i)); |
256 |
> |
assertFalse(q.contains(i)); |
257 |
> |
assertFalse(q.remove(i+1)); |
258 |
> |
assertFalse(q.contains(i+1)); |
259 |
|
} |
260 |
|
assertTrue(q.isEmpty()); |
261 |
|
} |
335 |
|
} |
336 |
|
} |
337 |
|
|
323 |
– |
|
324 |
– |
|
338 |
|
/** |
339 |
|
* lower returns preceding element |
340 |
|
*/ |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
< |
* toArray contains all elements |
411 |
> |
* toArray contains all elements in sorted order |
412 |
|
*/ |
413 |
|
public void testToArray() { |
414 |
|
NavigableSet q = populatedSet(SIZE); |
415 |
|
Object[] o = q.toArray(); |
403 |
– |
Arrays.sort(o); |
416 |
|
for (int i = 0; i < o.length; i++) |
417 |
< |
assertEquals(o[i], q.pollFirst()); |
417 |
> |
assertSame(o[i], q.pollFirst()); |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* toArray(a) contains all elements |
421 |
> |
* toArray(a) contains all elements in sorted order |
422 |
|
*/ |
423 |
|
public void testToArray2() { |
424 |
< |
NavigableSet q = populatedSet(SIZE); |
424 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
425 |
|
Integer[] ints = new Integer[SIZE]; |
426 |
< |
ints = (Integer[])q.toArray(ints); |
427 |
< |
Arrays.sort(ints); |
426 |
> |
Integer[] array = q.toArray(ints); |
427 |
> |
assertSame(ints, array); |
428 |
|
for (int i = 0; i < ints.length; i++) |
429 |
< |
assertEquals(ints[i], q.pollFirst()); |
429 |
> |
assertSame(ints[i], q.pollFirst()); |
430 |
|
} |
431 |
|
|
432 |
|
/** |
454 |
|
assertTrue(q.contains(it.next())); |
455 |
|
++i; |
456 |
|
} |
457 |
< |
assertEquals(i, 0); |
457 |
> |
assertEquals(0, i); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
476 |
|
assertFalse(it.hasNext()); |
477 |
|
} |
478 |
|
|
467 |
– |
|
479 |
|
/** |
480 |
|
* toString contains toStrings of elements |
481 |
|
*/ |
483 |
|
NavigableSet q = populatedSet(SIZE); |
484 |
|
String s = q.toString(); |
485 |
|
for (int i = 0; i < SIZE; ++i) { |
486 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
486 |
> |
assertTrue(s.contains(String.valueOf(i))); |
487 |
|
} |
488 |
|
} |
489 |
|
|
491 |
|
* A deserialized serialized set has same elements |
492 |
|
*/ |
493 |
|
public void testSerialization() throws Exception { |
494 |
< |
NavigableSet q = populatedSet(SIZE); |
495 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
496 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
497 |
< |
out.writeObject(q); |
498 |
< |
out.close(); |
499 |
< |
|
500 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
501 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
502 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
503 |
< |
assertEquals(q.size(), r.size()); |
504 |
< |
while (!q.isEmpty()) |
505 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
494 |
> |
NavigableSet x = populatedSet(SIZE); |
495 |
> |
NavigableSet y = serialClone(x); |
496 |
> |
|
497 |
> |
assertNotSame(y, x); |
498 |
> |
assertEquals(x.size(), y.size()); |
499 |
> |
assertEquals(x, y); |
500 |
> |
assertEquals(y, x); |
501 |
> |
while (!x.isEmpty()) { |
502 |
> |
assertFalse(y.isEmpty()); |
503 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
504 |
> |
} |
505 |
> |
assertTrue(y.isEmpty()); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
680 |
|
} catch (ClassCastException success) {} |
681 |
|
} |
682 |
|
|
672 |
– |
|
683 |
|
/** |
684 |
|
* addAll(null) throws NPE |
685 |
|
*/ |
690 |
|
shouldThrow(); |
691 |
|
} catch (NullPointerException success) {} |
692 |
|
} |
693 |
+ |
|
694 |
|
/** |
695 |
|
* addAll of a collection with null elements throws NPE |
696 |
|
*/ |
702 |
|
shouldThrow(); |
703 |
|
} catch (NullPointerException success) {} |
704 |
|
} |
705 |
+ |
|
706 |
|
/** |
707 |
|
* addAll of a collection with any null elements throws NPE after |
708 |
|
* possibly adding some elements |
834 |
|
} |
835 |
|
} |
836 |
|
|
825 |
– |
|
826 |
– |
|
837 |
|
/** |
838 |
|
* lower returns preceding element |
839 |
|
*/ |
923 |
|
public void testDescendingToArray2() { |
924 |
|
NavigableSet q = populatedSet(SIZE); |
925 |
|
Integer[] ints = new Integer[SIZE]; |
926 |
< |
ints = (Integer[])q.toArray(ints); |
926 |
> |
assertSame(ints, q.toArray(ints)); |
927 |
|
Arrays.sort(ints); |
928 |
|
for (int i = 0; i < ints.length; i++) |
929 |
|
assertEquals(ints[i], q.pollFirst()); |
954 |
|
assertTrue(q.contains(it.next())); |
955 |
|
++i; |
956 |
|
} |
957 |
< |
assertEquals(i, 0); |
957 |
> |
assertEquals(0, i); |
958 |
|
} |
959 |
|
|
960 |
|
/** |
976 |
|
assertFalse(it.hasNext()); |
977 |
|
} |
978 |
|
|
969 |
– |
|
979 |
|
/** |
980 |
|
* toString contains toStrings of elements |
981 |
|
*/ |
983 |
|
NavigableSet q = populatedSet(SIZE); |
984 |
|
String s = q.toString(); |
985 |
|
for (int i = 0; i < SIZE; ++i) { |
986 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
986 |
> |
assertTrue(s.contains(String.valueOf(i))); |
987 |
|
} |
988 |
|
} |
989 |
|
|
991 |
|
* A deserialized serialized set has same elements |
992 |
|
*/ |
993 |
|
public void testDescendingSerialization() throws Exception { |
994 |
< |
NavigableSet q = populatedSet(SIZE); |
995 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
996 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
997 |
< |
out.writeObject(q); |
998 |
< |
out.close(); |
999 |
< |
|
1000 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1001 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1002 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
1003 |
< |
assertEquals(q.size(), r.size()); |
1004 |
< |
while (!q.isEmpty()) |
1005 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
994 |
> |
NavigableSet x = dset5(); |
995 |
> |
NavigableSet y = serialClone(x); |
996 |
> |
|
997 |
> |
assertNotSame(y, x); |
998 |
> |
assertEquals(x.size(), y.size()); |
999 |
> |
assertEquals(x, y); |
1000 |
> |
assertEquals(y, x); |
1001 |
> |
while (!x.isEmpty()) { |
1002 |
> |
assertFalse(y.isEmpty()); |
1003 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
1004 |
> |
} |
1005 |
> |
assertTrue(y.isEmpty()); |
1006 |
|
} |
1007 |
|
|
1008 |
|
/** |