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 |
> |
junit.textui.TestRunner.run(suite()); |
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()); |
132 |
|
* add(null) throws NPE |
133 |
|
*/ |
134 |
|
public void testAddNull() { |
135 |
< |
try { |
135 |
> |
try { |
136 |
|
NavigableSet q = set0(); |
137 |
|
q.add(null); |
138 |
|
shouldThrow(); |
139 |
< |
} catch (NullPointerException success) { } |
139 |
> |
} catch (NullPointerException success) {} |
140 |
|
} |
141 |
|
|
142 |
|
/** |
166 |
|
q.add(new Object()); |
167 |
|
q.add(new Object()); |
168 |
|
shouldThrow(); |
169 |
< |
} |
169 |
< |
catch (ClassCastException success) {} |
169 |
> |
} catch (ClassCastException success) {} |
170 |
|
} |
171 |
|
|
172 |
– |
|
172 |
|
/** |
173 |
|
* addAll(null) throws NPE |
174 |
|
*/ |
177 |
|
NavigableSet q = set0(); |
178 |
|
q.addAll(null); |
179 |
|
shouldThrow(); |
180 |
< |
} |
182 |
< |
catch (NullPointerException success) {} |
180 |
> |
} catch (NullPointerException success) {} |
181 |
|
} |
182 |
+ |
|
183 |
|
/** |
184 |
|
* addAll of a collection with null elements throws NPE |
185 |
|
*/ |
189 |
|
Integer[] ints = new Integer[SIZE]; |
190 |
|
q.addAll(Arrays.asList(ints)); |
191 |
|
shouldThrow(); |
192 |
< |
} |
194 |
< |
catch (NullPointerException success) {} |
192 |
> |
} catch (NullPointerException success) {} |
193 |
|
} |
194 |
+ |
|
195 |
|
/** |
196 |
|
* addAll of a collection with any null elements throws NPE after |
197 |
|
* possibly adding some elements |
204 |
|
ints[i] = new Integer(i+SIZE); |
205 |
|
q.addAll(Arrays.asList(ints)); |
206 |
|
shouldThrow(); |
207 |
< |
} |
209 |
< |
catch (NullPointerException success) {} |
207 |
> |
} catch (NullPointerException success) {} |
208 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
|
* Set contains all elements of successful addAll |
212 |
|
*/ |
213 |
|
public void testAddAll5() { |
214 |
< |
try { |
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) |
225 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
226 |
< |
} |
227 |
< |
finally {} |
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) |
222 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
223 |
|
} |
224 |
|
|
225 |
|
/** |
228 |
|
public void testPoll() { |
229 |
|
NavigableSet q = populatedSet(SIZE); |
230 |
|
for (int i = 0; i < SIZE; ++i) { |
231 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
231 |
> |
assertEquals(i, q.pollFirst()); |
232 |
|
} |
233 |
< |
assertNull(q.pollFirst()); |
233 |
> |
assertNull(q.pollFirst()); |
234 |
|
} |
235 |
|
|
236 |
|
/** |
238 |
|
*/ |
239 |
|
public void testRemoveElement() { |
240 |
|
NavigableSet q = populatedSet(SIZE); |
241 |
< |
for (int i = 1; i < SIZE; i+=2) { |
242 |
< |
assertTrue(q.remove(new Integer(i))); |
243 |
< |
} |
244 |
< |
for (int i = 0; i < SIZE; i+=2) { |
245 |
< |
assertTrue(q.remove(new Integer(i))); |
246 |
< |
assertFalse(q.remove(new Integer(i+1))); |
241 |
> |
for (int i = 1; i < SIZE; i += 2) { |
242 |
> |
assertTrue(q.contains(i)); |
243 |
> |
assertTrue(q.remove(i)); |
244 |
> |
assertFalse(q.contains(i)); |
245 |
> |
assertTrue(q.contains(i-1)); |
246 |
> |
} |
247 |
> |
for (int i = 0; i < SIZE; i += 2) { |
248 |
> |
assertTrue(q.contains(i)); |
249 |
> |
assertTrue(q.remove(i)); |
250 |
> |
assertFalse(q.contains(i)); |
251 |
> |
assertFalse(q.remove(i+1)); |
252 |
> |
assertFalse(q.contains(i+1)); |
253 |
|
} |
254 |
|
assertTrue(q.isEmpty()); |
255 |
|
} |
274 |
|
q.clear(); |
275 |
|
assertTrue(q.isEmpty()); |
276 |
|
assertEquals(0, q.size()); |
277 |
< |
q.add(new Integer(1)); |
277 |
> |
assertTrue(q.add(new Integer(1))); |
278 |
|
assertFalse(q.isEmpty()); |
279 |
|
q.clear(); |
280 |
|
assertTrue(q.isEmpty()); |
323 |
|
assertTrue(q.removeAll(p)); |
324 |
|
assertEquals(SIZE-i, q.size()); |
325 |
|
for (int j = 0; j < i; ++j) { |
326 |
< |
Integer I = (Integer)(p.pollFirst()); |
327 |
< |
assertFalse(q.contains(I)); |
326 |
> |
Integer x = (Integer)(p.pollFirst()); |
327 |
> |
assertFalse(q.contains(x)); |
328 |
|
} |
329 |
|
} |
330 |
|
} |
331 |
|
|
331 |
– |
|
332 |
– |
|
332 |
|
/** |
333 |
|
* lower returns preceding element |
334 |
|
*/ |
345 |
|
|
346 |
|
Object e4 = q.lower(zero); |
347 |
|
assertNull(e4); |
349 |
– |
|
348 |
|
} |
349 |
|
|
350 |
|
/** |
363 |
|
|
364 |
|
Object e4 = q.higher(six); |
365 |
|
assertNull(e4); |
368 |
– |
|
366 |
|
} |
367 |
|
|
368 |
|
/** |
381 |
|
|
382 |
|
Object e4 = q.floor(zero); |
383 |
|
assertNull(e4); |
387 |
– |
|
384 |
|
} |
385 |
|
|
386 |
|
/** |
399 |
|
|
400 |
|
Object e4 = q.ceiling(six); |
401 |
|
assertNull(e4); |
406 |
– |
|
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
< |
* toArray contains all elements |
405 |
> |
* toArray contains all elements in sorted order |
406 |
|
*/ |
407 |
|
public void testToArray() { |
408 |
|
NavigableSet q = populatedSet(SIZE); |
409 |
< |
Object[] o = q.toArray(); |
410 |
< |
Arrays.sort(o); |
411 |
< |
for (int i = 0; i < o.length; i++) |
417 |
< |
assertEquals(o[i], q.pollFirst()); |
409 |
> |
Object[] o = q.toArray(); |
410 |
> |
for (int i = 0; i < o.length; i++) |
411 |
> |
assertSame(o[i], q.pollFirst()); |
412 |
|
} |
413 |
|
|
414 |
|
/** |
415 |
< |
* toArray(a) contains all elements |
415 |
> |
* toArray(a) contains all elements in sorted order |
416 |
|
*/ |
417 |
|
public void testToArray2() { |
418 |
< |
NavigableSet q = populatedSet(SIZE); |
419 |
< |
Integer[] ints = new Integer[SIZE]; |
420 |
< |
ints = (Integer[])q.toArray(ints); |
421 |
< |
Arrays.sort(ints); |
418 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
419 |
> |
Integer[] ints = new Integer[SIZE]; |
420 |
> |
Integer[] array = q.toArray(ints); |
421 |
> |
assertSame(ints, array); |
422 |
|
for (int i = 0; i < ints.length; i++) |
423 |
< |
assertEquals(ints[i], q.pollFirst()); |
423 |
> |
assertSame(ints[i], q.pollFirst()); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
428 |
|
*/ |
429 |
|
public void testIterator() { |
430 |
|
NavigableSet q = populatedSet(SIZE); |
431 |
< |
int i = 0; |
432 |
< |
Iterator it = q.iterator(); |
433 |
< |
while (it.hasNext()) { |
431 |
> |
Iterator it = q.iterator(); |
432 |
> |
int i; |
433 |
> |
for (i = 0; it.hasNext(); i++) |
434 |
|
assertTrue(q.contains(it.next())); |
441 |
– |
++i; |
442 |
– |
} |
435 |
|
assertEquals(i, SIZE); |
436 |
+ |
assertIteratorExhausted(it); |
437 |
|
} |
438 |
|
|
439 |
|
/** |
440 |
|
* iterator of empty set has no elements |
441 |
|
*/ |
442 |
|
public void testEmptyIterator() { |
443 |
< |
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); |
443 |
> |
assertIteratorExhausted(set0().iterator()); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
447 |
|
* iterator.remove removes current element |
448 |
|
*/ |
449 |
< |
public void testIteratorRemove () { |
449 |
> |
public void testIteratorRemove() { |
450 |
|
final NavigableSet q = set0(); |
451 |
|
q.add(new Integer(2)); |
452 |
|
q.add(new Integer(1)); |
457 |
|
it.remove(); |
458 |
|
|
459 |
|
it = q.iterator(); |
460 |
< |
assertEquals(it.next(), new Integer(2)); |
461 |
< |
assertEquals(it.next(), new Integer(3)); |
460 |
> |
assertEquals(2, it.next()); |
461 |
> |
assertEquals(3, it.next()); |
462 |
|
assertFalse(it.hasNext()); |
463 |
|
} |
464 |
|
|
479 |
– |
|
465 |
|
/** |
466 |
|
* toString contains toStrings of elements |
467 |
|
*/ |
469 |
|
NavigableSet q = populatedSet(SIZE); |
470 |
|
String s = q.toString(); |
471 |
|
for (int i = 0; i < SIZE; ++i) { |
472 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
472 |
> |
assertTrue(s.contains(String.valueOf(i))); |
473 |
|
} |
474 |
|
} |
475 |
|
|
476 |
|
/** |
477 |
|
* A deserialized serialized set has same elements |
478 |
|
*/ |
479 |
< |
public void testSerialization() { |
480 |
< |
NavigableSet q = populatedSet(SIZE); |
481 |
< |
try { |
482 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
483 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
484 |
< |
out.writeObject(q); |
485 |
< |
out.close(); |
486 |
< |
|
487 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
488 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
489 |
< |
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(); |
479 |
> |
public void testSerialization() throws Exception { |
480 |
> |
NavigableSet x = populatedSet(SIZE); |
481 |
> |
NavigableSet y = serialClone(x); |
482 |
> |
|
483 |
> |
assertNotSame(x, y); |
484 |
> |
assertEquals(x.size(), y.size()); |
485 |
> |
assertEquals(x, y); |
486 |
> |
assertEquals(y, x); |
487 |
> |
while (!x.isEmpty()) { |
488 |
> |
assertFalse(y.isEmpty()); |
489 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
490 |
|
} |
491 |
+ |
assertTrue(y.isEmpty()); |
492 |
|
} |
493 |
|
|
494 |
|
/** |
652 |
|
q.add(new Object()); |
653 |
|
q.add(new Object()); |
654 |
|
shouldThrow(); |
655 |
< |
} |
676 |
< |
catch (ClassCastException success) {} |
655 |
> |
} catch (ClassCastException success) {} |
656 |
|
} |
657 |
|
|
679 |
– |
|
658 |
|
/** |
659 |
|
* addAll(null) throws NPE |
660 |
|
*/ |
663 |
|
NavigableSet q = dset0(); |
664 |
|
q.addAll(null); |
665 |
|
shouldThrow(); |
666 |
< |
} |
689 |
< |
catch (NullPointerException success) {} |
666 |
> |
} catch (NullPointerException success) {} |
667 |
|
} |
668 |
+ |
|
669 |
|
/** |
670 |
|
* addAll of a collection with null elements throws NPE |
671 |
|
*/ |
675 |
|
Integer[] ints = new Integer[SIZE]; |
676 |
|
q.addAll(Arrays.asList(ints)); |
677 |
|
shouldThrow(); |
678 |
< |
} |
701 |
< |
catch (NullPointerException success) {} |
678 |
> |
} catch (NullPointerException success) {} |
679 |
|
} |
680 |
+ |
|
681 |
|
/** |
682 |
|
* addAll of a collection with any null elements throws NPE after |
683 |
|
* possibly adding some elements |
690 |
|
ints[i] = new Integer(i+SIZE); |
691 |
|
q.addAll(Arrays.asList(ints)); |
692 |
|
shouldThrow(); |
693 |
< |
} |
716 |
< |
catch (NullPointerException success) {} |
693 |
> |
} catch (NullPointerException success) {} |
694 |
|
} |
695 |
|
|
696 |
|
/** |
697 |
|
* Set contains all elements of successful addAll |
698 |
|
*/ |
699 |
|
public void testDescendingAddAll5() { |
700 |
< |
try { |
701 |
< |
Integer[] empty = new Integer[0]; |
702 |
< |
Integer[] ints = new Integer[SIZE]; |
703 |
< |
for (int i = 0; i < SIZE; ++i) |
704 |
< |
ints[i] = new Integer(SIZE-1- i); |
705 |
< |
NavigableSet q = dset0(); |
706 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
707 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
708 |
< |
for (int i = 0; i < SIZE; ++i) |
732 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
733 |
< |
} |
734 |
< |
finally {} |
700 |
> |
Integer[] empty = new Integer[0]; |
701 |
> |
Integer[] ints = new Integer[SIZE]; |
702 |
> |
for (int i = 0; i < SIZE; ++i) |
703 |
> |
ints[i] = new Integer(SIZE-1- i); |
704 |
> |
NavigableSet q = dset0(); |
705 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
706 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
707 |
> |
for (int i = 0; i < SIZE; ++i) |
708 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
709 |
|
} |
710 |
|
|
711 |
|
/** |
714 |
|
public void testDescendingPoll() { |
715 |
|
NavigableSet q = populatedSet(SIZE); |
716 |
|
for (int i = 0; i < SIZE; ++i) { |
717 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
717 |
> |
assertEquals(i, q.pollFirst()); |
718 |
|
} |
719 |
< |
assertNull(q.pollFirst()); |
719 |
> |
assertNull(q.pollFirst()); |
720 |
|
} |
721 |
|
|
722 |
|
/** |
724 |
|
*/ |
725 |
|
public void testDescendingRemoveElement() { |
726 |
|
NavigableSet q = populatedSet(SIZE); |
727 |
< |
for (int i = 1; i < SIZE; i+=2) { |
727 |
> |
for (int i = 1; i < SIZE; i += 2) { |
728 |
|
assertTrue(q.remove(new Integer(i))); |
729 |
|
} |
730 |
< |
for (int i = 0; i < SIZE; i+=2) { |
730 |
> |
for (int i = 0; i < SIZE; i += 2) { |
731 |
|
assertTrue(q.remove(new Integer(i))); |
732 |
|
assertFalse(q.remove(new Integer(i+1))); |
733 |
|
} |
754 |
|
q.clear(); |
755 |
|
assertTrue(q.isEmpty()); |
756 |
|
assertEquals(0, q.size()); |
757 |
< |
q.add(new Integer(1)); |
757 |
> |
assertTrue(q.add(new Integer(1))); |
758 |
|
assertFalse(q.isEmpty()); |
759 |
|
q.clear(); |
760 |
|
assertTrue(q.isEmpty()); |
803 |
|
assertTrue(q.removeAll(p)); |
804 |
|
assertEquals(SIZE-i, q.size()); |
805 |
|
for (int j = 0; j < i; ++j) { |
806 |
< |
Integer I = (Integer)(p.pollFirst()); |
807 |
< |
assertFalse(q.contains(I)); |
806 |
> |
Integer x = (Integer)(p.pollFirst()); |
807 |
> |
assertFalse(q.contains(x)); |
808 |
|
} |
809 |
|
} |
810 |
|
} |
811 |
|
|
838 |
– |
|
839 |
– |
|
812 |
|
/** |
813 |
|
* lower returns preceding element |
814 |
|
*/ |
825 |
|
|
826 |
|
Object e4 = q.lower(zero); |
827 |
|
assertNull(e4); |
856 |
– |
|
828 |
|
} |
829 |
|
|
830 |
|
/** |
843 |
|
|
844 |
|
Object e4 = q.higher(m6); |
845 |
|
assertNull(e4); |
875 |
– |
|
846 |
|
} |
847 |
|
|
848 |
|
/** |
861 |
|
|
862 |
|
Object e4 = q.floor(zero); |
863 |
|
assertNull(e4); |
894 |
– |
|
864 |
|
} |
865 |
|
|
866 |
|
/** |
879 |
|
|
880 |
|
Object e4 = q.ceiling(m6); |
881 |
|
assertNull(e4); |
913 |
– |
|
882 |
|
} |
883 |
|
|
884 |
|
/** |
886 |
|
*/ |
887 |
|
public void testDescendingToArray() { |
888 |
|
NavigableSet q = populatedSet(SIZE); |
889 |
< |
Object[] o = q.toArray(); |
889 |
> |
Object[] o = q.toArray(); |
890 |
|
Arrays.sort(o); |
891 |
< |
for (int i = 0; i < o.length; i++) |
892 |
< |
assertEquals(o[i], q.pollFirst()); |
891 |
> |
for (int i = 0; i < o.length; i++) |
892 |
> |
assertEquals(o[i], q.pollFirst()); |
893 |
|
} |
894 |
|
|
895 |
|
/** |
897 |
|
*/ |
898 |
|
public void testDescendingToArray2() { |
899 |
|
NavigableSet q = populatedSet(SIZE); |
900 |
< |
Integer[] ints = new Integer[SIZE]; |
901 |
< |
ints = (Integer[])q.toArray(ints); |
900 |
> |
Integer[] ints = new Integer[SIZE]; |
901 |
> |
assertSame(ints, q.toArray(ints)); |
902 |
|
Arrays.sort(ints); |
903 |
|
for (int i = 0; i < ints.length; i++) |
904 |
|
assertEquals(ints[i], q.pollFirst()); |
910 |
|
public void testDescendingIterator() { |
911 |
|
NavigableSet q = populatedSet(SIZE); |
912 |
|
int i = 0; |
913 |
< |
Iterator it = q.iterator(); |
913 |
> |
Iterator it = q.iterator(); |
914 |
|
while (it.hasNext()) { |
915 |
|
assertTrue(q.contains(it.next())); |
916 |
|
++i; |
924 |
|
public void testDescendingEmptyIterator() { |
925 |
|
NavigableSet q = dset0(); |
926 |
|
int i = 0; |
927 |
< |
Iterator it = q.iterator(); |
927 |
> |
Iterator it = q.iterator(); |
928 |
|
while (it.hasNext()) { |
929 |
|
assertTrue(q.contains(it.next())); |
930 |
|
++i; |
931 |
|
} |
932 |
< |
assertEquals(i, 0); |
932 |
> |
assertEquals(0, i); |
933 |
|
} |
934 |
|
|
935 |
|
/** |
936 |
|
* iterator.remove removes current element |
937 |
|
*/ |
938 |
< |
public void testDescendingIteratorRemove () { |
938 |
> |
public void testDescendingIteratorRemove() { |
939 |
|
final NavigableSet q = dset0(); |
940 |
|
q.add(new Integer(2)); |
941 |
|
q.add(new Integer(1)); |
946 |
|
it.remove(); |
947 |
|
|
948 |
|
it = q.iterator(); |
949 |
< |
assertEquals(it.next(), new Integer(2)); |
950 |
< |
assertEquals(it.next(), new Integer(3)); |
949 |
> |
assertEquals(2, it.next()); |
950 |
> |
assertEquals(3, it.next()); |
951 |
|
assertFalse(it.hasNext()); |
952 |
|
} |
953 |
|
|
986 |
– |
|
954 |
|
/** |
955 |
|
* toString contains toStrings of elements |
956 |
|
*/ |
958 |
|
NavigableSet q = populatedSet(SIZE); |
959 |
|
String s = q.toString(); |
960 |
|
for (int i = 0; i < SIZE; ++i) { |
961 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
961 |
> |
assertTrue(s.contains(String.valueOf(i))); |
962 |
|
} |
963 |
|
} |
964 |
|
|
965 |
|
/** |
966 |
|
* A deserialized serialized set has same elements |
967 |
|
*/ |
968 |
< |
public void testDescendingSerialization() { |
969 |
< |
NavigableSet q = populatedSet(SIZE); |
970 |
< |
try { |
971 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
972 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
973 |
< |
out.writeObject(q); |
974 |
< |
out.close(); |
975 |
< |
|
976 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
977 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
978 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
979 |
< |
assertEquals(q.size(), r.size()); |
1013 |
< |
while (!q.isEmpty()) |
1014 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
1015 |
< |
} catch (Exception e) { |
1016 |
< |
e.printStackTrace(); |
1017 |
< |
unexpectedException(); |
968 |
> |
public void testDescendingSerialization() throws Exception { |
969 |
> |
NavigableSet x = dset5(); |
970 |
> |
NavigableSet y = serialClone(x); |
971 |
> |
|
972 |
> |
assertNotSame(x, y); |
973 |
> |
assertEquals(x.size(), y.size()); |
974 |
> |
assertEquals(x.toString(), y.toString()); |
975 |
> |
assertEquals(x, y); |
976 |
> |
assertEquals(y, x); |
977 |
> |
while (!x.isEmpty()) { |
978 |
> |
assertFalse(y.isEmpty()); |
979 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
980 |
|
} |
981 |
+ |
assertTrue(y.isEmpty()); |
982 |
|
} |
983 |
|
|
984 |
|
/** |
1100 |
|
assertEquals(4, set.size()); |
1101 |
|
} |
1102 |
|
|
1103 |
+ |
/** |
1104 |
+ |
* addAll is idempotent |
1105 |
+ |
*/ |
1106 |
+ |
public void testAddAll_idempotent() throws Exception { |
1107 |
+ |
Set x = populatedSet(SIZE); |
1108 |
+ |
Set y = new TreeSet(x); |
1109 |
+ |
y.addAll(x); |
1110 |
+ |
assertEquals(x, y); |
1111 |
+ |
assertEquals(y, x); |
1112 |
+ |
} |
1113 |
+ |
|
1114 |
|
} |