J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001-2005 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 4485486 6197726 6232484 |
| 27 | * @summary IdentityHashMap's entrySet toArray tests |
| 28 | * @author Josh Bloch, Martin Buchholz |
| 29 | */ |
| 30 | |
| 31 | import java.util.*; |
| 32 | |
| 33 | public class ToArray { |
| 34 | public static void main(String[] args) { |
| 35 | //---------------------------------------------------------------- |
| 36 | // new ArrayList(IdentityHashMap.entrySet()) |
| 37 | // used to return bogus entries. |
| 38 | //---------------------------------------------------------------- |
| 39 | Map<String,String> mm = new IdentityHashMap<String,String>(); |
| 40 | mm.put("foo", "bar"); |
| 41 | mm.put("baz", "quux"); |
| 42 | List<Map.Entry<String,String>> lm |
| 43 | = new ArrayList<Map.Entry<String,String>>(mm.entrySet()); |
| 44 | String s = lm.toString(); |
| 45 | if (! (s.equals("[foo=bar, baz=quux]") || |
| 46 | s.equals("[baz=quux, foo=bar]"))) |
| 47 | throw new Error("bad string"); |
| 48 | |
| 49 | //---------------------------------------------------------------- |
| 50 | // IdentityHashMap's lack of internal entry objects caused |
| 51 | // the inherited (AbstractMap) version of toArray to |
| 52 | // return garbage when called on the entrySet view. |
| 53 | //---------------------------------------------------------------- |
| 54 | Map m = new IdentityHashMap(); |
| 55 | m.put("french", "connection"); |
| 56 | m.put("polish", "sausage"); |
| 57 | Object[] mArray = m.entrySet().toArray(); |
| 58 | if (mArray[0] == mArray[1]) |
| 59 | throw new RuntimeException("Broken"); |
| 60 | |
| 61 | mArray[0].toString(); |
| 62 | mArray[1].toString(); |
| 63 | |
| 64 | //---------------------------------------------------------------- |
| 65 | // IdentityHashMap.entrySet().toArray(T[] a) used to simply |
| 66 | // return toArray() ! |
| 67 | //---------------------------------------------------------------- |
| 68 | IdentityHashMap<Integer,Integer> map |
| 69 | = new IdentityHashMap<Integer,Integer>(); |
| 70 | Set<Map.Entry<Integer,Integer>> es = map.entrySet(); |
| 71 | if (es.toArray().length != 0) |
| 72 | throw new Error("non-empty"); |
| 73 | if (es.toArray(new Object[]{Boolean.TRUE})[0] != null) |
| 74 | throw new Error("non-null"); |
| 75 | map.put(7,49); |
| 76 | if (es.toArray().length != 1) |
| 77 | throw new Error("length"); |
| 78 | Object[] x = es.toArray(new Object[]{Boolean.TRUE, Boolean.TRUE}); |
| 79 | if (x[1] != null) |
| 80 | throw new Error("non-null"); |
| 81 | Map.Entry e = (Map.Entry) x[0]; |
| 82 | if (! e.getKey().equals(new Integer(7))) |
| 83 | throw new Error("bad key"); |
| 84 | if (! e.getValue().equals(new Integer(49))) |
| 85 | throw new Error("bad value"); |
| 86 | } |
| 87 | } |