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

UniversalGenericFieldLayoutTests.cs « tests « ILCompiler.TypeSystem « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8258cadf9474891df51193be6e1eefe29fd1a8ca (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// 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 System;
using System.Linq;
using Internal.TypeSystem;

using Xunit;

namespace TypeSystemTests
{
    public class UniversalGenericFieldLayoutTests
    {
        TestTypeSystemContext _contextX86;
        ModuleDesc _testModuleX86;
        TestTypeSystemContext _contextX64;
        ModuleDesc _testModuleX64;
        TestTypeSystemContext _contextARM;
        ModuleDesc _testModuleARM;

        public UniversalGenericFieldLayoutTests()
        {
            // Architecture specific tests may use these contexts
            _contextX64 = new TestTypeSystemContext(TargetArchitecture.X64);
            var systemModuleX64 = _contextX64.CreateModuleForSimpleName("CoreTestAssembly");
            _contextX64.SetSystemModule(systemModuleX64);

            _testModuleX64 = systemModuleX64;

            _contextARM = new TestTypeSystemContext(TargetArchitecture.ARM);
            var systemModuleARM = _contextARM.CreateModuleForSimpleName("CoreTestAssembly");
            _contextARM.SetSystemModule(systemModuleARM);

            _testModuleARM = systemModuleARM;

            _contextX86 = new TestTypeSystemContext(TargetArchitecture.X86);
            var systemModuleX86 = _contextX86.CreateModuleForSimpleName("CoreTestAssembly");
            _contextX86.SetSystemModule(systemModuleX86);

            _testModuleX86 = systemModuleX86;
        }

        [Fact]
        public void LayoutIntTests()
        {
            Assert.Throws<ArgumentException>(() => { return new LayoutInt(int.MinValue); });
            Assert.Throws<ArgumentException>(() => { return new LayoutInt(-1); });

            Assert.Equal(LayoutInt.Zero, new LayoutInt(0));
            Assert.Equal(LayoutInt.One, new LayoutInt(1));

            Assert.True(LayoutInt.Zero == new LayoutInt(0));
            Assert.True(LayoutInt.One == new LayoutInt(1));
            Assert.False(LayoutInt.Zero == new LayoutInt(1));
            Assert.False(LayoutInt.One == new LayoutInt(0));
#pragma warning disable 1718 // Allow comparison to same variable
            Assert.True(LayoutInt.Indeterminate == LayoutInt.Indeterminate);
#pragma warning restore 1718

            Assert.False(LayoutInt.Zero != new LayoutInt(0));
            Assert.False(LayoutInt.One != new LayoutInt(1));
            Assert.True(LayoutInt.Zero != new LayoutInt(1));
            Assert.True(LayoutInt.One != new LayoutInt(0));
#pragma warning disable 1718 // Allow comparison to same variable
            Assert.False(LayoutInt.Indeterminate != LayoutInt.Indeterminate);
#pragma warning restore 1718

            Assert.Equal(0, new LayoutInt(0).AsInt);
            Assert.Equal(1, new LayoutInt(1).AsInt);
            Assert.Equal(Int32.MaxValue, new LayoutInt(Int32.MaxValue).AsInt);
            Assert.Throws<InvalidOperationException>(() => { return LayoutInt.Indeterminate.AsInt; });

            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.Indeterminate + LayoutInt.Indeterminate);
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.One + LayoutInt.Indeterminate);
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.Indeterminate + LayoutInt.One);
            Assert.Equal(new LayoutInt(2), LayoutInt.One + LayoutInt.One);
            Assert.Throws<OverflowException>(() => { return new LayoutInt(int.MaxValue) + LayoutInt.One; });
            Assert.Throws<OverflowException>(() => { return new LayoutInt(int.MaxValue) + LayoutInt.One; });

            Assert.Equal(LayoutInt.One, LayoutInt.Max(LayoutInt.One, LayoutInt.Zero));
            Assert.Equal(LayoutInt.One, LayoutInt.Max(LayoutInt.Zero, LayoutInt.One));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.Max(LayoutInt.Indeterminate, LayoutInt.Zero));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.Max(LayoutInt.Zero, LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.Max(LayoutInt.Indeterminate, LayoutInt.Indeterminate));

            Assert.Equal(LayoutInt.Zero, LayoutInt.Min(LayoutInt.One, LayoutInt.Zero));
            Assert.Equal(LayoutInt.Zero, LayoutInt.Min(LayoutInt.Zero, LayoutInt.One));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.Min(LayoutInt.Indeterminate, LayoutInt.Zero));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.Min(LayoutInt.Zero, LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.Min(LayoutInt.Indeterminate, LayoutInt.Indeterminate));

            // AlignUp testing
            Assert.Equal(new LayoutInt(0), LayoutInt.AlignUp(new LayoutInt(0), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(0), LayoutInt.AlignUp(new LayoutInt(0), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(0), LayoutInt.AlignUp(new LayoutInt(0), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(0), LayoutInt.AlignUp(new LayoutInt(0), new LayoutInt(8)));

            Assert.Equal(new LayoutInt(1), LayoutInt.AlignUp(new LayoutInt(1), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(2), LayoutInt.AlignUp(new LayoutInt(2), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(3), LayoutInt.AlignUp(new LayoutInt(3), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(4), LayoutInt.AlignUp(new LayoutInt(4), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(5), LayoutInt.AlignUp(new LayoutInt(5), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(6), LayoutInt.AlignUp(new LayoutInt(6), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(7), LayoutInt.AlignUp(new LayoutInt(7), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(8), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(9), LayoutInt.AlignUp(new LayoutInt(9), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(10), LayoutInt.AlignUp(new LayoutInt(10), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(11), LayoutInt.AlignUp(new LayoutInt(11), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(12), LayoutInt.AlignUp(new LayoutInt(12), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(13), LayoutInt.AlignUp(new LayoutInt(13), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(14), LayoutInt.AlignUp(new LayoutInt(14), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(15), LayoutInt.AlignUp(new LayoutInt(15), new LayoutInt(1)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(16), new LayoutInt(1)));

            Assert.Equal(new LayoutInt(2), LayoutInt.AlignUp(new LayoutInt(1), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(2), LayoutInt.AlignUp(new LayoutInt(2), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(4), LayoutInt.AlignUp(new LayoutInt(3), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(4), LayoutInt.AlignUp(new LayoutInt(4), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(6), LayoutInt.AlignUp(new LayoutInt(5), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(6), LayoutInt.AlignUp(new LayoutInt(6), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(7), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(8), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(10), LayoutInt.AlignUp(new LayoutInt(9), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(10), LayoutInt.AlignUp(new LayoutInt(10), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(12), LayoutInt.AlignUp(new LayoutInt(11), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(12), LayoutInt.AlignUp(new LayoutInt(12), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(14), LayoutInt.AlignUp(new LayoutInt(13), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(14), LayoutInt.AlignUp(new LayoutInt(14), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(15), new LayoutInt(2)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(16), new LayoutInt(2)));

            Assert.Equal(new LayoutInt(4), LayoutInt.AlignUp(new LayoutInt(1), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(4), LayoutInt.AlignUp(new LayoutInt(2), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(4), LayoutInt.AlignUp(new LayoutInt(3), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(4), LayoutInt.AlignUp(new LayoutInt(4), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(5), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(6), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(7), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(8), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(12), LayoutInt.AlignUp(new LayoutInt(9), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(12), LayoutInt.AlignUp(new LayoutInt(10), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(12), LayoutInt.AlignUp(new LayoutInt(11), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(12), LayoutInt.AlignUp(new LayoutInt(12), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(13), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(14), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(15), new LayoutInt(4)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(16), new LayoutInt(4)));

            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(1), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(2), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(3), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(4), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(5), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(6), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(7), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(8), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(9), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(10), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(11), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(12), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(13), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(14), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(15), new LayoutInt(8)));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(16), new LayoutInt(8)));

            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(1), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(2), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(3), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(4), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(5), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(6), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(7), LayoutInt.Indeterminate));
            Assert.Equal(new LayoutInt(8), LayoutInt.AlignUp(new LayoutInt(8), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(9), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(10), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(11), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(12), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(13), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(14), LayoutInt.Indeterminate));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(new LayoutInt(15), LayoutInt.Indeterminate));
            Assert.Equal(new LayoutInt(16), LayoutInt.AlignUp(new LayoutInt(16), LayoutInt.Indeterminate));

            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(LayoutInt.Indeterminate, new LayoutInt(1)));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(LayoutInt.Indeterminate, new LayoutInt(2)));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(LayoutInt.Indeterminate, new LayoutInt(4)));
            Assert.Equal(LayoutInt.Indeterminate, LayoutInt.AlignUp(LayoutInt.Indeterminate, new LayoutInt(8)));
        }


        private void TestLayoutOfUniversalCanonTypeOnArchitecture(TypeSystemContext context)
        {
            // Assert all of the various layout information about the universal canon type itself
            Assert.Equal(LayoutInt.Indeterminate, context.UniversalCanonType.InstanceFieldAlignment);
            Assert.Equal(LayoutInt.Indeterminate, context.UniversalCanonType.InstanceFieldSize);
            Assert.Equal(LayoutInt.Indeterminate, context.UniversalCanonType.InstanceByteAlignment);
            Assert.Equal(LayoutInt.Indeterminate, context.UniversalCanonType.InstanceByteCount);
            Assert.Equal(LayoutInt.Indeterminate, context.UniversalCanonType.InstanceByteCountUnaligned);
            Assert.Equal(LayoutInt.Zero, context.UniversalCanonType.GCStaticFieldAlignment);
            Assert.Equal(LayoutInt.Zero, context.UniversalCanonType.GCStaticFieldSize);
            Assert.Equal(LayoutInt.Zero, context.UniversalCanonType.NonGCStaticFieldAlignment);
            Assert.Equal(LayoutInt.Zero, context.UniversalCanonType.NonGCStaticFieldSize);
            Assert.Equal(LayoutInt.Zero, context.UniversalCanonType.ThreadGcStaticFieldAlignment);
            Assert.Equal(LayoutInt.Zero, context.UniversalCanonType.ThreadGcStaticFieldSize);
        }
        [Fact]
        public void TestLayoutOfUniversalCanonType()
        {
            // Assert all of the various layout information about the universal canon type itself, do this for all architectures
            TestLayoutOfUniversalCanonTypeOnArchitecture(_contextX86);
            TestLayoutOfUniversalCanonTypeOnArchitecture(_contextX64);
            TestLayoutOfUniversalCanonTypeOnArchitecture(_contextARM);
        }

        [Fact]
        public void TestAllFieldsStructUniversalGeneric()
        {
            // Given a struct with all field universal, what is the layout?
            MetadataType tGen;
            InstantiatedType genOfUUU;
            ModuleDesc testModule;
            TypeSystemContext context;

            // X64 testing
            testModule = _testModuleX64;
            context = _contextX64;

            tGen = testModule.GetType("GenericTypes", "GenStruct`3");
            genOfUUU = tGen.MakeInstantiatedType(context.UniversalCanonType, context.UniversalCanonType, context.UniversalCanonType);

            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceFieldAlignment);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceFieldSize);
            Assert.Equal(new LayoutInt(8), genOfUUU.InstanceByteAlignment);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceByteCount);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceByteCountUnaligned);
            Assert.Equal(0, genOfUUU.GetFields().First().Offset.AsInt);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.GetFields().ElementAt(1).Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.GetFields().ElementAt(2).Offset);

            testModule = _testModuleX86;
            context = _contextX86;

            tGen = testModule.GetType("GenericTypes", "GenStruct`3");
            genOfUUU = tGen.MakeInstantiatedType(context.UniversalCanonType, context.UniversalCanonType, context.UniversalCanonType);

            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceFieldAlignment);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceFieldSize);
            Assert.Equal(new LayoutInt(4), genOfUUU.InstanceByteAlignment);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceByteCount);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceByteCountUnaligned);
            Assert.Equal(0, genOfUUU.GetFields().First().Offset.AsInt);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.GetFields().ElementAt(1).Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.GetFields().ElementAt(2).Offset);

            testModule = _testModuleARM;
            context = _contextARM;

            tGen = testModule.GetType("GenericTypes", "GenStruct`3");
            genOfUUU = tGen.MakeInstantiatedType(context.UniversalCanonType, context.UniversalCanonType, context.UniversalCanonType);

            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceFieldAlignment);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceFieldSize);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceByteAlignment);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceByteCount);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.InstanceByteCountUnaligned);
            Assert.Equal(0, genOfUUU.GetFields().First().Offset.AsInt);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.GetFields().ElementAt(1).Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUUU.GetFields().ElementAt(2).Offset);
        }

        private void TestIndeterminatedNestedStructFieldPerContext(TypeSystemContext context, ModuleDesc testModule, out InstantiatedType genOfIntNestedInt, out InstantiatedType genOfLongNestedInt)
        {
            // Given a struct with all field universal, what is the layout?
            MetadataType tGen = testModule.GetType("GenericTypes", "GenStruct`3");
            InstantiatedType genOfUUU = tGen.MakeInstantiatedType(context.UniversalCanonType, context.UniversalCanonType, context.UniversalCanonType);
            genOfIntNestedInt = tGen.MakeInstantiatedType(context.GetWellKnownType(WellKnownType.Int32), genOfUUU, context.GetWellKnownType(WellKnownType.Int32));
            genOfLongNestedInt = tGen.MakeInstantiatedType(context.GetWellKnownType(WellKnownType.Int64), genOfUUU, context.GetWellKnownType(WellKnownType.Int32));

            Assert.Equal(LayoutInt.Indeterminate, genOfIntNestedInt.InstanceFieldAlignment);
            Assert.Equal(LayoutInt.Indeterminate, genOfIntNestedInt.InstanceFieldSize);
            Assert.Equal(LayoutInt.Indeterminate, genOfIntNestedInt.InstanceByteCount);
            Assert.Equal(LayoutInt.Indeterminate, genOfIntNestedInt.InstanceByteCountUnaligned);
            Assert.Equal(0, genOfIntNestedInt.GetFields().First().Offset.AsInt);
            Assert.Equal(LayoutInt.Indeterminate, genOfIntNestedInt.GetFields().ElementAt(1).Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfIntNestedInt.GetFields().ElementAt(2).Offset);

            Assert.Equal(LayoutInt.Indeterminate, genOfLongNestedInt.InstanceFieldAlignment);
            Assert.Equal(LayoutInt.Indeterminate, genOfLongNestedInt.InstanceFieldSize);
            Assert.Equal(LayoutInt.Indeterminate, genOfLongNestedInt.InstanceByteCount);
            Assert.Equal(LayoutInt.Indeterminate, genOfLongNestedInt.InstanceByteCountUnaligned);
            Assert.Equal(0, genOfLongNestedInt.GetFields().First().Offset.AsInt);
            Assert.Equal(8, genOfLongNestedInt.GetFields().ElementAt(1).Offset.AsInt);
            Assert.Equal(LayoutInt.Indeterminate, genOfLongNestedInt.GetFields().ElementAt(2).Offset);
        }

        [Fact]
        public void TestIndeterminateNestedStructField()
        {
            InstantiatedType genOfIntNestedInt;
            InstantiatedType genOfLongNestedInt;

            TestIndeterminatedNestedStructFieldPerContext(_contextX64, _testModuleX64, out genOfIntNestedInt, out genOfLongNestedInt);
            Assert.Equal(new LayoutInt(8), genOfLongNestedInt.InstanceByteAlignment);
            Assert.Equal(new LayoutInt(8), genOfLongNestedInt.InstanceByteAlignment);
            TestIndeterminatedNestedStructFieldPerContext(_contextX86, _testModuleX86, out genOfIntNestedInt, out genOfLongNestedInt);
            Assert.Equal(new LayoutInt(4), genOfLongNestedInt.InstanceByteAlignment);
            Assert.Equal(new LayoutInt(4), genOfLongNestedInt.InstanceByteAlignment);
            TestIndeterminatedNestedStructFieldPerContext(_contextARM, _testModuleARM, out genOfIntNestedInt, out genOfLongNestedInt);
            Assert.Equal(LayoutInt.Indeterminate, genOfLongNestedInt.InstanceByteAlignment);
            Assert.Equal(LayoutInt.Indeterminate, genOfLongNestedInt.InstanceByteAlignment);
        }

        private void AssertClassIndeterminateSize(TypeSystemContext context, MetadataType type, LayoutInt expectedIndeterminateByteAlignment)
        {
            Assert.Equal(context.Target.LayoutPointerSize, type.InstanceFieldAlignment);
            Assert.Equal(context.Target.LayoutPointerSize, type.InstanceFieldSize);
            Assert.Equal(expectedIndeterminateByteAlignment, type.InstanceByteAlignment);
            Assert.Equal(LayoutInt.Indeterminate, type.InstanceByteCount);
            Assert.Equal(LayoutInt.Indeterminate, type.InstanceByteCountUnaligned);
        }

        private void CommonClassLayoutTestBits(ModuleDesc testModule, 
                                               TypeSystemContext context,
                                               LayoutInt expectedIndeterminateByteAlignment,
                                               out MetadataType tDerivedGen, 
                                               out InstantiatedType genOfIU,
                                               out InstantiatedType genOfLU,
                                               out InstantiatedType genOfUU,
                                               out InstantiatedType genOfUI,
                                               out InstantiatedType genOfUL)
        {
            tDerivedGen = testModule.GetType("GenericTypes", "GenDerivedClass`2");
            genOfIU = tDerivedGen.MakeInstantiatedType(context.GetWellKnownType(WellKnownType.Int32), context.UniversalCanonType);
            genOfLU = tDerivedGen.MakeInstantiatedType(context.GetWellKnownType(WellKnownType.Int64), context.UniversalCanonType);
            genOfUU = tDerivedGen.MakeInstantiatedType(context.UniversalCanonType, context.UniversalCanonType);

            genOfUI = tDerivedGen.MakeInstantiatedType(context.UniversalCanonType, context.GetWellKnownType(WellKnownType.Int32));
            genOfUL = tDerivedGen.MakeInstantiatedType(context.UniversalCanonType, context.GetWellKnownType(WellKnownType.Int64));

            // Assert that the class as a whole is known to be of undefined size
            AssertClassIndeterminateSize(context, genOfIU, expectedIndeterminateByteAlignment);
            AssertClassIndeterminateSize(context, genOfLU, expectedIndeterminateByteAlignment);
            AssertClassIndeterminateSize(context, genOfUU, expectedIndeterminateByteAlignment);
            AssertClassIndeterminateSize(context, genOfUI, expectedIndeterminateByteAlignment);
            AssertClassIndeterminateSize(context, genOfUL, expectedIndeterminateByteAlignment);
        }

        [Fact]
        public void TestClassLayout()
        {
            // Tests class layout behavior with universal generics
            // Tests handling universal base types as well as non-universal base types

            MetadataType tDerivedGen;
            InstantiatedType genOfIU;
            InstantiatedType genOfLU;
            InstantiatedType genOfUU;
            InstantiatedType genOfUI;
            InstantiatedType genOfUL;

            ModuleDesc testModule;
            TypeSystemContext context;

            // X64 testing
            testModule = _testModuleX64;
            context = _contextX64;

            CommonClassLayoutTestBits(testModule,
                                      context,
                                      new LayoutInt(8),
                                      out tDerivedGen,
                                      out genOfIU,
                                      out genOfLU,
                                      out genOfUU,
                                      out genOfUI,
                                      out genOfUL);

            // On x64 first field offset is well known always
            Assert.Equal(8, genOfIU.BaseType.GetFields().First().Offset.AsInt);
            Assert.Equal(8, genOfLU.BaseType.GetFields().First().Offset.AsInt);
            Assert.Equal(8, genOfUU.BaseType.GetFields().First().Offset.AsInt);
            Assert.Equal(8, genOfUI.BaseType.GetFields().First().Offset.AsInt);
            Assert.Equal(8, genOfUL.BaseType.GetFields().First().Offset.AsInt);

            Assert.Equal(LayoutInt.Indeterminate, genOfIU.GetFields().First().Offset);
            Assert.Equal(16, genOfLU.GetFields().First().Offset.AsInt);
            Assert.Equal(LayoutInt.Indeterminate, genOfUU.GetFields().First().Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUI.GetFields().First().Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUL.GetFields().First().Offset);

            // X86 testing
            testModule = _testModuleX86;
            context = _contextX86;

            CommonClassLayoutTestBits(testModule,
                                      context,
                                      new LayoutInt(4),
                                      out tDerivedGen,
                                      out genOfIU,
                                      out genOfLU,
                                      out genOfUU,
                                      out genOfUI,
                                      out genOfUL);

            Assert.Equal(4, genOfIU.BaseType.GetFields().First().Offset.AsInt);
            Assert.Equal(8, genOfLU.BaseType.GetFields().First().Offset.AsInt);
            Assert.Equal(LayoutInt.Indeterminate, genOfUU.BaseType.GetFields().First().Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUI.BaseType.GetFields().First().Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUL.BaseType.GetFields().First().Offset);

            Assert.Equal(8, genOfIU.GetFields().First().Offset.AsInt);
            Assert.Equal(16, genOfLU.GetFields().First().Offset.AsInt);
            Assert.Equal(LayoutInt.Indeterminate, genOfUU.GetFields().First().Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUI.GetFields().First().Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUL.GetFields().First().Offset);

            // ARM testing
            testModule = _testModuleARM;
            context = _contextARM;

            CommonClassLayoutTestBits(testModule,
                                      context,
                                      LayoutInt.Indeterminate,
                                      out tDerivedGen,
                                      out genOfIU,
                                      out genOfLU,
                                      out genOfUU,
                                      out genOfUI,
                                      out genOfUL);

            Assert.Equal(4, genOfIU.BaseType.GetFields().First().Offset.AsInt);
            Assert.Equal(8, genOfLU.BaseType.GetFields().First().Offset.AsInt);
            Assert.Equal(LayoutInt.Indeterminate, genOfUU.BaseType.GetFields().First().Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUI.BaseType.GetFields().First().Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUL.BaseType.GetFields().First().Offset);

            Assert.Equal(8, genOfIU.GetFields().First().Offset.AsInt);
            Assert.Equal(16, genOfLU.GetFields().First().Offset.AsInt);
            Assert.Equal(LayoutInt.Indeterminate, genOfUU.GetFields().First().Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUI.GetFields().First().Offset);
            Assert.Equal(LayoutInt.Indeterminate, genOfUL.GetFields().First().Offset);
        }
    }
}