test/java/util/Map/Get.java

Print this page
rev 5028 : 7126277: alternative hashing


  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 }


  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("containsValue(oldValue)", m.containsValue(oldValue));
  54             check("values.contains(oldValue)", m.values().contains(oldValue));
  55         }
  56         equal(m.put(key, value), oldValue);
  57         equal(m.get(key), value);
  58         check("containsKey", m.containsKey(key));
  59         check("keySet.contains", m.keySet().contains(key));
  60         check("containsValue", m.containsValue(value));
  61         check("values.contains",  m.values().contains(value));
  62         check("!isEmpty", ! 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.err.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(m.getClass().getName(), t); }
  85         } else {
  86             try { m.get(null); fail(m.getClass().getName() + " did not reject null key"); }
  87             catch (NullPointerException e) {}
  88             catch (Throwable t) { unexpected(m.getClass().getName(), t); }
  89 
  90             try { m.put(null, true); fail(m.getClass().getName() + " did not reject null key"); }
  91             catch (NullPointerException e) {}
  92             catch (Throwable t) { unexpected(m.getClass().getName(), 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(m.getClass().getName(), t); }
 101         } else {
 102             try { m.put('A', null); fail(m.getClass().getName() + " did not reject null key"); }
 103             catch (NullPointerException e) {}
 104             catch (Throwable t) { unexpected(m.getClass().getName(), t); }
 105 
 106             try { m.put('C', null); fail(m.getClass().getName() + " did not reject null key"); }
 107             catch (NullPointerException e) {}
 108             catch (Throwable t) { unexpected(m.getClass().getName(), t); }
 109         }
 110     }
 111 
 112     //--------------------- Infrastructure ---------------------------
 113     static volatile int passed = 0, failed = 0;
 114     static void pass() { passed++; }
 115     static void fail() { failed++; (new Error("Failure")).printStackTrace(System.err); }
 116     static void fail(String msg) { failed++; (new Error("Failure: " + msg)).printStackTrace(System.err); }
 117     static void unexpected(String msg, Throwable t) { System.err.println("Unexpected: " + msg); unexpected(t); }
 118     static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); }
 119     static void check(boolean cond) { if (cond) pass(); else fail(); }
 120     static void check(String desc, boolean cond) { if (cond) pass(); else fail(desc); }
 121     static void equal(Object x, Object y) {
 122         if(Objects.equals(x,y)) pass(); else fail(x + " not equal to " + y);
 123     }
 124 
 125     public static void main(String[] args) throws Throwable {
 126         try { realMain(args); } catch (Throwable t) { unexpected(t); }
 127 
 128         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 129         if (failed > 0) throw new Error("Some tests failed");
 130     }
 131 }