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.*; |
7 |
> |
import java.util.Arrays; |
8 |
> |
import java.util.Comparator; |
9 |
> |
import java.util.Iterator; |
10 |
> |
import java.util.NavigableSet; |
11 |
> |
import java.util.SortedSet; |
12 |
> |
import java.util.concurrent.ConcurrentSkipListSet; |
13 |
> |
|
14 |
> |
import junit.framework.Test; |
15 |
> |
import junit.framework.TestSuite; |
16 |
|
|
17 |
|
public class ConcurrentSkipListSubSetTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
29 |
|
} |
30 |
|
|
31 |
|
/** |
32 |
< |
* Create a set of given size containing consecutive |
32 |
> |
* Returns a new set of given size containing consecutive |
33 |
|
* Integers 0 ... n. |
34 |
|
*/ |
35 |
< |
private NavigableSet populatedSet(int n) { |
36 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
35 |
> |
private NavigableSet<Integer> populatedSet(int n) { |
36 |
> |
ConcurrentSkipListSet<Integer> q = |
37 |
> |
new ConcurrentSkipListSet<Integer>(); |
38 |
|
assertTrue(q.isEmpty()); |
39 |
|
|
40 |
|
for (int i = n-1; i >= 0; i-=2) |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
< |
* Create set of first 5 ints |
53 |
> |
* Returns a new set of first 5 ints. |
54 |
|
*/ |
55 |
|
private NavigableSet set5() { |
56 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* Create set of first 5 negative ints |
71 |
> |
* Returns a new set of first 5 negative ints. |
72 |
|
*/ |
73 |
|
private NavigableSet dset5() { |
74 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
102 |
|
assertEquals(0, set0().size()); |
103 |
|
} |
104 |
|
|
99 |
– |
|
105 |
|
/** |
106 |
|
* isEmpty is true before add, false after |
107 |
|
*/ |
172 |
|
} catch (ClassCastException success) {} |
173 |
|
} |
174 |
|
|
170 |
– |
|
175 |
|
/** |
176 |
|
* addAll(null) throws NPE |
177 |
|
*/ |
242 |
|
public void testRemoveElement() { |
243 |
|
NavigableSet q = populatedSet(SIZE); |
244 |
|
for (int i = 1; i < SIZE; i+=2) { |
245 |
< |
assertTrue(q.remove(new Integer(i))); |
245 |
> |
assertTrue(q.contains(i)); |
246 |
> |
assertTrue(q.remove(i)); |
247 |
> |
assertFalse(q.contains(i)); |
248 |
> |
assertTrue(q.contains(i-1)); |
249 |
|
} |
250 |
|
for (int i = 0; i < SIZE; i+=2) { |
251 |
< |
assertTrue(q.remove(new Integer(i))); |
252 |
< |
assertFalse(q.remove(new Integer(i+1))); |
251 |
> |
assertTrue(q.contains(i)); |
252 |
> |
assertTrue(q.remove(i)); |
253 |
> |
assertFalse(q.contains(i)); |
254 |
> |
assertFalse(q.remove(i+1)); |
255 |
> |
assertFalse(q.contains(i+1)); |
256 |
|
} |
257 |
|
assertTrue(q.isEmpty()); |
258 |
|
} |
332 |
|
} |
333 |
|
} |
334 |
|
|
325 |
– |
|
326 |
– |
|
335 |
|
/** |
336 |
|
* lower returns preceding element |
337 |
|
*/ |
418 |
|
* toArray(a) contains all elements in sorted order |
419 |
|
*/ |
420 |
|
public void testToArray2() { |
421 |
< |
NavigableSet q = populatedSet(SIZE); |
421 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
422 |
|
Integer[] ints = new Integer[SIZE]; |
423 |
< |
assertSame(ints, q.toArray(ints)); |
423 |
> |
Integer[] array = q.toArray(ints); |
424 |
> |
assertSame(ints, array); |
425 |
|
for (int i = 0; i < ints.length; i++) |
426 |
|
assertSame(ints[i], q.pollFirst()); |
427 |
|
} |
451 |
|
assertTrue(q.contains(it.next())); |
452 |
|
++i; |
453 |
|
} |
454 |
< |
assertEquals(i, 0); |
454 |
> |
assertEquals(0, i); |
455 |
|
} |
456 |
|
|
457 |
|
/** |
473 |
|
assertFalse(it.hasNext()); |
474 |
|
} |
475 |
|
|
467 |
– |
|
476 |
|
/** |
477 |
|
* toString contains toStrings of elements |
478 |
|
*/ |
480 |
|
NavigableSet q = populatedSet(SIZE); |
481 |
|
String s = q.toString(); |
482 |
|
for (int i = 0; i < SIZE; ++i) { |
483 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
483 |
> |
assertTrue(s.contains(String.valueOf(i))); |
484 |
|
} |
485 |
|
} |
486 |
|
|
488 |
|
* A deserialized serialized set has same elements |
489 |
|
*/ |
490 |
|
public void testSerialization() throws Exception { |
491 |
< |
NavigableSet q = populatedSet(SIZE); |
492 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
493 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
494 |
< |
out.writeObject(q); |
495 |
< |
out.close(); |
496 |
< |
|
497 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
498 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
499 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
500 |
< |
assertEquals(q.size(), r.size()); |
501 |
< |
while (!q.isEmpty()) |
502 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
491 |
> |
NavigableSet x = populatedSet(SIZE); |
492 |
> |
NavigableSet y = serialClone(x); |
493 |
> |
|
494 |
> |
assertNotSame(y, x); |
495 |
> |
assertEquals(x.size(), y.size()); |
496 |
> |
assertEquals(x, y); |
497 |
> |
assertEquals(y, x); |
498 |
> |
while (!x.isEmpty()) { |
499 |
> |
assertFalse(y.isEmpty()); |
500 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
501 |
> |
} |
502 |
> |
assertTrue(y.isEmpty()); |
503 |
|
} |
504 |
|
|
505 |
|
/** |
677 |
|
} catch (ClassCastException success) {} |
678 |
|
} |
679 |
|
|
672 |
– |
|
680 |
|
/** |
681 |
|
* addAll(null) throws NPE |
682 |
|
*/ |
831 |
|
} |
832 |
|
} |
833 |
|
|
827 |
– |
|
828 |
– |
|
834 |
|
/** |
835 |
|
* lower returns preceding element |
836 |
|
*/ |
951 |
|
assertTrue(q.contains(it.next())); |
952 |
|
++i; |
953 |
|
} |
954 |
< |
assertEquals(i, 0); |
954 |
> |
assertEquals(0, i); |
955 |
|
} |
956 |
|
|
957 |
|
/** |
973 |
|
assertFalse(it.hasNext()); |
974 |
|
} |
975 |
|
|
971 |
– |
|
976 |
|
/** |
977 |
|
* toString contains toStrings of elements |
978 |
|
*/ |
980 |
|
NavigableSet q = populatedSet(SIZE); |
981 |
|
String s = q.toString(); |
982 |
|
for (int i = 0; i < SIZE; ++i) { |
983 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
983 |
> |
assertTrue(s.contains(String.valueOf(i))); |
984 |
|
} |
985 |
|
} |
986 |
|
|
988 |
|
* A deserialized serialized set has same elements |
989 |
|
*/ |
990 |
|
public void testDescendingSerialization() throws Exception { |
991 |
< |
NavigableSet q = populatedSet(SIZE); |
992 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
993 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
994 |
< |
out.writeObject(q); |
995 |
< |
out.close(); |
996 |
< |
|
997 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
998 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
999 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
1000 |
< |
assertEquals(q.size(), r.size()); |
1001 |
< |
while (!q.isEmpty()) |
1002 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
991 |
> |
NavigableSet x = dset5(); |
992 |
> |
NavigableSet y = serialClone(x); |
993 |
> |
|
994 |
> |
assertNotSame(y, x); |
995 |
> |
assertEquals(x.size(), y.size()); |
996 |
> |
assertEquals(x, y); |
997 |
> |
assertEquals(y, x); |
998 |
> |
while (!x.isEmpty()) { |
999 |
> |
assertFalse(y.isEmpty()); |
1000 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
1001 |
> |
} |
1002 |
> |
assertTrue(y.isEmpty()); |
1003 |
|
} |
1004 |
|
|
1005 |
|
/** |