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 {
|
|
725 |
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
|
|
726 |
assertEquals(0, result.size());
|
|
727 |
assertFalse(result.iterator().hasNext());
|
|
728 |
return result;
|
|
729 |
}
|
|
730 |
|
|
731 |
void populate(NavigableSet<Integer> set, int limit) {
|
|
732 |
for (int i = 0, n = 2 * limit / 3; i < n; i++) {
|
|
733 |
int element = rnd.nextInt(limit);
|
|
734 |
put(set, element);
|
|
735 |
}
|
|
736 |
}
|
|
737 |
|
|
738 |
void mutateSet(NavigableSet<Integer> set, int min, int max) {
|
|
739 |
int size = set.size();
|
|
740 |
int rangeSize = max - min + 1;
|
|
741 |
|
|
742 |
// Remove a bunch of entries directly
|
|
743 |
for (int i = 0, n = rangeSize / 2; i < n; i++) {
|
|
744 |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
|
|
745 |
}
|
|
746 |
|
|
747 |
// Remove a bunch of entries with iterator
|
|
748 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
|
|
749 |
if (rnd.nextBoolean()) {
|
|
750 |
bs.clear(it.next());
|
|
751 |
it.remove();
|
|
752 |
}
|
|
753 |
}
|
|
754 |
|
|
755 |
// Add entries till we're back to original size
|
|
756 |
while (set.size() < size) {
|
|
757 |
int element = min + rnd.nextInt(rangeSize);
|
|
758 |
assertTrue(element >= min && element <= max);
|
|
759 |
put(set, element);
|
|
760 |
}
|
|
761 |
}
|
|
762 |
|
|
763 |
void mutateSubSet(NavigableSet<Integer> set, int min, int max) {
|
|
764 |
int size = set.size();
|
|
765 |
int rangeSize = max - min + 1;
|
|
766 |
|
|
767 |
// Remove a bunch of entries directly
|
|
768 |
for (int i = 0, n = rangeSize / 2; i < n; i++) {
|
|
769 |
remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
|
|
770 |
}
|
|
771 |
|
|
772 |
// Remove a bunch of entries with iterator
|
|
773 |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
|
|
774 |
if (rnd.nextBoolean()) {
|
|
775 |
bs.clear(it.next());
|
|
776 |
it.remove();
|
|
777 |
}
|
|
778 |
}
|
|
779 |
|
|
780 |
// Add entries till we're back to original size
|
|
781 |
while (set.size() < size) {
|
|
782 |
int element = min - 5 + rnd.nextInt(rangeSize + 10);
|
|
783 |
if (element >= min && element <= max) {
|
|
784 |
put(set, element);
|
|
785 |
} else {
|
|
786 |
try {
|
|
787 |
set.add(element);
|
|
788 |
shouldThrow();
|
|
789 |
} catch (IllegalArgumentException success) {}
|
|
790 |
}
|
|
791 |
}
|
|
792 |
}
|
|
793 |
|
|
794 |
void put(NavigableSet<Integer> set, int element) {
|
|
795 |
if (set.add(element))
|
|
796 |
bs.set(element);
|
|
797 |
}
|
|
798 |
|
|
799 |
void remove(NavigableSet<Integer> set, int element) {
|
|
800 |
if (set.remove(element))
|
|
801 |
bs.clear(element);
|
|
802 |
}
|
|
803 |
|
|
804 |
void bashSubSet(NavigableSet<Integer> set,
|
|
805 |
int min, int max, boolean ascending) {
|
|
806 |
check(set, min, max, ascending);
|
|
807 |
check(set.descendingSet(), min, max, !ascending);
|
|
808 |
|
|
809 |
mutateSubSet(set, min, max);
|
|
810 |
check(set, min, max, ascending);
|
|
811 |
check(set.descendingSet(), min, max, !ascending);
|
|
812 |
|
|
813 |
// Recurse
|
|
814 |
if (max - min < 2)
|
|
815 |
return;
|
|
816 |
int midPoint = (min + max) / 2;
|
|
817 |
|
|
818 |
// headSet - pick direction and endpoint inclusion randomly
|
|
819 |
boolean incl = rnd.nextBoolean();
|
|
820 |
NavigableSet<Integer> hm = set.headSet(midPoint, incl);
|
|
821 |
if (ascending) {
|
|
822 |
if (rnd.nextBoolean())
|
|
823 |
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true);
|
|
824 |
else
|
|
825 |
bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1),
|
|
826 |
false);
|
|
827 |
} else {
|
|
828 |
if (rnd.nextBoolean())
|
|
829 |
bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false);
|
|
830 |
else
|
|
831 |
bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max,
|
|
832 |
true);
|
|
833 |
}
|
|
834 |
|
|
835 |
// tailSet - pick direction and endpoint inclusion randomly
|
|
836 |
incl = rnd.nextBoolean();
|
|
837 |
NavigableSet<Integer> tm = set.tailSet(midPoint,incl);
|
|
838 |
if (ascending) {
|
|
839 |
if (rnd.nextBoolean())
|
|
840 |
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true);
|
|
841 |
else
|
|
842 |
bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max,
|
|
843 |
false);
|
|
844 |
} else {
|
|
845 |
if (rnd.nextBoolean()) {
|
|
846 |
bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false);
|
|
847 |
} else {
|
|
848 |
bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1),
|
|
849 |
true);
|
|
850 |
}
|
|
851 |
}
|
|
852 |
|
|
853 |
// subSet - pick direction and endpoint inclusion randomly
|
|
854 |
int rangeSize = max - min + 1;
|
|
855 |
int[] endpoints = new int[2];
|
|
856 |
endpoints[0] = min + rnd.nextInt(rangeSize);
|
|
857 |
endpoints[1] = min + rnd.nextInt(rangeSize);
|
|
858 |
Arrays.sort(endpoints);
|
|
859 |
boolean lowIncl = rnd.nextBoolean();
|
|
860 |
boolean highIncl = rnd.nextBoolean();
|
|
861 |
if (ascending) {
|
|
862 |
NavigableSet<Integer> sm = set.subSet(
|
|
863 |
endpoints[0], lowIncl, endpoints[1], highIncl);
|
|
864 |
if (rnd.nextBoolean())
|
|
865 |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
|
|
866 |
endpoints[1] - (highIncl ? 0 : 1), true);
|
|
867 |
else
|
|
868 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
|
|
869 |
endpoints[1] - (highIncl ? 0 : 1), false);
|
|
870 |
} else {
|
|
871 |
NavigableSet<Integer> sm = set.subSet(
|
|
872 |
endpoints[1], highIncl, endpoints[0], lowIncl);
|
|
873 |
if (rnd.nextBoolean())
|
|
874 |
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
|
|
875 |
endpoints[1] - (highIncl ? 0 : 1), false);
|
|
876 |
else
|
|
877 |
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
|
|
878 |
endpoints[1] - (highIncl ? 0 : 1), true);
|
|
879 |
}
|
|
880 |
}
|
|
881 |
|
|
882 |
/**
|
|
883 |
* min and max are both inclusive. If max < min, interval is empty.
|
|
884 |
*/
|
|
885 |
void check(NavigableSet<Integer> set,
|
|
886 |
final int min, final int max, final boolean ascending) {
|
|
887 |
class ReferenceSet {
|
|
888 |
int lower(int element) {
|
|
889 |
return ascending ?
|
|
890 |
lowerAscending(element) : higherAscending(element);
|
|
891 |
}
|
|
892 |
int floor(int element) {
|
|
893 |
return ascending ?
|
|
894 |
floorAscending(element) : ceilingAscending(element);
|
|
895 |
}
|
|
896 |
int ceiling(int element) {
|
|
897 |
return ascending ?
|
|
898 |
ceilingAscending(element) : floorAscending(element);
|
|
899 |
}
|
|
900 |
int higher(int element) {
|
|
901 |
return ascending ?
|
|
902 |
higherAscending(element) : lowerAscending(element);
|
|
903 |
}
|
|
904 |
int first() {
|
|
905 |
return ascending ? firstAscending() : lastAscending();
|
|
906 |
}
|
|
907 |
int last() {
|
|
908 |
return ascending ? lastAscending() : firstAscending();
|
|
909 |
}
|
|
910 |
int lowerAscending(int element) {
|
|
911 |
return floorAscending(element - 1);
|
|
912 |
}
|
|
913 |
int floorAscending(int element) {
|
|
914 |
if (element < min)
|
|
915 |
return -1;
|
|
916 |
else if (element > max)
|
|
917 |
element = max;
|
|
918 |
|
|
919 |
// BitSet should support this! Test would run much faster
|
|
920 |
while (element >= min) {
|
|
921 |
if (bs.get(element))
|
|
922 |
return element;
|
|
923 |
element--;
|
|
924 |
}
|
|
925 |
return -1;
|
|
926 |
}
|
|
927 |
int ceilingAscending(int element) {
|
|
928 |
if (element < min)
|
|
929 |
element = min;
|
|
930 |
else if (element > max)
|
|
931 |
return -1;
|
|
932 |
int result = bs.nextSetBit(element);
|
|
933 |
return (result > max) ? -1 : result;
|
|
934 |
}
|
|
935 |
int higherAscending(int element) {
|
|
936 |
return ceilingAscending(element + 1);
|
|
937 |
}
|
|
938 |
private int firstAscending() {
|
|
939 |
int result = ceilingAscending(min);
|
|
940 |
return (result > max) ? -1 : result;
|
|
941 |
}
|
|
942 |
private int lastAscending() {
|
|
943 |
int result = floorAscending(max);
|
|
944 |
return (result < min) ? -1 : result;
|
|
945 |
}
|
|
946 |
}
|
|
947 |
ReferenceSet rs = new ReferenceSet();
|
|
948 |
|
|
949 |
// Test contents using containsElement
|
|
950 |
int size = 0;
|
|
951 |
for (int i = min; i <= max; i++) {
|
|
952 |
boolean bsContainsI = bs.get(i);
|
|
953 |
assertEquals(bsContainsI, set.contains(i));
|
|
954 |
if (bsContainsI)
|
|
955 |
size++;
|
|
956 |
}
|
|
957 |
assertEquals(size, set.size());
|
|
958 |
|
|
959 |
// Test contents using contains elementSet iterator
|
|
960 |
int size2 = 0;
|
|
961 |
int previousElement = -1;
|
|
962 |
for (int element : set) {
|
|
963 |
assertTrue(bs.get(element));
|
|
964 |
size2++;
|
|
965 |
assertTrue(previousElement < 0 || (ascending ?
|
|
966 |
element - previousElement > 0 : element - previousElement < 0));
|
|
967 |
previousElement = element;
|
|
968 |
}
|
|
969 |
assertEquals(size2, size);
|
|
970 |
|
|
971 |
// Test navigation ops
|
|
972 |
for (int element = min - 1; element <= max + 1; element++) {
|
|
973 |
assertEq(set.lower(element), rs.lower(element));
|
|
974 |
assertEq(set.floor(element), rs.floor(element));
|
|
975 |
assertEq(set.higher(element), rs.higher(element));
|
|
976 |
assertEq(set.ceiling(element), rs.ceiling(element));
|
|
977 |
}
|
|
978 |
|
|
979 |
// Test extrema
|
|
980 |
if (set.size() != 0) {
|
|
981 |
assertEq(set.first(), rs.first());
|
|
982 |
assertEq(set.last(), rs.last());
|
|
983 |
} else {
|
|
984 |
assertEq(rs.first(), -1);
|
|
985 |
assertEq(rs.last(), -1);
|
|
986 |
try {
|
|
987 |
set.first();
|
|
988 |
shouldThrow();
|
|
989 |
} catch (NoSuchElementException success) {}
|
|
990 |
try {
|
|
991 |
set.last();
|
|
992 |
shouldThrow();
|
|
993 |
} catch (NoSuchElementException success) {}
|
|
994 |
}
|
|
995 |
}
|
|
996 |
|
|
997 |
static void assertEq(Integer i, int j) {
|
|
998 |
if (i == null)
|
|
999 |
assertEquals(j, -1);
|
|
1000 |
else
|
|
1001 |
assertEquals((int) i, j);
|
|
1002 |
}
|
|
1003 |
|
|
1004 |
static boolean eq(Integer i, int j) {
|
|
1005 |
return (i == null) ? j == -1 : i == j;
|
|
1006 |
}
|
|
1007 |
|
|
1008 |
}
|