2 using System.Collections.Generic;
4 using System.Runtime.CompilerServices;
5 using System.Threading;
6 using System.Threading.Tasks;
18 public TestStruct (int i, int j) {
25 class Enumerator <T> : MyIEnumerator <T> {
26 T MyIEnumerator<T>.Current {
32 bool MyIEnumerator<T>.MoveNext () {
37 class Comparer <T> : IComparer <T> {
38 bool IComparer<T>.Compare (T x, T y) {
45 static int Main (string[] args)
47 return TestDriver.RunTests (typeof (Tests), args);
51 public static int test_1_nullable_unbox ()
53 return Unbox<int?> (1).Value;
56 public static int test_1_nullable_unbox_null ()
58 return Unbox<int?> (null).HasValue ? 0 : 1;
61 public static int test_1_nullable_box ()
63 return (int) Box<int?> (1);
66 public static int test_1_nullable_box_null ()
68 return Box<int?> (null) == null ? 1 : 0;
71 public static int test_1_isinst_nullable ()
74 return (o is int?) ? 1 : 0;
77 public static int test_1_nullable_unbox_vtype ()
79 return Unbox<TestStruct?> (new TestStruct (1, 2)).Value.i;
82 public static int test_1_nullable_unbox_null_vtype ()
84 return Unbox<TestStruct?> (null).HasValue ? 0 : 1;
87 public static int test_1_nullable_box_vtype ()
89 return ((TestStruct)(Box<TestStruct?> (new TestStruct (1, 2)))).i;
92 public static int test_1_nullable_box_null_vtype ()
94 return Box<TestStruct?> (null) == null ? 1 : 0;
97 public static int test_1_isinst_nullable_vtype ()
99 object o = new TestStruct (1, 2);
100 return (o is TestStruct?) ? 1 : 0;
103 public static int test_0_nullable_normal_unbox ()
108 // This uses unbox instead of unbox_any
117 public static void stelem_any<T> (T[] arr, T elem) {
121 public static T ldelem_any<T> (T[] arr) {
125 public static int test_1_ldelem_stelem_any_int () {
126 int[] arr = new int [3];
129 return ldelem_any (arr);
132 public static T return_ref<T> (ref T t) {
136 public static T ldelema_any<T> (T[] arr) {
137 return return_ref<T> (ref arr [0]);
140 public static int test_0_ldelema () {
141 string[] arr = new string [1];
145 if (ldelema_any <string> (arr) == "Hello")
151 public static T[,] newarr_multi<T> () {
155 public static int test_0_newarr_multi_dim () {
156 return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
164 public static int test_0_iface_call_null_bug_77442 () {
170 catch (NullReferenceException) {
177 public static int test_18_ldobj_stobj_generics () {
178 GenericClass<int> t = new GenericClass <int> ();
181 return t.ldobj_stobj (ref i, ref j) + i + j;
184 public static int test_5_ldelem_stelem_generics () {
185 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
187 TestStruct s = new TestStruct (5, 5);
188 return t.ldelem_stelem (s).i;
191 public static int test_0_constrained_vtype_box () {
192 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
195 return t.toString (new TestStruct ()) == "GenericsTests+TestStruct" ? 0 : 1;
197 return t.toString (new TestStruct ()) == "Tests+TestStruct" ? 0 : 1;
201 public static int test_0_constrained_vtype () {
202 GenericClass<int> t = new GenericClass<int> ();
204 return t.toString (1234) == "1234" ? 0 : 1;
207 public static int test_0_constrained_reftype () {
208 GenericClass<String> t = new GenericClass<String> ();
210 return t.toString ("1234") == "1234" ? 0 : 1;
213 public static int test_0_box_brtrue_optimizations () {
217 if (!IsNull<object>(null))
223 [Category ("!FULLAOT")]
224 public static int test_0_generic_get_value_optimization_int () {
225 int[] x = new int[] {100, 200};
227 if (GenericClass<int>.Z (x, 0) != 100)
230 if (GenericClass<int>.Z (x, 1) != 200)
236 public static int test_0_generic_get_value_optimization_vtype () {
237 TestStruct[] arr = new TestStruct[] { new TestStruct (100, 200), new TestStruct (300, 400) };
238 IEnumerator<TestStruct> enumerator = GenericClass<TestStruct>.Y (arr);
241 while (enumerator.MoveNext ()) {
242 s = enumerator.Current;
249 s = GenericClass<TestStruct>.Z (arr, 0);
250 if (s.i != 100 || s.j != 200)
253 s = GenericClass<TestStruct>.Z (arr, 1);
254 if (s.i != 300 || s.j != 400)
260 public static int test_0_nullable_ldflda () {
261 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
264 public struct GenericStruct<T> {
267 public GenericStruct (T t) {
272 public class GenericClass<T> {
275 public GenericClass (T t) {
279 public GenericClass () {
282 public T ldobj_stobj (ref T t1, ref T t2) {
289 public T ldelem_stelem (T t) {
290 T[] arr = new T [10];
296 public String toString (T t) {
297 return t.ToString ();
300 public static IEnumerator<T> Y (IEnumerable <T> x)
302 return x.GetEnumerator ();
305 public static T Z (IList<T> x, int index)
310 protected static T NullB = default(T);
311 private static Nullable<bool> _BIsA = null;
312 public static bool BIsAClazz {
320 public class MRO : MarshalByRefObject {
321 public GenericStruct<int> struct_field;
322 public GenericClass<int> class_field;
325 public class MRO<T> : MarshalByRefObject {
328 public T stfld_ldfld (T t) {
335 public static int test_0_ldfld_stfld_mro () {
337 GenericStruct<int> s = new GenericStruct<int> (5);
338 // This generates stfld
341 // This generates ldflda
342 if (m.struct_field.t != 5)
345 // This generates ldfld
346 GenericStruct<int> s2 = m.struct_field;
350 if (m.struct_field.t != 5)
353 m.class_field = new GenericClass<int> (5);
354 if (m.class_field.t != 5)
358 var m2 = new MRO<string> ();
359 if (m2.stfld_ldfld ("A") != "A")
366 [Category ("!FULLAOT")]
367 public static int test_0_generic_virtual_call_on_vtype_unbox () {
368 object o = new Object ();
369 IFoo h = new Handler(o);
371 if (h.Bar<object> () != o)
377 public static int test_0_box_brtrue_opt () {
378 Foo<int> f = new Foo<int> (5);
385 public static int test_0_box_brtrue_opt_regress_81102 () {
386 if (new Foo<int>(5).ToString () == "null")
396 public static int test_0_ldloca_initobj_opt () {
397 if (new Foo<S> (new S ()).get_default ().i != 0)
399 if (new Foo<object> (null).get_default () != null)
405 public static int test_0_variance_reflection () {
406 // covariance on IEnumerator
407 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
409 // covariance on IEnumerator and covariance on arrays
410 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
412 // covariance and implemented interfaces
413 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
416 // contravariance on IComparer
417 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
419 // contravariance on IComparer, contravariance on arrays
420 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
422 // contravariance and interface inheritance
423 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
429 public static int test_0_ldvirtftn_generic_method () {
430 new GenericsTests ().ldvirtftn<string> ();
432 return the_type == typeof (string) ? 0 : 1;
435 public static int test_0_throw_dead_this () {
436 new Foo<string> ("").throw_dead_this ();
442 public static int test_0_inline_infinite_polymorphic_recursion () {
448 private static void f<T>(int i) {
449 if(i==42) f<S<T>>(i);
452 // This cannot be made to work with full-aot, since there it is impossible to
453 // statically determine that Foo<string>.Bar <int> is needed, the code only
454 // references IFoo.Bar<int>
455 [Category ("!FULLAOT")]
456 public static int test_0_generic_virtual_on_interfaces () {
457 Foo<string>.count1 = 0;
458 Foo<string>.count2 = 0;
459 Foo<string>.count3 = 0;
461 IFoo f = new Foo<string> ("");
462 for (int i = 0; i < 1000; ++i) {
468 if (Foo<string>.count1 != 1000)
470 if (Foo<string>.count2 != 1000)
472 if (Foo<string>.count3 != 1000)
475 VirtualInterfaceCallFromGenericMethod<long> (f);
480 public static int test_0_generic_virtual_on_interfaces_ref () {
481 Foo<string>.count1 = 0;
482 Foo<string>.count2 = 0;
483 Foo<string>.count3 = 0;
484 Foo<string>.count4 = 0;
486 IFoo f = new Foo<string> ("");
487 for (int i = 0; i < 1000; ++i) {
493 if (Foo<string>.count2 != 1000)
495 if (Foo<string>.count3 != 1000)
497 if (Foo<string>.count4 != 1000)
504 [Category ("!FULLAOT")]
505 public static int test_2_cprop_bug () {
508 var cmp = System.Collections.Generic.Comparer<int>.Default ;
509 if (cmp.Compare (a, 0) > 0)
511 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
515 enum MyEnumUlong : ulong {
519 public static int test_0_regress_550964_constrained_enum_long () {
520 MyEnumUlong a = MyEnumUlong.Value_2;
521 MyEnumUlong b = MyEnumUlong.Value_2;
523 return Pan (a, b) ? 0 : 1;
526 static bool Pan<T> (T a, T b)
531 public class XElement {
532 public string Value {
537 public static int test_0_fullaot_linq () {
538 var allWords = new XElement [] { new XElement { Value = "one" } };
539 var filteredWords = allWords.Where(kw => kw.Value.StartsWith("T"));
540 return filteredWords.Count ();
543 public static int test_0_fullaot_comparer_t () {
544 var l = new SortedList <TimeSpan, int> ();
548 public static int test_0_fullaot_comparer_t_2 () {
549 var l = new Dictionary <TimeSpan, int> ();
553 static void enumerate<T> (IEnumerable<T> arr) {
554 foreach (var o in arr)
556 int c = ((ICollection<T>)arr).Count;
559 /* Test that treating arrays as generic collections works with full-aot */
560 public static int test_0_fullaot_array_wrappers () {
561 GenericsTests[] arr = new GenericsTests [10];
562 enumerate<GenericsTests> (arr);
566 static int cctor_count = 0;
568 public abstract class Beta<TChanged>
576 public class Gamma<T> : Beta<T>
584 public static int test_2_generic_class_init_gshared_ctor () {
591 static int cctor_count2 = 0;
593 class ServiceController<T> {
594 static ServiceController () {
598 public ServiceController () {
602 static ServiceController<T> Create<T>() {
603 return new ServiceController<T>();
607 public static int test_2_generic_class_init_gshared_ctor_from_gshared () {
614 public static Type get_type<T> () {
618 public static int test_0_gshared_delegate_rgctx () {
619 Func<Type> t = new Func<Type> (get_type<string>);
621 if (t () == typeof (string))
627 // Creating a delegate from a generic method from gshared code
628 public static int test_0_gshared_delegate_from_gshared () {
629 if (gshared_delegate_from_gshared <object> () != 0)
631 if (gshared_delegate_from_gshared <string> () != 0)
636 public static int gshared_delegate_from_gshared <T> () {
637 Func<Type> t = new Func<Type> (get_type<T>);
639 return t () == typeof (T) ? 0 : 1;
642 public static int test_0_marshalbyref_call_from_gshared_virt_elim () {
643 /* Calling a virtual method from gshared code which is changed to a nonvirt call */
644 Class1<object> o = new Class1<object> ();
645 o.Do (new Class2<object> ());
649 class Pair<TKey, TValue> {
650 public static KeyValuePair<TKey, TValue> make_pair (TKey key, TValue value)
652 return new KeyValuePair<TKey, TValue> (key, value);
655 public delegate TRet Transform<TRet> (TKey key, TValue value);
658 public static int test_0_bug_620864 () {
659 var d = new Pair<string, Type>.Transform<KeyValuePair<string, Type>> (Pair<string, Type>.make_pair);
661 var p = d ("FOO", typeof (int));
662 if (p.Key != "FOO" || p.Value != typeof (int))
669 struct RecStruct<T> {
670 public void foo (RecStruct<RecStruct<T>> baz) {
674 public static int test_0_infinite_generic_recursion () {
675 // Check that the AOT compile can deal with infinite generic recursion through
685 bool IsNull2 <T> (object value) where T : struct {
686 T? item = (T?) value;
694 public static int test_0_full_aot_nullable_unbox_from_gshared_code () {
695 if (!new GenericsTests ().IsNull2<FooStruct> (null))
697 if (new GenericsTests ().IsNull2<FooStruct> (new FooStruct ()))
702 public static int test_0_partial_sharing () {
703 if (PartialShared1 (new List<string> (), 1) != typeof (string))
705 if (PartialShared1 (new List<GenericsTests> (), 1) != typeof (GenericsTests))
707 if (PartialShared2 (new List<string> (), 1) != typeof (int))
709 if (PartialShared2 (new List<GenericsTests> (), 1) != typeof (int))
714 [Category ("GSHAREDVT")]
715 public static int test_6_partial_sharing_linq () {
716 var messages = new List<Message> ();
718 messages.Add (new Message () { MessageID = 5 });
719 messages.Add (new Message () { MessageID = 6 });
721 return messages.Max(i => i.MessageID);
724 public static int test_0_partial_shared_method_in_nonshared_class () {
725 var c = new Class1<double> ();
726 return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
730 public int MessageID {
735 public static Type PartialShared1<T, K> (List<T> list, K k) {
739 public static Type PartialShared2<T, K> (List<T> list, K k) {
743 public class Class1<T> {
744 public virtual void Do (Class2<T> t) {
748 public virtual object Foo<U> (T t) {
749 return new Class1<U> ();
753 public interface IFace1<T> {
757 public class Class2<T> : MarshalByRefObject, IFace1<T> {
764 public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
768 public static Type the_type;
770 public void ldvirtftn<T> () {
771 Foo <T> binding = new Foo <T> (default (T));
773 binding.GenericEvent += event_handler;
777 public virtual void event_handler<T> (Foo<T> sender) {
778 the_type = typeof (T);
781 public interface IFoo {
786 public class Foo<T1> : IFoo
793 public override string ToString()
795 return Bar(m_t1 == null ? "null" : "null");
798 public String Bar (String s) {
802 public int this [T1 key] {
805 throw new ArgumentNullException ("key");
809 public void throw_dead_this () {
811 new SomeClass().ThrowAnException();
817 public T1 get_default () {
823 public delegate void GenericEventHandler (Foo<T1> sender);
825 public event GenericEventHandler GenericEvent;
827 public void fire () {
831 public static int count1, count2, count3, count4;
833 public void NonGeneric () {
837 public object Bar <T> () {
838 if (typeof (T) == typeof (int))
840 else if (typeof (T) == typeof (string))
842 else if (typeof (T) == typeof (object))
848 public class SomeClass {
849 public void ThrowAnException() {
850 throw new Exception ("Something went wrong");
854 struct Handler : IFoo {
857 public Handler(object o) {
861 public void NonGeneric () {
864 public object Bar<T>() {
869 static bool IsNull<T> (T t)
877 static object Box<T> (T t)
882 static T Unbox <T> (object o) {
886 interface IDefaultRetriever
891 class DefaultRetriever : IDefaultRetriever
893 [MethodImpl(MethodImplOptions.Synchronized)]
894 public T GetDefault<T>()
900 [Category ("!FULLAOT")]
901 [Category ("!BITCODE")]
902 public static int test_0_regress_668095_synchronized_gshared () {
903 return DoSomething (new DefaultRetriever ());
906 static int DoSomething(IDefaultRetriever foo) {
907 int result = foo.GetDefault<int>();
912 [MethodImpl(MethodImplOptions.Synchronized)]
913 public Type getInstance() {
918 [Category ("GSHAREDVT")]
919 static int test_0_synchronized_gshared () {
920 var c = new SyncClass<string> ();
921 if (c.getInstance () != typeof (string))
929 public static int test_0_687865_isinst_with_cache_wrapper () {
930 object o = new object ();
931 if (o is Action<IEnumerable<Response>>)
948 public DocType Type {
954 [Category ("GSHAREDVT")]
955 public static int test_0_fullaot_sflda_cctor () {
956 List<Doc> documents = new List<Doc>();
957 documents.Add(new Doc { Name = "Doc1", Type = DocType.One } );
958 documents.Add(new Doc { Name = "Doc2", Type = DocType.Two } );
959 documents.Add(new Doc { Name = "Doc3", Type = DocType.Three } );
960 documents.Add(new Doc { Name = "Doc4", Type = DocType.One } );
961 documents.Add(new Doc { Name = "Doc5", Type = DocType.Two } );
962 documents.Add(new Doc { Name = "Doc6", Type = DocType.Three } );
963 documents.Add(new Doc { Name = "Doc7", Type = DocType.One } );
964 documents.Add(new Doc { Name = "Doc8", Type = DocType.Two } );
965 documents.Add(new Doc { Name = "Doc9", Type = DocType.Three } );
967 List<DocType> categories = documents.Select(d=>d.Type).Distinct().ToList<DocType>().OrderBy(d => d).ToList();
968 foreach(DocType cat in categories) {
969 List<Doc> catDocs = documents.Where(d => d.Type == cat).OrderBy(d => d.Name).ToList<Doc>();
976 static List<A> sources = new List<A>();
979 public static int test_0_fullaot_imt () {
983 int a = sources.Count;
984 var enumerator = sources.GetEnumerator() as IEnumerator<object>;
986 while (enumerator.MoveNext())
988 object o = enumerator.Current;
997 class BClass : AClass {
1000 public static int test_0_fullaot_variant_iface () {
1001 var arr = new BClass [10];
1002 var enumerable = (IEnumerable<AClass>)arr;
1003 enumerable.GetEnumerator ();
1007 struct Record : Foo2<Record>.IRecord {
1009 int Foo2<Record>.IRecord.DoSomething () {
1014 class Foo2<T> where T : Foo2<T>.IRecord {
1015 public interface IRecord {
1019 public static int Extract (T[] t) {
1020 return t[0].DoSomething ();
1024 class Foo3<T> where T : IComparable {
1025 public static int CompareTo (T[] t) {
1026 // This is a constrained call to Enum.CompareTo ()
1027 return t[0].CompareTo (t [0]);
1031 public static int test_1_regress_constrained_iface_call_7571 () {
1032 var r = new Record [10];
1033 Foo2<Record>.Extract (r);
1034 return Foo2<Record>.Extract (r);
1037 enum ConstrainedEnum {
1041 public static int test_0_regress_constrained_iface_call_enum () {
1042 var r = new ConstrainedEnum [10];
1043 return Foo3<ConstrainedEnum>.CompareTo (r);
1046 public interface IFoo2 {
1050 public struct Foo2 : IFoo2 {
1051 public void MoveNext () {
1055 public static Action Dingus (ref Foo2 f) {
1056 return new Action (f.MoveNext);
1059 public static int test_0_delegate_unbox_full_aot () {
1060 Foo2 foo = new Foo2 ();
1061 Dingus (ref foo) ();
1065 public static int test_0_arrays_ireadonly () {
1066 int[] arr = new int [10];
1067 for (int i = 0; i < 10; ++i)
1069 IReadOnlyList<int> a = (IReadOnlyList<int>)(object)arr;
1079 public static int test_0_volatile_read_write () {
1081 Volatile.Write (ref foo, "DEF");
1082 return Volatile.Read (ref foo) == "DEF" ? 0 : 1;
1085 // FIXME: Doesn't work with --regression as Interlocked.Add(ref long) is only implemented as an intrinsic
1087 public static async Task<T> FooAsync<T> (int i, int j) {
1088 Task<int> t = new Task<int> (delegate () { Console.WriteLine ("HIT!"); return 0; });
1089 var response = await t;
1093 public static int test_0_fullaot_generic_async () {
1094 Task<string> t = FooAsync<string> (1, 2);
1095 t.RunSynchronously ();
1100 public static int test_0_delegate_callvirt_fullaot () {
1101 Func<string> f = delegate () { return "A"; };
1102 var f2 = (Func<Func<string>, string>)Delegate.CreateDelegate (typeof
1103 (Func<Func<string>, string>), null, f.GetType ().GetMethod ("Invoke"));
1106 return s == "A" ? 0 : 1;
1109 public interface ICovariant<out R>
1113 // Deleting the `out` modifier from this line stop the problem
1114 public interface IExtCovariant<out R> : ICovariant<R>
1118 public class Sample<R> : ICovariant<R>
1122 public interface IMyInterface
1126 public static int test_0_variant_cast_cache () {
1127 object covariant = new Sample<IMyInterface>();
1129 var foo = (ICovariant<IMyInterface>)(covariant);
1132 var extCovariant = (IExtCovariant<IMyInterface>)covariant;
1140 public int a1, a2, a3;
1143 class MyClass<T> where T: struct {
1144 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1145 public MyClass(int a1, int a2, int a3, int a4, int a5, int a6, Nullable<T> a) {
1148 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1149 public static MyClass<T> foo () {
1150 Nullable<T> a = new Nullable<T> ();
1151 return new MyClass<T> (0, 0, 0, 0, 0, 0, a);
1155 public static int test_0_newobj_generic_context () {
1156 MyClass<FooStruct2>.foo ();
1165 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1166 public static string constrained_tostring<T> (T t) {
1167 return t.ToString ();
1170 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1171 public static bool constrained_equals<T> (T t1, T t2) {
1172 var c = EqualityComparer<T>.Default;
1174 return c.Equals (t1, t2);
1177 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1178 public static int constrained_gethashcode<T> (T t) {
1179 return t.GetHashCode ();
1182 public static int test_0_constrained_partial_sharing () {
1185 s = constrained_tostring<int> (5);
1188 s = constrained_tostring<AnEnum> (AnEnum.B);
1192 if (!constrained_equals<int> (1, 1))
1194 if (constrained_equals<int> (1, 2))
1196 if (!constrained_equals<AnEnum> (AnEnum.A, AnEnum.A))
1198 if (constrained_equals<AnEnum> (AnEnum.A, AnEnum.B))
1201 int i = constrained_gethashcode<int> (5);
1204 i = constrained_gethashcode<AnEnum> (AnEnum.B);
1220 public static int test_0_partial_sharing_ginst () {
1221 var l1 = new List<KeyValuePair<int, Enum1>> ();
1222 l1.Add (new KeyValuePair<int, Enum1>(5, Enum1.A));
1223 if (l1 [0].Key != 5)
1225 if (l1 [0].Value != Enum1.A)
1227 var l2 = new List<KeyValuePair<int, Enum2>> ();
1228 l2.Add (new KeyValuePair<int, Enum2>(5, Enum2.B));
1229 if (l2 [0].Key != 5)
1231 if (l2 [0].Value != Enum2.B)
1236 static object delegate_8_args_res;
1238 public static int test_0_delegate_8_args () {
1239 delegate_8_args_res = null;
1240 Action<string, string, string, string, string, string, string,
1241 string> test = (a, b, c, d, e, f, g, h) =>
1243 delegate_8_args_res = h;
1245 test("a", "b", "c", "d", "e", "f", "g", "h");
1246 return delegate_8_args_res == "h" ? 0 : 1;
1249 static void throw_catch_t<T> () where T: Exception {
1251 throw new NotSupportedException ();
1256 public static int test_0_gshared_catch_open_type () {
1257 throw_catch_t<NotSupportedException> ();
1261 class ThrowClass<T> where T: Exception {
1262 public void throw_catch_t () {
1264 throw new NotSupportedException ();
1270 public static int test_0_gshared_catch_open_type_instance () {
1271 var c = new ThrowClass<NotSupportedException> ();
1278 class GenericsTests : Tests