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()); |
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 |
|
} |
92 |
|
public void testConstructor4() { |
93 |
|
try { |
94 |
|
Integer[] ints = new Integer[SIZE]; |
95 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
95 |
> |
new TreeSet(Arrays.asList(ints)); |
96 |
|
shouldThrow(); |
97 |
|
} catch (NullPointerException success) {} |
98 |
|
} |
105 |
|
Integer[] ints = new Integer[SIZE]; |
106 |
|
for (int i = 0; i < SIZE-1; ++i) |
107 |
|
ints[i] = new Integer(i); |
108 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
108 |
> |
new TreeSet(Arrays.asList(ints)); |
109 |
|
shouldThrow(); |
110 |
|
} catch (NullPointerException success) {} |
111 |
|
} |
288 |
|
*/ |
289 |
|
public void testRemoveElement() { |
290 |
|
TreeSet q = populatedSet(SIZE); |
291 |
< |
for (int i = 1; i < SIZE; i+=2) { |
291 |
> |
for (int i = 1; i < SIZE; i += 2) { |
292 |
|
assertTrue(q.contains(i)); |
293 |
|
assertTrue(q.remove(i)); |
294 |
|
assertFalse(q.contains(i)); |
295 |
|
assertTrue(q.contains(i-1)); |
296 |
|
} |
297 |
< |
for (int i = 0; i < SIZE; i+=2) { |
297 |
> |
for (int i = 0; i < SIZE; i += 2) { |
298 |
|
assertTrue(q.contains(i)); |
299 |
|
assertTrue(q.remove(i)); |
300 |
|
assertFalse(q.contains(i)); |
373 |
|
assertTrue(q.removeAll(p)); |
374 |
|
assertEquals(SIZE-i, q.size()); |
375 |
|
for (int j = 0; j < i; ++j) { |
376 |
< |
Integer I = (Integer)(p.pollFirst()); |
377 |
< |
assertFalse(q.contains(I)); |
376 |
> |
Integer x = (Integer)(p.pollFirst()); |
377 |
> |
assertFalse(q.contains(x)); |
378 |
|
} |
379 |
|
} |
380 |
|
} |
478 |
|
*/ |
479 |
|
public void testIterator() { |
480 |
|
TreeSet q = populatedSet(SIZE); |
479 |
– |
int i = 0; |
481 |
|
Iterator it = q.iterator(); |
482 |
< |
while (it.hasNext()) { |
482 |
> |
int i; |
483 |
> |
for (i = 0; it.hasNext(); i++) |
484 |
|
assertTrue(q.contains(it.next())); |
483 |
– |
++i; |
484 |
– |
} |
485 |
|
assertEquals(i, SIZE); |
486 |
+ |
assertIteratorExhausted(it); |
487 |
|
} |
488 |
|
|
489 |
|
/** |
490 |
|
* iterator of empty set has no elements |
491 |
|
*/ |
492 |
|
public void testEmptyIterator() { |
493 |
< |
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); |
493 |
> |
assertIteratorExhausted(new TreeSet().iterator()); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
530 |
|
NavigableSet x = populatedSet(SIZE); |
531 |
|
NavigableSet y = serialClone(x); |
532 |
|
|
533 |
< |
assertTrue(x != y); |
533 |
> |
assertNotSame(x, y); |
534 |
|
assertEquals(x.size(), y.size()); |
535 |
|
assertEquals(x, y); |
536 |
|
assertEquals(y, x); |
685 |
|
0, setSize - 1, true); |
686 |
|
} |
687 |
|
|
688 |
+ |
/** |
689 |
+ |
* addAll is idempotent |
690 |
+ |
*/ |
691 |
+ |
public void testAddAll_idempotent() throws Exception { |
692 |
+ |
Set x = populatedSet(SIZE); |
693 |
+ |
Set y = new TreeSet(x); |
694 |
+ |
y.addAll(x); |
695 |
+ |
assertEquals(x, y); |
696 |
+ |
assertEquals(y, x); |
697 |
+ |
} |
698 |
+ |
|
699 |
|
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
700 |
|
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
701 |
|
assertEquals(0, result.size()); |
730 |
|
// Add entries till we're back to original size |
731 |
|
while (set.size() < size) { |
732 |
|
int element = min + rnd.nextInt(rangeSize); |
733 |
< |
assertTrue(element >= min && element<= max); |
733 |
> |
assertTrue(element >= min && element <= max); |
734 |
|
put(set, element); |
735 |
|
} |
736 |
|
} |
755 |
|
// Add entries till we're back to original size |
756 |
|
while (set.size() < size) { |
757 |
|
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
758 |
< |
if (element >= min && element<= max) { |
758 |
> |
if (element >= min && element <= max) { |
759 |
|
put(set, element); |
760 |
|
} else { |
761 |
|
try { |