Merge pull request #3140 from esdrubal/syscall_details
[mono-project.git] / mono / mini / generics.cs
blob1284cb292ae733f3f58dc8b78c5a07d72d31e488
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Runtime.CompilerServices;
5 using System.Threading;
6 using System.Threading.Tasks;
8 #if __MOBILE__
9 class GenericsTests
10 #else
11 class Tests
12 #endif
14 struct TestStruct {
15 public int i;
16 public int j;
18 public TestStruct (int i, int j) {
19 this.i = i;
20 this.j = j;
24 #if !__MOBILE__
25 class Enumerator <T> : MyIEnumerator <T> {
26 T MyIEnumerator<T>.Current {
27 get {
28 return default(T);
32 bool MyIEnumerator<T>.MoveNext () {
33 return true;
37 class Comparer <T> : IComparer <T> {
38 bool IComparer<T>.Compare (T x, T y) {
39 return true;
42 #endif
44 #if !__MOBILE__
45 static int Main (string[] args)
47 return TestDriver.RunTests (typeof (Tests), args);
49 #endif
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 ()
73 object o = 1;
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 ()
105 int? i = 5;
107 object o = i;
108 // This uses unbox instead of unbox_any
109 int? j = (int?)o;
111 if (j != 5)
112 return 1;
114 return 0;
117 public static void stelem_any<T> (T[] arr, T elem) {
118 arr [0] = elem;
121 public static T ldelem_any<T> (T[] arr) {
122 return arr [0];
125 public static int test_1_ldelem_stelem_any_int () {
126 int[] arr = new int [3];
127 stelem_any (arr, 1);
129 return ldelem_any (arr);
132 public static T return_ref<T> (ref T t) {
133 return 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];
143 arr [0] = "Hello";
145 if (ldelema_any <string> (arr) == "Hello")
146 return 0;
147 else
148 return 1;
151 public static T[,] newarr_multi<T> () {
152 return new T [1, 1];
155 public static int test_0_newarr_multi_dim () {
156 return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
159 interface ITest
161 void Foo<T> ();
164 public static int test_0_iface_call_null_bug_77442 () {
165 ITest test = null;
167 try {
168 test.Foo<int> ();
170 catch (NullReferenceException) {
171 return 0;
174 return 1;
177 public static int test_18_ldobj_stobj_generics () {
178 GenericClass<int> t = new GenericClass <int> ();
179 int i = 5;
180 int j = 6;
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> ();
194 #if __MOBILE__
195 return t.toString (new TestStruct ()) == "GenericsTests+TestStruct" ? 0 : 1;
196 #else
197 return t.toString (new TestStruct ()) == "Tests+TestStruct" ? 0 : 1;
198 #endif
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 () {
214 if (IsNull<int>(5))
215 return 1;
217 if (!IsNull<object>(null))
218 return 1;
220 return 0;
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)
228 return 2;
230 if (GenericClass<int>.Z (x, 1) != 200)
231 return 3;
233 return 0;
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);
239 TestStruct s;
240 int sum = 0;
241 while (enumerator.MoveNext ()) {
242 s = enumerator.Current;
243 sum += s.i + s.j;
246 if (sum != 1000)
247 return 1;
249 s = GenericClass<TestStruct>.Z (arr, 0);
250 if (s.i != 100 || s.j != 200)
251 return 2;
253 s = GenericClass<TestStruct>.Z (arr, 1);
254 if (s.i != 300 || s.j != 400)
255 return 3;
257 return 0;
260 public static int test_0_nullable_ldflda () {
261 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
264 public struct GenericStruct<T> {
265 public T t;
267 public GenericStruct (T t) {
268 this.t = t;
272 public class GenericClass<T> {
273 public T t;
275 public GenericClass (T t) {
276 this.t = t;
279 public GenericClass () {
282 public T ldobj_stobj (ref T t1, ref T t2) {
283 t1 = t2;
284 T t = t1;
286 return t;
289 public T ldelem_stelem (T t) {
290 T[] arr = new T [10];
291 arr [0] = t;
293 return arr [0];
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)
307 return x [index];
310 protected static T NullB = default(T);
311 private static Nullable<bool> _BIsA = null;
312 public static bool BIsAClazz {
313 get {
314 _BIsA = false;
315 return _BIsA.Value;
320 public class MRO : MarshalByRefObject {
321 public GenericStruct<int> struct_field;
322 public GenericClass<int> class_field;
325 public class MRO<T> : MarshalByRefObject {
326 public T gen_field;
328 public T stfld_ldfld (T t) {
329 var m = this;
330 m.gen_field = t;
331 return m.gen_field;
335 public static int test_0_ldfld_stfld_mro () {
336 MRO m = new MRO ();
337 GenericStruct<int> s = new GenericStruct<int> (5);
338 // This generates stfld
339 m.struct_field = s;
341 // This generates ldflda
342 if (m.struct_field.t != 5)
343 return 1;
345 // This generates ldfld
346 GenericStruct<int> s2 = m.struct_field;
347 if (s2.t != 5)
348 return 2;
350 if (m.struct_field.t != 5)
351 return 3;
353 m.class_field = new GenericClass<int> (5);
354 if (m.class_field.t != 5)
355 return 4;
357 // gshared
358 var m2 = new MRO<string> ();
359 if (m2.stfld_ldfld ("A") != "A")
360 return 5;
362 return 0;
365 // FIXME:
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)
372 return 1;
373 else
374 return 0;
377 public static int test_0_box_brtrue_opt () {
378 Foo<int> f = new Foo<int> (5);
380 f [123] = 5;
382 return 0;
385 public static int test_0_box_brtrue_opt_regress_81102 () {
386 if (new Foo<int>(5).ToString () == "null")
387 return 0;
388 else
389 return 1;
392 struct S {
393 public int i;
396 public static int test_0_ldloca_initobj_opt () {
397 if (new Foo<S> (new S ()).get_default ().i != 0)
398 return 1;
399 if (new Foo<object> (null).get_default () != null)
400 return 2;
401 return 0;
404 #if !__MOBILE__
405 public static int test_0_variance_reflection () {
406 // covariance on IEnumerator
407 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
408 return 1;
409 // covariance on IEnumerator and covariance on arrays
410 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
411 return 2;
412 // covariance and implemented interfaces
413 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
414 return 3;
416 // contravariance on IComparer
417 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
418 return 4;
419 // contravariance on IComparer, contravariance on arrays
420 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
421 return 5;
422 // contravariance and interface inheritance
423 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
424 return 6;
425 return 0;
427 #endif
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 ();
437 return 0;
440 struct S<T> {}
442 public static int test_0_inline_infinite_polymorphic_recursion () {
443 f<int>(0);
445 return 0;
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) {
463 f.Bar <int> ();
464 f.Bar <string> ();
465 f.NonGeneric ();
468 if (Foo<string>.count1 != 1000)
469 return 1;
470 if (Foo<string>.count2 != 1000)
471 return 2;
472 if (Foo<string>.count3 != 1000)
473 return 3;
475 VirtualInterfaceCallFromGenericMethod<long> (f);
477 return 0;
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) {
488 f.Bar <string> ();
489 f.Bar <object> ();
490 f.NonGeneric ();
493 if (Foo<string>.count2 != 1000)
494 return 2;
495 if (Foo<string>.count3 != 1000)
496 return 3;
497 if (Foo<string>.count4 != 1000)
498 return 4;
500 return 0;
503 //repro for #505375
504 [Category ("!FULLAOT")]
505 public static int test_2_cprop_bug () {
506 int idx = 0;
507 int a = 1;
508 var cmp = System.Collections.Generic.Comparer<int>.Default ;
509 if (cmp.Compare (a, 0) > 0)
510 a = 0;
511 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
512 return idx;
515 enum MyEnumUlong : ulong {
516 Value_2 = 2
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)
528 return a.Equals (b);
531 public class XElement {
532 public string Value {
533 get; set;
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> ();
545 return l.Count;
548 public static int test_0_fullaot_comparer_t_2 () {
549 var l = new Dictionary <TimeSpan, int> ();
550 return l.Count;
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);
563 return 0;
566 static int cctor_count = 0;
568 public abstract class Beta<TChanged>
570 static Beta()
572 cctor_count ++;
576 public class Gamma<T> : Beta<T>
578 static Gamma()
583 // #519336
584 public static int test_2_generic_class_init_gshared_ctor () {
585 new Gamma<object>();
586 new Gamma<string>();
588 return cctor_count;
591 static int cctor_count2 = 0;
593 class ServiceController<T> {
594 static ServiceController () {
595 cctor_count2 ++;
598 public ServiceController () {
602 static ServiceController<T> Create<T>() {
603 return new ServiceController<T>();
606 // #631409
607 public static int test_2_generic_class_init_gshared_ctor_from_gshared () {
608 Create<object> ();
609 Create<string> ();
611 return cctor_count2;
614 public static Type get_type<T> () {
615 return typeof (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))
622 return 0;
623 else
624 return 1;
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)
630 return 1;
631 if (gshared_delegate_from_gshared <string> () != 0)
632 return 2;
633 return 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> ());
646 return 0;
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))
663 return 1;
665 return 0;
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
676 // parameter types
677 RecStruct<int> bla;
679 return 0;
682 struct FooStruct {
685 bool IsNull2 <T> (object value) where T : struct {
686 T? item = (T?) value;
688 if (item.HasValue)
689 return false;
691 return true;
694 public static int test_0_full_aot_nullable_unbox_from_gshared_code () {
695 if (!new GenericsTests ().IsNull2<FooStruct> (null))
696 return 1;
697 if (new GenericsTests ().IsNull2<FooStruct> (new FooStruct ()))
698 return 2;
699 return 0;
702 public static int test_0_partial_sharing () {
703 if (PartialShared1 (new List<string> (), 1) != typeof (string))
704 return 1;
705 if (PartialShared1 (new List<GenericsTests> (), 1) != typeof (GenericsTests))
706 return 2;
707 if (PartialShared2 (new List<string> (), 1) != typeof (int))
708 return 3;
709 if (PartialShared2 (new List<GenericsTests> (), 1) != typeof (int))
710 return 4;
711 return 0;
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;
729 class Message {
730 public int MessageID {
731 get; set;
735 public static Type PartialShared1<T, K> (List<T> list, K k) {
736 return typeof (T);
739 public static Type PartialShared2<T, K> (List<T> list, K k) {
740 return typeof (K);
743 public class Class1<T> {
744 public virtual void Do (Class2<T> t) {
745 t.Foo ();
748 public virtual object Foo<U> (T t) {
749 return new Class1<U> ();
753 public interface IFace1<T> {
754 void Foo ();
757 public class Class2<T> : MarshalByRefObject, IFace1<T> {
758 public void Foo () {
764 public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
765 f.Bar <T> ();
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;
774 binding.fire ();
777 public virtual void event_handler<T> (Foo<T> sender) {
778 the_type = typeof (T);
781 public interface IFoo {
782 void NonGeneric ();
783 object Bar<T>();
786 public class Foo<T1> : IFoo
788 public Foo(T1 t1)
790 m_t1 = t1;
793 public override string ToString()
795 return Bar(m_t1 == null ? "null" : "null");
798 public String Bar (String s) {
799 return s;
802 public int this [T1 key] {
803 set {
804 if (key == null)
805 throw new ArgumentNullException ("key");
809 public void throw_dead_this () {
810 try {
811 new SomeClass().ThrowAnException();
813 catch {
817 public T1 get_default () {
818 return default (T1);
821 readonly T1 m_t1;
823 public delegate void GenericEventHandler (Foo<T1> sender);
825 public event GenericEventHandler GenericEvent;
827 public void fire () {
828 GenericEvent (this);
831 public static int count1, count2, count3, count4;
833 public void NonGeneric () {
834 count3 ++;
837 public object Bar <T> () {
838 if (typeof (T) == typeof (int))
839 count1 ++;
840 else if (typeof (T) == typeof (string))
841 count2 ++;
842 else if (typeof (T) == typeof (object))
843 count4 ++;
844 return null;
848 public class SomeClass {
849 public void ThrowAnException() {
850 throw new Exception ("Something went wrong");
854 struct Handler : IFoo {
855 object o;
857 public Handler(object o) {
858 this.o = o;
861 public void NonGeneric () {
864 public object Bar<T>() {
865 return o;
869 static bool IsNull<T> (T t)
871 if (t == null)
872 return true;
873 else
874 return false;
877 static object Box<T> (T t)
879 return t;
882 static T Unbox <T> (object o) {
883 return (T) o;
886 interface IDefaultRetriever
888 T GetDefault<T>();
891 class DefaultRetriever : IDefaultRetriever
893 [MethodImpl(MethodImplOptions.Synchronized)]
894 public T GetDefault<T>()
896 return default(T);
900 [Category ("!FULLAOT")]
901 public static int test_0_regress_668095_synchronized_gshared () {
902 return DoSomething (new DefaultRetriever ());
905 static int DoSomething(IDefaultRetriever foo) {
906 int result = foo.GetDefault<int>();
907 return result;
910 class SyncClass<T> {
911 [MethodImpl(MethodImplOptions.Synchronized)]
912 public Type getInstance() {
913 return typeof (T);
917 [Category ("GSHAREDVT")]
918 static int test_0_synchronized_gshared () {
919 var c = new SyncClass<string> ();
920 if (c.getInstance () != typeof (string))
921 return 1;
922 return 0;
925 class Response {
928 public static int test_0_687865_isinst_with_cache_wrapper () {
929 object o = new object ();
930 if (o is Action<IEnumerable<Response>>)
931 return 1;
932 else
933 return 0;
936 enum DocType {
937 One,
938 Two,
939 Three
942 class Doc {
943 public string Name {
944 get; set;
947 public DocType Type {
948 get; set;
952 // #2155
953 [Category ("GSHAREDVT")]
954 public static int test_0_fullaot_sflda_cctor () {
955 List<Doc> documents = new List<Doc>();
956 documents.Add(new Doc { Name = "Doc1", Type = DocType.One } );
957 documents.Add(new Doc { Name = "Doc2", Type = DocType.Two } );
958 documents.Add(new Doc { Name = "Doc3", Type = DocType.Three } );
959 documents.Add(new Doc { Name = "Doc4", Type = DocType.One } );
960 documents.Add(new Doc { Name = "Doc5", Type = DocType.Two } );
961 documents.Add(new Doc { Name = "Doc6", Type = DocType.Three } );
962 documents.Add(new Doc { Name = "Doc7", Type = DocType.One } );
963 documents.Add(new Doc { Name = "Doc8", Type = DocType.Two } );
964 documents.Add(new Doc { Name = "Doc9", Type = DocType.Three } );
966 List<DocType> categories = documents.Select(d=>d.Type).Distinct().ToList<DocType>().OrderBy(d => d).ToList();
967 foreach(DocType cat in categories) {
968 List<Doc> catDocs = documents.Where(d => d.Type == cat).OrderBy(d => d.Name).ToList<Doc>();
970 return 0;
973 class A { }
975 static List<A> sources = new List<A>();
977 // #6112
978 public static int test_0_fullaot_imt () {
979 sources.Add(null);
980 sources.Add(null);
982 int a = sources.Count;
983 var enumerator = sources.GetEnumerator() as IEnumerator<object>;
985 while (enumerator.MoveNext())
987 object o = enumerator.Current;
990 return 0;
993 class AClass {
996 class BClass : AClass {
999 public static int test_0_fullaot_variant_iface () {
1000 var arr = new BClass [10];
1001 var enumerable = (IEnumerable<AClass>)arr;
1002 enumerable.GetEnumerator ();
1003 return 0;
1006 struct Record : Foo2<Record>.IRecord {
1007 int counter;
1008 int Foo2<Record>.IRecord.DoSomething () {
1009 return counter++;
1013 class Foo2<T> where T : Foo2<T>.IRecord {
1014 public interface IRecord {
1015 int DoSomething ();
1018 public static int Extract (T[] t) {
1019 return t[0].DoSomething ();
1023 class Foo3<T> where T : IComparable {
1024 public static int CompareTo (T[] t) {
1025 // This is a constrained call to Enum.CompareTo ()
1026 return t[0].CompareTo (t [0]);
1030 public static int test_1_regress_constrained_iface_call_7571 () {
1031 var r = new Record [10];
1032 Foo2<Record>.Extract (r);
1033 return Foo2<Record>.Extract (r);
1036 enum ConstrainedEnum {
1037 Val = 1
1040 public static int test_0_regress_constrained_iface_call_enum () {
1041 var r = new ConstrainedEnum [10];
1042 return Foo3<ConstrainedEnum>.CompareTo (r);
1045 public interface IFoo2 {
1046 void MoveNext ();
1049 public struct Foo2 : IFoo2 {
1050 public void MoveNext () {
1054 public static Action Dingus (ref Foo2 f) {
1055 return new Action (f.MoveNext);
1058 public static int test_0_delegate_unbox_full_aot () {
1059 Foo2 foo = new Foo2 ();
1060 Dingus (ref foo) ();
1061 return 0;
1064 public static int test_0_arrays_ireadonly () {
1065 int[] arr = new int [10];
1066 for (int i = 0; i < 10; ++i)
1067 arr [i] = i;
1068 IReadOnlyList<int> a = (IReadOnlyList<int>)(object)arr;
1069 if (a.Count != 10)
1070 return 1;
1071 if (a [0] != 0)
1072 return 2;
1073 if (a [1] != 1)
1074 return 3;
1075 return 0;
1078 public static int test_0_volatile_read_write () {
1079 string foo = "ABC";
1080 Volatile.Write (ref foo, "DEF");
1081 return Volatile.Read (ref foo) == "DEF" ? 0 : 1;
1084 // FIXME: Doesn't work with --regression as Interlocked.Add(ref long) is only implemented as an intrinsic
1085 #if FALSE
1086 public static async Task<T> FooAsync<T> (int i, int j) {
1087 Task<int> t = new Task<int> (delegate () { Console.WriteLine ("HIT!"); return 0; });
1088 var response = await t;
1089 return default(T);
1092 public static int test_0_fullaot_generic_async () {
1093 Task<string> t = FooAsync<string> (1, 2);
1094 t.RunSynchronously ();
1095 return 0;
1097 #endif
1099 public static int test_0_delegate_callvirt_fullaot () {
1100 Func<string> f = delegate () { return "A"; };
1101 var f2 = (Func<Func<string>, string>)Delegate.CreateDelegate (typeof
1102 (Func<Func<string>, string>), null, f.GetType ().GetMethod ("Invoke"));
1104 var s = f2 (f);
1105 return s == "A" ? 0 : 1;
1108 public interface ICovariant<out R>
1112 // Deleting the `out` modifier from this line stop the problem
1113 public interface IExtCovariant<out R> : ICovariant<R>
1117 public class Sample<R> : ICovariant<R>
1121 public interface IMyInterface
1125 public static int test_0_variant_cast_cache () {
1126 object covariant = new Sample<IMyInterface>();
1128 var foo = (ICovariant<IMyInterface>)(covariant);
1130 try {
1131 var extCovariant = (IExtCovariant<IMyInterface>)covariant;
1132 return 1;
1133 } catch {
1134 return 0;
1138 struct FooStruct2 {
1139 public int a1, a2, a3;
1142 class MyClass<T> where T: struct {
1143 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1144 public MyClass(int a1, int a2, int a3, int a4, int a5, int a6, Nullable<T> a) {
1147 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1148 public static MyClass<T> foo () {
1149 Nullable<T> a = new Nullable<T> ();
1150 return new MyClass<T> (0, 0, 0, 0, 0, 0, a);
1154 public static int test_0_newobj_generic_context () {
1155 MyClass<FooStruct2>.foo ();
1156 return 0;
1159 enum AnEnum {
1164 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1165 public static string constrained_tostring<T> (T t) {
1166 return t.ToString ();
1169 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1170 public static bool constrained_equals<T> (T t1, T t2) {
1171 var c = EqualityComparer<T>.Default;
1173 return c.Equals (t1, t2);
1176 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1177 public static int constrained_gethashcode<T> (T t) {
1178 return t.GetHashCode ();
1181 public static int test_0_constrained_partial_sharing () {
1182 string s;
1184 s = constrained_tostring<int> (5);
1185 if (s != "5")
1186 return 1;
1187 s = constrained_tostring<AnEnum> (AnEnum.B);
1188 if (s != "B")
1189 return 2;
1191 if (!constrained_equals<int> (1, 1))
1192 return 3;
1193 if (constrained_equals<int> (1, 2))
1194 return 4;
1195 if (!constrained_equals<AnEnum> (AnEnum.A, AnEnum.A))
1196 return 5;
1197 if (constrained_equals<AnEnum> (AnEnum.A, AnEnum.B))
1198 return 6;
1200 int i = constrained_gethashcode<int> (5);
1201 if (i != 5)
1202 return 7;
1203 i = constrained_gethashcode<AnEnum> (AnEnum.B);
1204 if (i != 1)
1205 return 8;
1206 return 0;
1209 enum Enum1 {
1214 enum Enum2 {
1219 public static int test_0_partial_sharing_ginst () {
1220 var l1 = new List<KeyValuePair<int, Enum1>> ();
1221 l1.Add (new KeyValuePair<int, Enum1>(5, Enum1.A));
1222 if (l1 [0].Key != 5)
1223 return 1;
1224 if (l1 [0].Value != Enum1.A)
1225 return 2;
1226 var l2 = new List<KeyValuePair<int, Enum2>> ();
1227 l2.Add (new KeyValuePair<int, Enum2>(5, Enum2.B));
1228 if (l2 [0].Key != 5)
1229 return 3;
1230 if (l2 [0].Value != Enum2.B)
1231 return 4;
1232 return 0;
1235 static object delegate_8_args_res;
1237 public static int test_0_delegate_8_args () {
1238 delegate_8_args_res = null;
1239 Action<string, string, string, string, string, string, string,
1240 string> test = (a, b, c, d, e, f, g, h) =>
1242 delegate_8_args_res = h;
1244 test("a", "b", "c", "d", "e", "f", "g", "h");
1245 return delegate_8_args_res == "h" ? 0 : 1;
1248 static void throw_catch_t<T> () where T: Exception {
1249 try {
1250 throw new NotSupportedException ();
1251 } catch (T) {
1255 public static int test_0_gshared_catch_open_type () {
1256 throw_catch_t<NotSupportedException> ();
1257 return 0;
1260 class ThrowClass<T> where T: Exception {
1261 public void throw_catch_t () {
1262 try {
1263 throw new NotSupportedException ();
1264 } catch (T) {
1269 public static int test_0_gshared_catch_open_type_instance () {
1270 var c = new ThrowClass<NotSupportedException> ();
1271 c.throw_catch_t ();
1272 return 0;
1277 #if !__MOBILE__
1278 class GenericsTests : Tests
1281 #endif