4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
import junit.framework.*; |
7 |
|
import java.util.Arrays; |
8 |
|
import java.util.BitSet; |
9 |
|
import java.util.Collection; |
16 |
|
import java.util.SortedSet; |
17 |
|
import java.util.TreeSet; |
18 |
|
|
19 |
+ |
import junit.framework.Test; |
20 |
+ |
import junit.framework.TestSuite; |
21 |
+ |
|
22 |
|
public class TreeSetTest extends JSR166TestCase { |
23 |
|
public static void main(String[] args) { |
24 |
< |
junit.textui.TestRunner.run(suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
public static Test suite() { |
27 |
|
return new TestSuite(TreeSetTest.class); |
39 |
|
static final int SIZE = 20; |
40 |
|
|
41 |
|
/** |
42 |
< |
* Create a set of given size containing consecutive |
42 |
> |
* Returns a new set of given size containing consecutive |
43 |
|
* Integers 0 ... n. |
44 |
|
*/ |
45 |
|
private TreeSet<Integer> populatedSet(int n) { |
46 |
|
TreeSet<Integer> q = new TreeSet<Integer>(); |
47 |
|
assertTrue(q.isEmpty()); |
48 |
< |
for (int i = n-1; i >= 0; i-=2) |
48 |
> |
for (int i = n-1; i >= 0; i -= 2) |
49 |
|
assertTrue(q.add(new Integer(i))); |
50 |
< |
for (int i = (n & 1); i < n; i+=2) |
50 |
> |
for (int i = (n & 1); i < n; i += 2) |
51 |
|
assertTrue(q.add(new Integer(i))); |
52 |
|
assertFalse(q.isEmpty()); |
53 |
|
assertEquals(n, q.size()); |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
< |
* Create set of first 5 ints |
58 |
> |
* Returns a new set of first 5 ints. |
59 |
|
*/ |
60 |
|
private TreeSet set5() { |
61 |
|
TreeSet q = new TreeSet(); |
81 |
|
*/ |
82 |
|
public void testConstructor3() { |
83 |
|
try { |
84 |
< |
TreeSet q = new TreeSet((Collection)null); |
84 |
> |
new TreeSet((Collection)null); |
85 |
|
shouldThrow(); |
86 |
|
} catch (NullPointerException success) {} |
87 |
|
} |
91 |
|
*/ |
92 |
|
public void testConstructor4() { |
93 |
|
try { |
94 |
< |
Integer[] ints = new Integer[SIZE]; |
93 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
94 |
> |
new TreeSet(Arrays.asList(new Integer[SIZE])); |
95 |
|
shouldThrow(); |
96 |
|
} catch (NullPointerException success) {} |
97 |
|
} |
100 |
|
* Initializing from Collection with some null elements throws NPE |
101 |
|
*/ |
102 |
|
public void testConstructor5() { |
103 |
+ |
Integer[] ints = new Integer[SIZE]; |
104 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
105 |
+ |
ints[i] = new Integer(i); |
106 |
|
try { |
107 |
< |
Integer[] ints = new Integer[SIZE]; |
104 |
< |
for (int i = 0; i < SIZE-1; ++i) |
105 |
< |
ints[i] = new Integer(i); |
106 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
107 |
> |
new TreeSet(Arrays.asList(ints)); |
108 |
|
shouldThrow(); |
109 |
|
} catch (NullPointerException success) {} |
110 |
|
} |
169 |
|
* add(null) throws NPE if nonempty |
170 |
|
*/ |
171 |
|
public void testAddNull() { |
172 |
+ |
TreeSet q = populatedSet(SIZE); |
173 |
|
try { |
172 |
– |
TreeSet q = populatedSet(SIZE); |
174 |
|
q.add(null); |
175 |
|
shouldThrow(); |
176 |
|
} catch (NullPointerException success) {} |
198 |
|
* Add of non-Comparable throws CCE |
199 |
|
*/ |
200 |
|
public void testAddNonComparable() { |
201 |
+ |
TreeSet q = new TreeSet(); |
202 |
|
try { |
201 |
– |
TreeSet q = new TreeSet(); |
202 |
– |
q.add(new Object()); |
203 |
|
q.add(new Object()); |
204 |
|
q.add(new Object()); |
205 |
|
shouldThrow(); |
210 |
|
* addAll(null) throws NPE |
211 |
|
*/ |
212 |
|
public void testAddAll1() { |
213 |
+ |
TreeSet q = new TreeSet(); |
214 |
|
try { |
214 |
– |
TreeSet q = new TreeSet(); |
215 |
|
q.addAll(null); |
216 |
|
shouldThrow(); |
217 |
|
} catch (NullPointerException success) {} |
221 |
|
* addAll of a collection with null elements throws NPE |
222 |
|
*/ |
223 |
|
public void testAddAll2() { |
224 |
+ |
TreeSet q = new TreeSet(); |
225 |
+ |
Integer[] ints = new Integer[SIZE]; |
226 |
|
try { |
225 |
– |
TreeSet q = new TreeSet(); |
226 |
– |
Integer[] ints = new Integer[SIZE]; |
227 |
|
q.addAll(Arrays.asList(ints)); |
228 |
|
shouldThrow(); |
229 |
|
} catch (NullPointerException success) {} |
234 |
|
* possibly adding some elements |
235 |
|
*/ |
236 |
|
public void testAddAll3() { |
237 |
+ |
TreeSet q = new TreeSet(); |
238 |
+ |
Integer[] ints = new Integer[SIZE]; |
239 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
240 |
+ |
ints[i] = new Integer(i); |
241 |
|
try { |
238 |
– |
TreeSet q = new TreeSet(); |
239 |
– |
Integer[] ints = new Integer[SIZE]; |
240 |
– |
for (int i = 0; i < SIZE-1; ++i) |
241 |
– |
ints[i] = new Integer(i); |
242 |
|
q.addAll(Arrays.asList(ints)); |
243 |
|
shouldThrow(); |
244 |
|
} catch (NullPointerException success) {} |
286 |
|
*/ |
287 |
|
public void testRemoveElement() { |
288 |
|
TreeSet q = populatedSet(SIZE); |
289 |
< |
for (int i = 1; i < SIZE; i+=2) { |
289 |
> |
for (int i = 1; i < SIZE; i += 2) { |
290 |
|
assertTrue(q.contains(i)); |
291 |
|
assertTrue(q.remove(i)); |
292 |
|
assertFalse(q.contains(i)); |
293 |
|
assertTrue(q.contains(i-1)); |
294 |
|
} |
295 |
< |
for (int i = 0; i < SIZE; i+=2) { |
295 |
> |
for (int i = 0; i < SIZE; i += 2) { |
296 |
|
assertTrue(q.contains(i)); |
297 |
|
assertTrue(q.remove(i)); |
298 |
|
assertFalse(q.contains(i)); |
371 |
|
assertTrue(q.removeAll(p)); |
372 |
|
assertEquals(SIZE-i, q.size()); |
373 |
|
for (int j = 0; j < i; ++j) { |
374 |
< |
Integer I = (Integer)(p.pollFirst()); |
375 |
< |
assertFalse(q.contains(I)); |
374 |
> |
Integer x = (Integer)(p.pollFirst()); |
375 |
> |
assertFalse(q.contains(x)); |
376 |
|
} |
377 |
|
} |
378 |
|
} |
476 |
|
*/ |
477 |
|
public void testIterator() { |
478 |
|
TreeSet q = populatedSet(SIZE); |
479 |
– |
int i = 0; |
479 |
|
Iterator it = q.iterator(); |
480 |
< |
while (it.hasNext()) { |
480 |
> |
int i; |
481 |
> |
for (i = 0; it.hasNext(); i++) |
482 |
|
assertTrue(q.contains(it.next())); |
483 |
– |
++i; |
484 |
– |
} |
483 |
|
assertEquals(i, SIZE); |
484 |
+ |
assertIteratorExhausted(it); |
485 |
|
} |
486 |
|
|
487 |
|
/** |
488 |
|
* iterator of empty set has no elements |
489 |
|
*/ |
490 |
|
public void testEmptyIterator() { |
491 |
< |
TreeSet q = new TreeSet(); |
493 |
< |
int i = 0; |
494 |
< |
Iterator it = q.iterator(); |
495 |
< |
while (it.hasNext()) { |
496 |
< |
assertTrue(q.contains(it.next())); |
497 |
< |
++i; |
498 |
< |
} |
499 |
< |
assertEquals(0, i); |
491 |
> |
assertIteratorExhausted(new TreeSet().iterator()); |
492 |
|
} |
493 |
|
|
494 |
|
/** |
528 |
|
NavigableSet x = populatedSet(SIZE); |
529 |
|
NavigableSet y = serialClone(x); |
530 |
|
|
531 |
< |
assertTrue(x != y); |
531 |
> |
assertNotSame(x, y); |
532 |
|
assertEquals(x.size(), y.size()); |
533 |
|
assertEquals(x, y); |
534 |
|
assertEquals(y, x); |
683 |
|
0, setSize - 1, true); |
684 |
|
} |
685 |
|
|
686 |
+ |
/** |
687 |
+ |
* addAll is idempotent |
688 |
+ |
*/ |
689 |
+ |
public void testAddAll_idempotent() throws Exception { |
690 |
+ |
Set x = populatedSet(SIZE); |
691 |
+ |
Set y = new TreeSet(x); |
692 |
+ |
y.addAll(x); |
693 |
+ |
assertEquals(x, y); |
694 |
+ |
assertEquals(y, x); |
695 |
+ |
} |
696 |
+ |
|
697 |
|
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
698 |
|
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
699 |
|
assertEquals(0, result.size()); |
728 |
|
// Add entries till we're back to original size |
729 |
|
while (set.size() < size) { |
730 |
|
int element = min + rnd.nextInt(rangeSize); |
731 |
< |
assertTrue(element >= min && element<= max); |
731 |
> |
assertTrue(element >= min && element <= max); |
732 |
|
put(set, element); |
733 |
|
} |
734 |
|
} |
753 |
|
// Add entries till we're back to original size |
754 |
|
while (set.size() < size) { |
755 |
|
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
756 |
< |
if (element >= min && element<= max) { |
756 |
> |
if (element >= min && element <= max) { |
757 |
|
put(set, element); |
758 |
|
} else { |
759 |
|
try { |