Exemple #1
0
        public void TestSimpleNames()
        {
            {
                TypeDesc result = _testModule.GetTypeByCustomAttributeTypeName("TypeNameParsing.Simple");
                Assert.Equal(_simpleType, result);
            }

            {
                TypeDesc result = _testModule.GetTypeByCustomAttributeTypeName("TypeNameParsing.Simple+Nested");
                Assert.Equal(_nestedType, result);
            }

            {
                TypeDesc result = _testModule.GetTypeByCustomAttributeTypeName("TypeNameParsing.Simple+Nested+NestedTwice");
                Assert.Equal(_nestedTwiceType, result);
            }

            {
                TypeDesc result = _testModule.GetTypeByCustomAttributeTypeName("System.Int32, " + _coreAssemblyQualifier);
                Assert.Equal(_context.GetWellKnownType(WellKnownType.Int32), result);
            }

            {
                TypeDesc result = _testModule.GetTypeByCustomAttributeTypeName("TypeNameParsing.VeryGeneric`3");
                Assert.Equal(_veryGenericType, result);
            }
        }
        public void TestAutoTypeLayoutMinPacking(WellKnownType type, int expectedSize)
        {
            MetadataType     minPackingType = _testModule.GetType("Auto", "MinPacking`1");
            InstantiatedType inst           = minPackingType.MakeInstantiatedType(_context.GetWellKnownType(type));

            Assert.Equal(expectedSize, inst.InstanceFieldSize.AsInt);
        }
Exemple #3
0
        public void TestMultidimensionalArrays()
        {
            DefType  systemArrayType = _context.GetWellKnownType(WellKnownType.Array);
            TypeDesc objectType      = _context.GetWellKnownType(WellKnownType.Object);

            ArrayType objectMDArray = _context.GetArrayType(objectType, 2);

            // MD array should have the same set of interfaces as System.Array
            Assert.Equal(systemArrayType.RuntimeInterfaces, objectMDArray.RuntimeInterfaces);
        }
        public ValueTypeShapeCharacteristicsTests()
        {
            _context = new TestTypeSystemContext(TargetArchitecture.X64);
            var systemModule = _context.CreateModuleForSimpleName("CoreTestAssembly");
            _context.SetSystemModule(systemModule);

            _testModule = systemModule;

            _singleType = _context.GetWellKnownType(WellKnownType.Single);
            _doubleType = _context.GetWellKnownType(WellKnownType.Double);
        }
        public VirtualFunctionOverrideTests()
        {
            _context = new TestTypeSystemContext(TargetArchitecture.X64);
            var systemModule = _context.CreateModuleForSimpleName("CoreTestAssembly");
            _context.SetSystemModule(systemModule);

            _testModule = systemModule;

            _stringType = _context.GetWellKnownType(WellKnownType.String);
            _voidType = _context.GetWellKnownType(WellKnownType.Void);
        }
        public void TestHfaPrimitives()
        {
            DefType singleType = _context.GetWellKnownType(WellKnownType.Single);
            DefType doubleType = _context.GetWellKnownType(WellKnownType.Double);

            Assert.True(singleType.IsHomogeneousAggregate);
            Assert.Equal(Float32Aggregate, singleType.ValueTypeShapeCharacteristics);

            Assert.True(doubleType.IsHomogeneousAggregate);
            Assert.Equal(Float64Aggregate, doubleType.ValueTypeShapeCharacteristics);
        }
        public VirtualFunctionOverrideTests()
        {
            _context = new TestTypeSystemContext(TargetArchitecture.X64);
            var systemModule = _context.CreateModuleForSimpleName("CoreTestAssembly");

            _context.SetSystemModule(systemModule);

            _testModule = systemModule;

            _stringType = _context.GetWellKnownType(WellKnownType.String);
            _voidType   = _context.GetWellKnownType(WellKnownType.Void);
        }
        public ValueTypeShapeCharacteristicsTests()
        {
            _context = new TestTypeSystemContext(TargetArchitecture.ARM);
            var systemModule = _context.CreateModuleForSimpleName("CoreTestAssembly");

            _context.SetSystemModule(systemModule);

            _testModule = systemModule;

            _singleType = _context.GetWellKnownType(WellKnownType.Single);
            _doubleType = _context.GetWellKnownType(WellKnownType.Double);
        }
