11 |
|
|
12 |
|
public class TreeSetTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
< |
return new TestSuite(TreeSetTest.class); |
17 |
> |
return new TestSuite(TreeSetTest.class); |
18 |
|
} |
19 |
|
|
20 |
< |
static class MyReverseComparator implements Comparator { |
20 |
> |
static class MyReverseComparator implements Comparator { |
21 |
|
public int compare(Object x, Object y) { |
22 |
< |
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; |
22 |
> |
return ((Comparable)y).compareTo(x); |
23 |
|
} |
24 |
|
} |
25 |
|
|
35 |
|
private TreeSet populatedSet(int n) { |
36 |
|
TreeSet q = new TreeSet(); |
37 |
|
assertTrue(q.isEmpty()); |
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 |
|
assertFalse(q.isEmpty()); |
43 |
< |
assertEquals(n, q.size()); |
43 |
> |
assertEquals(n, q.size()); |
44 |
|
return q; |
45 |
|
} |
46 |
|
|
55 |
|
q.add(three); |
56 |
|
q.add(four); |
57 |
|
q.add(five); |
58 |
< |
assertEquals(5, q.size()); |
58 |
> |
assertEquals(5, q.size()); |
59 |
|
return q; |
60 |
|
} |
61 |
< |
|
61 |
> |
|
62 |
|
/** |
63 |
|
* A new set has unbounded capacity |
64 |
|
*/ |
73 |
|
try { |
74 |
|
TreeSet q = new TreeSet((Collection)null); |
75 |
|
shouldThrow(); |
76 |
< |
} |
81 |
< |
catch (NullPointerException success) {} |
76 |
> |
} catch (NullPointerException success) {} |
77 |
|
} |
78 |
|
|
79 |
|
/** |
84 |
|
Integer[] ints = new Integer[SIZE]; |
85 |
|
TreeSet q = new TreeSet(Arrays.asList(ints)); |
86 |
|
shouldThrow(); |
87 |
< |
} |
93 |
< |
catch (NullPointerException success) {} |
87 |
> |
} catch (NullPointerException success) {} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
97 |
|
ints[i] = new Integer(i); |
98 |
|
TreeSet q = new TreeSet(Arrays.asList(ints)); |
99 |
|
shouldThrow(); |
100 |
< |
} |
107 |
< |
catch (NullPointerException success) {} |
100 |
> |
} catch (NullPointerException success) {} |
101 |
|
} |
102 |
|
|
103 |
|
/** |
104 |
|
* Set contains all elements of collection used to initialize |
105 |
|
*/ |
106 |
|
public void testConstructor6() { |
107 |
< |
try { |
108 |
< |
Integer[] ints = new Integer[SIZE]; |
109 |
< |
for (int i = 0; i < SIZE; ++i) |
110 |
< |
ints[i] = new Integer(i); |
111 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
112 |
< |
for (int i = 0; i < SIZE; ++i) |
120 |
< |
assertEquals(ints[i], q.pollFirst()); |
121 |
< |
} |
122 |
< |
finally {} |
107 |
> |
Integer[] ints = new Integer[SIZE]; |
108 |
> |
for (int i = 0; i < SIZE; ++i) |
109 |
> |
ints[i] = new Integer(i); |
110 |
> |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
111 |
> |
for (int i = 0; i < SIZE; ++i) |
112 |
> |
assertEquals(ints[i], q.pollFirst()); |
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
|
* The comparator used in constructor is used |
117 |
|
*/ |
118 |
|
public void testConstructor7() { |
119 |
< |
try { |
120 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
121 |
< |
TreeSet q = new TreeSet(cmp); |
122 |
< |
assertEquals(cmp, q.comparator()); |
123 |
< |
Integer[] ints = new Integer[SIZE]; |
124 |
< |
for (int i = 0; i < SIZE; ++i) |
125 |
< |
ints[i] = new Integer(i); |
126 |
< |
q.addAll(Arrays.asList(ints)); |
127 |
< |
for (int i = SIZE-1; i >= 0; --i) |
138 |
< |
assertEquals(ints[i], q.pollFirst()); |
139 |
< |
} |
140 |
< |
finally {} |
119 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
120 |
> |
TreeSet q = new TreeSet(cmp); |
121 |
> |
assertEquals(cmp, q.comparator()); |
122 |
> |
Integer[] ints = new Integer[SIZE]; |
123 |
> |
for (int i = 0; i < SIZE; ++i) |
124 |
> |
ints[i] = new Integer(i); |
125 |
> |
q.addAll(Arrays.asList(ints)); |
126 |
> |
for (int i = SIZE-1; i >= 0; --i) |
127 |
> |
assertEquals(ints[i], q.pollFirst()); |
128 |
|
} |
129 |
|
|
130 |
|
/** |
160 |
|
* add(null) throws NPE if nonempty |
161 |
|
*/ |
162 |
|
public void testAddNull() { |
163 |
< |
try { |
163 |
> |
try { |
164 |
|
TreeSet q = populatedSet(SIZE); |
165 |
|
q.add(null); |
166 |
|
shouldThrow(); |
167 |
< |
} catch (NullPointerException success) { } |
167 |
> |
} catch (NullPointerException success) {} |
168 |
|
} |
169 |
|
|
170 |
|
/** |
195 |
|
q.add(new Object()); |
196 |
|
q.add(new Object()); |
197 |
|
shouldThrow(); |
198 |
< |
} |
212 |
< |
catch(ClassCastException success) {} |
198 |
> |
} catch (ClassCastException success) {} |
199 |
|
} |
200 |
|
|
201 |
|
/** |
206 |
|
TreeSet q = new TreeSet(); |
207 |
|
q.addAll(null); |
208 |
|
shouldThrow(); |
209 |
< |
} |
224 |
< |
catch (NullPointerException success) {} |
209 |
> |
} catch (NullPointerException success) {} |
210 |
|
} |
211 |
|
/** |
212 |
|
* addAll of a collection with null elements throws NPE |
217 |
|
Integer[] ints = new Integer[SIZE]; |
218 |
|
q.addAll(Arrays.asList(ints)); |
219 |
|
shouldThrow(); |
220 |
< |
} |
236 |
< |
catch (NullPointerException success) {} |
220 |
> |
} catch (NullPointerException success) {} |
221 |
|
} |
222 |
|
/** |
223 |
|
* addAll of a collection with any null elements throws NPE after |
231 |
|
ints[i] = new Integer(i); |
232 |
|
q.addAll(Arrays.asList(ints)); |
233 |
|
shouldThrow(); |
234 |
< |
} |
251 |
< |
catch (NullPointerException success) {} |
234 |
> |
} catch (NullPointerException success) {} |
235 |
|
} |
236 |
|
|
237 |
|
/** |
238 |
|
* Set contains all elements of successful addAll |
239 |
|
*/ |
240 |
|
public void testAddAll5() { |
241 |
< |
try { |
242 |
< |
Integer[] empty = new Integer[0]; |
243 |
< |
Integer[] ints = new Integer[SIZE]; |
244 |
< |
for (int i = 0; i < SIZE; ++i) |
245 |
< |
ints[i] = new Integer(SIZE-1-i); |
246 |
< |
TreeSet q = new TreeSet(); |
247 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
248 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
249 |
< |
for (int i = 0; i < SIZE; ++i) |
267 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
268 |
< |
} |
269 |
< |
finally {} |
241 |
> |
Integer[] empty = new Integer[0]; |
242 |
> |
Integer[] ints = new Integer[SIZE]; |
243 |
> |
for (int i = 0; i < SIZE; ++i) |
244 |
> |
ints[i] = new Integer(SIZE-1-i); |
245 |
> |
TreeSet q = new TreeSet(); |
246 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
247 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
248 |
> |
for (int i = 0; i < SIZE; ++i) |
249 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
255 |
|
public void testPollFirst() { |
256 |
|
TreeSet q = populatedSet(SIZE); |
257 |
|
for (int i = 0; i < SIZE; ++i) { |
258 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
258 |
> |
assertEquals(i, q.pollFirst()); |
259 |
|
} |
260 |
< |
assertNull(q.pollFirst()); |
260 |
> |
assertNull(q.pollFirst()); |
261 |
|
} |
262 |
|
|
263 |
|
/** |
266 |
|
public void testPollLast() { |
267 |
|
TreeSet q = populatedSet(SIZE); |
268 |
|
for (int i = SIZE-1; i >= 0; --i) { |
269 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
269 |
> |
assertEquals(i, q.pollLast()); |
270 |
|
} |
271 |
< |
assertNull(q.pollFirst()); |
271 |
> |
assertNull(q.pollFirst()); |
272 |
|
} |
273 |
|
|
274 |
|
|
286 |
|
} |
287 |
|
assertTrue(q.isEmpty()); |
288 |
|
} |
289 |
< |
|
289 |
> |
|
290 |
|
/** |
291 |
|
* contains(x) reports true when elements added but not yet removed |
292 |
|
*/ |
362 |
|
} |
363 |
|
} |
364 |
|
|
365 |
< |
|
365 |
> |
|
366 |
|
|
367 |
|
/** |
368 |
|
* lower returns preceding element |
380 |
|
|
381 |
|
Object e4 = q.lower(zero); |
382 |
|
assertNull(e4); |
403 |
– |
|
383 |
|
} |
384 |
|
|
385 |
|
/** |
398 |
|
|
399 |
|
Object e4 = q.higher(six); |
400 |
|
assertNull(e4); |
422 |
– |
|
401 |
|
} |
402 |
|
|
403 |
|
/** |
416 |
|
|
417 |
|
Object e4 = q.floor(zero); |
418 |
|
assertNull(e4); |
441 |
– |
|
419 |
|
} |
420 |
|
|
421 |
|
/** |
434 |
|
|
435 |
|
Object e4 = q.ceiling(six); |
436 |
|
assertNull(e4); |
460 |
– |
|
437 |
|
} |
438 |
|
|
439 |
|
/** |
441 |
|
*/ |
442 |
|
public void testToArray() { |
443 |
|
TreeSet q = populatedSet(SIZE); |
444 |
< |
Object[] o = q.toArray(); |
444 |
> |
Object[] o = q.toArray(); |
445 |
|
Arrays.sort(o); |
446 |
< |
for(int i = 0; i < o.length; i++) |
447 |
< |
assertEquals(o[i], q.pollFirst()); |
446 |
> |
for (int i = 0; i < o.length; i++) |
447 |
> |
assertEquals(o[i], q.pollFirst()); |
448 |
|
} |
449 |
|
|
450 |
|
/** |
452 |
|
*/ |
453 |
|
public void testToArray2() { |
454 |
|
TreeSet q = populatedSet(SIZE); |
455 |
< |
Integer[] ints = new Integer[SIZE]; |
456 |
< |
ints = (Integer[])q.toArray(ints); |
455 |
> |
Integer[] ints = new Integer[SIZE]; |
456 |
> |
ints = (Integer[])q.toArray(ints); |
457 |
|
Arrays.sort(ints); |
458 |
< |
for(int i = 0; i < ints.length; i++) |
458 |
> |
for (int i = 0; i < ints.length; i++) |
459 |
|
assertEquals(ints[i], q.pollFirst()); |
460 |
|
} |
461 |
< |
|
461 |
> |
|
462 |
|
/** |
463 |
|
* iterator iterates through all elements |
464 |
|
*/ |
465 |
|
public void testIterator() { |
466 |
|
TreeSet q = populatedSet(SIZE); |
467 |
|
int i = 0; |
468 |
< |
Iterator it = q.iterator(); |
469 |
< |
while(it.hasNext()) { |
468 |
> |
Iterator it = q.iterator(); |
469 |
> |
while (it.hasNext()) { |
470 |
|
assertTrue(q.contains(it.next())); |
471 |
|
++i; |
472 |
|
} |
479 |
|
public void testEmptyIterator() { |
480 |
|
TreeSet q = new TreeSet(); |
481 |
|
int i = 0; |
482 |
< |
Iterator it = q.iterator(); |
483 |
< |
while(it.hasNext()) { |
482 |
> |
Iterator it = q.iterator(); |
483 |
> |
while (it.hasNext()) { |
484 |
|
assertTrue(q.contains(it.next())); |
485 |
|
++i; |
486 |
|
} |
516 |
|
for (int i = 0; i < SIZE; ++i) { |
517 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
518 |
|
} |
519 |
< |
} |
519 |
> |
} |
520 |
|
|
521 |
|
/** |
522 |
< |
* A deserialized serialized set has same elements |
522 |
> |
* A deserialized serialized set has same elements |
523 |
|
*/ |
524 |
< |
public void testSerialization() { |
524 |
> |
public void testSerialization() throws Exception { |
525 |
|
TreeSet q = populatedSet(SIZE); |
526 |
< |
try { |
527 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
528 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
529 |
< |
out.writeObject(q); |
530 |
< |
out.close(); |
531 |
< |
|
532 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
533 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
534 |
< |
TreeSet r = (TreeSet)in.readObject(); |
535 |
< |
assertEquals(q.size(), r.size()); |
536 |
< |
while (!q.isEmpty()) |
561 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
562 |
< |
} catch(Exception e){ |
563 |
< |
e.printStackTrace(); |
564 |
< |
unexpectedException(); |
565 |
< |
} |
526 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
527 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
528 |
> |
out.writeObject(q); |
529 |
> |
out.close(); |
530 |
> |
|
531 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
532 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
533 |
> |
TreeSet r = (TreeSet)in.readObject(); |
534 |
> |
assertEquals(q.size(), r.size()); |
535 |
> |
while (!q.isEmpty()) |
536 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
537 |
|
} |
538 |
|
|
539 |
|
/** |
661 |
|
/** |
662 |
|
* Subsets of subsets subdivide correctly |
663 |
|
*/ |
664 |
< |
public void testRecursiveSubSets() { |
665 |
< |
int setSize = 1000; |
666 |
< |
Class cl = TreeSet.class; |
664 |
> |
public void testRecursiveSubSets() throws Exception { |
665 |
> |
int setSize = 1000; |
666 |
> |
Class cl = TreeSet.class; |
667 |
|
|
668 |
|
NavigableSet<Integer> set = newSet(cl); |
669 |
|
bs = new BitSet(setSize); |
676 |
|
check(set, 0, setSize - 1, true); |
677 |
|
check(set.descendingSet(), 0, setSize - 1, false); |
678 |
|
|
679 |
< |
bashSubSet(set.navigableSubSet(0, true, setSize, false), |
679 |
> |
bashSubSet(set.subSet(0, true, setSize, false), |
680 |
|
0, setSize - 1, true); |
681 |
|
} |
682 |
|
|
683 |
< |
static NavigableSet<Integer> newSet(Class cl) { |
684 |
< |
NavigableSet<Integer> result = null; |
714 |
< |
try { |
715 |
< |
result = (NavigableSet<Integer>) cl.newInstance(); |
716 |
< |
} catch(Exception e) { |
717 |
< |
fail(); |
718 |
< |
} |
683 |
> |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
684 |
> |
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
685 |
|
assertEquals(result.size(), 0); |
686 |
|
assertFalse(result.iterator().hasNext()); |
687 |
|
return result; |
704 |
|
} |
705 |
|
|
706 |
|
// Remove a bunch of entries with iterator |
707 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
707 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
708 |
|
if (rnd.nextBoolean()) { |
709 |
|
bs.clear(it.next()); |
710 |
|
it.remove(); |
729 |
|
} |
730 |
|
|
731 |
|
// Remove a bunch of entries with iterator |
732 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
732 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
733 |
|
if (rnd.nextBoolean()) { |
734 |
|
bs.clear(it.next()); |
735 |
|
it.remove(); |
744 |
|
} else { |
745 |
|
try { |
746 |
|
set.add(element); |
747 |
< |
fail(); |
748 |
< |
} catch(IllegalArgumentException e) { |
783 |
< |
// expected |
784 |
< |
} |
747 |
> |
shouldThrow(); |
748 |
> |
} catch (IllegalArgumentException success) {} |
749 |
|
} |
750 |
|
} |
751 |
|
} |
776 |
|
|
777 |
|
// headSet - pick direction and endpoint inclusion randomly |
778 |
|
boolean incl = rnd.nextBoolean(); |
779 |
< |
NavigableSet<Integer> hm = set.navigableHeadSet(midPoint, incl); |
779 |
> |
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
780 |
|
if (ascending) { |
781 |
|
if (rnd.nextBoolean()) |
782 |
|
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
793 |
|
|
794 |
|
// tailSet - pick direction and endpoint inclusion randomly |
795 |
|
incl = rnd.nextBoolean(); |
796 |
< |
NavigableSet<Integer> tm = set.navigableTailSet(midPoint,incl); |
796 |
> |
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
797 |
|
if (ascending) { |
798 |
|
if (rnd.nextBoolean()) |
799 |
|
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
818 |
|
boolean lowIncl = rnd.nextBoolean(); |
819 |
|
boolean highIncl = rnd.nextBoolean(); |
820 |
|
if (ascending) { |
821 |
< |
NavigableSet<Integer> sm = set.navigableSubSet( |
821 |
> |
NavigableSet<Integer> sm = set.subSet( |
822 |
|
endpoints[0], lowIncl, endpoints[1], highIncl); |
823 |
|
if (rnd.nextBoolean()) |
824 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
827 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
828 |
|
endpoints[1] - (highIncl ? 0 : 1), false); |
829 |
|
} else { |
830 |
< |
NavigableSet<Integer> sm = set.navigableSubSet( |
830 |
> |
NavigableSet<Integer> sm = set.subSet( |
831 |
|
endpoints[1], highIncl, endpoints[0], lowIncl); |
832 |
|
if (rnd.nextBoolean()) |
833 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
944 |
|
assertEq(rs.last(), -1); |
945 |
|
try { |
946 |
|
set.first(); |
947 |
< |
fail(); |
948 |
< |
} catch(NoSuchElementException e) { |
985 |
< |
// expected |
986 |
< |
} |
947 |
> |
shouldThrow(); |
948 |
> |
} catch (NoSuchElementException success) {} |
949 |
|
try { |
950 |
|
set.last(); |
951 |
< |
fail(); |
952 |
< |
} catch(NoSuchElementException e) { |
991 |
< |
// expected |
992 |
< |
} |
951 |
> |
shouldThrow(); |
952 |
> |
} catch (NoSuchElementException success) {} |
953 |
|
} |
954 |
|
} |
955 |
|
|