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) { |
24 |
< |
junit.textui.TestRunner.run (suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
public static Test suite() { |
27 |
< |
return new TestSuite(TreeSetTest.class); |
27 |
> |
return new TestSuite(TreeSetTest.class); |
28 |
|
} |
29 |
|
|
30 |
< |
static class MyReverseComparator implements Comparator { |
30 |
> |
static class MyReverseComparator implements Comparator { |
31 |
|
public int compare(Object x, Object y) { |
32 |
< |
int i = ((Integer)x).intValue(); |
23 |
< |
int j = ((Integer)y).intValue(); |
24 |
< |
if (i < j) return 1; |
25 |
< |
if (i > j) return -1; |
26 |
< |
return 0; |
32 |
> |
return ((Comparable)y).compareTo(x); |
33 |
|
} |
34 |
|
} |
35 |
|
|
39 |
|
static final int SIZE = 20; |
40 |
|
|
41 |
|
/** |
42 |
< |
* Create a set of given size containing consecutive |
43 |
< |
* Integers 0 ... n. |
42 |
> |
* Returns a new set of given size containing consecutive |
43 |
> |
* Integers 0 ... n - 1. |
44 |
|
*/ |
45 |
< |
private TreeSet populatedSet(int n) { |
46 |
< |
TreeSet q = new TreeSet(); |
45 |
> |
private static TreeSet<Integer> populatedSet(int n) { |
46 |
> |
TreeSet<Integer> q = new TreeSet<>(); |
47 |
|
assertTrue(q.isEmpty()); |
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) |
51 |
< |
assertTrue(q.add(new Integer(i))); |
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) |
51 |
> |
assertTrue(q.add(new Integer(i))); |
52 |
|
assertFalse(q.isEmpty()); |
53 |
< |
assertEquals(n, q.size()); |
53 |
> |
assertEquals(n, q.size()); |
54 |
|
return q; |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
< |
* Create set of first 5 ints |
58 |
> |
* Returns a new set of first 5 ints. |
59 |
|
*/ |
60 |
< |
private TreeSet set5() { |
60 |
> |
private static TreeSet set5() { |
61 |
|
TreeSet q = new TreeSet(); |
62 |
|
assertTrue(q.isEmpty()); |
63 |
|
q.add(one); |
65 |
|
q.add(three); |
66 |
|
q.add(four); |
67 |
|
q.add(five); |
68 |
< |
assertEquals(5, q.size()); |
68 |
> |
assertEquals(5, q.size()); |
69 |
|
return q; |
70 |
|
} |
71 |
< |
|
71 |
> |
|
72 |
|
/** |
73 |
|
* A new set has unbounded capacity |
74 |
|
*/ |
81 |
|
*/ |
82 |
|
public void testConstructor3() { |
83 |
|
try { |
84 |
< |
TreeSet q = new TreeSet((Collection)null); |
84 |
> |
new TreeSet((Collection)null); |
85 |
|
shouldThrow(); |
86 |
< |
} |
81 |
< |
catch (NullPointerException success) {} |
86 |
> |
} catch (NullPointerException success) {} |
87 |
|
} |
88 |
|
|
89 |
|
/** |
91 |
|
*/ |
92 |
|
public void testConstructor4() { |
93 |
|
try { |
94 |
< |
Integer[] ints = new Integer[SIZE]; |
90 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
94 |
> |
new TreeSet(Arrays.asList(new Integer[SIZE])); |
95 |
|
shouldThrow(); |
96 |
< |
} |
93 |
< |
catch (NullPointerException success) {} |
96 |
> |
} catch (NullPointerException success) {} |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
|
* Initializing from Collection with some null elements throws NPE |
101 |
|
*/ |
102 |
|
public void testConstructor5() { |
103 |
+ |
Integer[] ints = new Integer[SIZE]; |
104 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
105 |
+ |
ints[i] = new Integer(i); |
106 |
|
try { |
107 |
< |
Integer[] ints = new Integer[SIZE]; |
102 |
< |
for (int i = 0; i < SIZE-1; ++i) |
103 |
< |
ints[i] = new Integer(i); |
104 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
107 |
> |
new TreeSet(Arrays.asList(ints)); |
108 |
|
shouldThrow(); |
109 |
< |
} |
107 |
< |
catch (NullPointerException success) {} |
109 |
> |
} catch (NullPointerException success) {} |
110 |
|
} |
111 |
|
|
112 |
|
/** |
113 |
|
* Set contains all elements of collection used to initialize |
114 |
|
*/ |
115 |
|
public void testConstructor6() { |
116 |
< |
try { |
117 |
< |
Integer[] ints = new Integer[SIZE]; |
118 |
< |
for (int i = 0; i < SIZE; ++i) |
119 |
< |
ints[i] = new Integer(i); |
120 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
121 |
< |
for (int i = 0; i < SIZE; ++i) |
120 |
< |
assertEquals(ints[i], q.pollFirst()); |
121 |
< |
} |
122 |
< |
finally {} |
116 |
> |
Integer[] ints = new Integer[SIZE]; |
117 |
> |
for (int i = 0; i < SIZE; ++i) |
118 |
> |
ints[i] = new Integer(i); |
119 |
> |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
120 |
> |
for (int i = 0; i < SIZE; ++i) |
121 |
> |
assertEquals(ints[i], q.pollFirst()); |
122 |
|
} |
123 |
|
|
124 |
|
/** |
125 |
|
* The comparator used in constructor is used |
126 |
|
*/ |
127 |
|
public void testConstructor7() { |
128 |
< |
try { |
129 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
130 |
< |
TreeSet q = new TreeSet(cmp); |
131 |
< |
assertEquals(cmp, q.comparator()); |
132 |
< |
Integer[] ints = new Integer[SIZE]; |
133 |
< |
for (int i = 0; i < SIZE; ++i) |
134 |
< |
ints[i] = new Integer(i); |
135 |
< |
q.addAll(Arrays.asList(ints)); |
136 |
< |
for (int i = SIZE-1; i >= 0; --i) |
138 |
< |
assertEquals(ints[i], q.pollFirst()); |
139 |
< |
} |
140 |
< |
finally {} |
128 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
129 |
> |
TreeSet q = new TreeSet(cmp); |
130 |
> |
assertEquals(cmp, q.comparator()); |
131 |
> |
Integer[] ints = new Integer[SIZE]; |
132 |
> |
for (int i = 0; i < SIZE; ++i) |
133 |
> |
ints[i] = new Integer(i); |
134 |
> |
q.addAll(Arrays.asList(ints)); |
135 |
> |
for (int i = SIZE - 1; i >= 0; --i) |
136 |
> |
assertEquals(ints[i], q.pollFirst()); |
137 |
|
} |
138 |
|
|
139 |
|
/** |
156 |
|
public void testSize() { |
157 |
|
TreeSet q = populatedSet(SIZE); |
158 |
|
for (int i = 0; i < SIZE; ++i) { |
159 |
< |
assertEquals(SIZE-i, q.size()); |
159 |
> |
assertEquals(SIZE - i, q.size()); |
160 |
|
q.pollFirst(); |
161 |
|
} |
162 |
|
for (int i = 0; i < SIZE; ++i) { |
169 |
|
* add(null) throws NPE if nonempty |
170 |
|
*/ |
171 |
|
public void testAddNull() { |
172 |
< |
try { |
173 |
< |
TreeSet q = populatedSet(SIZE); |
172 |
> |
TreeSet q = populatedSet(SIZE); |
173 |
> |
try { |
174 |
|
q.add(null); |
175 |
|
shouldThrow(); |
176 |
< |
} catch (NullPointerException success) { } |
176 |
> |
} catch (NullPointerException success) {} |
177 |
|
} |
178 |
|
|
179 |
|
/** |
198 |
|
* Add of non-Comparable throws CCE |
199 |
|
*/ |
200 |
|
public void testAddNonComparable() { |
201 |
+ |
TreeSet q = new TreeSet(); |
202 |
|
try { |
206 |
– |
TreeSet q = new TreeSet(); |
207 |
– |
q.add(new Object()); |
203 |
|
q.add(new Object()); |
204 |
|
q.add(new Object()); |
205 |
|
shouldThrow(); |
206 |
< |
} |
212 |
< |
catch(ClassCastException success) {} |
206 |
> |
} catch (ClassCastException success) {} |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
|
* addAll(null) throws NPE |
211 |
|
*/ |
212 |
|
public void testAddAll1() { |
213 |
+ |
TreeSet q = new TreeSet(); |
214 |
|
try { |
220 |
– |
TreeSet q = new TreeSet(); |
215 |
|
q.addAll(null); |
216 |
|
shouldThrow(); |
217 |
< |
} |
224 |
< |
catch (NullPointerException success) {} |
217 |
> |
} catch (NullPointerException success) {} |
218 |
|
} |
219 |
+ |
|
220 |
|
/** |
221 |
|
* addAll of a collection with null elements throws NPE |
222 |
|
*/ |
223 |
|
public void testAddAll2() { |
224 |
+ |
TreeSet q = new TreeSet(); |
225 |
+ |
Integer[] ints = new Integer[SIZE]; |
226 |
|
try { |
231 |
– |
TreeSet q = new TreeSet(); |
232 |
– |
Integer[] ints = new Integer[SIZE]; |
227 |
|
q.addAll(Arrays.asList(ints)); |
228 |
|
shouldThrow(); |
229 |
< |
} |
236 |
< |
catch (NullPointerException success) {} |
229 |
> |
} catch (NullPointerException success) {} |
230 |
|
} |
231 |
+ |
|
232 |
|
/** |
233 |
|
* addAll of a collection with any null elements throws NPE after |
234 |
|
* possibly adding some elements |
235 |
|
*/ |
236 |
|
public void testAddAll3() { |
237 |
+ |
TreeSet q = new TreeSet(); |
238 |
+ |
Integer[] ints = new Integer[SIZE]; |
239 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
240 |
+ |
ints[i] = new Integer(i); |
241 |
|
try { |
244 |
– |
TreeSet q = new TreeSet(); |
245 |
– |
Integer[] ints = new Integer[SIZE]; |
246 |
– |
for (int i = 0; i < SIZE-1; ++i) |
247 |
– |
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 |
|
/** |
248 |
|
* Set contains all elements of successful addAll |
249 |
|
*/ |
250 |
|
public void testAddAll5() { |
251 |
< |
try { |
252 |
< |
Integer[] empty = new Integer[0]; |
253 |
< |
Integer[] ints = new Integer[SIZE]; |
254 |
< |
for (int i = 0; i < SIZE; ++i) |
255 |
< |
ints[i] = new Integer(SIZE-1-i); |
256 |
< |
TreeSet q = new TreeSet(); |
257 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
258 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
259 |
< |
for (int i = 0; i < SIZE; ++i) |
267 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
268 |
< |
} |
269 |
< |
finally {} |
251 |
> |
Integer[] empty = new Integer[0]; |
252 |
> |
Integer[] ints = new Integer[SIZE]; |
253 |
> |
for (int i = 0; i < SIZE; ++i) |
254 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
255 |
> |
TreeSet q = new TreeSet(); |
256 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
257 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
258 |
> |
for (int i = 0; i < SIZE; ++i) |
259 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
260 |
|
} |
261 |
|
|
262 |
|
/** |
265 |
|
public void testPollFirst() { |
266 |
|
TreeSet q = populatedSet(SIZE); |
267 |
|
for (int i = 0; i < SIZE; ++i) { |
268 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
268 |
> |
assertEquals(i, q.pollFirst()); |
269 |
|
} |
270 |
< |
assertNull(q.pollFirst()); |
270 |
> |
assertNull(q.pollFirst()); |
271 |
|
} |
272 |
|
|
273 |
|
/** |
275 |
|
*/ |
276 |
|
public void testPollLast() { |
277 |
|
TreeSet q = populatedSet(SIZE); |
278 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
279 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
278 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
279 |
> |
assertEquals(i, q.pollLast()); |
280 |
|
} |
281 |
< |
assertNull(q.pollFirst()); |
281 |
> |
assertNull(q.pollFirst()); |
282 |
|
} |
283 |
|
|
294 |
– |
|
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))); |
291 |
< |
} |
292 |
< |
for (int i = 0; i < SIZE; i+=2) { |
293 |
< |
assertTrue(q.remove(new Integer(i))); |
294 |
< |
assertFalse(q.remove(new Integer(i+1))); |
289 |
> |
for (int i = 1; i < SIZE; i += 2) { |
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.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 |
|
} |
304 |
< |
|
304 |
> |
|
305 |
|
/** |
306 |
|
* contains(x) reports true when elements added but not yet removed |
307 |
|
*/ |
356 |
|
assertTrue(changed); |
357 |
|
|
358 |
|
assertTrue(q.containsAll(p)); |
359 |
< |
assertEquals(SIZE-i, q.size()); |
359 |
> |
assertEquals(SIZE - i, q.size()); |
360 |
|
p.pollFirst(); |
361 |
|
} |
362 |
|
} |
369 |
|
TreeSet q = populatedSet(SIZE); |
370 |
|
TreeSet p = populatedSet(i); |
371 |
|
assertTrue(q.removeAll(p)); |
372 |
< |
assertEquals(SIZE-i, q.size()); |
372 |
> |
assertEquals(SIZE - i, q.size()); |
373 |
|
for (int j = 0; j < i; ++j) { |
374 |
< |
Integer I = (Integer)(p.pollFirst()); |
375 |
< |
assertFalse(q.contains(I)); |
374 |
> |
Integer x = (Integer)(p.pollFirst()); |
375 |
> |
assertFalse(q.contains(x)); |
376 |
|
} |
377 |
|
} |
378 |
|
} |
379 |
|
|
385 |
– |
|
386 |
– |
|
380 |
|
/** |
381 |
|
* lower returns preceding element |
382 |
|
*/ |
393 |
|
|
394 |
|
Object e4 = q.lower(zero); |
395 |
|
assertNull(e4); |
403 |
– |
|
396 |
|
} |
397 |
|
|
398 |
|
/** |
411 |
|
|
412 |
|
Object e4 = q.higher(six); |
413 |
|
assertNull(e4); |
422 |
– |
|
414 |
|
} |
415 |
|
|
416 |
|
/** |
429 |
|
|
430 |
|
Object e4 = q.floor(zero); |
431 |
|
assertNull(e4); |
441 |
– |
|
432 |
|
} |
433 |
|
|
434 |
|
/** |
447 |
|
|
448 |
|
Object e4 = q.ceiling(six); |
449 |
|
assertNull(e4); |
460 |
– |
|
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(); |
458 |
< |
Arrays.sort(o); |
459 |
< |
for(int i = 0; i < o.length; i++) |
471 |
< |
assertEquals(o[i], q.pollFirst()); |
457 |
> |
Object[] o = q.toArray(); |
458 |
> |
for (int i = 0; i < o.length; i++) |
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); |
467 |
< |
Integer[] ints = new Integer[SIZE]; |
468 |
< |
ints = (Integer[])q.toArray(ints); |
469 |
< |
Arrays.sort(ints); |
470 |
< |
for(int i = 0; i < ints.length; i++) |
471 |
< |
assertEquals(ints[i], q.pollFirst()); |
466 |
> |
TreeSet<Integer> q = populatedSet(SIZE); |
467 |
> |
Integer[] ints = new Integer[SIZE]; |
468 |
> |
Integer[] array = q.toArray(ints); |
469 |
> |
assertSame(ints, array); |
470 |
> |
for (int i = 0; i < ints.length; i++) |
471 |
> |
assertSame(ints[i], q.pollFirst()); |
472 |
|
} |
473 |
< |
|
473 |
> |
|
474 |
|
/** |
475 |
|
* iterator iterates through all elements |
476 |
|
*/ |
477 |
|
public void testIterator() { |
478 |
|
TreeSet q = populatedSet(SIZE); |
479 |
< |
int i = 0; |
480 |
< |
Iterator it = q.iterator(); |
481 |
< |
while(it.hasNext()) { |
479 |
> |
Iterator it = q.iterator(); |
480 |
> |
int i; |
481 |
> |
for (i = 0; it.hasNext(); i++) |
482 |
|
assertTrue(q.contains(it.next())); |
495 |
– |
++i; |
496 |
– |
} |
483 |
|
assertEquals(i, SIZE); |
484 |
+ |
assertIteratorExhausted(it); |
485 |
|
} |
486 |
|
|
487 |
|
/** |
488 |
|
* iterator of empty set has no elements |
489 |
|
*/ |
490 |
|
public void testEmptyIterator() { |
491 |
< |
TreeSet q = new TreeSet(); |
505 |
< |
int i = 0; |
506 |
< |
Iterator it = q.iterator(); |
507 |
< |
while(it.hasNext()) { |
508 |
< |
assertTrue(q.contains(it.next())); |
509 |
< |
++i; |
510 |
< |
} |
511 |
< |
assertEquals(i, 0); |
491 |
> |
assertIteratorExhausted(new TreeSet().iterator()); |
492 |
|
} |
493 |
|
|
494 |
|
/** |
495 |
|
* iterator.remove removes current element |
496 |
|
*/ |
497 |
< |
public void testIteratorRemove () { |
497 |
> |
public void testIteratorRemove() { |
498 |
|
final TreeSet q = new TreeSet(); |
499 |
|
q.add(new Integer(2)); |
500 |
|
q.add(new Integer(1)); |
510 |
|
assertFalse(it.hasNext()); |
511 |
|
} |
512 |
|
|
533 |
– |
|
513 |
|
/** |
514 |
|
* toString contains toStrings of elements |
515 |
|
*/ |
517 |
|
TreeSet q = populatedSet(SIZE); |
518 |
|
String s = q.toString(); |
519 |
|
for (int i = 0; i < SIZE; ++i) { |
520 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
520 |
> |
assertTrue(s.contains(String.valueOf(i))); |
521 |
|
} |
522 |
< |
} |
522 |
> |
} |
523 |
|
|
524 |
|
/** |
525 |
< |
* A deserialized serialized set has same elements |
525 |
> |
* A deserialized/reserialized set equals original |
526 |
|
*/ |
527 |
< |
public void testSerialization() { |
528 |
< |
TreeSet q = populatedSet(SIZE); |
529 |
< |
try { |
530 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
531 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
532 |
< |
out.writeObject(q); |
533 |
< |
out.close(); |
534 |
< |
|
535 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
536 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
537 |
< |
TreeSet r = (TreeSet)in.readObject(); |
559 |
< |
assertEquals(q.size(), r.size()); |
560 |
< |
while (!q.isEmpty()) |
561 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
562 |
< |
} catch(Exception e){ |
563 |
< |
e.printStackTrace(); |
564 |
< |
unexpectedException(); |
527 |
> |
public void testSerialization() throws Exception { |
528 |
> |
NavigableSet x = populatedSet(SIZE); |
529 |
> |
NavigableSet y = serialClone(x); |
530 |
> |
|
531 |
> |
assertNotSame(x, y); |
532 |
> |
assertEquals(x.size(), y.size()); |
533 |
> |
assertEquals(x, y); |
534 |
> |
assertEquals(y, x); |
535 |
> |
while (!x.isEmpty()) { |
536 |
> |
assertFalse(y.isEmpty()); |
537 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
538 |
|
} |
539 |
+ |
assertTrue(y.isEmpty()); |
540 |
|
} |
541 |
|
|
542 |
|
/** |
664 |
|
/** |
665 |
|
* Subsets of subsets subdivide correctly |
666 |
|
*/ |
667 |
< |
public void testRecursiveSubSets() { |
668 |
< |
int setSize = 1000; |
669 |
< |
Class cl = TreeSet.class; |
667 |
> |
public void testRecursiveSubSets() throws Exception { |
668 |
> |
int setSize = expensiveTests ? 1000 : 100; |
669 |
> |
Class cl = TreeSet.class; |
670 |
|
|
671 |
|
NavigableSet<Integer> set = newSet(cl); |
672 |
|
bs = new BitSet(setSize); |
679 |
|
check(set, 0, setSize - 1, true); |
680 |
|
check(set.descendingSet(), 0, setSize - 1, false); |
681 |
|
|
682 |
< |
bashSubSet(set.navigableSubSet(0, true, setSize, false), |
682 |
> |
bashSubSet(set.subSet(0, true, setSize, false), |
683 |
|
0, setSize - 1, true); |
684 |
|
} |
685 |
|
|
686 |
< |
static NavigableSet<Integer> newSet(Class cl) { |
687 |
< |
NavigableSet<Integer> result = null; |
688 |
< |
try { |
689 |
< |
result = (NavigableSet<Integer>) cl.newInstance(); |
690 |
< |
} catch(Exception e) { |
691 |
< |
fail(); |
692 |
< |
} |
693 |
< |
assertEquals(result.size(), 0); |
686 |
> |
/** |
687 |
> |
* addAll is idempotent |
688 |
> |
*/ |
689 |
> |
public void testAddAll_idempotent() throws Exception { |
690 |
> |
Set x = populatedSet(SIZE); |
691 |
> |
Set y = new TreeSet(x); |
692 |
> |
y.addAll(x); |
693 |
> |
assertEquals(x, y); |
694 |
> |
assertEquals(y, x); |
695 |
> |
} |
696 |
> |
|
697 |
> |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
698 |
> |
NavigableSet<Integer> result = |
699 |
> |
(NavigableSet<Integer>) cl.getConstructor().newInstance(); |
700 |
> |
assertEquals(0, result.size()); |
701 |
|
assertFalse(result.iterator().hasNext()); |
702 |
|
return result; |
703 |
|
} |
719 |
|
} |
720 |
|
|
721 |
|
// Remove a bunch of entries with iterator |
722 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
722 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
723 |
|
if (rnd.nextBoolean()) { |
724 |
|
bs.clear(it.next()); |
725 |
|
it.remove(); |
729 |
|
// Add entries till we're back to original size |
730 |
|
while (set.size() < size) { |
731 |
|
int element = min + rnd.nextInt(rangeSize); |
732 |
< |
assertTrue(element >= min && element<= max); |
732 |
> |
assertTrue(element >= min && element <= max); |
733 |
|
put(set, element); |
734 |
|
} |
735 |
|
} |
744 |
|
} |
745 |
|
|
746 |
|
// Remove a bunch of entries with iterator |
747 |
< |
for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
747 |
> |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
748 |
|
if (rnd.nextBoolean()) { |
749 |
|
bs.clear(it.next()); |
750 |
|
it.remove(); |
754 |
|
// Add entries till we're back to original size |
755 |
|
while (set.size() < size) { |
756 |
|
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
757 |
< |
if (element >= min && element<= max) { |
757 |
> |
if (element >= min && element <= max) { |
758 |
|
put(set, element); |
759 |
|
} else { |
760 |
|
try { |
761 |
|
set.add(element); |
762 |
< |
fail(); |
763 |
< |
} catch(IllegalArgumentException e) { |
783 |
< |
// expected |
784 |
< |
} |
762 |
> |
shouldThrow(); |
763 |
> |
} catch (IllegalArgumentException success) {} |
764 |
|
} |
765 |
|
} |
766 |
|
} |
791 |
|
|
792 |
|
// headSet - pick direction and endpoint inclusion randomly |
793 |
|
boolean incl = rnd.nextBoolean(); |
794 |
< |
NavigableSet<Integer> hm = set.navigableHeadSet(midPoint, incl); |
794 |
> |
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
795 |
|
if (ascending) { |
796 |
|
if (rnd.nextBoolean()) |
797 |
|
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
808 |
|
|
809 |
|
// tailSet - pick direction and endpoint inclusion randomly |
810 |
|
incl = rnd.nextBoolean(); |
811 |
< |
NavigableSet<Integer> tm = set.navigableTailSet(midPoint,incl); |
811 |
> |
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
812 |
|
if (ascending) { |
813 |
|
if (rnd.nextBoolean()) |
814 |
|
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
833 |
|
boolean lowIncl = rnd.nextBoolean(); |
834 |
|
boolean highIncl = rnd.nextBoolean(); |
835 |
|
if (ascending) { |
836 |
< |
NavigableSet<Integer> sm = set.navigableSubSet( |
836 |
> |
NavigableSet<Integer> sm = set.subSet( |
837 |
|
endpoints[0], lowIncl, endpoints[1], highIncl); |
838 |
|
if (rnd.nextBoolean()) |
839 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
842 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
843 |
|
endpoints[1] - (highIncl ? 0 : 1), false); |
844 |
|
} else { |
845 |
< |
NavigableSet<Integer> sm = set.navigableSubSet( |
845 |
> |
NavigableSet<Integer> sm = set.subSet( |
846 |
|
endpoints[1], highIncl, endpoints[0], lowIncl); |
847 |
|
if (rnd.nextBoolean()) |
848 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
858 |
|
*/ |
859 |
|
void check(NavigableSet<Integer> set, |
860 |
|
final int min, final int max, final boolean ascending) { |
861 |
< |
class ReferenceSet { |
861 |
> |
class ReferenceSet { |
862 |
|
int lower(int element) { |
863 |
|
return ascending ? |
864 |
|
lowerAscending(element) : higherAscending(element); |
893 |
|
// BitSet should support this! Test would run much faster |
894 |
|
while (element >= min) { |
895 |
|
if (bs.get(element)) |
896 |
< |
return(element); |
896 |
> |
return element; |
897 |
|
element--; |
898 |
|
} |
899 |
|
return -1; |
904 |
|
else if (element > max) |
905 |
|
return -1; |
906 |
|
int result = bs.nextSetBit(element); |
907 |
< |
return result > max ? -1 : result; |
907 |
> |
return (result > max) ? -1 : result; |
908 |
|
} |
909 |
|
int higherAscending(int element) { |
910 |
|
return ceilingAscending(element + 1); |
911 |
|
} |
912 |
|
private int firstAscending() { |
913 |
|
int result = ceilingAscending(min); |
914 |
< |
return result > max ? -1 : result; |
914 |
> |
return (result > max) ? -1 : result; |
915 |
|
} |
916 |
|
private int lastAscending() { |
917 |
|
int result = floorAscending(max); |
918 |
< |
return result < min ? -1 : result; |
918 |
> |
return (result < min) ? -1 : result; |
919 |
|
} |
920 |
|
} |
921 |
|
ReferenceSet rs = new ReferenceSet(); |
928 |
|
if (bsContainsI) |
929 |
|
size++; |
930 |
|
} |
931 |
< |
assertEquals(set.size(), size); |
931 |
> |
assertEquals(size, set.size()); |
932 |
|
|
933 |
|
// Test contents using contains elementSet iterator |
934 |
|
int size2 = 0; |
959 |
|
assertEq(rs.last(), -1); |
960 |
|
try { |
961 |
|
set.first(); |
962 |
< |
fail(); |
963 |
< |
} catch(NoSuchElementException e) { |
985 |
< |
// expected |
986 |
< |
} |
962 |
> |
shouldThrow(); |
963 |
> |
} catch (NoSuchElementException success) {} |
964 |
|
try { |
965 |
|
set.last(); |
966 |
< |
fail(); |
967 |
< |
} catch(NoSuchElementException e) { |
991 |
< |
// expected |
992 |
< |
} |
966 |
> |
shouldThrow(); |
967 |
> |
} catch (NoSuchElementException success) {} |
968 |
|
} |
969 |
|
} |
970 |
|
|
976 |
|
} |
977 |
|
|
978 |
|
static boolean eq(Integer i, int j) { |
979 |
< |
return i == null ? j == -1 : i == j; |
979 |
> |
return (i == null) ? j == -1 : i == j; |
980 |
|
} |
981 |
|
|
982 |
|
} |