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 |
|
/** |
108 |
|
* Set contains all elements of collection used to initialize |
109 |
|
*/ |
110 |
|
public void testConstructor6() { |
111 |
< |
try { |
112 |
< |
Integer[] ints = new Integer[SIZE]; |
113 |
< |
for (int i = 0; i < SIZE; ++i) |
114 |
< |
ints[i] = new Integer(i); |
115 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
116 |
< |
for (int i = 0; i < SIZE; ++i) |
120 |
< |
assertEquals(ints[i], q.pollFirst()); |
121 |
< |
} |
122 |
< |
finally {} |
111 |
> |
Integer[] ints = new Integer[SIZE]; |
112 |
> |
for (int i = 0; i < SIZE; ++i) |
113 |
> |
ints[i] = new Integer(i); |
114 |
> |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
115 |
> |
for (int i = 0; i < SIZE; ++i) |
116 |
> |
assertEquals(ints[i], q.pollFirst()); |
117 |
|
} |
118 |
|
|
119 |
|
/** |
120 |
|
* The comparator used in constructor is used |
121 |
|
*/ |
122 |
|
public void testConstructor7() { |
123 |
< |
try { |
124 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
125 |
< |
TreeSet q = new TreeSet(cmp); |
126 |
< |
assertEquals(cmp, q.comparator()); |
127 |
< |
Integer[] ints = new Integer[SIZE]; |
128 |
< |
for (int i = 0; i < SIZE; ++i) |
129 |
< |
ints[i] = new Integer(i); |
130 |
< |
q.addAll(Arrays.asList(ints)); |
131 |
< |
for (int i = SIZE-1; i >= 0; --i) |
138 |
< |
assertEquals(ints[i], q.pollFirst()); |
139 |
< |
} |
140 |
< |
finally {} |
123 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
124 |
> |
TreeSet q = new TreeSet(cmp); |
125 |
> |
assertEquals(cmp, q.comparator()); |
126 |
> |
Integer[] ints = new Integer[SIZE]; |
127 |
> |
for (int i = 0; i < SIZE; ++i) |
128 |
> |
ints[i] = new Integer(i); |
129 |
> |
q.addAll(Arrays.asList(ints)); |
130 |
> |
for (int i = SIZE-1; i >= 0; --i) |
131 |
> |
assertEquals(ints[i], q.pollFirst()); |
132 |
|
} |
133 |
|
|
134 |
|
/** |
164 |
|
* add(null) throws NPE if nonempty |
165 |
|
*/ |
166 |
|
public void testAddNull() { |
167 |
< |
try { |
167 |
> |
try { |
168 |
|
TreeSet q = populatedSet(SIZE); |
169 |
|
q.add(null); |
170 |
|
shouldThrow(); |
171 |
< |
} catch (NullPointerException success) { } |
171 |
> |
} catch (NullPointerException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
/** |
199 |
|
q.add(new Object()); |
200 |
|
q.add(new Object()); |
201 |
|
shouldThrow(); |
202 |
< |
} |
212 |
< |
catch(ClassCastException success) {} |
202 |
> |
} catch (ClassCastException success) {} |
203 |
|
} |
204 |
|
|
205 |
|
/** |
210 |
|
TreeSet q = new TreeSet(); |
211 |
|
q.addAll(null); |
212 |
|
shouldThrow(); |
213 |
< |
} |
224 |
< |
catch (NullPointerException success) {} |
213 |
> |
} catch (NullPointerException success) {} |
214 |
|
} |
215 |
|
/** |
216 |
|
* addAll of a collection with null elements throws NPE |
221 |
|
Integer[] ints = new Integer[SIZE]; |
222 |
|
q.addAll(Arrays.asList(ints)); |
223 |
|
shouldThrow(); |
224 |
< |
} |
236 |
< |
catch (NullPointerException success) {} |
224 |
> |
} catch (NullPointerException success) {} |
225 |
|
} |
226 |
|
/** |
227 |
|
* addAll of a collection with any null elements throws NPE after |
235 |
|
ints[i] = new Integer(i); |
236 |
|
q.addAll(Arrays.asList(ints)); |
237 |
|
shouldThrow(); |
238 |
< |
} |
251 |
< |
catch (NullPointerException success) {} |
238 |
> |
} catch (NullPointerException success) {} |
239 |
|
} |
240 |
|
|
241 |
|
/** |
242 |
|
* Set contains all elements of successful addAll |
243 |
|
*/ |
244 |
|
public void testAddAll5() { |
245 |
< |
try { |
246 |
< |
Integer[] empty = new Integer[0]; |
247 |
< |
Integer[] ints = new Integer[SIZE]; |
248 |
< |
for (int i = 0; i < SIZE; ++i) |
249 |
< |
ints[i] = new Integer(SIZE-1-i); |
250 |
< |
TreeSet q = new TreeSet(); |
251 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
252 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
253 |
< |
for (int i = 0; i < SIZE; ++i) |
267 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
268 |
< |
} |
269 |
< |
finally {} |
245 |
> |
Integer[] empty = new Integer[0]; |
246 |
> |
Integer[] ints = new Integer[SIZE]; |
247 |
> |
for (int i = 0; i < SIZE; ++i) |
248 |
> |
ints[i] = new Integer(SIZE-1-i); |
249 |
> |
TreeSet q = new TreeSet(); |
250 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
251 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
252 |
> |
for (int i = 0; i < SIZE; ++i) |
253 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
254 |
|
} |
255 |
|
|
256 |
|
/** |
261 |
|
for (int i = 0; i < SIZE; ++i) { |
262 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
263 |
|
} |
264 |
< |
assertNull(q.pollFirst()); |
264 |
> |
assertNull(q.pollFirst()); |
265 |
|
} |
266 |
|
|
267 |
|
/** |
272 |
|
for (int i = SIZE-1; i >= 0; --i) { |
273 |
|
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
274 |
|
} |
275 |
< |
assertNull(q.pollFirst()); |
275 |
> |
assertNull(q.pollFirst()); |
276 |
|
} |
277 |
|
|
278 |
|
|
449 |
|
*/ |
450 |
|
public void testToArray() { |
451 |
|
TreeSet q = populatedSet(SIZE); |
452 |
< |
Object[] o = q.toArray(); |
452 |
> |
Object[] o = q.toArray(); |
453 |
|
Arrays.sort(o); |
454 |
< |
for(int i = 0; i < o.length; i++) |
455 |
< |
assertEquals(o[i], q.pollFirst()); |
454 |
> |
for (int i = 0; i < o.length; i++) |
455 |
> |
assertEquals(o[i], q.pollFirst()); |
456 |
|
} |
457 |
|
|
458 |
|
/** |
460 |
|
*/ |
461 |
|
public void testToArray2() { |
462 |
|
TreeSet q = populatedSet(SIZE); |
463 |
< |
Integer[] ints = new Integer[SIZE]; |
464 |
< |
ints = (Integer[])q.toArray(ints); |
463 |
> |
Integer[] ints = new Integer[SIZE]; |
464 |
> |
ints = (Integer[])q.toArray(ints); |
465 |
|
Arrays.sort(ints); |
466 |
< |
for(int i = 0; i < ints.length; i++) |
466 |
> |
for (int i = 0; i < ints.length; i++) |
467 |
|
assertEquals(ints[i], q.pollFirst()); |
468 |
|
} |
469 |
|
|
473 |
|
public void testIterator() { |
474 |
|
TreeSet q = populatedSet(SIZE); |
475 |
|
int i = 0; |
476 |
< |
Iterator it = q.iterator(); |
477 |
< |
while(it.hasNext()) { |
476 |
> |
Iterator it = q.iterator(); |
477 |
> |
while (it.hasNext()) { |
478 |
|
assertTrue(q.contains(it.next())); |
479 |
|
++i; |
480 |
|
} |
487 |
|
public void testEmptyIterator() { |
488 |
|
TreeSet q = new TreeSet(); |
489 |
|
int i = 0; |
490 |
< |
Iterator it = q.iterator(); |
491 |
< |
while(it.hasNext()) { |
490 |
> |
Iterator it = q.iterator(); |
491 |
> |
while (it.hasNext()) { |
492 |
|
assertTrue(q.contains(it.next())); |
493 |
|
++i; |
494 |
|
} |
529 |
|
/** |
530 |
|
* A deserialized serialized set has same elements |
531 |
|
*/ |
532 |
< |
public void testSerialization() { |
532 |
> |
public void testSerialization() throws Exception { |
533 |
|
TreeSet q = populatedSet(SIZE); |
534 |
< |
try { |
535 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
536 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
537 |
< |
out.writeObject(q); |
538 |
< |
out.close(); |
539 |
< |
|
540 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
541 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
542 |
< |
TreeSet r = (TreeSet)in.readObject(); |
543 |
< |
assertEquals(q.size(), r.size()); |
544 |
< |
while (!q.isEmpty()) |
561 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
562 |
< |
} catch(Exception e){ |
563 |
< |
e.printStackTrace(); |
564 |
< |
unexpectedException(); |
565 |
< |
} |
534 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
535 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
536 |
> |
out.writeObject(q); |
537 |
> |
out.close(); |
538 |
> |
|
539 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
540 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
541 |
> |
TreeSet r = (TreeSet)in.readObject(); |
542 |
> |
assertEquals(q.size(), r.size()); |
543 |
> |
while (!q.isEmpty()) |
544 |
> |
assertEquals(q.pollFirst(), r.pollFirst()); |
545 |
|
} |
546 |
|
|
547 |
|
/** |
670 |
|
* Subsets of subsets subdivide correctly |
671 |
|
*/ |
672 |
|
public void testRecursiveSubSets() { |
673 |
< |
int setSize = 1000; |
674 |
< |
Class cl = TreeSet.class; |
673 |
> |
int setSize = 1000; |
674 |
> |
Class cl = TreeSet.class; |
675 |
|
|
676 |
|
NavigableSet<Integer> set = newSet(cl); |
677 |
|
bs = new BitSet(setSize); |
690 |
|
|
691 |
|
static NavigableSet<Integer> newSet(Class cl) { |
692 |
|
NavigableSet<Integer> result = null; |
693 |
< |
try { |
693 |
> |
try { |
694 |
|
result = (NavigableSet<Integer>) cl.newInstance(); |
695 |
< |
} catch(Exception e) { |
695 |
> |
} catch (Exception e) { |
696 |
|
fail(); |
697 |
< |
} |
697 |
> |
} |
698 |
|
assertEquals(result.size(), 0); |
699 |
|
assertFalse(result.iterator().hasNext()); |
700 |
|
return result; |
717 |
|
} |
718 |
|
|
719 |
|
// Remove a bunch of entries with iterator |
720 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
720 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
721 |
|
if (rnd.nextBoolean()) { |
722 |
|
bs.clear(it.next()); |
723 |
|
it.remove(); |
742 |
|
} |
743 |
|
|
744 |
|
// Remove a bunch of entries with iterator |
745 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
745 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
746 |
|
if (rnd.nextBoolean()) { |
747 |
|
bs.clear(it.next()); |
748 |
|
it.remove(); |
758 |
|
try { |
759 |
|
set.add(element); |
760 |
|
fail(); |
761 |
< |
} catch(IllegalArgumentException e) { |
761 |
> |
} catch (IllegalArgumentException e) { |
762 |
|
// expected |
763 |
|
} |
764 |
|
} |
960 |
|
try { |
961 |
|
set.first(); |
962 |
|
fail(); |
963 |
< |
} catch(NoSuchElementException e) { |
963 |
> |
} catch (NoSuchElementException e) { |
964 |
|
// expected |
965 |
|
} |
966 |
|
try { |
967 |
|
set.last(); |
968 |
|
fail(); |
969 |
< |
} catch(NoSuchElementException e) { |
969 |
> |
} catch (NoSuchElementException e) { |
970 |
|
// expected |
971 |
|
} |
972 |
|
} |