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 |
|
* @test |
12 |
|
* can create one from a real dictionary (1 line per word) and then run |
13 |
|
* linux "shuf" to randomize entries. |
14 |
|
*/ |
15 |
– |
import java.util.*; |
15 |
|
import java.io.*; |
16 |
+ |
import java.util.*; |
17 |
|
|
18 |
|
public class MapCheck { |
19 |
|
static final Object MISSING = new Object(); |
20 |
|
static TestTimer timer = new TestTimer(); |
21 |
< |
static Class eclass; |
22 |
< |
static Class mapClass = java.util.concurrent.ConcurrentHashMap.class; |
21 |
> |
static Class<?> eclass; |
22 |
> |
static Class<?> mapClass = java.util.concurrent.ConcurrentHashMap.class; |
23 |
|
|
24 |
|
static final LoopHelpers.SimpleRandom srng = new LoopHelpers.SimpleRandom(); |
25 |
|
static final Random rng = new Random(3152688); |
79 |
|
Object[] absent = new Object[size]; |
80 |
|
initializeKeys(key, absent, size); |
81 |
|
|
82 |
< |
precheck(size, key, absent); |
82 |
> |
precheck(size, key, absent); |
83 |
|
|
84 |
|
for (int rep = 0; rep < numTests; ++rep) { |
85 |
|
mainTest(newMap(), key, absent); |
143 |
|
if (m.get(null) != null) throw new Error(); |
144 |
|
} |
145 |
|
|
146 |
– |
|
146 |
|
static void getTest(String nm, int n, Map s, Object[] key, int expect) { |
147 |
|
int sum = 0; |
148 |
|
timer.start(nm, n); |
152 |
|
++sum; |
153 |
|
} |
154 |
|
timer.finish(); |
155 |
< |
reallyAssert (sum == expect); |
155 |
> |
reallyAssert(sum == expect); |
156 |
|
checkSum += sum; |
157 |
|
} |
158 |
|
|
160 |
– |
|
159 |
|
// unused |
160 |
|
static void getTestBoxed(String nm, int n, Map s, Object[] key, int expect) { |
161 |
|
int sum = 0; |
162 |
|
Map<Integer,Integer> intMap = (Map<Integer,Integer>)s; |
163 |
|
timer.start(nm, n); |
164 |
|
for (int i = 0; i < n; i++) { |
165 |
< |
if ((Integer) (intMap.get(i)) != i) ++sum; |
165 |
> |
if (intMap.get(i) != i) ++sum; |
166 |
|
} |
167 |
|
timer.finish(); |
168 |
< |
reallyAssert (sum == expect); |
168 |
> |
reallyAssert(sum == expect); |
169 |
|
} |
170 |
|
|
171 |
|
static void remTest(String nm, int n, Map s, Object[] key, int expect) { |
175 |
|
if (s.remove(key[i]) != null) ++sum; |
176 |
|
} |
177 |
|
timer.finish(); |
178 |
< |
reallyAssert (sum == expect); |
178 |
> |
reallyAssert(sum == expect); |
179 |
|
checkSum += sum; |
180 |
|
} |
181 |
|
|
184 |
|
timer.start(nm, n); |
185 |
|
s.clear(); |
186 |
|
timer.finish(); |
187 |
< |
reallyAssert (s.isEmpty()); |
187 |
> |
reallyAssert(s.isEmpty()); |
188 |
|
} |
189 |
|
|
190 |
|
static void putTest(String nm, int n, Map s, Object[] key, int expect) { |
196 |
|
if (v == null) ++sum; |
197 |
|
} |
198 |
|
timer.finish(); |
199 |
< |
reallyAssert (sum == expect); |
199 |
> |
reallyAssert(sum == expect); |
200 |
|
checkSum += sum; |
201 |
|
} |
202 |
|
|
207 |
|
if (s.containsKey(key[i])) ++sum; |
208 |
|
} |
209 |
|
timer.finish(); |
210 |
< |
reallyAssert (sum == expect); |
210 |
> |
reallyAssert(sum == expect); |
211 |
|
checkSum += sum; |
212 |
|
} |
213 |
|
|
217 |
|
for (int i = 0; i < n; i++) { |
218 |
|
if (s.containsKey(key[i])) ++sum; |
219 |
|
} |
220 |
< |
reallyAssert (sum == expect); |
220 |
> |
reallyAssert(sum == expect); |
221 |
|
checkSum += sum; |
222 |
|
} |
223 |
|
|
228 |
|
if (s.remove(key[i]) != null) ++sum; |
229 |
|
} |
230 |
|
timer.finish(); |
231 |
< |
reallyAssert (sum == expect); |
231 |
> |
reallyAssert(sum == expect); |
232 |
|
checkSum += sum; |
233 |
|
} |
234 |
|
|
238 |
|
timer.start("Traverse key or value ", size); |
239 |
|
if (s.containsValue(MISSING)) ++sum; |
240 |
|
timer.finish(); |
241 |
< |
reallyAssert (sum == 0); |
241 |
> |
reallyAssert(sum == 0); |
242 |
|
checkSum += sum; |
243 |
|
} |
244 |
|
|
247 |
– |
|
245 |
|
static Object kitTest(Map s, int size) { |
246 |
|
Object last = null; |
247 |
|
int sum = 0; |
253 |
|
last = x; |
254 |
|
} |
255 |
|
timer.finish(); |
256 |
< |
reallyAssert (sum == size); |
256 |
> |
reallyAssert(sum == size); |
257 |
|
checkSum += sum; |
258 |
|
return last; |
259 |
|
} |
269 |
|
last = x; |
270 |
|
} |
271 |
|
timer.finish(); |
272 |
< |
reallyAssert (sum == size); |
272 |
> |
reallyAssert(sum == size); |
273 |
|
checkSum += sum; |
274 |
|
return last; |
275 |
|
} |
286 |
|
++sum; |
287 |
|
} |
288 |
|
timer.finish(); |
289 |
< |
reallyAssert (sum == size); |
289 |
> |
reallyAssert(sum == size); |
290 |
|
checkSum += sum; |
291 |
|
} |
292 |
|
|
293 |
|
static void itRemTest(Map s, int size) { |
294 |
|
int sz = s.size(); |
295 |
< |
reallyAssert (sz == size); |
295 |
> |
reallyAssert(sz == size); |
296 |
|
timer.start("Remove Present ", size); |
297 |
|
int sum = 0; |
298 |
|
for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { |
301 |
|
++sum; |
302 |
|
} |
303 |
|
timer.finish(); |
304 |
< |
reallyAssert (sum == sz); |
304 |
> |
reallyAssert(sum == sz); |
305 |
|
checkSum += sum; |
306 |
|
} |
307 |
|
|
308 |
|
static void itHalfRemTest(Map s, int size) { |
309 |
|
int sz = s.size(); |
310 |
< |
reallyAssert (sz == size); |
310 |
> |
reallyAssert(sz == size); |
311 |
|
timer.start("Remove Present ", size); |
312 |
|
int sum = 0; |
313 |
|
for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { |
318 |
|
++sum; |
319 |
|
} |
320 |
|
timer.finish(); |
321 |
< |
reallyAssert (sum == sz / 2); |
321 |
> |
reallyAssert(sum == sz / 2); |
322 |
|
checkSum += sum; |
323 |
|
} |
324 |
|
|
326 |
|
timer.start(nm, n); |
327 |
|
dst.putAll(src); |
328 |
|
timer.finish(); |
329 |
< |
reallyAssert (src.size() == dst.size()); |
329 |
> |
reallyAssert(src.size() == dst.size()); |
330 |
|
} |
331 |
|
|
332 |
|
static void serTest(Map s, int size) throws Exception { |
355 |
|
System.out.print(time + "ms"); |
356 |
|
|
357 |
|
if (s instanceof IdentityHashMap) return; |
358 |
< |
reallyAssert (s.equals(m)); |
358 |
> |
reallyAssert(s.equals(m)); |
359 |
|
} |
360 |
|
|
361 |
|
static void mainTest(Map s, Object[] key, Object[] absent) { |
423 |
|
timer.start("Traverse entry ", size * 12); // 12 until finish |
424 |
|
|
425 |
|
int sh1 = s.hashCode() - s2.hashCode(); |
426 |
< |
reallyAssert (sh1 == 0); |
426 |
> |
reallyAssert(sh1 == 0); |
427 |
|
|
428 |
|
boolean eq1 = s2.equals(s); |
429 |
|
boolean eq2 = s.equals(s2); |
430 |
< |
reallyAssert (eq1 && eq2); |
430 |
> |
reallyAssert(eq1 && eq2); |
431 |
|
|
432 |
|
Set es2 = s2.entrySet(); |
433 |
|
for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { |
434 |
|
Object entry = it.next(); |
435 |
|
if (es2.contains(entry)) ++sum; |
436 |
|
} |
437 |
< |
reallyAssert (sum == size); |
437 |
> |
reallyAssert(sum == size); |
438 |
|
|
439 |
|
s2.put(lastkey, MISSING); |
440 |
|
|
441 |
|
int sh2 = s.hashCode() - s2.hashCode(); |
442 |
< |
reallyAssert (sh2 != 0); |
442 |
> |
reallyAssert(sh2 != 0); |
443 |
|
|
444 |
|
eq1 = s2.equals(s); |
445 |
|
eq2 = s.equals(s2); |
446 |
< |
reallyAssert (!eq1 && !eq2); |
446 |
> |
reallyAssert(!eq1 && !eq2); |
447 |
|
|
448 |
|
sum = 0; |
449 |
|
for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { |
450 |
|
Map.Entry e = (Map.Entry)it.next(); |
451 |
|
e.setValue(absent[sum++]); |
452 |
|
} |
453 |
< |
reallyAssert (sum == size); |
453 |
> |
reallyAssert(sum == size); |
454 |
|
for (Iterator it = s2.entrySet().iterator(); it.hasNext(); ) { |
455 |
|
Map.Entry e = (Map.Entry)it.next(); |
456 |
|
e.setValue(s.get(e.getKey())); |
470 |
|
reallyAssert(rmiss == 0); |
471 |
|
|
472 |
|
clrTest(size, s2); |
473 |
< |
reallyAssert (s2.isEmpty() && s.isEmpty()); |
473 |
> |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
474 |
|
} |
475 |
|
|
476 |
|
static void itTest4(Map s, int size, int pos) { |
477 |
|
IdentityHashMap seen = new IdentityHashMap(size); |
478 |
< |
reallyAssert (s.size() == size); |
478 |
> |
reallyAssert(s.size() == size); |
479 |
|
int sum = 0; |
480 |
|
timer.start("Iter XEntry ", size); |
481 |
|
Iterator it = s.entrySet().iterator(); |
489 |
|
if (x != MISSING) |
490 |
|
++sum; |
491 |
|
} |
492 |
< |
reallyAssert (s.containsKey(k)); |
492 |
> |
reallyAssert(s.containsKey(k)); |
493 |
|
it.remove(); |
494 |
< |
reallyAssert (!s.containsKey(k)); |
494 |
> |
reallyAssert(!s.containsKey(k)); |
495 |
|
while (it.hasNext()) { |
496 |
|
Map.Entry x = (Map.Entry)(it.next()); |
497 |
|
Object k2 = x.getKey(); |
500 |
|
++sum; |
501 |
|
} |
502 |
|
|
503 |
< |
reallyAssert (s.size() == size-1); |
503 |
> |
reallyAssert(s.size() == size-1); |
504 |
|
s.put(k, v); |
505 |
< |
reallyAssert (seen.size() == size); |
505 |
> |
reallyAssert(seen.size() == size); |
506 |
|
timer.finish(); |
507 |
< |
reallyAssert (sum == size); |
508 |
< |
reallyAssert (s.size() == size); |
507 |
> |
reallyAssert(sum == size); |
508 |
> |
reallyAssert(s.size() == size); |
509 |
|
} |
510 |
|
|
514 |
– |
|
515 |
– |
|
511 |
|
static void initializeKeys(Object[] key, Object[] absent, int size) { |
512 |
|
if (eclass == Object.class) { |
513 |
|
for (int i = 0; i < size; ++i) key[i] = new Object(); |
605 |
|
abs[ai++] = s; |
606 |
|
break; |
607 |
|
} |
608 |
< |
sb.append((char)c); |
608 |
> |
sb.append((char) c); |
609 |
|
} |
610 |
|
} |
611 |
|
in.close(); |
616 |
|
} |
617 |
|
} |
618 |
|
|
624 |
– |
|
619 |
|
static void randomWords(Object[] ws, int origin, int size) { |
620 |
|
for (int i = origin; i < size; ++i) { |
621 |
|
int k = 0; |
623 |
|
char[] c = new char[len * 4 + 1]; |
624 |
|
for (int j = 1; j < len; ++j) { |
625 |
|
int r = srng.next(); |
626 |
< |
c[k++] = (char)(' ' + (r & 0x7f)); |
626 |
> |
c[k++] = (char) (' ' + (r & 0x7f)); |
627 |
|
r >>>= 8; |
628 |
< |
c[k++] = (char)(' ' + (r & 0x7f)); |
628 |
> |
c[k++] = (char) (' ' + (r & 0x7f)); |
629 |
|
r >>>= 8; |
630 |
< |
c[k++] = (char)(' ' + (r & 0x7f)); |
630 |
> |
c[k++] = (char) (' ' + (r & 0x7f)); |
631 |
|
r >>>= 8; |
632 |
< |
c[k++] = (char)(' ' + (r & 0x7f)); |
632 |
> |
c[k++] = (char) (' ' + (r & 0x7f)); |
633 |
|
} |
634 |
< |
c[k++] = (char)((i & 31) | 1); // never == to any testword |
634 |
> |
c[k++] = (char) ((i & 31) | 1); // never == to any testword |
635 |
|
ws[i] = new String(c); |
636 |
|
} |
637 |
|
} |
657 |
|
else |
658 |
|
s = stats.sum; |
659 |
|
|
660 |
< |
double t = ((double)s) / n; |
660 |
> |
double t = ((double) s) / n; |
661 |
|
long nano = Math.round(t); |
662 |
|
System.out.printf("%6d", + nano); |
663 |
|
System.out.println(); |
696 |
|
} |
697 |
|
} |
698 |
|
|
705 |
– |
|
699 |
|
static void shuffle(Object[] keys) { |
700 |
|
int size = keys.length; |
701 |
< |
for (int i= size; i>1; i--) { |
701 |
> |
for (int i = size; i > 1; i--) { |
702 |
|
int r = rng.nextInt(i); |
703 |
|
Object t = keys[i-1]; |
704 |
|
keys[i-1] = keys[r]; |
708 |
|
|
709 |
|
static void shuffle(ArrayList keys) { |
710 |
|
int size = keys.size(); |
711 |
< |
for (int i= size; i>1; i--) { |
711 |
> |
for (int i = size; i > 1; i--) { |
712 |
|
int r = rng.nextInt(i); |
713 |
|
Object t = keys.get(i-1); |
714 |
|
keys.set(i-1, keys.get(r)); |