Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ConstraintsValidationTest.cs « tests « ILCompiler.TypeSystem « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3b4d123955ca51f70bf7b659d917ca129fd7fe22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Licensed to the.NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Internal.TypeSystem;

using Xunit;

namespace TypeSystemTests
{
    public class ConstraintsValidationTest
    {
        private TestTypeSystemContext _context;
        private ModuleDesc _testModule;

        private MetadataType _iNonGenType;
        private MetadataType _iGenType;
        private MetadataType _arg1Type;
        private MetadataType _arg2Type;
        private MetadataType _arg3Type;
        private MetadataType _structArgWithDefaultCtorType;
        private MetadataType _structArgWithoutDefaultCtorType;
        private MetadataType _classArgWithDefaultCtorType;
        private MetadataType _classArgWithoutDefaultCtorType;
        private MetadataType _referenceTypeConstraintType;
        private MetadataType _defaultConstructorConstraintType;
        private MetadataType _notNullableValueTypeConstraintType;
        private MetadataType _simpleTypeConstraintType;
        private MetadataType _doubleSimpleTypeConstraintType;
        private MetadataType _simpleGenericConstraintType;
        private MetadataType _complexGenericConstraint1Type;
        private MetadataType _complexGenericConstraint2Type;
        private MetadataType _complexGenericConstraint3Type;
        private MetadataType _multipleConstraintsType;

        public ConstraintsValidationTest()
        {
            _context = new TestTypeSystemContext(TargetArchitecture.Unknown);
            var systemModule = _context.CreateModuleForSimpleName("CoreTestAssembly");
            _context.SetSystemModule(systemModule);
            
            _testModule = systemModule;

            _iNonGenType = _testModule.GetType("GenericConstraints", "INonGen");
            _iGenType = _testModule.GetType("GenericConstraints", "IGen`1");
            _arg1Type = _testModule.GetType("GenericConstraints", "Arg1");
            _arg2Type = _testModule.GetType("GenericConstraints", "Arg2`1");
            _arg3Type = _testModule.GetType("GenericConstraints", "Arg3`1");
            _structArgWithDefaultCtorType = _testModule.GetType("GenericConstraints", "StructArgWithDefaultCtor");
            _structArgWithoutDefaultCtorType = _testModule.GetType("GenericConstraints", "StructArgWithoutDefaultCtor");
            _classArgWithDefaultCtorType = _testModule.GetType("GenericConstraints", "ClassArgWithDefaultCtor");
            _classArgWithoutDefaultCtorType = _testModule.GetType("GenericConstraints", "ClassArgWithoutDefaultCtor");

            _referenceTypeConstraintType = _testModule.GetType("GenericConstraints", "ReferenceTypeConstraint`1");
            _defaultConstructorConstraintType = _testModule.GetType("GenericConstraints", "DefaultConstructorConstraint`1");
            _notNullableValueTypeConstraintType = _testModule.GetType("GenericConstraints", "NotNullableValueTypeConstraint`1");
            _simpleTypeConstraintType = _testModule.GetType("GenericConstraints", "SimpleTypeConstraint`1");
            _doubleSimpleTypeConstraintType = _testModule.GetType("GenericConstraints", "DoubleSimpleTypeConstraint`1");
            _simpleGenericConstraintType = _testModule.GetType("GenericConstraints", "SimpleGenericConstraint`2");
            _complexGenericConstraint1Type = _testModule.GetType("GenericConstraints", "ComplexGenericConstraint1`2");
            _complexGenericConstraint2Type = _testModule.GetType("GenericConstraints", "ComplexGenericConstraint2`2");
            _complexGenericConstraint3Type = _testModule.GetType("GenericConstraints", "ComplexGenericConstraint3`2");
            _multipleConstraintsType = _testModule.GetType("GenericConstraints", "MultipleConstraints`2");
        }

        [Fact]
        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(_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());
            }
        }
    }
}