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.Set; |
12 |
> |
import java.util.SortedSet; |
13 |
> |
import java.util.TreeSet; |
14 |
> |
|
15 |
> |
import junit.framework.Test; |
16 |
> |
import junit.framework.TestSuite; |
17 |
|
|
18 |
|
public class TreeSubSetTest extends JSR166TestCase { |
19 |
|
public static void main(String[] args) { |
20 |
< |
junit.textui.TestRunner.run (suite()); |
20 |
> |
main(suite(), args); |
21 |
|
} |
22 |
|
public static Test suite() { |
23 |
< |
return new TestSuite(TreeSubSetTest.class); |
23 |
> |
return new TestSuite(TreeSubSetTest.class); |
24 |
|
} |
25 |
|
|
26 |
|
static class MyReverseComparator implements Comparator { |
27 |
|
public int compare(Object x, Object y) { |
28 |
< |
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; |
28 |
> |
return ((Comparable)y).compareTo(x); |
29 |
|
} |
30 |
|
} |
31 |
|
|
32 |
|
/** |
33 |
< |
* Create a set of given size containing consecutive |
33 |
> |
* Returns a new set of given size containing consecutive |
34 |
|
* Integers 0 ... n. |
35 |
|
*/ |
36 |
< |
private NavigableSet populatedSet(int n) { |
37 |
< |
TreeSet q = new TreeSet(); |
36 |
> |
private NavigableSet<Integer> populatedSet(int n) { |
37 |
> |
TreeSet<Integer> q = new TreeSet<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), 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() { |
56 |
|
TreeSet q = new TreeSet(); |
63 |
|
q.add(zero); |
64 |
|
q.add(seven); |
65 |
|
NavigableSet s = q.subSet(one, true, seven, false); |
66 |
< |
assertEquals(5, s.size()); |
66 |
> |
assertEquals(5, s.size()); |
67 |
|
return s; |
68 |
|
} |
69 |
|
|
76 |
|
q.add(m4); |
77 |
|
q.add(m5); |
78 |
|
NavigableSet s = q.descendingSet(); |
79 |
< |
assertEquals(5, s.size()); |
79 |
> |
assertEquals(5, s.size()); |
80 |
|
return s; |
81 |
|
} |
82 |
|
|
83 |
|
private static NavigableSet set0() { |
84 |
< |
TreeSet set = new TreeSet(); |
84 |
> |
TreeSet set = new TreeSet(); |
85 |
|
assertTrue(set.isEmpty()); |
86 |
|
return set.tailSet(m1, false); |
87 |
|
} |
88 |
|
|
89 |
|
private static NavigableSet dset0() { |
90 |
< |
TreeSet set = new TreeSet(); |
90 |
> |
TreeSet set = new TreeSet(); |
91 |
|
assertTrue(set.isEmpty()); |
92 |
|
return set; |
93 |
|
} |
99 |
|
assertEquals(0, set0().size()); |
100 |
|
} |
101 |
|
|
100 |
– |
|
102 |
|
/** |
103 |
|
* isEmpty is true before add, false after |
104 |
|
*/ |
105 |
|
public void testEmpty() { |
106 |
|
NavigableSet q = set0(); |
107 |
|
assertTrue(q.isEmpty()); |
108 |
< |
q.add(new Integer(1)); |
108 |
> |
assertTrue(q.add(new Integer(1))); |
109 |
|
assertFalse(q.isEmpty()); |
110 |
< |
q.add(new Integer(2)); |
110 |
> |
assertTrue(q.add(new Integer(2))); |
111 |
|
q.pollFirst(); |
112 |
|
q.pollFirst(); |
113 |
|
assertTrue(q.isEmpty()); |
119 |
|
public void testSize() { |
120 |
|
NavigableSet q = populatedSet(SIZE); |
121 |
|
for (int i = 0; i < SIZE; ++i) { |
122 |
< |
assertEquals(SIZE-i, q.size()); |
122 |
> |
assertEquals(SIZE - i, q.size()); |
123 |
|
q.pollFirst(); |
124 |
|
} |
125 |
|
for (int i = 0; i < SIZE; ++i) { |
132 |
|
* add(null) throws NPE |
133 |
|
*/ |
134 |
|
public void testAddNull() { |
135 |
< |
try { |
136 |
< |
NavigableSet q = set0(); |
135 |
> |
NavigableSet q = set0(); |
136 |
> |
try { |
137 |
|
q.add(null); |
138 |
|
shouldThrow(); |
139 |
< |
} catch (NullPointerException success) { } |
139 |
> |
} catch (NullPointerException success) {} |
140 |
|
} |
141 |
|
|
142 |
|
/** |
160 |
|
* Add of non-Comparable throws CCE |
161 |
|
*/ |
162 |
|
public void testAddNonComparable() { |
163 |
+ |
NavigableSet q = set0(); |
164 |
|
try { |
163 |
– |
NavigableSet q = set0(); |
164 |
– |
q.add(new Object()); |
165 |
|
q.add(new Object()); |
166 |
|
q.add(new Object()); |
167 |
|
shouldThrow(); |
168 |
< |
} |
169 |
< |
catch (ClassCastException success) {} |
168 |
> |
} catch (ClassCastException success) {} |
169 |
|
} |
170 |
|
|
172 |
– |
|
171 |
|
/** |
172 |
|
* addAll(null) throws NPE |
173 |
|
*/ |
174 |
|
public void testAddAll1() { |
175 |
+ |
NavigableSet q = set0(); |
176 |
|
try { |
178 |
– |
NavigableSet q = set0(); |
177 |
|
q.addAll(null); |
178 |
|
shouldThrow(); |
179 |
< |
} |
182 |
< |
catch (NullPointerException success) {} |
179 |
> |
} catch (NullPointerException success) {} |
180 |
|
} |
181 |
+ |
|
182 |
|
/** |
183 |
|
* addAll of a collection with null elements throws NPE |
184 |
|
*/ |
185 |
|
public void testAddAll2() { |
186 |
+ |
NavigableSet q = set0(); |
187 |
+ |
Integer[] ints = new Integer[SIZE]; |
188 |
|
try { |
189 |
– |
NavigableSet q = set0(); |
190 |
– |
Integer[] ints = new Integer[SIZE]; |
189 |
|
q.addAll(Arrays.asList(ints)); |
190 |
|
shouldThrow(); |
191 |
< |
} |
194 |
< |
catch (NullPointerException success) {} |
191 |
> |
} catch (NullPointerException success) {} |
192 |
|
} |
193 |
+ |
|
194 |
|
/** |
195 |
|
* addAll of a collection with any null elements throws NPE after |
196 |
|
* possibly adding some elements |
197 |
|
*/ |
198 |
|
public void testAddAll3() { |
199 |
+ |
NavigableSet q = set0(); |
200 |
+ |
Integer[] ints = new Integer[SIZE]; |
201 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
202 |
+ |
ints[i] = new Integer(i + SIZE); |
203 |
|
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); |
204 |
|
q.addAll(Arrays.asList(ints)); |
205 |
|
shouldThrow(); |
206 |
< |
} |
209 |
< |
catch (NullPointerException success) {} |
206 |
> |
} catch (NullPointerException success) {} |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
|
* Set contains all elements of successful addAll |
211 |
|
*/ |
212 |
|
public void testAddAll5() { |
213 |
< |
try { |
214 |
< |
Integer[] empty = new Integer[0]; |
215 |
< |
Integer[] ints = new Integer[SIZE]; |
216 |
< |
for (int i = 0; i < SIZE; ++i) |
217 |
< |
ints[i] = new Integer(SIZE-1- i); |
218 |
< |
NavigableSet q = set0(); |
219 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
220 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
221 |
< |
for (int i = 0; i < SIZE; ++i) |
225 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
226 |
< |
} |
227 |
< |
finally {} |
213 |
> |
Integer[] empty = new Integer[0]; |
214 |
> |
Integer[] ints = new Integer[SIZE]; |
215 |
> |
for (int i = 0; i < SIZE; ++i) |
216 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
217 |
> |
NavigableSet q = set0(); |
218 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
219 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
220 |
> |
for (int i = 0; i < SIZE; ++i) |
221 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
222 |
|
} |
223 |
|
|
224 |
|
/** |
227 |
|
public void testPoll() { |
228 |
|
NavigableSet q = populatedSet(SIZE); |
229 |
|
for (int i = 0; i < SIZE; ++i) { |
230 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
230 |
> |
assertEquals(i, q.pollFirst()); |
231 |
|
} |
232 |
< |
assertNull(q.pollFirst()); |
232 |
> |
assertNull(q.pollFirst()); |
233 |
|
} |
234 |
|
|
235 |
|
/** |
237 |
|
*/ |
238 |
|
public void testRemoveElement() { |
239 |
|
NavigableSet q = populatedSet(SIZE); |
240 |
< |
for (int i = 1; i < SIZE; i+=2) { |
241 |
< |
assertTrue(q.remove(new Integer(i))); |
242 |
< |
} |
243 |
< |
for (int i = 0; i < SIZE; i+=2) { |
244 |
< |
assertTrue(q.remove(new Integer(i))); |
245 |
< |
assertFalse(q.remove(new Integer(i+1))); |
240 |
> |
for (int i = 1; i < SIZE; i += 2) { |
241 |
> |
assertTrue(q.contains(i)); |
242 |
> |
assertTrue(q.remove(i)); |
243 |
> |
assertFalse(q.contains(i)); |
244 |
> |
assertTrue(q.contains(i-1)); |
245 |
> |
} |
246 |
> |
for (int i = 0; i < SIZE; i += 2) { |
247 |
> |
assertTrue(q.contains(i)); |
248 |
> |
assertTrue(q.remove(i)); |
249 |
> |
assertFalse(q.contains(i)); |
250 |
> |
assertFalse(q.remove(i+1)); |
251 |
> |
assertFalse(q.contains(i+1)); |
252 |
|
} |
253 |
|
assertTrue(q.isEmpty()); |
254 |
|
} |
273 |
|
q.clear(); |
274 |
|
assertTrue(q.isEmpty()); |
275 |
|
assertEquals(0, q.size()); |
276 |
< |
q.add(new Integer(1)); |
276 |
> |
assertTrue(q.add(new Integer(1))); |
277 |
|
assertFalse(q.isEmpty()); |
278 |
|
q.clear(); |
279 |
|
assertTrue(q.isEmpty()); |
307 |
|
assertTrue(changed); |
308 |
|
|
309 |
|
assertTrue(q.containsAll(p)); |
310 |
< |
assertEquals(SIZE-i, q.size()); |
310 |
> |
assertEquals(SIZE - i, q.size()); |
311 |
|
p.pollFirst(); |
312 |
|
} |
313 |
|
} |
320 |
|
NavigableSet q = populatedSet(SIZE); |
321 |
|
NavigableSet p = populatedSet(i); |
322 |
|
assertTrue(q.removeAll(p)); |
323 |
< |
assertEquals(SIZE-i, q.size()); |
323 |
> |
assertEquals(SIZE - i, q.size()); |
324 |
|
for (int j = 0; j < i; ++j) { |
325 |
< |
Integer I = (Integer)(p.pollFirst()); |
326 |
< |
assertFalse(q.contains(I)); |
325 |
> |
Integer x = (Integer)(p.pollFirst()); |
326 |
> |
assertFalse(q.contains(x)); |
327 |
|
} |
328 |
|
} |
329 |
|
} |
330 |
|
|
331 |
– |
|
332 |
– |
|
331 |
|
/** |
332 |
|
* lower returns preceding element |
333 |
|
*/ |
344 |
|
|
345 |
|
Object e4 = q.lower(zero); |
346 |
|
assertNull(e4); |
349 |
– |
|
347 |
|
} |
348 |
|
|
349 |
|
/** |
362 |
|
|
363 |
|
Object e4 = q.higher(six); |
364 |
|
assertNull(e4); |
368 |
– |
|
365 |
|
} |
366 |
|
|
367 |
|
/** |
380 |
|
|
381 |
|
Object e4 = q.floor(zero); |
382 |
|
assertNull(e4); |
387 |
– |
|
383 |
|
} |
384 |
|
|
385 |
|
/** |
398 |
|
|
399 |
|
Object e4 = q.ceiling(six); |
400 |
|
assertNull(e4); |
406 |
– |
|
401 |
|
} |
402 |
|
|
403 |
|
/** |
404 |
< |
* toArray contains all elements |
404 |
> |
* toArray contains all elements in sorted order |
405 |
|
*/ |
406 |
|
public void testToArray() { |
407 |
|
NavigableSet q = populatedSet(SIZE); |
408 |
< |
Object[] o = q.toArray(); |
409 |
< |
Arrays.sort(o); |
410 |
< |
for (int i = 0; i < o.length; i++) |
417 |
< |
assertEquals(o[i], q.pollFirst()); |
408 |
> |
Object[] o = q.toArray(); |
409 |
> |
for (int i = 0; i < o.length; i++) |
410 |
> |
assertSame(o[i], q.pollFirst()); |
411 |
|
} |
412 |
|
|
413 |
|
/** |
414 |
< |
* toArray(a) contains all elements |
414 |
> |
* toArray(a) contains all elements in sorted order |
415 |
|
*/ |
416 |
|
public void testToArray2() { |
417 |
< |
NavigableSet q = populatedSet(SIZE); |
418 |
< |
Integer[] ints = new Integer[SIZE]; |
419 |
< |
ints = (Integer[])q.toArray(ints); |
420 |
< |
Arrays.sort(ints); |
417 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
418 |
> |
Integer[] ints = new Integer[SIZE]; |
419 |
> |
Integer[] array = q.toArray(ints); |
420 |
> |
assertSame(ints, array); |
421 |
|
for (int i = 0; i < ints.length; i++) |
422 |
< |
assertEquals(ints[i], q.pollFirst()); |
422 |
> |
assertSame(ints[i], q.pollFirst()); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
427 |
|
*/ |
428 |
|
public void testIterator() { |
429 |
|
NavigableSet q = populatedSet(SIZE); |
430 |
< |
int i = 0; |
431 |
< |
Iterator it = q.iterator(); |
432 |
< |
while (it.hasNext()) { |
430 |
> |
Iterator it = q.iterator(); |
431 |
> |
int i; |
432 |
> |
for (i = 0; it.hasNext(); i++) |
433 |
|
assertTrue(q.contains(it.next())); |
441 |
– |
++i; |
442 |
– |
} |
434 |
|
assertEquals(i, SIZE); |
435 |
+ |
assertIteratorExhausted(it); |
436 |
|
} |
437 |
|
|
438 |
|
/** |
439 |
|
* iterator of empty set has no elements |
440 |
|
*/ |
441 |
|
public void testEmptyIterator() { |
442 |
< |
NavigableSet q = set0(); |
451 |
< |
int i = 0; |
452 |
< |
Iterator it = q.iterator(); |
453 |
< |
while (it.hasNext()) { |
454 |
< |
assertTrue(q.contains(it.next())); |
455 |
< |
++i; |
456 |
< |
} |
457 |
< |
assertEquals(i, 0); |
442 |
> |
assertIteratorExhausted(set0().iterator()); |
443 |
|
} |
444 |
|
|
445 |
|
/** |
446 |
|
* iterator.remove removes current element |
447 |
|
*/ |
448 |
< |
public void testIteratorRemove () { |
448 |
> |
public void testIteratorRemove() { |
449 |
|
final NavigableSet q = set0(); |
450 |
|
q.add(new Integer(2)); |
451 |
|
q.add(new Integer(1)); |
456 |
|
it.remove(); |
457 |
|
|
458 |
|
it = q.iterator(); |
459 |
< |
assertEquals(it.next(), new Integer(2)); |
460 |
< |
assertEquals(it.next(), new Integer(3)); |
459 |
> |
assertEquals(2, it.next()); |
460 |
> |
assertEquals(3, it.next()); |
461 |
|
assertFalse(it.hasNext()); |
462 |
|
} |
463 |
|
|
479 |
– |
|
464 |
|
/** |
465 |
|
* toString contains toStrings of elements |
466 |
|
*/ |
468 |
|
NavigableSet q = populatedSet(SIZE); |
469 |
|
String s = q.toString(); |
470 |
|
for (int i = 0; i < SIZE; ++i) { |
471 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
471 |
> |
assertTrue(s.contains(String.valueOf(i))); |
472 |
|
} |
473 |
|
} |
474 |
|
|
475 |
|
/** |
476 |
|
* A deserialized serialized set has same elements |
477 |
|
*/ |
478 |
< |
public void testSerialization() { |
479 |
< |
NavigableSet q = populatedSet(SIZE); |
480 |
< |
try { |
481 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
482 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
483 |
< |
out.writeObject(q); |
484 |
< |
out.close(); |
485 |
< |
|
486 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
487 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
488 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
505 |
< |
assertEquals(q.size(), r.size()); |
506 |
< |
while (!q.isEmpty()) |
507 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
508 |
< |
} catch (Exception e){ |
509 |
< |
e.printStackTrace(); |
510 |
< |
unexpectedException(); |
478 |
> |
public void testSerialization() throws Exception { |
479 |
> |
NavigableSet x = populatedSet(SIZE); |
480 |
> |
NavigableSet y = serialClone(x); |
481 |
> |
|
482 |
> |
assertNotSame(x, y); |
483 |
> |
assertEquals(x.size(), y.size()); |
484 |
> |
assertEquals(x, y); |
485 |
> |
assertEquals(y, x); |
486 |
> |
while (!x.isEmpty()) { |
487 |
> |
assertFalse(y.isEmpty()); |
488 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
489 |
|
} |
490 |
+ |
assertTrue(y.isEmpty()); |
491 |
|
} |
492 |
|
|
493 |
|
/** |
615 |
|
public void testDescendingSize() { |
616 |
|
NavigableSet q = populatedSet(SIZE); |
617 |
|
for (int i = 0; i < SIZE; ++i) { |
618 |
< |
assertEquals(SIZE-i, q.size()); |
618 |
> |
assertEquals(SIZE - i, q.size()); |
619 |
|
q.pollFirst(); |
620 |
|
} |
621 |
|
for (int i = 0; i < SIZE; ++i) { |
645 |
|
* Add of non-Comparable throws CCE |
646 |
|
*/ |
647 |
|
public void testDescendingAddNonComparable() { |
648 |
+ |
NavigableSet q = dset0(); |
649 |
|
try { |
670 |
– |
NavigableSet q = dset0(); |
671 |
– |
q.add(new Object()); |
650 |
|
q.add(new Object()); |
651 |
|
q.add(new Object()); |
652 |
|
shouldThrow(); |
653 |
< |
} |
676 |
< |
catch (ClassCastException success) {} |
653 |
> |
} catch (ClassCastException success) {} |
654 |
|
} |
655 |
|
|
679 |
– |
|
656 |
|
/** |
657 |
|
* addAll(null) throws NPE |
658 |
|
*/ |
659 |
|
public void testDescendingAddAll1() { |
660 |
+ |
NavigableSet q = dset0(); |
661 |
|
try { |
685 |
– |
NavigableSet q = dset0(); |
662 |
|
q.addAll(null); |
663 |
|
shouldThrow(); |
664 |
< |
} |
689 |
< |
catch (NullPointerException success) {} |
664 |
> |
} catch (NullPointerException success) {} |
665 |
|
} |
666 |
+ |
|
667 |
|
/** |
668 |
|
* addAll of a collection with null elements throws NPE |
669 |
|
*/ |
670 |
|
public void testDescendingAddAll2() { |
671 |
+ |
NavigableSet q = dset0(); |
672 |
+ |
Integer[] ints = new Integer[SIZE]; |
673 |
|
try { |
696 |
– |
NavigableSet q = dset0(); |
697 |
– |
Integer[] ints = new Integer[SIZE]; |
674 |
|
q.addAll(Arrays.asList(ints)); |
675 |
|
shouldThrow(); |
676 |
< |
} |
701 |
< |
catch (NullPointerException success) {} |
676 |
> |
} catch (NullPointerException success) {} |
677 |
|
} |
678 |
+ |
|
679 |
|
/** |
680 |
|
* addAll of a collection with any null elements throws NPE after |
681 |
|
* possibly adding some elements |
682 |
|
*/ |
683 |
|
public void testDescendingAddAll3() { |
684 |
+ |
NavigableSet q = dset0(); |
685 |
+ |
Integer[] ints = new Integer[SIZE]; |
686 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
687 |
+ |
ints[i] = new Integer(i + SIZE); |
688 |
|
try { |
709 |
– |
NavigableSet q = dset0(); |
710 |
– |
Integer[] ints = new Integer[SIZE]; |
711 |
– |
for (int i = 0; i < SIZE-1; ++i) |
712 |
– |
ints[i] = new Integer(i+SIZE); |
689 |
|
q.addAll(Arrays.asList(ints)); |
690 |
|
shouldThrow(); |
691 |
< |
} |
716 |
< |
catch (NullPointerException success) {} |
691 |
> |
} catch (NullPointerException success) {} |
692 |
|
} |
693 |
|
|
694 |
|
/** |
695 |
|
* Set contains all elements of successful addAll |
696 |
|
*/ |
697 |
|
public void testDescendingAddAll5() { |
698 |
< |
try { |
699 |
< |
Integer[] empty = new Integer[0]; |
700 |
< |
Integer[] ints = new Integer[SIZE]; |
701 |
< |
for (int i = 0; i < SIZE; ++i) |
702 |
< |
ints[i] = new Integer(SIZE-1- i); |
703 |
< |
NavigableSet q = dset0(); |
704 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
705 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
706 |
< |
for (int i = 0; i < SIZE; ++i) |
732 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
733 |
< |
} |
734 |
< |
finally {} |
698 |
> |
Integer[] empty = new Integer[0]; |
699 |
> |
Integer[] ints = new Integer[SIZE]; |
700 |
> |
for (int i = 0; i < SIZE; ++i) |
701 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
702 |
> |
NavigableSet q = dset0(); |
703 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
704 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
705 |
> |
for (int i = 0; i < SIZE; ++i) |
706 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
707 |
|
} |
708 |
|
|
709 |
|
/** |
712 |
|
public void testDescendingPoll() { |
713 |
|
NavigableSet q = populatedSet(SIZE); |
714 |
|
for (int i = 0; i < SIZE; ++i) { |
715 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
715 |
> |
assertEquals(i, q.pollFirst()); |
716 |
|
} |
717 |
< |
assertNull(q.pollFirst()); |
717 |
> |
assertNull(q.pollFirst()); |
718 |
|
} |
719 |
|
|
720 |
|
/** |
722 |
|
*/ |
723 |
|
public void testDescendingRemoveElement() { |
724 |
|
NavigableSet q = populatedSet(SIZE); |
725 |
< |
for (int i = 1; i < SIZE; i+=2) { |
725 |
> |
for (int i = 1; i < SIZE; i += 2) { |
726 |
|
assertTrue(q.remove(new Integer(i))); |
727 |
|
} |
728 |
< |
for (int i = 0; i < SIZE; i+=2) { |
728 |
> |
for (int i = 0; i < SIZE; i += 2) { |
729 |
|
assertTrue(q.remove(new Integer(i))); |
730 |
|
assertFalse(q.remove(new Integer(i+1))); |
731 |
|
} |
752 |
|
q.clear(); |
753 |
|
assertTrue(q.isEmpty()); |
754 |
|
assertEquals(0, q.size()); |
755 |
< |
q.add(new Integer(1)); |
755 |
> |
assertTrue(q.add(new Integer(1))); |
756 |
|
assertFalse(q.isEmpty()); |
757 |
|
q.clear(); |
758 |
|
assertTrue(q.isEmpty()); |
786 |
|
assertTrue(changed); |
787 |
|
|
788 |
|
assertTrue(q.containsAll(p)); |
789 |
< |
assertEquals(SIZE-i, q.size()); |
789 |
> |
assertEquals(SIZE - i, q.size()); |
790 |
|
p.pollFirst(); |
791 |
|
} |
792 |
|
} |
799 |
|
NavigableSet q = populatedSet(SIZE); |
800 |
|
NavigableSet p = populatedSet(i); |
801 |
|
assertTrue(q.removeAll(p)); |
802 |
< |
assertEquals(SIZE-i, q.size()); |
802 |
> |
assertEquals(SIZE - i, q.size()); |
803 |
|
for (int j = 0; j < i; ++j) { |
804 |
< |
Integer I = (Integer)(p.pollFirst()); |
805 |
< |
assertFalse(q.contains(I)); |
804 |
> |
Integer x = (Integer)(p.pollFirst()); |
805 |
> |
assertFalse(q.contains(x)); |
806 |
|
} |
807 |
|
} |
808 |
|
} |
809 |
|
|
838 |
– |
|
839 |
– |
|
810 |
|
/** |
811 |
|
* lower returns preceding element |
812 |
|
*/ |
823 |
|
|
824 |
|
Object e4 = q.lower(zero); |
825 |
|
assertNull(e4); |
856 |
– |
|
826 |
|
} |
827 |
|
|
828 |
|
/** |
841 |
|
|
842 |
|
Object e4 = q.higher(m6); |
843 |
|
assertNull(e4); |
875 |
– |
|
844 |
|
} |
845 |
|
|
846 |
|
/** |
859 |
|
|
860 |
|
Object e4 = q.floor(zero); |
861 |
|
assertNull(e4); |
894 |
– |
|
862 |
|
} |
863 |
|
|
864 |
|
/** |
877 |
|
|
878 |
|
Object e4 = q.ceiling(m6); |
879 |
|
assertNull(e4); |
913 |
– |
|
880 |
|
} |
881 |
|
|
882 |
|
/** |
884 |
|
*/ |
885 |
|
public void testDescendingToArray() { |
886 |
|
NavigableSet q = populatedSet(SIZE); |
887 |
< |
Object[] o = q.toArray(); |
887 |
> |
Object[] o = q.toArray(); |
888 |
|
Arrays.sort(o); |
889 |
< |
for (int i = 0; i < o.length; i++) |
890 |
< |
assertEquals(o[i], q.pollFirst()); |
889 |
> |
for (int i = 0; i < o.length; i++) |
890 |
> |
assertEquals(o[i], q.pollFirst()); |
891 |
|
} |
892 |
|
|
893 |
|
/** |
895 |
|
*/ |
896 |
|
public void testDescendingToArray2() { |
897 |
|
NavigableSet q = populatedSet(SIZE); |
898 |
< |
Integer[] ints = new Integer[SIZE]; |
899 |
< |
ints = (Integer[])q.toArray(ints); |
898 |
> |
Integer[] ints = new Integer[SIZE]; |
899 |
> |
assertSame(ints, q.toArray(ints)); |
900 |
|
Arrays.sort(ints); |
901 |
|
for (int i = 0; i < ints.length; i++) |
902 |
|
assertEquals(ints[i], q.pollFirst()); |
908 |
|
public void testDescendingIterator() { |
909 |
|
NavigableSet q = populatedSet(SIZE); |
910 |
|
int i = 0; |
911 |
< |
Iterator it = q.iterator(); |
911 |
> |
Iterator it = q.iterator(); |
912 |
|
while (it.hasNext()) { |
913 |
|
assertTrue(q.contains(it.next())); |
914 |
|
++i; |
922 |
|
public void testDescendingEmptyIterator() { |
923 |
|
NavigableSet q = dset0(); |
924 |
|
int i = 0; |
925 |
< |
Iterator it = q.iterator(); |
925 |
> |
Iterator it = q.iterator(); |
926 |
|
while (it.hasNext()) { |
927 |
|
assertTrue(q.contains(it.next())); |
928 |
|
++i; |
929 |
|
} |
930 |
< |
assertEquals(i, 0); |
930 |
> |
assertEquals(0, i); |
931 |
|
} |
932 |
|
|
933 |
|
/** |
934 |
|
* iterator.remove removes current element |
935 |
|
*/ |
936 |
< |
public void testDescendingIteratorRemove () { |
936 |
> |
public void testDescendingIteratorRemove() { |
937 |
|
final NavigableSet q = dset0(); |
938 |
|
q.add(new Integer(2)); |
939 |
|
q.add(new Integer(1)); |
944 |
|
it.remove(); |
945 |
|
|
946 |
|
it = q.iterator(); |
947 |
< |
assertEquals(it.next(), new Integer(2)); |
948 |
< |
assertEquals(it.next(), new Integer(3)); |
947 |
> |
assertEquals(2, it.next()); |
948 |
> |
assertEquals(3, it.next()); |
949 |
|
assertFalse(it.hasNext()); |
950 |
|
} |
951 |
|
|
986 |
– |
|
952 |
|
/** |
953 |
|
* toString contains toStrings of elements |
954 |
|
*/ |
956 |
|
NavigableSet q = populatedSet(SIZE); |
957 |
|
String s = q.toString(); |
958 |
|
for (int i = 0; i < SIZE; ++i) { |
959 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
959 |
> |
assertTrue(s.contains(String.valueOf(i))); |
960 |
|
} |
961 |
|
} |
962 |
|
|
963 |
|
/** |
964 |
|
* A deserialized serialized set has same elements |
965 |
|
*/ |
966 |
< |
public void testDescendingSerialization() { |
967 |
< |
NavigableSet q = populatedSet(SIZE); |
968 |
< |
try { |
969 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
970 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
971 |
< |
out.writeObject(q); |
972 |
< |
out.close(); |
973 |
< |
|
974 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
975 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
976 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
977 |
< |
assertEquals(q.size(), r.size()); |
1013 |
< |
while (!q.isEmpty()) |
1014 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
1015 |
< |
} catch (Exception e){ |
1016 |
< |
e.printStackTrace(); |
1017 |
< |
unexpectedException(); |
966 |
> |
public void testDescendingSerialization() throws Exception { |
967 |
> |
NavigableSet x = dset5(); |
968 |
> |
NavigableSet y = serialClone(x); |
969 |
> |
|
970 |
> |
assertNotSame(x, y); |
971 |
> |
assertEquals(x.size(), y.size()); |
972 |
> |
assertEquals(x.toString(), y.toString()); |
973 |
> |
assertEquals(x, y); |
974 |
> |
assertEquals(y, x); |
975 |
> |
while (!x.isEmpty()) { |
976 |
> |
assertFalse(y.isEmpty()); |
977 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
978 |
|
} |
979 |
+ |
assertTrue(y.isEmpty()); |
980 |
|
} |
981 |
|
|
982 |
|
/** |
1098 |
|
assertEquals(4, set.size()); |
1099 |
|
} |
1100 |
|
|
1101 |
+ |
/** |
1102 |
+ |
* addAll is idempotent |
1103 |
+ |
*/ |
1104 |
+ |
public void testAddAll_idempotent() throws Exception { |
1105 |
+ |
Set x = populatedSet(SIZE); |
1106 |
+ |
Set y = new TreeSet(x); |
1107 |
+ |
y.addAll(x); |
1108 |
+ |
assertEquals(x, y); |
1109 |
+ |
assertEquals(y, x); |
1110 |
+ |
} |
1111 |
+ |
|
1112 |
|
} |