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 { |
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 |
|
|
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 |
|
/** |
213 |
|
* addAll of a collection with null elements throws NPE |
214 |
|
*/ |
218 |
|
Integer[] ints = new Integer[SIZE]; |
219 |
|
q.addAll(Arrays.asList(ints)); |
220 |
|
shouldThrow(); |
221 |
< |
} |
236 |
< |
catch (NullPointerException success) {} |
221 |
> |
} catch (NullPointerException success) {} |
222 |
|
} |
223 |
+ |
|
224 |
|
/** |
225 |
|
* addAll of a collection with any null elements throws NPE after |
226 |
|
* possibly adding some elements |
233 |
|
ints[i] = new Integer(i); |
234 |
|
q.addAll(Arrays.asList(ints)); |
235 |
|
shouldThrow(); |
236 |
< |
} |
251 |
< |
catch (NullPointerException success) {} |
236 |
> |
} catch (NullPointerException success) {} |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
|
* Set contains all elements of successful addAll |
241 |
|
*/ |
242 |
|
public void testAddAll5() { |
243 |
< |
try { |
244 |
< |
Integer[] empty = new Integer[0]; |
245 |
< |
Integer[] ints = new Integer[SIZE]; |
246 |
< |
for (int i = 0; i < SIZE; ++i) |
247 |
< |
ints[i] = new Integer(SIZE-1-i); |
248 |
< |
TreeSet q = new TreeSet(); |
249 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
250 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
251 |
< |
for (int i = 0; i < SIZE; ++i) |
267 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
268 |
< |
} |
269 |
< |
finally {} |
243 |
> |
Integer[] empty = new Integer[0]; |
244 |
> |
Integer[] ints = new Integer[SIZE]; |
245 |
> |
for (int i = 0; i < SIZE; ++i) |
246 |
> |
ints[i] = new Integer(SIZE-1-i); |
247 |
> |
TreeSet q = new TreeSet(); |
248 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
249 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
250 |
> |
for (int i = 0; i < SIZE; ++i) |
251 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
252 |
|
} |
253 |
|
|
254 |
|
/** |
257 |
|
public void testPollFirst() { |
258 |
|
TreeSet q = populatedSet(SIZE); |
259 |
|
for (int i = 0; i < SIZE; ++i) { |
260 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
260 |
> |
assertEquals(i, q.pollFirst()); |
261 |
|
} |
262 |
< |
assertNull(q.pollFirst()); |
262 |
> |
assertNull(q.pollFirst()); |
263 |
|
} |
264 |
|
|
265 |
|
/** |
268 |
|
public void testPollLast() { |
269 |
|
TreeSet q = populatedSet(SIZE); |
270 |
|
for (int i = SIZE-1; i >= 0; --i) { |
271 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
271 |
> |
assertEquals(i, q.pollLast()); |
272 |
|
} |
273 |
< |
assertNull(q.pollFirst()); |
273 |
> |
assertNull(q.pollFirst()); |
274 |
|
} |
275 |
|
|
276 |
|
|
382 |
|
|
383 |
|
Object e4 = q.lower(zero); |
384 |
|
assertNull(e4); |
403 |
– |
|
385 |
|
} |
386 |
|
|
387 |
|
/** |
400 |
|
|
401 |
|
Object e4 = q.higher(six); |
402 |
|
assertNull(e4); |
422 |
– |
|
403 |
|
} |
404 |
|
|
405 |
|
/** |
418 |
|
|
419 |
|
Object e4 = q.floor(zero); |
420 |
|
assertNull(e4); |
441 |
– |
|
421 |
|
} |
422 |
|
|
423 |
|
/** |
436 |
|
|
437 |
|
Object e4 = q.ceiling(six); |
438 |
|
assertNull(e4); |
460 |
– |
|
439 |
|
} |
440 |
|
|
441 |
|
/** |
443 |
|
*/ |
444 |
|
public void testToArray() { |
445 |
|
TreeSet q = populatedSet(SIZE); |
446 |
< |
Object[] o = q.toArray(); |
446 |
> |
Object[] o = q.toArray(); |
447 |
|
Arrays.sort(o); |
448 |
< |
for (int i = 0; i < o.length; i++) |
449 |
< |
assertEquals(o[i], q.pollFirst()); |
448 |
> |
for (int i = 0; i < o.length; i++) |
449 |
> |
assertEquals(o[i], q.pollFirst()); |
450 |
|
} |
451 |
|
|
452 |
|
/** |
454 |
|
*/ |
455 |
|
public void testToArray2() { |
456 |
|
TreeSet q = populatedSet(SIZE); |
457 |
< |
Integer[] ints = new Integer[SIZE]; |
458 |
< |
ints = (Integer[])q.toArray(ints); |
457 |
> |
Integer[] ints = new Integer[SIZE]; |
458 |
> |
ints = (Integer[])q.toArray(ints); |
459 |
|
Arrays.sort(ints); |
460 |
|
for (int i = 0; i < ints.length; i++) |
461 |
|
assertEquals(ints[i], q.pollFirst()); |
467 |
|
public void testIterator() { |
468 |
|
TreeSet q = populatedSet(SIZE); |
469 |
|
int i = 0; |
470 |
< |
Iterator it = q.iterator(); |
470 |
> |
Iterator it = q.iterator(); |
471 |
|
while (it.hasNext()) { |
472 |
|
assertTrue(q.contains(it.next())); |
473 |
|
++i; |
481 |
|
public void testEmptyIterator() { |
482 |
|
TreeSet q = new TreeSet(); |
483 |
|
int i = 0; |
484 |
< |
Iterator it = q.iterator(); |
484 |
> |
Iterator it = q.iterator(); |
485 |
|
while (it.hasNext()) { |
486 |
|
assertTrue(q.contains(it.next())); |
487 |
|
++i; |
492 |
|
/** |
493 |
|
* iterator.remove removes current element |
494 |
|
*/ |
495 |
< |
public void testIteratorRemove () { |
495 |
> |
public void testIteratorRemove() { |
496 |
|
final TreeSet q = new TreeSet(); |
497 |
|
q.add(new Integer(2)); |
498 |
|
q.add(new Integer(1)); |
523 |
|
/** |
524 |
|
* A deserialized serialized set has same elements |
525 |
|
*/ |
526 |
< |
public void testSerialization() { |
526 |
> |
public void testSerialization() throws Exception { |
527 |
|
TreeSet q = populatedSet(SIZE); |
528 |
< |
try { |
529 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
530 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
531 |
< |
out.writeObject(q); |
532 |
< |
out.close(); |
533 |
< |
|
534 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
535 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
536 |
< |
TreeSet r = (TreeSet)in.readObject(); |
537 |
< |
assertEquals(q.size(), r.size()); |
538 |
< |
while (!q.isEmpty()) |
561 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
562 |
< |
} catch (Exception e){ |
563 |
< |
e.printStackTrace(); |
564 |
< |
unexpectedException(); |
565 |
< |
} |
528 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
529 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
530 |
> |
out.writeObject(q); |
531 |
> |
out.close(); |
532 |
> |
|
533 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
534 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
535 |
> |
TreeSet r = (TreeSet)in.readObject(); |
536 |
> |
assertEquals(q.size(), r.size()); |
537 |
> |
while (!q.isEmpty()) |
538 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
539 |
|
} |
540 |
|
|
541 |
|
/** |
663 |
|
/** |
664 |
|
* Subsets of subsets subdivide correctly |
665 |
|
*/ |
666 |
< |
public void testRecursiveSubSets() { |
667 |
< |
int setSize = 1000; |
668 |
< |
Class cl = TreeSet.class; |
666 |
> |
public void testRecursiveSubSets() throws Exception { |
667 |
> |
int setSize = 1000; |
668 |
> |
Class cl = TreeSet.class; |
669 |
|
|
670 |
|
NavigableSet<Integer> set = newSet(cl); |
671 |
|
bs = new BitSet(setSize); |
682 |
|
0, setSize - 1, true); |
683 |
|
} |
684 |
|
|
685 |
< |
static NavigableSet<Integer> newSet(Class cl) { |
686 |
< |
NavigableSet<Integer> result = null; |
714 |
< |
try { |
715 |
< |
result = (NavigableSet<Integer>) cl.newInstance(); |
716 |
< |
} catch (Exception e) { |
717 |
< |
fail(); |
718 |
< |
} |
685 |
> |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
686 |
> |
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
687 |
|
assertEquals(result.size(), 0); |
688 |
|
assertFalse(result.iterator().hasNext()); |
689 |
|
return result; |
746 |
|
} else { |
747 |
|
try { |
748 |
|
set.add(element); |
749 |
< |
fail(); |
750 |
< |
} catch (IllegalArgumentException e) { |
783 |
< |
// expected |
784 |
< |
} |
749 |
> |
shouldThrow(); |
750 |
> |
} catch (IllegalArgumentException success) {} |
751 |
|
} |
752 |
|
} |
753 |
|
} |
880 |
|
// BitSet should support this! Test would run much faster |
881 |
|
while (element >= min) { |
882 |
|
if (bs.get(element)) |
883 |
< |
return(element); |
883 |
> |
return element; |
884 |
|
element--; |
885 |
|
} |
886 |
|
return -1; |
946 |
|
assertEq(rs.last(), -1); |
947 |
|
try { |
948 |
|
set.first(); |
949 |
< |
fail(); |
950 |
< |
} catch (NoSuchElementException e) { |
985 |
< |
// expected |
986 |
< |
} |
949 |
> |
shouldThrow(); |
950 |
> |
} catch (NoSuchElementException success) {} |
951 |
|
try { |
952 |
|
set.last(); |
953 |
< |
fail(); |
954 |
< |
} catch (NoSuchElementException e) { |
991 |
< |
// expected |
992 |
< |
} |
953 |
> |
shouldThrow(); |
954 |
> |
} catch (NoSuchElementException success) {} |
955 |
|
} |
956 |
|
} |
957 |
|
|