11 |
|
|
12 |
|
public class ConcurrentSkipListMapTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
< |
return new TestSuite(ConcurrentSkipListMapTest.class); |
17 |
> |
return new TestSuite(ConcurrentSkipListMapTest.class); |
18 |
|
} |
19 |
|
|
20 |
|
/** |
21 |
|
* Create a map from Integers 1-5 to Strings "A"-"E". |
22 |
|
*/ |
23 |
< |
private static ConcurrentSkipListMap map5() { |
24 |
< |
ConcurrentSkipListMap map = new ConcurrentSkipListMap(); |
23 |
> |
private static ConcurrentSkipListMap map5() { |
24 |
> |
ConcurrentSkipListMap map = new ConcurrentSkipListMap(); |
25 |
|
assertTrue(map.isEmpty()); |
26 |
< |
map.put(one, "A"); |
27 |
< |
map.put(five, "E"); |
28 |
< |
map.put(three, "C"); |
29 |
< |
map.put(two, "B"); |
30 |
< |
map.put(four, "D"); |
26 |
> |
map.put(one, "A"); |
27 |
> |
map.put(five, "E"); |
28 |
> |
map.put(three, "C"); |
29 |
> |
map.put(two, "B"); |
30 |
> |
map.put(four, "D"); |
31 |
|
assertFalse(map.isEmpty()); |
32 |
|
assertEquals(5, map.size()); |
33 |
< |
return map; |
33 |
> |
return map; |
34 |
|
} |
35 |
|
|
36 |
|
/** |
38 |
|
*/ |
39 |
|
public void testClear() { |
40 |
|
ConcurrentSkipListMap map = map5(); |
41 |
< |
map.clear(); |
42 |
< |
assertEquals(map.size(), 0); |
41 |
> |
map.clear(); |
42 |
> |
assertEquals(map.size(), 0); |
43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
< |
* |
46 |
> |
* |
47 |
|
*/ |
48 |
|
public void testConstructFromSorted() { |
49 |
|
ConcurrentSkipListMap map = map5(); |
59 |
|
ConcurrentSkipListMap map2 = map5(); |
60 |
|
assertEquals(map1, map2); |
61 |
|
assertEquals(map2, map1); |
62 |
< |
map1.clear(); |
62 |
> |
map1.clear(); |
63 |
|
assertFalse(map1.equals(map2)); |
64 |
|
assertFalse(map2.equals(map1)); |
65 |
|
} |
69 |
|
*/ |
70 |
|
public void testContainsKey() { |
71 |
|
ConcurrentSkipListMap map = map5(); |
72 |
< |
assertTrue(map.containsKey(one)); |
72 |
> |
assertTrue(map.containsKey(one)); |
73 |
|
assertFalse(map.containsKey(zero)); |
74 |
|
} |
75 |
|
|
78 |
|
*/ |
79 |
|
public void testContainsValue() { |
80 |
|
ConcurrentSkipListMap map = map5(); |
81 |
< |
assertTrue(map.containsValue("A")); |
81 |
> |
assertTrue(map.containsValue("A")); |
82 |
|
assertFalse(map.containsValue("Z")); |
83 |
|
} |
84 |
|
|
88 |
|
*/ |
89 |
|
public void testGet() { |
90 |
|
ConcurrentSkipListMap map = map5(); |
91 |
< |
assertEquals("A", (String)map.get(one)); |
91 |
> |
assertEquals("A", (String)map.get(one)); |
92 |
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
93 |
|
assertNull(empty.get(one)); |
94 |
|
} |
99 |
|
public void testIsEmpty() { |
100 |
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
101 |
|
ConcurrentSkipListMap map = map5(); |
102 |
< |
assertTrue(empty.isEmpty()); |
102 |
> |
assertTrue(empty.isEmpty()); |
103 |
|
assertFalse(map.isEmpty()); |
104 |
|
} |
105 |
|
|
108 |
|
*/ |
109 |
|
public void testFirstKey() { |
110 |
|
ConcurrentSkipListMap map = map5(); |
111 |
< |
assertEquals(one, map.firstKey()); |
111 |
> |
assertEquals(one, map.firstKey()); |
112 |
|
} |
113 |
|
|
114 |
|
/** |
116 |
|
*/ |
117 |
|
public void testLastKey() { |
118 |
|
ConcurrentSkipListMap map = map5(); |
119 |
< |
assertEquals(five, map.lastKey()); |
119 |
> |
assertEquals(five, map.lastKey()); |
120 |
|
} |
121 |
|
|
122 |
|
|
125 |
|
*/ |
126 |
|
public void testKeySetToArray() { |
127 |
|
ConcurrentSkipListMap map = map5(); |
128 |
< |
Set s = map.keySet(); |
128 |
> |
Set s = map.keySet(); |
129 |
|
Object[] ar = s.toArray(); |
130 |
|
assertTrue(s.containsAll(Arrays.asList(ar))); |
131 |
< |
assertEquals(5, ar.length); |
131 |
> |
assertEquals(5, ar.length); |
132 |
|
ar[0] = m10; |
133 |
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
134 |
|
} |
138 |
|
*/ |
139 |
|
public void testDescendingKeySetToArray() { |
140 |
|
ConcurrentSkipListMap map = map5(); |
141 |
< |
Set s = map.descendingKeySet(); |
141 |
> |
Set s = map.descendingKeySet(); |
142 |
|
Object[] ar = s.toArray(); |
143 |
< |
assertEquals(5, ar.length); |
143 |
> |
assertEquals(5, ar.length); |
144 |
|
assertTrue(s.containsAll(Arrays.asList(ar))); |
145 |
|
ar[0] = m10; |
146 |
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
151 |
|
*/ |
152 |
|
public void testKeySet() { |
153 |
|
ConcurrentSkipListMap map = map5(); |
154 |
< |
Set s = map.keySet(); |
155 |
< |
assertEquals(5, s.size()); |
156 |
< |
assertTrue(s.contains(one)); |
157 |
< |
assertTrue(s.contains(two)); |
158 |
< |
assertTrue(s.contains(three)); |
159 |
< |
assertTrue(s.contains(four)); |
160 |
< |
assertTrue(s.contains(five)); |
154 |
> |
Set s = map.keySet(); |
155 |
> |
assertEquals(5, s.size()); |
156 |
> |
assertTrue(s.contains(one)); |
157 |
> |
assertTrue(s.contains(two)); |
158 |
> |
assertTrue(s.contains(three)); |
159 |
> |
assertTrue(s.contains(four)); |
160 |
> |
assertTrue(s.contains(five)); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
165 |
|
*/ |
166 |
|
public void testKeySetOrder() { |
167 |
|
ConcurrentSkipListMap map = map5(); |
168 |
< |
Set s = map.keySet(); |
168 |
> |
Set s = map.keySet(); |
169 |
|
Iterator i = s.iterator(); |
170 |
|
Integer last = (Integer)i.next(); |
171 |
|
assertEquals(last, one); |
172 |
+ |
int count = 1; |
173 |
|
while (i.hasNext()) { |
174 |
|
Integer k = (Integer)i.next(); |
175 |
|
assertTrue(last.compareTo(k) < 0); |
176 |
|
last = k; |
177 |
+ |
++count; |
178 |
+ |
} |
179 |
+ |
assertEquals(count ,5); |
180 |
+ |
} |
181 |
+ |
|
182 |
+ |
/** |
183 |
+ |
* descending iterator of key set is inverse ordered |
184 |
+ |
*/ |
185 |
+ |
public void testKeySetDescendingIteratorOrder() { |
186 |
+ |
ConcurrentSkipListMap map = map5(); |
187 |
+ |
NavigableSet s = map.navigableKeySet(); |
188 |
+ |
Iterator i = s.descendingIterator(); |
189 |
+ |
Integer last = (Integer)i.next(); |
190 |
+ |
assertEquals(last, five); |
191 |
+ |
int count = 1; |
192 |
+ |
while (i.hasNext()) { |
193 |
+ |
Integer k = (Integer)i.next(); |
194 |
+ |
assertTrue(last.compareTo(k) > 0); |
195 |
+ |
last = k; |
196 |
+ |
++count; |
197 |
|
} |
198 |
+ |
assertEquals(count ,5); |
199 |
|
} |
200 |
|
|
201 |
|
/** |
203 |
|
*/ |
204 |
|
public void testDescendingKeySetOrder() { |
205 |
|
ConcurrentSkipListMap map = map5(); |
206 |
< |
Set s = map.descendingKeySet(); |
206 |
> |
Set s = map.descendingKeySet(); |
207 |
|
Iterator i = s.iterator(); |
208 |
|
Integer last = (Integer)i.next(); |
209 |
|
assertEquals(last, five); |
210 |
+ |
int count = 1; |
211 |
|
while (i.hasNext()) { |
212 |
|
Integer k = (Integer)i.next(); |
213 |
|
assertTrue(last.compareTo(k) > 0); |
214 |
|
last = k; |
215 |
+ |
++count; |
216 |
|
} |
217 |
+ |
assertEquals(count, 5); |
218 |
+ |
} |
219 |
+ |
|
220 |
+ |
/** |
221 |
+ |
* descending iterator of descendingKeySet is ordered |
222 |
+ |
*/ |
223 |
+ |
public void testDescendingKeySetDescendingIteratorOrder() { |
224 |
+ |
ConcurrentSkipListMap map = map5(); |
225 |
+ |
NavigableSet s = map.descendingKeySet(); |
226 |
+ |
Iterator i = s.descendingIterator(); |
227 |
+ |
Integer last = (Integer)i.next(); |
228 |
+ |
assertEquals(last, one); |
229 |
+ |
int count = 1; |
230 |
+ |
while (i.hasNext()) { |
231 |
+ |
Integer k = (Integer)i.next(); |
232 |
+ |
assertTrue(last.compareTo(k) < 0); |
233 |
+ |
last = k; |
234 |
+ |
++count; |
235 |
+ |
} |
236 |
+ |
assertEquals(count, 5); |
237 |
+ |
} |
238 |
+ |
|
239 |
+ |
/** |
240 |
+ |
* Values.toArray contains all values |
241 |
+ |
*/ |
242 |
+ |
public void testValuesToArray() { |
243 |
+ |
ConcurrentSkipListMap map = map5(); |
244 |
+ |
Collection v = map.values(); |
245 |
+ |
Object[] ar = v.toArray(); |
246 |
+ |
ArrayList s = new ArrayList(Arrays.asList(ar)); |
247 |
+ |
assertEquals(5, ar.length); |
248 |
+ |
assertTrue(s.contains("A")); |
249 |
+ |
assertTrue(s.contains("B")); |
250 |
+ |
assertTrue(s.contains("C")); |
251 |
+ |
assertTrue(s.contains("D")); |
252 |
+ |
assertTrue(s.contains("E")); |
253 |
|
} |
254 |
|
|
255 |
|
/** |
257 |
|
*/ |
258 |
|
public void testValues() { |
259 |
|
ConcurrentSkipListMap map = map5(); |
260 |
< |
Collection s = map.values(); |
261 |
< |
assertEquals(5, s.size()); |
262 |
< |
assertTrue(s.contains("A")); |
263 |
< |
assertTrue(s.contains("B")); |
264 |
< |
assertTrue(s.contains("C")); |
265 |
< |
assertTrue(s.contains("D")); |
266 |
< |
assertTrue(s.contains("E")); |
260 |
> |
Collection s = map.values(); |
261 |
> |
assertEquals(5, s.size()); |
262 |
> |
assertTrue(s.contains("A")); |
263 |
> |
assertTrue(s.contains("B")); |
264 |
> |
assertTrue(s.contains("C")); |
265 |
> |
assertTrue(s.contains("D")); |
266 |
> |
assertTrue(s.contains("E")); |
267 |
|
} |
268 |
|
|
269 |
|
/** |
271 |
|
*/ |
272 |
|
public void testEntrySet() { |
273 |
|
ConcurrentSkipListMap map = map5(); |
274 |
< |
Set s = map.entrySet(); |
275 |
< |
assertEquals(5, s.size()); |
274 |
> |
Set s = map.entrySet(); |
275 |
> |
assertEquals(5, s.size()); |
276 |
|
Iterator it = s.iterator(); |
277 |
|
while (it.hasNext()) { |
278 |
|
Map.Entry e = (Map.Entry) it.next(); |
279 |
< |
assertTrue( |
279 |
> |
assertTrue( |
280 |
|
(e.getKey().equals(one) && e.getValue().equals("A")) || |
281 |
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
282 |
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
290 |
|
*/ |
291 |
|
public void testDescendingEntrySet() { |
292 |
|
ConcurrentSkipListMap map = map5(); |
293 |
< |
Set s = map.descendingEntrySet(); |
294 |
< |
assertEquals(5, s.size()); |
293 |
> |
Set s = map.descendingMap().entrySet(); |
294 |
> |
assertEquals(5, s.size()); |
295 |
|
Iterator it = s.iterator(); |
296 |
|
while (it.hasNext()) { |
297 |
|
Map.Entry e = (Map.Entry) it.next(); |
298 |
< |
assertTrue( |
298 |
> |
assertTrue( |
299 |
|
(e.getKey().equals(one) && e.getValue().equals("A")) || |
300 |
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
301 |
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
309 |
|
*/ |
310 |
|
public void testEntrySetToArray() { |
311 |
|
ConcurrentSkipListMap map = map5(); |
312 |
< |
Set s = map.entrySet(); |
312 |
> |
Set s = map.entrySet(); |
313 |
|
Object[] ar = s.toArray(); |
314 |
|
assertEquals(5, ar.length); |
315 |
|
for (int i = 0; i < 5; ++i) { |
323 |
|
*/ |
324 |
|
public void testDescendingEntrySetToArray() { |
325 |
|
ConcurrentSkipListMap map = map5(); |
326 |
< |
Set s = map.descendingEntrySet(); |
326 |
> |
Set s = map.descendingMap().entrySet(); |
327 |
|
Object[] ar = s.toArray(); |
328 |
|
assertEquals(5, ar.length); |
329 |
|
for (int i = 0; i < 5; ++i) { |
338 |
|
public void testPutAll() { |
339 |
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
340 |
|
ConcurrentSkipListMap map = map5(); |
341 |
< |
empty.putAll(map); |
342 |
< |
assertEquals(5, empty.size()); |
343 |
< |
assertTrue(empty.containsKey(one)); |
344 |
< |
assertTrue(empty.containsKey(two)); |
345 |
< |
assertTrue(empty.containsKey(three)); |
346 |
< |
assertTrue(empty.containsKey(four)); |
347 |
< |
assertTrue(empty.containsKey(five)); |
341 |
> |
empty.putAll(map); |
342 |
> |
assertEquals(5, empty.size()); |
343 |
> |
assertTrue(empty.containsKey(one)); |
344 |
> |
assertTrue(empty.containsKey(two)); |
345 |
> |
assertTrue(empty.containsKey(three)); |
346 |
> |
assertTrue(empty.containsKey(four)); |
347 |
> |
assertTrue(empty.containsKey(five)); |
348 |
|
} |
349 |
|
|
350 |
|
/** |
352 |
|
*/ |
353 |
|
public void testPutIfAbsent() { |
354 |
|
ConcurrentSkipListMap map = map5(); |
355 |
< |
map.putIfAbsent(six, "Z"); |
355 |
> |
map.putIfAbsent(six, "Z"); |
356 |
|
assertTrue(map.containsKey(six)); |
357 |
|
} |
358 |
|
|
369 |
|
*/ |
370 |
|
public void testReplace() { |
371 |
|
ConcurrentSkipListMap map = map5(); |
372 |
< |
assertNull(map.replace(six, "Z")); |
372 |
> |
assertNull(map.replace(six, "Z")); |
373 |
|
assertFalse(map.containsKey(six)); |
374 |
|
} |
375 |
|
|
389 |
|
public void testReplaceValue() { |
390 |
|
ConcurrentSkipListMap map = map5(); |
391 |
|
assertEquals("A", map.get(one)); |
392 |
< |
assertFalse(map.replace(one, "Z", "Z")); |
392 |
> |
assertFalse(map.replace(one, "Z", "Z")); |
393 |
|
assertEquals("A", map.get(one)); |
394 |
|
} |
395 |
|
|
399 |
|
public void testReplaceValue2() { |
400 |
|
ConcurrentSkipListMap map = map5(); |
401 |
|
assertEquals("A", map.get(one)); |
402 |
< |
assertTrue(map.replace(one, "A", "Z")); |
402 |
> |
assertTrue(map.replace(one, "A", "Z")); |
403 |
|
assertEquals("Z", map.get(one)); |
404 |
|
} |
405 |
|
|
409 |
|
*/ |
410 |
|
public void testRemove() { |
411 |
|
ConcurrentSkipListMap map = map5(); |
412 |
< |
map.remove(five); |
413 |
< |
assertEquals(4, map.size()); |
414 |
< |
assertFalse(map.containsKey(five)); |
412 |
> |
map.remove(five); |
413 |
> |
assertEquals(4, map.size()); |
414 |
> |
assertFalse(map.containsKey(five)); |
415 |
|
} |
416 |
|
|
417 |
|
/** |
419 |
|
*/ |
420 |
|
public void testRemove2() { |
421 |
|
ConcurrentSkipListMap map = map5(); |
422 |
< |
assertTrue(map.containsKey(five)); |
422 |
> |
assertTrue(map.containsKey(five)); |
423 |
|
assertEquals("E", map.get(five)); |
424 |
< |
map.remove(five, "E"); |
425 |
< |
assertEquals(4, map.size()); |
426 |
< |
assertFalse(map.containsKey(five)); |
427 |
< |
map.remove(four, "A"); |
428 |
< |
assertEquals(4, map.size()); |
429 |
< |
assertTrue(map.containsKey(four)); |
424 |
> |
map.remove(five, "E"); |
425 |
> |
assertEquals(4, map.size()); |
426 |
> |
assertFalse(map.containsKey(five)); |
427 |
> |
map.remove(four, "A"); |
428 |
> |
assertEquals(4, map.size()); |
429 |
> |
assertTrue(map.containsKey(four)); |
430 |
|
|
431 |
|
} |
432 |
|
|
506 |
|
|
507 |
|
} |
508 |
|
|
509 |
+ |
/** |
510 |
+ |
* lowerEntry, higherEntry, ceilingEntry, and floorEntry return |
511 |
+ |
* imutable entries |
512 |
+ |
*/ |
513 |
+ |
public void testEntryImmutablity() { |
514 |
+ |
ConcurrentSkipListMap map = map5(); |
515 |
+ |
Map.Entry e = map.lowerEntry(three); |
516 |
+ |
assertEquals(two, e.getKey()); |
517 |
+ |
try { |
518 |
+ |
e.setValue("X"); |
519 |
+ |
fail(); |
520 |
+ |
} catch (UnsupportedOperationException success) {} |
521 |
+ |
e = map.higherEntry(zero); |
522 |
+ |
assertEquals(one, e.getKey()); |
523 |
+ |
try { |
524 |
+ |
e.setValue("X"); |
525 |
+ |
fail(); |
526 |
+ |
} catch (UnsupportedOperationException success) {} |
527 |
+ |
e = map.floorEntry(one); |
528 |
+ |
assertEquals(one, e.getKey()); |
529 |
+ |
try { |
530 |
+ |
e.setValue("X"); |
531 |
+ |
fail(); |
532 |
+ |
} catch (UnsupportedOperationException success) {} |
533 |
+ |
e = map.ceilingEntry(five); |
534 |
+ |
assertEquals(five, e.getKey()); |
535 |
+ |
try { |
536 |
+ |
e.setValue("X"); |
537 |
+ |
fail(); |
538 |
+ |
} catch (UnsupportedOperationException success) {} |
539 |
+ |
} |
540 |
+ |
|
541 |
+ |
|
542 |
|
|
543 |
|
/** |
544 |
|
* lowerKey returns preceding element |
678 |
|
public void testSize() { |
679 |
|
ConcurrentSkipListMap map = map5(); |
680 |
|
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
681 |
< |
assertEquals(0, empty.size()); |
682 |
< |
assertEquals(5, map.size()); |
681 |
> |
assertEquals(0, empty.size()); |
682 |
> |
assertEquals(5, map.size()); |
683 |
|
} |
684 |
|
|
685 |
|
/** |
691 |
|
for (int i = 1; i <= 5; ++i) { |
692 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
693 |
|
} |
694 |
< |
} |
694 |
> |
} |
695 |
|
|
696 |
|
// Exception tests |
697 |
|
|
703 |
|
ConcurrentSkipListMap c = map5(); |
704 |
|
c.get(null); |
705 |
|
shouldThrow(); |
706 |
< |
} catch(NullPointerException e){} |
706 |
> |
} catch (NullPointerException e) {} |
707 |
|
} |
708 |
|
|
709 |
|
/** |
714 |
|
ConcurrentSkipListMap c = map5(); |
715 |
|
c.containsKey(null); |
716 |
|
shouldThrow(); |
717 |
< |
} catch(NullPointerException e){} |
717 |
> |
} catch (NullPointerException e) {} |
718 |
|
} |
719 |
|
|
720 |
|
/** |
725 |
|
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
726 |
|
c.containsValue(null); |
727 |
|
shouldThrow(); |
728 |
< |
} catch(NullPointerException e){} |
728 |
> |
} catch (NullPointerException e) {} |
729 |
|
} |
730 |
|
|
731 |
|
|
737 |
|
ConcurrentSkipListMap c = map5(); |
738 |
|
c.put(null, "whatever"); |
739 |
|
shouldThrow(); |
740 |
< |
} catch(NullPointerException e){} |
740 |
> |
} catch (NullPointerException e) {} |
741 |
|
} |
742 |
|
|
743 |
|
/** |
748 |
|
ConcurrentSkipListMap c = map5(); |
749 |
|
c.putIfAbsent(null, "whatever"); |
750 |
|
shouldThrow(); |
751 |
< |
} catch(NullPointerException e){} |
751 |
> |
} catch (NullPointerException e) {} |
752 |
|
} |
753 |
|
|
754 |
|
/** |
759 |
|
ConcurrentSkipListMap c = map5(); |
760 |
|
c.replace(null, "whatever"); |
761 |
|
shouldThrow(); |
762 |
< |
} catch(NullPointerException e){} |
762 |
> |
} catch (NullPointerException e) {} |
763 |
|
} |
764 |
|
|
765 |
|
/** |
770 |
|
ConcurrentSkipListMap c = map5(); |
771 |
|
c.replace(null, one, "whatever"); |
772 |
|
shouldThrow(); |
773 |
< |
} catch(NullPointerException e){} |
773 |
> |
} catch (NullPointerException e) {} |
774 |
|
} |
775 |
|
|
776 |
|
/** |
782 |
|
c.put("sadsdf", "asdads"); |
783 |
|
c.remove(null); |
784 |
|
shouldThrow(); |
785 |
< |
} catch(NullPointerException e){} |
785 |
> |
} catch (NullPointerException e) {} |
786 |
|
} |
787 |
|
|
788 |
|
/** |
794 |
|
c.put("sadsdf", "asdads"); |
795 |
|
c.remove(null, "whatever"); |
796 |
|
shouldThrow(); |
797 |
< |
} catch(NullPointerException e){} |
797 |
> |
} catch (NullPointerException e) {} |
798 |
> |
} |
799 |
> |
|
800 |
> |
/** |
801 |
> |
* remove(x, null) returns false |
802 |
> |
*/ |
803 |
> |
public void testRemove3() { |
804 |
> |
try { |
805 |
> |
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
806 |
> |
c.put("sadsdf", "asdads"); |
807 |
> |
assertFalse(c.remove("sadsdf", null)); |
808 |
> |
} catch (NullPointerException e) { |
809 |
> |
fail(); |
810 |
> |
} |
811 |
|
} |
812 |
|
|
813 |
|
/** |
814 |
|
* A deserialized map equals original |
815 |
|
*/ |
816 |
< |
public void testSerialization() { |
816 |
> |
public void testSerialization() throws Exception { |
817 |
|
ConcurrentSkipListMap q = map5(); |
818 |
|
|
819 |
< |
try { |
820 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
821 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
822 |
< |
out.writeObject(q); |
823 |
< |
out.close(); |
824 |
< |
|
825 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
826 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
827 |
< |
ConcurrentSkipListMap r = (ConcurrentSkipListMap)in.readObject(); |
828 |
< |
assertEquals(q.size(), r.size()); |
829 |
< |
assertTrue(q.equals(r)); |
724 |
< |
assertTrue(r.equals(q)); |
725 |
< |
} catch(Exception e){ |
726 |
< |
e.printStackTrace(); |
727 |
< |
unexpectedException(); |
728 |
< |
} |
819 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
820 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
821 |
> |
out.writeObject(q); |
822 |
> |
out.close(); |
823 |
> |
|
824 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
825 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
826 |
> |
ConcurrentSkipListMap r = (ConcurrentSkipListMap)in.readObject(); |
827 |
> |
assertEquals(q.size(), r.size()); |
828 |
> |
assertTrue(q.equals(r)); |
829 |
> |
assertTrue(r.equals(q)); |
830 |
|
} |
831 |
|
|
832 |
|
|
836 |
|
*/ |
837 |
|
public void testSubMapContents() { |
838 |
|
ConcurrentSkipListMap map = map5(); |
839 |
< |
NavigableMap sm = map.subMap(two, four); |
839 |
> |
NavigableMap sm = map.subMap(two, true, four, false); |
840 |
|
assertEquals(two, sm.firstKey()); |
841 |
|
assertEquals(three, sm.lastKey()); |
842 |
|
assertEquals(2, sm.size()); |
858 |
|
k = (Integer)(r.next()); |
859 |
|
assertEquals(two, k); |
860 |
|
assertFalse(r.hasNext()); |
861 |
< |
|
861 |
> |
|
862 |
|
Iterator j = sm.keySet().iterator(); |
863 |
|
j.next(); |
864 |
|
j.remove(); |
874 |
|
|
875 |
|
public void testSubMapContents2() { |
876 |
|
ConcurrentSkipListMap map = map5(); |
877 |
< |
NavigableMap sm = map.subMap(two, three); |
877 |
> |
NavigableMap sm = map.subMap(two, true, three, false); |
878 |
|
assertEquals(1, sm.size()); |
879 |
|
assertEquals(two, sm.firstKey()); |
880 |
|
assertEquals(two, sm.lastKey()); |
892 |
|
k = (Integer)(r.next()); |
893 |
|
assertEquals(two, k); |
894 |
|
assertFalse(r.hasNext()); |
895 |
< |
|
895 |
> |
|
896 |
|
Iterator j = sm.keySet().iterator(); |
897 |
|
j.next(); |
898 |
|
j.remove(); |
909 |
|
*/ |
910 |
|
public void testHeadMapContents() { |
911 |
|
ConcurrentSkipListMap map = map5(); |
912 |
< |
NavigableMap sm = map.headMap(four); |
912 |
> |
NavigableMap sm = map.headMap(four, false); |
913 |
|
assertTrue(sm.containsKey(one)); |
914 |
|
assertTrue(sm.containsKey(two)); |
915 |
|
assertTrue(sm.containsKey(three)); |
931 |
|
} |
932 |
|
|
933 |
|
/** |
934 |
< |
* headMap returns map with keys in requested range |
934 |
> |
* tailMap returns map with keys in requested range |
935 |
|
*/ |
936 |
|
public void testTailMapContents() { |
937 |
|
ConcurrentSkipListMap map = map5(); |
938 |
< |
NavigableMap sm = map.tailMap(two); |
938 |
> |
NavigableMap sm = map.tailMap(two, true); |
939 |
|
assertFalse(sm.containsKey(one)); |
940 |
|
assertTrue(sm.containsKey(two)); |
941 |
|
assertTrue(sm.containsKey(three)); |
979 |
|
assertEquals("E", e.getValue()); |
980 |
|
assertFalse(i.hasNext()); |
981 |
|
|
982 |
< |
NavigableMap ssm = sm.tailMap(four); |
982 |
> |
NavigableMap ssm = sm.tailMap(four, true); |
983 |
|
assertEquals(four, ssm.firstKey()); |
984 |
|
assertEquals(five, ssm.lastKey()); |
985 |
|
assertTrue(ssm.remove(four) != null); |
987 |
|
assertEquals(3, sm.size()); |
988 |
|
assertEquals(4, map.size()); |
989 |
|
} |
990 |
< |
|
990 |
> |
|
991 |
> |
Random rnd = new Random(666); |
992 |
> |
BitSet bs; |
993 |
> |
|
994 |
> |
/** |
995 |
> |
* Submaps of submaps subdivide correctly |
996 |
> |
*/ |
997 |
> |
public void testRecursiveSubMaps() { |
998 |
> |
int mapSize = 1000; |
999 |
> |
Class cl = ConcurrentSkipListMap.class; |
1000 |
> |
NavigableMap<Integer, Integer> map = newMap(cl); |
1001 |
> |
bs = new BitSet(mapSize); |
1002 |
> |
|
1003 |
> |
populate(map, mapSize); |
1004 |
> |
check(map, 0, mapSize - 1, true); |
1005 |
> |
check(map.descendingMap(), 0, mapSize - 1, false); |
1006 |
> |
|
1007 |
> |
mutateMap(map, 0, mapSize - 1); |
1008 |
> |
check(map, 0, mapSize - 1, true); |
1009 |
> |
check(map.descendingMap(), 0, mapSize - 1, false); |
1010 |
> |
|
1011 |
> |
bashSubMap(map.subMap(0, true, mapSize, false), |
1012 |
> |
0, mapSize - 1, true); |
1013 |
> |
} |
1014 |
> |
|
1015 |
> |
static NavigableMap<Integer, Integer> newMap(Class cl) { |
1016 |
> |
NavigableMap<Integer, Integer> result = null; |
1017 |
> |
try { |
1018 |
> |
result = (NavigableMap<Integer, Integer>) cl.newInstance(); |
1019 |
> |
} catch (Exception e) { |
1020 |
> |
fail(); |
1021 |
> |
} |
1022 |
> |
assertEquals(result.size(), 0); |
1023 |
> |
assertFalse(result.keySet().iterator().hasNext()); |
1024 |
> |
return result; |
1025 |
> |
} |
1026 |
> |
|
1027 |
> |
void populate(NavigableMap<Integer, Integer> map, int limit) { |
1028 |
> |
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
1029 |
> |
int key = rnd.nextInt(limit); |
1030 |
> |
put(map, key); |
1031 |
> |
} |
1032 |
> |
} |
1033 |
> |
|
1034 |
> |
void mutateMap(NavigableMap<Integer, Integer> map, int min, int max) { |
1035 |
> |
int size = map.size(); |
1036 |
> |
int rangeSize = max - min + 1; |
1037 |
> |
|
1038 |
> |
// Remove a bunch of entries directly |
1039 |
> |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
1040 |
> |
remove(map, min - 5 + rnd.nextInt(rangeSize + 10)); |
1041 |
> |
} |
1042 |
> |
|
1043 |
> |
// Remove a bunch of entries with iterator |
1044 |
> |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
1045 |
> |
if (rnd.nextBoolean()) { |
1046 |
> |
bs.clear(it.next()); |
1047 |
> |
it.remove(); |
1048 |
> |
} |
1049 |
> |
} |
1050 |
> |
|
1051 |
> |
// Add entries till we're back to original size |
1052 |
> |
while (map.size() < size) { |
1053 |
> |
int key = min + rnd.nextInt(rangeSize); |
1054 |
> |
assertTrue(key >= min && key<= max); |
1055 |
> |
put(map, key); |
1056 |
> |
} |
1057 |
> |
} |
1058 |
> |
|
1059 |
> |
void mutateSubMap(NavigableMap<Integer, Integer> map, int min, int max) { |
1060 |
> |
int size = map.size(); |
1061 |
> |
int rangeSize = max - min + 1; |
1062 |
> |
|
1063 |
> |
// Remove a bunch of entries directly |
1064 |
> |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
1065 |
> |
remove(map, min - 5 + rnd.nextInt(rangeSize + 10)); |
1066 |
> |
} |
1067 |
> |
|
1068 |
> |
// Remove a bunch of entries with iterator |
1069 |
> |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
1070 |
> |
if (rnd.nextBoolean()) { |
1071 |
> |
bs.clear(it.next()); |
1072 |
> |
it.remove(); |
1073 |
> |
} |
1074 |
> |
} |
1075 |
> |
|
1076 |
> |
// Add entries till we're back to original size |
1077 |
> |
while (map.size() < size) { |
1078 |
> |
int key = min - 5 + rnd.nextInt(rangeSize + 10); |
1079 |
> |
if (key >= min && key<= max) { |
1080 |
> |
put(map, key); |
1081 |
> |
} else { |
1082 |
> |
try { |
1083 |
> |
map.put(key, 2 * key); |
1084 |
> |
fail(); |
1085 |
> |
} catch (IllegalArgumentException e) { |
1086 |
> |
// expected |
1087 |
> |
} |
1088 |
> |
} |
1089 |
> |
} |
1090 |
> |
} |
1091 |
> |
|
1092 |
> |
void put(NavigableMap<Integer, Integer> map, int key) { |
1093 |
> |
if (map.put(key, 2 * key) == null) |
1094 |
> |
bs.set(key); |
1095 |
> |
} |
1096 |
> |
|
1097 |
> |
void remove(NavigableMap<Integer, Integer> map, int key) { |
1098 |
> |
if (map.remove(key) != null) |
1099 |
> |
bs.clear(key); |
1100 |
> |
} |
1101 |
> |
|
1102 |
> |
void bashSubMap(NavigableMap<Integer, Integer> map, |
1103 |
> |
int min, int max, boolean ascending) { |
1104 |
> |
check(map, min, max, ascending); |
1105 |
> |
check(map.descendingMap(), min, max, !ascending); |
1106 |
> |
|
1107 |
> |
mutateSubMap(map, min, max); |
1108 |
> |
check(map, min, max, ascending); |
1109 |
> |
check(map.descendingMap(), min, max, !ascending); |
1110 |
> |
|
1111 |
> |
// Recurse |
1112 |
> |
if (max - min < 2) |
1113 |
> |
return; |
1114 |
> |
int midPoint = (min + max) / 2; |
1115 |
> |
|
1116 |
> |
// headMap - pick direction and endpoint inclusion randomly |
1117 |
> |
boolean incl = rnd.nextBoolean(); |
1118 |
> |
NavigableMap<Integer,Integer> hm = map.headMap(midPoint, incl); |
1119 |
> |
if (ascending) { |
1120 |
> |
if (rnd.nextBoolean()) |
1121 |
> |
bashSubMap(hm, min, midPoint - (incl ? 0 : 1), true); |
1122 |
> |
else |
1123 |
> |
bashSubMap(hm.descendingMap(), min, midPoint - (incl ? 0 : 1), |
1124 |
> |
false); |
1125 |
> |
} else { |
1126 |
> |
if (rnd.nextBoolean()) |
1127 |
> |
bashSubMap(hm, midPoint + (incl ? 0 : 1), max, false); |
1128 |
> |
else |
1129 |
> |
bashSubMap(hm.descendingMap(), midPoint + (incl ? 0 : 1), max, |
1130 |
> |
true); |
1131 |
> |
} |
1132 |
> |
|
1133 |
> |
// tailMap - pick direction and endpoint inclusion randomly |
1134 |
> |
incl = rnd.nextBoolean(); |
1135 |
> |
NavigableMap<Integer,Integer> tm = map.tailMap(midPoint,incl); |
1136 |
> |
if (ascending) { |
1137 |
> |
if (rnd.nextBoolean()) |
1138 |
> |
bashSubMap(tm, midPoint + (incl ? 0 : 1), max, true); |
1139 |
> |
else |
1140 |
> |
bashSubMap(tm.descendingMap(), midPoint + (incl ? 0 : 1), max, |
1141 |
> |
false); |
1142 |
> |
} else { |
1143 |
> |
if (rnd.nextBoolean()) { |
1144 |
> |
bashSubMap(tm, min, midPoint - (incl ? 0 : 1), false); |
1145 |
> |
} else { |
1146 |
> |
bashSubMap(tm.descendingMap(), min, midPoint - (incl ? 0 : 1), |
1147 |
> |
true); |
1148 |
> |
} |
1149 |
> |
} |
1150 |
> |
|
1151 |
> |
// subMap - pick direction and endpoint inclusion randomly |
1152 |
> |
int rangeSize = max - min + 1; |
1153 |
> |
int[] endpoints = new int[2]; |
1154 |
> |
endpoints[0] = min + rnd.nextInt(rangeSize); |
1155 |
> |
endpoints[1] = min + rnd.nextInt(rangeSize); |
1156 |
> |
Arrays.sort(endpoints); |
1157 |
> |
boolean lowIncl = rnd.nextBoolean(); |
1158 |
> |
boolean highIncl = rnd.nextBoolean(); |
1159 |
> |
if (ascending) { |
1160 |
> |
NavigableMap<Integer,Integer> sm = map.subMap( |
1161 |
> |
endpoints[0], lowIncl, endpoints[1], highIncl); |
1162 |
> |
if (rnd.nextBoolean()) |
1163 |
> |
bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), |
1164 |
> |
endpoints[1] - (highIncl ? 0 : 1), true); |
1165 |
> |
else |
1166 |
> |
bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), |
1167 |
> |
endpoints[1] - (highIncl ? 0 : 1), false); |
1168 |
> |
} else { |
1169 |
> |
NavigableMap<Integer,Integer> sm = map.subMap( |
1170 |
> |
endpoints[1], highIncl, endpoints[0], lowIncl); |
1171 |
> |
if (rnd.nextBoolean()) |
1172 |
> |
bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), |
1173 |
> |
endpoints[1] - (highIncl ? 0 : 1), false); |
1174 |
> |
else |
1175 |
> |
bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), |
1176 |
> |
endpoints[1] - (highIncl ? 0 : 1), true); |
1177 |
> |
} |
1178 |
> |
} |
1179 |
> |
|
1180 |
> |
/** |
1181 |
> |
* min and max are both inclusive. If max < min, interval is empty. |
1182 |
> |
*/ |
1183 |
> |
void check(NavigableMap<Integer, Integer> map, |
1184 |
> |
final int min, final int max, final boolean ascending) { |
1185 |
> |
class ReferenceSet { |
1186 |
> |
int lower(int key) { |
1187 |
> |
return ascending ? lowerAscending(key) : higherAscending(key); |
1188 |
> |
} |
1189 |
> |
int floor(int key) { |
1190 |
> |
return ascending ? floorAscending(key) : ceilingAscending(key); |
1191 |
> |
} |
1192 |
> |
int ceiling(int key) { |
1193 |
> |
return ascending ? ceilingAscending(key) : floorAscending(key); |
1194 |
> |
} |
1195 |
> |
int higher(int key) { |
1196 |
> |
return ascending ? higherAscending(key) : lowerAscending(key); |
1197 |
> |
} |
1198 |
> |
int first() { |
1199 |
> |
return ascending ? firstAscending() : lastAscending(); |
1200 |
> |
} |
1201 |
> |
int last() { |
1202 |
> |
return ascending ? lastAscending() : firstAscending(); |
1203 |
> |
} |
1204 |
> |
int lowerAscending(int key) { |
1205 |
> |
return floorAscending(key - 1); |
1206 |
> |
} |
1207 |
> |
int floorAscending(int key) { |
1208 |
> |
if (key < min) |
1209 |
> |
return -1; |
1210 |
> |
else if (key > max) |
1211 |
> |
key = max; |
1212 |
> |
|
1213 |
> |
// BitSet should support this! Test would run much faster |
1214 |
> |
while (key >= min) { |
1215 |
> |
if (bs.get(key)) |
1216 |
> |
return(key); |
1217 |
> |
key--; |
1218 |
> |
} |
1219 |
> |
return -1; |
1220 |
> |
} |
1221 |
> |
int ceilingAscending(int key) { |
1222 |
> |
if (key < min) |
1223 |
> |
key = min; |
1224 |
> |
else if (key > max) |
1225 |
> |
return -1; |
1226 |
> |
int result = bs.nextSetBit(key); |
1227 |
> |
return result > max ? -1 : result; |
1228 |
> |
} |
1229 |
> |
int higherAscending(int key) { |
1230 |
> |
return ceilingAscending(key + 1); |
1231 |
> |
} |
1232 |
> |
private int firstAscending() { |
1233 |
> |
int result = ceilingAscending(min); |
1234 |
> |
return result > max ? -1 : result; |
1235 |
> |
} |
1236 |
> |
private int lastAscending() { |
1237 |
> |
int result = floorAscending(max); |
1238 |
> |
return result < min ? -1 : result; |
1239 |
> |
} |
1240 |
> |
} |
1241 |
> |
ReferenceSet rs = new ReferenceSet(); |
1242 |
> |
|
1243 |
> |
// Test contents using containsKey |
1244 |
> |
int size = 0; |
1245 |
> |
for (int i = min; i <= max; i++) { |
1246 |
> |
boolean bsContainsI = bs.get(i); |
1247 |
> |
assertEquals(bsContainsI, map.containsKey(i)); |
1248 |
> |
if (bsContainsI) |
1249 |
> |
size++; |
1250 |
> |
} |
1251 |
> |
assertEquals(map.size(), size); |
1252 |
> |
|
1253 |
> |
// Test contents using contains keySet iterator |
1254 |
> |
int size2 = 0; |
1255 |
> |
int previousKey = -1; |
1256 |
> |
for (int key : map.keySet()) { |
1257 |
> |
assertTrue(bs.get(key)); |
1258 |
> |
size2++; |
1259 |
> |
assertTrue(previousKey < 0 || |
1260 |
> |
(ascending ? key - previousKey > 0 : key - previousKey < 0)); |
1261 |
> |
previousKey = key; |
1262 |
> |
} |
1263 |
> |
assertEquals(size2, size); |
1264 |
> |
|
1265 |
> |
// Test navigation ops |
1266 |
> |
for (int key = min - 1; key <= max + 1; key++) { |
1267 |
> |
assertEq(map.lowerKey(key), rs.lower(key)); |
1268 |
> |
assertEq(map.floorKey(key), rs.floor(key)); |
1269 |
> |
assertEq(map.higherKey(key), rs.higher(key)); |
1270 |
> |
assertEq(map.ceilingKey(key), rs.ceiling(key)); |
1271 |
> |
} |
1272 |
> |
|
1273 |
> |
// Test extrema |
1274 |
> |
if (map.size() != 0) { |
1275 |
> |
assertEq(map.firstKey(), rs.first()); |
1276 |
> |
assertEq(map.lastKey(), rs.last()); |
1277 |
> |
} else { |
1278 |
> |
assertEq(rs.first(), -1); |
1279 |
> |
assertEq(rs.last(), -1); |
1280 |
> |
try { |
1281 |
> |
map.firstKey(); |
1282 |
> |
fail(); |
1283 |
> |
} catch (NoSuchElementException e) { |
1284 |
> |
// expected |
1285 |
> |
} |
1286 |
> |
try { |
1287 |
> |
map.lastKey(); |
1288 |
> |
fail(); |
1289 |
> |
} catch (NoSuchElementException e) { |
1290 |
> |
// expected |
1291 |
> |
} |
1292 |
> |
} |
1293 |
> |
} |
1294 |
> |
|
1295 |
> |
static void assertEq(Integer i, int j) { |
1296 |
> |
if (i == null) |
1297 |
> |
assertEquals(j, -1); |
1298 |
> |
else |
1299 |
> |
assertEquals((int) i, j); |
1300 |
> |
} |
1301 |
> |
|
1302 |
> |
static boolean eq(Integer i, int j) { |
1303 |
> |
return i == null ? j == -1 : i == j; |
1304 |
> |
} |
1305 |
> |
|
1306 |
|
} |