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) |
40 |
> |
for (int i = n-1; i >= 0; i -= 2) |
41 |
|
assertTrue(q.add(new Integer(i))); |
42 |
< |
for (int i = (n & 1); i < n; i+=2) |
42 |
> |
for (int i = (n & 1); i < n; i += 2) |
43 |
|
assertTrue(q.add(new Integer(i))); |
44 |
|
assertTrue(q.add(new Integer(-n))); |
45 |
|
assertTrue(q.add(new Integer(n))); |
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 |
|
*/ |
241 |
|
*/ |
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))); |
246 |
< |
} |
247 |
< |
for (int i = 0; i < SIZE; i+=2) { |
248 |
< |
assertTrue(q.remove(new Integer(i))); |
249 |
< |
assertFalse(q.remove(new Integer(i+1))); |
244 |
> |
for (int i = 1; i < SIZE; i += 2) { |
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.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 |
|
} |
326 |
|
assertTrue(q.removeAll(p)); |
327 |
|
assertEquals(SIZE-i, q.size()); |
328 |
|
for (int j = 0; j < i; ++j) { |
329 |
< |
Integer I = (Integer)(p.pollFirst()); |
330 |
< |
assertFalse(q.contains(I)); |
329 |
> |
Integer x = (Integer)(p.pollFirst()); |
330 |
> |
assertFalse(q.contains(x)); |
331 |
|
} |
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 |
|
} |
431 |
|
*/ |
432 |
|
public void testIterator() { |
433 |
|
NavigableSet q = populatedSet(SIZE); |
425 |
– |
int i = 0; |
434 |
|
Iterator it = q.iterator(); |
435 |
< |
while (it.hasNext()) { |
435 |
> |
int i; |
436 |
> |
for (i = 0; it.hasNext(); i++) |
437 |
|
assertTrue(q.contains(it.next())); |
429 |
– |
++i; |
430 |
– |
} |
438 |
|
assertEquals(i, SIZE); |
439 |
+ |
assertIteratorExhausted(it); |
440 |
|
} |
441 |
|
|
442 |
|
/** |
443 |
|
* iterator of empty set has no elements |
444 |
|
*/ |
445 |
|
public void testEmptyIterator() { |
446 |
< |
NavigableSet q = set0(); |
439 |
< |
int i = 0; |
440 |
< |
Iterator it = q.iterator(); |
441 |
< |
while (it.hasNext()) { |
442 |
< |
assertTrue(q.contains(it.next())); |
443 |
< |
++i; |
444 |
< |
} |
445 |
< |
assertEquals(i, 0); |
446 |
> |
assertIteratorExhausted(set0().iterator()); |
447 |
|
} |
448 |
|
|
449 |
|
/** |
465 |
|
assertFalse(it.hasNext()); |
466 |
|
} |
467 |
|
|
467 |
– |
|
468 |
|
/** |
469 |
|
* toString contains toStrings of elements |
470 |
|
*/ |
472 |
|
NavigableSet q = populatedSet(SIZE); |
473 |
|
String s = q.toString(); |
474 |
|
for (int i = 0; i < SIZE; ++i) { |
475 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
475 |
> |
assertTrue(s.contains(String.valueOf(i))); |
476 |
|
} |
477 |
|
} |
478 |
|
|
480 |
|
* A deserialized serialized set has same elements |
481 |
|
*/ |
482 |
|
public void testSerialization() throws Exception { |
483 |
< |
NavigableSet q = populatedSet(SIZE); |
484 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
485 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
486 |
< |
out.writeObject(q); |
487 |
< |
out.close(); |
488 |
< |
|
489 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
490 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
491 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
492 |
< |
assertEquals(q.size(), r.size()); |
493 |
< |
while (!q.isEmpty()) |
494 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
483 |
> |
NavigableSet x = populatedSet(SIZE); |
484 |
> |
NavigableSet y = serialClone(x); |
485 |
> |
|
486 |
> |
assertNotSame(y, x); |
487 |
> |
assertEquals(x.size(), y.size()); |
488 |
> |
assertEquals(x, y); |
489 |
> |
assertEquals(y, x); |
490 |
> |
while (!x.isEmpty()) { |
491 |
> |
assertFalse(y.isEmpty()); |
492 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
493 |
> |
} |
494 |
> |
assertTrue(y.isEmpty()); |
495 |
|
} |
496 |
|
|
497 |
|
/** |
669 |
|
} catch (ClassCastException success) {} |
670 |
|
} |
671 |
|
|
672 |
– |
|
672 |
|
/** |
673 |
|
* addAll(null) throws NPE |
674 |
|
*/ |
738 |
|
*/ |
739 |
|
public void testDescendingRemoveElement() { |
740 |
|
NavigableSet q = populatedSet(SIZE); |
741 |
< |
for (int i = 1; i < SIZE; i+=2) { |
741 |
> |
for (int i = 1; i < SIZE; i += 2) { |
742 |
|
assertTrue(q.remove(new Integer(i))); |
743 |
|
} |
744 |
< |
for (int i = 0; i < SIZE; i+=2) { |
744 |
> |
for (int i = 0; i < SIZE; i += 2 ) { |
745 |
|
assertTrue(q.remove(new Integer(i))); |
746 |
|
assertFalse(q.remove(new Integer(i+1))); |
747 |
|
} |
817 |
|
assertTrue(q.removeAll(p)); |
818 |
|
assertEquals(SIZE-i, q.size()); |
819 |
|
for (int j = 0; j < i; ++j) { |
820 |
< |
Integer I = (Integer)(p.pollFirst()); |
821 |
< |
assertFalse(q.contains(I)); |
820 |
> |
Integer x = (Integer)(p.pollFirst()); |
821 |
> |
assertFalse(q.contains(x)); |
822 |
|
} |
823 |
|
} |
824 |
|
} |
825 |
|
|
827 |
– |
|
828 |
– |
|
826 |
|
/** |
827 |
|
* lower returns preceding element |
828 |
|
*/ |
943 |
|
assertTrue(q.contains(it.next())); |
944 |
|
++i; |
945 |
|
} |
946 |
< |
assertEquals(i, 0); |
946 |
> |
assertEquals(0, i); |
947 |
|
} |
948 |
|
|
949 |
|
/** |
965 |
|
assertFalse(it.hasNext()); |
966 |
|
} |
967 |
|
|
971 |
– |
|
968 |
|
/** |
969 |
|
* toString contains toStrings of elements |
970 |
|
*/ |
972 |
|
NavigableSet q = populatedSet(SIZE); |
973 |
|
String s = q.toString(); |
974 |
|
for (int i = 0; i < SIZE; ++i) { |
975 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
975 |
> |
assertTrue(s.contains(String.valueOf(i))); |
976 |
|
} |
977 |
|
} |
978 |
|
|
980 |
|
* A deserialized serialized set has same elements |
981 |
|
*/ |
982 |
|
public void testDescendingSerialization() throws Exception { |
983 |
< |
NavigableSet q = populatedSet(SIZE); |
984 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
985 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
986 |
< |
out.writeObject(q); |
987 |
< |
out.close(); |
988 |
< |
|
989 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
990 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
991 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
992 |
< |
assertEquals(q.size(), r.size()); |
993 |
< |
while (!q.isEmpty()) |
994 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
983 |
> |
NavigableSet x = dset5(); |
984 |
> |
NavigableSet y = serialClone(x); |
985 |
> |
|
986 |
> |
assertNotSame(y, x); |
987 |
> |
assertEquals(x.size(), y.size()); |
988 |
> |
assertEquals(x, y); |
989 |
> |
assertEquals(y, x); |
990 |
> |
while (!x.isEmpty()) { |
991 |
> |
assertFalse(y.isEmpty()); |
992 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
993 |
> |
} |
994 |
> |
assertTrue(y.isEmpty()); |
995 |
|
} |
996 |
|
|
997 |
|
/** |