2 using System.Collections.Generic;
4 using System.Runtime.CompilerServices;
5 using System.Threading;
6 using System.Threading.Tasks;
14 public TestStruct (int i, int j) {
20 class Enumerator <T> : MyIEnumerator <T> {
21 T MyIEnumerator<T>.Current {
27 bool MyIEnumerator<T>.MoveNext () {
32 class Comparer <T> : IComparer <T> {
33 bool IComparer<T>.Compare (T x, T y) {
38 static int Main (string[] args)
40 return TestDriver.RunTests (typeof (Tests), args);
43 public static int test_1_nullable_unbox ()
45 return Unbox<int?> (1).Value;
48 public static int test_1_nullable_unbox_null ()
50 return Unbox<int?> (null).HasValue ? 0 : 1;
53 public static int test_1_nullable_box ()
55 return (int) Box<int?> (1);
58 public static int test_1_nullable_box_null ()
60 return Box<int?> (null) == null ? 1 : 0;
63 public static int test_1_isinst_nullable ()
66 return (o is int?) ? 1 : 0;
69 public static int test_1_nullable_unbox_vtype ()
71 return Unbox<TestStruct?> (new TestStruct (1, 2)).Value.i;
74 public static int test_1_nullable_unbox_null_vtype ()
76 return Unbox<TestStruct?> (null).HasValue ? 0 : 1;
79 public static int test_1_nullable_box_vtype ()
81 return ((TestStruct)(Box<TestStruct?> (new TestStruct (1, 2)))).i;
84 public static int test_1_nullable_box_null_vtype ()
86 return Box<TestStruct?> (null) == null ? 1 : 0;
89 public static int test_1_isinst_nullable_vtype ()
91 object o = new TestStruct (1, 2);
92 return (o is TestStruct?) ? 1 : 0;
95 public static int test_0_nullable_normal_unbox ()
100 // This uses unbox instead of unbox_any
109 public static void stelem_any<T> (T[] arr, T elem) {
113 public static T ldelem_any<T> (T[] arr) {
117 public static int test_1_ldelem_stelem_any_int () {
118 int[] arr = new int [3];
121 return ldelem_any (arr);
124 public static T return_ref<T> (ref T t) {
128 public static T ldelema_any<T> (T[] arr) {
129 return return_ref<T> (ref arr [0]);
132 public static int test_0_ldelema () {
133 string[] arr = new string [1];
137 if (ldelema_any <string> (arr) == "Hello")
143 public static T[,] newarr_multi<T> () {
147 public static int test_0_newarr_multi_dim () {
148 return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
156 public static int test_0_iface_call_null_bug_77442 () {
162 catch (NullReferenceException) {
169 public static int test_18_ldobj_stobj_generics () {
170 GenericClass<int> t = new GenericClass <int> ();
173 return t.ldobj_stobj (ref i, ref j) + i + j;
176 public static int test_5_ldelem_stelem_generics () {
177 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
179 TestStruct s = new TestStruct (5, 5);
180 return t.ldelem_stelem (s).i;
183 public static int test_0_constrained_vtype_box () {
184 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
186 return t.toString (new TestStruct ()) == "Tests+TestStruct" ? 0 : 1;
189 public static int test_0_constrained_vtype () {
190 GenericClass<int> t = new GenericClass<int> ();
192 return t.toString (1234) == "1234" ? 0 : 1;
195 public static int test_0_constrained_reftype () {
196 GenericClass<String> t = new GenericClass<String> ();
198 return t.toString ("1234") == "1234" ? 0 : 1;
201 public static int test_0_box_brtrue_optimizations () {
205 if (!IsNull<object>(null))
211 [Category ("!FULLAOT")]
212 public static int test_0_generic_get_value_optimization_int () {
213 int[] x = new int[] {100, 200};
215 if (GenericClass<int>.Z (x, 0) != 100)
218 if (GenericClass<int>.Z (x, 1) != 200)
224 public static int test_0_generic_get_value_optimization_vtype () {
225 TestStruct[] arr = new TestStruct[] { new TestStruct (100, 200), new TestStruct (300, 400) };
226 IEnumerator<TestStruct> enumerator = GenericClass<TestStruct>.Y (arr);
229 while (enumerator.MoveNext ()) {
230 s = enumerator.Current;
237 s = GenericClass<TestStruct>.Z (arr, 0);
238 if (s.i != 100 || s.j != 200)
241 s = GenericClass<TestStruct>.Z (arr, 1);
242 if (s.i != 300 || s.j != 400)
248 public static int test_0_nullable_ldflda () {
249 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
252 public struct GenericStruct<T> {
255 public GenericStruct (T t) {
260 public class GenericClass<T> {
263 public GenericClass (T t) {
267 public GenericClass () {
270 public T ldobj_stobj (ref T t1, ref T t2) {
277 public T ldelem_stelem (T t) {
278 T[] arr = new T [10];
284 public String toString (T t) {
285 return t.ToString ();
288 public static IEnumerator<T> Y (IEnumerable <T> x)
290 return x.GetEnumerator ();
293 public static T Z (IList<T> x, int index)
298 protected static T NullB = default(T);
299 private static Nullable<bool> _BIsA = null;
300 public static bool BIsAClazz {
308 public class MRO : MarshalByRefObject {
309 public GenericStruct<int> struct_field;
310 public GenericClass<int> class_field;
313 public static int test_0_ldfld_stfld_mro () {
315 GenericStruct<int> s = new GenericStruct<int> (5);
316 // This generates stfld
319 // This generates ldflda
320 if (m.struct_field.t != 5)
323 // This generates ldfld
324 GenericStruct<int> s2 = m.struct_field;
328 if (m.struct_field.t != 5)
331 m.class_field = new GenericClass<int> (5);
332 if (m.class_field.t != 5)
339 [Category ("!FULLAOT")]
340 public static int test_0_generic_virtual_call_on_vtype_unbox () {
341 object o = new Object ();
342 IFoo h = new Handler(o);
344 if (h.Bar<object> () != o)
350 public static int test_0_box_brtrue_opt () {
351 Foo<int> f = new Foo<int> (5);
358 public static int test_0_box_brtrue_opt_regress_81102 () {
359 if (new Foo<int>(5).ToString () == "null")
369 public static int test_0_ldloca_initobj_opt () {
370 if (new Foo<S> (new S ()).get_default ().i != 0)
372 if (new Foo<object> (null).get_default () != null)
377 public static int test_0_variance_reflection () {
378 // covariance on IEnumerator
379 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
381 // covariance on IEnumerator and covariance on arrays
382 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
384 // covariance and implemented interfaces
385 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
388 // contravariance on IComparer
389 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
391 // contravariance on IComparer, contravariance on arrays
392 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
394 // contravariance and interface inheritance
395 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
400 public static int test_0_ldvirtftn_generic_method () {
401 new Tests ().ldvirtftn<string> ();
403 return the_type == typeof (string) ? 0 : 1;
406 public static int test_0_throw_dead_this () {
407 new Foo<string> ("").throw_dead_this ();
413 public static int test_0_inline_infinite_polymorphic_recursion () {
419 private static void f<T>(int i) {
420 if(i==42) f<S<T>>(i);
423 // This cannot be made to work with full-aot, since there it is impossible to
424 // statically determine that Foo<string>.Bar <int> is needed, the code only
425 // references IFoo.Bar<int>
426 [Category ("!FULLAOT")]
427 public static int test_0_generic_virtual_on_interfaces () {
428 Foo<string>.count1 = 0;
429 Foo<string>.count2 = 0;
430 Foo<string>.count3 = 0;
432 IFoo f = new Foo<string> ("");
433 for (int i = 0; i < 1000; ++i) {
439 if (Foo<string>.count1 != 1000)
441 if (Foo<string>.count2 != 1000)
443 if (Foo<string>.count3 != 1000)
446 VirtualInterfaceCallFromGenericMethod<long> (f);
451 public static int test_0_generic_virtual_on_interfaces_ref () {
452 Foo<string>.count1 = 0;
453 Foo<string>.count2 = 0;
454 Foo<string>.count3 = 0;
455 Foo<string>.count4 = 0;
457 IFoo f = new Foo<string> ("");
458 for (int i = 0; i < 1000; ++i) {
464 if (Foo<string>.count2 != 1000)
466 if (Foo<string>.count3 != 1000)
468 if (Foo<string>.count4 != 1000)
475 [Category ("!FULLAOT")]
476 public static int test_2_cprop_bug () {
479 var cmp = System.Collections.Generic.Comparer<int>.Default ;
480 if (cmp.Compare (a, 0) > 0)
482 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
486 enum MyEnumUlong : ulong {
490 public static int test_0_regress_550964_constrained_enum_long () {
491 MyEnumUlong a = MyEnumUlong.Value_2;
492 MyEnumUlong b = MyEnumUlong.Value_2;
494 return Pan (a, b) ? 0 : 1;
497 static bool Pan<T> (T a, T b)
502 public class XElement {
503 public string Value {
508 public static int test_0_fullaot_linq () {
509 var allWords = new XElement [] { new XElement { Value = "one" } };
510 var filteredWords = allWords.Where(kw => kw.Value.StartsWith("T"));
511 return filteredWords.Count ();
514 public static int test_0_fullaot_comparer_t () {
515 var l = new SortedList <TimeSpan, int> ();
519 public static int test_0_fullaot_comparer_t_2 () {
520 var l = new Dictionary <TimeSpan, int> ();
524 static void enumerate<T> (IEnumerable<T> arr) {
525 foreach (var o in arr)
527 int c = ((ICollection<T>)arr).Count;
530 /* Test that treating arrays as generic collections works with full-aot */
531 public static int test_0_fullaot_array_wrappers () {
532 Tests[] arr = new Tests [10];
533 enumerate<Tests> (arr);
537 static int cctor_count = 0;
539 public abstract class Beta<TChanged>
547 public class Gamma<T> : Beta<T>
555 public static int test_2_generic_class_init_gshared_ctor () {
562 static int cctor_count2 = 0;
564 class ServiceController<T> {
565 static ServiceController () {
569 public ServiceController () {
573 static ServiceController<T> Create<T>() {
574 return new ServiceController<T>();
578 public static int test_2_generic_class_init_gshared_ctor_from_gshared () {
585 public static Type get_type<T> () {
589 public static int test_0_gshared_delegate_rgctx () {
590 Func<Type> t = new Func<Type> (get_type<string>);
592 if (t () == typeof (string))
598 // Creating a delegate from a generic method from gshared code
599 public static int test_0_gshared_delegate_from_gshared () {
600 if (gshared_delegate_from_gshared <object> () != 0)
602 if (gshared_delegate_from_gshared <string> () != 0)
607 public static int gshared_delegate_from_gshared <T> () {
608 Func<Type> t = new Func<Type> (get_type<T>);
610 return t () == typeof (T) ? 0 : 1;
613 public static int test_0_marshalbyref_call_from_gshared_virt_elim () {
614 /* Calling a virtual method from gshared code which is changed to a nonvirt call */
615 Class1<object> o = new Class1<object> ();
616 o.Do (new Class2<object> ());
620 class Pair<TKey, TValue> {
621 public static KeyValuePair<TKey, TValue> make_pair (TKey key, TValue value)
623 return new KeyValuePair<TKey, TValue> (key, value);
626 public delegate TRet Transform<TRet> (TKey key, TValue value);
629 public static int test_0_bug_620864 () {
630 var d = new Pair<string, Type>.Transform<KeyValuePair<string, Type>> (Pair<string, Type>.make_pair);
632 var p = d ("FOO", typeof (int));
633 if (p.Key != "FOO" || p.Value != typeof (int))
640 struct RecStruct<T> {
641 public void foo (RecStruct<RecStruct<T>> baz) {
645 public static int test_0_infinite_generic_recursion () {
646 // Check that the AOT compile can deal with infinite generic recursion through
656 bool IsNull2 <T> (object value) where T : struct {
657 T? item = (T?) value;
665 public static int test_0_full_aot_nullable_unbox_from_gshared_code () {
666 if (!new Tests ().IsNull2<FooStruct> (null))
668 if (new Tests ().IsNull2<FooStruct> (new FooStruct ()))
673 public static int test_0_partial_sharing () {
674 if (PartialShared1 (new List<string> (), 1) != typeof (string))
676 if (PartialShared1 (new List<Tests> (), 1) != typeof (Tests))
678 if (PartialShared2 (new List<string> (), 1) != typeof (int))
680 if (PartialShared2 (new List<Tests> (), 1) != typeof (int))
685 public static int test_6_partial_sharing_linq () {
686 var messages = new List<Message> ();
688 messages.Add (new Message () { MessageID = 5 });
689 messages.Add (new Message () { MessageID = 6 });
691 return messages.Max(i => i.MessageID);
694 public static int test_0_partial_shared_method_in_nonshared_class () {
695 var c = new Class1<double> ();
696 return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
700 public int MessageID {
705 public static Type PartialShared1<T, K> (List<T> list, K k) {
709 public static Type PartialShared2<T, K> (List<T> list, K k) {
713 public class Class1<T> {
714 public virtual void Do (Class2<T> t) {
718 public virtual object Foo<U> (T t) {
719 return new Class1<U> ();
723 public interface IFace1<T> {
727 public class Class2<T> : MarshalByRefObject, IFace1<T> {
734 public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
738 public static Type the_type;
740 public void ldvirtftn<T> () {
741 Foo <T> binding = new Foo <T> (default (T));
743 binding.GenericEvent += event_handler;
747 public virtual void event_handler<T> (Foo<T> sender) {
748 the_type = typeof (T);
751 public interface IFoo {
756 public class Foo<T1> : IFoo
763 public override string ToString()
765 return Bar(m_t1 == null ? "null" : "null");
768 public String Bar (String s) {
772 public int this [T1 key] {
775 throw new ArgumentNullException ("key");
779 public void throw_dead_this () {
781 new SomeClass().ThrowAnException();
787 public T1 get_default () {
793 public delegate void GenericEventHandler (Foo<T1> sender);
795 public event GenericEventHandler GenericEvent;
797 public void fire () {
801 public static int count1, count2, count3, count4;
803 public void NonGeneric () {
807 public object Bar <T> () {
808 if (typeof (T) == typeof (int))
810 else if (typeof (T) == typeof (string))
812 else if (typeof (T) == typeof (object))
818 public class SomeClass {
819 public void ThrowAnException() {
820 throw new Exception ("Something went wrong");
824 struct Handler : IFoo {
827 public Handler(object o) {
831 public void NonGeneric () {
834 public object Bar<T>() {
839 static bool IsNull<T> (T t)
847 static object Box<T> (T t)
852 static T Unbox <T> (object o) {
856 interface IDefaultRetriever
861 class DefaultRetriever : IDefaultRetriever
863 [MethodImpl(MethodImplOptions.Synchronized)]
864 public T GetDefault<T>()
870 [Category ("!FULLAOT")]
871 public static int test_0_regress_668095_synchronized_gshared () {
872 return DoSomething (new DefaultRetriever ());
875 static int DoSomething(IDefaultRetriever foo) {
876 int result = foo.GetDefault<int>();
883 public static int test_0_687865_isinst_with_cache_wrapper () {
884 object o = new object ();
885 if (o is Action<IEnumerable<Response>>)
902 public DocType Type {
908 public static int test_0_fullaot_sflda_cctor () {
909 List<Doc> documents = new List<Doc>();
910 documents.Add(new Doc { Name = "Doc1", Type = DocType.One } );
911 documents.Add(new Doc { Name = "Doc2", Type = DocType.Two } );
912 documents.Add(new Doc { Name = "Doc3", Type = DocType.Three } );
913 documents.Add(new Doc { Name = "Doc4", Type = DocType.One } );
914 documents.Add(new Doc { Name = "Doc5", Type = DocType.Two } );
915 documents.Add(new Doc { Name = "Doc6", Type = DocType.Three } );
916 documents.Add(new Doc { Name = "Doc7", Type = DocType.One } );
917 documents.Add(new Doc { Name = "Doc8", Type = DocType.Two } );
918 documents.Add(new Doc { Name = "Doc9", Type = DocType.Three } );
920 List<DocType> categories = documents.Select(d=>d.Type).Distinct().ToList<DocType>().OrderBy(d => d).ToList();
921 foreach(DocType cat in categories) {
922 List<Doc> catDocs = documents.Where(d => d.Type == cat).OrderBy(d => d.Name).ToList<Doc>();
929 static List<A> sources = new List<A>();
932 public static int test_0_fullaot_imt () {
936 int a = sources.Count;
937 var enumerator = sources.GetEnumerator() as IEnumerator<object>;
939 while (enumerator.MoveNext())
941 object o = enumerator.Current;
947 struct Record : Foo2<Record>.IRecord {
949 int Foo2<Record>.IRecord.DoSomething () {
954 class Foo2<T> where T : Foo2<T>.IRecord {
955 public interface IRecord {
959 public static int Extract (T[] t) {
960 return t[0].DoSomething ();
964 class Foo3<T> where T : IComparable {
965 public static int CompareTo (T[] t) {
966 // This is a constrained call to Enum.CompareTo ()
967 return t[0].CompareTo (t [0]);
971 public static int test_1_regress_constrained_iface_call_7571 () {
972 var r = new Record [10];
973 Foo2<Record>.Extract (r);
974 return Foo2<Record>.Extract (r);
977 enum ConstrainedEnum {
981 public static int test_0_regress_constrained_iface_call_enum () {
982 var r = new ConstrainedEnum [10];
983 return Foo3<ConstrainedEnum>.CompareTo (r);
986 public interface IFoo2 {
990 public struct Foo2 : IFoo2 {
991 public void MoveNext () {
995 public static Action Dingus (ref Foo2 f) {
996 return new Action (f.MoveNext);
999 public static int test_0_delegate_unbox_full_aot () {
1000 Foo2 foo = new Foo2 ();
1001 Dingus (ref foo) ();
1005 public static int test_0_arrays_ireadonly () {
1006 int[] arr = new int [10];
1007 for (int i = 0; i < 10; ++i)
1009 IReadOnlyList<int> a = (IReadOnlyList<int>)(object)arr;
1019 public static int test_0_volatile_read_write () {
1021 Volatile.Write (ref foo, "DEF");
1022 return Volatile.Read (ref foo) == "DEF" ? 0 : 1;
1025 // FIXME: Doesn't work with --regression as Interlocked.Add(ref long) is only implemented as an intrinsic
1027 public static async Task<T> FooAsync<T> (int i, int j) {
1028 Task<int> t = new Task<int> (delegate () { Console.WriteLine ("HIT!"); return 0; });
1029 var response = await t;
1033 public static int test_0_fullaot_generic_async () {
1034 Task<string> t = FooAsync<string> (1, 2);
1035 t.RunSynchronously ();