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 |
> |
junit.textui.TestRunner.run(suite()); |
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 { |
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 |
42 |
> |
* Returns a new set of given size containing consecutive |
43 |
|
* Integers 0 ... n. |
44 |
|
*/ |
45 |
< |
private TreeSet populatedSet(int n) { |
46 |
< |
TreeSet q = new TreeSet(); |
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) |
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() { |
61 |
|
TreeSet q = new TreeSet(); |
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 |
|
|
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 |
|
/** |
92 |
|
public void testConstructor4() { |
93 |
|
try { |
94 |
|
Integer[] ints = new Integer[SIZE]; |
95 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
95 |
> |
new TreeSet(Arrays.asList(ints)); |
96 |
|
shouldThrow(); |
97 |
< |
} |
93 |
< |
catch (NullPointerException success) {} |
97 |
> |
} catch (NullPointerException success) {} |
98 |
|
} |
99 |
|
|
100 |
|
/** |
105 |
|
Integer[] ints = new Integer[SIZE]; |
106 |
|
for (int i = 0; i < SIZE-1; ++i) |
107 |
|
ints[i] = new Integer(i); |
108 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
108 |
> |
new TreeSet(Arrays.asList(ints)); |
109 |
|
shouldThrow(); |
110 |
< |
} |
107 |
< |
catch (NullPointerException success) {} |
110 |
> |
} catch (NullPointerException success) {} |
111 |
|
} |
112 |
|
|
113 |
|
/** |
114 |
|
* Set contains all elements of collection used to initialize |
115 |
|
*/ |
116 |
|
public void testConstructor6() { |
117 |
< |
try { |
118 |
< |
Integer[] ints = new Integer[SIZE]; |
119 |
< |
for (int i = 0; i < SIZE; ++i) |
120 |
< |
ints[i] = new Integer(i); |
121 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
122 |
< |
for (int i = 0; i < SIZE; ++i) |
120 |
< |
assertEquals(ints[i], q.pollFirst()); |
121 |
< |
} |
122 |
< |
finally {} |
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) |
122 |
> |
assertEquals(ints[i], q.pollFirst()); |
123 |
|
} |
124 |
|
|
125 |
|
/** |
126 |
|
* The comparator used in constructor is used |
127 |
|
*/ |
128 |
|
public void testConstructor7() { |
129 |
< |
try { |
130 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
131 |
< |
TreeSet q = new TreeSet(cmp); |
132 |
< |
assertEquals(cmp, q.comparator()); |
133 |
< |
Integer[] ints = new Integer[SIZE]; |
134 |
< |
for (int i = 0; i < SIZE; ++i) |
135 |
< |
ints[i] = new Integer(i); |
136 |
< |
q.addAll(Arrays.asList(ints)); |
137 |
< |
for (int i = SIZE-1; i >= 0; --i) |
138 |
< |
assertEquals(ints[i], q.pollFirst()); |
139 |
< |
} |
140 |
< |
finally {} |
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) |
137 |
> |
assertEquals(ints[i], q.pollFirst()); |
138 |
|
} |
139 |
|
|
140 |
|
/** |
170 |
|
* add(null) throws NPE if nonempty |
171 |
|
*/ |
172 |
|
public void testAddNull() { |
173 |
< |
try { |
174 |
< |
TreeSet q = populatedSet(SIZE); |
173 |
> |
TreeSet q = populatedSet(SIZE); |
174 |
> |
try { |
175 |
|
q.add(null); |
176 |
|
shouldThrow(); |
177 |
< |
} catch (NullPointerException success) { } |
177 |
> |
} catch (NullPointerException success) {} |
178 |
|
} |
179 |
|
|
180 |
|
/** |
199 |
|
* Add of non-Comparable throws CCE |
200 |
|
*/ |
201 |
|
public void testAddNonComparable() { |
202 |
+ |
TreeSet q = new TreeSet(); |
203 |
|
try { |
206 |
– |
TreeSet q = new TreeSet(); |
207 |
– |
q.add(new Object()); |
204 |
|
q.add(new Object()); |
205 |
|
q.add(new Object()); |
206 |
|
shouldThrow(); |
207 |
< |
} |
212 |
< |
catch(ClassCastException success) {} |
207 |
> |
} catch (ClassCastException success) {} |
208 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
|
* addAll(null) throws NPE |
212 |
|
*/ |
213 |
|
public void testAddAll1() { |
214 |
+ |
TreeSet q = new TreeSet(); |
215 |
|
try { |
220 |
– |
TreeSet q = new TreeSet(); |
216 |
|
q.addAll(null); |
217 |
|
shouldThrow(); |
218 |
< |
} |
224 |
< |
catch (NullPointerException success) {} |
218 |
> |
} catch (NullPointerException success) {} |
219 |
|
} |
220 |
+ |
|
221 |
|
/** |
222 |
|
* addAll of a collection with null elements throws NPE |
223 |
|
*/ |
224 |
|
public void testAddAll2() { |
225 |
+ |
TreeSet q = new TreeSet(); |
226 |
+ |
Integer[] ints = new Integer[SIZE]; |
227 |
|
try { |
231 |
– |
TreeSet q = new TreeSet(); |
232 |
– |
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 |
|
/** |
234 |
|
* addAll of a collection with any null elements throws NPE after |
235 |
|
* possibly adding some elements |
236 |
|
*/ |
237 |
|
public void testAddAll3() { |
238 |
+ |
TreeSet q = new TreeSet(); |
239 |
+ |
Integer[] ints = new Integer[SIZE]; |
240 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
241 |
+ |
ints[i] = new Integer(i); |
242 |
|
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); |
243 |
|
q.addAll(Arrays.asList(ints)); |
244 |
|
shouldThrow(); |
245 |
< |
} |
251 |
< |
catch (NullPointerException success) {} |
245 |
> |
} catch (NullPointerException success) {} |
246 |
|
} |
247 |
|
|
248 |
|
/** |
249 |
|
* Set contains all elements of successful addAll |
250 |
|
*/ |
251 |
|
public void testAddAll5() { |
252 |
< |
try { |
253 |
< |
Integer[] empty = new Integer[0]; |
254 |
< |
Integer[] ints = new Integer[SIZE]; |
255 |
< |
for (int i = 0; i < SIZE; ++i) |
256 |
< |
ints[i] = new Integer(SIZE-1-i); |
257 |
< |
TreeSet q = new TreeSet(); |
258 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
259 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
260 |
< |
for (int i = 0; i < SIZE; ++i) |
267 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
268 |
< |
} |
269 |
< |
finally {} |
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) |
260 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
261 |
|
} |
262 |
|
|
263 |
|
/** |
266 |
|
public void testPollFirst() { |
267 |
|
TreeSet q = populatedSet(SIZE); |
268 |
|
for (int i = 0; i < SIZE; ++i) { |
269 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
269 |
> |
assertEquals(i, q.pollFirst()); |
270 |
|
} |
271 |
< |
assertNull(q.pollFirst()); |
271 |
> |
assertNull(q.pollFirst()); |
272 |
|
} |
273 |
|
|
274 |
|
/** |
277 |
|
public void testPollLast() { |
278 |
|
TreeSet q = populatedSet(SIZE); |
279 |
|
for (int i = SIZE-1; i >= 0; --i) { |
280 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
280 |
> |
assertEquals(i, q.pollLast()); |
281 |
|
} |
282 |
< |
assertNull(q.pollFirst()); |
282 |
> |
assertNull(q.pollFirst()); |
283 |
|
} |
284 |
|
|
294 |
– |
|
285 |
|
/** |
286 |
|
* remove(x) removes x and returns true if present |
287 |
|
*/ |
288 |
|
public void testRemoveElement() { |
289 |
|
TreeSet q = populatedSet(SIZE); |
290 |
< |
for (int i = 1; i < SIZE; i+=2) { |
291 |
< |
assertTrue(q.remove(new Integer(i))); |
292 |
< |
} |
293 |
< |
for (int i = 0; i < SIZE; i+=2) { |
294 |
< |
assertTrue(q.remove(new Integer(i))); |
295 |
< |
assertFalse(q.remove(new Integer(i+1))); |
290 |
> |
for (int i = 1; i < SIZE; i += 2) { |
291 |
> |
assertTrue(q.contains(i)); |
292 |
> |
assertTrue(q.remove(i)); |
293 |
> |
assertFalse(q.contains(i)); |
294 |
> |
assertTrue(q.contains(i-1)); |
295 |
> |
} |
296 |
> |
for (int i = 0; i < SIZE; i += 2) { |
297 |
> |
assertTrue(q.contains(i)); |
298 |
> |
assertTrue(q.remove(i)); |
299 |
> |
assertFalse(q.contains(i)); |
300 |
> |
assertFalse(q.remove(i+1)); |
301 |
> |
assertFalse(q.contains(i+1)); |
302 |
|
} |
303 |
|
assertTrue(q.isEmpty()); |
304 |
|
} |
372 |
|
assertTrue(q.removeAll(p)); |
373 |
|
assertEquals(SIZE-i, q.size()); |
374 |
|
for (int j = 0; j < i; ++j) { |
375 |
< |
Integer I = (Integer)(p.pollFirst()); |
376 |
< |
assertFalse(q.contains(I)); |
375 |
> |
Integer x = (Integer)(p.pollFirst()); |
376 |
> |
assertFalse(q.contains(x)); |
377 |
|
} |
378 |
|
} |
379 |
|
} |
380 |
|
|
385 |
– |
|
386 |
– |
|
381 |
|
/** |
382 |
|
* lower returns preceding element |
383 |
|
*/ |
394 |
|
|
395 |
|
Object e4 = q.lower(zero); |
396 |
|
assertNull(e4); |
403 |
– |
|
397 |
|
} |
398 |
|
|
399 |
|
/** |
412 |
|
|
413 |
|
Object e4 = q.higher(six); |
414 |
|
assertNull(e4); |
422 |
– |
|
415 |
|
} |
416 |
|
|
417 |
|
/** |
430 |
|
|
431 |
|
Object e4 = q.floor(zero); |
432 |
|
assertNull(e4); |
441 |
– |
|
433 |
|
} |
434 |
|
|
435 |
|
/** |
448 |
|
|
449 |
|
Object e4 = q.ceiling(six); |
450 |
|
assertNull(e4); |
460 |
– |
|
451 |
|
} |
452 |
|
|
453 |
|
/** |
454 |
< |
* toArray contains all elements |
454 |
> |
* toArray contains all elements in sorted order |
455 |
|
*/ |
456 |
|
public void testToArray() { |
457 |
|
TreeSet q = populatedSet(SIZE); |
458 |
< |
Object[] o = q.toArray(); |
459 |
< |
Arrays.sort(o); |
460 |
< |
for(int i = 0; i < o.length; i++) |
471 |
< |
assertEquals(o[i], q.pollFirst()); |
458 |
> |
Object[] o = q.toArray(); |
459 |
> |
for (int i = 0; i < o.length; i++) |
460 |
> |
assertSame(o[i], q.pollFirst()); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
464 |
< |
* toArray(a) contains all elements |
464 |
> |
* toArray(a) contains all elements in sorted order |
465 |
|
*/ |
466 |
|
public void testToArray2() { |
467 |
< |
TreeSet q = populatedSet(SIZE); |
468 |
< |
Integer[] ints = new Integer[SIZE]; |
469 |
< |
ints = (Integer[])q.toArray(ints); |
470 |
< |
Arrays.sort(ints); |
471 |
< |
for(int i = 0; i < ints.length; i++) |
472 |
< |
assertEquals(ints[i], q.pollFirst()); |
467 |
> |
TreeSet<Integer> q = populatedSet(SIZE); |
468 |
> |
Integer[] ints = new Integer[SIZE]; |
469 |
> |
Integer[] array = q.toArray(ints); |
470 |
> |
assertSame(ints, array); |
471 |
> |
for (int i = 0; i < ints.length; i++) |
472 |
> |
assertSame(ints[i], q.pollFirst()); |
473 |
|
} |
474 |
|
|
475 |
|
/** |
477 |
|
*/ |
478 |
|
public void testIterator() { |
479 |
|
TreeSet q = populatedSet(SIZE); |
480 |
< |
int i = 0; |
481 |
< |
Iterator it = q.iterator(); |
482 |
< |
while(it.hasNext()) { |
480 |
> |
Iterator it = q.iterator(); |
481 |
> |
int i; |
482 |
> |
for (i = 0; it.hasNext(); i++) |
483 |
|
assertTrue(q.contains(it.next())); |
495 |
– |
++i; |
496 |
– |
} |
484 |
|
assertEquals(i, SIZE); |
485 |
+ |
assertIteratorExhausted(it); |
486 |
|
} |
487 |
|
|
488 |
|
/** |
489 |
|
* iterator of empty set has no elements |
490 |
|
*/ |
491 |
|
public void testEmptyIterator() { |
492 |
< |
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); |
492 |
> |
assertIteratorExhausted(new TreeSet().iterator()); |
493 |
|
} |
494 |
|
|
495 |
|
/** |
496 |
|
* iterator.remove removes current element |
497 |
|
*/ |
498 |
< |
public void testIteratorRemove () { |
498 |
> |
public void testIteratorRemove() { |
499 |
|
final TreeSet q = new TreeSet(); |
500 |
|
q.add(new Integer(2)); |
501 |
|
q.add(new Integer(1)); |
511 |
|
assertFalse(it.hasNext()); |
512 |
|
} |
513 |
|
|
533 |
– |
|
514 |
|
/** |
515 |
|
* toString contains toStrings of elements |
516 |
|
*/ |
518 |
|
TreeSet q = populatedSet(SIZE); |
519 |
|
String s = q.toString(); |
520 |
|
for (int i = 0; i < SIZE; ++i) { |
521 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
521 |
> |
assertTrue(s.contains(String.valueOf(i))); |
522 |
|
} |
523 |
|
} |
524 |
|
|
525 |
|
/** |
526 |
|
* A deserialized serialized set has same elements |
527 |
|
*/ |
528 |
< |
public void testSerialization() { |
529 |
< |
TreeSet q = populatedSet(SIZE); |
530 |
< |
try { |
531 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
532 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
533 |
< |
out.writeObject(q); |
534 |
< |
out.close(); |
535 |
< |
|
536 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
537 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
538 |
< |
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(); |
528 |
> |
public void testSerialization() throws Exception { |
529 |
> |
NavigableSet x = populatedSet(SIZE); |
530 |
> |
NavigableSet y = serialClone(x); |
531 |
> |
|
532 |
> |
assertNotSame(x, y); |
533 |
> |
assertEquals(x.size(), y.size()); |
534 |
> |
assertEquals(x, y); |
535 |
> |
assertEquals(y, x); |
536 |
> |
while (!x.isEmpty()) { |
537 |
> |
assertFalse(y.isEmpty()); |
538 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
539 |
|
} |
540 |
+ |
assertTrue(y.isEmpty()); |
541 |
|
} |
542 |
|
|
543 |
|
/** |
665 |
|
/** |
666 |
|
* Subsets of subsets subdivide correctly |
667 |
|
*/ |
668 |
< |
public void testRecursiveSubSets() { |
669 |
< |
int setSize = 1000; |
670 |
< |
Class cl = TreeSet.class; |
668 |
> |
public void testRecursiveSubSets() throws Exception { |
669 |
> |
int setSize = expensiveTests ? 1000 : 100; |
670 |
> |
Class cl = TreeSet.class; |
671 |
|
|
672 |
|
NavigableSet<Integer> set = newSet(cl); |
673 |
|
bs = new BitSet(setSize); |
684 |
|
0, setSize - 1, true); |
685 |
|
} |
686 |
|
|
687 |
< |
static NavigableSet<Integer> newSet(Class cl) { |
688 |
< |
NavigableSet<Integer> result = null; |
689 |
< |
try { |
690 |
< |
result = (NavigableSet<Integer>) cl.newInstance(); |
691 |
< |
} catch(Exception e) { |
692 |
< |
fail(); |
693 |
< |
} |
694 |
< |
assertEquals(result.size(), 0); |
687 |
> |
/** |
688 |
> |
* addAll is idempotent |
689 |
> |
*/ |
690 |
> |
public void testAddAll_idempotent() throws Exception { |
691 |
> |
Set x = populatedSet(SIZE); |
692 |
> |
Set y = new TreeSet(x); |
693 |
> |
y.addAll(x); |
694 |
> |
assertEquals(x, y); |
695 |
> |
assertEquals(y, x); |
696 |
> |
} |
697 |
> |
|
698 |
> |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
699 |
> |
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.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 |
|
} |
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; |
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 |
|
|