Exemple #9
0
        public void TestMultidimensionalArrays()
        {
            DefType  systemArrayType = _context.GetWellKnownType(WellKnownType.Array);
            TypeDesc objectType      = _context.GetWellKnownType(WellKnownType.Object);

            ArrayType objectMDArrayRank1 = _context.GetArrayType(objectType, 1);
            ArrayType objectMDArrayRank2 = _context.GetArrayType(objectType, 2);
            ArrayType objectMDArrayRank3 = _context.GetArrayType(objectType, 3);

            Assert.Equal(TypeHashingAlgorithms.ComputeArrayTypeHashCode(objectType.GetHashCode(), 1), objectMDArrayRank1.GetHashCode());
            Assert.Equal(TypeHashingAlgorithms.ComputeArrayTypeHashCode(objectType.GetHashCode(), 2), objectMDArrayRank2.GetHashCode());
            Assert.Equal(TypeHashingAlgorithms.ComputeArrayTypeHashCode(objectType.GetHashCode(), 3), objectMDArrayRank3.GetHashCode());
        }
        public void TestSerializedSignatureWithReferenceToMDIntArray()
        {
            var typeInInitialContext = _context.GetWellKnownType(WellKnownType.Int32).MakeArrayType(3);

            // Create assembly with reference to interesting type
            TypeSystemMetadataEmitter metadataEmitter = new TypeSystemMetadataEmitter(new System.Reflection.AssemblyName("Lookup"), _context);
            var    token    = metadataEmitter.GetTypeRef(typeInInitialContext);
            Stream peStream = new MemoryStream();

            metadataEmitter.SerializeToStream(peStream);
            peStream.Seek(0, SeekOrigin.Begin);


            // Create new TypeSystemContext with just created assembly inside
            var lookupContext = new TestTypeSystemContext(TargetArchitecture.X64);
            var systemModule  = lookupContext.CreateModuleForSimpleName("CoreTestAssembly");

            lookupContext.SetSystemModule(systemModule);
            lookupContext.CreateModuleForSimpleName("Lookup", peStream);

            // Use generated assembly to trigger a load through the token created above and verify that it loads correctly
            var      ilLookupModule       = (EcmaModule)lookupContext.GetModuleForSimpleName("Lookup");
            TypeDesc int32ArrayFromLookup = ilLookupModule.GetType(token);
            var      typeInLookupContext  = lookupContext.GetWellKnownType(WellKnownType.Int32).MakeArrayType(3);

            Assert.Equal(typeInLookupContext, int32ArrayFromLookup);
        }
        public void TestHfaNegative()
        {
            var nonHfaEmptyStruct = _testModule.GetType("ValueTypeShapeCharacteristics", "NonHfaEmptyStruct");

            Assert.False(nonHfaEmptyStruct.IsHfa);

            var nonHfaStruct = _testModule.GetType("ValueTypeShapeCharacteristics", "NonHfaStruct");

            Assert.False(nonHfaStruct.IsHfa);

            var nonHfaMixedStruct = _testModule.GetType("ValueTypeShapeCharacteristics", "NonHfaMixedStruct");

            Assert.False(nonHfaMixedStruct.IsHfa);

            var nonHfaCompositeStruct = _testModule.GetType("ValueTypeShapeCharacteristics", "NonHfaCompositeStruct");

            Assert.False(nonHfaCompositeStruct.IsHfa);

            var nonHfaStructWithManyFields = _testModule.GetType("ValueTypeShapeCharacteristics", "NonHfaStructWithManyFields");

            Assert.False(nonHfaStructWithManyFields.IsHfa);

            var objectType = _context.GetWellKnownType(WellKnownType.Object);

            Assert.False(objectType.IsHfa);
        }
