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.*; |
7 |
> |
import java.util.Arrays; |
8 |
> |
import java.util.BitSet; |
9 |
> |
import java.util.Collection; |
10 |
> |
import java.util.Comparator; |
11 |
> |
import java.util.Iterator; |
12 |
> |
import java.util.NavigableSet; |
13 |
> |
import java.util.NoSuchElementException; |
14 |
> |
import java.util.Random; |
15 |
> |
import java.util.Set; |
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) { |
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 populatedSet(int n) { |
46 |
< |
TreeSet q = new TreeSet(); |
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 |
|
} |
283 |
|
assertNull(q.pollFirst()); |
284 |
|
} |
285 |
|
|
276 |
– |
|
286 |
|
/** |
287 |
|
* remove(x) removes x and returns true if present |
288 |
|
*/ |
289 |
|
public void testRemoveElement() { |
290 |
|
TreeSet q = populatedSet(SIZE); |
291 |
< |
for (int i = 1; i < SIZE; i+=2) { |
292 |
< |
assertTrue(q.remove(new Integer(i))); |
293 |
< |
} |
294 |
< |
for (int i = 0; i < SIZE; i+=2) { |
295 |
< |
assertTrue(q.remove(new Integer(i))); |
296 |
< |
assertFalse(q.remove(new Integer(i+1))); |
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) { |
298 |
> |
assertTrue(q.contains(i)); |
299 |
> |
assertTrue(q.remove(i)); |
300 |
> |
assertFalse(q.contains(i)); |
301 |
> |
assertFalse(q.remove(i+1)); |
302 |
> |
assertFalse(q.contains(i+1)); |
303 |
|
} |
304 |
|
assertTrue(q.isEmpty()); |
305 |
|
} |
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 |
|
} |
381 |
|
|
367 |
– |
|
368 |
– |
|
382 |
|
/** |
383 |
|
* lower returns preceding element |
384 |
|
*/ |
452 |
|
} |
453 |
|
|
454 |
|
/** |
455 |
< |
* toArray contains all elements |
455 |
> |
* toArray contains all elements in sorted order |
456 |
|
*/ |
457 |
|
public void testToArray() { |
458 |
|
TreeSet q = populatedSet(SIZE); |
459 |
|
Object[] o = q.toArray(); |
447 |
– |
Arrays.sort(o); |
460 |
|
for (int i = 0; i < o.length; i++) |
461 |
< |
assertEquals(o[i], q.pollFirst()); |
461 |
> |
assertSame(o[i], q.pollFirst()); |
462 |
|
} |
463 |
|
|
464 |
|
/** |
465 |
< |
* toArray(a) contains all elements |
465 |
> |
* toArray(a) contains all elements in sorted order |
466 |
|
*/ |
467 |
|
public void testToArray2() { |
468 |
< |
TreeSet q = populatedSet(SIZE); |
468 |
> |
TreeSet<Integer> q = populatedSet(SIZE); |
469 |
|
Integer[] ints = new Integer[SIZE]; |
470 |
< |
ints = (Integer[])q.toArray(ints); |
471 |
< |
Arrays.sort(ints); |
470 |
> |
Integer[] array = q.toArray(ints); |
471 |
> |
assertSame(ints, array); |
472 |
|
for (int i = 0; i < ints.length; i++) |
473 |
< |
assertEquals(ints[i], q.pollFirst()); |
473 |
> |
assertSame(ints[i], q.pollFirst()); |
474 |
|
} |
475 |
|
|
476 |
|
/** |
478 |
|
*/ |
479 |
|
public void testIterator() { |
480 |
|
TreeSet q = populatedSet(SIZE); |
469 |
– |
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())); |
473 |
– |
++i; |
474 |
– |
} |
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(); |
483 |
< |
int i = 0; |
484 |
< |
Iterator it = q.iterator(); |
485 |
< |
while (it.hasNext()) { |
486 |
< |
assertTrue(q.contains(it.next())); |
487 |
< |
++i; |
488 |
< |
} |
489 |
< |
assertEquals(i, 0); |
493 |
> |
assertIteratorExhausted(new TreeSet().iterator()); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
512 |
|
assertFalse(it.hasNext()); |
513 |
|
} |
514 |
|
|
511 |
– |
|
515 |
|
/** |
516 |
|
* toString contains toStrings of elements |
517 |
|
*/ |
519 |
|
TreeSet q = populatedSet(SIZE); |
520 |
|
String s = q.toString(); |
521 |
|
for (int i = 0; i < SIZE; ++i) { |
522 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
522 |
> |
assertTrue(s.contains(String.valueOf(i))); |
523 |
|
} |
524 |
|
} |
525 |
|
|
527 |
|
* A deserialized serialized set has same elements |
528 |
|
*/ |
529 |
|
public void testSerialization() throws Exception { |
530 |
< |
TreeSet q = populatedSet(SIZE); |
531 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
532 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
533 |
< |
out.writeObject(q); |
534 |
< |
out.close(); |
535 |
< |
|
536 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
537 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
538 |
< |
TreeSet r = (TreeSet)in.readObject(); |
539 |
< |
assertEquals(q.size(), r.size()); |
540 |
< |
while (!q.isEmpty()) |
541 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
530 |
> |
NavigableSet x = populatedSet(SIZE); |
531 |
> |
NavigableSet y = serialClone(x); |
532 |
> |
|
533 |
> |
assertNotSame(x, y); |
534 |
> |
assertEquals(x.size(), y.size()); |
535 |
> |
assertEquals(x, y); |
536 |
> |
assertEquals(y, x); |
537 |
> |
while (!x.isEmpty()) { |
538 |
> |
assertFalse(y.isEmpty()); |
539 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
540 |
> |
} |
541 |
> |
assertTrue(y.isEmpty()); |
542 |
|
} |
543 |
|
|
544 |
|
/** |
667 |
|
* Subsets of subsets subdivide correctly |
668 |
|
*/ |
669 |
|
public void testRecursiveSubSets() throws Exception { |
670 |
< |
int setSize = 1000; |
670 |
> |
int setSize = expensiveTests ? 1000 : 100; |
671 |
|
Class cl = TreeSet.class; |
672 |
|
|
673 |
|
NavigableSet<Integer> set = newSet(cl); |
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(result.size(), 0); |
701 |
> |
assertEquals(0, result.size()); |
702 |
|
assertFalse(result.iterator().hasNext()); |
703 |
|
return result; |
704 |
|
} |
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 { |
859 |
|
*/ |
860 |
|
void check(NavigableSet<Integer> set, |
861 |
|
final int min, final int max, final boolean ascending) { |
862 |
< |
class ReferenceSet { |
862 |
> |
class ReferenceSet { |
863 |
|
int lower(int element) { |
864 |
|
return ascending ? |
865 |
|
lowerAscending(element) : higherAscending(element); |
894 |
|
// BitSet should support this! Test would run much faster |
895 |
|
while (element >= min) { |
896 |
|
if (bs.get(element)) |
897 |
< |
return(element); |
897 |
> |
return element; |
898 |
|
element--; |
899 |
|
} |
900 |
|
return -1; |
929 |
|
if (bsContainsI) |
930 |
|
size++; |
931 |
|
} |
932 |
< |
assertEquals(set.size(), size); |
932 |
> |
assertEquals(size, set.size()); |
933 |
|
|
934 |
|
// Test contents using contains elementSet iterator |
935 |
|
int size2 = 0; |