rev 5380 : 7126277: Alternative hashing implementation

   1 /*
   2  * Copyright (c) 2005, 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 6306829
  27  * @summary Verify assertions in get() javadocs
  28  * @author Martin Buchholz
  29  */
  30 
  31 import java.io.*;
  32 import java.util.*;
  33 import java.util.concurrent.*;
  34 import java.util.concurrent.atomic.*;
  35 
  36 public class Get {
  37 
  38     private static void realMain(String[] args) throws Throwable {
  39         testMap(new Hashtable<Character,Boolean>());
  40         testMap(new HashMap<Character,Boolean>());
  41         testMap(new IdentityHashMap<Character,Boolean>());
  42         testMap(new LinkedHashMap<Character,Boolean>());
  43         testMap(new ConcurrentHashMap<Character,Boolean>());
  44         testMap(new WeakHashMap<Character,Boolean>());
  45         testMap(new TreeMap<Character,Boolean>());
  46         testMap(new ConcurrentSkipListMap<Character,Boolean>());
  47     }
  48 
  49     private static void put(Map<Character,Boolean> m,
  50                             Character key, Boolean value,
  51                             Boolean oldValue) {
  52         if (oldValue != null) {
  53             check(m.containsValue(oldValue));
  54             check(m.values().contains(oldValue));
  55         }
  56         equal(m.put(key, value), oldValue);
  57         equal(m.get(key), value);
  58         check(m.containsKey(key));
  59         check(m.keySet().contains(key));
  60         check(m.containsValue(value));
  61         check(m.values().contains(value));
  62         check(! m.isEmpty());
  63     }
  64 
  65     private static void testMap(Map<Character,Boolean> m) {
  66         // We verify following assertions in get(Object) method javadocs
  67         boolean permitsNullKeys = (! (m instanceof ConcurrentMap ||
  68                                       m instanceof Hashtable     ||
  69                                       m instanceof SortedMap));
  70         boolean permitsNullValues = (! (m instanceof ConcurrentMap ||
  71                                         m instanceof Hashtable));
  72         boolean usesIdentity = m instanceof IdentityHashMap;
  73 
  74         System.out.println(m.getClass());
  75         put(m, 'A', true,  null);
  76         put(m, 'A', false, true);       // Guaranteed identical by JLS
  77         put(m, 'B', true,  null);
  78         put(m, new Character('A'), false, usesIdentity ? null : false);
  79         if (permitsNullKeys) {
  80             try {
  81                 put(m, null, true,  null);
  82                 put(m, null, false, true);
  83             }
  84             catch (Throwable t) { unexpected(t); }
  85         } else {
  86             try { m.get(null); fail(); }
  87             catch (NullPointerException e) {}
  88             catch (Throwable t) { unexpected(t); }
  89 
  90             try { m.put(null, true); fail(); }
  91             catch (NullPointerException e) {}
  92             catch (Throwable t) { unexpected(t); }
  93         }
  94         if (permitsNullValues) {
  95             try {
  96                 put(m, 'C', null, null);
  97                 put(m, 'C', true, null);
  98                 put(m, 'C', null, true);
  99             }
 100             catch (Throwable t) { unexpected(t); }
 101         } else {
 102             try { m.put('A', null); fail(); }
 103             catch (NullPointerException e) {}
 104             catch (Throwable t) { unexpected(t); }
 105 
 106             try { m.put('C', null); fail(); }
 107             catch (NullPointerException e) {}
 108             catch (Throwable t) { unexpected(t); }
 109         }
 110     }
 111 
 112     //--------------------- Infrastructure ---------------------------
 113     static volatile int passed = 0, failed = 0;
 114     static void pass() { passed++; }
 115     static void fail() { failed++; Thread.dumpStack(); }
 116     static void fail(String msg) { System.out.println(msg); fail(); }
 117     static void unexpected(Throwable t) { failed++; t.printStackTrace(); }

 118     static void check(boolean cond) { if (cond) pass(); else fail(); }

 119     static void equal(Object x, Object y) {
 120         if (x == null ? y == null : x.equals(y)) pass();
 121         else {System.out.println(x + " not equal to " + y); fail(); }}
 122 
 123     public static void main(String[] args) throws Throwable {
 124         try { realMain(args); } catch (Throwable t) { unexpected(t); }
 125 
 126         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 127         if (failed > 0) throw new Exception("Some tests failed");
 128     }
 129 }
--- EOF ---