Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/LinkedHashMap/Basic.java
38811 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.util.*;30import java.util.function.*;31import java.io.*;3233public class Basic {34final static Random rnd = new Random(666);35final static Integer nil = new Integer(0);3637public static void main(String[] args) throws Exception {38int numItr = 500;39int mapSize = 500;4041// Linked List testk42for (int i=0; i<numItr; i++) {43Map<Integer,Integer> m = new LinkedHashMap();44Integer head = nil;4546for (int j=0; j<mapSize; j++) {47Integer newHead;48do {49newHead = new Integer(rnd.nextInt());50} while (m.containsKey(newHead));51m.put(newHead, head);52head = newHead;53}54if (m.size() != mapSize)55throw new Exception("Size not as expected.");5657if (new HashMap(m).hashCode() != m.hashCode())58throw new Exception("Incorrect hashCode computation.");5960Map<Integer,Integer> m2 = new LinkedHashMap(); m2.putAll(m);61m2.values().removeAll(m.keySet());62if (m2.size()!= 1 || !m2.containsValue(nil))63throw new Exception("Collection views test failed.");6465int j=0;66while (head != nil) {67if (!m.containsKey(head))68throw new Exception("Linked list doesn't contain a link.");69Integer newHead = m.get(head);70if (newHead == null)71throw new Exception("Could not retrieve a link.");72m.remove(head);73head = newHead;74j++;75}76if (!m.isEmpty())77throw new Exception("Map nonempty after removing all links.");78if (j != mapSize)79throw new Exception("Linked list size not as expected.");80}8182Map<Integer,Integer> m = new LinkedHashMap();83for (int i=0; i<mapSize; i++)84if (m.put(new Integer(i), new Integer(2*i)) != null)85throw new Exception("put returns non-null value erroenously.");86for (int i=0; i<2*mapSize; i++)87if (m.containsValue(new Integer(i)) != (i%2==0))88throw new Exception("contains value "+i);89if (m.put(nil, nil) == null)90throw new Exception("put returns a null value erroenously.");91Map<Integer,Integer> m2 = new LinkedHashMap(); m2.putAll(m);92if (!m.equals(m2))93throw new Exception("Clone not equal to original. (1)");94if (!m2.equals(m))95throw new Exception("Clone not equal to original. (2)");96Set<Map.Entry<Integer,Integer>> s = m.entrySet(), s2 = m2.entrySet();97if (!s.equals(s2))98throw new Exception("Clone not equal to original. (3)");99if (!s2.equals(s))100throw new Exception("Clone not equal to original. (4)");101if (!s.containsAll(s2))102throw new Exception("Original doesn't contain clone!");103if (!s2.containsAll(s))104throw new Exception("Clone doesn't contain original!");105106m2 = serClone(m);107if (!m.equals(m2))108throw new Exception("Serialize Clone not equal to original. (1)");109if (!m2.equals(m))110throw new Exception("Serialize Clone not equal to original. (2)");111s = m.entrySet(); s2 = m2.entrySet();112if (!s.equals(s2))113throw new Exception("Serialize Clone not equal to original. (3)");114if (!s2.equals(s))115throw new Exception("Serialize Clone not equal to original. (4)");116if (!s.containsAll(s2))117throw new Exception("Original doesn't contain Serialize clone!");118if (!s2.containsAll(s))119throw new Exception("Serialize Clone doesn't contain original!");120121s2.removeAll(s);122if (!m2.isEmpty())123throw new Exception("entrySet().removeAll failed.");124125m2.putAll(m);126m2.clear();127if (!m2.isEmpty())128throw new Exception("clear failed.");129130Iterator it = m.entrySet().iterator();131while(it.hasNext()) {132it.next();133it.remove();134}135if (!m.isEmpty())136throw new Exception("Iterator.remove() failed");137138// Test ordering properties with insert order139m = new LinkedHashMap();140List<Integer> l = new ArrayList(mapSize);141for (int i=0; i<mapSize; i++) {142Integer x = new Integer(i);143m.put(x, x);144l.add(x);145}146if (!new ArrayList(m.keySet()).equals(l))147throw new Exception("Insertion order not preserved.");148for (int i=mapSize-1; i>=0; i--) {149Integer x = (Integer) l.get(i);150if (!m.get(x).equals(x))151throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);152}153if (!new ArrayList(m.keySet()).equals(l))154throw new Exception("Insertion order not preserved after read.");155156for (int i=mapSize-1; i>=0; i--) {157Integer x = (Integer) l.get(i);158m.put(x, x);159}160if (!new ArrayList(m.keySet()).equals(l))161throw new Exception("Insert order not preserved after reinsert.");162163m2 = (Map) ((LinkedHashMap)m).clone();164if (!m.equals(m2))165throw new Exception("Insert-order Map != clone.");166167List<Integer> l2 = new ArrayList(l);168Collections.shuffle(l2);169for (int i=0; i<mapSize; i++) {170Integer x = (Integer) l2.get(i);171if (!m2.get(x).equals(x))172throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x);173}174if (!new ArrayList(m2.keySet()).equals(l))175throw new Exception("Clone: altered by read.");176177// Test ordering properties with access order178m = new LinkedHashMap(2*mapSize, .75f, true);179for (int i=0; i<mapSize; i++) {180Integer x = new Integer(i);181m.put(x, x);182}183if (!new ArrayList(m.keySet()).equals(l))184throw new Exception("Insertion order not properly preserved.");185186for (int i=0; i<mapSize; i++) {187Integer x = (Integer) l2.get(i);188if (!m.get(x).equals(x))189throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);190}191if (!new ArrayList(m.keySet()).equals(l2))192throw new Exception("Insert order not properly altered by read.");193194for (int i=0; i<mapSize; i++) {195Integer x = (Integer) l2.get(i);196if (!m.getOrDefault(x, new Integer(i + 1000)).equals(x))197throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);198}199if (!new ArrayList(m.keySet()).equals(l2))200throw new Exception("Insert order not properly altered by read.");201202for (int i=0; i<mapSize; i++) {203Integer x = (Integer) l2.get(i);204if (!m.replace(x, x).equals(x))205throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);206}207if (!new ArrayList(m.keySet()).equals(l2))208throw new Exception("Insert order not properly altered by replace.");209210for (int i=0; i<mapSize; i++) {211Integer x = (Integer) l2.get(i);212if (!m.replace(x, x, x))213throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);214}215if (!new ArrayList(m.keySet()).equals(l2))216throw new Exception("Insert order not properly altered by replace.");217218BiFunction<Integer,Integer,Integer> f = (Integer y, Integer z) -> {219if (!Objects.equals(y,z))220throw new RuntimeException("unequal " + y + "," + z);221return new Integer(z);222};223224for (int i=0; i<mapSize; i++) {225Integer x = (Integer) l2.get(i);226if (!x.equals(m.merge(x, x, f)))227throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);228}229if (!new ArrayList(m.keySet()).equals(l2))230throw new Exception("Insert order not properly altered by replace.");231232for (int i=0; i<mapSize; i++) {233Integer x = (Integer) l2.get(i);234if (!x.equals(m.compute(x, f)))235throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);236}237if (!new ArrayList(m.keySet()).equals(l2))238throw new Exception("Insert order not properly altered by replace.");239240for (int i=0; i<mapSize; i++) {241Integer x = (Integer) l2.get(i);242if(!x.equals(m.remove(x)))243throw new Exception("Missing key: "+i+", "+x);244if (!x.equals(m.computeIfAbsent(x, Integer::valueOf)))245throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);246}247if (!new ArrayList(m.keySet()).equals(l2))248throw new Exception("Insert order not properly altered by replace.");249250for (int i=0; i<mapSize; i++) {251Integer x = (Integer) l2.get(i);252if (!x.equals(m.computeIfPresent(x, f)))253throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);254}255if (!new ArrayList(m.keySet()).equals(l2))256throw new Exception("Insert order not properly altered by replace.");257258for (int i=0; i<mapSize; i++) {259Integer x = new Integer(i);260m.put(x, x);261}262if (!new ArrayList(m.keySet()).equals(l))263throw new Exception("Insertion order not altered by reinsert.");264265m2 = (Map) ((LinkedHashMap)m).clone();266if (!m.equals(m2))267throw new Exception("Access-order Map != clone.");268for (int i=0; i<mapSize; i++) {269Integer x = (Integer) l.get(i);270if (!m2.get(x).equals(x))271throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x);272}273if (!new ArrayList(m2.keySet()).equals(l))274throw new Exception("Clone: order not properly altered by read.");275276System.err.println("Success.");277}278279private static Map serClone(Map m) {280Map result = null;281try {282// Serialize283ByteArrayOutputStream bos = new ByteArrayOutputStream();284ObjectOutputStream out = new ObjectOutputStream(bos);285out.writeObject(m);286out.flush();287288// Deserialize289ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());290out.close();291ObjectInputStream in = new ObjectInputStream(bis);292result = (Map)in.readObject();293in.close();294} catch(Exception e) {295e.printStackTrace();296}297return result;298}299}300301302