1 /*
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
181 testContainsKey(map, keys_desc, keys);
182
183 testRemove(map, keys_desc, keys);
184
185 map.clear();
186 testInsertion(map, keys_desc, keys);
187 testKeysIteratorRemove(map, keys_desc, keys);
188
189 map.clear();
190 testInsertion(map, keys_desc, keys);
191 testValuesIteratorRemove(map, keys_desc, keys);
192
193 map.clear();
194 testInsertion(map, keys_desc, keys);
195 testEntriesIteratorRemove(map, keys_desc, keys);
196
197 check(map.isEmpty());
198 }
199
200 private static <T> void testInsertion(Map<T, T> map, String keys_desc, T[] keys) {
201 check("map empty", (map.size() == 0) && map.isEmpty());
202
203 for (int i = 0; i < keys.length; i++) {
204 check(String.format("insertion: map expected size m%d != i%d", map.size(), i),
205 map.size() == i);
206 check(String.format("insertion: put(%s[%d])", keys_desc, i), null == map.put(keys[i], keys[i]));
207 check(String.format("insertion: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
208 check(String.format("insertion: containsValue(%s[%d])", keys_desc, i), map.containsValue(keys[i]));
209 }
210
211 check(String.format("map expected size m%d != k%d", map.size(), keys.length),
212 map.size() == keys.length);
213 }
214
215 private static void testIntegerIteration(Map<HashableInteger, HashableInteger> map, HashableInteger[] keys) {
216 check(String.format("map expected size m%d != k%d", map.size(), keys.length),
217 map.size() == keys.length);
218
219 BitSet all = new BitSet(keys.length);
220 for (Map.Entry<HashableInteger, HashableInteger> each : map.entrySet()) {
221 check("Iteration: key already seen", !all.get(each.getKey().value));
222 all.set(each.getKey().value);
223 }
224
225 all.flip(0, keys.length);
226 check("Iteration: some keys not visited", all.isEmpty());
227
228 for (HashableInteger each : map.keySet()) {
229 check("Iteration: key already seen", !all.get(each.value));
230 all.set(each.value);
231 }
232
233 all.flip(0, keys.length);
234 check("Iteration: some keys not visited", all.isEmpty());
235
236 int count = 0;
237 for (HashableInteger each : map.values()) {
238 count++;
239 }
240
241 check(String.format("Iteration: value count matches size m%d != c%d", map.size(), count),
242 map.size() == count);
243 }
244
245 private static void testStringIteration(Map<String, String> map, String[] keys) {
246 check(String.format("map expected size m%d != k%d", map.size(), keys.length),
247 map.size() == keys.length);
248
249 BitSet all = new BitSet(keys.length);
250 for (Map.Entry<String, String> each : map.entrySet()) {
251 String key = each.getKey();
252 boolean longKey = key.length() > 5;
253 int index = key.hashCode() + (longKey ? keys.length / 2 : 0);
254 check("key already seen", !all.get(index));
255 all.set(index);
256 }
257
258 all.flip(0, keys.length);
259 check("some keys not visited", all.isEmpty());
260
261 for (String each : map.keySet()) {
262 boolean longKey = each.length() > 5;
263 int index = each.hashCode() + (longKey ? keys.length / 2 : 0);
264 check("key already seen", !all.get(index));
265 all.set(index);
266 }
267
268 all.flip(0, keys.length);
269 check("some keys not visited", all.isEmpty());
270
271 int count = 0;
272 for (String each : map.values()) {
273 count++;
274 }
275
276 check(String.format("value count matches size m%d != k%d", map.size(), keys.length),
277 map.size() == keys.length);
278 }
279
280 private static <T> void testContainsKey(Map<T, T> map, String keys_desc, T[] keys) {
281 for (int i = 0; i < keys.length; i++) {
282 T each = keys[i];
283 check("containsKey: " + keys_desc + "[" + i + "]" + each, map.containsKey(each));
284 }
285 }
286
287 private static <T> void testRemove(Map<T, T> map, String keys_desc, T[] keys) {
288 check(String.format("remove: map expected size m%d != k%d", map.size(), keys.length),
289 map.size() == keys.length);
290
291 for (int i = 0; i < keys.length; i++) {
292 T each = keys[i];
293 check("remove: " + keys_desc + "[" + i + "]" + each, null != map.remove(each));
294 }
295
296 check(String.format("remove: map empty. size=%d", map.size()),
297 (map.size() == 0) && map.isEmpty());
298 }
299
300 private static <T> void testKeysIteratorRemove(Map<T, T> map, String keys_desc, T[] keys) {
301 check(String.format("remove: map expected size m%d != k%d", map.size(), keys.length),
302 map.size() == keys.length);
303
304 Iterator<T> each = map.keySet().iterator();
305 while (each.hasNext()) {
306 T t = each.next();
307 each.remove();
308 check("not removed: " + each, !map.containsKey(t) );
309 }
310
311 check(String.format("remove: map empty. size=%d", map.size()),
312 (map.size() == 0) && map.isEmpty());
313 }
314
315 private static <T> void testValuesIteratorRemove(Map<T, T> map, String keys_desc, T[] keys) {
316 check(String.format("remove: map expected size m%d != k%d", map.size(), keys.length),
317 map.size() == keys.length);
318
319 Iterator<T> each = map.values().iterator();
320 while (each.hasNext()) {
321 T t = each.next();
322 each.remove();
323 check("not removed: " + each, !map.containsValue(t) );
324 }
325
326 check(String.format("remove: map empty. size=%d", map.size()),
327 (map.size() == 0) && map.isEmpty());
328 }
329
330 private static <T> void testEntriesIteratorRemove(Map<T, T> map, String keys_desc, T[] keys) {
331 check(String.format("remove: map expected size m%d != k%d", map.size(), keys.length),
332 map.size() == keys.length);
333
334 Iterator<Map.Entry<T,T>> each = map.entrySet().iterator();
335 while (each.hasNext()) {
336 Map.Entry<T,T> t = each.next();
337 T key = t.getKey();
338 T value = t.getValue();
339 each.remove();
340 check("not removed: " + each, (map instanceof IdentityHashMap) || !map.entrySet().contains(t) );
341 check("not removed: " + each, !map.containsKey(key) );
342 check("not removed: " + each, !map.containsValue(value));
343 }
344
345 check(String.format("remove: map empty. size=%d", map.size()),
346 (map.size() == 0) && map.isEmpty());
347 }
348
349 //--------------------- Infrastructure ---------------------------
350 static volatile int passed = 0, failed = 0;
351
352 static void pass() {
353 passed++;
354 }
355
356 static void fail() {
357 failed++;
358 (new Error("Failure")).printStackTrace(System.err);
359 }
360
361 static void fail(String msg) {
362 failed++;
363 (new Error("Failure: " + msg)).printStackTrace(System.err);
364 }
365
366 static void abort() {
374 }
375
376 static void unexpected(String msg, Throwable t) {
377 System.err.println("Unexpected: " + msg);
378 unexpected(t);
379 }
380
381 static void unexpected(Throwable t) {
382 failed++;
383 t.printStackTrace(System.err);
384 }
385
386 static void check(boolean cond) {
387 if (cond) {
388 pass();
389 } else {
390 fail();
391 }
392 }
393
394 static void check(String desc, boolean cond) {
395 if (cond) {
396 pass();
397 } else {
398 fail(desc);
399 }
400 }
401
402 static void equal(Object x, Object y) {
403 if (Objects.equals(x, y)) {
404 pass();
405 } else {
406 fail(x + " not equal to " + y);
407 }
408 }
409
410 public static void main(String[] args) throws Throwable {
411 Thread.currentThread().setName(Collisions.class.getName());
412 // Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
413 try {
414 realMain(args);
415 } catch (Throwable t) {
416 unexpected(t);
417 }
418
419 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
420 if (failed > 0) {
421 throw new Error("Some tests failed");
|
1 /*
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
181 testContainsKey(map, keys_desc, keys);
182
183 testRemove(map, keys_desc, keys);
184
185 map.clear();
186 testInsertion(map, keys_desc, keys);
187 testKeysIteratorRemove(map, keys_desc, keys);
188
189 map.clear();
190 testInsertion(map, keys_desc, keys);
191 testValuesIteratorRemove(map, keys_desc, keys);
192
193 map.clear();
194 testInsertion(map, keys_desc, keys);
195 testEntriesIteratorRemove(map, keys_desc, keys);
196
197 check(map.isEmpty());
198 }
199
200 private static <T> void testInsertion(Map<T, T> map, String keys_desc, T[] keys) {
201 check(map.size() == 0 && map.isEmpty(), "map empty");
202
203 for (int i = 0; i < keys.length; i++) {
204 check(map.size() == i, "insertion: map expected size m%d != i%d", map.size(), i);
205 check(null == map.put(keys[i], keys[i]), "insertion: put(%s[%d])", keys_desc, i);
206 check(map.containsKey(keys[i]), "insertion: containsKey(%s[%d])", keys_desc, i);
207 check(map.containsValue(keys[i]), "insertion: containsValue(%s[%d])", keys_desc, i);
208 }
209
210 check(map.size() == keys.length, "map expected size m%d != k%d", map.size(), keys.length);
211 }
212
213 private static void testIntegerIteration(Map<HashableInteger, HashableInteger> map, HashableInteger[] keys) {
214 check(map.size() == keys.length, "map expected size m%d != k%d", map.size(), keys.length);
215
216 BitSet all = new BitSet(keys.length);
217 for (Map.Entry<HashableInteger, HashableInteger> each : map.entrySet()) {
218 check(!all.get(each.getKey().value), "Iteration: key already seen");
219 all.set(each.getKey().value);
220 }
221
222 all.flip(0, keys.length);
223 check(all.isEmpty(), "Iteration: some keys not visited");
224
225 for (HashableInteger each : map.keySet()) {
226 check(!all.get(each.value), "Iteration: key already seen");
227 all.set(each.value);
228 }
229
230 all.flip(0, keys.length);
231 check(all.isEmpty(), "Iteration: some keys not visited");
232
233 int count = 0;
234 for (HashableInteger each : map.values()) {
235 count++;
236 }
237
238 check(map.size() == count, "Iteration: value count matches size m%d != c%d", map.size(), count);
239 }
240
241 private static void testStringIteration(Map<String, String> map, String[] keys) {
242 check(map.size() == keys.length, "map expected size m%d != k%d", map.size(), keys.length);
243
244 BitSet all = new BitSet(keys.length);
245 for (Map.Entry<String, String> each : map.entrySet()) {
246 String key = each.getKey();
247 boolean longKey = key.length() > 5;
248 int index = key.hashCode() + (longKey ? keys.length / 2 : 0);
249 check(!all.get(index), "key already seen");
250 all.set(index);
251 }
252
253 all.flip(0, keys.length);
254 check(all.isEmpty(), "some keys not visited");
255
256 for (String each : map.keySet()) {
257 boolean longKey = each.length() > 5;
258 int index = each.hashCode() + (longKey ? keys.length / 2 : 0);
259 check(!all.get(index), "key already seen");
260 all.set(index);
261 }
262
263 all.flip(0, keys.length);
264 check(all.isEmpty(), "some keys not visited");
265
266 int count = 0;
267 for (String each : map.values()) {
268 count++;
269 }
270
271 check(map.size() == keys.length, "value count matches size m%d != k%d", map.size(), keys.length);
272 }
273
274 private static <T> void testContainsKey(Map<T, T> map, String keys_desc, T[] keys) {
275 for (int i = 0; i < keys.length; i++) {
276 T each = keys[i];
277 check(map.containsKey(each), "containsKey: %s[%d]%s", keys_desc, i, each);
278 }
279 }
280
281 private static <T> void testRemove(Map<T, T> map, String keys_desc, T[] keys) {
282 check(map.size() == keys.length, "remove: map expected size m%d != k%d", map.size(), keys.length);
283
284 for (int i = 0; i < keys.length; i++) {
285 T each = keys[i];
286 check(null != map.remove(each), "remove: %s[%d]%s", keys_desc, i, each);
287 }
288
289 check(map.size() == 0 && map.isEmpty(), "remove: map empty. size=%d", map.size());
290 }
291
292 private static <T> void testKeysIteratorRemove(Map<T, T> map, String keys_desc, T[] keys) {
293 check(map.size() == keys.length, "remove: map expected size m%d != k%d", map.size(), keys.length);
294
295 Iterator<T> each = map.keySet().iterator();
296 while (each.hasNext()) {
297 T t = each.next();
298 each.remove();
299 check(!map.containsKey(t), "not removed: %s", each);
300 }
301
302 check(map.size() == 0 && map.isEmpty(), "remove: map empty. size=%d", map.size());
303 }
304
305 private static <T> void testValuesIteratorRemove(Map<T, T> map, String keys_desc, T[] keys) {
306 check(map.size() == keys.length, "remove: map expected size m%d != k%d", map.size(), keys.length);
307
308 Iterator<T> each = map.values().iterator();
309 while (each.hasNext()) {
310 T t = each.next();
311 each.remove();
312 check(!map.containsValue(t), "not removed: %s", each);
313 }
314
315 check(map.size() == 0 && map.isEmpty(), "remove: map empty. size=%d", map.size());
316 }
317
318 private static <T> void testEntriesIteratorRemove(Map<T, T> map, String keys_desc, T[] keys) {
319 check(map.size() == keys.length, "remove: map expected size m%d != k%d", map.size(), keys.length);
320
321 Iterator<Map.Entry<T,T>> each = map.entrySet().iterator();
322 while (each.hasNext()) {
323 Map.Entry<T,T> t = each.next();
324 T key = t.getKey();
325 T value = t.getValue();
326 each.remove();
327 check((map instanceof IdentityHashMap) || !map.entrySet().contains(t), "not removed: %s", each);
328 check(!map.containsKey(key), "not removed: %s", each);
329 check(!map.containsValue(value), "not removed: %s", each);
330 }
331
332 check(map.size() == 0 && map.isEmpty(), "remove: map empty. size=%d", map.size());
333 }
334
335 //--------------------- Infrastructure ---------------------------
336 static volatile int passed = 0, failed = 0;
337
338 static void pass() {
339 passed++;
340 }
341
342 static void fail() {
343 failed++;
344 (new Error("Failure")).printStackTrace(System.err);
345 }
346
347 static void fail(String msg) {
348 failed++;
349 (new Error("Failure: " + msg)).printStackTrace(System.err);
350 }
351
352 static void abort() {
360 }
361
362 static void unexpected(String msg, Throwable t) {
363 System.err.println("Unexpected: " + msg);
364 unexpected(t);
365 }
366
367 static void unexpected(Throwable t) {
368 failed++;
369 t.printStackTrace(System.err);
370 }
371
372 static void check(boolean cond) {
373 if (cond) {
374 pass();
375 } else {
376 fail();
377 }
378 }
379
380 static void check(boolean cond, String desc) {
381 if (cond) {
382 pass();
383 } else {
384 fail(desc);
385 }
386 }
387
388 static void check(boolean cond, String fmt, int i) {
389 if (cond) {
390 pass();
391 } else {
392 fail(String.format(fmt, i));
393 }
394 }
395
396 static void check(boolean cond, String fmt, Object o) {
397 if (cond) {
398 pass();
399 } else {
400 fail(String.format(fmt, o));
401 }
402 }
403
404 static void check(boolean cond, String fmt, int i1, int i2) {
405 if (cond) {
406 pass();
407 } else {
408 fail(String.format(fmt, i1, i2));
409 }
410 }
411
412 static void check(boolean cond, String fmt, String s, int i) {
413 if (cond) {
414 pass();
415 } else {
416 fail(String.format(fmt, s, i));
417 }
418 }
419
420 static void check(boolean cond, String fmt, String s, int i, Object o) {
421 if (cond) {
422 pass();
423 } else {
424 fail(String.format(fmt, s, i, o));
425 }
426 }
427
428 static void equal(Object x, Object y) {
429 if (Objects.equals(x, y)) {
430 pass();
431 } else {
432 fail(x + " not equal to " + y);
433 }
434 }
435
436 public static void main(String[] args) throws Throwable {
437 Thread.currentThread().setName(Collisions.class.getName());
438 // Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
439 try {
440 realMain(args);
441 } catch (Throwable t) {
442 unexpected(t);
443 }
444
445 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
446 if (failed > 0) {
447 throw new Error("Some tests failed");
|