Exemplo n.º 1
0
        public void TestPointerArrayInterfaces()
        {
            MetadataType systemArrayType = _testModule.GetType("System", "Array");
            TypeDesc     intType         = _context.GetWellKnownType(WellKnownType.Int32);
            TypeDesc     intPointerType  = _context.GetPointerType(intType);

            ArrayType intPointerArray = _context.GetArrayType(intPointerType);

            // Pointer arrays should have the same set of interfaces as System.Array
            Assert.Equal(systemArrayType.RuntimeInterfaces, intPointerArray.RuntimeInterfaces);
        }
Exemplo n.º 2
0
        public void TestPointerTypes()
        {
            DefType intType = _context.GetWellKnownType(WellKnownType.Int32);

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

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

            int      expHashIntPointer = TypeHashingAlgorithms.ComputePointerTypeHashCode(expHashInt);
            TypeDesc intPointerType    = _context.GetPointerType(intType);

            Assert.Equal(expHashIntPointer, intPointerType.GetHashCode());
        }
        public void TestConstructedTypeAdjustment()
        {
            TypeDesc intType    = _context.GetWellKnownType(WellKnownType.Int32);
            TypeDesc stringType = _context.GetWellKnownType(WellKnownType.String);
            TypeDesc charType   = _context.GetWellKnownType(WellKnownType.Char);
            TypeDesc objectType = _context.GetWellKnownType(WellKnownType.Object);

            MetadataType genericOpenType = _testModule.GetType("GenericTypes", "TwoParamGenericClass`2");

            InstantiatedType genericOfCharObject = genericOpenType.MakeInstantiatedType(charType, objectType);
            InstantiatedType genericOfCharString = genericOpenType.MakeInstantiatedType(charType, stringType);
            InstantiatedType genericOfIntString  = genericOpenType.MakeInstantiatedType(intType, stringType);
            InstantiatedType genericOfIntObject  = genericOpenType.MakeInstantiatedType(intType, objectType);

            Assert.Equal(true, genericOfCharObject.IsConstructedOverType(new TypeDesc[] { charType }));
            Assert.Equal(true, genericOfCharObject.IsConstructedOverType(new TypeDesc[] { objectType }));
            Assert.Equal(false, genericOfCharObject.IsConstructedOverType(new TypeDesc[] { intType }));
            Assert.Equal(false, genericOfCharObject.IsConstructedOverType(new TypeDesc[] { stringType }));
            Assert.Equal(false, genericOfCharObject.IsConstructedOverType(new TypeDesc[] { genericOpenType }));

            Assert.Equal(true, genericOfCharString.IsConstructedOverType(new TypeDesc[] { charType }));
            Assert.Equal(false, genericOfCharString.IsConstructedOverType(new TypeDesc[] { objectType }));
            Assert.Equal(false, genericOfCharString.IsConstructedOverType(new TypeDesc[] { intType }));
            Assert.Equal(true, genericOfCharString.IsConstructedOverType(new TypeDesc[] { stringType }));

            // Test direct replacement
            TypeDesc testDirectReplaceAllTypes = genericOfCharObject.ReplaceTypesInConstructionOfType(new TypeDesc[] { charType, objectType }, new TypeDesc[] { intType, stringType });

            Assert.Equal(genericOfIntString, testDirectReplaceAllTypes);

            // Test direct replacement where not all types are replaced
            TypeDesc testDirectReplaceFirstType = genericOfCharObject.ReplaceTypesInConstructionOfType(new TypeDesc[] { charType }, new TypeDesc[] { intType });

            Assert.Equal(genericOfIntObject, testDirectReplaceFirstType);

            TypeDesc testDirectReplaceSecondType = genericOfCharObject.ReplaceTypesInConstructionOfType(new TypeDesc[] { objectType }, new TypeDesc[] { stringType });

            Assert.Equal(genericOfCharString, testDirectReplaceSecondType);

            // Test Arrays
            TypeDesc arrayChar = _context.GetArrayType(charType);

            Assert.False(arrayChar.IsMdArray);
            Assert.True(arrayChar.IsSzArray);
            Assert.True(arrayChar.IsArray);

            TypeDesc arrayInt = _context.GetArrayType(intType);

            Assert.False(arrayInt.IsMdArray);
            Assert.True(arrayInt.IsSzArray);
            Assert.True(arrayInt.IsArray);

            InstantiatedType genericOfCharArrayObject        = genericOpenType.MakeInstantiatedType(arrayChar, objectType);
            InstantiatedType genericOfIntArrayObject         = genericOpenType.MakeInstantiatedType(arrayInt, objectType);
            TypeDesc         testReplaceTypeInArrayInGeneric = genericOfCharArrayObject.ReplaceTypesInConstructionOfType(new TypeDesc[] { charType }, new TypeDesc[] { intType });

            Assert.Equal(genericOfIntArrayObject, testReplaceTypeInArrayInGeneric);

            // Test multidimensional arrays
            TypeDesc mdArrayChar = _context.GetArrayType(charType, 3);

            Assert.True(mdArrayChar.IsMdArray);
            Assert.False(mdArrayChar.IsSzArray);
            Assert.True(mdArrayChar.IsArray);

            TypeDesc mdArrayInt = _context.GetArrayType(intType, 3);

            Assert.True(mdArrayInt.IsMdArray);
            Assert.False(mdArrayInt.IsSzArray);
            Assert.True(mdArrayInt.IsArray);

            InstantiatedType genericOfCharMdArrayObject        = genericOpenType.MakeInstantiatedType(mdArrayChar, objectType);
            InstantiatedType genericOfIntMdArrayObject         = genericOpenType.MakeInstantiatedType(mdArrayInt, objectType);
            TypeDesc         testReplaceTypeInMdArrayInGeneric = genericOfCharMdArrayObject.ReplaceTypesInConstructionOfType(new TypeDesc[] { charType }, new TypeDesc[] { intType });

            Assert.Equal(genericOfIntMdArrayObject, testReplaceTypeInMdArrayInGeneric);

            // Test pointers
            TypeDesc charPointer = _context.GetPointerType(charType);
            TypeDesc intPointer  = _context.GetPointerType(intType);
            TypeDesc testReplaceTypeInPointer = charPointer.ReplaceTypesInConstructionOfType(new TypeDesc[] { charType }, new TypeDesc[] { intType });

            Assert.Equal(intPointer, testReplaceTypeInPointer);

            Assert.Equal(true, charPointer.IsConstructedOverType(new TypeDesc[] { charType }));
            Assert.Equal(false, charPointer.IsConstructedOverType(new TypeDesc[] { intType }));

            // Test byref
            TypeDesc charByRef = _context.GetByRefType(charType);
            TypeDesc intByRef  = _context.GetByRefType(intType);
            TypeDesc testReplaceTypeInByRef = charByRef.ReplaceTypesInConstructionOfType(new TypeDesc[] { charType }, new TypeDesc[] { intType });

            Assert.Equal(intByRef, testReplaceTypeInByRef);

            Assert.Equal(true, charByRef.IsConstructedOverType(new TypeDesc[] { charType }));
            Assert.Equal(false, charByRef.IsConstructedOverType(new TypeDesc[] { intType }));

            // Test replace type entirely
            TypeDesc testReplaceTypeEntirely = charByRef.ReplaceTypesInConstructionOfType(new TypeDesc[] { charByRef }, new TypeDesc[] { intByRef });

            Assert.Equal(intByRef, testReplaceTypeEntirely);
            Assert.Equal(true, charByRef.IsConstructedOverType(new TypeDesc[] { charByRef }));
        }