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.*; |
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) { |
40 |
|
* Create a set of given size containing consecutive |
41 |
|
* Integers 0 ... n. |
42 |
|
*/ |
43 |
< |
private TreeSet populatedSet(int n) { |
44 |
< |
TreeSet q = new TreeSet(); |
43 |
> |
private TreeSet<Integer> populatedSet(int n) { |
44 |
> |
TreeSet<Integer> q = new TreeSet<Integer>(); |
45 |
|
assertTrue(q.isEmpty()); |
46 |
|
for (int i = n-1; i >= 0; i-=2) |
47 |
|
assertTrue(q.add(new Integer(i))); |
281 |
|
assertNull(q.pollFirst()); |
282 |
|
} |
283 |
|
|
276 |
– |
|
284 |
|
/** |
285 |
|
* remove(x) removes x and returns true if present |
286 |
|
*/ |
287 |
|
public void testRemoveElement() { |
288 |
|
TreeSet q = populatedSet(SIZE); |
289 |
|
for (int i = 1; i < SIZE; i+=2) { |
290 |
< |
assertTrue(q.remove(new Integer(i))); |
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) { |
296 |
< |
assertTrue(q.remove(new Integer(i))); |
297 |
< |
assertFalse(q.remove(new Integer(i+1))); |
296 |
> |
assertTrue(q.contains(i)); |
297 |
> |
assertTrue(q.remove(i)); |
298 |
> |
assertFalse(q.contains(i)); |
299 |
> |
assertFalse(q.remove(i+1)); |
300 |
> |
assertFalse(q.contains(i+1)); |
301 |
|
} |
302 |
|
assertTrue(q.isEmpty()); |
303 |
|
} |
377 |
|
} |
378 |
|
} |
379 |
|
|
367 |
– |
|
368 |
– |
|
380 |
|
/** |
381 |
|
* lower returns preceding element |
382 |
|
*/ |
450 |
|
} |
451 |
|
|
452 |
|
/** |
453 |
< |
* toArray contains all elements |
453 |
> |
* toArray contains all elements in sorted order |
454 |
|
*/ |
455 |
|
public void testToArray() { |
456 |
|
TreeSet q = populatedSet(SIZE); |
457 |
|
Object[] o = q.toArray(); |
447 |
– |
Arrays.sort(o); |
458 |
|
for (int i = 0; i < o.length; i++) |
459 |
< |
assertEquals(o[i], q.pollFirst()); |
459 |
> |
assertSame(o[i], q.pollFirst()); |
460 |
|
} |
461 |
|
|
462 |
|
/** |
463 |
< |
* toArray(a) contains all elements |
463 |
> |
* toArray(a) contains all elements in sorted order |
464 |
|
*/ |
465 |
|
public void testToArray2() { |
466 |
< |
TreeSet q = populatedSet(SIZE); |
466 |
> |
TreeSet<Integer> q = populatedSet(SIZE); |
467 |
|
Integer[] ints = new Integer[SIZE]; |
468 |
< |
ints = (Integer[])q.toArray(ints); |
469 |
< |
Arrays.sort(ints); |
468 |
> |
Integer[] array = q.toArray(ints); |
469 |
> |
assertSame(ints, array); |
470 |
|
for (int i = 0; i < ints.length; i++) |
471 |
< |
assertEquals(ints[i], q.pollFirst()); |
471 |
> |
assertSame(ints[i], q.pollFirst()); |
472 |
|
} |
473 |
|
|
474 |
|
/** |
518 |
|
assertFalse(it.hasNext()); |
519 |
|
} |
520 |
|
|
511 |
– |
|
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 |
|
/** |
673 |
|
* Subsets of subsets subdivide correctly |
674 |
|
*/ |
675 |
|
public void testRecursiveSubSets() throws Exception { |
676 |
< |
int setSize = 1000; |
676 |
> |
int setSize = expensiveTests ? 1000 : 100; |
677 |
|
Class cl = TreeSet.class; |
678 |
|
|
679 |
|
NavigableSet<Integer> set = newSet(cl); |
854 |
|
*/ |
855 |
|
void check(NavigableSet<Integer> set, |
856 |
|
final int min, final int max, final boolean ascending) { |
857 |
< |
class ReferenceSet { |
857 |
> |
class ReferenceSet { |
858 |
|
int lower(int element) { |
859 |
|
return ascending ? |
860 |
|
lowerAscending(element) : higherAscending(element); |