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