Exemple #12
0
        public void TestVirtualDispatchOnGenericType()
        {
            // Verifies that virtual dispatch to a non-generic method on a generic instantiation works
            DefType         objectType     = _context.GetWellKnownType(WellKnownType.Object);
            MethodSignature toStringSig    = new MethodSignature(MethodSignatureFlags.None, 0, _stringType, Array.Empty <TypeDesc>());
            MethodDesc      objectToString = objectType.GetMethod("ToString", toStringSig);

            Assert.NotNull(objectToString);
            MetadataType     openTestType     = _testModule.GetType("VirtualFunctionOverride", "SimpleGeneric`1");
            InstantiatedType testInstance     = openTestType.MakeInstantiatedType(objectType);
            MethodDesc       targetOnInstance = testInstance.GetMethod("ToString", toStringSig);

            MethodDesc targetMethod = testInstance.FindVirtualFunctionTargetMethodOnObjectType(objectToString);

            Assert.Equal(targetOnInstance, targetMethod);
        }
        public void TestInstantiation()
        {
            MetadataType t = _testModule.GetType("GenericTypes", "GenericClass`1");

            // Verify that we get just type definitions.
            Assert.NotNull(t);
            Assert.True(t.IsTypeDefinition);
            Assert.NotNull(t.Instantiation);
            Assert.Equal(t.Instantiation.Length, 1);
            Assert.True(t.Instantiation[0].IsTypeDefinition);

            // Verify that we got a method definition
            MethodDesc fooMethod = t.GetMethods().First(m => m.Name == "Foo");

            Assert.True(fooMethod.IsTypicalMethodDefinition);

            // Verify that instantiating a method definition has no effect
            MethodDesc instantiatedMethod = fooMethod.InstantiateSignature(new Instantiation(_context.GetWellKnownType(WellKnownType.Int32)), Instantiation.Empty);

            Assert.Same(fooMethod, instantiatedMethod);

            MetadataType instantiatedType = t.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Int32));

            // Verify properties of the instantiated type
            Assert.NotNull(instantiatedType);
            Assert.False(instantiatedType.IsTypeDefinition);
            Assert.NotNull(instantiatedType.Instantiation);
            Assert.Equal(instantiatedType.Instantiation.Length, 1);
            Assert.Equal(instantiatedType.Instantiation[0], _context.GetWellKnownType(WellKnownType.Int32));

            // Verify that we get an instantiated method with the proper signature
            MethodDesc fooInstantiatedMethod = instantiatedType.GetMethods().First(m => m.Name == "Foo");

            Assert.False(fooInstantiatedMethod.IsTypicalMethodDefinition);
            Assert.Equal(fooInstantiatedMethod.Signature.ReturnType, _context.GetWellKnownType(WellKnownType.Int32));
            Assert.Same(fooInstantiatedMethod.GetTypicalMethodDefinition(), fooMethod);
            // This is not a generic method, so they should be the same
            Assert.Same(fooInstantiatedMethod.GetMethodDefinition(), fooInstantiatedMethod);

            // Verify that instantiating a type definition has no effect
            TypeDesc newType = t.InstantiateSignature(new Instantiation(_context.GetWellKnownType(WellKnownType.Int32)), Instantiation.Empty);

            Assert.NotNull(newType);
            Assert.Same(newType, t);
        }
Exemple #14
0
        public void TestMetadataEnumerationAlgorithm()
        {
            var algo = new MetadataVirtualMethodEnumerationAlgorithm();

            DefType objectType    = _context.GetWellKnownType(WellKnownType.Object);
            var     objectMethods = algo.ComputeAllVirtualMethods(objectType).ToArray();

            Assert.Equal(4, objectMethods.Length);
            Assert.Superset(new HashSet <string> {
                "Equals", "GetHashCode", "ToString", "Finalize"
            },
                            new HashSet <string>(objectMethods.Select(m => m.Name)));

            var testTypeMethods = algo.ComputeAllVirtualMethods(_testType).ToArray();

            Assert.Equal(1, testTypeMethods.Length);
            Assert.Equal("ToString", testTypeMethods[0].Name);
            Assert.Equal(_testType, testTypeMethods[0].OwningType);
        }
        public VirtualMethodEnumerationAlgorithmTests()
        {
            _context = new TestTypeSystemContext(TargetArchitecture.Unknown);
            var systemModule = _context.CreateModuleForSimpleName("CoreTestAssembly");
            _context.SetSystemModule(systemModule);

            _testModule = systemModule;

            _testType = _testModule.GetType("VirtualFunctionOverride", "SimpleGeneric`1")
                .MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Object));
        }
Exemple #16
0
        public VirtualMethodEnumerationAlgorithmTests()
        {
            _context = new TestTypeSystemContext(TargetArchitecture.Unknown);
            var systemModule = _context.CreateModuleForSimpleName("CoreTestAssembly");

            _context.SetSystemModule(systemModule);

            _testModule = systemModule;

            _testType = _testModule.GetType("VirtualFunctionOverride", "SimpleGeneric`1")
                        .MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Object));
        }
