28 |
|
} |
29 |
|
|
30 |
|
static class MyReverseComparator implements Comparator { |
31 |
+ |
@SuppressWarnings("unchecked") |
32 |
|
public int compare(Object x, Object y) { |
33 |
|
return ((Comparable)y).compareTo(x); |
34 |
|
} |
35 |
|
} |
36 |
|
|
37 |
|
/** |
37 |
– |
* The number of elements to place in collections, arrays, etc. |
38 |
– |
*/ |
39 |
– |
static final int SIZE = 20; |
40 |
– |
|
41 |
– |
/** |
38 |
|
* Returns a new set of given size containing consecutive |
39 |
< |
* Integers 0 ... n - 1. |
39 |
> |
* Items 0 ... n - 1. |
40 |
|
*/ |
41 |
< |
private static TreeSet<Integer> populatedSet(int n) { |
42 |
< |
TreeSet<Integer> q = new TreeSet<>(); |
41 |
> |
private static TreeSet<Item> populatedSet(int n) { |
42 |
> |
TreeSet<Item> q = new TreeSet<>(); |
43 |
|
assertTrue(q.isEmpty()); |
44 |
|
for (int i = n - 1; i >= 0; i -= 2) |
45 |
< |
assertTrue(q.add(new Integer(i))); |
45 |
> |
mustAdd(q, i); |
46 |
|
for (int i = (n & 1); i < n; i += 2) |
47 |
< |
assertTrue(q.add(new Integer(i))); |
47 |
> |
mustAdd(q, i); |
48 |
|
assertFalse(q.isEmpty()); |
49 |
< |
assertEquals(n, q.size()); |
49 |
> |
mustEqual(n, q.size()); |
50 |
|
return q; |
51 |
|
} |
52 |
|
|
53 |
|
/** |
54 |
|
* Returns a new set of first 5 ints. |
55 |
|
*/ |
56 |
< |
private static TreeSet set5() { |
57 |
< |
TreeSet q = new TreeSet(); |
56 |
> |
private static TreeSet<Item> set5() { |
57 |
> |
TreeSet<Item> q = new TreeSet<>(); |
58 |
|
assertTrue(q.isEmpty()); |
59 |
|
q.add(one); |
60 |
|
q.add(two); |
61 |
|
q.add(three); |
62 |
|
q.add(four); |
63 |
|
q.add(five); |
64 |
< |
assertEquals(5, q.size()); |
64 |
> |
mustEqual(5, q.size()); |
65 |
|
return q; |
66 |
|
} |
67 |
|
|
69 |
|
* A new set has unbounded capacity |
70 |
|
*/ |
71 |
|
public void testConstructor1() { |
72 |
< |
assertEquals(0, new TreeSet().size()); |
72 |
> |
mustEqual(0, new TreeSet<Item>().size()); |
73 |
|
} |
74 |
|
|
75 |
|
/** |
77 |
|
*/ |
78 |
|
public void testConstructor3() { |
79 |
|
try { |
80 |
< |
new TreeSet((Collection)null); |
80 |
> |
new TreeSet<Item>((Collection<Item>)null); |
81 |
|
shouldThrow(); |
82 |
|
} catch (NullPointerException success) {} |
83 |
|
} |
87 |
|
*/ |
88 |
|
public void testConstructor4() { |
89 |
|
try { |
90 |
< |
new TreeSet(Arrays.asList(new Integer[SIZE])); |
90 |
> |
new TreeSet<Item>(Arrays.asList(new Item[SIZE])); |
91 |
|
shouldThrow(); |
92 |
|
} catch (NullPointerException success) {} |
93 |
|
} |
96 |
|
* Initializing from Collection with some null elements throws NPE |
97 |
|
*/ |
98 |
|
public void testConstructor5() { |
99 |
< |
Integer[] ints = new Integer[SIZE]; |
100 |
< |
for (int i = 0; i < SIZE - 1; ++i) |
105 |
< |
ints[i] = new Integer(i); |
99 |
> |
Item[] items = new Item[2]; |
100 |
> |
items[1] = zero; |
101 |
|
try { |
102 |
< |
new TreeSet(Arrays.asList(ints)); |
102 |
> |
new TreeSet<Item>(Arrays.asList(items)); |
103 |
|
shouldThrow(); |
104 |
|
} catch (NullPointerException success) {} |
105 |
|
} |
108 |
|
* Set contains all elements of collection used to initialize |
109 |
|
*/ |
110 |
|
public void testConstructor6() { |
111 |
< |
Integer[] ints = new Integer[SIZE]; |
111 |
> |
Item[] items = defaultItems; |
112 |
> |
TreeSet<Item> q = new TreeSet<>(Arrays.asList(items)); |
113 |
|
for (int i = 0; i < SIZE; ++i) |
114 |
< |
ints[i] = new Integer(i); |
119 |
< |
TreeSet q = new TreeSet(Arrays.asList(ints)); |
120 |
< |
for (int i = 0; i < SIZE; ++i) |
121 |
< |
assertEquals(ints[i], q.pollFirst()); |
114 |
> |
mustEqual(items[i], q.pollFirst()); |
115 |
|
} |
116 |
|
|
117 |
|
/** |
119 |
|
*/ |
120 |
|
public void testConstructor7() { |
121 |
|
MyReverseComparator cmp = new MyReverseComparator(); |
122 |
< |
TreeSet q = new TreeSet(cmp); |
123 |
< |
assertEquals(cmp, q.comparator()); |
124 |
< |
Integer[] ints = new Integer[SIZE]; |
125 |
< |
for (int i = 0; i < SIZE; ++i) |
126 |
< |
ints[i] = new Integer(i); |
134 |
< |
q.addAll(Arrays.asList(ints)); |
122 |
> |
@SuppressWarnings("unchecked") |
123 |
> |
TreeSet<Item> q = new TreeSet<>(cmp); |
124 |
> |
mustEqual(cmp, q.comparator()); |
125 |
> |
Item[] items = defaultItems; |
126 |
> |
q.addAll(Arrays.asList(items)); |
127 |
|
for (int i = SIZE - 1; i >= 0; --i) |
128 |
< |
assertEquals(ints[i], q.pollFirst()); |
128 |
> |
mustEqual(items[i], q.pollFirst()); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
|
* isEmpty is true before add, false after |
133 |
|
*/ |
134 |
|
public void testEmpty() { |
135 |
< |
TreeSet q = new TreeSet(); |
135 |
> |
TreeSet<Item> q = new TreeSet<>(); |
136 |
|
assertTrue(q.isEmpty()); |
137 |
< |
q.add(new Integer(1)); |
137 |
> |
q.add(one); |
138 |
|
assertFalse(q.isEmpty()); |
139 |
< |
q.add(new Integer(2)); |
139 |
> |
q.add(two); |
140 |
|
q.pollFirst(); |
141 |
|
q.pollFirst(); |
142 |
|
assertTrue(q.isEmpty()); |
146 |
|
* size changes when elements added and removed |
147 |
|
*/ |
148 |
|
public void testSize() { |
149 |
< |
TreeSet q = populatedSet(SIZE); |
149 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
150 |
|
for (int i = 0; i < SIZE; ++i) { |
151 |
< |
assertEquals(SIZE - i, q.size()); |
151 |
> |
mustEqual(SIZE - i, q.size()); |
152 |
|
q.pollFirst(); |
153 |
|
} |
154 |
|
for (int i = 0; i < SIZE; ++i) { |
155 |
< |
assertEquals(i, q.size()); |
156 |
< |
q.add(new Integer(i)); |
155 |
> |
mustEqual(i, q.size()); |
156 |
> |
mustAdd(q, i); |
157 |
|
} |
158 |
|
} |
159 |
|
|
161 |
|
* add(null) throws NPE if nonempty |
162 |
|
*/ |
163 |
|
public void testAddNull() { |
164 |
< |
TreeSet q = populatedSet(SIZE); |
164 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
165 |
|
try { |
166 |
|
q.add(null); |
167 |
|
shouldThrow(); |
172 |
|
* Add of comparable element succeeds |
173 |
|
*/ |
174 |
|
public void testAdd() { |
175 |
< |
TreeSet q = new TreeSet(); |
175 |
> |
TreeSet<Item> q = new TreeSet<>(); |
176 |
|
assertTrue(q.add(zero)); |
177 |
|
assertTrue(q.add(one)); |
178 |
|
} |
181 |
|
* Add of duplicate element fails |
182 |
|
*/ |
183 |
|
public void testAddDup() { |
184 |
< |
TreeSet q = new TreeSet(); |
184 |
> |
TreeSet<Item> q = new TreeSet<>(); |
185 |
|
assertTrue(q.add(zero)); |
186 |
|
assertFalse(q.add(zero)); |
187 |
|
} |
190 |
|
* Add of non-Comparable throws CCE |
191 |
|
*/ |
192 |
|
public void testAddNonComparable() { |
193 |
< |
TreeSet q = new TreeSet(); |
193 |
> |
TreeSet<Object> q = new TreeSet<>(); |
194 |
|
try { |
195 |
|
q.add(new Object()); |
196 |
|
q.add(new Object()); |
202 |
|
* addAll(null) throws NPE |
203 |
|
*/ |
204 |
|
public void testAddAll1() { |
205 |
< |
TreeSet q = new TreeSet(); |
205 |
> |
TreeSet<Item> q = new TreeSet<>(); |
206 |
|
try { |
207 |
|
q.addAll(null); |
208 |
|
shouldThrow(); |
213 |
|
* addAll of a collection with null elements throws NPE |
214 |
|
*/ |
215 |
|
public void testAddAll2() { |
216 |
< |
TreeSet q = new TreeSet(); |
217 |
< |
Integer[] ints = new Integer[SIZE]; |
216 |
> |
TreeSet<Item> q = new TreeSet<>(); |
217 |
> |
Item[] items = new Item[2]; |
218 |
|
try { |
219 |
< |
q.addAll(Arrays.asList(ints)); |
219 |
> |
q.addAll(Arrays.asList(items)); |
220 |
|
shouldThrow(); |
221 |
|
} catch (NullPointerException success) {} |
222 |
|
} |
226 |
|
* possibly adding some elements |
227 |
|
*/ |
228 |
|
public void testAddAll3() { |
229 |
< |
TreeSet q = new TreeSet(); |
230 |
< |
Integer[] ints = new Integer[SIZE]; |
231 |
< |
for (int i = 0; i < SIZE - 1; ++i) |
240 |
< |
ints[i] = new Integer(i); |
229 |
> |
TreeSet<Item> q = new TreeSet<>(); |
230 |
> |
Item[] items = new Item[2]; |
231 |
> |
items[0] = zero; |
232 |
|
try { |
233 |
< |
q.addAll(Arrays.asList(ints)); |
233 |
> |
q.addAll(Arrays.asList(items)); |
234 |
|
shouldThrow(); |
235 |
|
} catch (NullPointerException success) {} |
236 |
|
} |
239 |
|
* Set contains all elements of successful addAll |
240 |
|
*/ |
241 |
|
public void testAddAll5() { |
242 |
< |
Integer[] empty = new Integer[0]; |
243 |
< |
Integer[] ints = new Integer[SIZE]; |
244 |
< |
for (int i = 0; i < SIZE; ++i) |
254 |
< |
ints[i] = new Integer(SIZE - 1 - i); |
255 |
< |
TreeSet q = new TreeSet(); |
242 |
> |
Item[] empty = new Item[0]; |
243 |
> |
Item[] items = defaultItems; |
244 |
> |
TreeSet<Item> q = new TreeSet<>(); |
245 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
246 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
246 |
> |
assertTrue(q.addAll(Arrays.asList(items))); |
247 |
|
for (int i = 0; i < SIZE; ++i) |
248 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
248 |
> |
mustEqual(i, q.pollFirst()); |
249 |
|
} |
250 |
|
|
251 |
|
/** |
252 |
|
* pollFirst succeeds unless empty |
253 |
|
*/ |
254 |
|
public void testPollFirst() { |
255 |
< |
TreeSet q = populatedSet(SIZE); |
255 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
256 |
|
for (int i = 0; i < SIZE; ++i) { |
257 |
< |
assertEquals(i, q.pollFirst()); |
257 |
> |
mustEqual(i, q.pollFirst()); |
258 |
|
} |
259 |
|
assertNull(q.pollFirst()); |
260 |
|
} |
263 |
|
* pollLast succeeds unless empty |
264 |
|
*/ |
265 |
|
public void testPollLast() { |
266 |
< |
TreeSet q = populatedSet(SIZE); |
266 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
267 |
|
for (int i = SIZE - 1; i >= 0; --i) { |
268 |
< |
assertEquals(i, q.pollLast()); |
268 |
> |
mustEqual(i, q.pollLast()); |
269 |
|
} |
270 |
|
assertNull(q.pollFirst()); |
271 |
|
} |
274 |
|
* remove(x) removes x and returns true if present |
275 |
|
*/ |
276 |
|
public void testRemoveElement() { |
277 |
< |
TreeSet q = populatedSet(SIZE); |
277 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
278 |
|
for (int i = 1; i < SIZE; i += 2) { |
279 |
< |
assertTrue(q.contains(i)); |
280 |
< |
assertTrue(q.remove(i)); |
281 |
< |
assertFalse(q.contains(i)); |
282 |
< |
assertTrue(q.contains(i - 1)); |
279 |
> |
mustContain(q, i); |
280 |
> |
mustRemove(q, i); |
281 |
> |
mustNotContain(q, i); |
282 |
> |
mustContain(q, i - 1); |
283 |
|
} |
284 |
|
for (int i = 0; i < SIZE; i += 2) { |
285 |
< |
assertTrue(q.contains(i)); |
286 |
< |
assertTrue(q.remove(i)); |
287 |
< |
assertFalse(q.contains(i)); |
288 |
< |
assertFalse(q.remove(i + 1)); |
289 |
< |
assertFalse(q.contains(i + 1)); |
285 |
> |
mustContain(q, i); |
286 |
> |
mustRemove(q, i); |
287 |
> |
mustNotContain(q, i); |
288 |
> |
mustNotRemove(q, i + 1); |
289 |
> |
mustNotContain(q, i + 1); |
290 |
|
} |
291 |
|
assertTrue(q.isEmpty()); |
292 |
|
} |
295 |
|
* contains(x) reports true when elements added but not yet removed |
296 |
|
*/ |
297 |
|
public void testContains() { |
298 |
< |
TreeSet q = populatedSet(SIZE); |
298 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
299 |
|
for (int i = 0; i < SIZE; ++i) { |
300 |
< |
assertTrue(q.contains(new Integer(i))); |
300 |
> |
mustContain(q, i); |
301 |
|
q.pollFirst(); |
302 |
< |
assertFalse(q.contains(new Integer(i))); |
302 |
> |
mustNotContain(q, i); |
303 |
|
} |
304 |
|
} |
305 |
|
|
307 |
|
* clear removes all elements |
308 |
|
*/ |
309 |
|
public void testClear() { |
310 |
< |
TreeSet q = populatedSet(SIZE); |
310 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
311 |
|
q.clear(); |
312 |
|
assertTrue(q.isEmpty()); |
313 |
< |
assertEquals(0, q.size()); |
314 |
< |
q.add(new Integer(1)); |
313 |
> |
mustEqual(0, q.size()); |
314 |
> |
q.add(one); |
315 |
|
assertFalse(q.isEmpty()); |
316 |
|
q.clear(); |
317 |
|
assertTrue(q.isEmpty()); |
321 |
|
* containsAll(c) is true when c contains a subset of elements |
322 |
|
*/ |
323 |
|
public void testContainsAll() { |
324 |
< |
TreeSet q = populatedSet(SIZE); |
325 |
< |
TreeSet p = new TreeSet(); |
324 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
325 |
> |
TreeSet<Item> p = new TreeSet<>(); |
326 |
|
for (int i = 0; i < SIZE; ++i) { |
327 |
|
assertTrue(q.containsAll(p)); |
328 |
|
assertFalse(p.containsAll(q)); |
329 |
< |
p.add(new Integer(i)); |
329 |
> |
mustAdd(p, i); |
330 |
|
} |
331 |
|
assertTrue(p.containsAll(q)); |
332 |
|
} |
335 |
|
* retainAll(c) retains only those elements of c and reports true if changed |
336 |
|
*/ |
337 |
|
public void testRetainAll() { |
338 |
< |
TreeSet q = populatedSet(SIZE); |
339 |
< |
TreeSet p = populatedSet(SIZE); |
338 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
339 |
> |
TreeSet<Item> p = populatedSet(SIZE); |
340 |
|
for (int i = 0; i < SIZE; ++i) { |
341 |
|
boolean changed = q.retainAll(p); |
342 |
|
if (i == 0) |
345 |
|
assertTrue(changed); |
346 |
|
|
347 |
|
assertTrue(q.containsAll(p)); |
348 |
< |
assertEquals(SIZE - i, q.size()); |
348 |
> |
mustEqual(SIZE - i, q.size()); |
349 |
|
p.pollFirst(); |
350 |
|
} |
351 |
|
} |
355 |
|
*/ |
356 |
|
public void testRemoveAll() { |
357 |
|
for (int i = 1; i < SIZE; ++i) { |
358 |
< |
TreeSet q = populatedSet(SIZE); |
359 |
< |
TreeSet p = populatedSet(i); |
358 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
359 |
> |
TreeSet<Item> p = populatedSet(i); |
360 |
|
assertTrue(q.removeAll(p)); |
361 |
< |
assertEquals(SIZE - i, q.size()); |
361 |
> |
mustEqual(SIZE - i, q.size()); |
362 |
|
for (int j = 0; j < i; ++j) { |
363 |
< |
Integer x = (Integer)(p.pollFirst()); |
375 |
< |
assertFalse(q.contains(x)); |
363 |
> |
mustNotContain(q, p.pollFirst()); |
364 |
|
} |
365 |
|
} |
366 |
|
} |
369 |
|
* lower returns preceding element |
370 |
|
*/ |
371 |
|
public void testLower() { |
372 |
< |
TreeSet q = set5(); |
372 |
> |
TreeSet<Item> q = set5(); |
373 |
|
Object e1 = q.lower(three); |
374 |
< |
assertEquals(two, e1); |
374 |
> |
mustEqual(two, e1); |
375 |
|
|
376 |
|
Object e2 = q.lower(six); |
377 |
< |
assertEquals(five, e2); |
377 |
> |
mustEqual(five, e2); |
378 |
|
|
379 |
|
Object e3 = q.lower(one); |
380 |
|
assertNull(e3); |
387 |
|
* higher returns next element |
388 |
|
*/ |
389 |
|
public void testHigher() { |
390 |
< |
TreeSet q = set5(); |
390 |
> |
TreeSet<Item> q = set5(); |
391 |
|
Object e1 = q.higher(three); |
392 |
< |
assertEquals(four, e1); |
392 |
> |
mustEqual(four, e1); |
393 |
|
|
394 |
|
Object e2 = q.higher(zero); |
395 |
< |
assertEquals(one, e2); |
395 |
> |
mustEqual(one, e2); |
396 |
|
|
397 |
|
Object e3 = q.higher(five); |
398 |
|
assertNull(e3); |
405 |
|
* floor returns preceding element |
406 |
|
*/ |
407 |
|
public void testFloor() { |
408 |
< |
TreeSet q = set5(); |
408 |
> |
TreeSet<Item> q = set5(); |
409 |
|
Object e1 = q.floor(three); |
410 |
< |
assertEquals(three, e1); |
410 |
> |
mustEqual(three, e1); |
411 |
|
|
412 |
|
Object e2 = q.floor(six); |
413 |
< |
assertEquals(five, e2); |
413 |
> |
mustEqual(five, e2); |
414 |
|
|
415 |
|
Object e3 = q.floor(one); |
416 |
< |
assertEquals(one, e3); |
416 |
> |
mustEqual(one, e3); |
417 |
|
|
418 |
|
Object e4 = q.floor(zero); |
419 |
|
assertNull(e4); |
423 |
|
* ceiling returns next element |
424 |
|
*/ |
425 |
|
public void testCeiling() { |
426 |
< |
TreeSet q = set5(); |
426 |
> |
TreeSet<Item> q = set5(); |
427 |
|
Object e1 = q.ceiling(three); |
428 |
< |
assertEquals(three, e1); |
428 |
> |
mustEqual(three, e1); |
429 |
|
|
430 |
|
Object e2 = q.ceiling(zero); |
431 |
< |
assertEquals(one, e2); |
431 |
> |
mustEqual(one, e2); |
432 |
|
|
433 |
|
Object e3 = q.ceiling(five); |
434 |
< |
assertEquals(five, e3); |
434 |
> |
mustEqual(five, e3); |
435 |
|
|
436 |
|
Object e4 = q.ceiling(six); |
437 |
|
assertNull(e4); |
441 |
|
* toArray contains all elements in sorted order |
442 |
|
*/ |
443 |
|
public void testToArray() { |
444 |
< |
TreeSet q = populatedSet(SIZE); |
444 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
445 |
|
Object[] a = q.toArray(); |
446 |
|
assertSame(Object[].class, a.getClass()); |
447 |
|
for (Object o : a) |
453 |
|
* toArray(a) contains all elements in sorted order |
454 |
|
*/ |
455 |
|
public void testToArray2() { |
456 |
< |
TreeSet<Integer> q = populatedSet(SIZE); |
457 |
< |
Integer[] ints = new Integer[SIZE]; |
458 |
< |
Integer[] array = q.toArray(ints); |
459 |
< |
assertSame(ints, array); |
460 |
< |
for (Integer o : ints) |
456 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
457 |
> |
Item[] items = new Item[SIZE]; |
458 |
> |
Item[] array = q.toArray(items); |
459 |
> |
assertSame(items, array); |
460 |
> |
for (Item o : items) |
461 |
|
assertSame(o, q.pollFirst()); |
462 |
|
assertTrue(q.isEmpty()); |
463 |
|
} |
466 |
|
* iterator iterates through all elements |
467 |
|
*/ |
468 |
|
public void testIterator() { |
469 |
< |
TreeSet q = populatedSet(SIZE); |
470 |
< |
Iterator it = q.iterator(); |
469 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
470 |
> |
Iterator<? extends Item> it = q.iterator(); |
471 |
|
int i; |
472 |
|
for (i = 0; it.hasNext(); i++) |
473 |
|
assertTrue(q.contains(it.next())); |
474 |
< |
assertEquals(i, SIZE); |
474 |
> |
mustEqual(i, SIZE); |
475 |
|
assertIteratorExhausted(it); |
476 |
|
} |
477 |
|
|
479 |
|
* iterator of empty set has no elements |
480 |
|
*/ |
481 |
|
public void testEmptyIterator() { |
482 |
< |
assertIteratorExhausted(new TreeSet().iterator()); |
482 |
> |
assertIteratorExhausted(new TreeSet<Item>().iterator()); |
483 |
|
} |
484 |
|
|
485 |
|
/** |
486 |
|
* iterator.remove removes current element |
487 |
|
*/ |
488 |
|
public void testIteratorRemove() { |
489 |
< |
final TreeSet q = new TreeSet(); |
490 |
< |
q.add(new Integer(2)); |
491 |
< |
q.add(new Integer(1)); |
492 |
< |
q.add(new Integer(3)); |
489 |
> |
final TreeSet<Item> q = new TreeSet<>(); |
490 |
> |
q.add(two); |
491 |
> |
q.add(one); |
492 |
> |
q.add(three); |
493 |
|
|
494 |
< |
Iterator it = q.iterator(); |
494 |
> |
Iterator<? extends Item> it = q.iterator(); |
495 |
|
it.next(); |
496 |
|
it.remove(); |
497 |
|
|
498 |
|
it = q.iterator(); |
499 |
< |
assertEquals(it.next(), new Integer(2)); |
500 |
< |
assertEquals(it.next(), new Integer(3)); |
499 |
> |
mustEqual(it.next(), two); |
500 |
> |
mustEqual(it.next(), three); |
501 |
|
assertFalse(it.hasNext()); |
502 |
|
} |
503 |
|
|
505 |
|
* toString contains toStrings of elements |
506 |
|
*/ |
507 |
|
public void testToString() { |
508 |
< |
TreeSet q = populatedSet(SIZE); |
508 |
> |
TreeSet<Item> q = populatedSet(SIZE); |
509 |
|
String s = q.toString(); |
510 |
|
for (int i = 0; i < SIZE; ++i) { |
511 |
|
assertTrue(s.contains(String.valueOf(i))); |
516 |
|
* A deserialized/reserialized set equals original |
517 |
|
*/ |
518 |
|
public void testSerialization() throws Exception { |
519 |
< |
NavigableSet x = populatedSet(SIZE); |
520 |
< |
NavigableSet y = serialClone(x); |
519 |
> |
NavigableSet<Item> x = populatedSet(SIZE); |
520 |
> |
NavigableSet<Item> y = serialClone(x); |
521 |
|
|
522 |
|
assertNotSame(x, y); |
523 |
< |
assertEquals(x.size(), y.size()); |
524 |
< |
assertEquals(x, y); |
525 |
< |
assertEquals(y, x); |
523 |
> |
mustEqual(x.size(), y.size()); |
524 |
> |
mustEqual(x, y); |
525 |
> |
mustEqual(y, x); |
526 |
|
while (!x.isEmpty()) { |
527 |
|
assertFalse(y.isEmpty()); |
528 |
< |
assertEquals(x.pollFirst(), y.pollFirst()); |
528 |
> |
mustEqual(x.pollFirst(), y.pollFirst()); |
529 |
|
} |
530 |
|
assertTrue(y.isEmpty()); |
531 |
|
} |
534 |
|
* subSet returns set with keys in requested range |
535 |
|
*/ |
536 |
|
public void testSubSetContents() { |
537 |
< |
TreeSet set = set5(); |
538 |
< |
SortedSet sm = set.subSet(two, four); |
539 |
< |
assertEquals(two, sm.first()); |
540 |
< |
assertEquals(three, sm.last()); |
541 |
< |
assertEquals(2, sm.size()); |
542 |
< |
assertFalse(sm.contains(one)); |
543 |
< |
assertTrue(sm.contains(two)); |
544 |
< |
assertTrue(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 |
< |
k = (Integer)(i.next()); |
564 |
< |
assertEquals(three, k); |
537 |
> |
TreeSet<Item> set = set5(); |
538 |
> |
SortedSet<Item> sm = set.subSet(two, four); |
539 |
> |
mustEqual(two, sm.first()); |
540 |
> |
mustEqual(three, sm.last()); |
541 |
> |
mustEqual(2, sm.size()); |
542 |
> |
mustNotContain(sm, one); |
543 |
> |
mustContain(sm, two); |
544 |
> |
mustContain(sm, three); |
545 |
> |
mustNotContain(sm, four); |
546 |
> |
mustNotContain(sm, five); |
547 |
> |
Iterator<? extends Item> i = sm.iterator(); |
548 |
> |
Item k = i.next(); |
549 |
> |
mustEqual(two, k); |
550 |
> |
k = i.next(); |
551 |
> |
mustEqual(three, k); |
552 |
|
assertFalse(i.hasNext()); |
553 |
< |
Iterator j = sm.iterator(); |
553 |
> |
Iterator<? extends Item> j = sm.iterator(); |
554 |
|
j.next(); |
555 |
|
j.remove(); |
556 |
< |
assertFalse(set.contains(two)); |
557 |
< |
assertEquals(4, set.size()); |
558 |
< |
assertEquals(1, sm.size()); |
559 |
< |
assertEquals(three, sm.first()); |
560 |
< |
assertEquals(three, sm.last()); |
561 |
< |
assertTrue(sm.remove(three)); |
556 |
> |
mustNotContain(set, two); |
557 |
> |
mustEqual(4, set.size()); |
558 |
> |
mustEqual(1, sm.size()); |
559 |
> |
mustEqual(three, sm.first()); |
560 |
> |
mustEqual(three, sm.last()); |
561 |
> |
mustRemove(sm, three); |
562 |
|
assertTrue(sm.isEmpty()); |
563 |
< |
assertEquals(3, set.size()); |
563 |
> |
mustEqual(3, set.size()); |
564 |
|
} |
565 |
|
|
566 |
|
public void testSubSetContents2() { |
567 |
< |
TreeSet set = set5(); |
568 |
< |
SortedSet sm = set.subSet(two, three); |
569 |
< |
assertEquals(1, sm.size()); |
570 |
< |
assertEquals(two, sm.first()); |
571 |
< |
assertEquals(two, sm.last()); |
572 |
< |
assertFalse(sm.contains(one)); |
573 |
< |
assertTrue(sm.contains(two)); |
574 |
< |
assertFalse(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()); |
593 |
< |
assertEquals(two, k); |
567 |
> |
TreeSet<Item> set = set5(); |
568 |
> |
SortedSet<Item> sm = set.subSet(two, three); |
569 |
> |
mustEqual(1, sm.size()); |
570 |
> |
mustEqual(two, sm.first()); |
571 |
> |
mustEqual(two, sm.last()); |
572 |
> |
mustNotContain(sm, one); |
573 |
> |
mustContain(sm, two); |
574 |
> |
mustNotContain(sm, three); |
575 |
> |
mustNotContain(sm, four); |
576 |
> |
mustNotContain(sm, five); |
577 |
> |
Iterator<? extends Item> i = sm.iterator(); |
578 |
> |
Item k = i.next(); |
579 |
> |
mustEqual(two, k); |
580 |
|
assertFalse(i.hasNext()); |
581 |
< |
Iterator j = sm.iterator(); |
581 |
> |
Iterator<? extends Item> j = sm.iterator(); |
582 |
|
j.next(); |
583 |
|
j.remove(); |
584 |
< |
assertFalse(set.contains(two)); |
585 |
< |
assertEquals(4, set.size()); |
586 |
< |
assertEquals(0, sm.size()); |
584 |
> |
mustNotContain(set, two); |
585 |
> |
mustEqual(4, set.size()); |
586 |
> |
mustEqual(0, sm.size()); |
587 |
|
assertTrue(sm.isEmpty()); |
588 |
< |
assertFalse(sm.remove(three)); |
589 |
< |
assertEquals(4, set.size()); |
588 |
> |
mustNotRemove(sm, three); |
589 |
> |
mustEqual(4, set.size()); |
590 |
|
} |
591 |
|
|
592 |
|
/** |
593 |
|
* headSet returns set with keys in requested range |
594 |
|
*/ |
595 |
|
public void testHeadSetContents() { |
596 |
< |
TreeSet set = set5(); |
597 |
< |
SortedSet sm = set.headSet(four); |
598 |
< |
assertTrue(sm.contains(one)); |
599 |
< |
assertTrue(sm.contains(two)); |
600 |
< |
assertTrue(sm.contains(three)); |
601 |
< |
assertFalse(sm.contains(four)); |
602 |
< |
assertFalse(sm.contains(five)); |
603 |
< |
Iterator i = sm.iterator(); |
604 |
< |
Object k; |
605 |
< |
k = (Integer)(i.next()); |
606 |
< |
assertEquals(one, k); |
607 |
< |
k = (Integer)(i.next()); |
608 |
< |
assertEquals(two, k); |
609 |
< |
k = (Integer)(i.next()); |
624 |
< |
assertEquals(three, k); |
596 |
> |
TreeSet<Item> set = set5(); |
597 |
> |
SortedSet<Item> sm = set.headSet(four); |
598 |
> |
mustContain(sm, one); |
599 |
> |
mustContain(sm, two); |
600 |
> |
mustContain(sm, three); |
601 |
> |
mustNotContain(sm, four); |
602 |
> |
mustNotContain(sm, five); |
603 |
> |
Iterator<? extends Item> i = sm.iterator(); |
604 |
> |
Item k = i.next(); |
605 |
> |
mustEqual(one, k); |
606 |
> |
k = i.next(); |
607 |
> |
mustEqual(two, k); |
608 |
> |
k = i.next(); |
609 |
> |
mustEqual(three, k); |
610 |
|
assertFalse(i.hasNext()); |
611 |
|
sm.clear(); |
612 |
|
assertTrue(sm.isEmpty()); |
613 |
< |
assertEquals(2, set.size()); |
614 |
< |
assertEquals(four, set.first()); |
613 |
> |
mustEqual(2, set.size()); |
614 |
> |
mustEqual(four, set.first()); |
615 |
|
} |
616 |
|
|
617 |
|
/** |
618 |
|
* tailSet returns set with keys in requested range |
619 |
|
*/ |
620 |
|
public void testTailSetContents() { |
621 |
< |
TreeSet set = set5(); |
622 |
< |
SortedSet sm = set.tailSet(two); |
623 |
< |
assertFalse(sm.contains(one)); |
624 |
< |
assertTrue(sm.contains(two)); |
625 |
< |
assertTrue(sm.contains(three)); |
626 |
< |
assertTrue(sm.contains(four)); |
627 |
< |
assertTrue(sm.contains(five)); |
628 |
< |
Iterator i = sm.iterator(); |
629 |
< |
Object k; |
630 |
< |
k = (Integer)(i.next()); |
631 |
< |
assertEquals(two, k); |
632 |
< |
k = (Integer)(i.next()); |
633 |
< |
assertEquals(three, k); |
634 |
< |
k = (Integer)(i.next()); |
635 |
< |
assertEquals(four, k); |
636 |
< |
k = (Integer)(i.next()); |
652 |
< |
assertEquals(five, k); |
621 |
> |
TreeSet<Item> set = set5(); |
622 |
> |
SortedSet<Item> sm = set.tailSet(two); |
623 |
> |
mustNotContain(sm, one); |
624 |
> |
mustContain(sm, two); |
625 |
> |
mustContain(sm, three); |
626 |
> |
mustContain(sm, four); |
627 |
> |
mustContain(sm, five); |
628 |
> |
Iterator<? extends Item> i = sm.iterator(); |
629 |
> |
Item k = i.next(); |
630 |
> |
mustEqual(two, k); |
631 |
> |
k = i.next(); |
632 |
> |
mustEqual(three, k); |
633 |
> |
k = i.next(); |
634 |
> |
mustEqual(four, k); |
635 |
> |
k = i.next(); |
636 |
> |
mustEqual(five, k); |
637 |
|
assertFalse(i.hasNext()); |
638 |
|
|
639 |
< |
SortedSet ssm = sm.tailSet(four); |
640 |
< |
assertEquals(four, ssm.first()); |
641 |
< |
assertEquals(five, ssm.last()); |
642 |
< |
assertTrue(ssm.remove(four)); |
643 |
< |
assertEquals(1, ssm.size()); |
644 |
< |
assertEquals(3, sm.size()); |
645 |
< |
assertEquals(4, set.size()); |
639 |
> |
SortedSet<Item> ssm = sm.tailSet(four); |
640 |
> |
mustEqual(four, ssm.first()); |
641 |
> |
mustEqual(five, ssm.last()); |
642 |
> |
mustRemove(ssm, four); |
643 |
> |
mustEqual(1, ssm.size()); |
644 |
> |
mustEqual(3, sm.size()); |
645 |
> |
mustEqual(4, set.size()); |
646 |
|
} |
647 |
|
|
648 |
|
Random rnd = new Random(666); |
653 |
|
*/ |
654 |
|
public void testRecursiveSubSets() throws Exception { |
655 |
|
int setSize = expensiveTests ? 1000 : 100; |
656 |
< |
Class cl = TreeSet.class; |
656 |
> |
Class<?> cl = TreeSet.class; |
657 |
|
|
658 |
< |
NavigableSet<Integer> set = newSet(cl); |
658 |
> |
NavigableSet<Item> set = newSet(cl); |
659 |
|
bs = new BitSet(setSize); |
660 |
|
|
661 |
|
populate(set, setSize); |
666 |
|
check(set, 0, setSize - 1, true); |
667 |
|
check(set.descendingSet(), 0, setSize - 1, false); |
668 |
|
|
669 |
< |
bashSubSet(set.subSet(0, true, setSize, false), |
669 |
> |
bashSubSet(set.subSet(zero, true, itemFor(setSize), false), |
670 |
|
0, setSize - 1, true); |
671 |
|
} |
672 |
|
|
674 |
|
* addAll is idempotent |
675 |
|
*/ |
676 |
|
public void testAddAll_idempotent() throws Exception { |
677 |
< |
Set x = populatedSet(SIZE); |
678 |
< |
Set y = new TreeSet(x); |
677 |
> |
Set<Item> x = populatedSet(SIZE); |
678 |
> |
Set<Item> y = new TreeSet<>(x); |
679 |
|
y.addAll(x); |
680 |
< |
assertEquals(x, y); |
681 |
< |
assertEquals(y, x); |
680 |
> |
mustEqual(x, y); |
681 |
> |
mustEqual(y, x); |
682 |
|
} |
683 |
|
|
684 |
< |
static NavigableSet<Integer> newSet(Class cl) throws Exception { |
685 |
< |
NavigableSet<Integer> result = |
686 |
< |
(NavigableSet<Integer>) cl.getConstructor().newInstance(); |
687 |
< |
assertEquals(0, result.size()); |
684 |
> |
static NavigableSet<Item> newSet(Class<?> cl) throws Exception { |
685 |
> |
@SuppressWarnings("unchecked") |
686 |
> |
NavigableSet<Item> result = |
687 |
> |
(NavigableSet<Item>) cl.getConstructor().newInstance(); |
688 |
> |
mustEqual(0, result.size()); |
689 |
|
assertFalse(result.iterator().hasNext()); |
690 |
|
return result; |
691 |
|
} |
692 |
|
|
693 |
< |
void populate(NavigableSet<Integer> set, int limit) { |
693 |
> |
void populate(NavigableSet<Item> set, int limit) { |
694 |
|
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
695 |
|
int element = rnd.nextInt(limit); |
696 |
|
put(set, element); |
697 |
|
} |
698 |
|
} |
699 |
|
|
700 |
< |
void mutateSet(NavigableSet<Integer> set, int min, int max) { |
700 |
> |
void mutateSet(NavigableSet<Item> set, int min, int max) { |
701 |
|
int size = set.size(); |
702 |
|
int rangeSize = max - min + 1; |
703 |
|
|
707 |
|
} |
708 |
|
|
709 |
|
// Remove a bunch of entries with iterator |
710 |
< |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
710 |
> |
for (Iterator<Item> it = set.iterator(); it.hasNext(); ) { |
711 |
|
if (rnd.nextBoolean()) { |
712 |
< |
bs.clear(it.next()); |
712 |
> |
bs.clear(it.next().value); |
713 |
|
it.remove(); |
714 |
|
} |
715 |
|
} |
722 |
|
} |
723 |
|
} |
724 |
|
|
725 |
< |
void mutateSubSet(NavigableSet<Integer> set, int min, int max) { |
725 |
> |
void mutateSubSet(NavigableSet<Item> set, int min, int max) { |
726 |
|
int size = set.size(); |
727 |
|
int rangeSize = max - min + 1; |
728 |
|
|
732 |
|
} |
733 |
|
|
734 |
|
// Remove a bunch of entries with iterator |
735 |
< |
for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) { |
735 |
> |
for (Iterator<Item> it = set.iterator(); it.hasNext(); ) { |
736 |
|
if (rnd.nextBoolean()) { |
737 |
< |
bs.clear(it.next()); |
737 |
> |
bs.clear(it.next().value); |
738 |
|
it.remove(); |
739 |
|
} |
740 |
|
} |
746 |
|
put(set, element); |
747 |
|
} else { |
748 |
|
try { |
749 |
< |
set.add(element); |
749 |
> |
set.add(itemFor(element)); |
750 |
|
shouldThrow(); |
751 |
|
} catch (IllegalArgumentException success) {} |
752 |
|
} |
753 |
|
} |
754 |
|
} |
755 |
|
|
756 |
< |
void put(NavigableSet<Integer> set, int element) { |
757 |
< |
if (set.add(element)) |
756 |
> |
void put(NavigableSet<Item> set, int element) { |
757 |
> |
if (set.add(itemFor(element))) |
758 |
|
bs.set(element); |
759 |
|
} |
760 |
|
|
761 |
< |
void remove(NavigableSet<Integer> set, int element) { |
762 |
< |
if (set.remove(element)) |
761 |
> |
void remove(NavigableSet<Item> set, int element) { |
762 |
> |
if (set.remove(itemFor(element))) |
763 |
|
bs.clear(element); |
764 |
|
} |
765 |
|
|
766 |
< |
void bashSubSet(NavigableSet<Integer> set, |
766 |
> |
void bashSubSet(NavigableSet<Item> set, |
767 |
|
int min, int max, boolean ascending) { |
768 |
|
check(set, min, max, ascending); |
769 |
|
check(set.descendingSet(), min, max, !ascending); |
779 |
|
|
780 |
|
// headSet - pick direction and endpoint inclusion randomly |
781 |
|
boolean incl = rnd.nextBoolean(); |
782 |
< |
NavigableSet<Integer> hm = set.headSet(midPoint, incl); |
782 |
> |
NavigableSet<Item> hm = set.headSet(itemFor(midPoint), incl); |
783 |
|
if (ascending) { |
784 |
|
if (rnd.nextBoolean()) |
785 |
|
bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true); |
796 |
|
|
797 |
|
// tailSet - pick direction and endpoint inclusion randomly |
798 |
|
incl = rnd.nextBoolean(); |
799 |
< |
NavigableSet<Integer> tm = set.tailSet(midPoint,incl); |
799 |
> |
NavigableSet<Item> tm = set.tailSet(itemFor(midPoint),incl); |
800 |
|
if (ascending) { |
801 |
|
if (rnd.nextBoolean()) |
802 |
|
bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true); |
821 |
|
boolean lowIncl = rnd.nextBoolean(); |
822 |
|
boolean highIncl = rnd.nextBoolean(); |
823 |
|
if (ascending) { |
824 |
< |
NavigableSet<Integer> sm = set.subSet( |
825 |
< |
endpoints[0], lowIncl, endpoints[1], highIncl); |
824 |
> |
NavigableSet<Item> sm = set.subSet( |
825 |
> |
itemFor(endpoints[0]), lowIncl, itemFor(endpoints[1]), highIncl); |
826 |
|
if (rnd.nextBoolean()) |
827 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
828 |
|
endpoints[1] - (highIncl ? 0 : 1), true); |
830 |
|
bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), |
831 |
|
endpoints[1] - (highIncl ? 0 : 1), false); |
832 |
|
} else { |
833 |
< |
NavigableSet<Integer> sm = set.subSet( |
834 |
< |
endpoints[1], highIncl, endpoints[0], lowIncl); |
833 |
> |
NavigableSet<Item> sm = set.subSet( |
834 |
> |
itemFor(endpoints[1]), highIncl, itemFor(endpoints[0]), lowIncl); |
835 |
|
if (rnd.nextBoolean()) |
836 |
|
bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), |
837 |
|
endpoints[1] - (highIncl ? 0 : 1), false); |
844 |
|
/** |
845 |
|
* min and max are both inclusive. If max < min, interval is empty. |
846 |
|
*/ |
847 |
< |
void check(NavigableSet<Integer> set, |
847 |
> |
void check(NavigableSet<Item> set, |
848 |
|
final int min, final int max, final boolean ascending) { |
849 |
|
class ReferenceSet { |
850 |
|
int lower(int element) { |
912 |
|
int size = 0; |
913 |
|
for (int i = min; i <= max; i++) { |
914 |
|
boolean bsContainsI = bs.get(i); |
915 |
< |
assertEquals(bsContainsI, set.contains(i)); |
915 |
> |
mustEqual(bsContainsI, set.contains(itemFor(i))); |
916 |
|
if (bsContainsI) |
917 |
|
size++; |
918 |
|
} |
919 |
< |
assertEquals(size, set.size()); |
919 |
> |
mustEqual(size, set.size()); |
920 |
|
|
921 |
|
// Test contents using contains elementSet iterator |
922 |
|
int size2 = 0; |
923 |
|
int previousElement = -1; |
924 |
< |
for (int element : set) { |
925 |
< |
assertTrue(bs.get(element)); |
924 |
> |
for (Item element : set) { |
925 |
> |
assertTrue(bs.get(element.value)); |
926 |
|
size2++; |
927 |
|
assertTrue(previousElement < 0 || (ascending ? |
928 |
< |
element - previousElement > 0 : element - previousElement < 0)); |
929 |
< |
previousElement = element; |
928 |
> |
element.value - previousElement > 0 : element.value - previousElement < 0)); |
929 |
> |
previousElement = element.value; |
930 |
|
} |
931 |
< |
assertEquals(size2, size); |
931 |
> |
mustEqual(size2, size); |
932 |
|
|
933 |
|
// Test navigation ops |
934 |
|
for (int element = min - 1; element <= max + 1; element++) { |
935 |
< |
assertEq(set.lower(element), rs.lower(element)); |
936 |
< |
assertEq(set.floor(element), rs.floor(element)); |
937 |
< |
assertEq(set.higher(element), rs.higher(element)); |
938 |
< |
assertEq(set.ceiling(element), rs.ceiling(element)); |
935 |
> |
Item e = itemFor(element); |
936 |
> |
assertEq(set.lower(e), rs.lower(element)); |
937 |
> |
assertEq(set.floor(e), rs.floor(element)); |
938 |
> |
assertEq(set.higher(e), rs.higher(element)); |
939 |
> |
assertEq(set.ceiling(e), rs.ceiling(element)); |
940 |
|
} |
941 |
|
|
942 |
|
// Test extrema |
944 |
|
assertEq(set.first(), rs.first()); |
945 |
|
assertEq(set.last(), rs.last()); |
946 |
|
} else { |
947 |
< |
assertEq(rs.first(), -1); |
948 |
< |
assertEq(rs.last(), -1); |
947 |
> |
mustEqual(rs.first(), -1); |
948 |
> |
mustEqual(rs.last(), -1); |
949 |
|
try { |
950 |
|
set.first(); |
951 |
|
shouldThrow(); |
957 |
|
} |
958 |
|
} |
959 |
|
|
960 |
< |
static void assertEq(Integer i, int j) { |
960 |
> |
static void assertEq(Item i, int j) { |
961 |
|
if (i == null) |
962 |
< |
assertEquals(j, -1); |
962 |
> |
mustEqual(j, -1); |
963 |
|
else |
964 |
< |
assertEquals((int) i, j); |
964 |
> |
mustEqual(i, j); |
965 |
|
} |
966 |
|
|
967 |
< |
static boolean eq(Integer i, int j) { |
968 |
< |
return (i == null) ? j == -1 : i == j; |
967 |
> |
static boolean eq(Item i, int j) { |
968 |
> |
return (i == null) ? j == -1 : i.value == j; |
969 |
|
} |
970 |
|
|
971 |
|
} |