J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4245809 |
| 27 | * @summary Basic test for LinkedHashMap. (Based on MapBash) |
| 28 | */ |
| 29 | |
| 30 | import java.util.*; |
| 31 | import java.io.*; |
| 32 | |
| 33 | public class Basic { |
| 34 | static Random rnd = new Random(666); |
| 35 | static Object nil = new Integer(0); |
| 36 | |
| 37 | public static void main(String[] args) throws Exception { |
| 38 | int numItr = 500; |
| 39 | int mapSize = 500; |
| 40 | |
| 41 | // Linked List test |
| 42 | for (int i=0; i<numItr; i++) { |
| 43 | Map m = new LinkedHashMap(); |
| 44 | Object head = nil; |
| 45 | |
| 46 | for (int j=0; j<mapSize; j++) { |
| 47 | Object newHead; |
| 48 | do { |
| 49 | newHead = new Integer(rnd.nextInt()); |
| 50 | } while (m.containsKey(newHead)); |
| 51 | m.put(newHead, head); |
| 52 | head = newHead; |
| 53 | } |
| 54 | if (m.size() != mapSize) |
| 55 | throw new Exception("Size not as expected."); |
| 56 | |
| 57 | if (new HashMap(m).hashCode() != m.hashCode()) |
| 58 | throw new Exception("Incorrect hashCode computation."); |
| 59 | |
| 60 | Map m2 = new LinkedHashMap(); m2.putAll(m); |
| 61 | m2.values().removeAll(m.keySet()); |
| 62 | if (m2.size()!= 1 || !m2.containsValue(nil)) |
| 63 | throw new Exception("Collection views test failed."); |
| 64 | |
| 65 | int j=0; |
| 66 | while (head != nil) { |
| 67 | if (!m.containsKey(head)) |
| 68 | throw new Exception("Linked list doesn't contain a link."); |
| 69 | Object newHead = m.get(head); |
| 70 | if (newHead == null) |
| 71 | throw new Exception("Could not retrieve a link."); |
| 72 | m.remove(head); |
| 73 | head = newHead; |
| 74 | j++; |
| 75 | } |
| 76 | if (!m.isEmpty()) |
| 77 | throw new Exception("Map nonempty after removing all links."); |
| 78 | if (j != mapSize) |
| 79 | throw new Exception("Linked list size not as expected."); |
| 80 | } |
| 81 | |
| 82 | Map m = new LinkedHashMap(); |
| 83 | for (int i=0; i<mapSize; i++) |
| 84 | if (m.put(new Integer(i), new Integer(2*i)) != null) |
| 85 | throw new Exception("put returns non-null value erroenously."); |
| 86 | for (int i=0; i<2*mapSize; i++) |
| 87 | if (m.containsValue(new Integer(i)) != (i%2==0)) |
| 88 | throw new Exception("contains value "+i); |
| 89 | if (m.put(nil, nil) == null) |
| 90 | throw new Exception("put returns a null value erroenously."); |
| 91 | Map m2 = new LinkedHashMap(); m2.putAll(m); |
| 92 | if (!m.equals(m2)) |
| 93 | throw new Exception("Clone not equal to original. (1)"); |
| 94 | if (!m2.equals(m)) |
| 95 | throw new Exception("Clone not equal to original. (2)"); |
| 96 | Set s = m.entrySet(), s2 = m2.entrySet(); |
| 97 | if (!s.equals(s2)) |
| 98 | throw new Exception("Clone not equal to original. (3)"); |
| 99 | if (!s2.equals(s)) |
| 100 | throw new Exception("Clone not equal to original. (4)"); |
| 101 | if (!s.containsAll(s2)) |
| 102 | throw new Exception("Original doesn't contain clone!"); |
| 103 | if (!s2.containsAll(s)) |
| 104 | throw new Exception("Clone doesn't contain original!"); |
| 105 | |
| 106 | m2 = serClone(m); |
| 107 | if (!m.equals(m2)) |
| 108 | throw new Exception("Serialize Clone not equal to original. (1)"); |
| 109 | if (!m2.equals(m)) |
| 110 | throw new Exception("Serialize Clone not equal to original. (2)"); |
| 111 | s = m.entrySet(); s2 = m2.entrySet(); |
| 112 | if (!s.equals(s2)) |
| 113 | throw new Exception("Serialize Clone not equal to original. (3)"); |
| 114 | if (!s2.equals(s)) |
| 115 | throw new Exception("Serialize Clone not equal to original. (4)"); |
| 116 | if (!s.containsAll(s2)) |
| 117 | throw new Exception("Original doesn't contain Serialize clone!"); |
| 118 | if (!s2.containsAll(s)) |
| 119 | throw new Exception("Serialize Clone doesn't contain original!"); |
| 120 | |
| 121 | s2.removeAll(s); |
| 122 | if (!m2.isEmpty()) |
| 123 | throw new Exception("entrySet().removeAll failed."); |
| 124 | |
| 125 | m2.putAll(m); |
| 126 | m2.clear(); |
| 127 | if (!m2.isEmpty()) |
| 128 | throw new Exception("clear failed."); |
| 129 | |
| 130 | Iterator it = m.entrySet().iterator(); |
| 131 | while(it.hasNext()) { |
| 132 | it.next(); |
| 133 | it.remove(); |
| 134 | } |
| 135 | if (!m.isEmpty()) |
| 136 | throw new Exception("Iterator.remove() failed"); |
| 137 | |
| 138 | // Test ordering properties with insert order |
| 139 | m = new LinkedHashMap(); |
| 140 | List l = new ArrayList(mapSize); |
| 141 | for (int i=0; i<mapSize; i++) { |
| 142 | Integer x = new Integer(i); |
| 143 | m.put(x, x); |
| 144 | l.add(x); |
| 145 | } |
| 146 | if (!new ArrayList(m.keySet()).equals(l)) |
| 147 | throw new Exception("Insertion order not preserved."); |
| 148 | for (int i=mapSize-1; i>=0; i--) { |
| 149 | Integer x = (Integer) l.get(i); |
| 150 | if (!m.get(x).equals(x)) |
| 151 | throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); |
| 152 | } |
| 153 | if (!new ArrayList(m.keySet()).equals(l)) |
| 154 | throw new Exception("Insertion order not preserved after read."); |
| 155 | |
| 156 | for (int i=mapSize-1; i>=0; i--) { |
| 157 | Integer x = (Integer) l.get(i); |
| 158 | m.put(x, x); |
| 159 | } |
| 160 | if (!new ArrayList(m.keySet()).equals(l)) |
| 161 | throw new Exception("Insert order not preserved after reinsert."); |
| 162 | |
| 163 | m2 = (Map) ((LinkedHashMap)m).clone(); |
| 164 | if (!m.equals(m2)) |
| 165 | throw new Exception("Insert-order Map != clone."); |
| 166 | |
| 167 | List l2 = new ArrayList(l); |
| 168 | Collections.shuffle(l2); |
| 169 | for (int i=0; i<mapSize; i++) { |
| 170 | Integer x = (Integer) l2.get(i); |
| 171 | if (!m2.get(x).equals(x)) |
| 172 | throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x); |
| 173 | } |
| 174 | if (!new ArrayList(m2.keySet()).equals(l)) |
| 175 | throw new Exception("Clone: altered by read."); |
| 176 | |
| 177 | // Test ordering properties with access order |
| 178 | m = new LinkedHashMap(1000, .75f, true); |
| 179 | for (int i=0; i<mapSize; i++) { |
| 180 | Integer x = new Integer(i); |
| 181 | m.put(x, x); |
| 182 | } |
| 183 | if (!new ArrayList(m.keySet()).equals(l)) |
| 184 | throw new Exception("Insertion order not properly preserved."); |
| 185 | |
| 186 | for (int i=0; i<mapSize; i++) { |
| 187 | Integer x = (Integer) l2.get(i); |
| 188 | if (!m.get(x).equals(x)) |
| 189 | throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); |
| 190 | } |
| 191 | if (!new ArrayList(m.keySet()).equals(l2)) |
| 192 | throw new Exception("Insert order not properly altered by read."); |
| 193 | |
| 194 | for (int i=0; i<mapSize; i++) { |
| 195 | Integer x = new Integer(i); |
| 196 | m.put(x, x); |
| 197 | } |
| 198 | if (!new ArrayList(m.keySet()).equals(l)) |
| 199 | throw new Exception("Insertion order not altered by reinsert."); |
| 200 | |
| 201 | m2 = (Map) ((LinkedHashMap)m).clone(); |
| 202 | if (!m.equals(m2)) |
| 203 | throw new Exception("Access-order Map != clone."); |
| 204 | for (int i=0; i<mapSize; i++) { |
| 205 | Integer x = (Integer) l.get(i); |
| 206 | if (!m2.get(x).equals(x)) |
| 207 | throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x); |
| 208 | } |
| 209 | if (!new ArrayList(m2.keySet()).equals(l)) |
| 210 | throw new Exception("Clone: order not properly altered by read."); |
| 211 | |
| 212 | System.err.println("Success."); |
| 213 | } |
| 214 | |
| 215 | private static Map serClone(Map m) { |
| 216 | Map result = null; |
| 217 | try { |
| 218 | // Serialize |
| 219 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 220 | ObjectOutputStream out = new ObjectOutputStream(bos); |
| 221 | out.writeObject(m); |
| 222 | out.flush(); |
| 223 | |
| 224 | // Deserialize |
| 225 | ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); |
| 226 | out.close(); |
| 227 | ObjectInputStream in = new ObjectInputStream(bis); |
| 228 | result = (Map)in.readObject(); |
| 229 | in.close(); |
| 230 | } catch(Exception e) { |
| 231 | e.printStackTrace(); |
| 232 | } |
| 233 | return result; |
| 234 | } |
| 235 | } |