Exemple #17
0
        public void TestByRefLikeTypes()
        {
            {
                DefType type = _context.GetWellKnownType(WellKnownType.TypedReference);
                Assert.True(type.IsByRefLike);
            }

            {
                DefType type = _context.GetWellKnownType(WellKnownType.ByReferenceOfT);
                Assert.True(type.IsByRefLike);
            }

            {
                DefType type = _testModule.GetType("IsByRefLike", "ByRefLikeStruct");
                Assert.True(type.IsByRefLike);
            }

            {
                DefType type = _testModule.GetType("IsByRefLike", "NotByRefLike");
                Assert.False(type.IsByRefLike);
            }
        }
        public void TestTypeConstraints()
        {
            MetadataType instantiatedType;

            MetadataType arg2OfInt    = _arg2Type.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Int32));
            MetadataType arg2OfBool   = _arg2Type.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Boolean));
            MetadataType arg2OfObject = _arg2Type.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Object));

            // ReferenceTypeConstraint
            {
                instantiatedType = _referenceTypeConstraintType.MakeInstantiatedType(_arg1Type);
                Assert.True(instantiatedType.CheckConstraints());

                instantiatedType = _referenceTypeConstraintType.MakeInstantiatedType(_iNonGenType);
                Assert.True(instantiatedType.CheckConstraints());

                instantiatedType = _referenceTypeConstraintType.MakeInstantiatedType(_structArgWithDefaultCtorType);
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = _referenceTypeConstraintType.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Int32));
                Assert.False(instantiatedType.CheckConstraints());
            }

            // DefaultConstructorConstraint
            {
                instantiatedType = _defaultConstructorConstraintType.MakeInstantiatedType(_arg1Type);
                Assert.True(instantiatedType.CheckConstraints());

                instantiatedType = _defaultConstructorConstraintType.MakeInstantiatedType(_classArgWithDefaultCtorType);
                Assert.True(instantiatedType.CheckConstraints());

                instantiatedType = _defaultConstructorConstraintType.MakeInstantiatedType(_classArgWithPrivateDefaultCtorType);
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = _defaultConstructorConstraintType.MakeInstantiatedType(_abstractClassArgWithDefaultCtorType);
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = _defaultConstructorConstraintType.MakeInstantiatedType(_classArgWithoutDefaultCtorType);
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = _defaultConstructorConstraintType.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Int32));
                Assert.True(instantiatedType.CheckConstraints());

                instantiatedType = _defaultConstructorConstraintType.MakeInstantiatedType(_structArgWithDefaultCtorType);
                Assert.True(instantiatedType.CheckConstraints());

                // Structs always have implicit default constructors
                instantiatedType = _defaultConstructorConstraintType.MakeInstantiatedType(_structArgWithoutDefaultCtorType);
                Assert.True(instantiatedType.CheckConstraints());
            }

            // NotNullableValueTypeConstraint
            {
                instantiatedType = _notNullableValueTypeConstraintType.MakeInstantiatedType(_arg1Type);
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = _notNullableValueTypeConstraintType.MakeInstantiatedType(_structArgWithDefaultCtorType);
                Assert.True(instantiatedType.CheckConstraints());

                MetadataType nullable      = (MetadataType)_context.GetWellKnownType(WellKnownType.Nullable);
                MetadataType nullableOfInt = nullable.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Int32));

                instantiatedType = _notNullableValueTypeConstraintType.MakeInstantiatedType(nullableOfInt);
                Assert.False(instantiatedType.CheckConstraints());
            }

            // SimpleTypeConstraint and DoubleSimpleTypeConstraint
            foreach (var genType in new MetadataType[] { _simpleTypeConstraintType, _doubleSimpleTypeConstraintType })
            {
                instantiatedType = genType.MakeInstantiatedType(_arg1Type);
                Assert.True(instantiatedType.CheckConstraints());

                instantiatedType = genType.MakeInstantiatedType(_iNonGenType);
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = genType.MakeInstantiatedType(_classArgWithDefaultCtorType);
                Assert.False(instantiatedType.CheckConstraints());
            }

            // SimpleGenericConstraint
            {
                instantiatedType = _simpleGenericConstraintType.MakeInstantiatedType(_arg1Type, _arg1Type);
                Assert.True(instantiatedType.CheckConstraints());

                instantiatedType = _simpleGenericConstraintType.MakeInstantiatedType(_arg1Type, _iNonGenType);
                Assert.True(instantiatedType.CheckConstraints());

                instantiatedType = _simpleGenericConstraintType.MakeInstantiatedType(_classArgWithDefaultCtorType, _classArgWithoutDefaultCtorType);
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = _simpleGenericConstraintType.MakeInstantiatedType(_arg1Type, _context.GetWellKnownType(WellKnownType.Object));
                Assert.True(instantiatedType.CheckConstraints());

                instantiatedType = _simpleGenericConstraintType.MakeInstantiatedType(_structArgWithDefaultCtorType, _context.GetWellKnownType(WellKnownType.ValueType));
                Assert.True(instantiatedType.CheckConstraints());

                instantiatedType = _simpleGenericConstraintType.MakeInstantiatedType(_arg1Type, _context.GetWellKnownType(WellKnownType.ValueType));
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = _simpleGenericConstraintType.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.UInt16), _context.GetWellKnownType(WellKnownType.UInt32));
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = _simpleGenericConstraintType.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.UInt16), _context.GetWellKnownType(WellKnownType.ValueType));
                Assert.True(instantiatedType.CheckConstraints());
            }

            // ComplexGenericConstraint1
            {
                instantiatedType = _complexGenericConstraint1Type.MakeInstantiatedType(_arg1Type, _arg1Type /* uninteresting */);
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = _complexGenericConstraint1Type.MakeInstantiatedType(arg2OfInt, _arg1Type /* uninteresting */);
                Assert.True(instantiatedType.CheckConstraints());

                instantiatedType = _complexGenericConstraint1Type.MakeInstantiatedType(arg2OfBool, _arg1Type /* uninteresting */);
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = _complexGenericConstraint1Type.MakeInstantiatedType(arg2OfObject, _arg1Type /* uninteresting */);
                Assert.False(instantiatedType.CheckConstraints());
            }

            // ComplexGenericConstraint2
            {
                MetadataType arg2OfArg2OfInt    = _arg2Type.MakeInstantiatedType(arg2OfInt);
                MetadataType arg2OfArg2OfBool   = _arg2Type.MakeInstantiatedType(arg2OfBool);
                MetadataType arg2OfArg2OfObject = _arg2Type.MakeInstantiatedType(arg2OfObject);

                instantiatedType = _complexGenericConstraint2Type.MakeInstantiatedType(_arg1Type, _context.GetWellKnownType(WellKnownType.Int32));
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = _complexGenericConstraint2Type.MakeInstantiatedType(arg2OfArg2OfInt, _context.GetWellKnownType(WellKnownType.Int32));
                Assert.True(instantiatedType.CheckConstraints());
                instantiatedType = _complexGenericConstraint2Type.MakeInstantiatedType(arg2OfArg2OfBool, _context.GetWellKnownType(WellKnownType.Int32));
                Assert.False(instantiatedType.CheckConstraints());
                instantiatedType = _complexGenericConstraint2Type.MakeInstantiatedType(arg2OfArg2OfObject, _context.GetWellKnownType(WellKnownType.Int32));
                Assert.False(instantiatedType.CheckConstraints());

                instantiatedType = _complexGenericConstraint2Type.MakeInstantiatedType(arg2OfArg2OfInt, _context.GetWellKnownType(WellKnownType.Object));
                Assert.False(instantiatedType.CheckConstraints());
                instantiatedType = _complexGenericConstraint2Type.MakeInstantiatedType(arg2OfArg2OfBool, _context.GetWellKnownType(WellKnownType.Object));
                Assert.False(instantiatedType.CheckConstraints());
                instantiatedType = _complexGenericConstraint2Type.MakeInstantiatedType(arg2OfArg2OfObject, _context.GetWellKnownType(WellKnownType.Object));
                Assert.True(instantiatedType.CheckConstraints());

                instantiatedType = _complexGenericConstraint2Type.MakeInstantiatedType(arg2OfArg2OfInt, _context.GetWellKnownType(WellKnownType.Boolean));
                Assert.False(instantiatedType.CheckConstraints());
                instantiatedType = _complexGenericConstraint2Type.MakeInstantiatedType(arg2OfArg2OfBool, _context.GetWellKnownType(WellKnownType.Boolean));
                Assert.True(instantiatedType.CheckConstraints());
                instantiatedType = _complexGenericConstraint2Type.MakeInstantiatedType(arg2OfArg2OfObject, _context.GetWellKnownType(WellKnownType.Boolean));
                Assert.False(instantiatedType.CheckConstraints());
            }

            // ComplexGenericConstraint3
            {
                MetadataType igenOfObject = _iGenType.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Object));

                instantiatedType = _complexGenericConstraint3Type.MakeInstantiatedType(igenOfObject, _context.GetWellKnownType(WellKnownType.Object));
                Assert.True(instantiatedType.CheckConstraints());

                // Variance-compatible instantiation argument
                instantiatedType = _complexGenericConstraint3Type.MakeInstantiatedType(igenOfObject, _context.GetWellKnownType(WellKnownType.String));
                Assert.True(instantiatedType.CheckConstraints());

                // Type that implements the interface
                var arg3OfObject = _arg3Type.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Object));
                instantiatedType = _complexGenericConstraint3Type.MakeInstantiatedType(arg3OfObject, _context.GetWellKnownType(WellKnownType.Object));
                Assert.True(instantiatedType.CheckConstraints());

                // Type that implements a variant compatible interface
                instantiatedType = _complexGenericConstraint3Type.MakeInstantiatedType(arg3OfObject, _context.GetWellKnownType(WellKnownType.String));
                Assert.True(instantiatedType.CheckConstraints());
            }

            // MultipleConstraints
            {
                // Violate the class constraint
                instantiatedType = _multipleConstraintsType.MakeInstantiatedType(_structArgWithDefaultCtorType, _context.GetWellKnownType(WellKnownType.Object));
                Assert.False(instantiatedType.CheckConstraints());

                // Violate the new() constraint
                instantiatedType = _multipleConstraintsType.MakeInstantiatedType(_classArgWithoutDefaultCtorType, _context.GetWellKnownType(WellKnownType.Object));
                Assert.False(instantiatedType.CheckConstraints());

                // Violate the IGen<U> constraint
                instantiatedType = _multipleConstraintsType.MakeInstantiatedType(_arg1Type, _context.GetWellKnownType(WellKnownType.Object));
                Assert.False(instantiatedType.CheckConstraints());

                // Satisfy all constraints
                instantiatedType = _multipleConstraintsType.MakeInstantiatedType(_classArgWithDefaultCtorType, _context.GetWellKnownType(WellKnownType.Object));
                Assert.True(instantiatedType.CheckConstraints());
            }

            // InvalidInstantiationArgs
            {
                var pointer = _context.GetWellKnownType(WellKnownType.Int16).MakePointerType();
                var byref   = _context.GetWellKnownType(WellKnownType.Int16).MakeByRefType();

                Assert.False(_iGenType.Instantiation.CheckValidInstantiationArguments());

                instantiatedType = _iGenType.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Void));
                Assert.False(instantiatedType.Instantiation.CheckValidInstantiationArguments());

                instantiatedType = _iGenType.MakeInstantiatedType(pointer);
                Assert.False(instantiatedType.Instantiation.CheckValidInstantiationArguments());

                instantiatedType = _iGenType.MakeInstantiatedType(byref);
                Assert.False(instantiatedType.Instantiation.CheckValidInstantiationArguments());

                instantiatedType = _iGenType.MakeInstantiatedType(byref);
                instantiatedType = _iGenType.MakeInstantiatedType(instantiatedType);
                Assert.False(instantiatedType.Instantiation.CheckValidInstantiationArguments());
            }
        }
 public void TestIsValueType()
 {
     Assert.True(_context.GetWellKnownType(WellKnownType.Boolean).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.Char).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.SByte).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.Byte).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.Int16).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.UInt16).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.Int32).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.UInt32).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.Int64).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.UInt64).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.IntPtr).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.UIntPtr).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.Single).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.Double).IsValueType);
     Assert.False(_context.GetWellKnownType(WellKnownType.ValueType).IsValueType);
     Assert.False(_context.GetWellKnownType(WellKnownType.Enum).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.Nullable).IsValueType);
     Assert.False(_context.GetWellKnownType(WellKnownType.Object).IsValueType);
     Assert.False(_context.GetWellKnownType(WellKnownType.String).IsValueType);
     Assert.False(_context.GetWellKnownType(WellKnownType.Array).IsValueType);
     Assert.False(_context.GetWellKnownType(WellKnownType.MulticastDelegate).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.RuntimeTypeHandle).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.RuntimeMethodHandle).IsValueType);
     Assert.True(_context.GetWellKnownType(WellKnownType.RuntimeFieldHandle).IsValueType);
     Assert.False(_context.GetWellKnownType(WellKnownType.Exception).IsValueType);
 }
