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<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(); |
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) { |
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 |
|
} |
381 |
|
|
373 |
– |
|
374 |
– |
|
382 |
|
/** |
383 |
|
* lower returns preceding element |
384 |
|
*/ |
498 |
|
assertTrue(q.contains(it.next())); |
499 |
|
++i; |
500 |
|
} |
501 |
< |
assertEquals(i, 0); |
501 |
> |
assertEquals(0, i); |
502 |
|
} |
503 |
|
|
504 |
|
/** |
520 |
|
assertFalse(it.hasNext()); |
521 |
|
} |
522 |
|
|
516 |
– |
|
523 |
|
/** |
524 |
|
* toString contains toStrings of elements |
525 |
|
*/ |
527 |
|
TreeSet q = populatedSet(SIZE); |
528 |
|
String s = q.toString(); |
529 |
|
for (int i = 0; i < SIZE; ++i) { |
530 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
530 |
> |
assertTrue(s.contains(String.valueOf(i))); |
531 |
|
} |
532 |
|
} |
533 |
|
|
535 |
|
* A deserialized serialized set has same elements |
536 |
|
*/ |
537 |
|
public void testSerialization() throws Exception { |
538 |
< |
TreeSet q = populatedSet(SIZE); |
539 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
540 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
541 |
< |
out.writeObject(q); |
542 |
< |
out.close(); |
543 |
< |
|
544 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
545 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
546 |
< |
TreeSet r = (TreeSet)in.readObject(); |
547 |
< |
assertEquals(q.size(), r.size()); |
548 |
< |
while (!q.isEmpty()) |
549 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
538 |
> |
NavigableSet x = populatedSet(SIZE); |
539 |
> |
NavigableSet y = serialClone(x); |
540 |
> |
|
541 |
> |
assertNotSame(x, y); |
542 |
> |
assertEquals(x.size(), y.size()); |
543 |
> |
assertEquals(x, y); |
544 |
> |
assertEquals(y, x); |
545 |
> |
while (!x.isEmpty()) { |
546 |
> |
assertFalse(y.isEmpty()); |
547 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
548 |
> |
} |
549 |
> |
assertTrue(y.isEmpty()); |
550 |
|
} |
551 |
|
|
552 |
|
/** |
693 |
|
0, setSize - 1, true); |
694 |
|
} |
695 |
|
|
696 |
+ |
/** |
697 |
+ |
* addAll is idempotent |
698 |
+ |
*/ |
699 |
+ |
public void testAddAll_idempotent() throws Exception { |
700 |
+ |
Set x = populatedSet(SIZE); |
701 |
+ |
Set y = new TreeSet(x); |
702 |
+ |
y.addAll(x); |
703 |
+ |
assertEquals(x, y); |
704 |
+ |
assertEquals(y, x); |
705 |
+ |
} |
706 |
+ |
|
707 |
|
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
708 |
|
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance(); |
709 |
< |
assertEquals(result.size(), 0); |
709 |
> |
assertEquals(0, result.size()); |
710 |
|
assertFalse(result.iterator().hasNext()); |
711 |
|
return result; |
712 |
|
} |
867 |
|
*/ |
868 |
|
void check(NavigableSet<Integer> set, |
869 |
|
final int min, final int max, final boolean ascending) { |
870 |
< |
class ReferenceSet { |
870 |
> |
class ReferenceSet { |
871 |
|
int lower(int element) { |
872 |
|
return ascending ? |
873 |
|
lowerAscending(element) : higherAscending(element); |
937 |
|
if (bsContainsI) |
938 |
|
size++; |
939 |
|
} |
940 |
< |
assertEquals(set.size(), size); |
940 |
> |
assertEquals(size, set.size()); |
941 |
|
|
942 |
|
// Test contents using contains elementSet iterator |
943 |
|
int size2 = 0; |