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