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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
10 |
< |
import java.io.*; |
7 |
> |
import java.util.ArrayList; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.BitSet; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Iterator; |
12 |
> |
import java.util.Map; |
13 |
> |
import java.util.NavigableMap; |
14 |
> |
import java.util.NavigableSet; |
15 |
> |
import java.util.NoSuchElementException; |
16 |
> |
import java.util.Random; |
17 |
> |
import java.util.Set; |
18 |
> |
import java.util.concurrent.ConcurrentSkipListMap; |
19 |
> |
|
20 |
> |
import junit.framework.Test; |
21 |
> |
import junit.framework.TestSuite; |
22 |
|
|
23 |
|
public class ConcurrentSkipListMapTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
29 |
|
} |
30 |
|
|
31 |
|
/** |
32 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
32 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
33 |
|
*/ |
34 |
|
private static ConcurrentSkipListMap map5() { |
35 |
|
ConcurrentSkipListMap map = new ConcurrentSkipListMap(); |
50 |
|
public void testClear() { |
51 |
|
ConcurrentSkipListMap map = map5(); |
52 |
|
map.clear(); |
53 |
< |
assertEquals(map.size(), 0); |
53 |
> |
assertEquals(0, map.size()); |
54 |
|
} |
55 |
|
|
56 |
|
/** |
57 |
< |
* |
57 |
> |
* copy constructor creates map equal to source map |
58 |
|
*/ |
59 |
|
public void testConstructFromSorted() { |
60 |
|
ConcurrentSkipListMap map = map5(); |
130 |
|
assertEquals(five, map.lastKey()); |
131 |
|
} |
132 |
|
|
122 |
– |
|
133 |
|
/** |
134 |
|
* keySet.toArray returns contains all keys |
135 |
|
*/ |
186 |
|
last = k; |
187 |
|
++count; |
188 |
|
} |
189 |
< |
assertEquals(count ,5); |
189 |
> |
assertEquals(5, count); |
190 |
|
} |
191 |
|
|
192 |
|
/** |
205 |
|
last = k; |
206 |
|
++count; |
207 |
|
} |
208 |
< |
assertEquals(count ,5); |
208 |
> |
assertEquals(5, count); |
209 |
|
} |
210 |
|
|
211 |
|
/** |
224 |
|
last = k; |
225 |
|
++count; |
226 |
|
} |
227 |
< |
assertEquals(count, 5); |
227 |
> |
assertEquals(5, count); |
228 |
|
} |
229 |
|
|
230 |
|
/** |
243 |
|
last = k; |
244 |
|
++count; |
245 |
|
} |
246 |
< |
assertEquals(count, 5); |
246 |
> |
assertEquals(5, count); |
247 |
|
} |
248 |
|
|
249 |
|
/** |
392 |
|
assertEquals("Z", map.get(one)); |
393 |
|
} |
394 |
|
|
385 |
– |
|
395 |
|
/** |
396 |
|
* replace value fails when the given key not mapped to expected value |
397 |
|
*/ |
412 |
|
assertEquals("Z", map.get(one)); |
413 |
|
} |
414 |
|
|
406 |
– |
|
415 |
|
/** |
416 |
|
* remove removes the correct key-value pair from the map |
417 |
|
*/ |
513 |
|
* lowerEntry, higherEntry, ceilingEntry, and floorEntry return |
514 |
|
* immutable entries |
515 |
|
*/ |
516 |
< |
public void testEntryImmutablity() { |
516 |
> |
public void testEntryImmutability() { |
517 |
|
ConcurrentSkipListMap map = map5(); |
518 |
|
Map.Entry e = map.lowerEntry(three); |
519 |
|
assertEquals(two, e.getKey()); |
541 |
|
} catch (UnsupportedOperationException success) {} |
542 |
|
} |
543 |
|
|
536 |
– |
|
537 |
– |
|
544 |
|
/** |
545 |
|
* lowerKey returns preceding element |
546 |
|
*/ |
684 |
|
ConcurrentSkipListMap map = map5(); |
685 |
|
String s = map.toString(); |
686 |
|
for (int i = 1; i <= 5; ++i) { |
687 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
687 |
> |
assertTrue(s.contains(String.valueOf(i))); |
688 |
|
} |
689 |
|
} |
690 |
|
|
723 |
|
} catch (NullPointerException success) {} |
724 |
|
} |
725 |
|
|
720 |
– |
|
726 |
|
/** |
727 |
|
* put(null,x) throws NPE |
728 |
|
*/ |
804 |
|
* A deserialized map equals original |
805 |
|
*/ |
806 |
|
public void testSerialization() throws Exception { |
807 |
< |
ConcurrentSkipListMap q = map5(); |
807 |
> |
NavigableMap x = map5(); |
808 |
> |
NavigableMap y = serialClone(x); |
809 |
|
|
810 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
811 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
812 |
< |
out.writeObject(q); |
813 |
< |
out.close(); |
814 |
< |
|
809 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
810 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
811 |
< |
ConcurrentSkipListMap r = (ConcurrentSkipListMap)in.readObject(); |
812 |
< |
assertEquals(q.size(), r.size()); |
813 |
< |
assertTrue(q.equals(r)); |
814 |
< |
assertTrue(r.equals(q)); |
810 |
> |
assertNotSame(x, y); |
811 |
> |
assertEquals(x.size(), y.size()); |
812 |
> |
assertEquals(x.toString(), y.toString()); |
813 |
> |
assertEquals(x, y); |
814 |
> |
assertEquals(y, x); |
815 |
|
} |
816 |
|
|
817 |
– |
|
818 |
– |
|
817 |
|
/** |
818 |
|
* subMap returns map with keys in requested range |
819 |
|
*/ |
998 |
|
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception { |
999 |
|
NavigableMap<Integer, Integer> result = |
1000 |
|
(NavigableMap<Integer, Integer>) cl.newInstance(); |
1001 |
< |
assertEquals(result.size(), 0); |
1001 |
> |
assertEquals(0, result.size()); |
1002 |
|
assertFalse(result.keySet().iterator().hasNext()); |
1003 |
|
return result; |
1004 |
|
} |
1030 |
|
// Add entries till we're back to original size |
1031 |
|
while (map.size() < size) { |
1032 |
|
int key = min + rnd.nextInt(rangeSize); |
1033 |
< |
assertTrue(key >= min && key<= max); |
1033 |
> |
assertTrue(key >= min && key <= max); |
1034 |
|
put(map, key); |
1035 |
|
} |
1036 |
|
} |
1055 |
|
// Add entries till we're back to original size |
1056 |
|
while (map.size() < size) { |
1057 |
|
int key = min - 5 + rnd.nextInt(rangeSize + 10); |
1058 |
< |
if (key >= min && key<= max) { |
1058 |
> |
if (key >= min && key <= max) { |
1059 |
|
put(map, key); |
1060 |
|
} else { |
1061 |
|
try { |
1159 |
|
*/ |
1160 |
|
void check(NavigableMap<Integer, Integer> map, |
1161 |
|
final int min, final int max, final boolean ascending) { |
1162 |
< |
class ReferenceSet { |
1162 |
> |
class ReferenceSet { |
1163 |
|
int lower(int key) { |
1164 |
|
return ascending ? lowerAscending(key) : higherAscending(key); |
1165 |
|
} |
1225 |
|
if (bsContainsI) |
1226 |
|
size++; |
1227 |
|
} |
1228 |
< |
assertEquals(map.size(), size); |
1228 |
> |
assertEquals(size, map.size()); |
1229 |
|
|
1230 |
|
// Test contents using contains keySet iterator |
1231 |
|
int size2 = 0; |