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