Exemple #20
0
        public void TestArrayTypes(CanonicalizationMode algorithmType)
        {
            _context.CanonMode = algorithmType;

            // Generic type instantiated over an array has the same canonical form as generic type over any other reference type
            var genericStructOverArrayOfInt    = _genericStructType.MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Int32).MakeArrayType());
            var genericStructOverReferenceType = _genericStructType.MakeInstantiatedType(_referenceType);

            Assert.Same(
                genericStructOverArrayOfInt.ConvertToCanonForm(CanonicalFormKind.Specific),
                genericStructOverReferenceType.ConvertToCanonForm(CanonicalFormKind.Specific));
            Assert.Same(
                genericStructOverArrayOfInt.ConvertToCanonForm(CanonicalFormKind.Universal),
                genericStructOverReferenceType.ConvertToCanonForm(CanonicalFormKind.Universal));

            // Canonical form of SzArray and Multidim array are not the same
            var arrayOfReferenceType   = _referenceType.MakeArrayType();
            var mdArrayOfReferenceType = _referenceType.MakeArrayType(1);

            Assert.NotSame(
                arrayOfReferenceType.ConvertToCanonForm(CanonicalFormKind.Specific),
                mdArrayOfReferenceType.ConvertToCanonForm(CanonicalFormKind.Specific));
            Assert.NotSame(
                arrayOfReferenceType.ConvertToCanonForm(CanonicalFormKind.Universal),
                mdArrayOfReferenceType.ConvertToCanonForm(CanonicalFormKind.Universal));

            // Canonical forms of arrays over different reference types are same
            var arrayOfOtherReferenceType = _otherReferenceType.MakeArrayType();

            Assert.Same(
                arrayOfReferenceType.ConvertToCanonForm(CanonicalFormKind.Specific),
                arrayOfOtherReferenceType.ConvertToCanonForm(CanonicalFormKind.Specific));
            Assert.Same(
                arrayOfReferenceType.ConvertToCanonForm(CanonicalFormKind.Universal),
                arrayOfOtherReferenceType.ConvertToCanonForm(CanonicalFormKind.Universal));

            // Canonical forms of arrays of value types are only same for universal canon form
            var arrayOfStruct = _structType.MakeArrayType();

            Assert.NotSame(
                arrayOfReferenceType.ConvertToCanonForm(CanonicalFormKind.Specific),
                arrayOfStruct.ConvertToCanonForm(CanonicalFormKind.Specific));
            Assert.Same(
                arrayOfReferenceType.ConvertToCanonForm(CanonicalFormKind.Universal),
                arrayOfStruct.ConvertToCanonForm(CanonicalFormKind.Universal));
        }
