11 |
|
|
12 |
|
public class TreeSetTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
< |
return new TestSuite(TreeSetTest.class); |
17 |
> |
return new TestSuite(TreeSetTest.class); |
18 |
|
} |
19 |
|
|
20 |
|
static class MyReverseComparator implements Comparator { |
39 |
|
private TreeSet populatedSet(int n) { |
40 |
|
TreeSet q = new TreeSet(); |
41 |
|
assertTrue(q.isEmpty()); |
42 |
< |
for(int i = n-1; i >= 0; i-=2) |
43 |
< |
assertTrue(q.add(new Integer(i))); |
44 |
< |
for(int i = (n & 1); i < n; i+=2) |
45 |
< |
assertTrue(q.add(new Integer(i))); |
42 |
> |
for (int i = n-1; i >= 0; i-=2) |
43 |
> |
assertTrue(q.add(new Integer(i))); |
44 |
> |
for (int i = (n & 1); i < n; i+=2) |
45 |
> |
assertTrue(q.add(new Integer(i))); |
46 |
|
assertFalse(q.isEmpty()); |
47 |
< |
assertEquals(n, q.size()); |
47 |
> |
assertEquals(n, q.size()); |
48 |
|
return q; |
49 |
|
} |
50 |
|
|
59 |
|
q.add(three); |
60 |
|
q.add(four); |
61 |
|
q.add(five); |
62 |
< |
assertEquals(5, q.size()); |
62 |
> |
assertEquals(5, q.size()); |
63 |
|
return q; |
64 |
|
} |
65 |
|
|
77 |
|
try { |
78 |
|
TreeSet q = new TreeSet((Collection)null); |
79 |
|
shouldThrow(); |
80 |
< |
} |
81 |
< |
catch (NullPointerException success) {} |
80 |
> |
} catch (NullPointerException success) {} |
81 |
|
} |
82 |
|
|
83 |
|
/** |
88 |
|
Integer[] ints = new Integer[SIZE]; |
89 |
|
TreeSet q = new TreeSet(Arrays.asList(ints)); |
90 |
|
shouldThrow(); |
91 |
< |
} |
93 |
< |
catch (NullPointerException success) {} |
91 |
> |
} catch (NullPointerException success) {} |
92 |
|
} |
93 |
|
|
94 |
|
/** |
101 |
|
ints[i] = new Integer(i); |
102 |
|
TreeSet q = new TreeSet(Arrays.asList(ints)); |
103 |
|
shouldThrow(); |
104 |
< |
} |
107 |
< |
catch (NullPointerException success) {} |
104 |
> |
} catch (NullPointerException success) {} |
105 |
|
} |
106 |
|
|
107 |
|
/** |
170 |
|
* add(null) throws NPE if nonempty |
171 |
|
*/ |
172 |
|
public void testAddNull() { |
173 |
< |
try { |
173 |
> |
try { |
174 |
|
TreeSet q = populatedSet(SIZE); |
175 |
|
q.add(null); |
176 |
|
shouldThrow(); |
177 |
< |
} catch (NullPointerException success) { } |
177 |
> |
} catch (NullPointerException success) {} |
178 |
|
} |
179 |
|
|
180 |
|
/** |
205 |
|
q.add(new Object()); |
206 |
|
q.add(new Object()); |
207 |
|
shouldThrow(); |
208 |
< |
} |
212 |
< |
catch(ClassCastException success) {} |
208 |
> |
} catch (ClassCastException success) {} |
209 |
|
} |
210 |
|
|
211 |
|
/** |
216 |
|
TreeSet q = new TreeSet(); |
217 |
|
q.addAll(null); |
218 |
|
shouldThrow(); |
219 |
< |
} |
224 |
< |
catch (NullPointerException success) {} |
219 |
> |
} catch (NullPointerException success) {} |
220 |
|
} |
221 |
|
/** |
222 |
|
* addAll of a collection with null elements throws NPE |
227 |
|
Integer[] ints = new Integer[SIZE]; |
228 |
|
q.addAll(Arrays.asList(ints)); |
229 |
|
shouldThrow(); |
230 |
< |
} |
236 |
< |
catch (NullPointerException success) {} |
230 |
> |
} catch (NullPointerException success) {} |
231 |
|
} |
232 |
|
/** |
233 |
|
* addAll of a collection with any null elements throws NPE after |
241 |
|
ints[i] = new Integer(i); |
242 |
|
q.addAll(Arrays.asList(ints)); |
243 |
|
shouldThrow(); |
244 |
< |
} |
251 |
< |
catch (NullPointerException success) {} |
244 |
> |
} catch (NullPointerException success) {} |
245 |
|
} |
246 |
|
|
247 |
|
/** |
270 |
|
for (int i = 0; i < SIZE; ++i) { |
271 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
272 |
|
} |
273 |
< |
assertNull(q.pollFirst()); |
273 |
> |
assertNull(q.pollFirst()); |
274 |
|
} |
275 |
|
|
276 |
|
/** |
281 |
|
for (int i = SIZE-1; i >= 0; --i) { |
282 |
|
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
283 |
|
} |
284 |
< |
assertNull(q.pollFirst()); |
284 |
> |
assertNull(q.pollFirst()); |
285 |
|
} |
286 |
|
|
287 |
|
|
458 |
|
*/ |
459 |
|
public void testToArray() { |
460 |
|
TreeSet q = populatedSet(SIZE); |
461 |
< |
Object[] o = q.toArray(); |
461 |
> |
Object[] o = q.toArray(); |
462 |
|
Arrays.sort(o); |
463 |
< |
for(int i = 0; i < o.length; i++) |
464 |
< |
assertEquals(o[i], q.pollFirst()); |
463 |
> |
for (int i = 0; i < o.length; i++) |
464 |
> |
assertEquals(o[i], q.pollFirst()); |
465 |
|
} |
466 |
|
|
467 |
|
/** |
469 |
|
*/ |
470 |
|
public void testToArray2() { |
471 |
|
TreeSet q = populatedSet(SIZE); |
472 |
< |
Integer[] ints = new Integer[SIZE]; |
473 |
< |
ints = (Integer[])q.toArray(ints); |
472 |
> |
Integer[] ints = new Integer[SIZE]; |
473 |
> |
ints = (Integer[])q.toArray(ints); |
474 |
|
Arrays.sort(ints); |
475 |
< |
for(int i = 0; i < ints.length; i++) |
475 |
> |
for (int i = 0; i < ints.length; i++) |
476 |
|
assertEquals(ints[i], q.pollFirst()); |
477 |
|
} |
478 |
|
|
482 |
|
public void testIterator() { |
483 |
|
TreeSet q = populatedSet(SIZE); |
484 |
|
int i = 0; |
485 |
< |
Iterator it = q.iterator(); |
486 |
< |
while(it.hasNext()) { |
485 |
> |
Iterator it = q.iterator(); |
486 |
> |
while (it.hasNext()) { |
487 |
|
assertTrue(q.contains(it.next())); |
488 |
|
++i; |
489 |
|
} |
496 |
|
public void testEmptyIterator() { |
497 |
|
TreeSet q = new TreeSet(); |
498 |
|
int i = 0; |
499 |
< |
Iterator it = q.iterator(); |
500 |
< |
while(it.hasNext()) { |
499 |
> |
Iterator it = q.iterator(); |
500 |
> |
while (it.hasNext()) { |
501 |
|
assertTrue(q.contains(it.next())); |
502 |
|
++i; |
503 |
|
} |
538 |
|
/** |
539 |
|
* A deserialized serialized set has same elements |
540 |
|
*/ |
541 |
< |
public void testSerialization() { |
541 |
> |
public void testSerialization() throws Exception { |
542 |
|
TreeSet q = populatedSet(SIZE); |
543 |
< |
try { |
544 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
545 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
546 |
< |
out.writeObject(q); |
547 |
< |
out.close(); |
548 |
< |
|
549 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
550 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
551 |
< |
TreeSet r = (TreeSet)in.readObject(); |
552 |
< |
assertEquals(q.size(), r.size()); |
553 |
< |
while (!q.isEmpty()) |
561 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
562 |
< |
} catch(Exception e){ |
563 |
< |
e.printStackTrace(); |
564 |
< |
unexpectedException(); |
565 |
< |
} |
543 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
544 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
545 |
> |
out.writeObject(q); |
546 |
> |
out.close(); |
547 |
> |
|
548 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
549 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
550 |
> |
TreeSet r = (TreeSet)in.readObject(); |
551 |
> |
assertEquals(q.size(), r.size()); |
552 |
> |
while (!q.isEmpty()) |
553 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
554 |
|
} |
555 |
|
|
556 |
|
/** |
679 |
|
* Subsets of subsets subdivide correctly |
680 |
|
*/ |
681 |
|
public void testRecursiveSubSets() { |
682 |
< |
int setSize = 1000; |
683 |
< |
Class cl = TreeSet.class; |
682 |
> |
int setSize = 1000; |
683 |
> |
Class cl = TreeSet.class; |
684 |
|
|
685 |
|
NavigableSet<Integer> set = newSet(cl); |
686 |
|
bs = new BitSet(setSize); |
699 |
|
|
700 |
|
static NavigableSet<Integer> newSet(Class cl) { |
701 |
|
NavigableSet<Integer> result = null; |
702 |
< |
try { |
702 |
> |
try { |
703 |
|
result = (NavigableSet<Integer>) cl.newInstance(); |
704 |
< |
} catch(Exception e) { |
704 |
> |
} catch (Exception e) { |
705 |
|
fail(); |
706 |
< |
} |
706 |
> |
} |
707 |
|
assertEquals(result.size(), 0); |
708 |
|
assertFalse(result.iterator().hasNext()); |
709 |
|
return result; |
726 |
|
} |
727 |
|
|
728 |
|
// Remove a bunch of entries with iterator |
729 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
729 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
730 |
|
if (rnd.nextBoolean()) { |
731 |
|
bs.clear(it.next()); |
732 |
|
it.remove(); |
751 |
|
} |
752 |
|
|
753 |
|
// Remove a bunch of entries with iterator |
754 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
754 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
755 |
|
if (rnd.nextBoolean()) { |
756 |
|
bs.clear(it.next()); |
757 |
|
it.remove(); |
767 |
|
try { |
768 |
|
set.add(element); |
769 |
|
fail(); |
770 |
< |
} catch(IllegalArgumentException e) { |
770 |
> |
} catch (IllegalArgumentException e) { |
771 |
|
// expected |
772 |
|
} |
773 |
|
} |
969 |
|
try { |
970 |
|
set.first(); |
971 |
|
fail(); |
972 |
< |
} catch(NoSuchElementException e) { |
972 |
> |
} catch (NoSuchElementException e) { |
973 |
|
// expected |
974 |
|
} |
975 |
|
try { |
976 |
|
set.last(); |
977 |
|
fail(); |
978 |
< |
} catch(NoSuchElementException e) { |
978 |
> |
} catch (NoSuchElementException e) { |
979 |
|
// expected |
980 |
|
} |
981 |
|
} |