test/java/util/LinkedHashMap/Basic.java

Print this page
rev 8969 : 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
Reviewed-by: psandoz

*** 21,52 **** * questions. */ /** * @test ! * @bug 4245809 * @summary Basic test for LinkedHashMap. (Based on MapBash) */ import java.util.*; import java.io.*; public class Basic { ! static Random rnd = new Random(666); ! static Object nil = new Integer(0); public static void main(String[] args) throws Exception { int numItr = 500; int mapSize = 500; ! // Linked List test for (int i=0; i<numItr; i++) { ! Map m = new LinkedHashMap(); ! Object head = nil; for (int j=0; j<mapSize; j++) { ! Object newHead; do { newHead = new Integer(rnd.nextInt()); } while (m.containsKey(newHead)); m.put(newHead, head); head = newHead; --- 21,53 ---- * questions. */ /** * @test ! * @bug 4245809 8029795 * @summary Basic test for LinkedHashMap. (Based on MapBash) */ import java.util.*; + import java.util.function.*; import java.io.*; public class Basic { ! final static Random rnd = new Random(666); ! final static Integer nil = new Integer(0); public static void main(String[] args) throws Exception { int numItr = 500; int mapSize = 500; ! // Linked List testk for (int i=0; i<numItr; i++) { ! Map<Integer,Integer> m = new LinkedHashMap(); ! Integer head = nil; for (int j=0; j<mapSize; j++) { ! Integer newHead; do { newHead = new Integer(rnd.nextInt()); } while (m.containsKey(newHead)); m.put(newHead, head); head = newHead;
*** 55,74 **** throw new Exception("Size not as expected."); if (new HashMap(m).hashCode() != m.hashCode()) throw new Exception("Incorrect hashCode computation."); ! Map m2 = new LinkedHashMap(); m2.putAll(m); m2.values().removeAll(m.keySet()); if (m2.size()!= 1 || !m2.containsValue(nil)) throw new Exception("Collection views test failed."); int j=0; while (head != nil) { if (!m.containsKey(head)) throw new Exception("Linked list doesn't contain a link."); ! Object newHead = m.get(head); if (newHead == null) throw new Exception("Could not retrieve a link."); m.remove(head); head = newHead; j++; --- 56,75 ---- throw new Exception("Size not as expected."); if (new HashMap(m).hashCode() != m.hashCode()) throw new Exception("Incorrect hashCode computation."); ! Map<Integer,Integer> m2 = new LinkedHashMap(); m2.putAll(m); m2.values().removeAll(m.keySet()); if (m2.size()!= 1 || !m2.containsValue(nil)) throw new Exception("Collection views test failed."); int j=0; while (head != nil) { if (!m.containsKey(head)) throw new Exception("Linked list doesn't contain a link."); ! Integer newHead = m.get(head); if (newHead == null) throw new Exception("Could not retrieve a link."); m.remove(head); head = newHead; j++;
*** 77,101 **** throw new Exception("Map nonempty after removing all links."); if (j != mapSize) throw new Exception("Linked list size not as expected."); } ! Map m = new LinkedHashMap(); for (int i=0; i<mapSize; i++) if (m.put(new Integer(i), new Integer(2*i)) != null) throw new Exception("put returns non-null value erroenously."); for (int i=0; i<2*mapSize; i++) if (m.containsValue(new Integer(i)) != (i%2==0)) throw new Exception("contains value "+i); if (m.put(nil, nil) == null) throw new Exception("put returns a null value erroenously."); ! Map m2 = new LinkedHashMap(); m2.putAll(m); if (!m.equals(m2)) throw new Exception("Clone not equal to original. (1)"); if (!m2.equals(m)) throw new Exception("Clone not equal to original. (2)"); ! Set s = m.entrySet(), s2 = m2.entrySet(); if (!s.equals(s2)) throw new Exception("Clone not equal to original. (3)"); if (!s2.equals(s)) throw new Exception("Clone not equal to original. (4)"); if (!s.containsAll(s2)) --- 78,102 ---- throw new Exception("Map nonempty after removing all links."); if (j != mapSize) throw new Exception("Linked list size not as expected."); } ! Map<Integer,Integer> m = new LinkedHashMap(); for (int i=0; i<mapSize; i++) if (m.put(new Integer(i), new Integer(2*i)) != null) throw new Exception("put returns non-null value erroenously."); for (int i=0; i<2*mapSize; i++) if (m.containsValue(new Integer(i)) != (i%2==0)) throw new Exception("contains value "+i); if (m.put(nil, nil) == null) throw new Exception("put returns a null value erroenously."); ! Map<Integer,Integer> m2 = new LinkedHashMap(); m2.putAll(m); if (!m.equals(m2)) throw new Exception("Clone not equal to original. (1)"); if (!m2.equals(m)) throw new Exception("Clone not equal to original. (2)"); ! Set<Map.Entry<Integer,Integer>> s = m.entrySet(), s2 = m2.entrySet(); if (!s.equals(s2)) throw new Exception("Clone not equal to original. (3)"); if (!s2.equals(s)) throw new Exception("Clone not equal to original. (4)"); if (!s.containsAll(s2))
*** 135,145 **** if (!m.isEmpty()) throw new Exception("Iterator.remove() failed"); // Test ordering properties with insert order m = new LinkedHashMap(); ! List l = new ArrayList(mapSize); for (int i=0; i<mapSize; i++) { Integer x = new Integer(i); m.put(x, x); l.add(x); } --- 136,146 ---- if (!m.isEmpty()) throw new Exception("Iterator.remove() failed"); // Test ordering properties with insert order m = new LinkedHashMap(); ! List<Integer> l = new ArrayList(mapSize); for (int i=0; i<mapSize; i++) { Integer x = new Integer(i); m.put(x, x); l.add(x); }
*** 162,183 **** m2 = (Map) ((LinkedHashMap)m).clone(); if (!m.equals(m2)) throw new Exception("Insert-order Map != clone."); ! List l2 = new ArrayList(l); Collections.shuffle(l2); for (int i=0; i<mapSize; i++) { Integer x = (Integer) l2.get(i); if (!m2.get(x).equals(x)) throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x); } if (!new ArrayList(m2.keySet()).equals(l)) throw new Exception("Clone: altered by read."); // Test ordering properties with access order ! m = new LinkedHashMap(1000, .75f, true); for (int i=0; i<mapSize; i++) { Integer x = new Integer(i); m.put(x, x); } if (!new ArrayList(m.keySet()).equals(l)) --- 163,184 ---- m2 = (Map) ((LinkedHashMap)m).clone(); if (!m.equals(m2)) throw new Exception("Insert-order Map != clone."); ! List<Integer> l2 = new ArrayList(l); Collections.shuffle(l2); for (int i=0; i<mapSize; i++) { Integer x = (Integer) l2.get(i); if (!m2.get(x).equals(x)) throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x); } if (!new ArrayList(m2.keySet()).equals(l)) throw new Exception("Clone: altered by read."); // Test ordering properties with access order ! m = new LinkedHashMap(2*mapSize, .75f, true); for (int i=0; i<mapSize; i++) { Integer x = new Integer(i); m.put(x, x); } if (!new ArrayList(m.keySet()).equals(l))
*** 190,199 **** --- 191,264 ---- } if (!new ArrayList(m.keySet()).equals(l2)) throw new Exception("Insert order not properly altered by read."); for (int i=0; i<mapSize; i++) { + Integer x = (Integer) l2.get(i); + if (!m.getOrDefault(x, new Integer(i + 1000)).equals(x)) + throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); + } + if (!new ArrayList(m.keySet()).equals(l2)) + throw new Exception("Insert order not properly altered by read."); + + for (int i=0; i<mapSize; i++) { + Integer x = (Integer) l2.get(i); + if (!m.replace(x, x).equals(x)) + throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); + } + if (!new ArrayList(m.keySet()).equals(l2)) + throw new Exception("Insert order not properly altered by replace."); + + for (int i=0; i<mapSize; i++) { + Integer x = (Integer) l2.get(i); + if (!m.replace(x, x, x)) + throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); + } + if (!new ArrayList(m.keySet()).equals(l2)) + throw new Exception("Insert order not properly altered by replace."); + + BiFunction<Integer,Integer,Integer> f = (Integer y, Integer z) -> { + if (!Objects.equals(y,z)) + throw new RuntimeException("unequal " + y + "," + z); + return new Integer(z); + }; + + for (int i=0; i<mapSize; i++) { + Integer x = (Integer) l2.get(i); + if (!x.equals(m.merge(x, x, f))) + throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); + } + if (!new ArrayList(m.keySet()).equals(l2)) + throw new Exception("Insert order not properly altered by replace."); + + for (int i=0; i<mapSize; i++) { + Integer x = (Integer) l2.get(i); + if (!x.equals(m.compute(x, f))) + throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); + } + if (!new ArrayList(m.keySet()).equals(l2)) + throw new Exception("Insert order not properly altered by replace."); + + for (int i=0; i<mapSize; i++) { + Integer x = (Integer) l2.get(i); + if(!x.equals(m.remove(x))) + throw new Exception("Missing key: "+i+", "+x); + if (!x.equals(m.computeIfAbsent(x, Integer::valueOf))) + throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); + } + if (!new ArrayList(m.keySet()).equals(l2)) + throw new Exception("Insert order not properly altered by replace."); + + for (int i=0; i<mapSize; i++) { + Integer x = (Integer) l2.get(i); + if (!x.equals(m.computeIfPresent(x, f))) + throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); + } + if (!new ArrayList(m.keySet()).equals(l2)) + throw new Exception("Insert order not properly altered by replace."); + + for (int i=0; i<mapSize; i++) { Integer x = new Integer(i); m.put(x, x); } if (!new ArrayList(m.keySet()).equals(l)) throw new Exception("Insertion order not altered by reinsert.");