diff options
Diffstat (limited to 'java')
24 files changed, 736 insertions, 102 deletions
diff --git a/java/allTests.py b/java/allTests.py index d69abc728fb..ce7ac3ac3c4 100755 --- a/java/allTests.py +++ b/java/allTests.py @@ -64,6 +64,7 @@ tests = [ ("Ice/classLoader", ["core"]), ("Ice/invoke", ["core"]), ("Ice/properties", ["once"]), + ("Ice/hash", ["once"]), ("Ice/optional", ["once"]), ("IceBox/configuration", ["core", "noipv6"]), ("Freeze/dbmap", ["once"]), diff --git a/java/build.xml b/java/build.xml index 4164c47b28d..0c59f90231e 100644 --- a/java/build.xml +++ b/java/build.xml @@ -434,6 +434,9 @@ <fileset dir="test/Ice/faultTolerance"> <include name="Test.ice" /> </fileset> + <fileset dir="test/Ice/hash"> + <include name="Test.ice" /> + </fileset> <fileset dir="test/Ice/hold"> <include name="Test.ice" /> </fileset> diff --git a/java/src/Freeze/SharedDbEnv.java b/java/src/Freeze/SharedDbEnv.java index ab7163c20b7..76bf51d78e1 100644 --- a/java/src/Freeze/SharedDbEnv.java +++ b/java/src/Freeze/SharedDbEnv.java @@ -545,7 +545,9 @@ public class SharedDbEnv implements com.sleepycat.db.ErrorHandler, Runnable public int hashCode() { - return envName.hashCode() ^ communicator.hashCode(); + int h = 5381; + h = IceInternal.HashUtil.hashAdd(h, envName); + return IceInternal.HashUtil.hashAdd(h, communicator); } } diff --git a/java/src/Ice/LocalObject.java b/java/src/Ice/LocalObject.java index 03cbba57d9b..335198e6f01 100644 --- a/java/src/Ice/LocalObject.java +++ b/java/src/Ice/LocalObject.java @@ -17,6 +17,4 @@ public interface LocalObject boolean equals(java.lang.Object rhs); java.lang.Object clone() throws java.lang.CloneNotSupportedException; - - int ice_hash(); } diff --git a/java/src/Ice/LocalObjectImpl.java b/java/src/Ice/LocalObjectImpl.java index c2634d3e15e..da39eac7dab 100644 --- a/java/src/Ice/LocalObjectImpl.java +++ b/java/src/Ice/LocalObjectImpl.java @@ -19,10 +19,4 @@ public abstract class LocalObjectImpl implements LocalObject, java.lang.Cloneabl { return super.clone(); } - - public int - ice_hash() - { - return hashCode(); - } } diff --git a/java/src/Ice/Object.java b/java/src/Ice/Object.java index 70461f26b86..20184f6657f 100644 --- a/java/src/Ice/Object.java +++ b/java/src/Ice/Object.java @@ -22,11 +22,6 @@ public interface Object **/ java.lang.Object clone() throws java.lang.CloneNotSupportedException; - /** - * @deprecated This method is deprecated, use hashCode instead. - **/ - int ice_hash(); - /** * Tests whether this object supports a specific Slice interface. * diff --git a/java/src/Ice/ObjectImpl.java b/java/src/Ice/ObjectImpl.java index 7ff5636cc0f..a1e0fb5c7a4 100644 --- a/java/src/Ice/ObjectImpl.java +++ b/java/src/Ice/ObjectImpl.java @@ -43,15 +43,6 @@ public abstract class ObjectImpl implements Object, java.lang.Cloneable, java.io return o; } - /** - * @deprecated - **/ - public int - ice_hash() - { - return hashCode(); - } - public final static String[] __ids = { "::Ice::Object" diff --git a/java/src/Ice/ObjectPrx.java b/java/src/Ice/ObjectPrx.java index 119482ba45e..17587de0a9e 100644 --- a/java/src/Ice/ObjectPrx.java +++ b/java/src/Ice/ObjectPrx.java @@ -15,11 +15,6 @@ package Ice; public interface ObjectPrx { /** - * @deprecated This method is deprecated, use hashCode instead. - **/ - int ice_getHash(); - - /** * Returns the communicator that created this proxy. * * @return The communicator that created this proxy. diff --git a/java/src/Ice/ObjectPrxHelperBase.java b/java/src/Ice/ObjectPrxHelperBase.java index 9a1de69d685..fd6deac1100 100644 --- a/java/src/Ice/ObjectPrxHelperBase.java +++ b/java/src/Ice/ObjectPrxHelperBase.java @@ -26,15 +26,6 @@ public class ObjectPrxHelperBase implements ObjectPrx, java.io.Serializable } /** - * @deprecated - **/ - public final int - ice_getHash() - { - return _reference.hashCode(); - } - - /** * Returns the communicator that created this proxy. * * @return The communicator that created this proxy. diff --git a/java/src/Ice/ProxyIdentityFacetKey.java b/java/src/Ice/ProxyIdentityFacetKey.java index f23bf093952..a2b859e958d 100644 --- a/java/src/Ice/ProxyIdentityFacetKey.java +++ b/java/src/Ice/ProxyIdentityFacetKey.java @@ -17,6 +17,7 @@ package Ice; * @see ProxyIdentityAndFacetCompare * @see ProxyIdentityCompare * @see ProxyIdentityKey + * **/ public class ProxyIdentityFacetKey { @@ -35,8 +36,9 @@ public class ProxyIdentityFacetKey // _identity = proxy.ice_getIdentity(); _facet = proxy.ice_getFacet(); - int h = _identity.hashCode(); - h = 5 * h + _facet.hashCode(); + int h = 5381; + h = IceInternal.HashUtil.hashAdd(h, _identity); + h = IceInternal.HashUtil.hashAdd(h, _facet); _hashCode = h; } diff --git a/java/src/Ice/ProxyIdentityKey.java b/java/src/Ice/ProxyIdentityKey.java index a008ad63869..6bfe9b640ab 100644 --- a/java/src/Ice/ProxyIdentityKey.java +++ b/java/src/Ice/ProxyIdentityKey.java @@ -17,6 +17,7 @@ package Ice; * @see ProxyIdentityCompare * @see ProxyIdentityAndFacetCompare * @see ProxyIdentityFacetKey + * **/ public class ProxyIdentityKey { @@ -34,7 +35,9 @@ public class ProxyIdentityKey // Cache the identity and its hash code. // _identity = proxy.ice_getIdentity(); - _hashCode = _identity.hashCode(); + int h = 5381; + h = IceInternal.HashUtil.hashAdd(h, _identity); + _hashCode = h; } /** diff --git a/java/src/IceInternal/HashUtil.java b/java/src/IceInternal/HashUtil.java new file mode 100644 index 00000000000..520254bbc64 --- /dev/null +++ b/java/src/IceInternal/HashUtil.java @@ -0,0 +1,120 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +package IceInternal; + +public final class HashUtil +{ + public static int + hashAdd(int hashCode, boolean value) + { + return ((hashCode << 5) + hashCode) ^ (value ? 0 : 1); + } + + public static int + hashAdd(int hashCode, short value) + { + return ((hashCode << 5) + hashCode) ^ (int)(2654435761l * value); + } + + public static int + hashAdd(int hashCode, byte value) + { + return ((hashCode << 5) + hashCode) ^ (int)(2654435761l * value); + } + + public static int + hashAdd(int hashCode, int value) + { + return ((hashCode << 5) + hashCode) ^ (int)(2654435761l * value); + } + + public static int + hashAdd(int hashCode, long value) + { + return ((hashCode << 5) + hashCode) ^ (int)(value ^ (value >>> 32)); + } + + public static int + hashAdd(int hashCode, float value) + { + return ((hashCode << 5) + hashCode) ^ Float.floatToIntBits(value); + } + + public static int + hashAdd(int hashCode, double value) + { + long v = Double.doubleToLongBits(value); + return ((hashCode << 5) + hashCode) ^ (int)(v ^ (v >>> 32)); + } + + public static int + hashAdd(int hashCode, java.lang.Object value) + { + if(value != null) + { + hashCode = ((hashCode << 5) + hashCode) ^ value.hashCode(); + } + return hashCode; + } + + public static int + hashAdd(int hashCode, boolean[] arr) + { + return ((hashCode << 5) + hashCode) ^ java.util.Arrays.hashCode(arr); + } + + public static int + hashAdd(int hashCode, byte[] arr) + { + return ((hashCode << 5) + hashCode) ^ java.util.Arrays.hashCode(arr); + } + + public static int + hashAdd(int hashCode, char[] arr) + { + return ((hashCode << 5) + hashCode) ^ java.util.Arrays.hashCode(arr); + } + + public static int + hashAdd(int hashCode, double[] arr) + { + return ((hashCode << 5) + hashCode) ^ java.util.Arrays.hashCode(arr); + } + + public static int + hashAdd(int hashCode, float[] arr) + { + return ((hashCode << 5) + hashCode) ^ java.util.Arrays.hashCode(arr); + } + + public static int + hashAdd(int hashCode, int[] arr) + { + return ((hashCode << 5) + hashCode) ^ java.util.Arrays.hashCode(arr); + } + + public static int + hashAdd(int hashCode, long[] arr) + { + return ((hashCode << 5) + hashCode) ^ java.util.Arrays.hashCode(arr); + } + + public static int + hashAdd(int hashCode, Object[] arr) + { + return ((hashCode << 5) + hashCode) ^ java.util.Arrays.hashCode(arr); + } + + public static int + hashAdd(int hashCode, short[] arr) + { + return ((hashCode << 5) + hashCode) ^ java.util.Arrays.hashCode(arr); + } +} diff --git a/java/src/IceInternal/OpaqueEndpointI.java b/java/src/IceInternal/OpaqueEndpointI.java index 3982b095415..f40763a418a 100644 --- a/java/src/IceInternal/OpaqueEndpointI.java +++ b/java/src/IceInternal/OpaqueEndpointI.java @@ -433,13 +433,11 @@ final class OpaqueEndpointI extends EndpointI private void calcHashValue() { - _hashCode = _type; - _hashCode = 5 * _hashCode + _rawEncoding.major; - _hashCode = 5 * _hashCode + _rawEncoding.minor; - for(int i = 0; i < _rawBytes.length; i++) - { - _hashCode = 5 * _hashCode + _rawBytes[i]; - } + int h = 5381; + h = IceInternal.HashUtil.hashAdd(h, _type); + h = IceInternal.HashUtil.hashAdd(h, _rawEncoding); + h = IceInternal.HashUtil.hashAdd(h, _rawBytes); + _hashCode = h; } private short _type; diff --git a/java/src/IceInternal/Reference.java b/java/src/IceInternal/Reference.java index 8a1357d8401..2e6e1f4af1a 100644 --- a/java/src/IceInternal/Reference.java +++ b/java/src/IceInternal/Reference.java @@ -201,20 +201,23 @@ public abstract class Reference implements Cloneable return _hashValue; } - int h = _mode; - - h = 5 * h + _identity.hashCode(); - - h = 5 * h + _context.hashCode(); - - h = 5 * h + _facet.hashCode(); - - h = 5 * h + (_secure ? 1 : 0); + int h = 5381; + h = IceInternal.HashUtil.hashAdd(h, _mode); + h = IceInternal.HashUtil.hashAdd(h, _secure); + h = IceInternal.HashUtil.hashAdd(h, _identity); + h = IceInternal.HashUtil.hashAdd(h, _context); + h = IceInternal.HashUtil.hashAdd(h, _facet); + h = IceInternal.HashUtil.hashAdd(h, _overrideCompress); + if(_overrideCompress) + { + h = IceInternal.HashUtil.hashAdd(h, _compress); + } + h = IceInternal.HashUtil.hashAdd(h, _encoding); _hashValue = h; _hashInitialized = true; - return h; + return _hashValue; } // diff --git a/java/src/IceInternal/RoutableReference.java b/java/src/IceInternal/RoutableReference.java index 2e70432de90..e1f9607a4a8 100644 --- a/java/src/IceInternal/RoutableReference.java +++ b/java/src/IceInternal/RoutableReference.java @@ -369,9 +369,7 @@ public class RoutableReference extends Reference if(!_hashInitialized) { super.hashCode(); // Initializes _hashValue. - - // Add hash of adapter ID to base hash. - _hashValue = 5 * _hashValue + _adapterId.hashCode(); + _hashValue = IceInternal.HashUtil.hashAdd(_hashValue, _adapterId); } return _hashValue; } diff --git a/java/src/IceInternal/TcpConnector.java b/java/src/IceInternal/TcpConnector.java index 7947a4152f8..71503101dc5 100644 --- a/java/src/IceInternal/TcpConnector.java +++ b/java/src/IceInternal/TcpConnector.java @@ -80,12 +80,13 @@ final class TcpConnector implements Connector _encoding = encoding; _connectionId = connectionId; - _hashCode = _addr.getAddress().getHostAddress().hashCode(); - _hashCode = 5 * _hashCode + _addr.getPort(); - _hashCode = 5 * _hashCode + _timeout; - _hashCode = 5 * _hashCode + _protocol.hashCode(); - _hashCode = 5 * _hashCode + _encoding.hashCode(); - _hashCode = 5 * _hashCode + _connectionId.hashCode(); + _hashCode = 5381; + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _addr.getAddress().getHostAddress()); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _addr.getPort()); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _timeout); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _protocol); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _encoding); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _connectionId); } public boolean diff --git a/java/src/IceInternal/TcpEndpointI.java b/java/src/IceInternal/TcpEndpointI.java index 0d2ab229193..18426c4494d 100644 --- a/java/src/IceInternal/TcpEndpointI.java +++ b/java/src/IceInternal/TcpEndpointI.java @@ -556,13 +556,16 @@ final class TcpEndpointI extends EndpointI private void calcHashValue() { - _hashCode = _host.hashCode(); - _hashCode = 5 * _hashCode + _port; - _hashCode = 5 * _hashCode + _timeout; - _hashCode = 5 * _hashCode + _protocol.hashCode(); - _hashCode = 5 * _hashCode + _encoding.hashCode(); - _hashCode = 5 * _hashCode + _connectionId.hashCode(); - _hashCode = 5 * _hashCode + (_compress ? 1 : 0); + int h = 5381; + h = IceInternal.HashUtil.hashAdd(h, Ice.TCPEndpointType.value); + h = IceInternal.HashUtil.hashAdd(h, _host); + h = IceInternal.HashUtil.hashAdd(h, _port); + h = IceInternal.HashUtil.hashAdd(h, _timeout); + h = IceInternal.HashUtil.hashAdd(h, _protocol); + h = IceInternal.HashUtil.hashAdd(h, _encoding); + h = IceInternal.HashUtil.hashAdd(h, _connectionId); + h = IceInternal.HashUtil.hashAdd(h, _compress); + _hashCode = h; } private Instance _instance; diff --git a/java/src/IceInternal/UdpConnector.java b/java/src/IceInternal/UdpConnector.java index 1d231ef6b31..152e838dd58 100644 --- a/java/src/IceInternal/UdpConnector.java +++ b/java/src/IceInternal/UdpConnector.java @@ -56,13 +56,14 @@ final class UdpConnector implements Connector _encoding = encoding; _connectionId = connectionId; - _hashCode = _addr.getAddress().getHostAddress().hashCode(); - _hashCode = 5 * _hashCode + _addr.getPort(); - _hashCode = 5 * _hashCode + _mcastInterface.hashCode(); - _hashCode = 5 * _hashCode + _protocol.hashCode(); - _hashCode = 5 * _hashCode + _encoding.hashCode(); - _hashCode = 5 * _hashCode + _mcastTtl; - _hashCode = 5 * _hashCode + _connectionId.hashCode(); + _hashCode = 5381; + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _addr.getAddress().getHostAddress()); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _addr.getPort()); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _mcastInterface); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _protocol); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _encoding); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _mcastTtl); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _connectionId); } public boolean diff --git a/java/src/IceInternal/UdpEndpointI.java b/java/src/IceInternal/UdpEndpointI.java index d3a8c23f27f..8e78a33079c 100644 --- a/java/src/IceInternal/UdpEndpointI.java +++ b/java/src/IceInternal/UdpEndpointI.java @@ -583,15 +583,18 @@ final class UdpEndpointI extends EndpointI private void calcHashValue() { - _hashCode = _host.hashCode(); - _hashCode = 5 * _hashCode + _port; - _hashCode = 5 * _hashCode + _mcastInterface.hashCode(); - _hashCode = 5 * _hashCode + _mcastTtl; - _hashCode = 5 * _hashCode + (_connect ? 1 : 0); - _hashCode = 5 * _hashCode + _protocol.hashCode(); - _hashCode = 5 * _hashCode + _encoding.hashCode(); - _hashCode = 5 * _hashCode + _connectionId.hashCode(); - _hashCode = 5 * _hashCode + (_compress ? 1 : 0); + int h = 5381; + h = IceInternal.HashUtil.hashAdd(h, Ice.UDPEndpointType.value); + h = IceInternal.HashUtil.hashAdd(h, _host); + h = IceInternal.HashUtil.hashAdd(h, _port); + h = IceInternal.HashUtil.hashAdd(h, _mcastInterface); + h = IceInternal.HashUtil.hashAdd(h, _mcastTtl); + h = IceInternal.HashUtil.hashAdd(h, _connect); + h = IceInternal.HashUtil.hashAdd(h, _protocol); + h = IceInternal.HashUtil.hashAdd(h, _encoding); + h = IceInternal.HashUtil.hashAdd(h, _connectionId); + h = IceInternal.HashUtil.hashAdd(h, _compress); + _hashCode = h; } private Instance _instance; diff --git a/java/src/IceSSL/ConnectorI.java b/java/src/IceSSL/ConnectorI.java index a2478eb3867..12db25b4f18 100644 --- a/java/src/IceSSL/ConnectorI.java +++ b/java/src/IceSSL/ConnectorI.java @@ -91,12 +91,13 @@ final class ConnectorI implements IceInternal.Connector _encoding = encoding; _connectionId = connectionId; - _hashCode = _addr.getAddress().getHostAddress().hashCode(); - _hashCode = 5 * _hashCode + _addr.getPort(); - _hashCode = 5 * _hashCode + _timeout; - _hashCode = 5 * _hashCode + _protocol.hashCode(); - _hashCode = 5 * _hashCode + _encoding.hashCode(); - _hashCode = 5 * _hashCode + _connectionId.hashCode(); + _hashCode = 5381; + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _addr.getAddress().getHostAddress()); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _addr.getPort()); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _timeout); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _protocol); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _encoding); + _hashCode = IceInternal.HashUtil.hashAdd(_hashCode , _connectionId); } public boolean diff --git a/java/src/IceSSL/EndpointI.java b/java/src/IceSSL/EndpointI.java index 8ccd13656fc..e768b26d40e 100644 --- a/java/src/IceSSL/EndpointI.java +++ b/java/src/IceSSL/EndpointI.java @@ -554,13 +554,16 @@ final class EndpointI extends IceInternal.EndpointI private void calcHashValue() { - _hashCode = _host.hashCode(); - _hashCode = 5 * _hashCode + _port; - _hashCode = 5 * _hashCode + _timeout; - _hashCode = 5 * _hashCode + _protocol.hashCode(); - _hashCode = 5 * _hashCode + _encoding.hashCode(); - _hashCode = 5 * _hashCode + _connectionId.hashCode(); - _hashCode = 5 * _hashCode + (_compress ? 1 : 0); + int h = 5381; + h = IceInternal.HashUtil.hashAdd(h, EndpointType.value); + h = IceInternal.HashUtil.hashAdd(h, _host); + h = IceInternal.HashUtil.hashAdd(h, _port); + h = IceInternal.HashUtil.hashAdd(h, _timeout); + h = IceInternal.HashUtil.hashAdd(h, _protocol); + h = IceInternal.HashUtil.hashAdd(h, _encoding); + h = IceInternal.HashUtil.hashAdd(h, _connectionId); + h = IceInternal.HashUtil.hashAdd(h, _compress); + _hashCode = h; } private Instance _instance; diff --git a/java/test/Ice/hash/Client.java b/java/test/Ice/hash/Client.java new file mode 100644 index 00000000000..07bc968b7e7 --- /dev/null +++ b/java/test/Ice/hash/Client.java @@ -0,0 +1,429 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +package test.Ice.hash; + +public class Client +{ + private static void + test(boolean b) + { + if(!b) + { + throw new RuntimeException(); + } + } + + public static void main(String[] args) + { + int status = 0; + try + { + java.util.Map<Integer, Ice.ObjectPrx> seenProxy = new java.util.HashMap<Integer, Ice.ObjectPrx>(); + java.util.Map<Integer, Ice.Endpoint> seenEndpoint = new java.util.HashMap<Integer, Ice.Endpoint>(); + int proxyCollisions = 0; + int endpointCollisions = 0; + int i = 0; + int maxCollisions = 10; + int maxIterations = 10000; + + Ice.InitializationData initData = new Ice.InitializationData(); + initData.properties = Ice.Util.createProperties(args); + initData.properties.setProperty("Ice.Plugin.IceSSL", "IceSSL.PluginFactory"); + Ice.Communicator communicator = Ice.Util.initialize(args, initData); + + System.out.print("testing proxy & endpoint hash algorithm collisions... "); + { + java.util.Random rand = new java.util.Random(); + for(i = 0; proxyCollisions < maxCollisions && + endpointCollisions < maxCollisions && + i < maxIterations; ++i) + { + java.io.StringWriter sw = new java.io.StringWriter(); + sw.write(Integer.toString(i)); + sw.write(":tcp -p "); + sw.write(Integer.toString(rand.nextInt(65536))); + sw.write(" -t 10"); + sw.write(Integer.toString(rand.nextInt(1000000))); + sw.write(":udp -p "); + sw.write(Integer.toString(rand.nextInt(65536))); + sw.write(" -h "); + sw.write(Integer.toString(rand.nextInt(100))); + + Ice.ObjectPrx obj = communicator.stringToProxy(sw.toString()); + java.util.List<Ice.Endpoint> endpoints = new java.util.ArrayList<Ice.Endpoint>(java.util.Arrays.asList(obj.ice_getEndpoints())); + if(seenProxy.containsKey(obj.hashCode())) + { + if(obj.equals(seenProxy.get(obj.hashCode()))) + { + continue; // Same object + } + ++proxyCollisions; + } + else + { + seenProxy.put(obj.hashCode(), obj); + } + + java.util.Iterator<Ice.Endpoint> j = endpoints.iterator(); + while(j.hasNext()) + { + Ice.Endpoint endpoint = j.next(); + if(seenEndpoint.containsKey(endpoint.hashCode())) + { + if(endpoint.equals(seenEndpoint.get(endpoint.hashCode()))) + { + continue; // Same endpoint + } + ++endpointCollisions; + System.out.println(endpoint.toString() + " == " + seenEndpoint.get(endpoint.hashCode()).toString() + " == " + endpoint.hashCode()); + } + else + { + seenEndpoint.put(endpoint.hashCode(), endpoint); + } + // + // Check the same endpoint produce always the same hash + // + test(endpoint.hashCode() == endpoint.hashCode()); + } + // + // Check the same proxy produce always the same hash + // + test(obj.hashCode() == obj.hashCode()); + } + test(proxyCollisions < maxCollisions); + test(endpointCollisions < maxCollisions); + + proxyCollisions = 0; + seenProxy = new java.util.HashMap<Integer, Ice.ObjectPrx>(); + for(i = 0; proxyCollisions < maxCollisions && + endpointCollisions < maxCollisions && + i < maxIterations; ++i) + { + java.io.StringWriter sw = new java.io.StringWriter(); + sw.write(Integer.toString(i)); + sw.write(":tcp -p "); + sw.write(Integer.toString(rand.nextInt(65536))); + sw.write(" -t 10"); + sw.write(Integer.toString(rand.nextInt(1000000))); + sw.write(":udp -p "); + sw.write(Integer.toString(rand.nextInt(65536))); + sw.write(" -h "); + sw.write(Integer.toString(rand.nextInt(100))); + + Ice.ProxyIdentityKey obj = new Ice.ProxyIdentityKey(communicator.stringToProxy(sw.toString())); + if(seenProxy.containsKey(obj.hashCode())) + { + ++proxyCollisions; + } + else + { + seenProxy.put(obj.hashCode(), obj.getProxy()); + } + // + // Check the same proxy produce always the same hash + // + test(obj.hashCode() == obj.hashCode()); + } + test(proxyCollisions < maxCollisions); + + proxyCollisions = 0; + seenProxy = new java.util.HashMap<Integer, Ice.ObjectPrx>(); + for(i = 0; proxyCollisions < maxCollisions && + endpointCollisions < maxCollisions && + i < maxIterations; ++i) + { + java.io.StringWriter sw = new java.io.StringWriter(); + sw.write(Integer.toString(i)); + sw.write(":tcp -p "); + sw.write(Integer.toString(rand.nextInt(65536))); + sw.write(" -t 10"); + sw.write(Integer.toString(rand.nextInt(1000000))); + sw.write(":udp -p "); + sw.write(Integer.toString(rand.nextInt(65536))); + sw.write(" -h "); + sw.write(Integer.toString(rand.nextInt(100))); + + Ice.ProxyIdentityFacetKey obj = new Ice.ProxyIdentityFacetKey(communicator.stringToProxy(sw.toString())); + if(seenProxy.containsKey(obj.hashCode())) + { + ++proxyCollisions; + } + else + { + seenProxy.put(obj.hashCode(), obj.getProxy()); + } + // + // Check the same proxy produce always the same hash + // + test(obj.hashCode() == obj.hashCode()); + } + test(proxyCollisions < maxCollisions); + + Ice.ObjectPrx prx1 = communicator.stringToProxy("Glacier2/router:tcp -p 10010"); + Ice.ObjectPrx prx2 = communicator.stringToProxy("Glacier2/router:ssl -p 10011"); + Ice.ObjectPrx prx3 = communicator.stringToProxy("Glacier2/router:udp -p 10012"); + Ice.ObjectPrx prx4 = communicator.stringToProxy("Glacier2/router:tcp -h zeroc.com -p 10010"); + Ice.ObjectPrx prx5 = communicator.stringToProxy("Glacier2/router:ssl -h zeroc.com -p 10011"); + Ice.ObjectPrx prx6 = communicator.stringToProxy("Glacier2/router:udp -h zeroc.com -p 10012"); + Ice.ObjectPrx prx7 = communicator.stringToProxy("Glacier2/router:tcp -p 10010 -t 10000"); + Ice.ObjectPrx prx8 = communicator.stringToProxy("Glacier2/router:ssl -p 10011 -t 10000"); + Ice.ObjectPrx prx9 = communicator.stringToProxy("Glacier2/router:tcp -h zeroc.com -p 10010 -t 10000"); + Ice.ObjectPrx prx10 = communicator.stringToProxy("Glacier2/router:ssl -h zeroc.com -p 10011 -t 10000"); + + java.util.Map<String, Integer> proxyMap = new java.util.HashMap<String, Integer>(); + proxyMap.put("prx1", prx1.hashCode()); + proxyMap.put("prx2", prx2.hashCode()); + proxyMap.put("prx3", prx3.hashCode()); + proxyMap.put("prx4", prx4.hashCode()); + proxyMap.put("prx5", prx5.hashCode()); + proxyMap.put("prx6", prx6.hashCode()); + proxyMap.put("prx7", prx7.hashCode()); + proxyMap.put("prx8", prx8.hashCode()); + proxyMap.put("prx9", prx9.hashCode()); + proxyMap.put("prx10", prx10.hashCode()); + + test(communicator.stringToProxy("Glacier2/router:tcp -p 10010").hashCode() == proxyMap.get("prx1")); + test(communicator.stringToProxy("Glacier2/router:ssl -p 10011").hashCode() == proxyMap.get("prx2")); + test(communicator.stringToProxy("Glacier2/router:udp -p 10012").hashCode() == proxyMap.get("prx3")); + test(communicator.stringToProxy("Glacier2/router:tcp -h zeroc.com -p 10010").hashCode() == proxyMap.get("prx4")); + test(communicator.stringToProxy("Glacier2/router:ssl -h zeroc.com -p 10011").hashCode() == proxyMap.get("prx5")); + test(communicator.stringToProxy("Glacier2/router:udp -h zeroc.com -p 10012").hashCode() == proxyMap.get("prx6")); + test(communicator.stringToProxy("Glacier2/router:tcp -p 10010 -t 10000").hashCode() == proxyMap.get("prx7")); + test(communicator.stringToProxy("Glacier2/router:ssl -p 10011 -t 10000").hashCode() == proxyMap.get("prx8")); + test(communicator.stringToProxy("Glacier2/router:tcp -h zeroc.com -p 10010 -t 10000").hashCode() == proxyMap.get("prx9")); + test(communicator.stringToProxy("Glacier2/router:ssl -h zeroc.com -p 10011 -t 10000").hashCode() == proxyMap.get("prx10")); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx1).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx1).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx2).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx2).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx3).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx3).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx4).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx4).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx5).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx5).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx6).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx6).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx7).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx7).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx8).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx8).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx9).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx9).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx10).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx10).hashCode()); + } + + System.out.println("ok"); + + System.out.print("testing struct hash algorithm collisions... "); + { + java.util.Map<Integer,Test.PointF> seenPointF = new java.util.HashMap<Integer, Test.PointF>(); + java.util.Random rand = new java.util.Random(); + int structCollisions = 0; + for(i = 0; i < maxIterations && structCollisions < maxCollisions; ++i) + { + Test.PointF pf = new Test.PointF(rand.nextFloat(), rand.nextFloat(), rand.nextFloat()); + if(seenPointF.containsKey(pf.hashCode())) + { + if(pf.equals(seenPointF.get(pf.hashCode()))) + { + continue; // same object + } + structCollisions++; + } + else + { + seenPointF.put(pf.hashCode(), pf); + } + // + // Check the same struct produce always the same hash + // + test(pf.hashCode() == pf.hashCode()); + } + test(structCollisions < maxCollisions); + + java.util.Map<Integer,Test.PointD> seenPointD = new java.util.HashMap<Integer, Test.PointD>(); + rand = new java.util.Random(); + structCollisions = 0; + for(i = 0; i < maxIterations && structCollisions < maxCollisions; ++i) + { + Test.PointD pd = new Test.PointD(rand.nextDouble(), rand.nextDouble(), rand.nextDouble()); + if(seenPointD.containsKey(pd.hashCode())) + { + if(pd.equals(seenPointF.get(pd.hashCode()))) + { + continue; // same object + } + structCollisions++; + } + else + { + seenPointD.put(pd.hashCode(), pd); + } + // + // Check the same struct produce always the same hash + // + test(pd.hashCode() == pd.hashCode()); + } + test(structCollisions < maxCollisions); + + java.util.Map<Integer,Test.Polyline> seenPolyline = new java.util.HashMap<Integer, Test.Polyline>(); + structCollisions = 0; + for(i = 0; i < maxIterations && structCollisions < maxCollisions; ++i) + { + Test.Polyline polyline = new Test.Polyline(); + java.util.List<Test.Point> vertices = new java.util.ArrayList<Test.Point>(); + for(int j = 0; j < 100; ++j) + { + vertices.add(new Test.Point(rand.nextInt(100), rand.nextInt(100))); + } + polyline.vertices = new Test.Point[vertices.size()]; + vertices.toArray(polyline.vertices); + + if(seenPolyline.containsKey(polyline.hashCode())) + { + if(polyline.equals(seenPolyline.get(polyline.hashCode()))) + { + continue; // same object + } + structCollisions++; + } + else + { + seenPolyline.put(polyline.hashCode(), polyline); + } + // + // Check the same struct produce always the same hash + // + test(polyline.hashCode() == polyline.hashCode()); + } + test(structCollisions < maxCollisions); + + java.util.Map<Integer,Test.ColorPalette> seenColorPalette = new java.util.HashMap<Integer, Test.ColorPalette>(); + structCollisions = 0; + for(i = 0; i < maxIterations && structCollisions < maxCollisions; ++i) + { + Test.ColorPalette colorPalette = new Test.ColorPalette(); + colorPalette.colors = new java.util.HashMap<Integer, Test.Color>(); + for(int j = 0; j < 100; ++j) + { + colorPalette.colors.put(j, new Test.Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(255))); + } + + if(seenColorPalette.containsKey(colorPalette.hashCode())) + { + if(colorPalette.equals(seenColorPalette.get(colorPalette.hashCode()))) + { + continue; // same object + } + structCollisions++; + } + else + { + seenColorPalette.put(colorPalette.hashCode(), colorPalette); + } + // + // Check the same struct produce always the same hash + // + test(colorPalette.hashCode() == colorPalette.hashCode()); + } + test(structCollisions < maxCollisions); + + java.util.Map<Integer,Test.Color> seenColor = new java.util.HashMap<Integer, Test.Color>(); + rand = new java.util.Random(); + structCollisions = 0; + for(i = 0; i < maxIterations && structCollisions < maxCollisions; ++i) + { + Test.Color c = new Test.Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)); + if(seenColor.containsKey(c.hashCode())) + { + if(c.equals(seenColor.get(c.hashCode()))) + { + continue; // same object + } + structCollisions++; + } + else + { + seenColor.put(c.hashCode(), c); + } + // + // Check the same struct produce always the same hash + // + test(c.hashCode() == c.hashCode()); + } + test(structCollisions < maxCollisions); + + structCollisions = 0; + java.util.Map<Integer,Test.Draw> seenDraw = new java.util.HashMap<Integer, Test.Draw>(); + structCollisions = 0; + for(i = 0; i < maxIterations && structCollisions < maxCollisions; ++i) + { + Test.Draw draw = new Test.Draw( + new Test.Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)), + new Test.Pen(rand.nextInt(10), + new Test.Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(255))), + false); + + if(seenDraw.containsKey(draw.hashCode())) + { + if(draw.equals(seenDraw.get(draw.hashCode()))) + { + continue; // same object + } + structCollisions++; + } + else + { + seenDraw.put(draw.hashCode(), draw); + } + // + // Check the same struct produce always the same hash + // + test(draw.hashCode() == draw.hashCode()); + } + test(structCollisions < maxCollisions); + } + System.out.println("ok"); + + if(communicator != null) + { + try + { + communicator.destroy(); + } + catch(Ice.LocalException ex) + { + System.out.println(ex.toString()); + status = 1; + } + } + } + catch(Exception ex) + { + System.out.println(ex.toString()); + status = 1; + } + System.gc(); + System.exit(status); + } +} diff --git a/java/test/Ice/hash/Test.ice b/java/test/Ice/hash/Test.ice new file mode 100644 index 00000000000..29f0a0c803d --- /dev/null +++ b/java/test/Ice/hash/Test.ice @@ -0,0 +1,71 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +#pragma once + +module Test +{ + +struct PointF +{ + float x; + float y; + float z; +}; + +struct PointD +{ + double x; + double y; + double z; +}; + +struct Point +{ + int x; + int y; +}; +sequence<Point> Points; + +struct Polyline +{ + Points vertices; +}; + +struct Color +{ + int r; + int g; + int b; + int a; +}; + + +dictionary<int, Color> StringColorMap; + +struct ColorPalette +{ + StringColorMap colors; +}; + +class Pen +{ + int thickness; + Test::Color color; +}; + +struct Draw +{ + Test::Color backgroundColor; + Test::Pen pen; + bool shared; +}; + +}; + diff --git a/java/test/Ice/hash/run.py b/java/test/Ice/hash/run.py new file mode 100755 index 00000000000..de3d7b155fb --- /dev/null +++ b/java/test/Ice/hash/run.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# ********************************************************************** +# +# Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved. +# +# This copy of Ice is licensed to you under the terms described in the +# ICE_LICENSE file included in this distribution. +# +# ********************************************************************** + +import os, sys + +path = [ ".", "..", "../..", "../../..", "../../../.." ] +head = os.path.dirname(sys.argv[0]) +if len(head) > 0: + path = [os.path.join(head, p) for p in path] +path = [os.path.abspath(p) for p in path if os.path.exists(os.path.join(p, "scripts", "TestUtil.py")) ] +if len(path) == 0: + raise "can't find toplevel directory!" +sys.path.append(os.path.join(path[0])) +from scripts import * + +print "starting client...", +clientProc = TestUtil.startClient("test.Ice.hash.Client",startReader=False) +print "ok" +clientProc.startReader() +clientProc.waitTestSuccess() + |