author | dl |
Fri, 23 Sep 2016 13:24:33 -0700 | |
changeset 41131 | 87edc8451f8a |
parent 35394 | 282c3cb6a0c1 |
child 42322 | c3474fef4fe4 |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
*/ |
|
33 |
||
34 |
import java.util.Arrays; |
|
35 |
import java.util.BitSet; |
|
36 |
import java.util.Collection; |
|
37 |
import java.util.Comparator; |
|
38 |
import java.util.Iterator; |
|
39 |
import java.util.NavigableSet; |
|
40 |
import java.util.NoSuchElementException; |
|
41 |
import java.util.Random; |
|
42 |
import java.util.Set; |
|
43 |
import java.util.SortedSet; |
|
44 |
import java.util.TreeSet; |
|
45 |
||
46 |
import junit.framework.Test; |
|
47 |
import junit.framework.TestSuite; |
|
48 |
||
49 |
public class TreeSetTest extends JSR166TestCase { |
|
50 |
public static void main(String[] args) { |
|
51 |
main(suite(), args); |
|
52 |
} |
|
53 |
public static Test suite() { |
|
54 |
return new TestSuite(TreeSetTest.class); |
|
55 |
} |
|
56 |
||
57 |
static class MyReverseComparator implements Comparator { |
|
58 |
public int compare(Object x, Object y) { |
|
59 |
return ((Comparable)y).compareTo(x); |
|
60 |
} |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* The number of elements to place in collections, arrays, etc. |
|
65 |
*/ |
|
66 |
static final int SIZE = 20; |
|
67 |
||
68 |
/** |
|
69 |
* Returns a new set of given size containing consecutive |
|
70 |
* Integers 0 ... n. |
|
71 |
*/ |
|
72 |
private TreeSet<Integer> populatedSet(int n) { |
|
73 |
TreeSet<Integer> q = new TreeSet<Integer>(); |
|
74 |
assertTrue(q.isEmpty()); |
|
75 |
for (int i = n - 1; i >= 0; i -= 2) |
|
76 |
assertTrue(q.add(new Integer(i))); |
|
77 |
for (int i = (n & 1); i < n; i += 2) |
|
78 |
assertTrue(q.add(new Integer(i))); |
|
79 |
assertFalse(q.isEmpty()); |
|
80 |
assertEquals(n, q.size()); |
|
81 |
return q; |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Returns a new set of first 5 ints. |
|
86 |
*/ |
|
87 |
private TreeSet set5() { |
|
88 |
TreeSet q = new TreeSet(); |
|
89 |
assertTrue(q.isEmpty()); |
|
90 |
q.add(one); |
|
91 |
q.add(two); |
|
92 |
q.add(three); |
|
93 |
q.add(four); |
|
94 |
q.add(five); |
|
95 |
assertEquals(5, q.size()); |
|
96 |
return q; |
|
97 |
} |
|
98 |
||
99 |
/** |
|
100 |
* A new set has unbounded capacity |
|
101 |
*/ |
|
102 |
public void testConstructor1() { |
|
103 |
assertEquals(0, new TreeSet().size()); |
|
104 |
} |
|
105 |
||
106 |
/** |
|
107 |
* Initializing from null Collection throws NPE |
|
108 |
*/ |
|
109 |
public void testConstructor3() { |
|
110 |
try { |
|
111 |
new TreeSet((Collection)null); |
|
112 |
shouldThrow(); |
|
113 |
} catch (NullPointerException success) {} |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Initializing from Collection of null elements throws NPE |
|
118 |
*/ |
|
119 |
public void testConstructor4() { |
|
120 |
try { |
|
121 |
new TreeSet(Arrays.asList(new Integer[SIZE])); |
|
122 |
shouldThrow(); |
|
123 |
} catch (NullPointerException success) {} |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Initializing from Collection with some null elements throws NPE |
|
128 |
*/ |
|
129 |
public void testConstructor5() { |
|
130 |
Integer[] ints = new Integer[SIZE]; |
|
131 |
for (int i = 0; i < SIZE - 1; ++i) |
|
132 |
ints[i] = new Integer(i); |
|
133 |
try { |
|
134 |
new TreeSet(Arrays.asList(ints)); |
|
135 |
shouldThrow(); |
|
136 |
} catch (NullPointerException success) {} |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Set contains all elements of collection used to initialize |
|
141 |
*/ |
|
142 |
public void testConstructor6() { |
|
143 |
Integer[] ints = new Integer[SIZE]; |
|
144 |
for (int i = 0; i < SIZE; ++i) |
|
145 |
ints[i] = new Integer(i); |
|
146 |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
|
147 |
for (int i = 0; i < SIZE; ++i) |
|
148 |
assertEquals(ints[i], q.pollFirst()); |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* The comparator used in constructor is used |
|
153 |
*/ |
|
154 |
public void testConstructor7() { |
|
155 |
MyReverseComparator cmp = new MyReverseComparator(); |
|
156 |
TreeSet q = new TreeSet(cmp); |
|
157 |
assertEquals(cmp, q.comparator()); |
|
158 |
Integer[] ints = new Integer[SIZE]; |
|
159 |
for (int i = 0; i < SIZE; ++i) |
|
160 |
ints[i] = new Integer(i); |
|
161 |
q.addAll(Arrays.asList(ints)); |
|
162 |
for (int i = SIZE - 1; i >= 0; --i) |
|
163 |
assertEquals(ints[i], q.pollFirst()); |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* isEmpty is true before add, false after |
|
168 |
*/ |
|
169 |
public void testEmpty() { |
|
170 |
TreeSet q = new TreeSet(); |
|
171 |
assertTrue(q.isEmpty()); |
|
172 |
q.add(new Integer(1)); |
|
173 |
assertFalse(q.isEmpty()); |
|
174 |
q.add(new Integer(2)); |
|
175 |
q.pollFirst(); |
|
176 |
q.pollFirst(); |
|
177 |
assertTrue(q.isEmpty()); |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* size changes when elements added and removed |
|
182 |
*/ |
|
183 |
public void testSize() { |
|
184 |
TreeSet q = populatedSet(SIZE); |
|
185 |
for (int i = 0; i < SIZE; ++i) { |
|
186 |
assertEquals(SIZE - i, q.size()); |
|
187 |
q.pollFirst(); |
|
188 |
} |
|
189 |
for (int i = 0; i < SIZE; ++i) { |
|
190 |
assertEquals(i, q.size()); |
|
191 |
q.add(new Integer(i)); |
|
192 |
} |
|
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* add(null) throws NPE if nonempty |
|
197 |
*/ |
|
198 |
public void testAddNull() { |
|
199 |
TreeSet q = populatedSet(SIZE); |
|
200 |
try { |
|
201 |
q.add(null); |
|
202 |
shouldThrow(); |
|
203 |
} catch (NullPointerException success) {} |
|
204 |
} |
|
205 |
||
206 |
/** |
|
207 |
* Add of comparable element succeeds |
|
208 |
*/ |
|
209 |
public void testAdd() { |
|
210 |
TreeSet q = new TreeSet(); |
|
211 |
assertTrue(q.add(zero)); |
|
212 |
assertTrue(q.add(one)); |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Add of duplicate element fails |
|
217 |
*/ |
|
218 |
public void testAddDup() { |
|
219 |
TreeSet q = new TreeSet(); |
|
220 |
assertTrue(q.add(zero)); |
|
221 |
assertFalse(q.add(zero)); |
|
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* Add of non-Comparable throws CCE |
|
226 |
*/ |
|
227 |
public void testAddNonComparable() { |
|
228 |
TreeSet q = new TreeSet(); |
|
229 |
try { |
|
230 |
q.add(new Object()); |
|
231 |
q.add(new Object()); |
|
232 |
shouldThrow(); |
|
233 |
} catch (ClassCastException success) {} |
|
234 |
} |
|
235 |
||
236 |
/** |
|
237 |
* addAll(null) throws NPE |
|
238 |
*/ |
|
239 |
public void testAddAll1() { |
|
240 |
TreeSet q = new TreeSet(); |
|
241 |
try { |
|
242 |
q.addAll(null); |
|
243 |
shouldThrow(); |
|
244 |
} catch (NullPointerException success) {} |
|
245 |
} |
|
246 |
||
247 |
/** |
|
248 |
* addAll of a collection with null elements throws NPE |
|
249 |
*/ |
|
250 |
public void testAddAll2() { |
|
251 |
TreeSet q = new TreeSet(); |
|
252 |
Integer[] ints = new Integer[SIZE]; |
|
253 |
try { |
|
254 |
q.addAll(Arrays.asList(ints)); |
|
255 |
shouldThrow(); |
|
256 |
} catch (NullPointerException success) {} |
|
257 |
} |
|
258 |
||
259 |
/** |
|
260 |
* addAll of a collection with any null elements throws NPE after |
|
261 |
* possibly adding some elements |
|
262 |
*/ |
|
263 |
public void testAddAll3() { |
|
264 |
TreeSet q = new TreeSet(); |
|
265 |
Integer[] ints = new Integer[SIZE]; |
|
266 |
for (int i = 0; i < SIZE - 1; ++i) |
|
267 |
ints[i] = new Integer(i); |
|
268 |
try { |
|
269 |
q.addAll(Arrays.asList(ints)); |
|
270 |
shouldThrow(); |
|
271 |
} catch (NullPointerException success) {} |
|
272 |
} |
|
273 |
||
274 |
/** |
|
275 |
* Set contains all elements of successful addAll |
|
276 |
*/ |
|
277 |
public void testAddAll5() { |
|
278 |
Integer[] empty = new Integer[0]; |
|
279 |
Integer[] ints = new Integer[SIZE]; |
|
280 |
for (int i = 0; i < SIZE; ++i) |
|
281 |
ints[i] = new Integer(SIZE - 1 - i); |
|
282 |
TreeSet q = new TreeSet(); |
|
283 |
assertFalse(q.addAll(Arrays.asList(empty))); |
|
284 |
assertTrue(q.addAll(Arrays.asList(ints))); |
|
285 |
for (int i = 0; i < SIZE; ++i) |
|
286 |
assertEquals(new Integer(i), q.pollFirst()); |
|
287 |
} |
|
288 |
||
289 |
/** |
|
290 |
* pollFirst succeeds unless empty |
|
291 |
*/ |
|
292 |
public void testPollFirst() { |
|
293 |
TreeSet q = populatedSet(SIZE); |
|
294 |
for (int i = 0; i < SIZE; ++i) { |
|
295 |
assertEquals(i, q.pollFirst()); |
|
296 |
} |
|
297 |
assertNull(q.pollFirst()); |
|
298 |
} |
|
299 |
||
300 |
/** |
|
301 |
* pollLast succeeds unless empty |
|
302 |
*/ |
|
303 |
public void testPollLast() { |
|
304 |
TreeSet q = populatedSet(SIZE); |
|
305 |
for (int i = SIZE - 1; i >= 0; --i) { |
|
306 |
assertEquals(i, q.pollLast()); |
|
307 |
} |
|
308 |
assertNull(q.pollFirst()); |
|
309 |
} |
|
310 |
||
311 |
/** |
|
312 |
* remove(x) removes x and returns true if present |
|
313 |
*/ |
|
314 |
public void testRemoveElement() { |
|
315 |
TreeSet q = populatedSet(SIZE); |
|
316 |
for (int i = 1; i < SIZE; i += 2) { |
|
317 |
assertTrue(q.contains(i)); |
|
318 |
assertTrue(q.remove(i)); |
|
319 |
assertFalse(q.contains(i)); |
|
320 |
assertTrue(q.contains(i - 1)); |
|
321 |
} |
|
322 |
for (int i = 0; i < SIZE; i += 2) { |
|
323 |
assertTrue(q.contains(i)); |
|
324 |
assertTrue(q.remove(i)); |
|
325 |
assertFalse(q.contains(i)); |
|
326 |
assertFalse(q.remove(i + 1)); |
|
327 |
assertFalse(q.contains(i + 1)); |
|
328 |
} |
|
329 |
assertTrue(q.isEmpty()); |
|
330 |
} |
|
331 |
||
332 |
/** |
|
333 |
* contains(x) reports true when elements added but not yet removed |
|
334 |
*/ |
|
335 |
public void testContains() { |
|
336 |
TreeSet q = populatedSet(SIZE); |
|
337 |
for (int i = 0; i < SIZE; ++i) { |
|
338 |
assertTrue(q.contains(new Integer(i))); |
|
339 |
q.pollFirst(); |
|
340 |
assertFalse(q.contains(new Integer(i))); |
|
341 |
} |
|
342 |
} |
|
343 |
||
344 |
/** |
|
345 |
* clear removes all elements |
|
346 |
*/ |
|
347 |
public void testClear() { |
|
348 |
TreeSet q = populatedSet(SIZE); |
|
349 |
q.clear(); |
|
350 |
assertTrue(q.isEmpty()); |
|
351 |
assertEquals(0, q.size()); |
|
352 |
q.add(new Integer(1)); |
|
353 |
assertFalse(q.isEmpty()); |
|
354 |
q.clear(); |
|
355 |
assertTrue(q.isEmpty()); |
|
356 |
} |
|
357 |
||
358 |
/** |
|
359 |
* containsAll(c) is true when c contains a subset of elements |
|
360 |
*/ |
|
361 |
public void testContainsAll() { |
|
362 |
TreeSet q = populatedSet(SIZE); |
|
363 |
TreeSet p = new TreeSet(); |
|
364 |
for (int i = 0; i < SIZE; ++i) { |
|
365 |
assertTrue(q.containsAll(p)); |
|
366 |
assertFalse(p.containsAll(q)); |
|
367 |
p.add(new Integer(i)); |
|
368 |
} |
|
369 |
assertTrue(p.containsAll(q)); |
|
370 |
} |
|
371 |
||
372 |
/** |
|
373 |
* retainAll(c) retains only those elements of c and reports true if changed |
|
374 |
*/ |
|
375 |
public void testRetainAll() { |
|
376 |
TreeSet q = populatedSet(SIZE); |
|
377 |
TreeSet p = populatedSet(SIZE); |
|
378 |
for (int i = 0; i < SIZE; ++i) { |
|
379 |
boolean changed = q.retainAll(p); |
|
380 |
if (i == 0) |
|
381 |
assertFalse(changed); |
|
382 |
else |
|
383 |
assertTrue(changed); |
|
384 |
||
385 |
assertTrue(q.containsAll(p)); |
|
386 |
assertEquals(SIZE - i, q.size()); |
|
387 |
p.pollFirst(); |
|
388 |
} |
|
389 |
} |
|
390 |
||
391 |
/** |
|
392 |
* removeAll(c) removes only those elements of c and reports true if changed |
|
393 |
*/ |
|
394 |
public void testRemoveAll() { |
|
395 |
for (int i = 1; i < SIZE; ++i) { |
|
396 |
TreeSet q = populatedSet(SIZE); |
|
397 |
TreeSet p = populatedSet(i); |
|
398 |
assertTrue(q.removeAll(p)); |
|
399 |
assertEquals(SIZE - i, q.size()); |
|
400 |
for (int j = 0; j < i; ++j) { |
|
401 |
Integer x = (Integer)(p.pollFirst()); |
|
402 |
assertFalse(q.contains(x)); |
|
403 |
} |
|
404 |
} |
|
405 |
} |
|
406 |
||
407 |
/** |
|
408 |
* lower returns preceding element |
|
409 |
*/ |
|
410 |
public void testLower() { |
|
411 |
TreeSet q = set5(); |
|
412 |
Object e1 = q.lower(three); |
|
413 |
assertEquals(two, e1); |
|
414 |
||
415 |
Object e2 = q.lower(six); |
|
416 |
assertEquals(five, e2); |
|
417 |
||
418 |
Object e3 = q.lower(one); |
|
419 |
assertNull(e3); |
|
420 |
||
421 |
Object e4 = q.lower(zero); |
|
422 |
assertNull(e4); |
|
423 |
} |
|
424 |
||
425 |
/** |
|
426 |
* higher returns next element |
|
427 |
*/ |
|
428 |
public void testHigher() { |
|
429 |
TreeSet q = set5(); |
|
430 |
Object e1 = q.higher(three); |
|
431 |
assertEquals(four, e1); |
|
432 |
||
433 |
Object e2 = q.higher(zero); |
|
434 |
assertEquals(one, e2); |
|
435 |
||
436 |
Object e3 = q.higher(five); |
|
437 |
assertNull(e3); |
|
438 |
||
439 |
Object e4 = q.higher(six); |
|
440 |
assertNull(e4); |
|
441 |
} |
|
442 |
||
443 |
/** |
|
444 |
* floor returns preceding element |
|
445 |
*/ |
|
446 |
public void testFloor() { |
|
447 |
TreeSet q = set5(); |
|
448 |
Object e1 = q.floor(three); |
|
449 |
assertEquals(three, e1); |
|
450 |
||
451 |
Object e2 = q.floor(six); |
|
452 |
assertEquals(five, e2); |
|
453 |
||
454 |
Object e3 = q.floor(one); |
|
455 |
assertEquals(one, e3); |
|
456 |
||
457 |
Object e4 = q.floor(zero); |
|
458 |
assertNull(e4); |
|
459 |
} |
|
460 |
||
461 |
/** |
|
462 |
* ceiling returns next element |
|
463 |
*/ |
|
464 |
public void testCeiling() { |
|
465 |
TreeSet q = set5(); |
|
466 |
Object e1 = q.ceiling(three); |
|
467 |
assertEquals(three, e1); |
|
468 |
||
469 |
Object e2 = q.ceiling(zero); |
|
470 |
assertEquals(one, e2); |
|
471 |
||
472 |
Object e3 = q.ceiling(five); |
|
473 |
assertEquals(five, e3); |
|
474 |
||
475 |
Object e4 = q.ceiling(six); |
|
476 |
assertNull(e4); |
|
477 |
} |
|
478 |
||
479 |
/** |
|
480 |
* toArray contains all elements in sorted order |
|
481 |
*/ |
|
482 |
public void testToArray() { |
|
483 |
TreeSet q = populatedSet(SIZE); |
|
484 |
Object[] o = q.toArray(); |
|
485 |
for (int i = 0; i < o.length; i++) |
|
486 |
assertSame(o[i], q.pollFirst()); |
|
487 |
} |
|
488 |
||
489 |
/** |
|
490 |
* toArray(a) contains all elements in sorted order |
|
491 |
*/ |
|
492 |
public void testToArray2() { |
|
493 |
TreeSet<Integer> q = populatedSet(SIZE); |
|
494 |
Integer[] ints = new Integer[SIZE]; |
|
495 |
Integer[] array = q.toArray(ints); |
|
496 |
assertSame(ints, array); |
|
497 |
for (int i = 0; i < ints.length; i++) |
|
498 |
assertSame(ints[i], q.pollFirst()); |
|
499 |
} |
|
500 |
||
501 |
/** |
|
502 |
* iterator iterates through all elements |
|
503 |
*/ |
|
504 |
public void testIterator() { |
|
505 |
TreeSet q = populatedSet(SIZE); |
|
506 |
Iterator it = q.iterator(); |
|
507 |
int i; |
|
508 |
for (i = 0; it.hasNext(); i++) |
|
509 |
assertTrue(q.contains(it.next())); |
|
510 |
assertEquals(i, SIZE); |
|
511 |
assertIteratorExhausted(it); |
|
512 |
} |
|
513 |
||
514 |
/** |
|
515 |
* iterator of empty set has no elements |
|
516 |
*/ |
|
517 |
public void testEmptyIterator() { |
|
518 |
assertIteratorExhausted(new TreeSet().iterator()); |
|
519 |
} |
|
520 |
||
521 |
/** |
|
522 |
* iterator.remove removes current element |
|
523 |
*/ |
|
524 |
public void testIteratorRemove() { |
|
525 |
final TreeSet q = new TreeSet(); |
|
526 |
q.add(new Integer(2)); |
|
527 |
q.add(new Integer(1)); |
|
528 |
q.add(new Integer(3)); |
|
529 |
||
530 |
Iterator it = q.iterator(); |
|
531 |
it.next(); |
|
532 |
it.remove(); |
|
533 |
||
534 |
it = q.iterator(); |
|
535 |
assertEquals(it.next(), new Integer(2)); |
|
536 |
assertEquals(it.next(), new Integer(3)); |
|
537 |
assertFalse(it.hasNext()); |
|
538 |
} |
|
539 |
||
540 |
/** |
|
541 |
* toString contains toStrings of elements |
|
542 |
*/ |
|
543 |
public void testToString() { |
|
544 |
TreeSet q = populatedSet(SIZE); |
|
545 |
String s = q.toString(); |
|
546 |
for (int i = 0; i < SIZE; ++i) { |
|
547 |
assertTrue(s.contains(String.valueOf(i))); |
|
548 |
} |
|
549 |
} |
|
550 |
||
551 |
/** |
|
552 |
* A deserialized serialized set has same elements |
|
553 |
*/ |
|
554 |
public void testSerialization() throws Exception { |
|
555 |
NavigableSet x = populatedSet(SIZE); |
|
556 |
NavigableSet y = serialClone(x); |
|
557 |
||
558 |
assertNotSame(x, y); |
|
559 |
assertEquals(x.size(), y.size()); |
|
560 |
assertEquals(x, y); |
|
561 |
assertEquals(y, x); |
|
562 |
while (!x.isEmpty()) { |
|
563 |
assertFalse(y.isEmpty()); |
|
564 |
assertEquals(x.pollFirst(), y.pollFirst()); |
|
565 |
} |
|
566 |
assertTrue(y.isEmpty()); |
|
567 |
} |
|
568 |
||
569 |
/** |
|
570 |
* subSet returns set with keys in requested range |
|
571 |
*/ |
|
572 |
public void testSubSetContents() { |
|
573 |
TreeSet set = set5(); |
|
574 |
SortedSet sm = set.subSet(two, four); |
|
575 |
assertEquals(two, sm.first()); |
|
576 |
assertEquals(three, sm.last()); |
|
577 |
assertEquals(2, sm.size()); |
|
578 |
assertFalse(sm.contains(one)); |
|
579 |
assertTrue(sm.contains(two)); |
|
580 |
assertTrue(sm.contains(three)); |
|
581 |
assertFalse(sm.contains(four)); |
|
582 |
assertFalse(sm.contains(five)); |
|
583 |
Iterator i = sm.iterator(); |
|
584 |
Object k; |
|
585 |
k = (Integer)(i.next()); |
|
586 |
assertEquals(two, k); |
|
587 |
k = (Integer)(i.next()); |
|
588 |
assertEquals(three, k); |
|
589 |
assertFalse(i.hasNext()); |
|
590 |
Iterator j = sm.iterator(); |
|
591 |
j.next(); |
|
592 |
j.remove(); |
|
593 |
assertFalse(set.contains(two)); |
|
594 |
assertEquals(4, set.size()); |
|
595 |
assertEquals(1, sm.size()); |
|
596 |
assertEquals(three, sm.first()); |
|
597 |
assertEquals(three, sm.last()); |
|
598 |
assertTrue(sm.remove(three)); |
|
599 |
assertTrue(sm.isEmpty()); |
|
600 |
assertEquals(3, set.size()); |
|
601 |
} |
|
602 |
||
603 |
public void testSubSetContents2() { |
|
604 |
TreeSet set = set5(); |
|
605 |
SortedSet sm = set.subSet(two, three); |
|
606 |
assertEquals(1, sm.size()); |
|
607 |
assertEquals(two, sm.first()); |
|
608 |
assertEquals(two, sm.last()); |
|
609 |
assertFalse(sm.contains(one)); |
|
610 |
assertTrue(sm.contains(two)); |
|
611 |
assertFalse(sm.contains(three)); |
|
612 |
assertFalse(sm.contains(four)); |
|
613 |
assertFalse(sm.contains(five)); |
|
614 |
Iterator i = sm.iterator(); |
|
615 |
Object k; |
|
616 |
k = (Integer)(i.next()); |
|
617 |
assertEquals(two, k); |
|
618 |
assertFalse(i.hasNext()); |
|
619 |
Iterator j = sm.iterator(); |
|
620 |
j.next(); |
|
621 |
j.remove(); |
|
622 |
assertFalse(set.contains(two)); |
|
623 |
assertEquals(4, set.size()); |
|
624 |
assertEquals(0, sm.size()); |
|
625 |
assertTrue(sm.isEmpty()); |
|
626 |
assertFalse(sm.remove(three)); |
|
627 |
assertEquals(4, set.size()); |
|
628 |
} |
|
629 |
||
630 |
/** |
|
631 |
* headSet returns set with keys in requested range |
|
632 |
*/ |
|
633 |
public void testHeadSetContents() { |
|
634 |
TreeSet set = set5(); |
|
635 |
SortedSet sm = set.headSet(four); |
|
636 |
assertTrue(sm.contains(one)); |
|
637 |
assertTrue(sm.contains(two)); |
|
638 |
assertTrue(sm.contains(three)); |
|
639 |
assertFalse(sm.contains(four)); |
|
640 |
assertFalse(sm.contains(five)); |
|
641 |
Iterator i = sm.iterator(); |
|
642 |
Object k; |
|
643 |
k = (Integer)(i.next()); |
|
644 |
assertEquals(one, k); |
|
645 |
k = (Integer)(i.next()); |
|
646 |
assertEquals(two, k); |
|
647 |
k = (Integer)(i.next()); |
|
648 |
assertEquals(three, k); |
|
649 |
assertFalse(i.hasNext()); |
|
650 |
sm.clear(); |
|
651 |
assertTrue(sm.isEmpty()); |
|
652 |
assertEquals(2, set.size()); |
|
653 |
assertEquals(four, set.first()); |
|
654 |
} |
|
655 |
||
656 |
/** |
|
657 |
* tailSet returns set with keys in requested range |
|
658 |
*/ |
|
659 |
public void testTailSetContents() { |
|
660 |
TreeSet set = set5(); |
|
661 |
SortedSet sm = set.tailSet(two); |
|
662 |
assertFalse(sm.contains(one)); |
|
663 |
assertTrue(sm.contains(two)); |
|
664 |
assertTrue(sm.contains(three)); |
|
665 |
assertTrue(sm.contains(four)); |
|
666 |
assertTrue(sm.contains(five)); |
|
667 |
Iterator i = sm.iterator(); |
|
668 |
Object k; |
|
669 |
k = (Integer)(i.next()); |
|
670 |
assertEquals(two, k); |
|
671 |
k = (Integer)(i.next()); |
|
672 |
assertEquals(three, k); |
|
673 |
k = (Integer)(i.next()); |
|
674 |
assertEquals(four, k); |
|
675 |
k = (Integer)(i.next()); |
|
676 |
assertEquals(five, k); |
|
677 |
assertFalse(i.hasNext()); |
|
678 |
||
679 |
SortedSet ssm = sm.tailSet(four); |
|
680 |
assertEquals(four, ssm.first()); |
|
681 |
assertEquals(five, ssm.last()); |
|
682 |
assertTrue(ssm.remove(four)); |
|
683 |
assertEquals(1, ssm.size()); |
|
684 |
assertEquals(3, sm.size()); |
|
685 |
assertEquals(4, set.size()); |
|
686 |
} |
|
687 |
||
688 |
Random rnd = new Random(666); |
|
689 |
BitSet bs; |
|
690 |
||
691 |
/** |
|
692 |
* Subsets of subsets subdivide correctly |
|
693 |
*/ |
|
694 |
public void testRecursiveSubSets() throws Exception { |
|
695 |
int setSize = expensiveTests ? 1000 : 100; |
|
696 |
Class cl = TreeSet.class; |
|
697 |
||
698 |
NavigableSet<Integer> set = newSet(cl); |
|
699 |
bs = new BitSet(setSize); |
|
700 |
||
701 |
populate(set, setSize); |
|
702 |
check(set, 0, setSize - 1, true); |
|
703 |
check(set.descendingSet(), 0, setSize - 1, false); |
|
704 |
||
705 |
mutateSet(set, 0, setSize - 1); |
|
706 |
check(set, 0, setSize - 1, true); |
|
707 |
check(set.descendingSet(), 0, setSize - 1, false); |
|
708 |
||
709 |
bashSubSet(set.subSet(0, true, setSize, false), |
|
710 |
0, setSize - 1, true); |
|
711 |
} |
|
712 |
||
713 |
/** |
|
714 |
* addAll is idempotent |
|
715 |
*/ |
|
716 |
public void testAddAll_idempotent() throws Exception { |
|
717 |
Set x = populatedSet(SIZE); |
|
718 |
Set y = new TreeSet(x); |
|
719 |
y.addAll(x); |
|
720 |
assertEquals(x, y); |
|
721 |
assertEquals(y, x); |
|
722 |
} |
|
723 |
||
724 |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
|
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
35394
diff
changeset
|
725 |
NavigableSet<Integer> result = |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
35394
diff
changeset
|
726 |
(NavigableSet<Integer>) cl.getConstructor().newInstance(); |
35394 | 727 |
assertEquals(0, result.size()); |
728 |
assertFalse(result.iterator().hasNext()); |
|
729 |
return result; |
|
730 |
} |
|
731 |
||
732 |
void populate(NavigableSet<Integer> set, int limit) { |
|
733 |
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
|
734 |
int element = rnd.nextInt(limit); |
|
735 |
put(set, element); |
|
736 |
} |
|
737 |
} |
|
738 |
||
739 |
void mutateSet(NavigableSet<Integer> set, int min, int max) { |
|
740 |
int size = set.size(); |
|
741 |
int rangeSize = max - min + 1; |
|
742 |
||
743 |
// Remove a bunch of entries directly |
|
744 |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
|
745 |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
|
746 |
} |
|
747 |
||
748 |
// Remove a bunch of entries with iterator |
|
749 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
|
750 |
if (rnd.nextBoolean()) { |
|
751 |
bs.clear(it.next()); |
|
752 |
it.remove(); |
|
753 |
} |
|
754 |
} |
|
755 |
||
756 |
// Add entries till we're back to original size |
|
757 |
while (set.size() < size) { |
|
758 |
int element = min + rnd.nextInt(rangeSize); |
|
759 |
assertTrue(element >= min && element <= max); |
|
760 |
put(set, element); |
|
761 |
} |
|
762 |
} |
|
763 |
||
764 |
void mutateSubSet(NavigableSet<Integer> set, int min, int max) { |
|
765 |
int size = set.size(); |
|
766 |
int rangeSize = max - min + 1; |
|
767 |
||
768 |
// Remove a bunch of entries directly |
|
769 |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
|
770 |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10)); |
|
771 |
} |
|
772 |
||
773 |
// Remove a bunch of entries with iterator |
|
774 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
|
775 |
if (rnd.nextBoolean()) { |
|
776 |
bs.clear(it.next()); |
|
777 |
it.remove(); |
|
778 |
} |
|
779 |
} |
|
780 |
||
781 |
// Add entries till we're back to original size |
|
782 |
while (set.size() < size) { |
|
783 |
int element = min - 5 + rnd.nextInt(rangeSize + 10); |
|
784 |
if (element >= min && element <= max) { |
|
785 |
put(set, element); |
|
786 |
} else { |
|
787 |
try { |
|
788 |
set.add(element); |
|
789 |
shouldThrow(); |
|
790 |
} catch (IllegalArgumentException success) {} |
|
791 |
} |
|
792 |
} |
|
793 |
} |
|
794 |
||
795 |
void put(NavigableSet<Integer> set, int element) { |
|
796 |
if (set.add(element)) |
|
797 |
bs.set(element); |
|
798 |
} |
|
799 |
||
800 |
void remove(NavigableSet<Integer> set, int element) { |
|
801 |
if (set.remove(element)) |
|
802 |
bs.clear(element); |
|
803 |
} |
|
804 |
||
805 |
void bashSubSet(NavigableSet<Integer> set, |
|
806 |
int min, int max, boolean ascending) { |
|
807 |
check(set, min, max, ascending); |
|
808 |
check(set.descendingSet(), min, max, !ascending); |
|
809 |
||
810 |
mutateSubSet(set, min, max); |
|
811 |
check(set, min, max, ascending); |
|
812 |
check(set.descendingSet(), min, max, !ascending); |
|
813 |
||
814 |
// Recurse |
|
815 |
if (max - min < 2) |
|
816 |
return; |
|
817 |
int midPoint = (min + max) / 2; |
|
818 |
||
819 |
// headSet - pick direction and endpoint inclusion randomly |
|
820 |
boolean incl = rnd.nextBoolean(); |
|
821 |
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
|
822 |
if (ascending) { |
|
823 |
if (rnd.nextBoolean()) |
|
824 |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
|
825 |
else |
|
826 |
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
|
827 |
false); |
|
828 |
} else { |
|
829 |
if (rnd.nextBoolean()) |
|
830 |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false); |
|
831 |
else |
|
832 |
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
|
833 |
true); |
|
834 |
} |
|
835 |
||
836 |
// tailSet - pick direction and endpoint inclusion randomly |
|
837 |
incl = rnd.nextBoolean(); |
|
838 |
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
|
839 |
if (ascending) { |
|
840 |
if (rnd.nextBoolean()) |
|
841 |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
|
842 |
else |
|
843 |
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max, |
|
844 |
false); |
|
845 |
} else { |
|
846 |
if (rnd.nextBoolean()) { |
|
847 |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false); |
|
848 |
} else { |
|
849 |
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1), |
|
850 |
true); |
|
851 |
} |
|
852 |
} |
|
853 |
||
854 |
// subSet - pick direction and endpoint inclusion randomly |
|
855 |
int rangeSize = max - min + 1; |
|
856 |
int[] endpoints = new int[2]; |
|
857 |
endpoints[0] = min + rnd.nextInt(rangeSize); |
|
858 |
endpoints[1] = min + rnd.nextInt(rangeSize); |
|
859 |
Arrays.sort(endpoints); |
|
860 |
boolean lowIncl = rnd.nextBoolean(); |
|
861 |
boolean highIncl = rnd.nextBoolean(); |
|
862 |
if (ascending) { |
|
863 |
NavigableSet<Integer> sm = set.subSet( |
|
864 |
endpoints[0], lowIncl, endpoints[1], highIncl); |
|
865 |
if (rnd.nextBoolean()) |
|
866 |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
|
867 |
endpoints[1] - (highIncl ? 0 : 1), true); |
|
868 |
else |
|
869 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
|
870 |
endpoints[1] - (highIncl ? 0 : 1), false); |
|
871 |
} else { |
|
872 |
NavigableSet<Integer> sm = set.subSet( |
|
873 |
endpoints[1], highIncl, endpoints[0], lowIncl); |
|
874 |
if (rnd.nextBoolean()) |
|
875 |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
|
876 |
endpoints[1] - (highIncl ? 0 : 1), false); |
|
877 |
else |
|
878 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
|
879 |
endpoints[1] - (highIncl ? 0 : 1), true); |
|
880 |
} |
|
881 |
} |
|
882 |
||
883 |
/** |
|
884 |
* min and max are both inclusive. If max < min, interval is empty. |
|
885 |
*/ |
|
886 |
void check(NavigableSet<Integer> set, |
|
887 |
final int min, final int max, final boolean ascending) { |
|
888 |
class ReferenceSet { |
|
889 |
int lower(int element) { |
|
890 |
return ascending ? |
|
891 |
lowerAscending(element) : higherAscending(element); |
|
892 |
} |
|
893 |
int floor(int element) { |
|
894 |
return ascending ? |
|
895 |
floorAscending(element) : ceilingAscending(element); |
|
896 |
} |
|
897 |
int ceiling(int element) { |
|
898 |
return ascending ? |
|
899 |
ceilingAscending(element) : floorAscending(element); |
|
900 |
} |
|
901 |
int higher(int element) { |
|
902 |
return ascending ? |
|
903 |
higherAscending(element) : lowerAscending(element); |
|
904 |
} |
|
905 |
int first() { |
|
906 |
return ascending ? firstAscending() : lastAscending(); |
|
907 |
} |
|
908 |
int last() { |
|
909 |
return ascending ? lastAscending() : firstAscending(); |
|
910 |
} |
|
911 |
int lowerAscending(int element) { |
|
912 |
return floorAscending(element - 1); |
|
913 |
} |
|
914 |
int floorAscending(int element) { |
|
915 |
if (element < min) |
|
916 |
return -1; |
|
917 |
else if (element > max) |
|
918 |
element = max; |
|
919 |
||
920 |
// BitSet should support this! Test would run much faster |
|
921 |
while (element >= min) { |
|
922 |
if (bs.get(element)) |
|
923 |
return element; |
|
924 |
element--; |
|
925 |
} |
|
926 |
return -1; |
|
927 |
} |
|
928 |
int ceilingAscending(int element) { |
|
929 |
if (element < min) |
|
930 |
element = min; |
|
931 |
else if (element > max) |
|
932 |
return -1; |
|
933 |
int result = bs.nextSetBit(element); |
|
934 |
return (result > max) ? -1 : result; |
|
935 |
} |
|
936 |
int higherAscending(int element) { |
|
937 |
return ceilingAscending(element + 1); |
|
938 |
} |
|
939 |
private int firstAscending() { |
|
940 |
int result = ceilingAscending(min); |
|
941 |
return (result > max) ? -1 : result; |
|
942 |
} |
|
943 |
private int lastAscending() { |
|
944 |
int result = floorAscending(max); |
|
945 |
return (result < min) ? -1 : result; |
|
946 |
} |
|
947 |
} |
|
948 |
ReferenceSet rs = new ReferenceSet(); |
|
949 |
||
950 |
// Test contents using containsElement |
|
951 |
int size = 0; |
|
952 |
for (int i = min; i <= max; i++) { |
|
953 |
boolean bsContainsI = bs.get(i); |
|
954 |
assertEquals(bsContainsI, set.contains(i)); |
|
955 |
if (bsContainsI) |
|
956 |
size++; |
|
957 |
} |
|
958 |
assertEquals(size, set.size()); |
|
959 |
||
960 |
// Test contents using contains elementSet iterator |
|
961 |
int size2 = 0; |
|
962 |
int previousElement = -1; |
|
963 |
for (int element : set) { |
|
964 |
assertTrue(bs.get(element)); |
|
965 |
size2++; |
|
966 |
assertTrue(previousElement < 0 || (ascending ? |
|
967 |
element - previousElement > 0 : element - previousElement < 0)); |
|
968 |
previousElement = element; |
|
969 |
} |
|
970 |
assertEquals(size2, size); |
|
971 |
||
972 |
// Test navigation ops |
|
973 |
for (int element = min - 1; element <= max + 1; element++) { |
|
974 |
assertEq(set.lower(element), rs.lower(element)); |
|
975 |
assertEq(set.floor(element), rs.floor(element)); |
|
976 |
assertEq(set.higher(element), rs.higher(element)); |
|
977 |
assertEq(set.ceiling(element), rs.ceiling(element)); |
|
978 |
} |
|
979 |
||
980 |
// Test extrema |
|
981 |
if (set.size() != 0) { |
|
982 |
assertEq(set.first(), rs.first()); |
|
983 |
assertEq(set.last(), rs.last()); |
|
984 |
} else { |
|
985 |
assertEq(rs.first(), -1); |
|
986 |
assertEq(rs.last(), -1); |
|
987 |
try { |
|
988 |
set.first(); |
|
989 |
shouldThrow(); |
|
990 |
} catch (NoSuchElementException success) {} |
|
991 |
try { |
|
992 |
set.last(); |
|
993 |
shouldThrow(); |
|
994 |
} catch (NoSuchElementException success) {} |
|
995 |
} |
|
996 |
} |
|
997 |
||
998 |
static void assertEq(Integer i, int j) { |
|
999 |
if (i == null) |
|
1000 |
assertEquals(j, -1); |
|
1001 |
else |
|
1002 |
assertEquals((int) i, j); |
|
1003 |
} |
|
1004 |
||
1005 |
static boolean eq(Integer i, int j) { |
|
1006 |
return (i == null) ? j == -1 : i == j; |
|
1007 |
} |
|
1008 |
||
1009 |
} |