示例#1
0
        public void TestByRefTypes()
        {
            DefType intType = _context.GetWellKnownType(WellKnownType.Int32);

            int expHashInt = TypeHashingAlgorithms.ComputeNameHashCode("System.Int32");

            Assert.Equal(expHashInt, intType.GetHashCode());

            int      expHashIntByRef = TypeHashingAlgorithms.ComputeByrefTypeHashCode(expHashInt);
            TypeDesc intByRefType    = _context.GetByRefType(intType);

            Assert.Equal(expHashIntByRef, intByRefType.GetHashCode());
        }
示例#2
0
        public void TestInstantiatedMethods()
        {
            MetadataType nonNestedType = (MetadataType)_testModule.GetType("Hashcode", "NonNestedType");
            MetadataType genericType   = (MetadataType)_testModule.GetType("Hashcode", "GenericType`2");
            DefType      intType       = _context.GetWellKnownType(WellKnownType.Int32);
            DefType      stringType    = _context.GetWellKnownType(WellKnownType.String);

            MetadataType genericTypeOfIntString = genericType.MakeInstantiatedType(intType, stringType);
            MetadataType genericTypeOfStringInt = genericType.MakeInstantiatedType(stringType, intType);

            // build up expected hash codes for the above
            int expHashNonNestedType = TypeHashingAlgorithms.ComputeNameHashCode("Hashcode.NonNestedType");

            Assert.Equal(expHashNonNestedType, nonNestedType.GetHashCode());
            int expHashGenType = TypeHashingAlgorithms.ComputeNameHashCode("Hashcode.GenericType`2");

            Assert.Equal(expHashGenType, genericType.GetHashCode());
            int expHashInt = TypeHashingAlgorithms.ComputeNameHashCode("System.Int32");

            Assert.Equal(expHashInt, intType.GetHashCode());
            int expHashString = TypeHashingAlgorithms.ComputeNameHashCode("System.String");

            Assert.Equal(expHashString, stringType.GetHashCode());
            int expHashGenTypeOfIS = TypeHashingAlgorithms.ComputeGenericInstanceHashCode(expHashGenType, new int[] { expHashInt, expHashString });

            Assert.Equal(expHashGenTypeOfIS, genericTypeOfIntString.GetHashCode());
            int expHashGenTypeOfSI = TypeHashingAlgorithms.ComputeGenericInstanceHashCode(expHashGenType, new int[] { expHashString, expHashInt });

            Assert.Equal(expHashGenTypeOfSI, genericTypeOfStringInt.GetHashCode());

            // Test that instantiated method's have the right hashes

            int genMethodNameHash     = TypeHashingAlgorithms.ComputeNameHashCode("GenericMethod");
            int genMethodNameAndIHash = TypeHashingAlgorithms.ComputeGenericInstanceHashCode(genMethodNameHash, new int[] { expHashInt });
            int genMethodNameAndSHash = TypeHashingAlgorithms.ComputeGenericInstanceHashCode(genMethodNameHash, new int[] { expHashString });


            Action <MetadataType, int> testSequence = (MetadataType typeWithGenericMethod, int expectedTypeHash) =>
            {
                // Uninstantiated Generic method
                MethodDesc genMethod = typeWithGenericMethod.GetMethod("GenericMethod", null);
                Assert.Equal(TypeHashingAlgorithms.ComputeMethodHashCode(expectedTypeHash, genMethodNameHash), genMethod.GetHashCode());

                // Instantiated over int
                MethodDesc genMethodI = genMethod.MakeInstantiatedMethod(intType);
                Assert.Equal(TypeHashingAlgorithms.ComputeMethodHashCode(expectedTypeHash, genMethodNameAndIHash), genMethodI.GetHashCode());

                // Instantiated over string
                MethodDesc genMethodS = genMethod.MakeInstantiatedMethod(stringType);
                Assert.Equal(TypeHashingAlgorithms.ComputeMethodHashCode(expectedTypeHash, genMethodNameAndSHash), genMethodS.GetHashCode());

                // Assert they aren't the same as the other hashes
                Assert.NotEqual(genMethodI.GetHashCode(), genMethodS.GetHashCode());
                Assert.NotEqual(genMethodI.GetHashCode(), genMethod.GetHashCode());
                Assert.NotEqual(genMethodS.GetHashCode(), genMethod.GetHashCode());
            };

            // Test cases on non-generic type
            testSequence(nonNestedType, expHashNonNestedType);

            // Test cases on generic type
            testSequence(genericType, expHashGenType);

            // Test cases on instantiated generic type
            testSequence(genericTypeOfIntString, expHashGenTypeOfIS);
            testSequence(genericTypeOfStringInt, expHashGenTypeOfSI);
        }
示例#3
0
        void TestGenericTypes()
        {
            MetadataType ilistType          = (MetadataType)_testModule.GetType("System.Collections.Generic", "IList`1");
            DefType      systemArrayType    = _context.GetWellKnownType(WellKnownType.Array);
            DefType      ilistOfSystemArray = ilistType.MakeInstantiatedType(systemArrayType);

            int expectedIListOfTHashcode    = TypeHashingAlgorithms.ComputeNameHashCode("System.Collections.Generic.IList`1");
            int expectedSystemArrayHashcode = TypeHashingAlgorithms.ComputeNameHashCode("System.Array");

            Assert.Equal(expectedIListOfTHashcode, ilistType.GetHashCode());
            Assert.Equal(TypeHashingAlgorithms.ComputeGenericInstanceHashCode(expectedIListOfTHashcode, new int[] { expectedSystemArrayHashcode }), ilistOfSystemArray.GetHashCode());
        }
示例#4
0
 internal override int LookupHashCode()
 {
     return(_typeToLookup.GetHashCode());
 }