[interp] support r4 (single) in stelem instruction
[mono-project.git] / mono / mini / generics.cs
blob6cb61e16e1adcda5615780b8e28ac7e870dc256a
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 int test_1_ldelem_stelem_any_single () {
133 float[] arr = new float [3];
134 stelem_any (arr, 1);
136 return (int) ldelem_any (arr);
139 public static int test_1_ldelem_stelem_any_double () {
140 double[] arr = new double [3];
141 stelem_any (arr, 1);
143 return (int) ldelem_any (arr);
146 public static T return_ref<T> (ref T t) {
147 return t;
150 public static T ldelema_any<T> (T[] arr) {
151 return return_ref<T> (ref arr [0]);
154 public static int test_0_ldelema () {
155 string[] arr = new string [1];
157 arr [0] = "Hello";
159 if (ldelema_any <string> (arr) == "Hello")
160 return 0;
161 else
162 return 1;
165 public static T[,] newarr_multi<T> () {
166 return new T [1, 1];
169 public static int test_0_newarr_multi_dim () {
170 return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
173 interface ITest
175 void Foo<T> ();
178 public static int test_0_iface_call_null_bug_77442 () {
179 ITest test = null;
181 try {
182 test.Foo<int> ();
184 catch (NullReferenceException) {
185 return 0;
188 return 1;
191 public static int test_18_ldobj_stobj_generics () {
192 GenericClass<int> t = new GenericClass <int> ();
193 int i = 5;
194 int j = 6;
195 return t.ldobj_stobj (ref i, ref j) + i + j;
198 public static int test_5_ldelem_stelem_generics () {
199 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
201 TestStruct s = new TestStruct (5, 5);
202 return t.ldelem_stelem (s).i;
205 public static int test_0_constrained_vtype_box () {
206 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
208 #if __MOBILE__
209 return t.toString (new TestStruct ()) == "GenericsTests+TestStruct" ? 0 : 1;
210 #else
211 return t.toString (new TestStruct ()) == "Tests+TestStruct" ? 0 : 1;
212 #endif
215 public static int test_0_constrained_vtype () {
216 GenericClass<int> t = new GenericClass<int> ();
218 return t.toString (1234) == "1234" ? 0 : 1;
221 public static int test_0_constrained_reftype () {
222 GenericClass<String> t = new GenericClass<String> ();
224 return t.toString ("1234") == "1234" ? 0 : 1;
227 public static int test_0_box_brtrue_optimizations () {
228 if (IsNull<int>(5))
229 return 1;
231 if (!IsNull<object>(null))
232 return 1;
234 return 0;
237 [Category ("!FULLAOT")]
238 public static int test_0_generic_get_value_optimization_int () {
239 int[] x = new int[] {100, 200};
241 if (GenericClass<int>.Z (x, 0) != 100)
242 return 2;
244 if (GenericClass<int>.Z (x, 1) != 200)
245 return 3;
247 return 0;
250 interface NonGenericInterface {
251 int return_field ();
254 interface GenericInterface<T> : NonGenericInterface {
255 T not_used ();
258 struct ImplementGenericInterface<T> : GenericInterface<T> {
259 public Object padding1;
260 public Object padding2;
261 public Object padding3;
262 public T[] arr_t;
264 public ImplementGenericInterface (T[] arr_t) {
265 this.padding1 = null;
266 this.padding2 = null;
267 this.padding3 = null;
268 this.arr_t = arr_t;
271 public T not_used () {
272 return arr_t [0];
275 public int return_field () {
276 return arr_t.Length;
280 public static int test_8_struct_implements_generic_interface () {
281 int[] arr = {1, 2, 3, 4};
282 NonGenericInterface s = new ImplementGenericInterface<int> (arr);
283 return s.return_field () + s.return_field ();
286 public static int test_0_generic_get_value_optimization_vtype () {
287 TestStruct[] arr = new TestStruct[] { new TestStruct (100, 200), new TestStruct (300, 400) };
288 IEnumerator<TestStruct> enumerator = GenericClass<TestStruct>.Y (arr);
289 TestStruct s;
290 int sum = 0;
291 while (enumerator.MoveNext ()) {
292 s = enumerator.Current;
293 sum += s.i + s.j;
296 if (sum != 1000)
297 return 1;
299 s = GenericClass<TestStruct>.Z (arr, 0);
300 if (s.i != 100 || s.j != 200)
301 return 2;
303 s = GenericClass<TestStruct>.Z (arr, 1);
304 if (s.i != 300 || s.j != 400)
305 return 3;
307 return 0;
310 public static int test_0_nullable_ldflda () {
311 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
314 public struct GenericStruct<T> {
315 public T t;
317 public GenericStruct (T t) {
318 this.t = t;
322 public class GenericClass<T> {
323 public T t;
325 public GenericClass (T t) {
326 this.t = t;
329 public GenericClass () {
332 public T ldobj_stobj (ref T t1, ref T t2) {
333 t1 = t2;
334 T t = t1;
336 return t;
339 public T ldelem_stelem (T t) {
340 T[] arr = new T [10];
341 arr [0] = t;
343 return arr [0];
346 public String toString (T t) {
347 return t.ToString ();
350 public static IEnumerator<T> Y (IEnumerable <T> x)
352 return x.GetEnumerator ();
355 public static T Z (IList<T> x, int index)
357 return x [index];
360 protected static T NullB = default(T);
361 private static Nullable<bool> _BIsA = null;
362 public static bool BIsAClazz {
363 get {
364 _BIsA = false;
365 return _BIsA.Value;
370 public class MRO : MarshalByRefObject {
371 public GenericStruct<int> struct_field;
372 public GenericClass<int> class_field;
375 public class MRO<T> : MarshalByRefObject {
376 public T gen_field;
378 public T stfld_ldfld (T t) {
379 var m = this;
380 m.gen_field = t;
381 return m.gen_field;
385 public static int test_0_ldfld_stfld_mro () {
386 MRO m = new MRO ();
387 GenericStruct<int> s = new GenericStruct<int> (5);
388 // This generates stfld
389 m.struct_field = s;
391 // This generates ldflda
392 if (m.struct_field.t != 5)
393 return 1;
395 // This generates ldfld
396 GenericStruct<int> s2 = m.struct_field;
397 if (s2.t != 5)
398 return 2;
400 if (m.struct_field.t != 5)
401 return 3;
403 m.class_field = new GenericClass<int> (5);
404 if (m.class_field.t != 5)
405 return 4;
407 // gshared
408 var m2 = new MRO<string> ();
409 if (m2.stfld_ldfld ("A") != "A")
410 return 5;
412 return 0;
415 // FIXME:
416 [Category ("!FULLAOT")]
417 public static int test_0_generic_virtual_call_on_vtype_unbox () {
418 object o = new Object ();
419 IFoo h = new Handler(o);
421 if (h.Bar<object> () != o)
422 return 1;
423 else
424 return 0;
427 public static int test_0_box_brtrue_opt () {
428 Foo<int> f = new Foo<int> (5);
430 f [123] = 5;
432 return 0;
435 public static int test_0_box_brtrue_opt_regress_81102 () {
436 if (new Foo<int>(5).ToString () == "null")
437 return 0;
438 else
439 return 1;
442 struct S {
443 public int i;
446 public static int test_0_ldloca_initobj_opt () {
447 if (new Foo<S> (new S ()).get_default ().i != 0)
448 return 1;
449 if (new Foo<object> (null).get_default () != null)
450 return 2;
451 return 0;
454 #if !__MOBILE__
455 public static int test_0_variance_reflection () {
456 // covariance on IEnumerator
457 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
458 return 1;
459 // covariance on IEnumerator and covariance on arrays
460 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
461 return 2;
462 // covariance and implemented interfaces
463 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
464 return 3;
466 // contravariance on IComparer
467 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
468 return 4;
469 // contravariance on IComparer, contravariance on arrays
470 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
471 return 5;
472 // contravariance and interface inheritance
473 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
474 return 6;
475 return 0;
477 #endif
479 public static int test_0_ldvirtftn_generic_method () {
480 new GenericsTests ().ldvirtftn<string> ();
482 return the_type == typeof (string) ? 0 : 1;
485 public static int test_0_throw_dead_this () {
486 new Foo<string> ("").throw_dead_this ();
487 return 0;
490 struct S<T> {}
492 public static int test_0_inline_infinite_polymorphic_recursion () {
493 f<int>(0);
495 return 0;
498 private static void f<T>(int i) {
499 if(i==42) f<S<T>>(i);
502 // This cannot be made to work with full-aot, since there it is impossible to
503 // statically determine that Foo<string>.Bar <int> is needed, the code only
504 // references IFoo.Bar<int>
505 [Category ("!FULLAOT")]
506 public static int test_0_generic_virtual_on_interfaces () {
507 Foo<string>.count1 = 0;
508 Foo<string>.count2 = 0;
509 Foo<string>.count3 = 0;
511 IFoo f = new Foo<string> ("");
512 for (int i = 0; i < 1000; ++i) {
513 f.Bar <int> ();
514 f.Bar <string> ();
515 f.NonGeneric ();
518 if (Foo<string>.count1 != 1000)
519 return 1;
520 if (Foo<string>.count2 != 1000)
521 return 2;
522 if (Foo<string>.count3 != 1000)
523 return 3;
525 VirtualInterfaceCallFromGenericMethod<long> (f);
527 return 0;
530 public static int test_0_generic_virtual_on_interfaces_ref () {
531 Foo<string>.count1 = 0;
532 Foo<string>.count2 = 0;
533 Foo<string>.count3 = 0;
534 Foo<string>.count4 = 0;
536 IFoo f = new Foo<string> ("");
537 for (int i = 0; i < 1000; ++i) {
538 f.Bar <string> ();
539 f.Bar <object> ();
540 f.NonGeneric ();
543 if (Foo<string>.count2 != 1000)
544 return 2;
545 if (Foo<string>.count3 != 1000)
546 return 3;
547 if (Foo<string>.count4 != 1000)
548 return 4;
550 return 0;
553 //repro for #505375
554 [Category ("!FULLAOT")]
555 public static int test_2_cprop_bug () {
556 int idx = 0;
557 int a = 1;
558 var cmp = System.Collections.Generic.Comparer<int>.Default ;
559 if (cmp.Compare (a, 0) > 0)
560 a = 0;
561 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
562 return idx;
565 enum MyEnumUlong : ulong {
566 Value_2 = 2
569 public static int test_0_regress_550964_constrained_enum_long () {
570 MyEnumUlong a = MyEnumUlong.Value_2;
571 MyEnumUlong b = MyEnumUlong.Value_2;
573 return Pan (a, b) ? 0 : 1;
576 static bool Pan<T> (T a, T b)
578 return a.Equals (b);
581 public class XElement {
582 public string Value {
583 get; set;
587 public static int test_0_fullaot_linq () {
588 var allWords = new XElement [] { new XElement { Value = "one" } };
589 var filteredWords = allWords.Where(kw => kw.Value.StartsWith("T"));
590 return filteredWords.Count ();
593 public static int test_0_fullaot_comparer_t () {
594 var l = new SortedList <TimeSpan, int> ();
595 return l.Count;
598 public static int test_0_fullaot_comparer_t_2 () {
599 var l = new Dictionary <TimeSpan, int> ();
600 return l.Count;
603 static void enumerate<T> (IEnumerable<T> arr) {
604 foreach (var o in arr)
606 int c = ((ICollection<T>)arr).Count;
609 /* Test that treating arrays as generic collections works with full-aot */
610 public static int test_0_fullaot_array_wrappers () {
611 GenericsTests[] arr = new GenericsTests [10];
612 enumerate<GenericsTests> (arr);
613 return 0;
616 static int cctor_count = 0;
618 public abstract class Beta<TChanged>
620 static Beta()
622 cctor_count ++;
626 public class Gamma<T> : Beta<T>
628 static Gamma()
633 // #519336
634 public static int test_2_generic_class_init_gshared_ctor () {
635 new Gamma<object>();
636 new Gamma<string>();
638 return cctor_count;
641 static int cctor_count2 = 0;
643 class ServiceController<T> {
644 static ServiceController () {
645 cctor_count2 ++;
648 public ServiceController () {
652 static ServiceController<T> Create<T>() {
653 return new ServiceController<T>();
656 // #631409
657 public static int test_2_generic_class_init_gshared_ctor_from_gshared () {
658 Create<object> ();
659 Create<string> ();
661 return cctor_count2;
664 public static Type get_type<T> () {
665 return typeof (T);
668 public static int test_0_gshared_delegate_rgctx () {
669 Func<Type> t = new Func<Type> (get_type<string>);
671 if (t () == typeof (string))
672 return 0;
673 else
674 return 1;
677 // Creating a delegate from a generic method from gshared code
678 public static int test_0_gshared_delegate_from_gshared () {
679 if (gshared_delegate_from_gshared <object> () != 0)
680 return 1;
681 if (gshared_delegate_from_gshared <string> () != 0)
682 return 2;
683 return 0;
686 public static int gshared_delegate_from_gshared <T> () {
687 Func<Type> t = new Func<Type> (get_type<T>);
689 return t () == typeof (T) ? 0 : 1;
692 public static int test_0_marshalbyref_call_from_gshared_virt_elim () {
693 /* Calling a virtual method from gshared code which is changed to a nonvirt call */
694 Class1<object> o = new Class1<object> ();
695 o.Do (new Class2<object> ());
696 return 0;
699 class Pair<TKey, TValue> {
700 public static KeyValuePair<TKey, TValue> make_pair (TKey key, TValue value)
702 return new KeyValuePair<TKey, TValue> (key, value);
705 public delegate TRet Transform<TRet> (TKey key, TValue value);
708 public static int test_0_bug_620864 () {
709 var d = new Pair<string, Type>.Transform<KeyValuePair<string, Type>> (Pair<string, Type>.make_pair);
711 var p = d ("FOO", typeof (int));
712 if (p.Key != "FOO" || p.Value != typeof (int))
713 return 1;
715 return 0;
719 struct RecStruct<T> {
720 public void foo (RecStruct<RecStruct<T>> baz) {
724 public static int test_0_infinite_generic_recursion () {
725 // Check that the AOT compile can deal with infinite generic recursion through
726 // parameter types
727 RecStruct<int> bla;
729 return 0;
732 struct FooStruct {
735 bool IsNull2 <T> (object value) where T : struct {
736 T? item = (T?) value;
738 if (item.HasValue)
739 return false;
741 return true;
744 public static int test_0_full_aot_nullable_unbox_from_gshared_code () {
745 if (!new GenericsTests ().IsNull2<FooStruct> (null))
746 return 1;
747 if (new GenericsTests ().IsNull2<FooStruct> (new FooStruct ()))
748 return 2;
749 return 0;
752 public static int test_0_partial_sharing () {
753 if (PartialShared1 (new List<string> (), 1) != typeof (string))
754 return 1;
755 if (PartialShared1 (new List<GenericsTests> (), 1) != typeof (GenericsTests))
756 return 2;
757 if (PartialShared2 (new List<string> (), 1) != typeof (int))
758 return 3;
759 if (PartialShared2 (new List<GenericsTests> (), 1) != typeof (int))
760 return 4;
761 return 0;
764 [Category ("GSHAREDVT")]
765 public static int test_6_partial_sharing_linq () {
766 var messages = new List<Message> ();
768 messages.Add (new Message () { MessageID = 5 });
769 messages.Add (new Message () { MessageID = 6 });
771 return messages.Max(i => i.MessageID);
774 public static int test_0_partial_shared_method_in_nonshared_class () {
775 var c = new Class1<double> ();
776 return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
779 class Message {
780 public int MessageID {
781 get; set;
785 public static Type PartialShared1<T, K> (List<T> list, K k) {
786 return typeof (T);
789 public static Type PartialShared2<T, K> (List<T> list, K k) {
790 return typeof (K);
793 public class Class1<T> {
794 public virtual void Do (Class2<T> t) {
795 t.Foo ();
798 public virtual object Foo<U> (T t) {
799 return new Class1<U> ();
803 public interface IFace1<T> {
804 void Foo ();
807 public class Class2<T> : MarshalByRefObject, IFace1<T> {
808 public void Foo () {
814 public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
815 f.Bar <T> ();
818 public static Type the_type;
820 public void ldvirtftn<T> () {
821 Foo <T> binding = new Foo <T> (default (T));
823 binding.GenericEvent += event_handler;
824 binding.fire ();
827 public virtual void event_handler<T> (Foo<T> sender) {
828 the_type = typeof (T);
831 public interface IFoo {
832 void NonGeneric ();
833 object Bar<T>();
836 public class Foo<T1> : IFoo
838 public Foo(T1 t1)
840 m_t1 = t1;
843 public override string ToString()
845 return Bar(m_t1 == null ? "null" : "null");
848 public String Bar (String s) {
849 return s;
852 public int this [T1 key] {
853 set {
854 if (key == null)
855 throw new ArgumentNullException ("key");
859 public void throw_dead_this () {
860 try {
861 new SomeClass().ThrowAnException();
863 catch {
867 public T1 get_default () {
868 return default (T1);
871 readonly T1 m_t1;
873 public delegate void GenericEventHandler (Foo<T1> sender);
875 public event GenericEventHandler GenericEvent;
877 public void fire () {
878 GenericEvent (this);
881 public static int count1, count2, count3, count4;
883 public void NonGeneric () {
884 count3 ++;
887 public object Bar <T> () {
888 if (typeof (T) == typeof (int))
889 count1 ++;
890 else if (typeof (T) == typeof (string))
891 count2 ++;
892 else if (typeof (T) == typeof (object))
893 count4 ++;
894 return null;
898 public class SomeClass {
899 public void ThrowAnException() {
900 throw new Exception ("Something went wrong");
904 struct Handler : IFoo {
905 object o;
907 public Handler(object o) {
908 this.o = o;
911 public void NonGeneric () {
914 public object Bar<T>() {
915 return o;
919 static bool IsNull<T> (T t)
921 if (t == null)
922 return true;
923 else
924 return false;
927 static object Box<T> (T t)
929 return t;
932 static T Unbox <T> (object o) {
933 return (T) o;
936 interface IDefaultRetriever
938 T GetDefault<T>();
941 class DefaultRetriever : IDefaultRetriever
943 [MethodImpl(MethodImplOptions.Synchronized)]
944 public T GetDefault<T>()
946 return default(T);
950 [Category ("!FULLAOT")]
951 [Category ("!BITCODE")]
952 public static int test_0_regress_668095_synchronized_gshared () {
953 return DoSomething (new DefaultRetriever ());
956 static int DoSomething(IDefaultRetriever foo) {
957 int result = foo.GetDefault<int>();
958 return result;
961 class SyncClass<T> {
962 [MethodImpl(MethodImplOptions.Synchronized)]
963 public Type getInstance() {
964 return typeof (T);
968 [Category ("GSHAREDVT")]
969 static int test_0_synchronized_gshared () {
970 var c = new SyncClass<string> ();
971 if (c.getInstance () != typeof (string))
972 return 1;
973 return 0;
976 class Response {
979 public static int test_0_687865_isinst_with_cache_wrapper () {
980 object o = new object ();
981 if (o is Action<IEnumerable<Response>>)
982 return 1;
983 else
984 return 0;
987 enum DocType {
988 One,
989 Two,
990 Three
993 class Doc {
994 public string Name {
995 get; set;
998 public DocType Type {
999 get; set;
1003 // #2155
1004 [Category ("GSHAREDVT")]
1005 public static int test_0_fullaot_sflda_cctor () {
1006 List<Doc> documents = new List<Doc>();
1007 documents.Add(new Doc { Name = "Doc1", Type = DocType.One } );
1008 documents.Add(new Doc { Name = "Doc2", Type = DocType.Two } );
1009 documents.Add(new Doc { Name = "Doc3", Type = DocType.Three } );
1010 documents.Add(new Doc { Name = "Doc4", Type = DocType.One } );
1011 documents.Add(new Doc { Name = "Doc5", Type = DocType.Two } );
1012 documents.Add(new Doc { Name = "Doc6", Type = DocType.Three } );
1013 documents.Add(new Doc { Name = "Doc7", Type = DocType.One } );
1014 documents.Add(new Doc { Name = "Doc8", Type = DocType.Two } );
1015 documents.Add(new Doc { Name = "Doc9", Type = DocType.Three } );
1017 List<DocType> categories = documents.Select(d=>d.Type).Distinct().ToList<DocType>().OrderBy(d => d).ToList();
1018 foreach(DocType cat in categories) {
1019 List<Doc> catDocs = documents.Where(d => d.Type == cat).OrderBy(d => d.Name).ToList<Doc>();
1021 return 0;
1024 class A { }
1026 static List<A> sources = new List<A>();
1028 // #6112
1029 public static int test_0_fullaot_imt () {
1030 sources.Add(null);
1031 sources.Add(null);
1033 int a = sources.Count;
1034 var enumerator = sources.GetEnumerator() as IEnumerator<object>;
1036 while (enumerator.MoveNext())
1038 object o = enumerator.Current;
1041 return 0;
1044 class AClass {
1047 class BClass : AClass {
1050 public static int test_0_fullaot_variant_iface () {
1051 var arr = new BClass [10];
1052 var enumerable = (IEnumerable<AClass>)arr;
1053 enumerable.GetEnumerator ();
1054 return 0;
1057 struct Record : Foo2<Record>.IRecord {
1058 int counter;
1059 int Foo2<Record>.IRecord.DoSomething () {
1060 return counter++;
1064 class Foo2<T> where T : Foo2<T>.IRecord {
1065 public interface IRecord {
1066 int DoSomething ();
1069 public static int Extract (T[] t) {
1070 return t[0].DoSomething ();
1074 class Foo3<T> where T : IComparable {
1075 public static int CompareTo (T[] t) {
1076 // This is a constrained call to Enum.CompareTo ()
1077 return t[0].CompareTo (t [0]);
1081 public static int test_1_regress_constrained_iface_call_7571 () {
1082 var r = new Record [10];
1083 Foo2<Record>.Extract (r);
1084 return Foo2<Record>.Extract (r);
1087 enum ConstrainedEnum {
1088 Val = 1
1091 public static int test_0_regress_constrained_iface_call_enum () {
1092 var r = new ConstrainedEnum [10];
1093 return Foo3<ConstrainedEnum>.CompareTo (r);
1096 public interface IFoo2 {
1097 void MoveNext ();
1100 public struct Foo2 : IFoo2 {
1101 public void MoveNext () {
1105 public static Action Dingus (ref Foo2 f) {
1106 return new Action (f.MoveNext);
1109 public static int test_0_delegate_unbox_full_aot () {
1110 Foo2 foo = new Foo2 ();
1111 Dingus (ref foo) ();
1112 return 0;
1115 public static int test_0_arrays_ireadonly () {
1116 int[] arr = new int [10];
1117 for (int i = 0; i < 10; ++i)
1118 arr [i] = i;
1119 IReadOnlyList<int> a = (IReadOnlyList<int>)(object)arr;
1120 if (a.Count != 10)
1121 return 1;
1122 if (a [0] != 0)
1123 return 2;
1124 if (a [1] != 1)
1125 return 3;
1126 return 0;
1129 public static int test_0_volatile_read_write () {
1130 string foo = "ABC";
1131 Volatile.Write (ref foo, "DEF");
1132 return Volatile.Read (ref foo) == "DEF" ? 0 : 1;
1135 // FIXME: Doesn't work with --regression as Interlocked.Add(ref long) is only implemented as an intrinsic
1136 #if FALSE
1137 public static async Task<T> FooAsync<T> (int i, int j) {
1138 Task<int> t = new Task<int> (delegate () { Console.WriteLine ("HIT!"); return 0; });
1139 var response = await t;
1140 return default(T);
1143 public static int test_0_fullaot_generic_async () {
1144 Task<string> t = FooAsync<string> (1, 2);
1145 t.RunSynchronously ();
1146 return 0;
1148 #endif
1150 public static int test_0_delegate_callvirt_fullaot () {
1151 Func<string> f = delegate () { return "A"; };
1152 var f2 = (Func<Func<string>, string>)Delegate.CreateDelegate (typeof
1153 (Func<Func<string>, string>), null, f.GetType ().GetMethod ("Invoke"));
1155 var s = f2 (f);
1156 return s == "A" ? 0 : 1;
1159 public interface ICovariant<out R>
1163 // Deleting the `out` modifier from this line stop the problem
1164 public interface IExtCovariant<out R> : ICovariant<R>
1168 public class Sample<R> : ICovariant<R>
1172 public interface IMyInterface
1176 public static int test_0_variant_cast_cache () {
1177 object covariant = new Sample<IMyInterface>();
1179 var foo = (ICovariant<IMyInterface>)(covariant);
1181 try {
1182 var extCovariant = (IExtCovariant<IMyInterface>)covariant;
1183 return 1;
1184 } catch {
1185 return 0;
1189 struct FooStruct2 {
1190 public int a1, a2, a3;
1193 class MyClass<T> where T: struct {
1194 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1195 public MyClass(int a1, int a2, int a3, int a4, int a5, int a6, Nullable<T> a) {
1198 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1199 public static MyClass<T> foo () {
1200 Nullable<T> a = new Nullable<T> ();
1201 return new MyClass<T> (0, 0, 0, 0, 0, 0, a);
1205 public static int test_0_newobj_generic_context () {
1206 MyClass<FooStruct2>.foo ();
1207 return 0;
1210 enum AnEnum {
1215 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1216 public static string constrained_tostring<T> (T t) {
1217 return t.ToString ();
1220 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1221 public static bool constrained_equals<T> (T t1, T t2) {
1222 var c = EqualityComparer<T>.Default;
1224 return c.Equals (t1, t2);
1227 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1228 public static int constrained_gethashcode<T> (T t) {
1229 return t.GetHashCode ();
1232 public static int test_0_constrained_partial_sharing () {
1233 string s;
1235 s = constrained_tostring<int> (5);
1236 if (s != "5")
1237 return 1;
1238 s = constrained_tostring<AnEnum> (AnEnum.B);
1239 if (s != "B")
1240 return 2;
1242 if (!constrained_equals<int> (1, 1))
1243 return 3;
1244 if (constrained_equals<int> (1, 2))
1245 return 4;
1246 if (!constrained_equals<AnEnum> (AnEnum.A, AnEnum.A))
1247 return 5;
1248 if (constrained_equals<AnEnum> (AnEnum.A, AnEnum.B))
1249 return 6;
1251 int i = constrained_gethashcode<int> (5);
1252 if (i != 5)
1253 return 7;
1254 i = constrained_gethashcode<AnEnum> (AnEnum.B);
1255 if (i != 1)
1256 return 8;
1257 return 0;
1260 enum Enum1 {
1265 enum Enum2 {
1270 public static int test_0_partial_sharing_ginst () {
1271 var l1 = new List<KeyValuePair<int, Enum1>> ();
1272 l1.Add (new KeyValuePair<int, Enum1>(5, Enum1.A));
1273 if (l1 [0].Key != 5)
1274 return 1;
1275 if (l1 [0].Value != Enum1.A)
1276 return 2;
1277 var l2 = new List<KeyValuePair<int, Enum2>> ();
1278 l2.Add (new KeyValuePair<int, Enum2>(5, Enum2.B));
1279 if (l2 [0].Key != 5)
1280 return 3;
1281 if (l2 [0].Value != Enum2.B)
1282 return 4;
1283 return 0;
1286 static object delegate_8_args_res;
1288 public static int test_0_delegate_8_args () {
1289 delegate_8_args_res = null;
1290 Action<string, string, string, string, string, string, string,
1291 string> test = (a, b, c, d, e, f, g, h) =>
1293 delegate_8_args_res = h;
1295 test("a", "b", "c", "d", "e", "f", "g", "h");
1296 return delegate_8_args_res == "h" ? 0 : 1;
1299 static void throw_catch_t<T> () where T: Exception {
1300 try {
1301 throw new NotSupportedException ();
1302 } catch (T) {
1306 public static int test_0_gshared_catch_open_type () {
1307 throw_catch_t<NotSupportedException> ();
1308 return 0;
1311 class ThrowClass<T> where T: Exception {
1312 public void throw_catch_t () {
1313 try {
1314 throw new NotSupportedException ();
1315 } catch (T) {
1320 public static int test_0_gshared_catch_open_type_instance () {
1321 var c = new ThrowClass<NotSupportedException> ();
1322 c.throw_catch_t ();
1323 return 0;
1326 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1327 public static bool is_ref_or_contains_refs<T> () {
1328 return RuntimeHelpers.IsReferenceOrContainsReferences<T> ();
1331 class IsRefClass<T> {
1332 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1333 public bool is_ref () {
1334 return RuntimeHelpers.IsReferenceOrContainsReferences<T> ();
1338 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1339 public static bool is_ref_or_contains_refs_gen_ref<T> () {
1340 return RuntimeHelpers.IsReferenceOrContainsReferences<GenStruct<T>> ();
1343 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1344 public static bool is_ref_or_contains_refs_gen_noref<T> () {
1345 return RuntimeHelpers.IsReferenceOrContainsReferences<NoRefGenStruct<T>> ();
1348 struct GenStruct<T> {
1349 T t;
1352 struct NoRefGenStruct<T> {
1355 struct RefStruct {
1356 string s;
1359 struct NestedRefStruct {
1360 RefStruct r;
1363 struct NoRefStruct {
1364 int i;
1367 struct AStruct3<T1, T2, T3> {
1368 T1 t1;
1369 T2 t2;
1370 T3 t3;
1373 public static int test_0_isreference_intrins () {
1374 if (RuntimeHelpers.IsReferenceOrContainsReferences<int> ())
1375 return 1;
1376 if (!RuntimeHelpers.IsReferenceOrContainsReferences<string> ())
1377 return 2;
1378 if (!RuntimeHelpers.IsReferenceOrContainsReferences<RefStruct> ())
1379 return 3;
1380 if (!RuntimeHelpers.IsReferenceOrContainsReferences<NestedRefStruct> ())
1381 return 4;
1382 if (RuntimeHelpers.IsReferenceOrContainsReferences<NoRefStruct> ())
1383 return 5;
1384 // Generic code
1385 if (is_ref_or_contains_refs<int> ())
1386 return 6;
1387 // Shared code
1388 if (!is_ref_or_contains_refs<string> ())
1389 return 7;
1390 // Complex type from shared code
1391 if (!is_ref_or_contains_refs_gen_ref<string> ())
1392 return 8;
1393 if (is_ref_or_contains_refs_gen_ref<int> ())
1394 return 9;
1395 if (is_ref_or_contains_refs_gen_noref<string> ())
1396 return 10;
1398 // Complex type from shared class method
1399 var c1 = new IsRefClass<AStruct3<int, int, int>> ();
1400 if (c1.is_ref ())
1401 return 11;
1402 var c2 = new IsRefClass<AStruct3<string, int, int>> ();
1403 if (!c2.is_ref ())
1404 return 12;
1406 return 0;
1409 class LdobjStobj {
1410 public int counter;
1411 public LdobjStobj buffer1;
1412 public LdobjStobj buffer2;
1415 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1416 private static void swap<T>(ref T first, ref T second) {
1417 second = first;
1420 public static int test_42_ldobj_stobj_ref () {
1421 var obj = new LdobjStobj ();
1422 obj.counter = 42;
1423 swap (ref obj.buffer1, ref obj.buffer2);
1424 return obj.counter;
1427 public interface ICompletion {
1428 Type UnsafeOnCompleted ();
1431 public struct TaskAwaiter<T> : ICompletion {
1432 public Type UnsafeOnCompleted () {
1433 typeof(T).GetHashCode ();
1434 return typeof(T);
1438 public struct AStruct {
1439 public Type Caller<TAwaiter>(ref TAwaiter awaiter)
1440 where TAwaiter : ICompletion {
1441 return awaiter.UnsafeOnCompleted();
1445 public static int test_0_partial_constrained_call_llvmonly () {
1446 var builder = new AStruct ();
1447 var awaiter = new TaskAwaiter<bool> ();
1448 var res = builder.Caller (ref awaiter);
1449 return res == typeof (bool) ? 0 : 1;
1453 #if !__MOBILE__
1454 class GenericsTests : Tests
1457 #endif