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