test/java/util/LinkedHashMap/Basic.java

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


   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
  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++) {


 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.");




   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.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++) {


 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(2*mapSize, .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 = (Integer) l2.get(i);
 196             if (!m.getOrDefault(x, new Integer(i + 1000)).equals(x))
 197                 throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);
 198         }
 199         if (!new ArrayList(m.keySet()).equals(l2))
 200             throw new Exception("Insert order not properly altered by read.");
 201 
 202         for (int i=0; i<mapSize; i++) {
 203             Integer x = (Integer) l2.get(i);
 204             if (!m.replace(x, x).equals(x))
 205                 throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);
 206         }
 207         if (!new ArrayList(m.keySet()).equals(l2))
 208             throw new Exception("Insert order not properly altered by replace.");
 209 
 210         for (int i=0; i<mapSize; i++) {
 211             Integer x = (Integer) l2.get(i);
 212             if (!m.replace(x, x, new Integer(i)))
 213                 throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x);
 214         }
 215         if (!new ArrayList(m.keySet()).equals(l2))
 216             throw new Exception("Insert order not properly altered by replace.");
 217 
 218         for (int i=0; i<mapSize; i++) {
 219             Integer x = new Integer(i);
 220             m.put(x, x);
 221         }
 222         if (!new ArrayList(m.keySet()).equals(l))
 223             throw new Exception("Insertion order not altered by reinsert.");
 224 
 225         m2 = (Map) ((LinkedHashMap)m).clone();
 226         if (!m.equals(m2))
 227             throw new Exception("Access-order Map != clone.");
 228         for (int i=0; i<mapSize; i++) {
 229             Integer x = (Integer) l.get(i);
 230             if (!m2.get(x).equals(x))
 231               throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x);
 232         }
 233         if (!new ArrayList(m2.keySet()).equals(l))
 234             throw new Exception("Clone: order not properly altered by read.");
 235 
 236         System.err.println("Success.");