4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
import junit.framework.*; |
7 |
|
import java.util.Arrays; |
8 |
|
import java.util.Comparator; |
9 |
|
import java.util.Iterator; |
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) { |
19 |
< |
junit.textui.TestRunner.run(suite()); |
19 |
> |
main(suite(), args); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(ConcurrentSkipListSubSetTest.class); |
30 |
|
|
31 |
|
/** |
32 |
|
* Returns a new set of given size containing consecutive |
33 |
< |
* Integers 0 ... n. |
33 |
> |
* Integers 0 ... n - 1. |
34 |
|
*/ |
35 |
< |
private NavigableSet<Integer> populatedSet(int n) { |
36 |
< |
ConcurrentSkipListSet<Integer> q = |
35 |
< |
new ConcurrentSkipListSet<Integer>(); |
35 |
> |
private static NavigableSet<Integer> populatedSet(int n) { |
36 |
> |
ConcurrentSkipListSet<Integer> q = new ConcurrentSkipListSet<>(); |
37 |
|
assertTrue(q.isEmpty()); |
38 |
|
|
39 |
< |
for (int i = n-1; i >= 0; i-=2) |
39 |
> |
for (int i = n - 1; i >= 0; i -= 2) |
40 |
|
assertTrue(q.add(new Integer(i))); |
41 |
< |
for (int i = (n & 1); i < n; i+=2) |
41 |
> |
for (int i = (n & 1); i < n; i += 2) |
42 |
|
assertTrue(q.add(new Integer(i))); |
43 |
|
assertTrue(q.add(new Integer(-n))); |
44 |
|
assertTrue(q.add(new Integer(n))); |
51 |
|
/** |
52 |
|
* Returns a new set of first 5 ints. |
53 |
|
*/ |
54 |
< |
private NavigableSet set5() { |
54 |
> |
private static NavigableSet set5() { |
55 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
56 |
|
assertTrue(q.isEmpty()); |
57 |
|
q.add(one); |
69 |
|
/** |
70 |
|
* Returns a new set of first 5 negative ints. |
71 |
|
*/ |
72 |
< |
private NavigableSet dset5() { |
72 |
> |
private static NavigableSet dset5() { |
73 |
|
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
74 |
|
assertTrue(q.isEmpty()); |
75 |
|
q.add(m1); |
121 |
|
public void testSize() { |
122 |
|
NavigableSet q = populatedSet(SIZE); |
123 |
|
for (int i = 0; i < SIZE; ++i) { |
124 |
< |
assertEquals(SIZE-i, q.size()); |
124 |
> |
assertEquals(SIZE - i, q.size()); |
125 |
|
q.pollFirst(); |
126 |
|
} |
127 |
|
for (int i = 0; i < SIZE; ++i) { |
134 |
|
* add(null) throws NPE |
135 |
|
*/ |
136 |
|
public void testAddNull() { |
137 |
+ |
NavigableSet q = set0(); |
138 |
|
try { |
137 |
– |
NavigableSet q = set0(); |
139 |
|
q.add(null); |
140 |
|
shouldThrow(); |
141 |
|
} catch (NullPointerException success) {} |
162 |
|
* Add of non-Comparable throws CCE |
163 |
|
*/ |
164 |
|
public void testAddNonComparable() { |
165 |
+ |
NavigableSet q = set0(); |
166 |
|
try { |
165 |
– |
NavigableSet q = set0(); |
166 |
– |
q.add(new Object()); |
167 |
|
q.add(new Object()); |
168 |
|
q.add(new Object()); |
169 |
|
shouldThrow(); |
174 |
|
* addAll(null) throws NPE |
175 |
|
*/ |
176 |
|
public void testAddAll1() { |
177 |
+ |
NavigableSet q = set0(); |
178 |
|
try { |
178 |
– |
NavigableSet q = set0(); |
179 |
|
q.addAll(null); |
180 |
|
shouldThrow(); |
181 |
|
} catch (NullPointerException success) {} |
185 |
|
* addAll of a collection with null elements throws NPE |
186 |
|
*/ |
187 |
|
public void testAddAll2() { |
188 |
+ |
NavigableSet q = set0(); |
189 |
+ |
Integer[] ints = new Integer[SIZE]; |
190 |
|
try { |
189 |
– |
NavigableSet q = set0(); |
190 |
– |
Integer[] ints = new Integer[SIZE]; |
191 |
|
q.addAll(Arrays.asList(ints)); |
192 |
|
shouldThrow(); |
193 |
|
} catch (NullPointerException success) {} |
198 |
|
* possibly adding some elements |
199 |
|
*/ |
200 |
|
public void testAddAll3() { |
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); |
205 |
|
try { |
202 |
– |
NavigableSet q = set0(); |
203 |
– |
Integer[] ints = new Integer[SIZE]; |
204 |
– |
for (int i = 0; i < SIZE-1; ++i) |
205 |
– |
ints[i] = new Integer(i+SIZE); |
206 |
|
q.addAll(Arrays.asList(ints)); |
207 |
|
shouldThrow(); |
208 |
|
} catch (NullPointerException success) {} |
215 |
|
Integer[] empty = new Integer[0]; |
216 |
|
Integer[] ints = new Integer[SIZE]; |
217 |
|
for (int i = 0; i < SIZE; ++i) |
218 |
< |
ints[i] = new Integer(SIZE-1- i); |
218 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
219 |
|
NavigableSet q = set0(); |
220 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
221 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
239 |
|
*/ |
240 |
|
public void testRemoveElement() { |
241 |
|
NavigableSet q = populatedSet(SIZE); |
242 |
< |
for (int i = 1; i < SIZE; i+=2) { |
242 |
> |
for (int i = 1; i < SIZE; i += 2) { |
243 |
|
assertTrue(q.contains(i)); |
244 |
|
assertTrue(q.remove(i)); |
245 |
|
assertFalse(q.contains(i)); |
246 |
< |
assertTrue(q.contains(i-1)); |
246 |
> |
assertTrue(q.contains(i - 1)); |
247 |
|
} |
248 |
< |
for (int i = 0; i < SIZE; i+=2) { |
248 |
> |
for (int i = 0; i < SIZE; i += 2) { |
249 |
|
assertTrue(q.contains(i)); |
250 |
|
assertTrue(q.remove(i)); |
251 |
|
assertFalse(q.contains(i)); |
252 |
< |
assertFalse(q.remove(i+1)); |
253 |
< |
assertFalse(q.contains(i+1)); |
252 |
> |
assertFalse(q.remove(i + 1)); |
253 |
> |
assertFalse(q.contains(i + 1)); |
254 |
|
} |
255 |
|
assertTrue(q.isEmpty()); |
256 |
|
} |
309 |
|
assertTrue(changed); |
310 |
|
|
311 |
|
assertTrue(q.containsAll(p)); |
312 |
< |
assertEquals(SIZE-i, q.size()); |
312 |
> |
assertEquals(SIZE - i, q.size()); |
313 |
|
p.pollFirst(); |
314 |
|
} |
315 |
|
} |
322 |
|
NavigableSet q = populatedSet(SIZE); |
323 |
|
NavigableSet p = populatedSet(i); |
324 |
|
assertTrue(q.removeAll(p)); |
325 |
< |
assertEquals(SIZE-i, q.size()); |
325 |
> |
assertEquals(SIZE - i, q.size()); |
326 |
|
for (int j = 0; j < i; ++j) { |
327 |
< |
Integer I = (Integer)(p.pollFirst()); |
328 |
< |
assertFalse(q.contains(I)); |
327 |
> |
Integer x = (Integer)(p.pollFirst()); |
328 |
> |
assertFalse(q.contains(x)); |
329 |
|
} |
330 |
|
} |
331 |
|
} |
429 |
|
*/ |
430 |
|
public void testIterator() { |
431 |
|
NavigableSet q = populatedSet(SIZE); |
432 |
– |
int i = 0; |
432 |
|
Iterator it = q.iterator(); |
433 |
< |
while (it.hasNext()) { |
433 |
> |
int i; |
434 |
> |
for (i = 0; it.hasNext(); i++) |
435 |
|
assertTrue(q.contains(it.next())); |
436 |
– |
++i; |
437 |
– |
} |
436 |
|
assertEquals(i, SIZE); |
437 |
+ |
assertIteratorExhausted(it); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
|
* iterator of empty set has no elements |
442 |
|
*/ |
443 |
|
public void testEmptyIterator() { |
444 |
< |
NavigableSet q = set0(); |
446 |
< |
int i = 0; |
447 |
< |
Iterator it = q.iterator(); |
448 |
< |
while (it.hasNext()) { |
449 |
< |
assertTrue(q.contains(it.next())); |
450 |
< |
++i; |
451 |
< |
} |
452 |
< |
assertEquals(0, i); |
444 |
> |
assertIteratorExhausted(set0().iterator()); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
475 |
|
} |
476 |
|
|
477 |
|
/** |
478 |
< |
* A deserialized serialized set has same elements |
478 |
> |
* A deserialized/reserialized set equals original |
479 |
|
*/ |
480 |
|
public void testSerialization() throws Exception { |
481 |
|
NavigableSet x = populatedSet(SIZE); |
617 |
|
public void testDescendingSize() { |
618 |
|
NavigableSet q = populatedSet(SIZE); |
619 |
|
for (int i = 0; i < SIZE; ++i) { |
620 |
< |
assertEquals(SIZE-i, q.size()); |
620 |
> |
assertEquals(SIZE - i, q.size()); |
621 |
|
q.pollFirst(); |
622 |
|
} |
623 |
|
for (int i = 0; i < SIZE; ++i) { |
630 |
|
* add(null) throws NPE |
631 |
|
*/ |
632 |
|
public void testDescendingAddNull() { |
633 |
+ |
NavigableSet q = dset0(); |
634 |
|
try { |
642 |
– |
NavigableSet q = dset0(); |
635 |
|
q.add(null); |
636 |
|
shouldThrow(); |
637 |
|
} catch (NullPointerException success) {} |
658 |
|
* Add of non-Comparable throws CCE |
659 |
|
*/ |
660 |
|
public void testDescendingAddNonComparable() { |
661 |
+ |
NavigableSet q = dset0(); |
662 |
|
try { |
670 |
– |
NavigableSet q = dset0(); |
671 |
– |
q.add(new Object()); |
663 |
|
q.add(new Object()); |
664 |
|
q.add(new Object()); |
665 |
|
shouldThrow(); |
670 |
|
* addAll(null) throws NPE |
671 |
|
*/ |
672 |
|
public void testDescendingAddAll1() { |
673 |
+ |
NavigableSet q = dset0(); |
674 |
|
try { |
683 |
– |
NavigableSet q = dset0(); |
675 |
|
q.addAll(null); |
676 |
|
shouldThrow(); |
677 |
|
} catch (NullPointerException success) {} |
681 |
|
* addAll of a collection with null elements throws NPE |
682 |
|
*/ |
683 |
|
public void testDescendingAddAll2() { |
684 |
+ |
NavigableSet q = dset0(); |
685 |
+ |
Integer[] ints = new Integer[SIZE]; |
686 |
|
try { |
694 |
– |
NavigableSet q = dset0(); |
695 |
– |
Integer[] ints = new Integer[SIZE]; |
687 |
|
q.addAll(Arrays.asList(ints)); |
688 |
|
shouldThrow(); |
689 |
|
} catch (NullPointerException success) {} |
694 |
|
* possibly adding some elements |
695 |
|
*/ |
696 |
|
public void testDescendingAddAll3() { |
697 |
+ |
NavigableSet q = dset0(); |
698 |
+ |
Integer[] ints = new Integer[SIZE]; |
699 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
700 |
+ |
ints[i] = new Integer(i + SIZE); |
701 |
|
try { |
707 |
– |
NavigableSet q = dset0(); |
708 |
– |
Integer[] ints = new Integer[SIZE]; |
709 |
– |
for (int i = 0; i < SIZE-1; ++i) |
710 |
– |
ints[i] = new Integer(i+SIZE); |
702 |
|
q.addAll(Arrays.asList(ints)); |
703 |
|
shouldThrow(); |
704 |
|
} catch (NullPointerException success) {} |
711 |
|
Integer[] empty = new Integer[0]; |
712 |
|
Integer[] ints = new Integer[SIZE]; |
713 |
|
for (int i = 0; i < SIZE; ++i) |
714 |
< |
ints[i] = new Integer(SIZE-1- i); |
714 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
715 |
|
NavigableSet q = dset0(); |
716 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
717 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
735 |
|
*/ |
736 |
|
public void testDescendingRemoveElement() { |
737 |
|
NavigableSet q = populatedSet(SIZE); |
738 |
< |
for (int i = 1; i < SIZE; i+=2) { |
738 |
> |
for (int i = 1; i < SIZE; i += 2) { |
739 |
|
assertTrue(q.remove(new Integer(i))); |
740 |
|
} |
741 |
< |
for (int i = 0; i < SIZE; i+=2) { |
741 |
> |
for (int i = 0; i < SIZE; i += 2 ) { |
742 |
|
assertTrue(q.remove(new Integer(i))); |
743 |
< |
assertFalse(q.remove(new Integer(i+1))); |
743 |
> |
assertFalse(q.remove(new Integer(i + 1))); |
744 |
|
} |
745 |
|
assertTrue(q.isEmpty()); |
746 |
|
} |
799 |
|
assertTrue(changed); |
800 |
|
|
801 |
|
assertTrue(q.containsAll(p)); |
802 |
< |
assertEquals(SIZE-i, q.size()); |
802 |
> |
assertEquals(SIZE - i, q.size()); |
803 |
|
p.pollFirst(); |
804 |
|
} |
805 |
|
} |
812 |
|
NavigableSet q = populatedSet(SIZE); |
813 |
|
NavigableSet p = populatedSet(i); |
814 |
|
assertTrue(q.removeAll(p)); |
815 |
< |
assertEquals(SIZE-i, q.size()); |
815 |
> |
assertEquals(SIZE - i, q.size()); |
816 |
|
for (int j = 0; j < i; ++j) { |
817 |
< |
Integer I = (Integer)(p.pollFirst()); |
818 |
< |
assertFalse(q.contains(I)); |
817 |
> |
Integer x = (Integer)(p.pollFirst()); |
818 |
> |
assertFalse(q.contains(x)); |
819 |
|
} |
820 |
|
} |
821 |
|
} |
974 |
|
} |
975 |
|
|
976 |
|
/** |
977 |
< |
* A deserialized serialized set has same elements |
977 |
> |
* A deserialized/reserialized set equals original |
978 |
|
*/ |
979 |
|
public void testDescendingSerialization() throws Exception { |
980 |
|
NavigableSet x = dset5(); |