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<Integer> populatedSet(int n) { |
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 |
|
|
100 |
– |
|
105 |
|
/** |
106 |
|
* isEmpty is true before add, false after |
107 |
|
*/ |
135 |
|
* add(null) throws NPE |
136 |
|
*/ |
137 |
|
public void testAddNull() { |
138 |
+ |
NavigableSet q = set0(); |
139 |
|
try { |
135 |
– |
NavigableSet q = set0(); |
140 |
|
q.add(null); |
141 |
|
shouldThrow(); |
142 |
|
} catch (NullPointerException success) {} |
163 |
|
* Add of non-Comparable throws CCE |
164 |
|
*/ |
165 |
|
public void testAddNonComparable() { |
166 |
+ |
NavigableSet q = set0(); |
167 |
|
try { |
163 |
– |
NavigableSet q = set0(); |
168 |
|
q.add(new Object()); |
169 |
|
q.add(new Object()); |
170 |
|
q.add(new Object()); |
172 |
|
} catch (ClassCastException success) {} |
173 |
|
} |
174 |
|
|
171 |
– |
|
175 |
|
/** |
176 |
|
* addAll(null) throws NPE |
177 |
|
*/ |
178 |
|
public void testAddAll1() { |
179 |
+ |
NavigableSet q = set0(); |
180 |
|
try { |
177 |
– |
NavigableSet q = set0(); |
181 |
|
q.addAll(null); |
182 |
|
shouldThrow(); |
183 |
|
} catch (NullPointerException success) {} |
187 |
|
* addAll of a collection with null elements throws NPE |
188 |
|
*/ |
189 |
|
public void testAddAll2() { |
190 |
+ |
NavigableSet q = set0(); |
191 |
+ |
Integer[] ints = new Integer[SIZE]; |
192 |
|
try { |
188 |
– |
NavigableSet q = set0(); |
189 |
– |
Integer[] ints = new Integer[SIZE]; |
193 |
|
q.addAll(Arrays.asList(ints)); |
194 |
|
shouldThrow(); |
195 |
|
} catch (NullPointerException success) {} |
200 |
|
* possibly adding some elements |
201 |
|
*/ |
202 |
|
public void testAddAll3() { |
203 |
+ |
NavigableSet q = set0(); |
204 |
+ |
Integer[] ints = new Integer[SIZE]; |
205 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
206 |
+ |
ints[i] = new Integer(i+SIZE); |
207 |
|
try { |
201 |
– |
NavigableSet q = set0(); |
202 |
– |
Integer[] ints = new Integer[SIZE]; |
203 |
– |
for (int i = 0; i < SIZE-1; ++i) |
204 |
– |
ints[i] = new Integer(i+SIZE); |
208 |
|
q.addAll(Arrays.asList(ints)); |
209 |
|
shouldThrow(); |
210 |
|
} catch (NullPointerException success) {} |
241 |
|
*/ |
242 |
|
public void testRemoveElement() { |
243 |
|
NavigableSet q = populatedSet(SIZE); |
244 |
< |
for (int i = 1; i < SIZE; i+=2) { |
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) { |
250 |
> |
for (int i = 0; i < SIZE; i += 2) { |
251 |
|
assertTrue(q.contains(i)); |
252 |
|
assertTrue(q.remove(i)); |
253 |
|
assertFalse(q.contains(i)); |
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 |
|
|
332 |
– |
|
333 |
– |
|
335 |
|
/** |
336 |
|
* lower returns preceding element |
337 |
|
*/ |
431 |
|
*/ |
432 |
|
public void testIterator() { |
433 |
|
NavigableSet q = populatedSet(SIZE); |
433 |
– |
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())); |
437 |
– |
++i; |
438 |
– |
} |
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(); |
447 |
< |
int i = 0; |
448 |
< |
Iterator it = q.iterator(); |
449 |
< |
while (it.hasNext()) { |
450 |
< |
assertTrue(q.contains(it.next())); |
451 |
< |
++i; |
452 |
< |
} |
453 |
< |
assertEquals(i, 0); |
446 |
> |
assertIteratorExhausted(set0().iterator()); |
447 |
|
} |
448 |
|
|
449 |
|
/** |
465 |
|
assertFalse(it.hasNext()); |
466 |
|
} |
467 |
|
|
475 |
– |
|
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 |
|
/** |
632 |
|
* add(null) throws NPE |
633 |
|
*/ |
634 |
|
public void testDescendingAddNull() { |
635 |
+ |
NavigableSet q = dset0(); |
636 |
|
try { |
644 |
– |
NavigableSet q = dset0(); |
637 |
|
q.add(null); |
638 |
|
shouldThrow(); |
639 |
|
} catch (NullPointerException success) {} |
660 |
|
* Add of non-Comparable throws CCE |
661 |
|
*/ |
662 |
|
public void testDescendingAddNonComparable() { |
663 |
+ |
NavigableSet q = dset0(); |
664 |
|
try { |
672 |
– |
NavigableSet q = dset0(); |
665 |
|
q.add(new Object()); |
666 |
|
q.add(new Object()); |
667 |
|
q.add(new Object()); |
669 |
|
} catch (ClassCastException success) {} |
670 |
|
} |
671 |
|
|
680 |
– |
|
672 |
|
/** |
673 |
|
* addAll(null) throws NPE |
674 |
|
*/ |
675 |
|
public void testDescendingAddAll1() { |
676 |
+ |
NavigableSet q = dset0(); |
677 |
|
try { |
686 |
– |
NavigableSet q = dset0(); |
678 |
|
q.addAll(null); |
679 |
|
shouldThrow(); |
680 |
|
} catch (NullPointerException success) {} |
684 |
|
* addAll of a collection with null elements throws NPE |
685 |
|
*/ |
686 |
|
public void testDescendingAddAll2() { |
687 |
+ |
NavigableSet q = dset0(); |
688 |
+ |
Integer[] ints = new Integer[SIZE]; |
689 |
|
try { |
697 |
– |
NavigableSet q = dset0(); |
698 |
– |
Integer[] ints = new Integer[SIZE]; |
690 |
|
q.addAll(Arrays.asList(ints)); |
691 |
|
shouldThrow(); |
692 |
|
} catch (NullPointerException success) {} |
697 |
|
* possibly adding some elements |
698 |
|
*/ |
699 |
|
public void testDescendingAddAll3() { |
700 |
+ |
NavigableSet q = dset0(); |
701 |
+ |
Integer[] ints = new Integer[SIZE]; |
702 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
703 |
+ |
ints[i] = new Integer(i+SIZE); |
704 |
|
try { |
710 |
– |
NavigableSet q = dset0(); |
711 |
– |
Integer[] ints = new Integer[SIZE]; |
712 |
– |
for (int i = 0; i < SIZE-1; ++i) |
713 |
– |
ints[i] = new Integer(i+SIZE); |
705 |
|
q.addAll(Arrays.asList(ints)); |
706 |
|
shouldThrow(); |
707 |
|
} catch (NullPointerException success) {} |
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 |
|
|
835 |
– |
|
836 |
– |
|
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 |
|
|
979 |
– |
|
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 |
|
/** |