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.*; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.Comparator; |
10 |
> |
import java.util.Iterator; |
11 |
> |
import java.util.NavigableSet; |
12 |
> |
import java.util.SortedSet; |
13 |
> |
import java.util.Set; |
14 |
> |
import java.util.TreeSet; |
15 |
|
|
16 |
|
public class TreeSubSetTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(TreeSubSetTest.class); |
21 |
> |
return new TestSuite(TreeSubSetTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
static class MyReverseComparator implements Comparator { |
25 |
|
public int compare(Object x, Object y) { |
26 |
< |
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; |
26 |
> |
return ((Comparable)y).compareTo(x); |
27 |
|
} |
28 |
|
} |
29 |
|
|
30 |
|
/** |
31 |
< |
* Create a set of given size containing consecutive |
31 |
> |
* Returns a new set of given size containing consecutive |
32 |
|
* Integers 0 ... n. |
33 |
|
*/ |
34 |
< |
private NavigableSet populatedSet(int n) { |
35 |
< |
TreeSet q = new TreeSet(); |
34 |
> |
private NavigableSet<Integer> populatedSet(int n) { |
35 |
> |
TreeSet<Integer> q = new TreeSet<Integer>(); |
36 |
|
assertTrue(q.isEmpty()); |
37 |
|
|
38 |
< |
for (int i = n-1; i >= 0; i-=2) |
39 |
< |
assertTrue(q.add(new Integer(i))); |
40 |
< |
for (int i = (n & 1); i < n; i+=2) |
41 |
< |
assertTrue(q.add(new Integer(i))); |
38 |
> |
for (int i = n-1; i >= 0; i-=2) |
39 |
> |
assertTrue(q.add(new Integer(i))); |
40 |
> |
for (int i = (n & 1); i < n; i+=2) |
41 |
> |
assertTrue(q.add(new Integer(i))); |
42 |
|
assertTrue(q.add(new Integer(-n))); |
43 |
|
assertTrue(q.add(new Integer(n))); |
44 |
|
NavigableSet s = q.subSet(new Integer(0), true, new Integer(n), false); |
45 |
|
assertFalse(s.isEmpty()); |
46 |
< |
assertEquals(n, s.size()); |
46 |
> |
assertEquals(n, s.size()); |
47 |
|
return s; |
48 |
|
} |
49 |
|
|
50 |
|
/** |
51 |
< |
* Create set of first 5 ints |
51 |
> |
* Returns a new set of first 5 ints. |
52 |
|
*/ |
53 |
|
private NavigableSet set5() { |
54 |
|
TreeSet q = new TreeSet(); |
61 |
|
q.add(zero); |
62 |
|
q.add(seven); |
63 |
|
NavigableSet s = q.subSet(one, true, seven, false); |
64 |
< |
assertEquals(5, s.size()); |
64 |
> |
assertEquals(5, s.size()); |
65 |
|
return s; |
66 |
|
} |
67 |
|
|
74 |
|
q.add(m4); |
75 |
|
q.add(m5); |
76 |
|
NavigableSet s = q.descendingSet(); |
77 |
< |
assertEquals(5, s.size()); |
77 |
> |
assertEquals(5, s.size()); |
78 |
|
return s; |
79 |
|
} |
80 |
|
|
81 |
|
private static NavigableSet set0() { |
82 |
< |
TreeSet set = new TreeSet(); |
82 |
> |
TreeSet set = new TreeSet(); |
83 |
|
assertTrue(set.isEmpty()); |
84 |
|
return set.tailSet(m1, false); |
85 |
|
} |
86 |
|
|
87 |
|
private static NavigableSet dset0() { |
88 |
< |
TreeSet set = new TreeSet(); |
88 |
> |
TreeSet set = new TreeSet(); |
89 |
|
assertTrue(set.isEmpty()); |
90 |
|
return set; |
91 |
|
} |
97 |
|
assertEquals(0, set0().size()); |
98 |
|
} |
99 |
|
|
100 |
– |
|
100 |
|
/** |
101 |
|
* isEmpty is true before add, false after |
102 |
|
*/ |
103 |
|
public void testEmpty() { |
104 |
|
NavigableSet q = set0(); |
105 |
|
assertTrue(q.isEmpty()); |
106 |
< |
q.add(new Integer(1)); |
106 |
> |
assertTrue(q.add(new Integer(1))); |
107 |
|
assertFalse(q.isEmpty()); |
108 |
< |
q.add(new Integer(2)); |
108 |
> |
assertTrue(q.add(new Integer(2))); |
109 |
|
q.pollFirst(); |
110 |
|
q.pollFirst(); |
111 |
|
assertTrue(q.isEmpty()); |
130 |
|
* add(null) throws NPE |
131 |
|
*/ |
132 |
|
public void testAddNull() { |
133 |
< |
try { |
133 |
> |
try { |
134 |
|
NavigableSet q = set0(); |
135 |
|
q.add(null); |
136 |
|
shouldThrow(); |
137 |
< |
} catch (NullPointerException success) { } |
137 |
> |
} catch (NullPointerException success) {} |
138 |
|
} |
139 |
|
|
140 |
|
/** |
164 |
|
q.add(new Object()); |
165 |
|
q.add(new Object()); |
166 |
|
shouldThrow(); |
167 |
< |
} |
169 |
< |
catch (ClassCastException success) {} |
167 |
> |
} catch (ClassCastException success) {} |
168 |
|
} |
169 |
|
|
172 |
– |
|
170 |
|
/** |
171 |
|
* addAll(null) throws NPE |
172 |
|
*/ |
175 |
|
NavigableSet q = set0(); |
176 |
|
q.addAll(null); |
177 |
|
shouldThrow(); |
178 |
< |
} |
182 |
< |
catch (NullPointerException success) {} |
178 |
> |
} catch (NullPointerException success) {} |
179 |
|
} |
180 |
+ |
|
181 |
|
/** |
182 |
|
* addAll of a collection with null elements throws NPE |
183 |
|
*/ |
187 |
|
Integer[] ints = new Integer[SIZE]; |
188 |
|
q.addAll(Arrays.asList(ints)); |
189 |
|
shouldThrow(); |
190 |
< |
} |
194 |
< |
catch (NullPointerException success) {} |
190 |
> |
} catch (NullPointerException success) {} |
191 |
|
} |
192 |
+ |
|
193 |
|
/** |
194 |
|
* addAll of a collection with any null elements throws NPE after |
195 |
|
* possibly adding some elements |
202 |
|
ints[i] = new Integer(i+SIZE); |
203 |
|
q.addAll(Arrays.asList(ints)); |
204 |
|
shouldThrow(); |
205 |
< |
} |
209 |
< |
catch (NullPointerException success) {} |
205 |
> |
} catch (NullPointerException success) {} |
206 |
|
} |
207 |
|
|
208 |
|
/** |
209 |
|
* Set contains all elements of successful addAll |
210 |
|
*/ |
211 |
|
public void testAddAll5() { |
212 |
< |
try { |
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) |
225 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
226 |
< |
} |
227 |
< |
finally {} |
212 |
> |
Integer[] empty = new Integer[0]; |
213 |
> |
Integer[] ints = new Integer[SIZE]; |
214 |
> |
for (int i = 0; i < SIZE; ++i) |
215 |
> |
ints[i] = new Integer(SIZE-1- i); |
216 |
> |
NavigableSet q = set0(); |
217 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
218 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
219 |
> |
for (int i = 0; i < SIZE; ++i) |
220 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
221 |
|
} |
222 |
|
|
223 |
|
/** |
226 |
|
public void testPoll() { |
227 |
|
NavigableSet q = populatedSet(SIZE); |
228 |
|
for (int i = 0; i < SIZE; ++i) { |
229 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
229 |
> |
assertEquals(i, q.pollFirst()); |
230 |
|
} |
231 |
< |
assertNull(q.pollFirst()); |
231 |
> |
assertNull(q.pollFirst()); |
232 |
|
} |
233 |
|
|
234 |
|
/** |
237 |
|
public void testRemoveElement() { |
238 |
|
NavigableSet q = populatedSet(SIZE); |
239 |
|
for (int i = 1; i < SIZE; i+=2) { |
240 |
< |
assertTrue(q.remove(new Integer(i))); |
240 |
> |
assertTrue(q.contains(i)); |
241 |
> |
assertTrue(q.remove(i)); |
242 |
> |
assertFalse(q.contains(i)); |
243 |
> |
assertTrue(q.contains(i-1)); |
244 |
|
} |
245 |
|
for (int i = 0; i < SIZE; i+=2) { |
246 |
< |
assertTrue(q.remove(new Integer(i))); |
247 |
< |
assertFalse(q.remove(new Integer(i+1))); |
246 |
> |
assertTrue(q.contains(i)); |
247 |
> |
assertTrue(q.remove(i)); |
248 |
> |
assertFalse(q.contains(i)); |
249 |
> |
assertFalse(q.remove(i+1)); |
250 |
> |
assertFalse(q.contains(i+1)); |
251 |
|
} |
252 |
|
assertTrue(q.isEmpty()); |
253 |
|
} |
272 |
|
q.clear(); |
273 |
|
assertTrue(q.isEmpty()); |
274 |
|
assertEquals(0, q.size()); |
275 |
< |
q.add(new Integer(1)); |
275 |
> |
assertTrue(q.add(new Integer(1))); |
276 |
|
assertFalse(q.isEmpty()); |
277 |
|
q.clear(); |
278 |
|
assertTrue(q.isEmpty()); |
327 |
|
} |
328 |
|
} |
329 |
|
|
331 |
– |
|
332 |
– |
|
330 |
|
/** |
331 |
|
* lower returns preceding element |
332 |
|
*/ |
343 |
|
|
344 |
|
Object e4 = q.lower(zero); |
345 |
|
assertNull(e4); |
349 |
– |
|
346 |
|
} |
347 |
|
|
348 |
|
/** |
361 |
|
|
362 |
|
Object e4 = q.higher(six); |
363 |
|
assertNull(e4); |
368 |
– |
|
364 |
|
} |
365 |
|
|
366 |
|
/** |
379 |
|
|
380 |
|
Object e4 = q.floor(zero); |
381 |
|
assertNull(e4); |
387 |
– |
|
382 |
|
} |
383 |
|
|
384 |
|
/** |
397 |
|
|
398 |
|
Object e4 = q.ceiling(six); |
399 |
|
assertNull(e4); |
406 |
– |
|
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
< |
* toArray contains all elements |
403 |
> |
* toArray contains all elements in sorted order |
404 |
|
*/ |
405 |
|
public void testToArray() { |
406 |
|
NavigableSet q = populatedSet(SIZE); |
407 |
< |
Object[] o = q.toArray(); |
408 |
< |
Arrays.sort(o); |
409 |
< |
for (int i = 0; i < o.length; i++) |
417 |
< |
assertEquals(o[i], q.pollFirst()); |
407 |
> |
Object[] o = q.toArray(); |
408 |
> |
for (int i = 0; i < o.length; i++) |
409 |
> |
assertSame(o[i], q.pollFirst()); |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
< |
* toArray(a) contains all elements |
413 |
> |
* toArray(a) contains all elements in sorted order |
414 |
|
*/ |
415 |
|
public void testToArray2() { |
416 |
< |
NavigableSet q = populatedSet(SIZE); |
417 |
< |
Integer[] ints = new Integer[SIZE]; |
418 |
< |
ints = (Integer[])q.toArray(ints); |
419 |
< |
Arrays.sort(ints); |
416 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
417 |
> |
Integer[] ints = new Integer[SIZE]; |
418 |
> |
Integer[] array = q.toArray(ints); |
419 |
> |
assertSame(ints, array); |
420 |
|
for (int i = 0; i < ints.length; i++) |
421 |
< |
assertEquals(ints[i], q.pollFirst()); |
421 |
> |
assertSame(ints[i], q.pollFirst()); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
427 |
|
public void testIterator() { |
428 |
|
NavigableSet q = populatedSet(SIZE); |
429 |
|
int i = 0; |
430 |
< |
Iterator it = q.iterator(); |
430 |
> |
Iterator it = q.iterator(); |
431 |
|
while (it.hasNext()) { |
432 |
|
assertTrue(q.contains(it.next())); |
433 |
|
++i; |
441 |
|
public void testEmptyIterator() { |
442 |
|
NavigableSet q = set0(); |
443 |
|
int i = 0; |
444 |
< |
Iterator it = q.iterator(); |
444 |
> |
Iterator it = q.iterator(); |
445 |
|
while (it.hasNext()) { |
446 |
|
assertTrue(q.contains(it.next())); |
447 |
|
++i; |
448 |
|
} |
449 |
< |
assertEquals(i, 0); |
449 |
> |
assertEquals(0, i); |
450 |
|
} |
451 |
|
|
452 |
|
/** |
453 |
|
* iterator.remove removes current element |
454 |
|
*/ |
455 |
< |
public void testIteratorRemove () { |
455 |
> |
public void testIteratorRemove() { |
456 |
|
final NavigableSet q = set0(); |
457 |
|
q.add(new Integer(2)); |
458 |
|
q.add(new Integer(1)); |
463 |
|
it.remove(); |
464 |
|
|
465 |
|
it = q.iterator(); |
466 |
< |
assertEquals(it.next(), new Integer(2)); |
467 |
< |
assertEquals(it.next(), new Integer(3)); |
466 |
> |
assertEquals(2, it.next()); |
467 |
> |
assertEquals(3, it.next()); |
468 |
|
assertFalse(it.hasNext()); |
469 |
|
} |
470 |
|
|
479 |
– |
|
471 |
|
/** |
472 |
|
* toString contains toStrings of elements |
473 |
|
*/ |
475 |
|
NavigableSet q = populatedSet(SIZE); |
476 |
|
String s = q.toString(); |
477 |
|
for (int i = 0; i < SIZE; ++i) { |
478 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
478 |
> |
assertTrue(s.contains(String.valueOf(i))); |
479 |
|
} |
480 |
|
} |
481 |
|
|
482 |
|
/** |
483 |
|
* A deserialized serialized set has same elements |
484 |
|
*/ |
485 |
< |
public void testSerialization() { |
486 |
< |
NavigableSet q = populatedSet(SIZE); |
487 |
< |
try { |
488 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
489 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
490 |
< |
out.writeObject(q); |
491 |
< |
out.close(); |
492 |
< |
|
493 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
494 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
495 |
< |
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(); |
485 |
> |
public void testSerialization() throws Exception { |
486 |
> |
NavigableSet x = populatedSet(SIZE); |
487 |
> |
NavigableSet y = serialClone(x); |
488 |
> |
|
489 |
> |
assertNotSame(x, y); |
490 |
> |
assertEquals(x.size(), y.size()); |
491 |
> |
assertEquals(x, y); |
492 |
> |
assertEquals(y, x); |
493 |
> |
while (!x.isEmpty()) { |
494 |
> |
assertFalse(y.isEmpty()); |
495 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
496 |
|
} |
497 |
+ |
assertTrue(y.isEmpty()); |
498 |
|
} |
499 |
|
|
500 |
|
/** |
658 |
|
q.add(new Object()); |
659 |
|
q.add(new Object()); |
660 |
|
shouldThrow(); |
661 |
< |
} |
676 |
< |
catch (ClassCastException success) {} |
661 |
> |
} catch (ClassCastException success) {} |
662 |
|
} |
663 |
|
|
679 |
– |
|
664 |
|
/** |
665 |
|
* addAll(null) throws NPE |
666 |
|
*/ |
669 |
|
NavigableSet q = dset0(); |
670 |
|
q.addAll(null); |
671 |
|
shouldThrow(); |
672 |
< |
} |
689 |
< |
catch (NullPointerException success) {} |
672 |
> |
} catch (NullPointerException success) {} |
673 |
|
} |
674 |
+ |
|
675 |
|
/** |
676 |
|
* addAll of a collection with null elements throws NPE |
677 |
|
*/ |
681 |
|
Integer[] ints = new Integer[SIZE]; |
682 |
|
q.addAll(Arrays.asList(ints)); |
683 |
|
shouldThrow(); |
684 |
< |
} |
701 |
< |
catch (NullPointerException success) {} |
684 |
> |
} catch (NullPointerException success) {} |
685 |
|
} |
686 |
+ |
|
687 |
|
/** |
688 |
|
* addAll of a collection with any null elements throws NPE after |
689 |
|
* possibly adding some elements |
696 |
|
ints[i] = new Integer(i+SIZE); |
697 |
|
q.addAll(Arrays.asList(ints)); |
698 |
|
shouldThrow(); |
699 |
< |
} |
716 |
< |
catch (NullPointerException success) {} |
699 |
> |
} catch (NullPointerException success) {} |
700 |
|
} |
701 |
|
|
702 |
|
/** |
703 |
|
* Set contains all elements of successful addAll |
704 |
|
*/ |
705 |
|
public void testDescendingAddAll5() { |
706 |
< |
try { |
707 |
< |
Integer[] empty = new Integer[0]; |
708 |
< |
Integer[] ints = new Integer[SIZE]; |
709 |
< |
for (int i = 0; i < SIZE; ++i) |
710 |
< |
ints[i] = new Integer(SIZE-1- i); |
711 |
< |
NavigableSet q = dset0(); |
712 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
713 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
714 |
< |
for (int i = 0; i < SIZE; ++i) |
732 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
733 |
< |
} |
734 |
< |
finally {} |
706 |
> |
Integer[] empty = new Integer[0]; |
707 |
> |
Integer[] ints = new Integer[SIZE]; |
708 |
> |
for (int i = 0; i < SIZE; ++i) |
709 |
> |
ints[i] = new Integer(SIZE-1- i); |
710 |
> |
NavigableSet q = dset0(); |
711 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
712 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
713 |
> |
for (int i = 0; i < SIZE; ++i) |
714 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
715 |
|
} |
716 |
|
|
717 |
|
/** |
720 |
|
public void testDescendingPoll() { |
721 |
|
NavigableSet q = populatedSet(SIZE); |
722 |
|
for (int i = 0; i < SIZE; ++i) { |
723 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
723 |
> |
assertEquals(i, q.pollFirst()); |
724 |
|
} |
725 |
< |
assertNull(q.pollFirst()); |
725 |
> |
assertNull(q.pollFirst()); |
726 |
|
} |
727 |
|
|
728 |
|
/** |
760 |
|
q.clear(); |
761 |
|
assertTrue(q.isEmpty()); |
762 |
|
assertEquals(0, q.size()); |
763 |
< |
q.add(new Integer(1)); |
763 |
> |
assertTrue(q.add(new Integer(1))); |
764 |
|
assertFalse(q.isEmpty()); |
765 |
|
q.clear(); |
766 |
|
assertTrue(q.isEmpty()); |
815 |
|
} |
816 |
|
} |
817 |
|
|
838 |
– |
|
839 |
– |
|
818 |
|
/** |
819 |
|
* lower returns preceding element |
820 |
|
*/ |
831 |
|
|
832 |
|
Object e4 = q.lower(zero); |
833 |
|
assertNull(e4); |
856 |
– |
|
834 |
|
} |
835 |
|
|
836 |
|
/** |
849 |
|
|
850 |
|
Object e4 = q.higher(m6); |
851 |
|
assertNull(e4); |
875 |
– |
|
852 |
|
} |
853 |
|
|
854 |
|
/** |
867 |
|
|
868 |
|
Object e4 = q.floor(zero); |
869 |
|
assertNull(e4); |
894 |
– |
|
870 |
|
} |
871 |
|
|
872 |
|
/** |
885 |
|
|
886 |
|
Object e4 = q.ceiling(m6); |
887 |
|
assertNull(e4); |
913 |
– |
|
888 |
|
} |
889 |
|
|
890 |
|
/** |
892 |
|
*/ |
893 |
|
public void testDescendingToArray() { |
894 |
|
NavigableSet q = populatedSet(SIZE); |
895 |
< |
Object[] o = q.toArray(); |
895 |
> |
Object[] o = q.toArray(); |
896 |
|
Arrays.sort(o); |
897 |
< |
for (int i = 0; i < o.length; i++) |
898 |
< |
assertEquals(o[i], q.pollFirst()); |
897 |
> |
for (int i = 0; i < o.length; i++) |
898 |
> |
assertEquals(o[i], q.pollFirst()); |
899 |
|
} |
900 |
|
|
901 |
|
/** |
903 |
|
*/ |
904 |
|
public void testDescendingToArray2() { |
905 |
|
NavigableSet q = populatedSet(SIZE); |
906 |
< |
Integer[] ints = new Integer[SIZE]; |
907 |
< |
ints = (Integer[])q.toArray(ints); |
906 |
> |
Integer[] ints = new Integer[SIZE]; |
907 |
> |
assertSame(ints, q.toArray(ints)); |
908 |
|
Arrays.sort(ints); |
909 |
|
for (int i = 0; i < ints.length; i++) |
910 |
|
assertEquals(ints[i], q.pollFirst()); |
916 |
|
public void testDescendingIterator() { |
917 |
|
NavigableSet q = populatedSet(SIZE); |
918 |
|
int i = 0; |
919 |
< |
Iterator it = q.iterator(); |
919 |
> |
Iterator it = q.iterator(); |
920 |
|
while (it.hasNext()) { |
921 |
|
assertTrue(q.contains(it.next())); |
922 |
|
++i; |
930 |
|
public void testDescendingEmptyIterator() { |
931 |
|
NavigableSet q = dset0(); |
932 |
|
int i = 0; |
933 |
< |
Iterator it = q.iterator(); |
933 |
> |
Iterator it = q.iterator(); |
934 |
|
while (it.hasNext()) { |
935 |
|
assertTrue(q.contains(it.next())); |
936 |
|
++i; |
937 |
|
} |
938 |
< |
assertEquals(i, 0); |
938 |
> |
assertEquals(0, i); |
939 |
|
} |
940 |
|
|
941 |
|
/** |
942 |
|
* iterator.remove removes current element |
943 |
|
*/ |
944 |
< |
public void testDescendingIteratorRemove () { |
944 |
> |
public void testDescendingIteratorRemove() { |
945 |
|
final NavigableSet q = dset0(); |
946 |
|
q.add(new Integer(2)); |
947 |
|
q.add(new Integer(1)); |
952 |
|
it.remove(); |
953 |
|
|
954 |
|
it = q.iterator(); |
955 |
< |
assertEquals(it.next(), new Integer(2)); |
956 |
< |
assertEquals(it.next(), new Integer(3)); |
955 |
> |
assertEquals(2, it.next()); |
956 |
> |
assertEquals(3, it.next()); |
957 |
|
assertFalse(it.hasNext()); |
958 |
|
} |
959 |
|
|
986 |
– |
|
960 |
|
/** |
961 |
|
* toString contains toStrings of elements |
962 |
|
*/ |
964 |
|
NavigableSet q = populatedSet(SIZE); |
965 |
|
String s = q.toString(); |
966 |
|
for (int i = 0; i < SIZE; ++i) { |
967 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
967 |
> |
assertTrue(s.contains(String.valueOf(i))); |
968 |
|
} |
969 |
|
} |
970 |
|
|
971 |
|
/** |
972 |
|
* A deserialized serialized set has same elements |
973 |
|
*/ |
974 |
< |
public void testDescendingSerialization() { |
975 |
< |
NavigableSet q = populatedSet(SIZE); |
976 |
< |
try { |
977 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
978 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
979 |
< |
out.writeObject(q); |
980 |
< |
out.close(); |
981 |
< |
|
982 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
983 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
984 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
985 |
< |
assertEquals(q.size(), r.size()); |
1013 |
< |
while (!q.isEmpty()) |
1014 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
1015 |
< |
} catch (Exception e){ |
1016 |
< |
e.printStackTrace(); |
1017 |
< |
unexpectedException(); |
974 |
> |
public void testDescendingSerialization() throws Exception { |
975 |
> |
NavigableSet x = dset5(); |
976 |
> |
NavigableSet y = serialClone(x); |
977 |
> |
|
978 |
> |
assertNotSame(x, y); |
979 |
> |
assertEquals(x.size(), y.size()); |
980 |
> |
assertEquals(x.toString(), y.toString()); |
981 |
> |
assertEquals(x, y); |
982 |
> |
assertEquals(y, x); |
983 |
> |
while (!x.isEmpty()) { |
984 |
> |
assertFalse(y.isEmpty()); |
985 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
986 |
|
} |
987 |
+ |
assertTrue(y.isEmpty()); |
988 |
|
} |
989 |
|
|
990 |
|
/** |
1106 |
|
assertEquals(4, set.size()); |
1107 |
|
} |
1108 |
|
|
1109 |
+ |
/** |
1110 |
+ |
* addAll is idempotent |
1111 |
+ |
*/ |
1112 |
+ |
public void testAddAll_idempotent() throws Exception { |
1113 |
+ |
Set x = populatedSet(SIZE); |
1114 |
+ |
Set y = new TreeSet(x); |
1115 |
+ |
y.addAll(x); |
1116 |
+ |
assertEquals(x, y); |
1117 |
+ |
assertEquals(y, x); |
1118 |
+ |
} |
1119 |
+ |
|
1120 |
|
} |