1 /*
   2  * Copyright (c) 2000, 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  */
  23 
  24 /**
  25  * @test
  26  * @bug 4245809 8029795
  27  * @summary Basic test for LinkedHashMap.  (Based on MapBash)
  28  */
  29 
  30 import java.util.*;
  31 import java.util.function.*;
  32 import java.io.*;
  33 
  34 public class Basic {
  35     final static Random rnd = new Random(666);
  36     final static Integer nil = new Integer(0);
  37 
  38     public static void main(String[] args)  throws Exception {
  39         int numItr =  500;
  40         int mapSize = 500;
  41 
  42         // Linked List testk
  43         for (int i=0; i<numItr; i++) {
  44             Map<Integer,Integer> m = new LinkedHashMap();
  45             Integer head = nil;
  46 
  47             for (int j=0; j<mapSize; j++) {
  48                 Integer newHead;
  49                 do {
  50                     newHead = new Integer(rnd.nextInt());
  51                 } while (m.containsKey(newHead));
  52                 m.put(newHead, head);
  53                 head = newHead;
  54             }
  55             if (m.size() != mapSize)
  56                 throw new Exception("Size not as expected.");
  57 
  58             if (new HashMap(m).hashCode() != m.hashCode())
  59                 throw new Exception("Incorrect hashCode computation.");
  60 
  61             Map<Integer,Integer> m2 = new LinkedHashMap(); m2.putAll(m);
  62             m2.values().removeAll(m.keySet());
  63             if (m2.size()!= 1 || !m2.containsValue(nil))
  64                 throw new Exception("Collection views test failed.");
  65 
  66             int j=0;
  67             while (head != nil) {
  68                 if (!m.containsKey(head))
  69                     throw new Exception("Linked list doesn't contain a link.");
  70                 Integer newHead = m.get(head);
  71                 if (newHead == null)
  72                     throw new Exception("Could not retrieve a link.");
  73                 m.remove(head);
  74                 head = newHead;
  75                 j++;
  76             }
  77             if (!m.isEmpty())
  78                 throw new Exception("Map nonempty after removing all links.");
  79             if (j != mapSize)
  80                 throw new Exception("Linked list size not as expected.");
  81         }
  82 
  83         Map<Integer,Integer> m = new LinkedHashMap();
  84         for (int i=0; i<mapSize; i++)
  85             if (m.put(new Integer(i), new Integer(2*i)) != null)
  86                 throw new Exception("put returns non-null value erroenously.");
  87         for (int i=0; i<2*mapSize; i++)
  88             if (m.containsValue(new Integer(i)) != (i%2==0))
  89                 throw new Exception("contains value "+i);
  90         if (m.put(nil, nil) == null)
  91             throw new Exception("put returns a null value erroenously.");
  92         Map<Integer,Integer> m2 = new LinkedHashMap(); m2.putAll(m);
  93         if (!m.equals(m2))
  94             throw new Exception("Clone not equal to original. (1)");
  95         if (!m2.equals(m))
  96             throw new Exception("Clone not equal to original. (2)");
  97         Set<Map.Entry<Integer,Integer>> s = m.entrySet(), s2 = m2.entrySet();
  98         if (!s.equals(s2))
  99             throw new Exception("Clone not equal to original. (3)");
 100         if (!s2.equals(s))
 101             throw new Exception("Clone not equal to original. (4)");
 102         if (!s.containsAll(s2))
 103             throw new Exception("Original doesn't contain clone!");
 104         if (!s2.containsAll(s))
 105             throw new Exception("Clone doesn't contain original!");
 106 
 107         m2 = serClone(m);
 108         if (!m.equals(m2))
 109             throw new Exception("Serialize Clone not equal to original. (1)");
 110         if (!m2.equals(m))
 111             throw new Exception("Serialize Clone not equal to original. (2)");
 112         s = m.entrySet(); s2 = m2.entrySet();
 113         if (!s.equals(s2))
 114             throw new Exception("Serialize Clone not equal to original. (3)");
 115         if (!s2.equals(s))
 116             throw new Exception("Serialize Clone not equal to original. (4)");
 117         if (!s.containsAll(s2))
 118             throw new Exception("Original doesn't contain Serialize clone!");
 119         if (!s2.containsAll(s))
 120             throw new Exception("Serialize Clone doesn't contain original!");
 121 
 122         s2.removeAll(s);
 123         if (!m2.isEmpty())
 124             throw new Exception("entrySet().removeAll failed.");
 125 
 126         m2.putAll(m);
 127         m2.clear();
 128         if (!m2.isEmpty())
 129             throw new Exception("clear failed.");
 130 
 131         Iterator it = m.entrySet().iterator();
 132         while(it.hasNext()) {
 133             it.next();
 134             it.remove();
 135         }
 136         if (!m.isEmpty())
 137             throw new Exception("Iterator.remove() failed");
 138 
 139         // Test ordering properties with insert order
 140         m = new LinkedHashMap();
 141         List<Integer> l = new ArrayList(mapSize);
 142         for (int i=0; i<mapSize; i++) {
 143             Integer x = new Integer(i);
 144             m.put(x, x);
 145             l.add(x);
 146         }
 147         if (!new ArrayList(m.keySet()).equals(l))
 148             throw new Exception("Insertion order not preserved.");
 149         for (int i=mapSize-1; i>=0; i--) {
 150             Integer x = (Integer) l.get(i);
 151             if (!m.get(x).equals(x))
 152                 throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);
 153         }
 154         if (!new ArrayList(m.keySet()).equals(l))
 155             throw new Exception("Insertion order not preserved after read.");
 156 
 157         for (int i=mapSize-1; i>=0; i--) {
 158             Integer x = (Integer) l.get(i);
 159             m.put(x, x);
 160         }
 161         if (!new ArrayList(m.keySet()).equals(l))
 162             throw new Exception("Insert order not preserved after reinsert.");
 163 
 164         m2 = (Map) ((LinkedHashMap)m).clone();
 165         if (!m.equals(m2))
 166             throw new Exception("Insert-order Map != clone.");
 167 
 168         List<Integer> l2 = new ArrayList(l);
 169         Collections.shuffle(l2);
 170         for (int i=0; i<mapSize; i++) {
 171             Integer x = (Integer) l2.get(i);
 172             if (!m2.get(x).equals(x))
 173               throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x);
 174         }
 175         if (!new ArrayList(m2.keySet()).equals(l))
 176             throw new Exception("Clone: altered by read.");
 177 
 178         // Test ordering properties with access order
 179         m = new LinkedHashMap(2*mapSize, .75f, true);
 180         for (int i=0; i<mapSize; i++) {
 181             Integer x = new Integer(i);
 182             m.put(x, x);
 183         }
 184         if (!new ArrayList(m.keySet()).equals(l))
 185             throw new Exception("Insertion order not properly preserved.");
 186 
 187         for (int i=0; i<mapSize; i++) {
 188             Integer x = (Integer) l2.get(i);
 189             if (!m.get(x).equals(x))
 190                 throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);
 191         }
 192         if (!new ArrayList(m.keySet()).equals(l2))
 193             throw new Exception("Insert order not properly altered by read.");
 194 
 195         for (int i=0; i<mapSize; i++) {
 196             Integer x = (Integer) l2.get(i);
 197             if (!m.getOrDefault(x, new Integer(i + 1000)).equals(x))
 198                 throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);
 199         }
 200         if (!new ArrayList(m.keySet()).equals(l2))
 201             throw new Exception("Insert order not properly altered by read.");
 202 
 203         for (int i=0; i<mapSize; i++) {
 204             Integer x = (Integer) l2.get(i);
 205             if (!m.replace(x, x).equals(x))
 206                 throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);
 207         }
 208         if (!new ArrayList(m.keySet()).equals(l2))
 209             throw new Exception("Insert order not properly altered by replace.");
 210 
 211         for (int i=0; i<mapSize; i++) {
 212             Integer x = (Integer) l2.get(i);
 213             if (!m.replace(x, x, x))
 214                 throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);
 215         }
 216         if (!new ArrayList(m.keySet()).equals(l2))
 217             throw new Exception("Insert order not properly altered by replace.");
 218 
 219         BiFunction<Integer,Integer,Integer> f = (Integer y, Integer z) -> {
 220             if (!Objects.equals(y,z))
 221                 throw new RuntimeException("unequal " + y + "," + z);
 222             return new Integer(z);
 223         };
 224 
 225         for (int i=0; i<mapSize; i++) {
 226             Integer x = (Integer) l2.get(i);
 227             if (!x.equals(m.merge(x, x, f)))
 228                 throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);
 229         }
 230         if (!new ArrayList(m.keySet()).equals(l2))
 231             throw new Exception("Insert order not properly altered by replace.");
 232 
 233         for (int i=0; i<mapSize; i++) {
 234             Integer x = (Integer) l2.get(i);
 235             if (!x.equals(m.compute(x, f)))
 236                 throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);
 237         }
 238         if (!new ArrayList(m.keySet()).equals(l2))
 239             throw new Exception("Insert order not properly altered by replace.");
 240 
 241         for (int i=0; i<mapSize; i++) {
 242             Integer x = (Integer) l2.get(i);
 243             if(!x.equals(m.remove(x)))
 244               throw new Exception("Missing key: "+i+", "+x);
 245             if (!x.equals(m.computeIfAbsent(x, Integer::valueOf)))
 246                 throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);
 247         }
 248         if (!new ArrayList(m.keySet()).equals(l2))
 249             throw new Exception("Insert order not properly altered by replace.");
 250 
 251         for (int i=0; i<mapSize; i++) {
 252             Integer x = (Integer) l2.get(i);
 253             if (!x.equals(m.computeIfPresent(x, f)))
 254                 throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);
 255         }
 256         if (!new ArrayList(m.keySet()).equals(l2))
 257             throw new Exception("Insert order not properly altered by replace.");
 258 
 259         for (int i=0; i<mapSize; i++) {
 260             Integer x = new Integer(i);
 261             m.put(x, x);
 262         }
 263         if (!new ArrayList(m.keySet()).equals(l))
 264             throw new Exception("Insertion order not altered by reinsert.");
 265 
 266         m2 = (Map) ((LinkedHashMap)m).clone();
 267         if (!m.equals(m2))
 268             throw new Exception("Access-order Map != clone.");
 269         for (int i=0; i<mapSize; i++) {
 270             Integer x = (Integer) l.get(i);
 271             if (!m2.get(x).equals(x))
 272               throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x);
 273         }
 274         if (!new ArrayList(m2.keySet()).equals(l))
 275             throw new Exception("Clone: order not properly altered by read.");
 276 
 277         System.err.println("Success.");
 278     }
 279 
 280     private static Map serClone(Map m) {
 281         Map result = null;
 282         try {
 283             // Serialize
 284             ByteArrayOutputStream bos = new ByteArrayOutputStream();
 285             ObjectOutputStream out = new ObjectOutputStream(bos);
 286             out.writeObject(m);
 287             out.flush();
 288 
 289             // Deserialize
 290             ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
 291             out.close();
 292             ObjectInputStream in = new ObjectInputStream(bis);
 293             result = (Map)in.readObject();
 294             in.close();
 295         } catch(Exception e) {
 296             e.printStackTrace();
 297         }
 298         return result;
 299     }
 300 }