-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
dictionaries.cs
1735 lines (1498 loc) · 69.2 KB
/
dictionaries.cs
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.IO;
using System.Runtime.CompilerServices;
using CoreFXTestLibrary;
using TypeOfRepo;
namespace Dictionaries
{
public abstract class Base
{
public abstract Object Test(int i, Object o);
}
public struct GenericStruct<T>
{
}
public class NullableTest<T> : Base where T : struct
{
public override Object Test(int i, Object o)
{
switch (i)
{
case 0:
T? nullable_t = (T?)o;
return nullable_t.HasValue ? nullable_t.Value : default(T);
case 1:
return o is T?;
case 2:
T?[] a = new T?[3];
a.SetValue(o, 1);
if (o == null)
{
Assert.IsTrue(!a[1].HasValue);
return default(T);
}
else
return (T)a[1];
}
return null;
}
}
public delegate Object DelWithNullable<T>(Nullable<T> o) where T : struct;
public class DelegateTarget<T> where T : struct
{
public static Object DelWithNullableTarget(T? n)
{
Assert.IsTrue(n is T?);
if (n.HasValue)
return (T)n;
return default(T);
}
}
public abstract class GenBase<T> : Base
{
public override Object Test(int i, Object o) { return null; }
}
public interface IFace<T>
{
int InterfaceMethod();
}
public interface IFace2<T>
{
string Interface2Method();
}
public interface IFace3<T>
{
string Interface3Method();
}
public interface IDerivedIFace<T> : IFace2<T>
{
string IDerivedIFaceMethod();
}
public class Gen2<T>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static string M()
{
return typeof(T).ToString();
}
}
public class Gen<T> : GenBase<T>, IFace<T>, IDerivedIFace<long>, IFace2<int>, IFace3<T>, IFace2<string>, IDerivedIFace<string>
{
public Gen()
{
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static string TestTypeDict()
{
return Gen2<T>.M();
}
[MethodImplAttribute(MethodImplOptions.NoInlining)]
public override Object Test(int i, Object o)
{
switch (i)
{
case 0:
return typeof(T);
case 1:
return typeof(List<T>);
case 2:
return ((IList<T>)o).Count;
case 3:
return typeof(T[]);
case 4:
return TestTypeDict();
case 5:
return ((IList<IList<T>>)o).Count;
default:
break;
}
return null;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public int InterfaceMethod() { return 42; }
[MethodImpl(MethodImplOptions.NoInlining)]
string IFace2<int>.Interface2Method() { return "IFace2<int>.Interface2Method on Gen<" + typeof(T) + ">"; }
[MethodImpl(MethodImplOptions.NoInlining)]
public virtual string Interface2Method() { return "Interface2Method on Gen<" + typeof(T) + ">"; }
[MethodImpl(MethodImplOptions.NoInlining)]
public virtual string Interface3Method() { return "Interface3Method on Gen<" + typeof(T) + ">"; }
[MethodImpl(MethodImplOptions.NoInlining)]
string IDerivedIFace<long>.IDerivedIFaceMethod() { return "IDerivedIFace<long>.IDerivedIFaceMethod on Gen<" + typeof(T) + ">"; }
[MethodImpl(MethodImplOptions.NoInlining)]
public virtual string IDerivedIFaceMethod() { return "IDerivedIFaceMethod on Gen<" + typeof(T) + ">"; }
}
// Do not extend this or use it for anything. You might accidentally
// make it stop testing what this is supposed to test (DEK_ARRAY_TYPE).
public class SingleUseArrayOnlyGen<T> : GenBase<T>
{
public override Object Test(int i, object o)
{
return new T[0][][];
}
}
public class DictionariesTest
{
[TestMethod]
public static void TestBasicDictionaryEntryTypes()
{
Type genOfType = TypeOf.D_Gen.MakeGenericType(TypeOf.CommonType1);
Type genBaseOfType = TypeOf.D_GenBase.MakeGenericType(TypeOf.CommonType1);
var genOf = (Base)Activator.CreateInstance(genOfType);
MethodInfo Test_Method = genOfType.GetTypeInfo().GetDeclaredMethod("Test");
MethodInfo GenBaseTest_Method = genBaseOfType.GetTypeInfo().GetDeclaredMethod("Test");
MethodInfo BaseTest_Method = TypeOf.D_Base.GetTypeInfo().GetDeclaredMethod("Test");
Console.WriteLine("Testing typeof() dictionary entries");
var result0 = genOf.Test(0, null);
Assert.AreEqual(result0.ToString(), "CommonType1");
result0 = Test_Method.Invoke(genOf, new object[] { 0, null });
Assert.AreEqual(result0.ToString(), "CommonType1");
result0 = BaseTest_Method.Invoke(genOf, new object[] { 0, null });
Assert.AreEqual(result0.ToString(), "CommonType1");
result0 = GenBaseTest_Method.Invoke(genOf, new object[] { 0, null });
Assert.AreEqual(result0.ToString(), "CommonType1");
var result1 = genOf.Test(1, null);
Assert.AreEqual(result1.ToString(), "System.Collections.Generic.List`1[CommonType1]");
result1 = Test_Method.Invoke(genOf, new object[] { 1, null });
Assert.AreEqual(result1.ToString(), "System.Collections.Generic.List`1[CommonType1]");
result1 = BaseTest_Method.Invoke(genOf, new object[] { 1, null });
Assert.AreEqual(result1.ToString(), "System.Collections.Generic.List`1[CommonType1]");
result1 = GenBaseTest_Method.Invoke(genOf, new object[] { 1, null });
Assert.AreEqual(result1.ToString(), "System.Collections.Generic.List`1[CommonType1]");
var result2 = genOf.Test(3, null);
Assert.AreEqual(result2.ToString(), "CommonType1[]");
result2 = Test_Method.Invoke(genOf, new object[] { 3, null });
Assert.AreEqual(result2.ToString(), "CommonType1[]");
result2 = BaseTest_Method.Invoke(genOf, new object[] { 3, null });
Assert.AreEqual(result2.ToString(), "CommonType1[]");
result2 = GenBaseTest_Method.Invoke(genOf, new object[] { 3, null });
Assert.AreEqual(result2.ToString(), "CommonType1[]");
var result3 = genOf.Test(4, null);
Assert.AreEqual(result3.ToString(), "CommonType1");
result3 = Test_Method.Invoke(genOf, new object[] { 4, null });
Assert.AreEqual(result3.ToString(), "CommonType1");
result3 = BaseTest_Method.Invoke(genOf, new object[] { 4, null });
Assert.AreEqual(result3.ToString(), "CommonType1");
result3 = GenBaseTest_Method.Invoke(genOf, new object[] { 4, null });
Assert.AreEqual(result3.ToString(), "CommonType1");
Console.WriteLine("Testing interface dispatch dictionary entries");
var listType = TypeOf.List.MakeGenericType(TypeOf.CommonType1);
var listObject = Activator.CreateInstance(listType);
var listAddMethod = listType.GetTypeInfo().GetDeclaredMethod("Add");
listAddMethod.Invoke(listObject, new object[] { new CommonType1() });
listAddMethod.Invoke(listObject, new object[] { new CommonType1() });
listAddMethod.Invoke(listObject, new object[] { new CommonType1() });
var result4 = genOf.Test(2, listObject);
Assert.AreEqual(result4, 3);
result4 = Test_Method.Invoke(genOf, new object[] { 2, listObject });
Assert.AreEqual(result4, 3);
result4 = BaseTest_Method.Invoke(genOf, new object[] { 2, listObject });
Assert.AreEqual(result4, 3);
result4 = GenBaseTest_Method.Invoke(genOf, new object[] { 2, listObject });
Assert.AreEqual(result4, 3);
Console.WriteLine("Testing invalid cast");
try
{
genOf.Test(5, genOf);
Console.WriteLine("Didn't throw expected exception!");
Assert.AreEqual(false, true);
}
catch (InvalidCastException)
{
}
try
{
Test_Method.Invoke(genOf, new object[] { 5, genOf });
Console.WriteLine("Didn't throw expected exception!");
Assert.AreEqual(false, true);
}
catch (TargetInvocationException ex)
{
Assert.AreEqual(ex.InnerException.GetType(), typeof(InvalidCastException));
}
object result_iface;
Console.WriteLine("Testing interface dispatch");
{
result_iface = ((IFace<CommonType1>)genOf).InterfaceMethod();
Assert.AreEqual(result_iface, 42);
result_iface = ((IFace2<int>)genOf).Interface2Method();
Assert.AreEqual(result_iface, "IFace2<int>.Interface2Method on Gen<" + TypeOf.CommonType1 + ">");
result_iface = ((IFace2<string>)genOf).Interface2Method();
Assert.AreEqual(result_iface, "Interface2Method on Gen<" + TypeOf.CommonType1 + ">");
result_iface = ((IDerivedIFace<long>)genOf).IDerivedIFaceMethod();
Assert.AreEqual(result_iface, "IDerivedIFace<long>.IDerivedIFaceMethod on Gen<" + TypeOf.CommonType1 + ">");
result_iface = ((IDerivedIFace<string>)genOf).IDerivedIFaceMethod();
Assert.AreEqual(result_iface, "IDerivedIFaceMethod on Gen<" + TypeOf.CommonType1 + ">");
// IFace2<long>/<string> comes from the inheritance of IDerivedIFace<long>/<string>
result_iface = ((IFace2<long>)genOf).Interface2Method();
Assert.AreEqual(result_iface, "Interface2Method on Gen<" + TypeOf.CommonType1 + ">");
result_iface = ((IFace2<string>)genOf).Interface2Method();
Assert.AreEqual(result_iface, "Interface2Method on Gen<" + TypeOf.CommonType1 + ">");
}
// Reflection calls for statically existing interface instantiations
{
MethodInfo InterfaceMethod_Method = typeof(IFace2<int>).GetTypeInfo().GetDeclaredMethod("Interface2Method");
result_iface = InterfaceMethod_Method.Invoke(genOf, new object[0]);
Assert.AreEqual(result_iface, "IFace2<int>.Interface2Method on Gen<" + TypeOf.CommonType1 + ">");
InterfaceMethod_Method = typeof(IFace2<string>).GetTypeInfo().GetDeclaredMethod("Interface2Method");
result_iface = InterfaceMethod_Method.Invoke(genOf, new object[0]);
Assert.AreEqual(result_iface, "Interface2Method on Gen<" + TypeOf.CommonType1 + ">");
InterfaceMethod_Method = typeof(IFace<CommonType1>).GetTypeInfo().GetDeclaredMethod("InterfaceMethod");
result_iface = (int)InterfaceMethod_Method.Invoke(genOf, new object[0]);
Assert.AreEqual(result_iface, 42);
InterfaceMethod_Method = typeof(IDerivedIFace<long>).GetTypeInfo().GetDeclaredMethod("IDerivedIFaceMethod");
result_iface = InterfaceMethod_Method.Invoke(genOf, new object[0]);
Assert.AreEqual(result_iface, "IDerivedIFace<long>.IDerivedIFaceMethod on Gen<" + TypeOf.CommonType1 + ">");
InterfaceMethod_Method = typeof(IDerivedIFace<string>).GetTypeInfo().GetDeclaredMethod("IDerivedIFaceMethod");
result_iface = InterfaceMethod_Method.Invoke(genOf, new object[0]);
Assert.AreEqual(result_iface, "IDerivedIFaceMethod on Gen<" + TypeOf.CommonType1 + ">");
// IFace2<long>/<string> comes from the inheritance of IDerivedIFace<long>/<string>
InterfaceMethod_Method = typeof(IFace2<long>).GetTypeInfo().GetDeclaredMethod("Interface2Method");
result_iface = InterfaceMethod_Method.Invoke(genOf, new object[0]);
Assert.AreEqual(result_iface, "Interface2Method on Gen<" + TypeOf.CommonType1 + ">");
InterfaceMethod_Method = typeof(IFace2<string>).GetTypeInfo().GetDeclaredMethod("Interface2Method");
result_iface = InterfaceMethod_Method.Invoke(genOf, new object[0]);
Assert.AreEqual(result_iface, "Interface2Method on Gen<" + TypeOf.CommonType1 + ">");
}
// Reflection calls for dynamically created interface instantiation
{
Type IFace3Of = TypeOf.D_IFace3.MakeGenericType(TypeOf.CommonType1);
MethodInfo Interface3Method_Method = IFace3Of.GetTypeInfo().GetDeclaredMethod("Interface3Method");
result_iface = Interface3Method_Method.Invoke(genOf, new object[0]);
Assert.AreEqual(result_iface, "Interface3Method on Gen<" + TypeOf.CommonType1 + ">");
}
Type singleUseArrayOnlyGenOfType = TypeOf.D_SingleUseArrayOnlyGen.MakeGenericType(TypeOf.CommonType1);
Test_Method = singleUseArrayOnlyGenOfType.GetTypeInfo().GetDeclaredMethod("Test");
var singleUseArrayOnlyGenOf = (Base)Activator.CreateInstance(singleUseArrayOnlyGenOfType);
var result6 = singleUseArrayOnlyGenOf.Test(0, null);
Assert.AreEqual(result6.GetType().ToString(), "CommonType1[][][]");
result6 = Test_Method.Invoke(singleUseArrayOnlyGenOf, new object[] { 0, null });
Assert.AreEqual(result6.GetType().ToString(), "CommonType1[][][]");
result6 = BaseTest_Method.Invoke(singleUseArrayOnlyGenOf, new object[] { 0, null });
Assert.AreEqual(result6.GetType().ToString(), "CommonType1[][][]");
result6 = GenBaseTest_Method.Invoke(singleUseArrayOnlyGenOf, new object[] { 0, null });
Assert.AreEqual(result6.GetType().ToString(), "CommonType1[][][]");
}
[TestMethod]
public static void StaticMethodFolding_Test()
{
var genOfType = TypeOf.D_Gen.MakeGenericType(TypeOf.CommonType1);
var result = genOfType.GetTypeInfo().GetDeclaredMethod("TestTypeDict").Invoke(null, new object[0]);
Assert.AreEqual(result.ToString(), "CommonType1");
var temp = typeof(Gen<string>);
var staticallyExistingGenOfType = TypeOf.D_Gen.MakeGenericType(TypeOf.String);
result = staticallyExistingGenOfType.GetTypeInfo().GetDeclaredMethod("TestTypeDict").Invoke(null, new object[0]);
Assert.AreEqual(result.ToString(), "System.String");
temp = typeof(Gen<Type>);
staticallyExistingGenOfType = TypeOf.D_Gen.MakeGenericType(TypeOf.Type);
result = staticallyExistingGenOfType.GetTypeInfo().GetDeclaredMethod("TestTypeDict").Invoke(null, new object[0]);
Assert.AreEqual(result.ToString(), "System.Type");
}
[TestMethod]
public static void NullableTesting()
{
NullableTesting_Inner(TypeOf.CommonType1, TypeOf.CommonType2);
NullableTesting_Inner(TypeOf.CommonType2, TypeOf.CommonType3);
NullableTesting_Inner(TypeOf.CommonType3, TypeOf.CommonType1);
}
static void NullableTesting_Inner(Type arg1, Type arg2)
{
var structOf = TypeOf.D_GenericStruct.MakeGenericType(arg1);
var structInst1 = Activator.CreateInstance(TypeOf.D_GenericStruct.MakeGenericType(arg1));
var structInst2 = Activator.CreateInstance(TypeOf.D_GenericStruct.MakeGenericType(arg2));
var nullableTestOf = TypeOf.D_NullableTest.MakeGenericType(structOf);
Base test = (Base)Activator.CreateInstance(nullableTestOf);
// Type cast T -> T?
{
var result = test.Test(0, null);
Assert.AreEqual(structOf, result.GetType());
Assert.IsTrue(structInst1 != result);
result = test.Test(0, structInst1);
Assert.AreEqual(structOf, result.GetType());
Assert.IsTrue(structInst1.Equals(result));
}
// is T?
{
var result = test.Test(1, null);
Assert.IsTrue((bool)result == false);
result = test.Test(1, structInst1);
Assert.IsTrue((bool)result == true);
result = test.Test(1, structInst2);
Assert.IsTrue((bool)result == false);
}
// Arrays of T?
{
object result = test.Test(2, null);
Assert.AreEqual(structOf, result.GetType());
result = test.Test(2, structInst1);
Assert.IsTrue(structInst1.Equals(result));
try
{
test.Test(2, structInst2);
Console.WriteLine("Didn't throw expected exception!");
Assert.AreEqual(false, true);
}
catch (InvalidCastException)
{
}
}
// Delegates taking Nullable<T>
{
var targetType = TypeOf.D_DelegateTarget.MakeGenericType(structOf);
var delegateType = TypeOf.D_DelWithNullable.MakeGenericType(structOf);
var targetMethod = targetType.GetTypeInfo().GetDeclaredMethod("DelWithNullableTarget");
Delegate d = targetMethod.CreateDelegate(delegateType, null);
Object[] args = { structInst1 };
object result = d.DynamicInvoke(args);
Assert.IsTrue(structInst1.Equals(result));
}
}
}
}
namespace TypeDictTestTypes
{
public class MyClass5 { }
public class MyClass4<X, Y>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static void M(X x, Y y, string typename)
{
Assert.AreEqual(typeof(X) + "," + typeof(Y).ToString(), typename + ",TypeDictTestTypes.MyClass5");
// Recursive reference
TypeDictTestTypes.MyClass1<X>.M2(x, typename);
TypeDictTestTypes.MyClass1<Y>.M2(y, "TypeDictTestTypes.MyClass5");
}
}
public class MyClass3<U, V>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static void M(U u, V v, string typename)
{
Assert.AreEqual(typeof(U) + "," + typeof(V).ToString(), "TypeDictTestTypes.MyClass5," + typename);
TypeDictTestTypes.MyClass4<V, U>.M(v, u, typename);
}
}
public class MyClass7 { public int f; }
public class MyClass6<T, U> where T : MyClass7
{
public int f;
[MethodImpl(MethodImplOptions.NoInlining)]
public static void M(T t)
{
// VSO Bug 237266. This will become a MDIL_BOX_ANY operation when T is instantiated over
// Universal Canon. The NUTC optimizer doesn't model the incoming parameter "t" as being
// dereferenced, therefore the initialization to the parameter on the caller side will
// be removed, firing the assert.
MyClass7 o = (MyClass7)t;
Assert.AreEqual(o.f, 100);
}
}
public class MyClass2<T>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static void M(T t, string typename)
{
Assert.AreEqual(typeof(T).ToString(), typename);
TypeDictTestTypes.MyClass3<MyClass5, T>.M(new MyClass5(), t, typename);
MyClass7 o = new MyClass7();
// VSO Bug 237266. The assignment will be removed as the following instantiation will
// be on Universal Canon. See the comment inside MyClass6.M.
o.f = 100;
TypeDictTestTypes.MyClass6<MyClass7, T>.M(o);
}
}
public class MyClass1<T>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static void M(T t, string typename)
{
Assert.AreEqual(typeof(T).ToString(), typename);
TypeDictTestTypes.MyClass2<T>.M(t, typename);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void M2(T t, string typename)
{
Assert.AreEqual(typeof(T).ToString(), typename);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public void M3(T t, string typename)
{
M(t, typename);
}
}
public struct MyStruct
{
public int m_IntMember;
public string m_StringMember;
}
public class DictionariesTest
{
[TestMethod]
public static void TestGenericTypeDictionary()
{
TypeDictionaryTestInstance(TypeOf.CommonType1, new object[] { new CommonType1(), "CommonType1" });
TypeDictionaryTestInstance(TypeOf.CommonType2, new object[] { new CommonType2(), "CommonType2" });
TypeDictionaryTestInstance(TypeOf.Int32, new object[] { 1, "System.Int32" });
TypeDictionaryTestInstance(TypeOf.Bool, new object[] { true, "System.Boolean" });
TypeDictionaryTestInstance(TypeOf.TDT_MyStruct, new object[] { new MyStruct { m_IntMember = 2, m_StringMember = "fff" }, "TypeDictTestTypes.MyStruct" });
}
static void TypeDictionaryTestInstance(Type typeArg, object[] parameters)
{
Type genMyClass1OfTypeArg = TypeOf.TDT_MyClass1.MakeGenericType(typeArg);
var methodM = genMyClass1OfTypeArg.GetTypeInfo().GetDeclaredMethod("M3");
var instanceObj = Activator.CreateInstance(genMyClass1OfTypeArg);
Assert.AreEqual(instanceObj.GetType(), genMyClass1OfTypeArg);
Assert.AreEqual(instanceObj.GetType().ToString(), genMyClass1OfTypeArg.ToString());
methodM.Invoke(instanceObj, parameters);
}
}
}
namespace MethodDictionaryTest
{
public class MyClass1<T>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static void M()
{
DictionariesTest.s_Result.AppendLine("MyClass1<" + typeof(T).Name + ">.M()");
}
}
public class MyClass2
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static void M<T>()
{
DictionariesTest.s_Result.AppendLine("MyClass2.M<" + typeof(T).Name + ">()");
}
}
public class Yahoo<X>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static void SGenM<Y>()
{
DictionariesTest.s_Result.AppendLine("Yahoo<" + typeof(X).Name + ">.SGenM<" + typeof(Y).Name + ">()");
}
[MethodImpl(MethodImplOptions.NoInlining)]
public void GenM<Y>(bool recurse = true)
{
DictionariesTest.s_Result.AppendLine("Yahoo<" + typeof(X).Name + ">.GenM<" + typeof(Y).Name + ">()");
if (recurse)
{
GenM<Y>(false);
GenM<X>(false);
MyClass1<X>.M();
MyClass1<Y>.M();
MyClass2.M<X>();
MyClass2.M<Y>();
Yahoo<X>.SGenM<Y>();
Yahoo<X>.SGenM<X>();
Yahoo<Y>.SGenM<Y>();
Yahoo<Y>.SGenM<X>();
}
}
}
public class Foo
{
[MethodImpl(MethodImplOptions.NoInlining)]
public void GenM<M, N>()
{
DictionariesTest.s_Result.AppendLine("Foo.GenM<" + typeof(M).Name + "," + typeof(N).Name + ">()");
new Yahoo<N>().GenM<M>();
}
}
public class Bar<T, U>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public void M1()
{
DictionariesTest.s_Result.AppendLine("Bar<" + typeof(T).Name + "," + typeof(U).Name + ">.M1()");
new Foo().GenM<U, T>();
}
}
public class DictionariesTest
{
public static StringBuilder s_Result;
static void DoTest(Type inst1, Type inst2)
{
s_Result = new StringBuilder();
var t = TypeOf.MDT_Bar.MakeGenericType(new Type[] { inst1, inst2 });
var o = Activator.CreateInstance(t);
var mi = t.GetTypeInfo().GetDeclaredMethod("M1");
mi.Invoke(o, null);
StringBuilder expected = new StringBuilder();
expected.AppendLine("Bar<" + inst1.Name + "," + inst2.Name + ">.M1()");
expected.AppendLine("Foo.GenM<" + inst2.Name + "," + inst1.Name + ">()");
expected.AppendLine("Yahoo<" + inst1.Name + ">.GenM<" + inst2.Name + ">()");
expected.AppendLine("Yahoo<" + inst1.Name + ">.GenM<" + inst2.Name + ">()");
expected.AppendLine("Yahoo<" + inst1.Name + ">.GenM<" + inst1.Name + ">()");
expected.AppendLine("MyClass1<" + inst1.Name + ">.M()");
expected.AppendLine("MyClass1<" + inst2.Name + ">.M()");
expected.AppendLine("MyClass2.M<" + inst1.Name + ">()");
expected.AppendLine("MyClass2.M<" + inst2.Name + ">()");
expected.AppendLine("Yahoo<" + inst1.Name + ">.SGenM<" + inst2.Name + ">()");
expected.AppendLine("Yahoo<" + inst1.Name + ">.SGenM<" + inst1.Name + ">()");
expected.AppendLine("Yahoo<" + inst2.Name + ">.SGenM<" + inst2.Name + ">()");
expected.AppendLine("Yahoo<" + inst2.Name + ">.SGenM<" + inst1.Name + ">()");
using (StringReader expectedReader = new StringReader(expected.ToString()))
{
using (StringReader resultReader = new StringReader(s_Result.ToString()))
{
string expectedLine = expectedReader.ReadLine();
string resultLine = resultReader.ReadLine();
Assert.AreEqual(expectedLine, resultLine);
}
}
}
[TestMethod]
public static void TestMethodDictionaries()
{
DoTest(TypeOf.CommonType2, TypeOf.CommonType2);
DoTest(TypeOf.CommonType1, TypeOf.CommonType2);
DoTest(TypeOf.CommonType2, TypeOf.CommonType1);
DoTest(TypeOf.CommonType1, TypeOf.CommonType1);
}
}
}
namespace BaseTypeDict
{
public class MyClass1
{
[MethodImpl(MethodImplOptions.NoInlining)]
virtual public string M1() { return "MyClass1.M1()"; }
[MethodImpl(MethodImplOptions.NoInlining)]
virtual public string M2() { return "MyClass1.M2()"; }
}
public class MyClass2<T> : MyClass1
{
[MethodImpl(MethodImplOptions.NoInlining)]
public override string M1() { return "MyClass2`1<" + typeof(T).Name + ">.M1() - " + base.M1(); }
[MethodImpl(MethodImplOptions.NoInlining)]
public override string M2() { return "MyClass2`1<" + typeof(T).Name + ">.M2() - " + base.M2(); }
}
public class MyClass3<T> : MyClass2<T>
{
[MethodImpl(MethodImplOptions.NoInlining)]
new virtual public string M1() { return "MyClass3`1<" + typeof(T).Name + ">.M1() - " + base.M1(); }
[MethodImpl(MethodImplOptions.NoInlining)]
public override string M2() { return "MyClass3`1<" + typeof(T).Name + ">.M2() - " + base.M2(); }
}
public class MyClass4<T> : MyClass3<T>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public override string M1() { return "MyClass4`1<" + typeof(T).Name + ">.M1() - " + base.M1(); }
[MethodImpl(MethodImplOptions.NoInlining)]
public override string M2() { return "MyClass4`1<" + typeof(T).Name + ">.M2() - " + base.M2(); }
}
public class MyClass3_2 : MyClass2<MyClass1>
{
[MethodImpl(MethodImplOptions.NoInlining)]
new virtual public string M1() { return "MyClass3_2.M1() - " + base.M1(); }
[MethodImpl(MethodImplOptions.NoInlining)]
public override string M2() { return "MyClass3_2.M2() - " + base.M2(); }
}
public class MyClass4_2<T> : MyClass3_2
{
[MethodImpl(MethodImplOptions.NoInlining)]
public override string M1() { return "MyClass4_2`1<" + typeof(T).Name + ">.M1() - " + base.M1(); }
[MethodImpl(MethodImplOptions.NoInlining)]
public override string M2() { return "MyClass4_2`1<" + typeof(T).Name + ">.M2() - " + base.M2(); }
}
public class MyClass4_3<T> : MyClass2<MyClass1>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public override string M1() { return "MyClass4_3`1<" + typeof(T).Name + ">.M1() - " + base.M1(); }
[MethodImpl(MethodImplOptions.NoInlining)]
public override string M2() { return "MyClass4_3`1<" + typeof(T).Name + ">.M2() - " + base.M2(); }
}
#if USC
public struct Foo2 { }
#else
public class Foo1
{
}
public class Foo2 : Foo1
{
}
#endif
public class GenBase2<T, U>
{
public virtual string M() { return "GenBase2<" + typeof(T).Name + "," + typeof(U).Name + ">"; }
}
public class GenBase1<T> : GenBase2<int, T>
{
public new virtual string M() { return "GenBase1<" + typeof(T).Name + "> - " + base.M(); }
}
public class Gen1<T> : GenBase1<T>
{
public new virtual string M() { return "Gen1<" + typeof(T).Name + "> - " + base.M(); }
}
public class Gen2<T> : GenBase1<int>
{
public new virtual string M() { return "Gen2<" + typeof(T).Name + "> - " + base.M(); }
}
public class GenBase2<T>
{
public virtual string M1() { return "GenBase2<" + typeof(T).Name + ">.M1()"; }
public virtual string M2() { return "GenBase2<" + typeof(T).Name + ">.M2()"; }
}
public class GenDerived2<T, U> : GenBase2<U>
{
public override string M1() { return "GenDerived2<" + typeof(T).Name + "," + typeof(U).Name + ">.M1() - " + base.M1(); }
}
public class Test
{
[TestMethod]
public static void TestVirtCallTwoGenParams()
{
var t = TypeOf.BTDT_GenDerived2.MakeGenericType(TypeOf.CommonType1, TypeOf.String);
var o = Activator.CreateInstance(t);
var m1 = t.GetTypeInfo().GetDeclaredMethod("M1");
string m1result = (string)m1.Invoke(o, null);
Assert.AreEqual("GenDerived2<CommonType1,String>.M1() - GenBase2<String>.M1()", m1result);
GenBase2<string> oo = (GenBase2<string>)o;
m1result = oo.M1();
Assert.AreEqual("GenDerived2<CommonType1,String>.M1() - GenBase2<String>.M1()", m1result);
string m2result = oo.M2();
Assert.AreEqual("GenBase2<String>.M2()", m2result);
}
static string BuildResultString(string genericType, string genericArg, string methodName)
{
if (genericType == "MyClass4`1")
return genericType + "<" + genericArg + ">." + methodName + "() - " + BuildResultString("MyClass3`1", genericArg, methodName);
else if (genericType == "MyClass4_2`1")
return genericType + "<" + genericArg + ">." + methodName + "() - " + BuildResultString("MyClass3_2", "MyClass1", methodName);
else if (genericType == "MyClass4_3`1")
return genericType + "<" + genericArg + ">." + methodName + "() - " + BuildResultString("MyClass2`1", "MyClass1", methodName);
else if (genericType == "MyClass3`1")
return genericType + "<" + genericArg + ">." + methodName + "() - " + BuildResultString("MyClass2`1", genericArg, methodName);
else if (genericType == "MyClass3_2")
return genericType + "." + methodName + "() - " + BuildResultString("MyClass2`1", "MyClass1", methodName);
else if (genericType == "MyClass2`1")
return genericType + "<" + genericArg + ">." + methodName + "() - " + BuildResultString("MyClass1", genericArg, methodName);
else
return genericType + "." + methodName + "()";
}
static void DoTest(Type genericType, Type genericArg)
{
var t = genericType.MakeGenericType(genericArg);
var o = Activator.CreateInstance(t);
var m1 = t.GetTypeInfo().GetDeclaredMethod("M1");
var m2 = t.GetTypeInfo().GetDeclaredMethod("M2");
string m1result = (string)m1.Invoke(o, null);
string m2result = (string)m2.Invoke(o, null);
string m1Expected = BuildResultString(genericType.Name, genericArg.Name, "M1");
string m2Expected = BuildResultString(genericType.Name, genericArg.Name, "M2");
Assert.AreEqual(m1Expected, m1result);
Assert.AreEqual(m2Expected, m2result);
}
[TestMethod]
public static void TestUsingPrimitiveTypes()
{
var t1 = TypeOf.BTDT_Gen1.MakeGenericType(TypeOf.BTDT_Foo2);
var t2 = TypeOf.BTDT_Gen2.MakeGenericType(TypeOf.BTDT_Foo2);
var o1 = Activator.CreateInstance(t1);
var o2 = Activator.CreateInstance(t2);
var mi1 = t1.GetTypeInfo().GetDeclaredMethod("M");
var mi2 = t2.GetTypeInfo().GetDeclaredMethod("M");
var result1 = (string)mi1.Invoke(o1, null);
var result2 = (string)mi2.Invoke(o2, null);
Assert.AreEqual("Gen1<Foo2> - GenBase1<Foo2> - GenBase2<Int32,Foo2>", result1);
Assert.AreEqual("Gen2<Foo2> - GenBase1<Int32> - GenBase2<Int32,Int32>", result2);
}
[TestMethod]
public static void TestBaseTypeDictionaries()
{
foreach (var genType in new[] { TypeOf.BTDT_MyClass2, TypeOf.BTDT_MyClass3, TypeOf.BTDT_MyClass4 })
{
DoTest(genType, TypeOf.CommonType1);
DoTest(genType, TypeOf.CommonType2);
}
foreach (var genType in new[] { TypeOf.BTDT_MyClass4, TypeOf.BTDT_MyClass3, TypeOf.BTDT_MyClass2 })
{
DoTest(genType, TypeOf.CommonType3);
DoTest(genType, TypeOf.CommonType2);
}
foreach (var genType in new[] { TypeOf.BTDT_MyClass2, TypeOf.BTDT_MyClass4_2, TypeOf.BTDT_MyClass4_3 })
{
DoTest(genType, TypeOf.CommonType4);
DoTest(genType, TypeOf.CommonType2);
}
foreach (var genType in new[] { TypeOf.BTDT_MyClass4_3, TypeOf.BTDT_MyClass4_2, TypeOf.BTDT_MyClass2 })
{
DoTest(genType, TypeOf.CommonType5);
DoTest(genType, TypeOf.CommonType2);
}
}
}
}
namespace DictDependency
{
#if USC
public struct MyType1<A, B> { }
public struct MyType2<A, B> { }
public struct MyType3 { }
public struct MyType4 { }
#else
public class MyType1<A, B> { }
public class MyType2<A, B> { }
public class MyType3 { }
public class MyType4 { }
#endif
public class TestClass1<T>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static string M<U>()
{
return "TestClass1<" + typeof(T) + ">.M<" + typeof(U) + ">()";
}
}
public class TestClass2<T>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static string M<U>()
{
return "TestClass2<" + typeof(T) + ">.M<" + typeof(U) + ">()";
}
}
public abstract class Base
{
public abstract string Method1();
public abstract string Method2();
public abstract string Method3();
public abstract string Method4();
}
public class Yahoo<X, Y> : Base
{
[MethodImpl(MethodImplOptions.NoInlining)]
public override string Method1()
{
return TestClass1<X>.M<Action<MyType1<X, Y>>>();
}
[MethodImpl(MethodImplOptions.NoInlining)]
public override string Method2()
{
return TestClass1<Y>.M<Action<MyType1<Y, X>>>();
}
[MethodImpl(MethodImplOptions.NoInlining)]
public override string Method3()
{
return TestClass2<Action<MyType2<X, Y>>>.M<X>();
}
[MethodImpl(MethodImplOptions.NoInlining)]
public override string Method4()
{
return TestClass2<Action<MyType2<Y, X>>>.M<Y>();
}
}
public class Test
{
[TestMethod]
public static void TestIndirectDictionaryDependencies()
{
{
var t = TypeOf.DDT_Yahoo.MakeGenericType(TypeOf.CommonType1, typeof(MyType3));
Base b = (Base)Activator.CreateInstance(t);
var result = b.Method1();
Assert.AreEqual("TestClass1<CommonType1>.M<System.Action`1[DictDependency.MyType1`2[CommonType1,DictDependency.MyType3]]>()", result);
result = b.Method2();
Assert.AreEqual("TestClass1<DictDependency.MyType3>.M<System.Action`1[DictDependency.MyType1`2[DictDependency.MyType3,CommonType1]]>()", result);
}
{
var t = TypeOf.DDT_Yahoo.MakeGenericType(TypeOf.CommonType1, typeof(MyType4));
Base b = (Base)Activator.CreateInstance(t);
var result = b.Method3();
Assert.AreEqual("TestClass2<System.Action`1[DictDependency.MyType2`2[CommonType1,DictDependency.MyType4]]>.M<CommonType1>()", result);
result = b.Method4();
Assert.AreEqual("TestClass2<System.Action`1[DictDependency.MyType2`2[DictDependency.MyType4,CommonType1]]>.M<DictDependency.MyType4>()", result);
}
}
}
}
namespace CtorDict
{
public class MyType1
{
private string _n;
public MyType1() { _n = "TEST"; }
public override string ToString() { return "MyType1::" + _n; }
}
public class MyType2
{
private int _n;
public MyType2() { _n = 123; }
~MyType2() { }
public override string ToString() { return "MyType2::" + _n; }
}
public class MyType3
{
public MyType3(string s) { }
public override string ToString() { return "MyType3"; }
}
public class MyType4<T>
{
private T _n;
public MyType4() { _n = default(T); }
public override string ToString() { return "MyType4<" + typeof(T) + ">::" + _n; }
}
public class MyType5
{
UInt64 _long;
double _double;
public MyType5() { _long = 123456789; _double = 12345.6789; }
public override string ToString() { return "MyType5::" + _long + "~" + _double; }
}
public class MyType6
{
long _long;
double _double;
public MyType6() { _long = 123456789; _double = 12345.6789; }
~MyType6() { }
public override string ToString() { return "MyType6::" + _long + "~" + _double; }
}
public class MyType7
{
private int _n;
private string _s;
public MyType7(string a, int b) { _s = a; _n = b; }
public override string ToString() { return "MyType7::" + _s + "~" + _n; }
}
public class MyType8
{
private string _s;
private MyType8(string a) { _s = a; }
public override string ToString() { return "MyType8::" + _s; }
}
// TODO once we support instantiations over non-reference type arguments : Test with :
// enums(short) and valuetype that has alignment requirement <= 4, with finalizer (to test usage of the NewFinalizable helper)
// enums(short) and valuetype that has alignment requirement <= 4, without finalizer (to test usage of the NewFast helper)
// enums(long) and valuetype that has alignment requirement > 4, with finalizer (to test usage of the NewFinalizableAlign8 helper)
// enums(long) and valuetype that has alignment requirement > 4, without finalizer (to test usage of the NewFastMisalign helper)