Exemple #21
0
        public void TestCastingInHierarchy()
        {
            TypeDesc objectType = _context.GetWellKnownType(WellKnownType.Object);
            TypeDesc stringType = _context.GetWellKnownType(WellKnownType.String);
            TypeDesc intType    = _context.GetWellKnownType(WellKnownType.Int32);
            TypeDesc uintType   = _context.GetWellKnownType(WellKnownType.UInt32);

            Assert.True(stringType.CanCastTo(objectType));
            Assert.True(objectType.CanCastTo(objectType));
            Assert.True(intType.CanCastTo(objectType));

            Assert.False(objectType.CanCastTo(stringType));
            Assert.False(intType.CanCastTo(uintType));
            Assert.False(uintType.CanCastTo(intType));
        }
 public void IsBlittableType_BilittableWellKnownTypes_ReturnsTrue(WellKnownType type) =>
 Assert.True(MarshalUtils.IsBlittableType(_context.GetWellKnownType(type)));
Exemple #23
0
        public static IEnumerable <object[]> VariantTestData()
        {
            var        context    = new TestTypeSystemContext(TargetArchitecture.Unknown);
            ModuleDesc testModule = context.CreateModuleForSimpleName("CoreTestAssembly");

            context.SetSystemModule(testModule);

            MetadataType simple                               = testModule.GetType("VirtualStaticInterfaceMethods", "Simple");
            MetadataType iSimple                              = testModule.GetType("VirtualStaticInterfaceMethods", "ISimple");
            MetadataType iVariant                             = testModule.GetType("VirtualStaticInterfaceMethods", "IVariant`1");
            MetadataType @base                                = testModule.GetType("VirtualStaticInterfaceMethods", "Base");
            MetadataType mid                                  = testModule.GetType("VirtualStaticInterfaceMethods", "Mid");
            MetadataType derived                              = testModule.GetType("VirtualStaticInterfaceMethods", "Derived");
            MetadataType simpleVariant                        = testModule.GetType("VirtualStaticInterfaceMethods", "SimpleVariant");
            MetadataType simpleVariantTwice                   = testModule.GetType("VirtualStaticInterfaceMethods", "SimpleVariantTwice");
            MetadataType variantWithInheritanceDerived        = testModule.GetType("VirtualStaticInterfaceMethods", "VariantWithInheritanceDerived");
            MetadataType genericVariantWithInheritanceDerived = testModule.GetType("VirtualStaticInterfaceMethods", "GenericVariantWithInheritanceDerived`1");
            MetadataType genericVariantWithHiddenBase         = testModule.GetType("VirtualStaticInterfaceMethods", "GenericVariantWithHiddenBase");
            MetadataType genericVariantWithHiddenDerived      = testModule.GetType("VirtualStaticInterfaceMethods", "GenericVariantWithHiddenDerived`1");

            MethodDesc iSimpleMethod         = iSimple.GetMethod("WhichMethod", null);
            MethodDesc iVariantBaseMethod    = iVariant.MakeInstantiatedType(@base).GetMethod("WhichMethod", null);
            MethodDesc iVariantMidMethod     = iVariant.MakeInstantiatedType(mid).GetMethod("WhichMethod", null);
            MethodDesc iVariantDerivedMethod = iVariant.MakeInstantiatedType(derived).GetMethod("WhichMethod", null);

            yield return(new object[] { simple, iSimpleMethod, simple.GetMethod("WhichMethod", null) });

            yield return(new object[] { simpleVariant, iVariantBaseMethod, simpleVariant.GetMethod("WhichMethod", null) });

            yield return(new object[] { simpleVariant, iVariantDerivedMethod, simpleVariant.GetMethod("WhichMethod", null) });

            yield return(new object[] { simpleVariantTwice, iVariantBaseMethod, simpleVariantTwice.GetMethod("WhichMethod", new MethodSignature(MethodSignatureFlags.Static, 0, context.GetWellKnownType(WellKnownType.String), new TypeDesc[] { @base })) });

            yield return(new object[] { simpleVariantTwice, iVariantMidMethod, simpleVariantTwice.GetMethod("WhichMethod", new MethodSignature(MethodSignatureFlags.Static, 0, context.GetWellKnownType(WellKnownType.String), new TypeDesc[] { mid })) });

            yield return(new object[] { simpleVariantTwice, iVariantDerivedMethod, simpleVariantTwice.GetMethod("WhichMethod", new MethodSignature(MethodSignatureFlags.Static, 0, context.GetWellKnownType(WellKnownType.String), new TypeDesc[] { @base })) });

            yield return(new object[] { variantWithInheritanceDerived, iVariantBaseMethod, variantWithInheritanceDerived.GetMethod("WhichMethod", null) });

            yield return(new object[] { variantWithInheritanceDerived, iVariantMidMethod, variantWithInheritanceDerived.GetMethod("WhichMethod", null) });

            yield return(new object[] { variantWithInheritanceDerived, iVariantDerivedMethod, variantWithInheritanceDerived.GetMethod("WhichMethod", null) });

            yield return(new object[] { genericVariantWithInheritanceDerived.MakeInstantiatedType(@base), iVariantBaseMethod, genericVariantWithInheritanceDerived.MakeInstantiatedType(@base).GetMethod("WhichMethod", null) });

            yield return(new object[] { genericVariantWithInheritanceDerived.MakeInstantiatedType(@base), iVariantMidMethod, genericVariantWithInheritanceDerived.MakeInstantiatedType(@base).GetMethod("WhichMethod", null) });

            yield return(new object[] { genericVariantWithInheritanceDerived.MakeInstantiatedType(mid), iVariantMidMethod, genericVariantWithInheritanceDerived.MakeInstantiatedType(mid).GetMethod("WhichMethod", null) });

            yield return(new object[] { genericVariantWithHiddenDerived.MakeInstantiatedType(@base), iVariantBaseMethod, genericVariantWithHiddenDerived.MakeInstantiatedType(@base).GetMethod("WhichMethod", null) });

            yield return(new object[] { genericVariantWithHiddenDerived.MakeInstantiatedType(@base), iVariantMidMethod, genericVariantWithHiddenDerived.MakeInstantiatedType(@base).GetMethod("WhichMethod", null) });

            yield return(new object[] { genericVariantWithHiddenDerived.MakeInstantiatedType(mid), iVariantMidMethod, genericVariantWithHiddenDerived.MakeInstantiatedType(mid).GetMethod("WhichMethod", null) });

            yield return(new object[] { genericVariantWithHiddenDerived.MakeInstantiatedType(derived), iVariantMidMethod, genericVariantWithHiddenBase.GetMethod("WhichMethod", null) });
        }