Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Map/Get.java
38812 views
/*1* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 630682926* @summary Verify assertions in get() javadocs27* @author Martin Buchholz28*/2930import java.io.*;31import java.util.*;32import java.util.concurrent.*;33import java.util.concurrent.atomic.*;3435public class Get {3637private static void realMain(String[] args) throws Throwable {38testMap(new Hashtable<Character,Boolean>());39testMap(new HashMap<Character,Boolean>());40testMap(new IdentityHashMap<Character,Boolean>());41testMap(new LinkedHashMap<Character,Boolean>());42testMap(new ConcurrentHashMap<Character,Boolean>());43testMap(new WeakHashMap<Character,Boolean>());44testMap(new TreeMap<Character,Boolean>());45testMap(new ConcurrentSkipListMap<Character,Boolean>());46}4748private static void put(Map<Character,Boolean> m,49Character key, Boolean value,50Boolean oldValue) {51if (oldValue != null) {52check("containsValue(oldValue)", m.containsValue(oldValue));53check("values.contains(oldValue)", m.values().contains(oldValue));54}55equal(m.put(key, value), oldValue);56equal(m.get(key), value);57check("containsKey", m.containsKey(key));58check("keySet.contains", m.keySet().contains(key));59check("containsValue", m.containsValue(value));60check("values.contains", m.values().contains(value));61check("!isEmpty", ! m.isEmpty());62}6364private static void testMap(Map<Character,Boolean> m) {65// We verify following assertions in get(Object) method javadocs66boolean permitsNullKeys = (! (m instanceof ConcurrentMap ||67m instanceof Hashtable ||68m instanceof SortedMap));69boolean permitsNullValues = (! (m instanceof ConcurrentMap ||70m instanceof Hashtable));71boolean usesIdentity = m instanceof IdentityHashMap;7273System.err.println(m.getClass());74put(m, 'A', true, null);75put(m, 'A', false, true); // Guaranteed identical by JLS76put(m, 'B', true, null);77put(m, new Character('A'), false, usesIdentity ? null : false);78if (permitsNullKeys) {79try {80put(m, null, true, null);81put(m, null, false, true);82}83catch (Throwable t) { unexpected(m.getClass().getName(), t); }84} else {85try { m.get(null); fail(m.getClass().getName() + " did not reject null key"); }86catch (NullPointerException e) {}87catch (Throwable t) { unexpected(m.getClass().getName(), t); }8889try { m.put(null, true); fail(m.getClass().getName() + " did not reject null key"); }90catch (NullPointerException e) {}91catch (Throwable t) { unexpected(m.getClass().getName(), t); }92}93if (permitsNullValues) {94try {95put(m, 'C', null, null);96put(m, 'C', true, null);97put(m, 'C', null, true);98}99catch (Throwable t) { unexpected(m.getClass().getName(), t); }100} else {101try { m.put('A', null); fail(m.getClass().getName() + " did not reject null key"); }102catch (NullPointerException e) {}103catch (Throwable t) { unexpected(m.getClass().getName(), t); }104105try { m.put('C', null); fail(m.getClass().getName() + " did not reject null key"); }106catch (NullPointerException e) {}107catch (Throwable t) { unexpected(m.getClass().getName(), t); }108}109}110111//--------------------- Infrastructure ---------------------------112static volatile int passed = 0, failed = 0;113static void pass() { passed++; }114static void fail() { failed++; (new Error("Failure")).printStackTrace(System.err); }115static void fail(String msg) { failed++; (new Error("Failure: " + msg)).printStackTrace(System.err); }116static void unexpected(String msg, Throwable t) { System.err.println("Unexpected: " + msg); unexpected(t); }117static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); }118static void check(boolean cond) { if (cond) pass(); else fail(); }119static void check(String desc, boolean cond) { if (cond) pass(); else fail(desc); }120static void equal(Object x, Object y) {121if(Objects.equals(x,y)) pass(); else fail(x + " not equal to " + y);122}123124public static void main(String[] args) throws Throwable {125try { realMain(args); } catch (Throwable t) { unexpected(t); }126127System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);128if (failed > 0) throw new Error("Some tests failed");129}130}131132133