1 |
dl |
1.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 |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
import junit.framework.*; |
8 |
|
|
import java.util.*; |
9 |
|
|
import java.util.concurrent.*; |
10 |
|
|
import java.io.*; |
11 |
|
|
|
12 |
|
|
public class TreeSubSetTest extends JSR166TestCase { |
13 |
|
|
public static void main(String[] args) { |
14 |
jsr166 |
1.8 |
junit.textui.TestRunner.run (suite()); |
15 |
dl |
1.1 |
} |
16 |
|
|
public static Test suite() { |
17 |
jsr166 |
1.8 |
return new TestSuite(TreeSubSetTest.class); |
18 |
dl |
1.1 |
} |
19 |
|
|
|
20 |
jsr166 |
1.4 |
static class MyReverseComparator implements Comparator { |
21 |
dl |
1.1 |
public int compare(Object x, Object y) { |
22 |
|
|
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; |
27 |
|
|
} |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
/** |
31 |
|
|
* Create a set of given size containing consecutive |
32 |
|
|
* Integers 0 ... n. |
33 |
|
|
*/ |
34 |
|
|
private NavigableSet populatedSet(int n) { |
35 |
|
|
TreeSet q = new TreeSet(); |
36 |
|
|
assertTrue(q.isEmpty()); |
37 |
|
|
|
38 |
jsr166 |
1.8 |
for (int i = n-1; i >= 0; i-=2) |
39 |
|
|
assertTrue(q.add(new Integer(i))); |
40 |
|
|
for (int i = (n & 1); i < n; i+=2) |
41 |
|
|
assertTrue(q.add(new Integer(i))); |
42 |
dl |
1.1 |
assertTrue(q.add(new Integer(-n))); |
43 |
|
|
assertTrue(q.add(new Integer(n))); |
44 |
dl |
1.3 |
NavigableSet s = q.subSet(new Integer(0), true, new Integer(n), false); |
45 |
dl |
1.1 |
assertFalse(s.isEmpty()); |
46 |
jsr166 |
1.8 |
assertEquals(n, s.size()); |
47 |
dl |
1.1 |
return s; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
/** |
51 |
|
|
* Create set of first 5 ints |
52 |
|
|
*/ |
53 |
|
|
private NavigableSet set5() { |
54 |
|
|
TreeSet q = new TreeSet(); |
55 |
|
|
assertTrue(q.isEmpty()); |
56 |
|
|
q.add(one); |
57 |
|
|
q.add(two); |
58 |
|
|
q.add(three); |
59 |
|
|
q.add(four); |
60 |
|
|
q.add(five); |
61 |
|
|
q.add(zero); |
62 |
|
|
q.add(seven); |
63 |
dl |
1.3 |
NavigableSet s = q.subSet(one, true, seven, false); |
64 |
jsr166 |
1.8 |
assertEquals(5, s.size()); |
65 |
dl |
1.3 |
return s; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
private NavigableSet dset5() { |
69 |
|
|
TreeSet q = new TreeSet(); |
70 |
|
|
assertTrue(q.isEmpty()); |
71 |
|
|
q.add(m1); |
72 |
|
|
q.add(m2); |
73 |
|
|
q.add(m3); |
74 |
|
|
q.add(m4); |
75 |
|
|
q.add(m5); |
76 |
|
|
NavigableSet s = q.descendingSet(); |
77 |
jsr166 |
1.8 |
assertEquals(5, s.size()); |
78 |
dl |
1.1 |
return s; |
79 |
|
|
} |
80 |
|
|
|
81 |
jsr166 |
1.4 |
private static NavigableSet set0() { |
82 |
jsr166 |
1.8 |
TreeSet set = new TreeSet(); |
83 |
dl |
1.1 |
assertTrue(set.isEmpty()); |
84 |
dl |
1.3 |
return set.tailSet(m1, false); |
85 |
|
|
} |
86 |
|
|
|
87 |
jsr166 |
1.4 |
private static NavigableSet dset0() { |
88 |
jsr166 |
1.8 |
TreeSet set = new TreeSet(); |
89 |
dl |
1.3 |
assertTrue(set.isEmpty()); |
90 |
|
|
return set; |
91 |
dl |
1.1 |
} |
92 |
jsr166 |
1.4 |
|
93 |
dl |
1.1 |
/** |
94 |
|
|
* A new set has unbounded capacity |
95 |
|
|
*/ |
96 |
|
|
public void testConstructor1() { |
97 |
|
|
assertEquals(0, set0().size()); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
/** |
102 |
|
|
* isEmpty is true before add, false after |
103 |
|
|
*/ |
104 |
|
|
public void testEmpty() { |
105 |
|
|
NavigableSet q = set0(); |
106 |
|
|
assertTrue(q.isEmpty()); |
107 |
|
|
q.add(new Integer(1)); |
108 |
|
|
assertFalse(q.isEmpty()); |
109 |
|
|
q.add(new Integer(2)); |
110 |
|
|
q.pollFirst(); |
111 |
|
|
q.pollFirst(); |
112 |
|
|
assertTrue(q.isEmpty()); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
/** |
116 |
|
|
* size changes when elements added and removed |
117 |
|
|
*/ |
118 |
|
|
public void testSize() { |
119 |
|
|
NavigableSet q = populatedSet(SIZE); |
120 |
|
|
for (int i = 0; i < SIZE; ++i) { |
121 |
|
|
assertEquals(SIZE-i, q.size()); |
122 |
|
|
q.pollFirst(); |
123 |
|
|
} |
124 |
|
|
for (int i = 0; i < SIZE; ++i) { |
125 |
|
|
assertEquals(i, q.size()); |
126 |
|
|
q.add(new Integer(i)); |
127 |
|
|
} |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/** |
131 |
|
|
* add(null) throws NPE |
132 |
|
|
*/ |
133 |
|
|
public void testAddNull() { |
134 |
jsr166 |
1.8 |
try { |
135 |
dl |
1.1 |
NavigableSet q = set0(); |
136 |
|
|
q.add(null); |
137 |
|
|
shouldThrow(); |
138 |
jsr166 |
1.7 |
} catch (NullPointerException success) {} |
139 |
dl |
1.1 |
} |
140 |
|
|
|
141 |
|
|
/** |
142 |
|
|
* Add of comparable element succeeds |
143 |
|
|
*/ |
144 |
|
|
public void testAdd() { |
145 |
|
|
NavigableSet q = set0(); |
146 |
|
|
assertTrue(q.add(six)); |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
/** |
150 |
|
|
* Add of duplicate element fails |
151 |
|
|
*/ |
152 |
|
|
public void testAddDup() { |
153 |
|
|
NavigableSet q = set0(); |
154 |
|
|
assertTrue(q.add(six)); |
155 |
|
|
assertFalse(q.add(six)); |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
/** |
159 |
|
|
* Add of non-Comparable throws CCE |
160 |
|
|
*/ |
161 |
|
|
public void testAddNonComparable() { |
162 |
|
|
try { |
163 |
|
|
NavigableSet q = set0(); |
164 |
|
|
q.add(new Object()); |
165 |
|
|
q.add(new Object()); |
166 |
|
|
q.add(new Object()); |
167 |
|
|
shouldThrow(); |
168 |
jsr166 |
1.7 |
} catch (ClassCastException success) {} |
169 |
dl |
1.1 |
} |
170 |
|
|
|
171 |
|
|
|
172 |
|
|
/** |
173 |
|
|
* addAll(null) throws NPE |
174 |
|
|
*/ |
175 |
|
|
public void testAddAll1() { |
176 |
|
|
try { |
177 |
|
|
NavigableSet q = set0(); |
178 |
|
|
q.addAll(null); |
179 |
|
|
shouldThrow(); |
180 |
jsr166 |
1.7 |
} catch (NullPointerException success) {} |
181 |
dl |
1.1 |
} |
182 |
|
|
/** |
183 |
|
|
* addAll of a collection with null elements throws NPE |
184 |
|
|
*/ |
185 |
|
|
public void testAddAll2() { |
186 |
|
|
try { |
187 |
|
|
NavigableSet q = set0(); |
188 |
|
|
Integer[] ints = new Integer[SIZE]; |
189 |
|
|
q.addAll(Arrays.asList(ints)); |
190 |
|
|
shouldThrow(); |
191 |
jsr166 |
1.7 |
} catch (NullPointerException success) {} |
192 |
dl |
1.1 |
} |
193 |
|
|
/** |
194 |
|
|
* addAll of a collection with any null elements throws NPE after |
195 |
|
|
* possibly adding some elements |
196 |
|
|
*/ |
197 |
|
|
public void testAddAll3() { |
198 |
|
|
try { |
199 |
|
|
NavigableSet q = set0(); |
200 |
|
|
Integer[] ints = new Integer[SIZE]; |
201 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
202 |
|
|
ints[i] = new Integer(i+SIZE); |
203 |
|
|
q.addAll(Arrays.asList(ints)); |
204 |
|
|
shouldThrow(); |
205 |
jsr166 |
1.7 |
} catch (NullPointerException success) {} |
206 |
dl |
1.1 |
} |
207 |
|
|
|
208 |
|
|
/** |
209 |
|
|
* Set contains all elements of successful addAll |
210 |
|
|
*/ |
211 |
|
|
public void testAddAll5() { |
212 |
jsr166 |
1.9 |
Integer[] empty = new Integer[0]; |
213 |
|
|
Integer[] ints = new Integer[SIZE]; |
214 |
|
|
for (int i = 0; i < SIZE; ++i) |
215 |
|
|
ints[i] = new Integer(SIZE-1- i); |
216 |
|
|
NavigableSet q = set0(); |
217 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
218 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
219 |
|
|
for (int i = 0; i < SIZE; ++i) |
220 |
|
|
assertEquals(new Integer(i), q.pollFirst()); |
221 |
dl |
1.1 |
} |
222 |
|
|
|
223 |
|
|
/** |
224 |
|
|
* poll succeeds unless empty |
225 |
|
|
*/ |
226 |
|
|
public void testPoll() { |
227 |
|
|
NavigableSet q = populatedSet(SIZE); |
228 |
|
|
for (int i = 0; i < SIZE; ++i) { |
229 |
|
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
230 |
|
|
} |
231 |
jsr166 |
1.8 |
assertNull(q.pollFirst()); |
232 |
dl |
1.1 |
} |
233 |
|
|
|
234 |
|
|
/** |
235 |
|
|
* remove(x) removes x and returns true if present |
236 |
|
|
*/ |
237 |
|
|
public void testRemoveElement() { |
238 |
|
|
NavigableSet q = populatedSet(SIZE); |
239 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
240 |
|
|
assertTrue(q.remove(new Integer(i))); |
241 |
|
|
} |
242 |
|
|
for (int i = 0; i < SIZE; i+=2) { |
243 |
|
|
assertTrue(q.remove(new Integer(i))); |
244 |
|
|
assertFalse(q.remove(new Integer(i+1))); |
245 |
|
|
} |
246 |
|
|
assertTrue(q.isEmpty()); |
247 |
|
|
} |
248 |
jsr166 |
1.4 |
|
249 |
dl |
1.1 |
/** |
250 |
|
|
* contains(x) reports true when elements added but not yet removed |
251 |
|
|
*/ |
252 |
|
|
public void testContains() { |
253 |
|
|
NavigableSet q = populatedSet(SIZE); |
254 |
|
|
for (int i = 0; i < SIZE; ++i) { |
255 |
|
|
assertTrue(q.contains(new Integer(i))); |
256 |
|
|
q.pollFirst(); |
257 |
|
|
assertFalse(q.contains(new Integer(i))); |
258 |
|
|
} |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
/** |
262 |
|
|
* clear removes all elements |
263 |
|
|
*/ |
264 |
|
|
public void testClear() { |
265 |
|
|
NavigableSet q = populatedSet(SIZE); |
266 |
|
|
q.clear(); |
267 |
|
|
assertTrue(q.isEmpty()); |
268 |
|
|
assertEquals(0, q.size()); |
269 |
|
|
q.add(new Integer(1)); |
270 |
|
|
assertFalse(q.isEmpty()); |
271 |
|
|
q.clear(); |
272 |
|
|
assertTrue(q.isEmpty()); |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
/** |
276 |
|
|
* containsAll(c) is true when c contains a subset of elements |
277 |
|
|
*/ |
278 |
|
|
public void testContainsAll() { |
279 |
|
|
NavigableSet q = populatedSet(SIZE); |
280 |
|
|
NavigableSet p = set0(); |
281 |
|
|
for (int i = 0; i < SIZE; ++i) { |
282 |
|
|
assertTrue(q.containsAll(p)); |
283 |
|
|
assertFalse(p.containsAll(q)); |
284 |
|
|
p.add(new Integer(i)); |
285 |
|
|
} |
286 |
|
|
assertTrue(p.containsAll(q)); |
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
/** |
290 |
|
|
* retainAll(c) retains only those elements of c and reports true if changed |
291 |
|
|
*/ |
292 |
|
|
public void testRetainAll() { |
293 |
|
|
NavigableSet q = populatedSet(SIZE); |
294 |
|
|
NavigableSet p = populatedSet(SIZE); |
295 |
|
|
for (int i = 0; i < SIZE; ++i) { |
296 |
|
|
boolean changed = q.retainAll(p); |
297 |
|
|
if (i == 0) |
298 |
|
|
assertFalse(changed); |
299 |
|
|
else |
300 |
|
|
assertTrue(changed); |
301 |
|
|
|
302 |
|
|
assertTrue(q.containsAll(p)); |
303 |
|
|
assertEquals(SIZE-i, q.size()); |
304 |
|
|
p.pollFirst(); |
305 |
|
|
} |
306 |
|
|
} |
307 |
|
|
|
308 |
|
|
/** |
309 |
|
|
* removeAll(c) removes only those elements of c and reports true if changed |
310 |
|
|
*/ |
311 |
|
|
public void testRemoveAll() { |
312 |
|
|
for (int i = 1; i < SIZE; ++i) { |
313 |
|
|
NavigableSet q = populatedSet(SIZE); |
314 |
|
|
NavigableSet p = populatedSet(i); |
315 |
|
|
assertTrue(q.removeAll(p)); |
316 |
|
|
assertEquals(SIZE-i, q.size()); |
317 |
|
|
for (int j = 0; j < i; ++j) { |
318 |
|
|
Integer I = (Integer)(p.pollFirst()); |
319 |
|
|
assertFalse(q.contains(I)); |
320 |
|
|
} |
321 |
|
|
} |
322 |
|
|
} |
323 |
|
|
|
324 |
jsr166 |
1.4 |
|
325 |
dl |
1.1 |
|
326 |
|
|
/** |
327 |
|
|
* lower returns preceding element |
328 |
|
|
*/ |
329 |
|
|
public void testLower() { |
330 |
|
|
NavigableSet q = set5(); |
331 |
|
|
Object e1 = q.lower(three); |
332 |
|
|
assertEquals(two, e1); |
333 |
|
|
|
334 |
|
|
Object e2 = q.lower(six); |
335 |
|
|
assertEquals(five, e2); |
336 |
|
|
|
337 |
|
|
Object e3 = q.lower(one); |
338 |
|
|
assertNull(e3); |
339 |
|
|
|
340 |
|
|
Object e4 = q.lower(zero); |
341 |
|
|
assertNull(e4); |
342 |
|
|
|
343 |
|
|
} |
344 |
|
|
|
345 |
|
|
/** |
346 |
|
|
* higher returns next element |
347 |
|
|
*/ |
348 |
|
|
public void testHigher() { |
349 |
|
|
NavigableSet q = set5(); |
350 |
|
|
Object e1 = q.higher(three); |
351 |
|
|
assertEquals(four, e1); |
352 |
|
|
|
353 |
|
|
Object e2 = q.higher(zero); |
354 |
|
|
assertEquals(one, e2); |
355 |
|
|
|
356 |
|
|
Object e3 = q.higher(five); |
357 |
|
|
assertNull(e3); |
358 |
|
|
|
359 |
|
|
Object e4 = q.higher(six); |
360 |
|
|
assertNull(e4); |
361 |
|
|
|
362 |
|
|
} |
363 |
|
|
|
364 |
|
|
/** |
365 |
|
|
* floor returns preceding element |
366 |
|
|
*/ |
367 |
|
|
public void testFloor() { |
368 |
|
|
NavigableSet q = set5(); |
369 |
|
|
Object e1 = q.floor(three); |
370 |
|
|
assertEquals(three, e1); |
371 |
|
|
|
372 |
|
|
Object e2 = q.floor(six); |
373 |
|
|
assertEquals(five, e2); |
374 |
|
|
|
375 |
|
|
Object e3 = q.floor(one); |
376 |
|
|
assertEquals(one, e3); |
377 |
|
|
|
378 |
|
|
Object e4 = q.floor(zero); |
379 |
|
|
assertNull(e4); |
380 |
|
|
|
381 |
|
|
} |
382 |
|
|
|
383 |
|
|
/** |
384 |
|
|
* ceiling returns next element |
385 |
|
|
*/ |
386 |
|
|
public void testCeiling() { |
387 |
|
|
NavigableSet q = set5(); |
388 |
|
|
Object e1 = q.ceiling(three); |
389 |
|
|
assertEquals(three, e1); |
390 |
|
|
|
391 |
|
|
Object e2 = q.ceiling(zero); |
392 |
|
|
assertEquals(one, e2); |
393 |
|
|
|
394 |
|
|
Object e3 = q.ceiling(five); |
395 |
|
|
assertEquals(five, e3); |
396 |
|
|
|
397 |
|
|
Object e4 = q.ceiling(six); |
398 |
|
|
assertNull(e4); |
399 |
|
|
|
400 |
|
|
} |
401 |
|
|
|
402 |
|
|
/** |
403 |
|
|
* toArray contains all elements |
404 |
|
|
*/ |
405 |
|
|
public void testToArray() { |
406 |
|
|
NavigableSet q = populatedSet(SIZE); |
407 |
jsr166 |
1.8 |
Object[] o = q.toArray(); |
408 |
dl |
1.1 |
Arrays.sort(o); |
409 |
jsr166 |
1.8 |
for (int i = 0; i < o.length; i++) |
410 |
|
|
assertEquals(o[i], q.pollFirst()); |
411 |
dl |
1.1 |
} |
412 |
|
|
|
413 |
|
|
/** |
414 |
|
|
* toArray(a) contains all elements |
415 |
|
|
*/ |
416 |
|
|
public void testToArray2() { |
417 |
|
|
NavigableSet q = populatedSet(SIZE); |
418 |
jsr166 |
1.8 |
Integer[] ints = new Integer[SIZE]; |
419 |
|
|
ints = (Integer[])q.toArray(ints); |
420 |
dl |
1.1 |
Arrays.sort(ints); |
421 |
jsr166 |
1.5 |
for (int i = 0; i < ints.length; i++) |
422 |
dl |
1.1 |
assertEquals(ints[i], q.pollFirst()); |
423 |
|
|
} |
424 |
jsr166 |
1.4 |
|
425 |
dl |
1.1 |
/** |
426 |
|
|
* iterator iterates through all elements |
427 |
|
|
*/ |
428 |
|
|
public void testIterator() { |
429 |
|
|
NavigableSet q = populatedSet(SIZE); |
430 |
|
|
int i = 0; |
431 |
jsr166 |
1.8 |
Iterator it = q.iterator(); |
432 |
jsr166 |
1.5 |
while (it.hasNext()) { |
433 |
dl |
1.1 |
assertTrue(q.contains(it.next())); |
434 |
|
|
++i; |
435 |
|
|
} |
436 |
|
|
assertEquals(i, SIZE); |
437 |
|
|
} |
438 |
|
|
|
439 |
|
|
/** |
440 |
|
|
* iterator of empty set has no elements |
441 |
|
|
*/ |
442 |
|
|
public void testEmptyIterator() { |
443 |
|
|
NavigableSet q = set0(); |
444 |
|
|
int i = 0; |
445 |
jsr166 |
1.8 |
Iterator it = q.iterator(); |
446 |
jsr166 |
1.5 |
while (it.hasNext()) { |
447 |
dl |
1.1 |
assertTrue(q.contains(it.next())); |
448 |
|
|
++i; |
449 |
|
|
} |
450 |
|
|
assertEquals(i, 0); |
451 |
|
|
} |
452 |
|
|
|
453 |
|
|
/** |
454 |
|
|
* iterator.remove removes current element |
455 |
|
|
*/ |
456 |
|
|
public void testIteratorRemove () { |
457 |
|
|
final NavigableSet q = set0(); |
458 |
|
|
q.add(new Integer(2)); |
459 |
|
|
q.add(new Integer(1)); |
460 |
|
|
q.add(new Integer(3)); |
461 |
|
|
|
462 |
|
|
Iterator it = q.iterator(); |
463 |
|
|
it.next(); |
464 |
|
|
it.remove(); |
465 |
|
|
|
466 |
|
|
it = q.iterator(); |
467 |
|
|
assertEquals(it.next(), new Integer(2)); |
468 |
|
|
assertEquals(it.next(), new Integer(3)); |
469 |
|
|
assertFalse(it.hasNext()); |
470 |
|
|
} |
471 |
|
|
|
472 |
|
|
|
473 |
|
|
/** |
474 |
|
|
* toString contains toStrings of elements |
475 |
|
|
*/ |
476 |
|
|
public void testToString() { |
477 |
|
|
NavigableSet q = populatedSet(SIZE); |
478 |
|
|
String s = q.toString(); |
479 |
|
|
for (int i = 0; i < SIZE; ++i) { |
480 |
|
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
481 |
|
|
} |
482 |
jsr166 |
1.4 |
} |
483 |
dl |
1.1 |
|
484 |
|
|
/** |
485 |
jsr166 |
1.4 |
* A deserialized serialized set has same elements |
486 |
dl |
1.1 |
*/ |
487 |
jsr166 |
1.7 |
public void testSerialization() throws Exception { |
488 |
dl |
1.1 |
NavigableSet q = populatedSet(SIZE); |
489 |
jsr166 |
1.7 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
490 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
491 |
|
|
out.writeObject(q); |
492 |
|
|
out.close(); |
493 |
|
|
|
494 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
495 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
496 |
|
|
NavigableSet r = (NavigableSet)in.readObject(); |
497 |
|
|
assertEquals(q.size(), r.size()); |
498 |
|
|
while (!q.isEmpty()) |
499 |
|
|
assertEquals(q.pollFirst(), r.pollFirst()); |
500 |
dl |
1.1 |
} |
501 |
|
|
|
502 |
|
|
/** |
503 |
|
|
* subSet returns set with keys in requested range |
504 |
|
|
*/ |
505 |
|
|
public void testSubSetContents() { |
506 |
|
|
NavigableSet set = set5(); |
507 |
|
|
SortedSet sm = set.subSet(two, four); |
508 |
|
|
assertEquals(two, sm.first()); |
509 |
|
|
assertEquals(three, sm.last()); |
510 |
|
|
assertEquals(2, sm.size()); |
511 |
|
|
assertFalse(sm.contains(one)); |
512 |
|
|
assertTrue(sm.contains(two)); |
513 |
|
|
assertTrue(sm.contains(three)); |
514 |
|
|
assertFalse(sm.contains(four)); |
515 |
|
|
assertFalse(sm.contains(five)); |
516 |
|
|
Iterator i = sm.iterator(); |
517 |
|
|
Object k; |
518 |
|
|
k = (Integer)(i.next()); |
519 |
|
|
assertEquals(two, k); |
520 |
|
|
k = (Integer)(i.next()); |
521 |
|
|
assertEquals(three, k); |
522 |
|
|
assertFalse(i.hasNext()); |
523 |
|
|
Iterator j = sm.iterator(); |
524 |
|
|
j.next(); |
525 |
|
|
j.remove(); |
526 |
|
|
assertFalse(set.contains(two)); |
527 |
|
|
assertEquals(4, set.size()); |
528 |
|
|
assertEquals(1, sm.size()); |
529 |
|
|
assertEquals(three, sm.first()); |
530 |
|
|
assertEquals(three, sm.last()); |
531 |
|
|
assertTrue(sm.remove(three)); |
532 |
|
|
assertTrue(sm.isEmpty()); |
533 |
|
|
assertEquals(3, set.size()); |
534 |
|
|
} |
535 |
|
|
|
536 |
|
|
public void testSubSetContents2() { |
537 |
|
|
NavigableSet set = set5(); |
538 |
|
|
SortedSet sm = set.subSet(two, three); |
539 |
|
|
assertEquals(1, sm.size()); |
540 |
|
|
assertEquals(two, sm.first()); |
541 |
|
|
assertEquals(two, sm.last()); |
542 |
|
|
assertFalse(sm.contains(one)); |
543 |
|
|
assertTrue(sm.contains(two)); |
544 |
|
|
assertFalse(sm.contains(three)); |
545 |
|
|
assertFalse(sm.contains(four)); |
546 |
|
|
assertFalse(sm.contains(five)); |
547 |
|
|
Iterator i = sm.iterator(); |
548 |
|
|
Object k; |
549 |
|
|
k = (Integer)(i.next()); |
550 |
|
|
assertEquals(two, k); |
551 |
|
|
assertFalse(i.hasNext()); |
552 |
|
|
Iterator j = sm.iterator(); |
553 |
|
|
j.next(); |
554 |
|
|
j.remove(); |
555 |
|
|
assertFalse(set.contains(two)); |
556 |
|
|
assertEquals(4, set.size()); |
557 |
|
|
assertEquals(0, sm.size()); |
558 |
|
|
assertTrue(sm.isEmpty()); |
559 |
|
|
assertFalse(sm.remove(three)); |
560 |
|
|
assertEquals(4, set.size()); |
561 |
|
|
} |
562 |
|
|
|
563 |
|
|
/** |
564 |
|
|
* headSet returns set with keys in requested range |
565 |
|
|
*/ |
566 |
|
|
public void testHeadSetContents() { |
567 |
|
|
NavigableSet set = set5(); |
568 |
|
|
SortedSet sm = set.headSet(four); |
569 |
|
|
assertTrue(sm.contains(one)); |
570 |
|
|
assertTrue(sm.contains(two)); |
571 |
|
|
assertTrue(sm.contains(three)); |
572 |
|
|
assertFalse(sm.contains(four)); |
573 |
|
|
assertFalse(sm.contains(five)); |
574 |
|
|
Iterator i = sm.iterator(); |
575 |
|
|
Object k; |
576 |
|
|
k = (Integer)(i.next()); |
577 |
|
|
assertEquals(one, k); |
578 |
|
|
k = (Integer)(i.next()); |
579 |
|
|
assertEquals(two, k); |
580 |
|
|
k = (Integer)(i.next()); |
581 |
|
|
assertEquals(three, k); |
582 |
|
|
assertFalse(i.hasNext()); |
583 |
|
|
sm.clear(); |
584 |
|
|
assertTrue(sm.isEmpty()); |
585 |
|
|
assertEquals(2, set.size()); |
586 |
|
|
assertEquals(four, set.first()); |
587 |
|
|
} |
588 |
|
|
|
589 |
|
|
/** |
590 |
|
|
* tailSet returns set with keys in requested range |
591 |
|
|
*/ |
592 |
|
|
public void testTailSetContents() { |
593 |
|
|
NavigableSet set = set5(); |
594 |
|
|
SortedSet sm = set.tailSet(two); |
595 |
|
|
assertFalse(sm.contains(one)); |
596 |
|
|
assertTrue(sm.contains(two)); |
597 |
|
|
assertTrue(sm.contains(three)); |
598 |
|
|
assertTrue(sm.contains(four)); |
599 |
|
|
assertTrue(sm.contains(five)); |
600 |
|
|
Iterator i = sm.iterator(); |
601 |
|
|
Object k; |
602 |
|
|
k = (Integer)(i.next()); |
603 |
|
|
assertEquals(two, k); |
604 |
|
|
k = (Integer)(i.next()); |
605 |
|
|
assertEquals(three, k); |
606 |
|
|
k = (Integer)(i.next()); |
607 |
|
|
assertEquals(four, k); |
608 |
|
|
k = (Integer)(i.next()); |
609 |
|
|
assertEquals(five, k); |
610 |
|
|
assertFalse(i.hasNext()); |
611 |
|
|
|
612 |
|
|
SortedSet ssm = sm.tailSet(four); |
613 |
|
|
assertEquals(four, ssm.first()); |
614 |
|
|
assertEquals(five, ssm.last()); |
615 |
|
|
assertTrue(ssm.remove(four)); |
616 |
|
|
assertEquals(1, ssm.size()); |
617 |
|
|
assertEquals(3, sm.size()); |
618 |
|
|
assertEquals(4, set.size()); |
619 |
|
|
} |
620 |
|
|
|
621 |
dl |
1.3 |
/** |
622 |
|
|
* size changes when elements added and removed |
623 |
|
|
*/ |
624 |
|
|
public void testDescendingSize() { |
625 |
|
|
NavigableSet q = populatedSet(SIZE); |
626 |
|
|
for (int i = 0; i < SIZE; ++i) { |
627 |
|
|
assertEquals(SIZE-i, q.size()); |
628 |
|
|
q.pollFirst(); |
629 |
|
|
} |
630 |
|
|
for (int i = 0; i < SIZE; ++i) { |
631 |
|
|
assertEquals(i, q.size()); |
632 |
|
|
q.add(new Integer(i)); |
633 |
|
|
} |
634 |
|
|
} |
635 |
|
|
|
636 |
|
|
/** |
637 |
|
|
* Add of comparable element succeeds |
638 |
|
|
*/ |
639 |
|
|
public void testDescendingAdd() { |
640 |
|
|
NavigableSet q = dset0(); |
641 |
|
|
assertTrue(q.add(m6)); |
642 |
|
|
} |
643 |
|
|
|
644 |
|
|
/** |
645 |
|
|
* Add of duplicate element fails |
646 |
|
|
*/ |
647 |
|
|
public void testDescendingAddDup() { |
648 |
|
|
NavigableSet q = dset0(); |
649 |
|
|
assertTrue(q.add(m6)); |
650 |
|
|
assertFalse(q.add(m6)); |
651 |
|
|
} |
652 |
|
|
|
653 |
|
|
/** |
654 |
|
|
* Add of non-Comparable throws CCE |
655 |
|
|
*/ |
656 |
|
|
public void testDescendingAddNonComparable() { |
657 |
|
|
try { |
658 |
|
|
NavigableSet q = dset0(); |
659 |
|
|
q.add(new Object()); |
660 |
|
|
q.add(new Object()); |
661 |
|
|
q.add(new Object()); |
662 |
|
|
shouldThrow(); |
663 |
jsr166 |
1.7 |
} catch (ClassCastException success) {} |
664 |
dl |
1.3 |
} |
665 |
|
|
|
666 |
|
|
|
667 |
|
|
/** |
668 |
|
|
* addAll(null) throws NPE |
669 |
|
|
*/ |
670 |
|
|
public void testDescendingAddAll1() { |
671 |
|
|
try { |
672 |
|
|
NavigableSet q = dset0(); |
673 |
|
|
q.addAll(null); |
674 |
|
|
shouldThrow(); |
675 |
jsr166 |
1.7 |
} catch (NullPointerException success) {} |
676 |
dl |
1.3 |
} |
677 |
|
|
/** |
678 |
|
|
* addAll of a collection with null elements throws NPE |
679 |
|
|
*/ |
680 |
|
|
public void testDescendingAddAll2() { |
681 |
|
|
try { |
682 |
|
|
NavigableSet q = dset0(); |
683 |
|
|
Integer[] ints = new Integer[SIZE]; |
684 |
|
|
q.addAll(Arrays.asList(ints)); |
685 |
|
|
shouldThrow(); |
686 |
jsr166 |
1.7 |
} catch (NullPointerException success) {} |
687 |
dl |
1.3 |
} |
688 |
|
|
/** |
689 |
|
|
* addAll of a collection with any null elements throws NPE after |
690 |
|
|
* possibly adding some elements |
691 |
|
|
*/ |
692 |
|
|
public void testDescendingAddAll3() { |
693 |
|
|
try { |
694 |
|
|
NavigableSet q = dset0(); |
695 |
|
|
Integer[] ints = new Integer[SIZE]; |
696 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
697 |
|
|
ints[i] = new Integer(i+SIZE); |
698 |
|
|
q.addAll(Arrays.asList(ints)); |
699 |
|
|
shouldThrow(); |
700 |
jsr166 |
1.7 |
} catch (NullPointerException success) {} |
701 |
dl |
1.3 |
} |
702 |
|
|
|
703 |
|
|
/** |
704 |
|
|
* Set contains all elements of successful addAll |
705 |
|
|
*/ |
706 |
|
|
public void testDescendingAddAll5() { |
707 |
jsr166 |
1.7 |
Integer[] empty = new Integer[0]; |
708 |
|
|
Integer[] ints = new Integer[SIZE]; |
709 |
|
|
for (int i = 0; i < SIZE; ++i) |
710 |
|
|
ints[i] = new Integer(SIZE-1- i); |
711 |
|
|
NavigableSet q = dset0(); |
712 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
713 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
714 |
|
|
for (int i = 0; i < SIZE; ++i) |
715 |
|
|
assertEquals(new Integer(i), q.pollFirst()); |
716 |
dl |
1.3 |
} |
717 |
|
|
|
718 |
|
|
/** |
719 |
|
|
* poll succeeds unless empty |
720 |
|
|
*/ |
721 |
|
|
public void testDescendingPoll() { |
722 |
|
|
NavigableSet q = populatedSet(SIZE); |
723 |
|
|
for (int i = 0; i < SIZE; ++i) { |
724 |
|
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
725 |
|
|
} |
726 |
jsr166 |
1.8 |
assertNull(q.pollFirst()); |
727 |
dl |
1.3 |
} |
728 |
|
|
|
729 |
|
|
/** |
730 |
|
|
* remove(x) removes x and returns true if present |
731 |
|
|
*/ |
732 |
|
|
public void testDescendingRemoveElement() { |
733 |
|
|
NavigableSet q = populatedSet(SIZE); |
734 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
735 |
|
|
assertTrue(q.remove(new Integer(i))); |
736 |
|
|
} |
737 |
|
|
for (int i = 0; i < SIZE; i+=2) { |
738 |
|
|
assertTrue(q.remove(new Integer(i))); |
739 |
|
|
assertFalse(q.remove(new Integer(i+1))); |
740 |
|
|
} |
741 |
|
|
assertTrue(q.isEmpty()); |
742 |
|
|
} |
743 |
jsr166 |
1.4 |
|
744 |
dl |
1.3 |
/** |
745 |
|
|
* contains(x) reports true when elements added but not yet removed |
746 |
|
|
*/ |
747 |
|
|
public void testDescendingContains() { |
748 |
|
|
NavigableSet q = populatedSet(SIZE); |
749 |
|
|
for (int i = 0; i < SIZE; ++i) { |
750 |
|
|
assertTrue(q.contains(new Integer(i))); |
751 |
|
|
q.pollFirst(); |
752 |
|
|
assertFalse(q.contains(new Integer(i))); |
753 |
|
|
} |
754 |
|
|
} |
755 |
|
|
|
756 |
|
|
/** |
757 |
|
|
* clear removes all elements |
758 |
|
|
*/ |
759 |
|
|
public void testDescendingClear() { |
760 |
|
|
NavigableSet q = populatedSet(SIZE); |
761 |
|
|
q.clear(); |
762 |
|
|
assertTrue(q.isEmpty()); |
763 |
|
|
assertEquals(0, q.size()); |
764 |
|
|
q.add(new Integer(1)); |
765 |
|
|
assertFalse(q.isEmpty()); |
766 |
|
|
q.clear(); |
767 |
|
|
assertTrue(q.isEmpty()); |
768 |
|
|
} |
769 |
|
|
|
770 |
|
|
/** |
771 |
|
|
* containsAll(c) is true when c contains a subset of elements |
772 |
|
|
*/ |
773 |
|
|
public void testDescendingContainsAll() { |
774 |
|
|
NavigableSet q = populatedSet(SIZE); |
775 |
|
|
NavigableSet p = dset0(); |
776 |
|
|
for (int i = 0; i < SIZE; ++i) { |
777 |
|
|
assertTrue(q.containsAll(p)); |
778 |
|
|
assertFalse(p.containsAll(q)); |
779 |
|
|
p.add(new Integer(i)); |
780 |
|
|
} |
781 |
|
|
assertTrue(p.containsAll(q)); |
782 |
|
|
} |
783 |
|
|
|
784 |
|
|
/** |
785 |
|
|
* retainAll(c) retains only those elements of c and reports true if changed |
786 |
|
|
*/ |
787 |
|
|
public void testDescendingRetainAll() { |
788 |
|
|
NavigableSet q = populatedSet(SIZE); |
789 |
|
|
NavigableSet p = populatedSet(SIZE); |
790 |
|
|
for (int i = 0; i < SIZE; ++i) { |
791 |
|
|
boolean changed = q.retainAll(p); |
792 |
|
|
if (i == 0) |
793 |
|
|
assertFalse(changed); |
794 |
|
|
else |
795 |
|
|
assertTrue(changed); |
796 |
|
|
|
797 |
|
|
assertTrue(q.containsAll(p)); |
798 |
|
|
assertEquals(SIZE-i, q.size()); |
799 |
|
|
p.pollFirst(); |
800 |
|
|
} |
801 |
|
|
} |
802 |
|
|
|
803 |
|
|
/** |
804 |
|
|
* removeAll(c) removes only those elements of c and reports true if changed |
805 |
|
|
*/ |
806 |
|
|
public void testDescendingRemoveAll() { |
807 |
|
|
for (int i = 1; i < SIZE; ++i) { |
808 |
|
|
NavigableSet q = populatedSet(SIZE); |
809 |
|
|
NavigableSet p = populatedSet(i); |
810 |
|
|
assertTrue(q.removeAll(p)); |
811 |
|
|
assertEquals(SIZE-i, q.size()); |
812 |
|
|
for (int j = 0; j < i; ++j) { |
813 |
|
|
Integer I = (Integer)(p.pollFirst()); |
814 |
|
|
assertFalse(q.contains(I)); |
815 |
|
|
} |
816 |
|
|
} |
817 |
|
|
} |
818 |
|
|
|
819 |
jsr166 |
1.4 |
|
820 |
dl |
1.3 |
|
821 |
|
|
/** |
822 |
|
|
* lower returns preceding element |
823 |
|
|
*/ |
824 |
|
|
public void testDescendingLower() { |
825 |
|
|
NavigableSet q = dset5(); |
826 |
|
|
Object e1 = q.lower(m3); |
827 |
|
|
assertEquals(m2, e1); |
828 |
|
|
|
829 |
|
|
Object e2 = q.lower(m6); |
830 |
|
|
assertEquals(m5, e2); |
831 |
|
|
|
832 |
|
|
Object e3 = q.lower(m1); |
833 |
|
|
assertNull(e3); |
834 |
|
|
|
835 |
|
|
Object e4 = q.lower(zero); |
836 |
|
|
assertNull(e4); |
837 |
|
|
|
838 |
|
|
} |
839 |
|
|
|
840 |
|
|
/** |
841 |
|
|
* higher returns next element |
842 |
|
|
*/ |
843 |
|
|
public void testDescendingHigher() { |
844 |
|
|
NavigableSet q = dset5(); |
845 |
|
|
Object e1 = q.higher(m3); |
846 |
|
|
assertEquals(m4, e1); |
847 |
|
|
|
848 |
|
|
Object e2 = q.higher(zero); |
849 |
|
|
assertEquals(m1, e2); |
850 |
|
|
|
851 |
|
|
Object e3 = q.higher(m5); |
852 |
|
|
assertNull(e3); |
853 |
|
|
|
854 |
|
|
Object e4 = q.higher(m6); |
855 |
|
|
assertNull(e4); |
856 |
|
|
|
857 |
|
|
} |
858 |
|
|
|
859 |
|
|
/** |
860 |
|
|
* floor returns preceding element |
861 |
|
|
*/ |
862 |
|
|
public void testDescendingFloor() { |
863 |
|
|
NavigableSet q = dset5(); |
864 |
|
|
Object e1 = q.floor(m3); |
865 |
|
|
assertEquals(m3, e1); |
866 |
|
|
|
867 |
|
|
Object e2 = q.floor(m6); |
868 |
|
|
assertEquals(m5, e2); |
869 |
|
|
|
870 |
|
|
Object e3 = q.floor(m1); |
871 |
|
|
assertEquals(m1, e3); |
872 |
|
|
|
873 |
|
|
Object e4 = q.floor(zero); |
874 |
|
|
assertNull(e4); |
875 |
|
|
|
876 |
|
|
} |
877 |
|
|
|
878 |
|
|
/** |
879 |
|
|
* ceiling returns next element |
880 |
|
|
*/ |
881 |
|
|
public void testDescendingCeiling() { |
882 |
|
|
NavigableSet q = dset5(); |
883 |
|
|
Object e1 = q.ceiling(m3); |
884 |
|
|
assertEquals(m3, e1); |
885 |
|
|
|
886 |
|
|
Object e2 = q.ceiling(zero); |
887 |
|
|
assertEquals(m1, e2); |
888 |
|
|
|
889 |
|
|
Object e3 = q.ceiling(m5); |
890 |
|
|
assertEquals(m5, e3); |
891 |
|
|
|
892 |
|
|
Object e4 = q.ceiling(m6); |
893 |
|
|
assertNull(e4); |
894 |
|
|
|
895 |
|
|
} |
896 |
|
|
|
897 |
|
|
/** |
898 |
|
|
* toArray contains all elements |
899 |
|
|
*/ |
900 |
|
|
public void testDescendingToArray() { |
901 |
|
|
NavigableSet q = populatedSet(SIZE); |
902 |
jsr166 |
1.8 |
Object[] o = q.toArray(); |
903 |
dl |
1.3 |
Arrays.sort(o); |
904 |
jsr166 |
1.8 |
for (int i = 0; i < o.length; i++) |
905 |
|
|
assertEquals(o[i], q.pollFirst()); |
906 |
dl |
1.3 |
} |
907 |
|
|
|
908 |
|
|
/** |
909 |
|
|
* toArray(a) contains all elements |
910 |
|
|
*/ |
911 |
|
|
public void testDescendingToArray2() { |
912 |
|
|
NavigableSet q = populatedSet(SIZE); |
913 |
jsr166 |
1.8 |
Integer[] ints = new Integer[SIZE]; |
914 |
|
|
ints = (Integer[])q.toArray(ints); |
915 |
dl |
1.3 |
Arrays.sort(ints); |
916 |
jsr166 |
1.5 |
for (int i = 0; i < ints.length; i++) |
917 |
dl |
1.3 |
assertEquals(ints[i], q.pollFirst()); |
918 |
|
|
} |
919 |
jsr166 |
1.4 |
|
920 |
dl |
1.3 |
/** |
921 |
|
|
* iterator iterates through all elements |
922 |
|
|
*/ |
923 |
|
|
public void testDescendingIterator() { |
924 |
|
|
NavigableSet q = populatedSet(SIZE); |
925 |
|
|
int i = 0; |
926 |
jsr166 |
1.8 |
Iterator it = q.iterator(); |
927 |
jsr166 |
1.5 |
while (it.hasNext()) { |
928 |
dl |
1.3 |
assertTrue(q.contains(it.next())); |
929 |
|
|
++i; |
930 |
|
|
} |
931 |
|
|
assertEquals(i, SIZE); |
932 |
|
|
} |
933 |
|
|
|
934 |
|
|
/** |
935 |
|
|
* iterator of empty set has no elements |
936 |
|
|
*/ |
937 |
|
|
public void testDescendingEmptyIterator() { |
938 |
|
|
NavigableSet q = dset0(); |
939 |
|
|
int i = 0; |
940 |
jsr166 |
1.8 |
Iterator it = q.iterator(); |
941 |
jsr166 |
1.5 |
while (it.hasNext()) { |
942 |
dl |
1.3 |
assertTrue(q.contains(it.next())); |
943 |
|
|
++i; |
944 |
|
|
} |
945 |
|
|
assertEquals(i, 0); |
946 |
|
|
} |
947 |
|
|
|
948 |
|
|
/** |
949 |
|
|
* iterator.remove removes current element |
950 |
|
|
*/ |
951 |
|
|
public void testDescendingIteratorRemove () { |
952 |
|
|
final NavigableSet q = dset0(); |
953 |
|
|
q.add(new Integer(2)); |
954 |
|
|
q.add(new Integer(1)); |
955 |
|
|
q.add(new Integer(3)); |
956 |
|
|
|
957 |
|
|
Iterator it = q.iterator(); |
958 |
|
|
it.next(); |
959 |
|
|
it.remove(); |
960 |
|
|
|
961 |
|
|
it = q.iterator(); |
962 |
|
|
assertEquals(it.next(), new Integer(2)); |
963 |
|
|
assertEquals(it.next(), new Integer(3)); |
964 |
|
|
assertFalse(it.hasNext()); |
965 |
|
|
} |
966 |
|
|
|
967 |
|
|
|
968 |
|
|
/** |
969 |
|
|
* toString contains toStrings of elements |
970 |
|
|
*/ |
971 |
|
|
public void testDescendingToString() { |
972 |
|
|
NavigableSet q = populatedSet(SIZE); |
973 |
|
|
String s = q.toString(); |
974 |
|
|
for (int i = 0; i < SIZE; ++i) { |
975 |
|
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
976 |
|
|
} |
977 |
jsr166 |
1.4 |
} |
978 |
dl |
1.3 |
|
979 |
|
|
/** |
980 |
jsr166 |
1.4 |
* A deserialized serialized set has same elements |
981 |
dl |
1.3 |
*/ |
982 |
jsr166 |
1.7 |
public void testDescendingSerialization() throws Exception { |
983 |
dl |
1.3 |
NavigableSet q = populatedSet(SIZE); |
984 |
jsr166 |
1.7 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
985 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
986 |
|
|
out.writeObject(q); |
987 |
|
|
out.close(); |
988 |
|
|
|
989 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
990 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
991 |
|
|
NavigableSet r = (NavigableSet)in.readObject(); |
992 |
|
|
assertEquals(q.size(), r.size()); |
993 |
|
|
while (!q.isEmpty()) |
994 |
|
|
assertEquals(q.pollFirst(), r.pollFirst()); |
995 |
dl |
1.3 |
} |
996 |
|
|
|
997 |
|
|
/** |
998 |
|
|
* subSet returns set with keys in requested range |
999 |
|
|
*/ |
1000 |
|
|
public void testDescendingSubSetContents() { |
1001 |
|
|
NavigableSet set = dset5(); |
1002 |
|
|
SortedSet sm = set.subSet(m2, m4); |
1003 |
|
|
assertEquals(m2, sm.first()); |
1004 |
|
|
assertEquals(m3, sm.last()); |
1005 |
|
|
assertEquals(2, sm.size()); |
1006 |
|
|
assertFalse(sm.contains(m1)); |
1007 |
|
|
assertTrue(sm.contains(m2)); |
1008 |
|
|
assertTrue(sm.contains(m3)); |
1009 |
|
|
assertFalse(sm.contains(m4)); |
1010 |
|
|
assertFalse(sm.contains(m5)); |
1011 |
|
|
Iterator i = sm.iterator(); |
1012 |
|
|
Object k; |
1013 |
|
|
k = (Integer)(i.next()); |
1014 |
|
|
assertEquals(m2, k); |
1015 |
|
|
k = (Integer)(i.next()); |
1016 |
|
|
assertEquals(m3, k); |
1017 |
|
|
assertFalse(i.hasNext()); |
1018 |
|
|
Iterator j = sm.iterator(); |
1019 |
|
|
j.next(); |
1020 |
|
|
j.remove(); |
1021 |
|
|
assertFalse(set.contains(m2)); |
1022 |
|
|
assertEquals(4, set.size()); |
1023 |
|
|
assertEquals(1, sm.size()); |
1024 |
|
|
assertEquals(m3, sm.first()); |
1025 |
|
|
assertEquals(m3, sm.last()); |
1026 |
|
|
assertTrue(sm.remove(m3)); |
1027 |
|
|
assertTrue(sm.isEmpty()); |
1028 |
|
|
assertEquals(3, set.size()); |
1029 |
|
|
} |
1030 |
|
|
|
1031 |
|
|
public void testDescendingSubSetContents2() { |
1032 |
|
|
NavigableSet set = dset5(); |
1033 |
|
|
SortedSet sm = set.subSet(m2, m3); |
1034 |
|
|
assertEquals(1, sm.size()); |
1035 |
|
|
assertEquals(m2, sm.first()); |
1036 |
|
|
assertEquals(m2, sm.last()); |
1037 |
|
|
assertFalse(sm.contains(m1)); |
1038 |
|
|
assertTrue(sm.contains(m2)); |
1039 |
|
|
assertFalse(sm.contains(m3)); |
1040 |
|
|
assertFalse(sm.contains(m4)); |
1041 |
|
|
assertFalse(sm.contains(m5)); |
1042 |
|
|
Iterator i = sm.iterator(); |
1043 |
|
|
Object k; |
1044 |
|
|
k = (Integer)(i.next()); |
1045 |
|
|
assertEquals(m2, k); |
1046 |
|
|
assertFalse(i.hasNext()); |
1047 |
|
|
Iterator j = sm.iterator(); |
1048 |
|
|
j.next(); |
1049 |
|
|
j.remove(); |
1050 |
|
|
assertFalse(set.contains(m2)); |
1051 |
|
|
assertEquals(4, set.size()); |
1052 |
|
|
assertEquals(0, sm.size()); |
1053 |
|
|
assertTrue(sm.isEmpty()); |
1054 |
|
|
assertFalse(sm.remove(m3)); |
1055 |
|
|
assertEquals(4, set.size()); |
1056 |
|
|
} |
1057 |
|
|
|
1058 |
|
|
/** |
1059 |
|
|
* headSet returns set with keys in requested range |
1060 |
|
|
*/ |
1061 |
|
|
public void testDescendingHeadSetContents() { |
1062 |
|
|
NavigableSet set = dset5(); |
1063 |
|
|
SortedSet sm = set.headSet(m4); |
1064 |
|
|
assertTrue(sm.contains(m1)); |
1065 |
|
|
assertTrue(sm.contains(m2)); |
1066 |
|
|
assertTrue(sm.contains(m3)); |
1067 |
|
|
assertFalse(sm.contains(m4)); |
1068 |
|
|
assertFalse(sm.contains(m5)); |
1069 |
|
|
Iterator i = sm.iterator(); |
1070 |
|
|
Object k; |
1071 |
|
|
k = (Integer)(i.next()); |
1072 |
|
|
assertEquals(m1, k); |
1073 |
|
|
k = (Integer)(i.next()); |
1074 |
|
|
assertEquals(m2, k); |
1075 |
|
|
k = (Integer)(i.next()); |
1076 |
|
|
assertEquals(m3, k); |
1077 |
|
|
assertFalse(i.hasNext()); |
1078 |
|
|
sm.clear(); |
1079 |
|
|
assertTrue(sm.isEmpty()); |
1080 |
|
|
assertEquals(2, set.size()); |
1081 |
|
|
assertEquals(m4, set.first()); |
1082 |
|
|
} |
1083 |
|
|
|
1084 |
|
|
/** |
1085 |
|
|
* tailSet returns set with keys in requested range |
1086 |
|
|
*/ |
1087 |
|
|
public void testDescendingTailSetContents() { |
1088 |
|
|
NavigableSet set = dset5(); |
1089 |
|
|
SortedSet sm = set.tailSet(m2); |
1090 |
|
|
assertFalse(sm.contains(m1)); |
1091 |
|
|
assertTrue(sm.contains(m2)); |
1092 |
|
|
assertTrue(sm.contains(m3)); |
1093 |
|
|
assertTrue(sm.contains(m4)); |
1094 |
|
|
assertTrue(sm.contains(m5)); |
1095 |
|
|
Iterator i = sm.iterator(); |
1096 |
|
|
Object k; |
1097 |
|
|
k = (Integer)(i.next()); |
1098 |
|
|
assertEquals(m2, k); |
1099 |
|
|
k = (Integer)(i.next()); |
1100 |
|
|
assertEquals(m3, k); |
1101 |
|
|
k = (Integer)(i.next()); |
1102 |
|
|
assertEquals(m4, k); |
1103 |
|
|
k = (Integer)(i.next()); |
1104 |
|
|
assertEquals(m5, k); |
1105 |
|
|
assertFalse(i.hasNext()); |
1106 |
|
|
|
1107 |
|
|
SortedSet ssm = sm.tailSet(m4); |
1108 |
|
|
assertEquals(m4, ssm.first()); |
1109 |
|
|
assertEquals(m5, ssm.last()); |
1110 |
|
|
assertTrue(ssm.remove(m4)); |
1111 |
|
|
assertEquals(1, ssm.size()); |
1112 |
|
|
assertEquals(3, sm.size()); |
1113 |
|
|
assertEquals(4, set.size()); |
1114 |
|
|
} |
1115 |
|
|
|
1116 |
dl |
1.1 |
} |