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()); |
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 |
|
/** |
429 |
|
public void testIterator() { |
430 |
|
NavigableSet q = populatedSet(SIZE); |
431 |
|
int i = 0; |
432 |
< |
Iterator it = q.iterator(); |
432 |
> |
Iterator it = q.iterator(); |
433 |
|
while (it.hasNext()) { |
434 |
|
assertTrue(q.contains(it.next())); |
435 |
|
++i; |
443 |
|
public void testEmptyIterator() { |
444 |
|
NavigableSet q = set0(); |
445 |
|
int i = 0; |
446 |
< |
Iterator it = q.iterator(); |
446 |
> |
Iterator it = q.iterator(); |
447 |
|
while (it.hasNext()) { |
448 |
|
assertTrue(q.contains(it.next())); |
449 |
|
++i; |
450 |
|
} |
451 |
< |
assertEquals(i, 0); |
451 |
> |
assertEquals(0, i); |
452 |
|
} |
453 |
|
|
454 |
|
/** |
455 |
|
* iterator.remove removes current element |
456 |
|
*/ |
457 |
< |
public void testIteratorRemove () { |
457 |
> |
public void testIteratorRemove() { |
458 |
|
final NavigableSet q = set0(); |
459 |
|
q.add(new Integer(2)); |
460 |
|
q.add(new Integer(1)); |
465 |
|
it.remove(); |
466 |
|
|
467 |
|
it = q.iterator(); |
468 |
< |
assertEquals(it.next(), new Integer(2)); |
469 |
< |
assertEquals(it.next(), new Integer(3)); |
468 |
> |
assertEquals(2, it.next()); |
469 |
> |
assertEquals(3, it.next()); |
470 |
|
assertFalse(it.hasNext()); |
471 |
|
} |
472 |
|
|
479 |
– |
|
473 |
|
/** |
474 |
|
* toString contains toStrings of elements |
475 |
|
*/ |
477 |
|
NavigableSet q = populatedSet(SIZE); |
478 |
|
String s = q.toString(); |
479 |
|
for (int i = 0; i < SIZE; ++i) { |
480 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
480 |
> |
assertTrue(s.contains(String.valueOf(i))); |
481 |
|
} |
482 |
|
} |
483 |
|
|
484 |
|
/** |
485 |
|
* A deserialized serialized set has same elements |
486 |
|
*/ |
487 |
< |
public void testSerialization() { |
488 |
< |
NavigableSet q = populatedSet(SIZE); |
489 |
< |
try { |
490 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
491 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
492 |
< |
out.writeObject(q); |
493 |
< |
out.close(); |
494 |
< |
|
495 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
496 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
497 |
< |
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(); |
487 |
> |
public void testSerialization() throws Exception { |
488 |
> |
NavigableSet x = populatedSet(SIZE); |
489 |
> |
NavigableSet y = serialClone(x); |
490 |
> |
|
491 |
> |
assertNotSame(x, y); |
492 |
> |
assertEquals(x.size(), y.size()); |
493 |
> |
assertEquals(x, y); |
494 |
> |
assertEquals(y, x); |
495 |
> |
while (!x.isEmpty()) { |
496 |
> |
assertFalse(y.isEmpty()); |
497 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
498 |
|
} |
499 |
+ |
assertTrue(y.isEmpty()); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
660 |
|
q.add(new Object()); |
661 |
|
q.add(new Object()); |
662 |
|
shouldThrow(); |
663 |
< |
} |
676 |
< |
catch (ClassCastException success) {} |
663 |
> |
} catch (ClassCastException success) {} |
664 |
|
} |
665 |
|
|
679 |
– |
|
666 |
|
/** |
667 |
|
* addAll(null) throws NPE |
668 |
|
*/ |
671 |
|
NavigableSet q = dset0(); |
672 |
|
q.addAll(null); |
673 |
|
shouldThrow(); |
674 |
< |
} |
689 |
< |
catch (NullPointerException success) {} |
674 |
> |
} catch (NullPointerException success) {} |
675 |
|
} |
676 |
+ |
|
677 |
|
/** |
678 |
|
* addAll of a collection with null elements throws NPE |
679 |
|
*/ |
683 |
|
Integer[] ints = new Integer[SIZE]; |
684 |
|
q.addAll(Arrays.asList(ints)); |
685 |
|
shouldThrow(); |
686 |
< |
} |
701 |
< |
catch (NullPointerException success) {} |
686 |
> |
} catch (NullPointerException success) {} |
687 |
|
} |
688 |
+ |
|
689 |
|
/** |
690 |
|
* addAll of a collection with any null elements throws NPE after |
691 |
|
* possibly adding some elements |
698 |
|
ints[i] = new Integer(i+SIZE); |
699 |
|
q.addAll(Arrays.asList(ints)); |
700 |
|
shouldThrow(); |
701 |
< |
} |
716 |
< |
catch (NullPointerException success) {} |
701 |
> |
} catch (NullPointerException success) {} |
702 |
|
} |
703 |
|
|
704 |
|
/** |
705 |
|
* Set contains all elements of successful addAll |
706 |
|
*/ |
707 |
|
public void testDescendingAddAll5() { |
708 |
< |
try { |
709 |
< |
Integer[] empty = new Integer[0]; |
710 |
< |
Integer[] ints = new Integer[SIZE]; |
711 |
< |
for (int i = 0; i < SIZE; ++i) |
712 |
< |
ints[i] = new Integer(SIZE-1- i); |
713 |
< |
NavigableSet q = dset0(); |
714 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
715 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
716 |
< |
for (int i = 0; i < SIZE; ++i) |
732 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
733 |
< |
} |
734 |
< |
finally {} |
708 |
> |
Integer[] empty = new Integer[0]; |
709 |
> |
Integer[] ints = new Integer[SIZE]; |
710 |
> |
for (int i = 0; i < SIZE; ++i) |
711 |
> |
ints[i] = new Integer(SIZE-1- i); |
712 |
> |
NavigableSet q = dset0(); |
713 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
714 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
715 |
> |
for (int i = 0; i < SIZE; ++i) |
716 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
717 |
|
} |
718 |
|
|
719 |
|
/** |
722 |
|
public void testDescendingPoll() { |
723 |
|
NavigableSet q = populatedSet(SIZE); |
724 |
|
for (int i = 0; i < SIZE; ++i) { |
725 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
725 |
> |
assertEquals(i, q.pollFirst()); |
726 |
|
} |
727 |
< |
assertNull(q.pollFirst()); |
727 |
> |
assertNull(q.pollFirst()); |
728 |
|
} |
729 |
|
|
730 |
|
/** |
732 |
|
*/ |
733 |
|
public void testDescendingRemoveElement() { |
734 |
|
NavigableSet q = populatedSet(SIZE); |
735 |
< |
for (int i = 1; i < SIZE; i+=2) { |
735 |
> |
for (int i = 1; i < SIZE; i += 2) { |
736 |
|
assertTrue(q.remove(new Integer(i))); |
737 |
|
} |
738 |
< |
for (int i = 0; i < SIZE; i+=2) { |
738 |
> |
for (int i = 0; i < SIZE; i += 2) { |
739 |
|
assertTrue(q.remove(new Integer(i))); |
740 |
|
assertFalse(q.remove(new Integer(i+1))); |
741 |
|
} |
762 |
|
q.clear(); |
763 |
|
assertTrue(q.isEmpty()); |
764 |
|
assertEquals(0, q.size()); |
765 |
< |
q.add(new Integer(1)); |
765 |
> |
assertTrue(q.add(new Integer(1))); |
766 |
|
assertFalse(q.isEmpty()); |
767 |
|
q.clear(); |
768 |
|
assertTrue(q.isEmpty()); |
817 |
|
} |
818 |
|
} |
819 |
|
|
838 |
– |
|
839 |
– |
|
820 |
|
/** |
821 |
|
* lower returns preceding element |
822 |
|
*/ |
833 |
|
|
834 |
|
Object e4 = q.lower(zero); |
835 |
|
assertNull(e4); |
856 |
– |
|
836 |
|
} |
837 |
|
|
838 |
|
/** |
851 |
|
|
852 |
|
Object e4 = q.higher(m6); |
853 |
|
assertNull(e4); |
875 |
– |
|
854 |
|
} |
855 |
|
|
856 |
|
/** |
869 |
|
|
870 |
|
Object e4 = q.floor(zero); |
871 |
|
assertNull(e4); |
894 |
– |
|
872 |
|
} |
873 |
|
|
874 |
|
/** |
887 |
|
|
888 |
|
Object e4 = q.ceiling(m6); |
889 |
|
assertNull(e4); |
913 |
– |
|
890 |
|
} |
891 |
|
|
892 |
|
/** |
894 |
|
*/ |
895 |
|
public void testDescendingToArray() { |
896 |
|
NavigableSet q = populatedSet(SIZE); |
897 |
< |
Object[] o = q.toArray(); |
897 |
> |
Object[] o = q.toArray(); |
898 |
|
Arrays.sort(o); |
899 |
< |
for (int i = 0; i < o.length; i++) |
900 |
< |
assertEquals(o[i], q.pollFirst()); |
899 |
> |
for (int i = 0; i < o.length; i++) |
900 |
> |
assertEquals(o[i], q.pollFirst()); |
901 |
|
} |
902 |
|
|
903 |
|
/** |
905 |
|
*/ |
906 |
|
public void testDescendingToArray2() { |
907 |
|
NavigableSet q = populatedSet(SIZE); |
908 |
< |
Integer[] ints = new Integer[SIZE]; |
909 |
< |
ints = (Integer[])q.toArray(ints); |
908 |
> |
Integer[] ints = new Integer[SIZE]; |
909 |
> |
assertSame(ints, q.toArray(ints)); |
910 |
|
Arrays.sort(ints); |
911 |
|
for (int i = 0; i < ints.length; i++) |
912 |
|
assertEquals(ints[i], q.pollFirst()); |
918 |
|
public void testDescendingIterator() { |
919 |
|
NavigableSet q = populatedSet(SIZE); |
920 |
|
int i = 0; |
921 |
< |
Iterator it = q.iterator(); |
921 |
> |
Iterator it = q.iterator(); |
922 |
|
while (it.hasNext()) { |
923 |
|
assertTrue(q.contains(it.next())); |
924 |
|
++i; |
932 |
|
public void testDescendingEmptyIterator() { |
933 |
|
NavigableSet q = dset0(); |
934 |
|
int i = 0; |
935 |
< |
Iterator it = q.iterator(); |
935 |
> |
Iterator it = q.iterator(); |
936 |
|
while (it.hasNext()) { |
937 |
|
assertTrue(q.contains(it.next())); |
938 |
|
++i; |
939 |
|
} |
940 |
< |
assertEquals(i, 0); |
940 |
> |
assertEquals(0, i); |
941 |
|
} |
942 |
|
|
943 |
|
/** |
944 |
|
* iterator.remove removes current element |
945 |
|
*/ |
946 |
< |
public void testDescendingIteratorRemove () { |
946 |
> |
public void testDescendingIteratorRemove() { |
947 |
|
final NavigableSet q = dset0(); |
948 |
|
q.add(new Integer(2)); |
949 |
|
q.add(new Integer(1)); |
954 |
|
it.remove(); |
955 |
|
|
956 |
|
it = q.iterator(); |
957 |
< |
assertEquals(it.next(), new Integer(2)); |
958 |
< |
assertEquals(it.next(), new Integer(3)); |
957 |
> |
assertEquals(2, it.next()); |
958 |
> |
assertEquals(3, it.next()); |
959 |
|
assertFalse(it.hasNext()); |
960 |
|
} |
961 |
|
|
986 |
– |
|
962 |
|
/** |
963 |
|
* toString contains toStrings of elements |
964 |
|
*/ |
966 |
|
NavigableSet q = populatedSet(SIZE); |
967 |
|
String s = q.toString(); |
968 |
|
for (int i = 0; i < SIZE; ++i) { |
969 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
969 |
> |
assertTrue(s.contains(String.valueOf(i))); |
970 |
|
} |
971 |
|
} |
972 |
|
|
973 |
|
/** |
974 |
|
* A deserialized serialized set has same elements |
975 |
|
*/ |
976 |
< |
public void testDescendingSerialization() { |
977 |
< |
NavigableSet q = populatedSet(SIZE); |
978 |
< |
try { |
979 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
980 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
981 |
< |
out.writeObject(q); |
982 |
< |
out.close(); |
983 |
< |
|
984 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
985 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
986 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
987 |
< |
assertEquals(q.size(), r.size()); |
1013 |
< |
while (!q.isEmpty()) |
1014 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
1015 |
< |
} catch (Exception e){ |
1016 |
< |
e.printStackTrace(); |
1017 |
< |
unexpectedException(); |
976 |
> |
public void testDescendingSerialization() throws Exception { |
977 |
> |
NavigableSet x = dset5(); |
978 |
> |
NavigableSet y = serialClone(x); |
979 |
> |
|
980 |
> |
assertNotSame(x, y); |
981 |
> |
assertEquals(x.size(), y.size()); |
982 |
> |
assertEquals(x.toString(), y.toString()); |
983 |
> |
assertEquals(x, y); |
984 |
> |
assertEquals(y, x); |
985 |
> |
while (!x.isEmpty()) { |
986 |
> |
assertFalse(y.isEmpty()); |
987 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
988 |
|
} |
989 |
+ |
assertTrue(y.isEmpty()); |
990 |
|
} |
991 |
|
|
992 |
|
/** |
1108 |
|
assertEquals(4, set.size()); |
1109 |
|
} |
1110 |
|
|
1111 |
+ |
/** |
1112 |
+ |
* addAll is idempotent |
1113 |
+ |
*/ |
1114 |
+ |
public void testAddAll_idempotent() throws Exception { |
1115 |
+ |
Set x = populatedSet(SIZE); |
1116 |
+ |
Set y = new TreeSet(x); |
1117 |
+ |
y.addAll(x); |
1118 |
+ |
assertEquals(x, y); |
1119 |
+ |
assertEquals(y, x); |
1120 |
+ |
} |
1121 |
+ |
|
1122 |
|
} |