Path: blob/master/test/jdk/java/util/LinkedHashMap/Basic.java
41149 views
/*1* Copyright (c) 2000, 2013, 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 4245809 802979526* @summary Basic test for LinkedHashMap. (Based on MapBash)27*/2829import java.io.ByteArrayInputStream;30import java.io.ByteArrayOutputStream;31import java.io.ObjectInputStream;32import java.io.ObjectOutputStream;33import java.util.ArrayList;34import java.util.Collections;35import java.util.HashMap;36import java.util.Iterator;37import java.util.LinkedHashMap;38import java.util.List;39import java.util.Map;40import java.util.Objects;41import java.util.Random;42import java.util.Set;43import java.util.function.BiFunction;4445public class Basic {46static final Random rnd = new Random(666);47static final Integer nil = new Integer(0);4849public static void main(String[] args) throws Exception {50int numItr = 500;51int mapSize = 500;5253// Linked List testk54for (int i=0; i<numItr; i++) {55Map<Integer,Integer> m = new LinkedHashMap();56Integer head = nil;5758for (int j=0; j<mapSize; j++) {59Integer newHead;60do {61newHead = new Integer(rnd.nextInt());62} while (m.containsKey(newHead));63m.put(newHead, head);64head = newHead;65}66if (m.size() != mapSize)67throw new Exception("Size not as expected.");6869if (new HashMap(m).hashCode() != m.hashCode())70throw new Exception("Incorrect hashCode computation.");7172Map<Integer,Integer> m2 = new LinkedHashMap(); m2.putAll(m);73m2.values().removeAll(m.keySet());74if (m2.size()!= 1 || !m2.containsValue(nil))75throw new Exception("Collection views test failed.");7677int j=0;78while (head != nil) {79if (!m.containsKey(head))80throw new Exception("Linked list doesn't contain a link.");81Integer newHead = m.get(head);82if (newHead == null)83throw new Exception("Could not retrieve a link.");84m.remove(head);85head = newHead;86j++;87}88if (!m.isEmpty())89throw new Exception("Map nonempty after removing all links.");90if (j != mapSize)91throw new Exception("Linked list size not as expected.");92}9394Map<Integer,Integer> m = new LinkedHashMap();95for (int i=0; i<mapSize; i++)96if (m.put(new Integer(i), new Integer(2*i)) != null)97throw new Exception("put returns non-null value erroneously.");98for (int i=0; i<2*mapSize; i++)99if (m.containsValue(new Integer(i)) != (i%2==0))100throw new Exception("contains value "+i);101if (m.put(nil, nil) == null)102throw new Exception("put returns a null value erroneously.");103Map<Integer,Integer> m2 = new LinkedHashMap(); m2.putAll(m);104if (!m.equals(m2))105throw new Exception("Clone not equal to original. (1)");106if (!m2.equals(m))107throw new Exception("Clone not equal to original. (2)");108Set<Map.Entry<Integer,Integer>> s = m.entrySet(), s2 = m2.entrySet();109if (!s.equals(s2))110throw new Exception("Clone not equal to original. (3)");111if (!s2.equals(s))112throw new Exception("Clone not equal to original. (4)");113if (!s.containsAll(s2))114throw new Exception("Original doesn't contain clone!");115if (!s2.containsAll(s))116throw new Exception("Clone doesn't contain original!");117118m2 = serClone(m);119if (!m.equals(m2))120throw new Exception("Serialize Clone not equal to original. (1)");121if (!m2.equals(m))122throw new Exception("Serialize Clone not equal to original. (2)");123s = m.entrySet(); s2 = m2.entrySet();124if (!s.equals(s2))125throw new Exception("Serialize Clone not equal to original. (3)");126if (!s2.equals(s))127throw new Exception("Serialize Clone not equal to original. (4)");128if (!s.containsAll(s2))129throw new Exception("Original doesn't contain Serialize clone!");130if (!s2.containsAll(s))131throw new Exception("Serialize Clone doesn't contain original!");132133s2.removeAll(s);134if (!m2.isEmpty())135throw new Exception("entrySet().removeAll failed.");136137m2.putAll(m);138m2.clear();139if (!m2.isEmpty())140throw new Exception("clear failed.");141142Iterator it = m.entrySet().iterator();143while (it.hasNext()) {144it.next();145it.remove();146}147if (!m.isEmpty())148throw new Exception("Iterator.remove() failed");149150// Test ordering properties with insert order151m = new LinkedHashMap();152List<Integer> l = new ArrayList(mapSize);153for (int i=0; i<mapSize; i++) {154Integer x = new Integer(i);155m.put(x, x);156l.add(x);157}158if (!new ArrayList(m.keySet()).equals(l))159throw new Exception("Insertion order not preserved.");160for (int i=mapSize-1; i>=0; i--) {161Integer x = (Integer) l.get(i);162if (!m.get(x).equals(x))163throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);164}165if (!new ArrayList(m.keySet()).equals(l))166throw new Exception("Insertion order not preserved after read.");167168for (int i=mapSize-1; i>=0; i--) {169Integer x = (Integer) l.get(i);170m.put(x, x);171}172if (!new ArrayList(m.keySet()).equals(l))173throw new Exception("Insert order not preserved after reinsert.");174175m2 = (Map) ((LinkedHashMap)m).clone();176if (!m.equals(m2))177throw new Exception("Insert-order Map != clone.");178179List<Integer> l2 = new ArrayList(l);180Collections.shuffle(l2);181for (int i=0; i<mapSize; i++) {182Integer x = (Integer) l2.get(i);183if (!m2.get(x).equals(x))184throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x);185}186if (!new ArrayList(m2.keySet()).equals(l))187throw new Exception("Clone: altered by read.");188189// Test ordering properties with access order190m = new LinkedHashMap(2*mapSize, .75f, true);191for (int i=0; i<mapSize; i++) {192Integer x = new Integer(i);193m.put(x, x);194}195if (!new ArrayList(m.keySet()).equals(l))196throw new Exception("Insertion order not properly preserved.");197198for (int i=0; i<mapSize; i++) {199Integer x = (Integer) l2.get(i);200if (!m.get(x).equals(x))201throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);202}203if (!new ArrayList(m.keySet()).equals(l2))204throw new Exception("Insert order not properly altered by read.");205206for (int i=0; i<mapSize; i++) {207Integer x = (Integer) l2.get(i);208if (!m.getOrDefault(x, new Integer(i + 1000)).equals(x))209throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);210}211if (!new ArrayList(m.keySet()).equals(l2))212throw new Exception("Insert order not properly altered by read.");213214for (int i=0; i<mapSize; i++) {215Integer x = (Integer) l2.get(i);216if (!m.replace(x, x).equals(x))217throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);218}219if (!new ArrayList(m.keySet()).equals(l2))220throw new Exception("Insert order not properly altered by replace.");221222for (int i=0; i<mapSize; i++) {223Integer x = (Integer) l2.get(i);224if (!m.replace(x, x, x))225throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);226}227if (!new ArrayList(m.keySet()).equals(l2))228throw new Exception("Insert order not properly altered by replace.");229230BiFunction<Integer,Integer,Integer> f = (Integer y, Integer z) -> {231if (!Objects.equals(y,z))232throw new RuntimeException("unequal " + y + "," + z);233return new Integer(z);234};235236for (int i=0; i<mapSize; i++) {237Integer x = (Integer) l2.get(i);238if (!x.equals(m.merge(x, x, f)))239throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);240}241if (!new ArrayList(m.keySet()).equals(l2))242throw new Exception("Insert order not properly altered by replace.");243244for (int i=0; i<mapSize; i++) {245Integer x = (Integer) l2.get(i);246if (!x.equals(m.compute(x, f)))247throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);248}249if (!new ArrayList(m.keySet()).equals(l2))250throw new Exception("Insert order not properly altered by replace.");251252for (int i=0; i<mapSize; i++) {253Integer x = (Integer) l2.get(i);254if (!x.equals(m.remove(x)))255throw new Exception("Missing key: "+i+", "+x);256if (!x.equals(m.computeIfAbsent(x, Integer::valueOf)))257throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);258}259if (!new ArrayList(m.keySet()).equals(l2))260throw new Exception("Insert order not properly altered by replace.");261262for (int i=0; i<mapSize; i++) {263Integer x = (Integer) l2.get(i);264if (!x.equals(m.computeIfPresent(x, f)))265throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);266}267if (!new ArrayList(m.keySet()).equals(l2))268throw new Exception("Insert order not properly altered by replace.");269270for (int i=0; i<mapSize; i++) {271Integer x = new Integer(i);272m.put(x, x);273}274if (!new ArrayList(m.keySet()).equals(l))275throw new Exception("Insertion order not altered by reinsert.");276277m2 = (Map) ((LinkedHashMap)m).clone();278if (!m.equals(m2))279throw new Exception("Access-order Map != clone.");280for (int i=0; i<mapSize; i++) {281Integer x = (Integer) l.get(i);282if (!m2.get(x).equals(x))283throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x);284}285if (!new ArrayList(m2.keySet()).equals(l))286throw new Exception("Clone: order not properly altered by read.");287288System.err.println("Success.");289}290291private static Map serClone(Map m) {292Map result = null;293try {294// Serialize295ByteArrayOutputStream bos = new ByteArrayOutputStream();296ObjectOutputStream out = new ObjectOutputStream(bos);297out.writeObject(m);298out.flush();299300// Deserialize301ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());302out.close();303ObjectInputStream in = new ObjectInputStream(bis);304result = (Map)in.readObject();305in.close();306} catch (Exception e) {307e.printStackTrace();308}309return result;310}311}312313314