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 class MRO<T> : MarshalByRefObject {
316 public T stfld_ldfld (T t) {
323 public static int test_0_ldfld_stfld_mro () {
325 GenericStruct<int> s = new GenericStruct<int> (5);
326 // This generates stfld
329 // This generates ldflda
330 if (m.struct_field.t != 5)
333 // This generates ldfld
334 GenericStruct<int> s2 = m.struct_field;
338 if (m.struct_field.t != 5)
341 m.class_field = new GenericClass<int> (5);
342 if (m.class_field.t != 5)
346 var m2 = new MRO<string> ();
347 if (m2.stfld_ldfld ("A") != "A")
354 [Category ("!FULLAOT")]
355 public static int test_0_generic_virtual_call_on_vtype_unbox () {
356 object o = new Object ();
357 IFoo h = new Handler(o);
359 if (h.Bar<object> () != o)
365 public static int test_0_box_brtrue_opt () {
366 Foo<int> f = new Foo<int> (5);
373 public static int test_0_box_brtrue_opt_regress_81102 () {
374 if (new Foo<int>(5).ToString () == "null")
384 public static int test_0_ldloca_initobj_opt () {
385 if (new Foo<S> (new S ()).get_default ().i != 0)
387 if (new Foo<object> (null).get_default () != null)
392 public static int test_0_variance_reflection () {
393 // covariance on IEnumerator
394 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
396 // covariance on IEnumerator and covariance on arrays
397 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
399 // covariance and implemented interfaces
400 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
403 // contravariance on IComparer
404 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
406 // contravariance on IComparer, contravariance on arrays
407 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
409 // contravariance and interface inheritance
410 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
415 public static int test_0_ldvirtftn_generic_method () {
416 new Tests ().ldvirtftn<string> ();
418 return the_type == typeof (string) ? 0 : 1;
421 public static int test_0_throw_dead_this () {
422 new Foo<string> ("").throw_dead_this ();
428 public static int test_0_inline_infinite_polymorphic_recursion () {
434 private static void f<T>(int i) {
435 if(i==42) f<S<T>>(i);
438 // This cannot be made to work with full-aot, since there it is impossible to
439 // statically determine that Foo<string>.Bar <int> is needed, the code only
440 // references IFoo.Bar<int>
441 [Category ("!FULLAOT")]
442 public static int test_0_generic_virtual_on_interfaces () {
443 Foo<string>.count1 = 0;
444 Foo<string>.count2 = 0;
445 Foo<string>.count3 = 0;
447 IFoo f = new Foo<string> ("");
448 for (int i = 0; i < 1000; ++i) {
454 if (Foo<string>.count1 != 1000)
456 if (Foo<string>.count2 != 1000)
458 if (Foo<string>.count3 != 1000)
461 VirtualInterfaceCallFromGenericMethod<long> (f);
466 public static int test_0_generic_virtual_on_interfaces_ref () {
467 Foo<string>.count1 = 0;
468 Foo<string>.count2 = 0;
469 Foo<string>.count3 = 0;
470 Foo<string>.count4 = 0;
472 IFoo f = new Foo<string> ("");
473 for (int i = 0; i < 1000; ++i) {
479 if (Foo<string>.count2 != 1000)
481 if (Foo<string>.count3 != 1000)
483 if (Foo<string>.count4 != 1000)
490 [Category ("!FULLAOT")]
491 public static int test_2_cprop_bug () {
494 var cmp = System.Collections.Generic.Comparer<int>.Default ;
495 if (cmp.Compare (a, 0) > 0)
497 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
501 enum MyEnumUlong : ulong {
505 public static int test_0_regress_550964_constrained_enum_long () {
506 MyEnumUlong a = MyEnumUlong.Value_2;
507 MyEnumUlong b = MyEnumUlong.Value_2;
509 return Pan (a, b) ? 0 : 1;
512 static bool Pan<T> (T a, T b)
517 public class XElement {
518 public string Value {
523 public static int test_0_fullaot_linq () {
524 var allWords = new XElement [] { new XElement { Value = "one" } };
525 var filteredWords = allWords.Where(kw => kw.Value.StartsWith("T"));
526 return filteredWords.Count ();
529 public static int test_0_fullaot_comparer_t () {
530 var l = new SortedList <TimeSpan, int> ();
534 public static int test_0_fullaot_comparer_t_2 () {
535 var l = new Dictionary <TimeSpan, int> ();
539 static void enumerate<T> (IEnumerable<T> arr) {
540 foreach (var o in arr)
542 int c = ((ICollection<T>)arr).Count;
545 /* Test that treating arrays as generic collections works with full-aot */
546 public static int test_0_fullaot_array_wrappers () {
547 Tests[] arr = new Tests [10];
548 enumerate<Tests> (arr);
552 static int cctor_count = 0;
554 public abstract class Beta<TChanged>
562 public class Gamma<T> : Beta<T>
570 public static int test_2_generic_class_init_gshared_ctor () {
577 static int cctor_count2 = 0;
579 class ServiceController<T> {
580 static ServiceController () {
584 public ServiceController () {
588 static ServiceController<T> Create<T>() {
589 return new ServiceController<T>();
593 public static int test_2_generic_class_init_gshared_ctor_from_gshared () {
600 public static Type get_type<T> () {
604 public static int test_0_gshared_delegate_rgctx () {
605 Func<Type> t = new Func<Type> (get_type<string>);
607 if (t () == typeof (string))
613 // Creating a delegate from a generic method from gshared code
614 public static int test_0_gshared_delegate_from_gshared () {
615 if (gshared_delegate_from_gshared <object> () != 0)
617 if (gshared_delegate_from_gshared <string> () != 0)
622 public static int gshared_delegate_from_gshared <T> () {
623 Func<Type> t = new Func<Type> (get_type<T>);
625 return t () == typeof (T) ? 0 : 1;
628 public static int test_0_marshalbyref_call_from_gshared_virt_elim () {
629 /* Calling a virtual method from gshared code which is changed to a nonvirt call */
630 Class1<object> o = new Class1<object> ();
631 o.Do (new Class2<object> ());
635 class Pair<TKey, TValue> {
636 public static KeyValuePair<TKey, TValue> make_pair (TKey key, TValue value)
638 return new KeyValuePair<TKey, TValue> (key, value);
641 public delegate TRet Transform<TRet> (TKey key, TValue value);
644 public static int test_0_bug_620864 () {
645 var d = new Pair<string, Type>.Transform<KeyValuePair<string, Type>> (Pair<string, Type>.make_pair);
647 var p = d ("FOO", typeof (int));
648 if (p.Key != "FOO" || p.Value != typeof (int))
655 struct RecStruct<T> {
656 public void foo (RecStruct<RecStruct<T>> baz) {
660 public static int test_0_infinite_generic_recursion () {
661 // Check that the AOT compile can deal with infinite generic recursion through
671 bool IsNull2 <T> (object value) where T : struct {
672 T? item = (T?) value;
680 public static int test_0_full_aot_nullable_unbox_from_gshared_code () {
681 if (!new Tests ().IsNull2<FooStruct> (null))
683 if (new Tests ().IsNull2<FooStruct> (new FooStruct ()))
688 public static int test_0_partial_sharing () {
689 if (PartialShared1 (new List<string> (), 1) != typeof (string))
691 if (PartialShared1 (new List<Tests> (), 1) != typeof (Tests))
693 if (PartialShared2 (new List<string> (), 1) != typeof (int))
695 if (PartialShared2 (new List<Tests> (), 1) != typeof (int))
700 public static int test_6_partial_sharing_linq () {
701 var messages = new List<Message> ();
703 messages.Add (new Message () { MessageID = 5 });
704 messages.Add (new Message () { MessageID = 6 });
706 return messages.Max(i => i.MessageID);
709 public static int test_0_partial_shared_method_in_nonshared_class () {
710 var c = new Class1<double> ();
711 return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
715 public int MessageID {
720 public static Type PartialShared1<T, K> (List<T> list, K k) {
724 public static Type PartialShared2<T, K> (List<T> list, K k) {
728 public class Class1<T> {
729 public virtual void Do (Class2<T> t) {
733 public virtual object Foo<U> (T t) {
734 return new Class1<U> ();
738 public interface IFace1<T> {
742 public class Class2<T> : MarshalByRefObject, IFace1<T> {
749 public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
753 public static Type the_type;
755 public void ldvirtftn<T> () {
756 Foo <T> binding = new Foo <T> (default (T));
758 binding.GenericEvent += event_handler;
762 public virtual void event_handler<T> (Foo<T> sender) {
763 the_type = typeof (T);
766 public interface IFoo {
771 public class Foo<T1> : IFoo
778 public override string ToString()
780 return Bar(m_t1 == null ? "null" : "null");
783 public String Bar (String s) {
787 public int this [T1 key] {
790 throw new ArgumentNullException ("key");
794 public void throw_dead_this () {
796 new SomeClass().ThrowAnException();
802 public T1 get_default () {
808 public delegate void GenericEventHandler (Foo<T1> sender);
810 public event GenericEventHandler GenericEvent;
812 public void fire () {
816 public static int count1, count2, count3, count4;
818 public void NonGeneric () {
822 public object Bar <T> () {
823 if (typeof (T) == typeof (int))
825 else if (typeof (T) == typeof (string))
827 else if (typeof (T) == typeof (object))
833 public class SomeClass {
834 public void ThrowAnException() {
835 throw new Exception ("Something went wrong");
839 struct Handler : IFoo {
842 public Handler(object o) {
846 public void NonGeneric () {
849 public object Bar<T>() {
854 static bool IsNull<T> (T t)
862 static object Box<T> (T t)
867 static T Unbox <T> (object o) {
871 interface IDefaultRetriever
876 class DefaultRetriever : IDefaultRetriever
878 [MethodImpl(MethodImplOptions.Synchronized)]
879 public T GetDefault<T>()
885 [Category ("!FULLAOT")]
886 public static int test_0_regress_668095_synchronized_gshared () {
887 return DoSomething (new DefaultRetriever ());
890 static int DoSomething(IDefaultRetriever foo) {
891 int result = foo.GetDefault<int>();
898 public static int test_0_687865_isinst_with_cache_wrapper () {
899 object o = new object ();
900 if (o is Action<IEnumerable<Response>>)
917 public DocType Type {
923 public static int test_0_fullaot_sflda_cctor () {
924 List<Doc> documents = new List<Doc>();
925 documents.Add(new Doc { Name = "Doc1", Type = DocType.One } );
926 documents.Add(new Doc { Name = "Doc2", Type = DocType.Two } );
927 documents.Add(new Doc { Name = "Doc3", Type = DocType.Three } );
928 documents.Add(new Doc { Name = "Doc4", Type = DocType.One } );
929 documents.Add(new Doc { Name = "Doc5", Type = DocType.Two } );
930 documents.Add(new Doc { Name = "Doc6", Type = DocType.Three } );
931 documents.Add(new Doc { Name = "Doc7", Type = DocType.One } );
932 documents.Add(new Doc { Name = "Doc8", Type = DocType.Two } );
933 documents.Add(new Doc { Name = "Doc9", Type = DocType.Three } );
935 List<DocType> categories = documents.Select(d=>d.Type).Distinct().ToList<DocType>().OrderBy(d => d).ToList();
936 foreach(DocType cat in categories) {
937 List<Doc> catDocs = documents.Where(d => d.Type == cat).OrderBy(d => d.Name).ToList<Doc>();
944 static List<A> sources = new List<A>();
947 public static int test_0_fullaot_imt () {
951 int a = sources.Count;
952 var enumerator = sources.GetEnumerator() as IEnumerator<object>;
954 while (enumerator.MoveNext())
956 object o = enumerator.Current;
962 struct Record : Foo2<Record>.IRecord {
964 int Foo2<Record>.IRecord.DoSomething () {
969 class Foo2<T> where T : Foo2<T>.IRecord {
970 public interface IRecord {
974 public static int Extract (T[] t) {
975 return t[0].DoSomething ();
979 class Foo3<T> where T : IComparable {
980 public static int CompareTo (T[] t) {
981 // This is a constrained call to Enum.CompareTo ()
982 return t[0].CompareTo (t [0]);
986 public static int test_1_regress_constrained_iface_call_7571 () {
987 var r = new Record [10];
988 Foo2<Record>.Extract (r);
989 return Foo2<Record>.Extract (r);
992 enum ConstrainedEnum {
996 public static int test_0_regress_constrained_iface_call_enum () {
997 var r = new ConstrainedEnum [10];
998 return Foo3<ConstrainedEnum>.CompareTo (r);
1001 public interface IFoo2 {
1005 public struct Foo2 : IFoo2 {
1006 public void MoveNext () {
1010 public static Action Dingus (ref Foo2 f) {
1011 return new Action (f.MoveNext);
1014 public static int test_0_delegate_unbox_full_aot () {
1015 Foo2 foo = new Foo2 ();
1016 Dingus (ref foo) ();
1020 public static int test_0_arrays_ireadonly () {
1021 int[] arr = new int [10];
1022 for (int i = 0; i < 10; ++i)
1024 IReadOnlyList<int> a = (IReadOnlyList<int>)(object)arr;
1034 public static int test_0_volatile_read_write () {
1036 Volatile.Write (ref foo, "DEF");
1037 return Volatile.Read (ref foo) == "DEF" ? 0 : 1;
1040 // FIXME: Doesn't work with --regression as Interlocked.Add(ref long) is only implemented as an intrinsic
1042 public static async Task<T> FooAsync<T> (int i, int j) {
1043 Task<int> t = new Task<int> (delegate () { Console.WriteLine ("HIT!"); return 0; });
1044 var response = await t;
1048 public static int test_0_fullaot_generic_async () {
1049 Task<string> t = FooAsync<string> (1, 2);
1050 t.RunSynchronously ();