0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4904067 5023830
0N/A * @summary Unit test for Collections.checkedMap
0N/A * @author Josh Bloch
0N/A */
0N/A
0N/Aimport java.util.*;
0N/A
0N/Apublic class CheckedMapBash {
0N/A static Random rnd = new Random();
0N/A static Object nil = new Integer(0);
0N/A
0N/A public static void main(String[] args) {
0N/A int numItr = 100;
0N/A int mapSize = 100;
0N/A
0N/A // Linked List test
0N/A for (int i=0; i<numItr; i++) {
0N/A Map m = newMap();
0N/A Object head = nil;
0N/A
0N/A for (int j=0; j<mapSize; j++) {
0N/A Object newHead;
0N/A do {
0N/A newHead = new Integer(rnd.nextInt());
0N/A } while (m.containsKey(newHead));
0N/A m.put(newHead, head);
0N/A head = newHead;
0N/A }
0N/A if (m.size() != mapSize)
0N/A fail("Size not as expected.");
0N/A
0N/A {
0N/A HashMap hm = new HashMap(m);
0N/A if (! (hm.hashCode() == m.hashCode() &&
0N/A hm.entrySet().hashCode() == m.entrySet().hashCode() &&
0N/A hm.keySet().hashCode() == m.keySet().hashCode()))
0N/A fail("Incorrect hashCode computation.");
0N/A
0N/A if (! (hm.equals(m) &&
0N/A hm.entrySet().equals(m.entrySet()) &&
0N/A hm.keySet().equals(m.keySet()) &&
0N/A m.equals(hm) &&
0N/A m.entrySet().equals(hm.entrySet()) &&
0N/A m.keySet().equals(hm.keySet())))
0N/A fail("Incorrect equals computation.");
0N/A }
0N/A
0N/A Map m2 = newMap(); m2.putAll(m);
0N/A m2.values().removeAll(m.keySet());
0N/A if (m2.size()!= 1 || !m2.containsValue(nil))
0N/A fail("Collection views test failed.");
0N/A
0N/A int j=0;
0N/A while (head != nil) {
0N/A if (!m.containsKey(head))
0N/A fail("Linked list doesn't contain a link.");
0N/A Object newHead = m.get(head);
0N/A if (newHead == null)
0N/A fail("Could not retrieve a link.");
0N/A m.remove(head);
0N/A head = newHead;
0N/A j++;
0N/A }
0N/A if (!m.isEmpty())
0N/A fail("Map nonempty after removing all links.");
0N/A if (j != mapSize)
0N/A fail("Linked list size not as expected.");
0N/A }
0N/A
0N/A Map m = newMap();
0N/A for (int i=0; i<mapSize; i++)
0N/A if (m.put(new Integer(i), new Integer(2*i)) != null)
0N/A fail("put returns a non-null value erroenously.");
0N/A for (int i=0; i<2*mapSize; i++)
0N/A if (m.containsValue(new Integer(i)) != (i%2==0))
0N/A fail("contains value "+i);
0N/A if (m.put(nil, nil) == null)
0N/A fail("put returns a null value erroenously.");
0N/A Map m2 = newMap(); m2.putAll(m);
0N/A if (!m.equals(m2))
0N/A fail("Clone not equal to original. (1)");
0N/A if (!m2.equals(m))
0N/A fail("Clone not equal to original. (2)");
0N/A Set s = m.entrySet(), s2 = m2.entrySet();
0N/A if (!s.equals(s2))
0N/A fail("Clone not equal to original. (3)");
0N/A if (!s2.equals(s))
0N/A fail("Clone not equal to original. (4)");
0N/A if (!s.containsAll(s2))
0N/A fail("Original doesn't contain clone!");
0N/A if (!s2.containsAll(s))
0N/A fail("Clone doesn't contain original!");
0N/A
0N/A s2.removeAll(s);
0N/A if (!m2.isEmpty())
0N/A fail("entrySet().removeAll failed.");
0N/A
0N/A m2.putAll(m);
0N/A m2.clear();
0N/A if (!m2.isEmpty())
0N/A fail("clear failed.");
0N/A
0N/A Iterator i = m.entrySet().iterator();
0N/A while(i.hasNext()) {
0N/A i.next();
0N/A i.remove();
0N/A }
0N/A if (!m.isEmpty())
0N/A fail("Iterator.remove() failed");
0N/A }
0N/A
0N/A static Map newMap() {
0N/A Map m = Collections.checkedMap(new HashMap(),
0N/A Integer.class, Integer.class);
0N/A
0N/A if (!m.isEmpty())
0N/A fail("New instance non empty.");
0N/A return m;
0N/A }
0N/A
0N/A static void fail(String s) {
0N/A throw new RuntimeException(s);
0N/A }
0N/A}