5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
10 |
< |
import java.io.*; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.BitSet; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Comparator; |
12 |
> |
import java.util.Iterator; |
13 |
> |
import java.util.NavigableSet; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.Random; |
16 |
> |
import java.util.Set; |
17 |
> |
import java.util.SortedSet; |
18 |
> |
import java.util.TreeSet; |
19 |
|
|
20 |
|
public class TreeSetTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
37 |
|
static final int SIZE = 20; |
38 |
|
|
39 |
|
/** |
40 |
< |
* Create a set of given size containing consecutive |
40 |
> |
* Returns a new set of given size containing consecutive |
41 |
|
* Integers 0 ... n. |
42 |
|
*/ |
43 |
|
private TreeSet<Integer> populatedSet(int n) { |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
< |
* Create set of first 5 ints |
56 |
> |
* Returns a new set of first 5 ints. |
57 |
|
*/ |
58 |
|
private TreeSet set5() { |
59 |
|
TreeSet q = new TreeSet(); |
281 |
|
assertNull(q.pollFirst()); |
282 |
|
} |
283 |
|
|
276 |
– |
|
284 |
|
/** |
285 |
|
* remove(x) removes x and returns true if present |
286 |
|
*/ |
377 |
|
} |
378 |
|
} |
379 |
|
|
373 |
– |
|
374 |
– |
|
380 |
|
/** |
381 |
|
* lower returns preceding element |
382 |
|
*/ |
496 |
|
assertTrue(q.contains(it.next())); |
497 |
|
++i; |
498 |
|
} |
499 |
< |
assertEquals(i, 0); |
499 |
> |
assertEquals(0, i); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
518 |
|
assertFalse(it.hasNext()); |
519 |
|
} |
520 |
|
|
516 |
– |
|
521 |
|
/** |
522 |
|
* toString contains toStrings of elements |
523 |
|
*/ |
525 |
|
TreeSet q = populatedSet(SIZE); |
526 |
|
String s = q.toString(); |
527 |
|
for (int i = 0; i < SIZE; ++i) { |
528 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
528 |
> |
assertTrue(s.contains(String.valueOf(i))); |
529 |
|
} |
530 |
|
} |
531 |
|
|
533 |
|
* A deserialized serialized set has same elements |
534 |
|
*/ |
535 |
|
public void testSerialization() throws Exception { |
536 |
< |
TreeSet q = populatedSet(SIZE); |
537 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
538 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
539 |
< |
out.writeObject(q); |
540 |
< |
out.close(); |
541 |
< |
|
542 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
543 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
544 |
< |
TreeSet r = (TreeSet)in.readObject(); |
545 |
< |
assertEquals(q.size(), r.size()); |
546 |
< |
while (!q.isEmpty()) |
547 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
536 |
> |
NavigableSet x = populatedSet(SIZE); |
537 |
> |
NavigableSet y = serialClone(x); |
538 |
> |
|
539 |
> |
assertTrue(x != y); |
540 |
> |
assertEquals(x.size(), y.size()); |
541 |
> |
assertEquals(x, y); |
542 |
> |
assertEquals(y, x); |
543 |
> |
while (!x.isEmpty()) { |
544 |
> |
assertFalse(y.isEmpty()); |
545 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
546 |
> |
} |
547 |
> |
assertTrue(y.isEmpty()); |
548 |
|
} |
549 |
|
|
550 |
|
/** |
691 |
|
0, setSize - 1, true); |
692 |
|
} |
693 |
|
|
694 |
+ |
/** |
695 |
+ |
* addAll is idempotent |
696 |
+ |
*/ |
697 |
+ |
public void testAddAll_idempotent() throws Exception { |
698 |
+ |
Set x = populatedSet(SIZE); |
699 |
+ |
Set y = new TreeSet(x); |
700 |
+ |
y.addAll(x); |
701 |
+ |
assertEquals(x, y); |
702 |
+ |
assertEquals(y, x); |
703 |
+ |
} |
704 |
+ |
|
705 |
|
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
706 |
|
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
707 |
< |
assertEquals(result.size(), 0); |
707 |
> |
assertEquals(0, result.size()); |
708 |
|
assertFalse(result.iterator().hasNext()); |
709 |
|
return result; |
710 |
|
} |
865 |
|
*/ |
866 |
|
void check(NavigableSet<Integer> set, |
867 |
|
final int min, final int max, final boolean ascending) { |
868 |
< |
class ReferenceSet { |
868 |
> |
class ReferenceSet { |
869 |
|
int lower(int element) { |
870 |
|
return ascending ? |
871 |
|
lowerAscending(element) : higherAscending(element); |
935 |
|
if (bsContainsI) |
936 |
|
size++; |
937 |
|
} |
938 |
< |
assertEquals(set.size(), size); |
938 |
> |
assertEquals(size, set.size()); |
939 |
|
|
940 |
|
// Test contents using contains elementSet iterator |
941 |
|
int size2 = 0; |