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) { |
19 |
< |
junit.textui.TestRunner.run (suite()); |
19 |
> |
main(suite(), args); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
< |
return new TestSuite(ConcurrentSkipListSubSetTest.class); |
22 |
> |
return new TestSuite(ConcurrentSkipListSubSetTest.class); |
23 |
|
} |
24 |
|
|
25 |
< |
static class MyReverseComparator implements Comparator { |
25 |
> |
static class MyReverseComparator implements Comparator { |
26 |
|
public int compare(Object x, Object y) { |
27 |
< |
int i = ((Integer)x).intValue(); |
23 |
< |
int j = ((Integer)y).intValue(); |
24 |
< |
if (i < j) return 1; |
25 |
< |
if (i > j) return -1; |
26 |
< |
return 0; |
27 |
> |
return ((Comparable)y).compareTo(x); |
28 |
|
} |
29 |
|
} |
30 |
|
|
31 |
|
/** |
32 |
< |
* Create a set of given size containing consecutive |
33 |
< |
* Integers 0 ... n. |
32 |
> |
* Returns a new set of given size containing consecutive |
33 |
> |
* Integers 0 ... n - 1. |
34 |
|
*/ |
35 |
< |
private NavigableSet populatedSet(int n) { |
36 |
< |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
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) |
40 |
< |
assertTrue(q.add(new Integer(i))); |
41 |
< |
for(int i = (n & 1); i < n; i+=2) |
42 |
< |
assertTrue(q.add(new Integer(i))); |
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) |
42 |
> |
assertTrue(q.add(new Integer(i))); |
43 |
|
assertTrue(q.add(new Integer(-n))); |
44 |
|
assertTrue(q.add(new Integer(n))); |
45 |
< |
NavigableSet s = q.navigableSubSet(new Integer(0), true, new Integer(n), false); |
45 |
> |
NavigableSet s = q.subSet(new Integer(0), true, new Integer(n), false); |
46 |
|
assertFalse(s.isEmpty()); |
47 |
< |
assertEquals(n, s.size()); |
47 |
> |
assertEquals(n, s.size()); |
48 |
|
return s; |
49 |
|
} |
50 |
|
|
51 |
|
/** |
52 |
< |
* Create set of first 5 ints |
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); |
61 |
|
q.add(five); |
62 |
|
q.add(zero); |
63 |
|
q.add(seven); |
64 |
< |
NavigableSet s = q.navigableSubSet(one, true, seven, false); |
65 |
< |
assertEquals(5, s.size()); |
64 |
> |
NavigableSet s = q.subSet(one, true, seven, false); |
65 |
> |
assertEquals(5, s.size()); |
66 |
|
return s; |
67 |
|
} |
68 |
|
|
69 |
< |
private static NavigableSet set0() { |
70 |
< |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
69 |
> |
/** |
70 |
> |
* Returns a new set of first 5 negative ints. |
71 |
> |
*/ |
72 |
> |
private static NavigableSet dset5() { |
73 |
> |
ConcurrentSkipListSet q = new ConcurrentSkipListSet(); |
74 |
> |
assertTrue(q.isEmpty()); |
75 |
> |
q.add(m1); |
76 |
> |
q.add(m2); |
77 |
> |
q.add(m3); |
78 |
> |
q.add(m4); |
79 |
> |
q.add(m5); |
80 |
> |
NavigableSet s = q.descendingSet(); |
81 |
> |
assertEquals(5, s.size()); |
82 |
> |
return s; |
83 |
> |
} |
84 |
> |
|
85 |
> |
private static NavigableSet set0() { |
86 |
> |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
87 |
> |
assertTrue(set.isEmpty()); |
88 |
> |
return set.tailSet(m1, true); |
89 |
> |
} |
90 |
> |
|
91 |
> |
private static NavigableSet dset0() { |
92 |
> |
ConcurrentSkipListSet set = new ConcurrentSkipListSet(); |
93 |
|
assertTrue(set.isEmpty()); |
94 |
< |
return set.navigableTailSet(m1, true); |
94 |
> |
return set; |
95 |
|
} |
96 |
< |
|
96 |
> |
|
97 |
|
/** |
98 |
|
* A new set has unbounded capacity |
99 |
|
*/ |
101 |
|
assertEquals(0, set0().size()); |
102 |
|
} |
103 |
|
|
81 |
– |
|
104 |
|
/** |
105 |
|
* isEmpty is true before add, false after |
106 |
|
*/ |
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 |
< |
try { |
138 |
< |
NavigableSet q = set0(); |
137 |
> |
NavigableSet q = set0(); |
138 |
> |
try { |
139 |
|
q.add(null); |
140 |
|
shouldThrow(); |
141 |
< |
} catch (NullPointerException success) { } |
141 |
> |
} catch (NullPointerException success) {} |
142 |
|
} |
143 |
|
|
144 |
|
/** |
162 |
|
* Add of non-Comparable throws CCE |
163 |
|
*/ |
164 |
|
public void testAddNonComparable() { |
165 |
+ |
NavigableSet q = set0(); |
166 |
|
try { |
144 |
– |
NavigableSet q = set0(); |
145 |
– |
q.add(new Object()); |
167 |
|
q.add(new Object()); |
168 |
|
q.add(new Object()); |
169 |
|
shouldThrow(); |
170 |
< |
} |
150 |
< |
catch(ClassCastException success) {} |
170 |
> |
} catch (ClassCastException success) {} |
171 |
|
} |
172 |
|
|
153 |
– |
|
173 |
|
/** |
174 |
|
* addAll(null) throws NPE |
175 |
|
*/ |
176 |
|
public void testAddAll1() { |
177 |
+ |
NavigableSet q = set0(); |
178 |
|
try { |
159 |
– |
NavigableSet q = set0(); |
179 |
|
q.addAll(null); |
180 |
|
shouldThrow(); |
181 |
< |
} |
163 |
< |
catch (NullPointerException success) {} |
181 |
> |
} catch (NullPointerException success) {} |
182 |
|
} |
183 |
+ |
|
184 |
|
/** |
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 { |
170 |
– |
NavigableSet q = set0(); |
171 |
– |
Integer[] ints = new Integer[SIZE]; |
191 |
|
q.addAll(Arrays.asList(ints)); |
192 |
|
shouldThrow(); |
193 |
< |
} |
175 |
< |
catch (NullPointerException success) {} |
193 |
> |
} catch (NullPointerException success) {} |
194 |
|
} |
195 |
+ |
|
196 |
|
/** |
197 |
|
* addAll of a collection with any null elements throws NPE after |
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 { |
183 |
– |
NavigableSet q = set0(); |
184 |
– |
Integer[] ints = new Integer[SIZE]; |
185 |
– |
for (int i = 0; i < SIZE-1; ++i) |
186 |
– |
ints[i] = new Integer(i+SIZE); |
206 |
|
q.addAll(Arrays.asList(ints)); |
207 |
|
shouldThrow(); |
208 |
< |
} |
190 |
< |
catch (NullPointerException success) {} |
208 |
> |
} catch (NullPointerException success) {} |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
|
* Set contains all elements of successful addAll |
213 |
|
*/ |
214 |
|
public void testAddAll5() { |
215 |
< |
try { |
216 |
< |
Integer[] empty = new Integer[0]; |
217 |
< |
Integer[] ints = new Integer[SIZE]; |
218 |
< |
for (int i = 0; i < SIZE; ++i) |
219 |
< |
ints[i] = new Integer(SIZE-1- i); |
220 |
< |
NavigableSet q = set0(); |
221 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
222 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
223 |
< |
for (int i = 0; i < SIZE; ++i) |
206 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
207 |
< |
} |
208 |
< |
finally {} |
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); |
219 |
> |
NavigableSet q = set0(); |
220 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
221 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
222 |
> |
for (int i = 0; i < SIZE; ++i) |
223 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
229 |
|
public void testPoll() { |
230 |
|
NavigableSet q = populatedSet(SIZE); |
231 |
|
for (int i = 0; i < SIZE; ++i) { |
232 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
232 |
> |
assertEquals(i, q.pollFirst()); |
233 |
|
} |
234 |
< |
assertNull(q.pollFirst()); |
234 |
> |
assertNull(q.pollFirst()); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
239 |
|
*/ |
240 |
|
public void testRemoveElement() { |
241 |
|
NavigableSet q = populatedSet(SIZE); |
242 |
< |
for (int i = 1; i < SIZE; i+=2) { |
243 |
< |
assertTrue(q.remove(new Integer(i))); |
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)); |
247 |
|
} |
248 |
< |
for (int i = 0; i < SIZE; i+=2) { |
249 |
< |
assertTrue(q.remove(new Integer(i))); |
250 |
< |
assertFalse(q.remove(new Integer(i+1))); |
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)); |
254 |
|
} |
255 |
|
assertTrue(q.isEmpty()); |
256 |
|
} |
257 |
< |
|
257 |
> |
|
258 |
|
/** |
259 |
|
* contains(x) reports true when elements added but not yet removed |
260 |
|
*/ |
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 |
|
} |
332 |
|
|
312 |
– |
|
313 |
– |
|
333 |
|
/** |
334 |
|
* lower returns preceding element |
335 |
|
*/ |
346 |
|
|
347 |
|
Object e4 = q.lower(zero); |
348 |
|
assertNull(e4); |
330 |
– |
|
349 |
|
} |
350 |
|
|
351 |
|
/** |
364 |
|
|
365 |
|
Object e4 = q.higher(six); |
366 |
|
assertNull(e4); |
349 |
– |
|
367 |
|
} |
368 |
|
|
369 |
|
/** |
382 |
|
|
383 |
|
Object e4 = q.floor(zero); |
384 |
|
assertNull(e4); |
368 |
– |
|
385 |
|
} |
386 |
|
|
387 |
|
/** |
400 |
|
|
401 |
|
Object e4 = q.ceiling(six); |
402 |
|
assertNull(e4); |
387 |
– |
|
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
< |
* toArray contains all elements |
406 |
> |
* toArray contains all elements in sorted order |
407 |
|
*/ |
408 |
|
public void testToArray() { |
409 |
|
NavigableSet q = populatedSet(SIZE); |
410 |
< |
Object[] o = q.toArray(); |
411 |
< |
Arrays.sort(o); |
412 |
< |
for(int i = 0; i < o.length; i++) |
398 |
< |
assertEquals(o[i], q.pollFirst()); |
410 |
> |
Object[] o = q.toArray(); |
411 |
> |
for (int i = 0; i < o.length; i++) |
412 |
> |
assertSame(o[i], q.pollFirst()); |
413 |
|
} |
414 |
|
|
415 |
|
/** |
416 |
< |
* toArray(a) contains all elements |
416 |
> |
* toArray(a) contains all elements in sorted order |
417 |
|
*/ |
418 |
|
public void testToArray2() { |
419 |
< |
NavigableSet q = populatedSet(SIZE); |
420 |
< |
Integer[] ints = new Integer[SIZE]; |
421 |
< |
ints = (Integer[])q.toArray(ints); |
422 |
< |
Arrays.sort(ints); |
423 |
< |
for(int i = 0; i < ints.length; i++) |
424 |
< |
assertEquals(ints[i], q.pollFirst()); |
419 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
420 |
> |
Integer[] ints = new Integer[SIZE]; |
421 |
> |
Integer[] array = q.toArray(ints); |
422 |
> |
assertSame(ints, array); |
423 |
> |
for (int i = 0; i < ints.length; i++) |
424 |
> |
assertSame(ints[i], q.pollFirst()); |
425 |
|
} |
426 |
< |
|
426 |
> |
|
427 |
|
/** |
428 |
|
* iterator iterates through all elements |
429 |
|
*/ |
430 |
|
public void testIterator() { |
431 |
|
NavigableSet q = populatedSet(SIZE); |
432 |
< |
int i = 0; |
433 |
< |
Iterator it = q.iterator(); |
434 |
< |
while(it.hasNext()) { |
432 |
> |
Iterator it = q.iterator(); |
433 |
> |
int i; |
434 |
> |
for (i = 0; it.hasNext(); i++) |
435 |
|
assertTrue(q.contains(it.next())); |
422 |
– |
++i; |
423 |
– |
} |
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(); |
432 |
< |
int i = 0; |
433 |
< |
Iterator it = q.iterator(); |
434 |
< |
while(it.hasNext()) { |
435 |
< |
assertTrue(q.contains(it.next())); |
436 |
< |
++i; |
437 |
< |
} |
438 |
< |
assertEquals(i, 0); |
444 |
> |
assertIteratorExhausted(set0().iterator()); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
|
* iterator.remove removes current element |
449 |
|
*/ |
450 |
< |
public void testIteratorRemove () { |
450 |
> |
public void testIteratorRemove() { |
451 |
|
final NavigableSet q = set0(); |
452 |
|
q.add(new Integer(2)); |
453 |
|
q.add(new Integer(1)); |
463 |
|
assertFalse(it.hasNext()); |
464 |
|
} |
465 |
|
|
460 |
– |
|
466 |
|
/** |
467 |
|
* toString contains toStrings of elements |
468 |
|
*/ |
470 |
|
NavigableSet q = populatedSet(SIZE); |
471 |
|
String s = q.toString(); |
472 |
|
for (int i = 0; i < SIZE; ++i) { |
473 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
473 |
> |
assertTrue(s.contains(String.valueOf(i))); |
474 |
|
} |
475 |
< |
} |
475 |
> |
} |
476 |
|
|
477 |
|
/** |
478 |
< |
* A deserialized serialized set has same elements |
478 |
> |
* A deserialized/reserialized set equals original |
479 |
|
*/ |
480 |
< |
public void testSerialization() { |
481 |
< |
NavigableSet q = populatedSet(SIZE); |
482 |
< |
try { |
483 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
484 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
485 |
< |
out.writeObject(q); |
486 |
< |
out.close(); |
487 |
< |
|
488 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
489 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
490 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
486 |
< |
assertEquals(q.size(), r.size()); |
487 |
< |
while (!q.isEmpty()) |
488 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
489 |
< |
} catch(Exception e){ |
490 |
< |
e.printStackTrace(); |
491 |
< |
unexpectedException(); |
480 |
> |
public void testSerialization() throws Exception { |
481 |
> |
NavigableSet x = populatedSet(SIZE); |
482 |
> |
NavigableSet y = serialClone(x); |
483 |
> |
|
484 |
> |
assertNotSame(y, x); |
485 |
> |
assertEquals(x.size(), y.size()); |
486 |
> |
assertEquals(x, y); |
487 |
> |
assertEquals(y, x); |
488 |
> |
while (!x.isEmpty()) { |
489 |
> |
assertFalse(y.isEmpty()); |
490 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
491 |
|
} |
492 |
+ |
assertTrue(y.isEmpty()); |
493 |
|
} |
494 |
|
|
495 |
|
/** |
609 |
|
assertEquals(1, ssm.size()); |
610 |
|
assertEquals(3, sm.size()); |
611 |
|
assertEquals(4, set.size()); |
612 |
+ |
} |
613 |
+ |
|
614 |
+ |
/** |
615 |
+ |
* size changes when elements added and removed |
616 |
+ |
*/ |
617 |
+ |
public void testDescendingSize() { |
618 |
+ |
NavigableSet q = populatedSet(SIZE); |
619 |
+ |
for (int i = 0; i < SIZE; ++i) { |
620 |
+ |
assertEquals(SIZE - i, q.size()); |
621 |
+ |
q.pollFirst(); |
622 |
+ |
} |
623 |
+ |
for (int i = 0; i < SIZE; ++i) { |
624 |
+ |
assertEquals(i, q.size()); |
625 |
+ |
q.add(new Integer(i)); |
626 |
+ |
} |
627 |
+ |
} |
628 |
+ |
|
629 |
+ |
/** |
630 |
+ |
* add(null) throws NPE |
631 |
+ |
*/ |
632 |
+ |
public void testDescendingAddNull() { |
633 |
+ |
NavigableSet q = dset0(); |
634 |
+ |
try { |
635 |
+ |
q.add(null); |
636 |
+ |
shouldThrow(); |
637 |
+ |
} catch (NullPointerException success) {} |
638 |
+ |
} |
639 |
+ |
|
640 |
+ |
/** |
641 |
+ |
* Add of comparable element succeeds |
642 |
+ |
*/ |
643 |
+ |
public void testDescendingAdd() { |
644 |
+ |
NavigableSet q = dset0(); |
645 |
+ |
assertTrue(q.add(m6)); |
646 |
+ |
} |
647 |
+ |
|
648 |
+ |
/** |
649 |
+ |
* Add of duplicate element fails |
650 |
+ |
*/ |
651 |
+ |
public void testDescendingAddDup() { |
652 |
+ |
NavigableSet q = dset0(); |
653 |
+ |
assertTrue(q.add(m6)); |
654 |
+ |
assertFalse(q.add(m6)); |
655 |
+ |
} |
656 |
+ |
|
657 |
+ |
/** |
658 |
+ |
* Add of non-Comparable throws CCE |
659 |
+ |
*/ |
660 |
+ |
public void testDescendingAddNonComparable() { |
661 |
+ |
NavigableSet q = dset0(); |
662 |
+ |
try { |
663 |
+ |
q.add(new Object()); |
664 |
+ |
q.add(new Object()); |
665 |
+ |
shouldThrow(); |
666 |
+ |
} catch (ClassCastException success) {} |
667 |
+ |
} |
668 |
+ |
|
669 |
+ |
/** |
670 |
+ |
* addAll(null) throws NPE |
671 |
+ |
*/ |
672 |
+ |
public void testDescendingAddAll1() { |
673 |
+ |
NavigableSet q = dset0(); |
674 |
+ |
try { |
675 |
+ |
q.addAll(null); |
676 |
+ |
shouldThrow(); |
677 |
+ |
} catch (NullPointerException success) {} |
678 |
+ |
} |
679 |
+ |
|
680 |
+ |
/** |
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 { |
687 |
+ |
q.addAll(Arrays.asList(ints)); |
688 |
+ |
shouldThrow(); |
689 |
+ |
} catch (NullPointerException success) {} |
690 |
+ |
} |
691 |
+ |
|
692 |
+ |
/** |
693 |
+ |
* addAll of a collection with any null elements throws NPE after |
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 { |
702 |
+ |
q.addAll(Arrays.asList(ints)); |
703 |
+ |
shouldThrow(); |
704 |
+ |
} catch (NullPointerException success) {} |
705 |
+ |
} |
706 |
+ |
|
707 |
+ |
/** |
708 |
+ |
* Set contains all elements of successful addAll |
709 |
+ |
*/ |
710 |
+ |
public void testDescendingAddAll5() { |
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); |
715 |
+ |
NavigableSet q = dset0(); |
716 |
+ |
assertFalse(q.addAll(Arrays.asList(empty))); |
717 |
+ |
assertTrue(q.addAll(Arrays.asList(ints))); |
718 |
+ |
for (int i = 0; i < SIZE; ++i) |
719 |
+ |
assertEquals(new Integer(i), q.pollFirst()); |
720 |
+ |
} |
721 |
+ |
|
722 |
+ |
/** |
723 |
+ |
* poll succeeds unless empty |
724 |
+ |
*/ |
725 |
+ |
public void testDescendingPoll() { |
726 |
+ |
NavigableSet q = populatedSet(SIZE); |
727 |
+ |
for (int i = 0; i < SIZE; ++i) { |
728 |
+ |
assertEquals(i, q.pollFirst()); |
729 |
+ |
} |
730 |
+ |
assertNull(q.pollFirst()); |
731 |
+ |
} |
732 |
+ |
|
733 |
+ |
/** |
734 |
+ |
* remove(x) removes x and returns true if present |
735 |
+ |
*/ |
736 |
+ |
public void testDescendingRemoveElement() { |
737 |
+ |
NavigableSet q = populatedSet(SIZE); |
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 ) { |
742 |
+ |
assertTrue(q.remove(new Integer(i))); |
743 |
+ |
assertFalse(q.remove(new Integer(i + 1))); |
744 |
+ |
} |
745 |
+ |
assertTrue(q.isEmpty()); |
746 |
+ |
} |
747 |
+ |
|
748 |
+ |
/** |
749 |
+ |
* contains(x) reports true when elements added but not yet removed |
750 |
+ |
*/ |
751 |
+ |
public void testDescendingContains() { |
752 |
+ |
NavigableSet q = populatedSet(SIZE); |
753 |
+ |
for (int i = 0; i < SIZE; ++i) { |
754 |
+ |
assertTrue(q.contains(new Integer(i))); |
755 |
+ |
q.pollFirst(); |
756 |
+ |
assertFalse(q.contains(new Integer(i))); |
757 |
+ |
} |
758 |
+ |
} |
759 |
+ |
|
760 |
+ |
/** |
761 |
+ |
* clear removes all elements |
762 |
+ |
*/ |
763 |
+ |
public void testDescendingClear() { |
764 |
+ |
NavigableSet q = populatedSet(SIZE); |
765 |
+ |
q.clear(); |
766 |
+ |
assertTrue(q.isEmpty()); |
767 |
+ |
assertEquals(0, q.size()); |
768 |
+ |
q.add(new Integer(1)); |
769 |
+ |
assertFalse(q.isEmpty()); |
770 |
+ |
q.clear(); |
771 |
+ |
assertTrue(q.isEmpty()); |
772 |
+ |
} |
773 |
+ |
|
774 |
+ |
/** |
775 |
+ |
* containsAll(c) is true when c contains a subset of elements |
776 |
+ |
*/ |
777 |
+ |
public void testDescendingContainsAll() { |
778 |
+ |
NavigableSet q = populatedSet(SIZE); |
779 |
+ |
NavigableSet p = dset0(); |
780 |
+ |
for (int i = 0; i < SIZE; ++i) { |
781 |
+ |
assertTrue(q.containsAll(p)); |
782 |
+ |
assertFalse(p.containsAll(q)); |
783 |
+ |
p.add(new Integer(i)); |
784 |
+ |
} |
785 |
+ |
assertTrue(p.containsAll(q)); |
786 |
+ |
} |
787 |
+ |
|
788 |
+ |
/** |
789 |
+ |
* retainAll(c) retains only those elements of c and reports true if changed |
790 |
+ |
*/ |
791 |
+ |
public void testDescendingRetainAll() { |
792 |
+ |
NavigableSet q = populatedSet(SIZE); |
793 |
+ |
NavigableSet p = populatedSet(SIZE); |
794 |
+ |
for (int i = 0; i < SIZE; ++i) { |
795 |
+ |
boolean changed = q.retainAll(p); |
796 |
+ |
if (i == 0) |
797 |
+ |
assertFalse(changed); |
798 |
+ |
else |
799 |
+ |
assertTrue(changed); |
800 |
+ |
|
801 |
+ |
assertTrue(q.containsAll(p)); |
802 |
+ |
assertEquals(SIZE - i, q.size()); |
803 |
+ |
p.pollFirst(); |
804 |
+ |
} |
805 |
+ |
} |
806 |
+ |
|
807 |
+ |
/** |
808 |
+ |
* removeAll(c) removes only those elements of c and reports true if changed |
809 |
+ |
*/ |
810 |
+ |
public void testDescendingRemoveAll() { |
811 |
+ |
for (int i = 1; i < SIZE; ++i) { |
812 |
+ |
NavigableSet q = populatedSet(SIZE); |
813 |
+ |
NavigableSet p = populatedSet(i); |
814 |
+ |
assertTrue(q.removeAll(p)); |
815 |
+ |
assertEquals(SIZE - i, q.size()); |
816 |
+ |
for (int j = 0; j < i; ++j) { |
817 |
+ |
Integer x = (Integer)(p.pollFirst()); |
818 |
+ |
assertFalse(q.contains(x)); |
819 |
+ |
} |
820 |
+ |
} |
821 |
+ |
} |
822 |
+ |
|
823 |
+ |
/** |
824 |
+ |
* lower returns preceding element |
825 |
+ |
*/ |
826 |
+ |
public void testDescendingLower() { |
827 |
+ |
NavigableSet q = dset5(); |
828 |
+ |
Object e1 = q.lower(m3); |
829 |
+ |
assertEquals(m2, e1); |
830 |
+ |
|
831 |
+ |
Object e2 = q.lower(m6); |
832 |
+ |
assertEquals(m5, e2); |
833 |
+ |
|
834 |
+ |
Object e3 = q.lower(m1); |
835 |
+ |
assertNull(e3); |
836 |
+ |
|
837 |
+ |
Object e4 = q.lower(zero); |
838 |
+ |
assertNull(e4); |
839 |
+ |
} |
840 |
+ |
|
841 |
+ |
/** |
842 |
+ |
* higher returns next element |
843 |
+ |
*/ |
844 |
+ |
public void testDescendingHigher() { |
845 |
+ |
NavigableSet q = dset5(); |
846 |
+ |
Object e1 = q.higher(m3); |
847 |
+ |
assertEquals(m4, e1); |
848 |
+ |
|
849 |
+ |
Object e2 = q.higher(zero); |
850 |
+ |
assertEquals(m1, e2); |
851 |
+ |
|
852 |
+ |
Object e3 = q.higher(m5); |
853 |
+ |
assertNull(e3); |
854 |
+ |
|
855 |
+ |
Object e4 = q.higher(m6); |
856 |
+ |
assertNull(e4); |
857 |
+ |
} |
858 |
+ |
|
859 |
+ |
/** |
860 |
+ |
* floor returns preceding element |
861 |
+ |
*/ |
862 |
+ |
public void testDescendingFloor() { |
863 |
+ |
NavigableSet q = dset5(); |
864 |
+ |
Object e1 = q.floor(m3); |
865 |
+ |
assertEquals(m3, e1); |
866 |
+ |
|
867 |
+ |
Object e2 = q.floor(m6); |
868 |
+ |
assertEquals(m5, e2); |
869 |
+ |
|
870 |
+ |
Object e3 = q.floor(m1); |
871 |
+ |
assertEquals(m1, e3); |
872 |
+ |
|
873 |
+ |
Object e4 = q.floor(zero); |
874 |
+ |
assertNull(e4); |
875 |
+ |
} |
876 |
+ |
|
877 |
+ |
/** |
878 |
+ |
* ceiling returns next element |
879 |
+ |
*/ |
880 |
+ |
public void testDescendingCeiling() { |
881 |
+ |
NavigableSet q = dset5(); |
882 |
+ |
Object e1 = q.ceiling(m3); |
883 |
+ |
assertEquals(m3, e1); |
884 |
+ |
|
885 |
+ |
Object e2 = q.ceiling(zero); |
886 |
+ |
assertEquals(m1, e2); |
887 |
+ |
|
888 |
+ |
Object e3 = q.ceiling(m5); |
889 |
+ |
assertEquals(m5, e3); |
890 |
+ |
|
891 |
+ |
Object e4 = q.ceiling(m6); |
892 |
+ |
assertNull(e4); |
893 |
+ |
} |
894 |
+ |
|
895 |
+ |
/** |
896 |
+ |
* toArray contains all elements |
897 |
+ |
*/ |
898 |
+ |
public void testDescendingToArray() { |
899 |
+ |
NavigableSet q = populatedSet(SIZE); |
900 |
+ |
Object[] o = q.toArray(); |
901 |
+ |
Arrays.sort(o); |
902 |
+ |
for (int i = 0; i < o.length; i++) |
903 |
+ |
assertEquals(o[i], q.pollFirst()); |
904 |
+ |
} |
905 |
+ |
|
906 |
+ |
/** |
907 |
+ |
* toArray(a) contains all elements |
908 |
+ |
*/ |
909 |
+ |
public void testDescendingToArray2() { |
910 |
+ |
NavigableSet q = populatedSet(SIZE); |
911 |
+ |
Integer[] ints = new Integer[SIZE]; |
912 |
+ |
assertSame(ints, q.toArray(ints)); |
913 |
+ |
Arrays.sort(ints); |
914 |
+ |
for (int i = 0; i < ints.length; i++) |
915 |
+ |
assertEquals(ints[i], q.pollFirst()); |
916 |
+ |
} |
917 |
+ |
|
918 |
+ |
/** |
919 |
+ |
* iterator iterates through all elements |
920 |
+ |
*/ |
921 |
+ |
public void testDescendingIterator() { |
922 |
+ |
NavigableSet q = populatedSet(SIZE); |
923 |
+ |
int i = 0; |
924 |
+ |
Iterator it = q.iterator(); |
925 |
+ |
while (it.hasNext()) { |
926 |
+ |
assertTrue(q.contains(it.next())); |
927 |
+ |
++i; |
928 |
+ |
} |
929 |
+ |
assertEquals(i, SIZE); |
930 |
+ |
} |
931 |
+ |
|
932 |
+ |
/** |
933 |
+ |
* iterator of empty set has no elements |
934 |
+ |
*/ |
935 |
+ |
public void testDescendingEmptyIterator() { |
936 |
+ |
NavigableSet q = dset0(); |
937 |
+ |
int i = 0; |
938 |
+ |
Iterator it = q.iterator(); |
939 |
+ |
while (it.hasNext()) { |
940 |
+ |
assertTrue(q.contains(it.next())); |
941 |
+ |
++i; |
942 |
+ |
} |
943 |
+ |
assertEquals(0, i); |
944 |
+ |
} |
945 |
+ |
|
946 |
+ |
/** |
947 |
+ |
* iterator.remove removes current element |
948 |
+ |
*/ |
949 |
+ |
public void testDescendingIteratorRemove() { |
950 |
+ |
final NavigableSet q = dset0(); |
951 |
+ |
q.add(new Integer(2)); |
952 |
+ |
q.add(new Integer(1)); |
953 |
+ |
q.add(new Integer(3)); |
954 |
+ |
|
955 |
+ |
Iterator it = q.iterator(); |
956 |
+ |
it.next(); |
957 |
+ |
it.remove(); |
958 |
+ |
|
959 |
+ |
it = q.iterator(); |
960 |
+ |
assertEquals(it.next(), new Integer(2)); |
961 |
+ |
assertEquals(it.next(), new Integer(3)); |
962 |
+ |
assertFalse(it.hasNext()); |
963 |
+ |
} |
964 |
+ |
|
965 |
+ |
/** |
966 |
+ |
* toString contains toStrings of elements |
967 |
+ |
*/ |
968 |
+ |
public void testDescendingToString() { |
969 |
+ |
NavigableSet q = populatedSet(SIZE); |
970 |
+ |
String s = q.toString(); |
971 |
+ |
for (int i = 0; i < SIZE; ++i) { |
972 |
+ |
assertTrue(s.contains(String.valueOf(i))); |
973 |
+ |
} |
974 |
+ |
} |
975 |
+ |
|
976 |
+ |
/** |
977 |
+ |
* A deserialized/reserialized set equals original |
978 |
+ |
*/ |
979 |
+ |
public void testDescendingSerialization() throws Exception { |
980 |
+ |
NavigableSet x = dset5(); |
981 |
+ |
NavigableSet y = serialClone(x); |
982 |
+ |
|
983 |
+ |
assertNotSame(y, x); |
984 |
+ |
assertEquals(x.size(), y.size()); |
985 |
+ |
assertEquals(x, y); |
986 |
+ |
assertEquals(y, x); |
987 |
+ |
while (!x.isEmpty()) { |
988 |
+ |
assertFalse(y.isEmpty()); |
989 |
+ |
assertEquals(x.pollFirst(), y.pollFirst()); |
990 |
+ |
} |
991 |
+ |
assertTrue(y.isEmpty()); |
992 |
+ |
} |
993 |
+ |
|
994 |
+ |
/** |
995 |
+ |
* subSet returns set with keys in requested range |
996 |
+ |
*/ |
997 |
+ |
public void testDescendingSubSetContents() { |
998 |
+ |
NavigableSet set = dset5(); |
999 |
+ |
SortedSet sm = set.subSet(m2, m4); |
1000 |
+ |
assertEquals(m2, sm.first()); |
1001 |
+ |
assertEquals(m3, sm.last()); |
1002 |
+ |
assertEquals(2, sm.size()); |
1003 |
+ |
assertFalse(sm.contains(m1)); |
1004 |
+ |
assertTrue(sm.contains(m2)); |
1005 |
+ |
assertTrue(sm.contains(m3)); |
1006 |
+ |
assertFalse(sm.contains(m4)); |
1007 |
+ |
assertFalse(sm.contains(m5)); |
1008 |
+ |
Iterator i = sm.iterator(); |
1009 |
+ |
Object k; |
1010 |
+ |
k = (Integer)(i.next()); |
1011 |
+ |
assertEquals(m2, k); |
1012 |
+ |
k = (Integer)(i.next()); |
1013 |
+ |
assertEquals(m3, k); |
1014 |
+ |
assertFalse(i.hasNext()); |
1015 |
+ |
Iterator j = sm.iterator(); |
1016 |
+ |
j.next(); |
1017 |
+ |
j.remove(); |
1018 |
+ |
assertFalse(set.contains(m2)); |
1019 |
+ |
assertEquals(4, set.size()); |
1020 |
+ |
assertEquals(1, sm.size()); |
1021 |
+ |
assertEquals(m3, sm.first()); |
1022 |
+ |
assertEquals(m3, sm.last()); |
1023 |
+ |
assertTrue(sm.remove(m3)); |
1024 |
+ |
assertTrue(sm.isEmpty()); |
1025 |
+ |
assertEquals(3, set.size()); |
1026 |
+ |
} |
1027 |
+ |
|
1028 |
+ |
public void testDescendingSubSetContents2() { |
1029 |
+ |
NavigableSet set = dset5(); |
1030 |
+ |
SortedSet sm = set.subSet(m2, m3); |
1031 |
+ |
assertEquals(1, sm.size()); |
1032 |
+ |
assertEquals(m2, sm.first()); |
1033 |
+ |
assertEquals(m2, sm.last()); |
1034 |
+ |
assertFalse(sm.contains(m1)); |
1035 |
+ |
assertTrue(sm.contains(m2)); |
1036 |
+ |
assertFalse(sm.contains(m3)); |
1037 |
+ |
assertFalse(sm.contains(m4)); |
1038 |
+ |
assertFalse(sm.contains(m5)); |
1039 |
+ |
Iterator i = sm.iterator(); |
1040 |
+ |
Object k; |
1041 |
+ |
k = (Integer)(i.next()); |
1042 |
+ |
assertEquals(m2, k); |
1043 |
+ |
assertFalse(i.hasNext()); |
1044 |
+ |
Iterator j = sm.iterator(); |
1045 |
+ |
j.next(); |
1046 |
+ |
j.remove(); |
1047 |
+ |
assertFalse(set.contains(m2)); |
1048 |
+ |
assertEquals(4, set.size()); |
1049 |
+ |
assertEquals(0, sm.size()); |
1050 |
+ |
assertTrue(sm.isEmpty()); |
1051 |
+ |
assertFalse(sm.remove(m3)); |
1052 |
+ |
assertEquals(4, set.size()); |
1053 |
+ |
} |
1054 |
+ |
|
1055 |
+ |
/** |
1056 |
+ |
* headSet returns set with keys in requested range |
1057 |
+ |
*/ |
1058 |
+ |
public void testDescendingHeadSetContents() { |
1059 |
+ |
NavigableSet set = dset5(); |
1060 |
+ |
SortedSet sm = set.headSet(m4); |
1061 |
+ |
assertTrue(sm.contains(m1)); |
1062 |
+ |
assertTrue(sm.contains(m2)); |
1063 |
+ |
assertTrue(sm.contains(m3)); |
1064 |
+ |
assertFalse(sm.contains(m4)); |
1065 |
+ |
assertFalse(sm.contains(m5)); |
1066 |
+ |
Iterator i = sm.iterator(); |
1067 |
+ |
Object k; |
1068 |
+ |
k = (Integer)(i.next()); |
1069 |
+ |
assertEquals(m1, k); |
1070 |
+ |
k = (Integer)(i.next()); |
1071 |
+ |
assertEquals(m2, k); |
1072 |
+ |
k = (Integer)(i.next()); |
1073 |
+ |
assertEquals(m3, k); |
1074 |
+ |
assertFalse(i.hasNext()); |
1075 |
+ |
sm.clear(); |
1076 |
+ |
assertTrue(sm.isEmpty()); |
1077 |
+ |
assertEquals(2, set.size()); |
1078 |
+ |
assertEquals(m4, set.first()); |
1079 |
+ |
} |
1080 |
+ |
|
1081 |
+ |
/** |
1082 |
+ |
* tailSet returns set with keys in requested range |
1083 |
+ |
*/ |
1084 |
+ |
public void testDescendingTailSetContents() { |
1085 |
+ |
NavigableSet set = dset5(); |
1086 |
+ |
SortedSet sm = set.tailSet(m2); |
1087 |
+ |
assertFalse(sm.contains(m1)); |
1088 |
+ |
assertTrue(sm.contains(m2)); |
1089 |
+ |
assertTrue(sm.contains(m3)); |
1090 |
+ |
assertTrue(sm.contains(m4)); |
1091 |
+ |
assertTrue(sm.contains(m5)); |
1092 |
+ |
Iterator i = sm.iterator(); |
1093 |
+ |
Object k; |
1094 |
+ |
k = (Integer)(i.next()); |
1095 |
+ |
assertEquals(m2, k); |
1096 |
+ |
k = (Integer)(i.next()); |
1097 |
+ |
assertEquals(m3, k); |
1098 |
+ |
k = (Integer)(i.next()); |
1099 |
+ |
assertEquals(m4, k); |
1100 |
+ |
k = (Integer)(i.next()); |
1101 |
+ |
assertEquals(m5, k); |
1102 |
+ |
assertFalse(i.hasNext()); |
1103 |
+ |
|
1104 |
+ |
SortedSet ssm = sm.tailSet(m4); |
1105 |
+ |
assertEquals(m4, ssm.first()); |
1106 |
+ |
assertEquals(m5, ssm.last()); |
1107 |
+ |
assertTrue(ssm.remove(m4)); |
1108 |
+ |
assertEquals(1, ssm.size()); |
1109 |
+ |
assertEquals(3, sm.size()); |
1110 |
+ |
assertEquals(4, set.size()); |
1111 |
|
} |
1112 |
|
|
1113 |
|
} |