1 /* 2 * Copyright (c) 2000, 2013, 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 package test.java.util.LinkedHashMap; 30 31 import java.io.ByteArrayInputStream; 32 import java.io.ByteArrayOutputStream; 33 import java.io.ObjectInputStream; 34 import java.io.ObjectOutputStream; 35 import java.util.ArrayList; 36 import java.util.Collections; 37 import java.util.HashMap; 38 import java.util.Iterator; 39 import java.util.LinkedHashMap; 40 import java.util.List; 41 import java.util.Map; 42 import java.util.Objects; 43 import java.util.Random; 44 import java.util.Set; 45 import java.util.function.BiFunction; 46 47 import org.testng.Assert; 48 import org.testng.annotations.Test; 49 50 public class Basic { 51 static final Random rnd = new Random(666); 52 static final Integer nil = new Integer(0); 53 54 @Test testBasic()55 public void testBasic() { 56 int numItr = 500; 57 int mapSize = 500; 58 59 // Linked List testk 60 for (int i=0; i<numItr; i++) { 61 Map<Integer,Integer> m = new LinkedHashMap(); 62 Integer head = nil; 63 64 for (int j=0; j<mapSize; j++) { 65 Integer newHead; 66 do { 67 newHead = new Integer(rnd.nextInt()); 68 } while (m.containsKey(newHead)); 69 m.put(newHead, head); 70 head = newHead; 71 } 72 Assert.assertEquals(m.size(), mapSize); 73 74 Assert.assertEquals(new HashMap(m).hashCode(), m.hashCode()); 75 76 Map<Integer,Integer> m2 = new LinkedHashMap(); m2.putAll(m); 77 m2.values().removeAll(m.keySet()); 78 Assert.assertFalse(m2.size()!= 1 || !m2.containsValue(nil)); 79 80 int j=0; 81 while (head != nil) { 82 Assert.assertTrue(m.containsKey(head)); 83 Integer newHead = m.get(head); 84 Assert.assertTrue(newHead != null); 85 m.remove(head); 86 head = newHead; 87 j++; 88 } 89 Assert.assertTrue(m.isEmpty()); 90 Assert.assertEquals(j, mapSize); 91 } 92 93 Map<Integer,Integer> m = new LinkedHashMap(); 94 for (int i=0; i<mapSize; i++) 95 Assert.assertEquals(m.put(new Integer(i), new Integer(2*i)), null); 96 for (int i=0; i<2*mapSize; i++) 97 Assert.assertEquals(m.containsValue(new Integer(i)), (i%2==0)); 98 Assert.assertFalse(m.put(nil, nil) == null); 99 Map<Integer,Integer> m2 = new LinkedHashMap(); m2.putAll(m); 100 Assert.assertTrue(m.equals(m2)); 101 Assert.assertTrue(m2.equals(m)); 102 Set<Map.Entry<Integer,Integer>> s = m.entrySet(), s2 = m2.entrySet(); 103 Assert.assertTrue(s.equals(s2)); 104 Assert.assertTrue(s2.equals(s)); 105 Assert.assertTrue(s.containsAll(s2)); 106 Assert.assertTrue(s2.containsAll(s)); 107 108 m2 = serClone(m); 109 Assert.assertTrue(m.equals(m2)); 110 Assert.assertTrue(m2.equals(m)); 111 s = m.entrySet(); s2 = m2.entrySet(); 112 Assert.assertTrue(s.equals(s2)); 113 Assert.assertTrue(s2.equals(s)); 114 Assert.assertTrue(s.containsAll(s2)); 115 Assert.assertTrue(s2.containsAll(s)); 116 117 s2.removeAll(s); 118 Assert.assertTrue(m2.isEmpty()); 119 120 m2.putAll(m); 121 m2.clear(); 122 Assert.assertTrue(m2.isEmpty()); 123 124 Iterator it = m.entrySet().iterator(); 125 while (it.hasNext()) { 126 it.next(); 127 it.remove(); 128 } 129 Assert.assertTrue(m.isEmpty()); 130 131 // Test ordering properties with insert order 132 m = new LinkedHashMap(); 133 List<Integer> l = new ArrayList(mapSize); 134 for (int i=0; i<mapSize; i++) { 135 Integer x = new Integer(i); 136 m.put(x, x); 137 l.add(x); 138 } 139 Assert.assertTrue(new ArrayList(m.keySet()).equals(l)); 140 for (int i=mapSize-1; i>=0; i--) { 141 Integer x = (Integer) l.get(i); 142 Assert.assertTrue(m.get(x).equals(x)); 143 } 144 Assert.assertTrue(new ArrayList(m.keySet()).equals(l)); 145 146 for (int i=mapSize-1; i>=0; i--) { 147 Integer x = (Integer) l.get(i); 148 m.put(x, x); 149 } 150 Assert.assertTrue(new ArrayList(m.keySet()).equals(l)); 151 152 m2 = (Map) ((LinkedHashMap)m).clone(); 153 Assert.assertTrue(m.equals(m2)); 154 155 List<Integer> l2 = new ArrayList(l); 156 Collections.shuffle(l2); 157 for (int i=0; i<mapSize; i++) { 158 Integer x = (Integer) l2.get(i); 159 Assert.assertTrue(m2.get(x).equals(x)); 160 } 161 Assert.assertTrue(new ArrayList(m2.keySet()).equals(l)); 162 163 // Test ordering properties with access order 164 m = new LinkedHashMap(2*mapSize, .75f, true); 165 for (int i=0; i<mapSize; i++) { 166 Integer x = new Integer(i); 167 m.put(x, x); 168 } 169 Assert.assertTrue(new ArrayList(m.keySet()).equals(l)); 170 171 for (int i=0; i<mapSize; i++) { 172 Integer x = (Integer) l2.get(i); 173 Assert.assertTrue(m.get(x).equals(x)); 174 } 175 Assert.assertTrue(new ArrayList(m.keySet()).equals(l2)); 176 177 for (int i=0; i<mapSize; i++) { 178 Integer x = (Integer) l2.get(i); 179 Assert.assertTrue(m.getOrDefault(x, new Integer(i + 1000)).equals(x)); 180 } 181 Assert.assertTrue(new ArrayList(m.keySet()).equals(l2)); 182 183 for (int i=0; i<mapSize; i++) { 184 Integer x = (Integer) l2.get(i); 185 Assert.assertTrue(m.replace(x, x).equals(x)); 186 } 187 Assert.assertTrue(new ArrayList(m.keySet()).equals(l2)); 188 189 for (int i=0; i<mapSize; i++) { 190 Integer x = (Integer) l2.get(i); 191 Assert.assertTrue(m.replace(x, x, x)); 192 } 193 Assert.assertTrue(new ArrayList(m.keySet()).equals(l2)); 194 195 BiFunction<Integer,Integer,Integer> f = (Integer y, Integer z) -> { 196 Assert.assertTrue(Objects.equals(y,z)); 197 return new Integer(z); 198 }; 199 200 for (int i=0; i<mapSize; i++) { 201 Integer x = (Integer) l2.get(i); 202 Assert.assertTrue(x.equals(m.merge(x, x, f))); 203 } 204 Assert.assertTrue(new ArrayList(m.keySet()).equals(l2)); 205 206 for (int i=0; i<mapSize; i++) { 207 Integer x = (Integer) l2.get(i); 208 Assert.assertTrue(x.equals(m.compute(x, f))); 209 } 210 Assert.assertTrue(new ArrayList(m.keySet()).equals(l2)); 211 212 for (int i=0; i<mapSize; i++) { 213 Integer x = (Integer) l2.get(i); 214 Assert.assertTrue(x.equals(m.remove(x))); 215 Assert.assertTrue(x.equals(m.computeIfAbsent(x, Integer::valueOf))); 216 } 217 Assert.assertTrue(new ArrayList(m.keySet()).equals(l2)); 218 219 for (int i=0; i<mapSize; i++) { 220 Integer x = (Integer) l2.get(i); 221 Assert.assertTrue(x.equals(m.computeIfPresent(x, f))); 222 } 223 Assert.assertTrue(new ArrayList(m.keySet()).equals(l2)); 224 225 for (int i=0; i<mapSize; i++) { 226 Integer x = new Integer(i); 227 m.put(x, x); 228 } 229 Assert.assertTrue(new ArrayList(m.keySet()).equals(l)); 230 231 m2 = (Map) ((LinkedHashMap)m).clone(); 232 Assert.assertTrue(m.equals(m2)); 233 for (int i=0; i<mapSize; i++) { 234 Integer x = (Integer) l.get(i); 235 Assert.assertTrue(m2.get(x).equals(x)); 236 } 237 Assert.assertTrue(new ArrayList(m2.keySet()).equals(l)); 238 239 } 240 serClone(Map m)241 private static Map serClone(Map m) { 242 Map result = null; 243 try { 244 // Serialize 245 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 246 ObjectOutputStream out = new ObjectOutputStream(bos); 247 out.writeObject(m); 248 out.flush(); 249 250 // Deserialize 251 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 252 out.close(); 253 ObjectInputStream in = new ObjectInputStream(bis); 254 result = (Map)in.readObject(); 255 in.close(); 256 } catch (Exception e) { 257 Assert.fail(); 258 } 259 return result; 260 } 261 }