Merge pull request #4433 from kumpera/android-fixes
[mono.git] / mono / mini / generics.cs
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;
7
8 #if __MOBILE__
9 class GenericsTests
10 #else
11 class Tests
12 #endif
13 {
14         struct TestStruct {
15                 public int i;
16                 public int j;
17
18                 public TestStruct (int i, int j) {
19                         this.i = i;
20                         this.j = j;
21                 }
22         }
23
24 #if !__MOBILE__
25         class Enumerator <T> : MyIEnumerator <T> {
26                 T MyIEnumerator<T>.Current {
27                         get {
28                                 return default(T);
29                         }
30                 }
31
32                 bool MyIEnumerator<T>.MoveNext () {
33                         return true;
34                 }
35         }
36
37         class Comparer <T> : IComparer <T> {
38                 bool IComparer<T>.Compare (T x, T y) {
39                         return true;
40                 }
41         }
42 #endif
43
44 #if !__MOBILE__
45         static int Main (string[] args)
46         {
47                 return TestDriver.RunTests (typeof (Tests), args);
48         }
49 #endif
50
51         public static int test_1_nullable_unbox ()
52         {
53                 return Unbox<int?> (1).Value;
54         }
55
56         public static int test_1_nullable_unbox_null ()
57         {
58                 return Unbox<int?> (null).HasValue ? 0 : 1;
59         }
60
61         public static int test_1_nullable_box ()
62         {
63                 return (int) Box<int?> (1);
64         }
65
66         public static int test_1_nullable_box_null ()
67         {
68                 return Box<int?> (null) == null ? 1 : 0;
69         }
70
71         public static int test_1_isinst_nullable ()
72         {
73                 object o = 1;
74                 return (o is int?) ? 1 : 0;
75         }
76
77         public static int test_1_nullable_unbox_vtype ()
78         {
79                 return Unbox<TestStruct?> (new TestStruct (1, 2)).Value.i;
80         }
81
82         public static int test_1_nullable_unbox_null_vtype ()
83         {
84                 return Unbox<TestStruct?> (null).HasValue ? 0 : 1;
85         }
86
87         public static int test_1_nullable_box_vtype ()
88         {
89                 return ((TestStruct)(Box<TestStruct?> (new TestStruct (1, 2)))).i;
90         }
91
92         public static int test_1_nullable_box_null_vtype ()
93         {
94                 return Box<TestStruct?> (null) == null ? 1 : 0;
95         }
96
97         public static int test_1_isinst_nullable_vtype ()
98         {
99                 object o = new TestStruct (1, 2);
100                 return (o is TestStruct?) ? 1 : 0;
101         }
102
103         public static int test_0_nullable_normal_unbox ()
104         {
105                 int? i = 5;
106
107                 object o = i;
108                 // This uses unbox instead of unbox_any
109                 int? j = (int?)o;
110
111                 if (j != 5)
112                         return 1;
113
114                 return 0;
115         }
116
117         public static void stelem_any<T> (T[] arr, T elem) {
118                 arr [0] = elem;
119         }
120
121         public static T ldelem_any<T> (T[] arr) {
122                 return arr [0];
123         }
124
125         public static int test_1_ldelem_stelem_any_int () {
126                 int[] arr = new int [3];
127                 stelem_any (arr, 1);
128
129                 return ldelem_any (arr);
130         }
131
132         public static T return_ref<T> (ref T t) {
133                 return t;
134         }
135
136         public static T ldelema_any<T> (T[] arr) {
137                 return return_ref<T> (ref arr [0]);
138         }
139
140         public static int test_0_ldelema () {
141                 string[] arr = new string [1];
142
143                 arr [0] = "Hello";
144
145                 if (ldelema_any <string> (arr) == "Hello")
146                         return 0;
147                 else
148                         return 1;
149         }
150
151         public static T[,] newarr_multi<T> () {
152                 return new T [1, 1];
153         }
154
155         public static int test_0_newarr_multi_dim () {
156                 return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
157         }
158
159         interface ITest
160         {
161                 void Foo<T> ();
162         }
163
164         public static int test_0_iface_call_null_bug_77442 () {
165                 ITest test = null;
166
167                 try {
168                         test.Foo<int> ();
169                 }
170                 catch (NullReferenceException) {
171                         return 0;
172                 }
173                 
174                 return 1;
175         }
176
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;
182         }
183
184         public static int test_5_ldelem_stelem_generics () {
185                 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
186
187                 TestStruct s = new TestStruct (5, 5);
188                 return t.ldelem_stelem (s).i;
189         }
190
191         public static int test_0_constrained_vtype_box () {
192                 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
193
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
199         }
200
201         public static int test_0_constrained_vtype () {
202                 GenericClass<int> t = new GenericClass<int> ();
203
204                 return t.toString (1234) == "1234" ? 0 : 1;
205         }
206
207         public static int test_0_constrained_reftype () {
208                 GenericClass<String> t = new GenericClass<String> ();
209
210                 return t.toString ("1234") == "1234" ? 0 : 1;
211         }
212
213         public static int test_0_box_brtrue_optimizations () {
214                 if (IsNull<int>(5))
215                         return 1;
216
217                 if (!IsNull<object>(null))
218                         return 1;
219
220                 return 0;
221         }
222
223         [Category ("!FULLAOT")]
224         public static int test_0_generic_get_value_optimization_int () {
225                 int[] x = new int[] {100, 200};
226
227                 if (GenericClass<int>.Z (x, 0) != 100)
228                         return 2;
229
230                 if (GenericClass<int>.Z (x, 1) != 200)
231                         return 3;
232
233                 return 0;
234         }
235
236         [Category ("!INTERPRETER")]
237         public static int test_0_generic_get_value_optimization_vtype () {
238                 TestStruct[] arr = new TestStruct[] { new TestStruct (100, 200), new TestStruct (300, 400) };
239                 IEnumerator<TestStruct> enumerator = GenericClass<TestStruct>.Y (arr);
240                 TestStruct s;
241                 int sum = 0;
242                 while (enumerator.MoveNext ()) {
243                         s = enumerator.Current;
244                         sum += s.i + s.j;
245                 }
246
247                 if (sum != 1000)
248                         return 1;
249
250                 s = GenericClass<TestStruct>.Z (arr, 0);
251                 if (s.i != 100 || s.j != 200)
252                         return 2;
253
254                 s = GenericClass<TestStruct>.Z (arr, 1);
255                 if (s.i != 300 || s.j != 400)
256                         return 3;
257
258                 return 0;
259         }
260
261         public static int test_0_nullable_ldflda () {
262                 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
263         }
264
265         public struct GenericStruct<T> {
266                 public T t;
267
268                 public GenericStruct (T t) {
269                         this.t = t;
270                 }
271         }
272
273         public class GenericClass<T> {
274                 public T t;
275
276                 public GenericClass (T t) {
277                         this.t = t;
278                 }
279
280                 public GenericClass () {
281                 }
282
283                 public T ldobj_stobj (ref T t1, ref T t2) {
284                         t1 = t2;
285                         T t = t1;
286
287                         return t;
288                 }
289
290                 public T ldelem_stelem (T t) {
291                         T[] arr = new T [10];
292                         arr [0] = t;
293
294                         return arr [0];
295                 }
296
297                 public String toString (T t) {
298                         return t.ToString ();
299                 }
300
301                 public static IEnumerator<T> Y (IEnumerable <T> x)
302                 {
303                         return x.GetEnumerator ();
304                 }
305
306                 public static T Z (IList<T> x, int index)
307                 {
308                         return x [index];
309                 }
310
311         protected static T NullB = default(T);       
312         private static Nullable<bool>  _BIsA = null;
313         public static bool BIsAClazz {
314             get {
315                 _BIsA = false;
316                 return _BIsA.Value;
317             }
318         }
319         }
320
321         public class MRO : MarshalByRefObject {
322                 public GenericStruct<int> struct_field;
323                 public GenericClass<int> class_field;
324         }
325
326         public class MRO<T> : MarshalByRefObject {
327                 public T gen_field;
328
329                 public T stfld_ldfld (T t) {
330                         var m = this;
331                         m.gen_field = t;
332                         return m.gen_field;
333                 }
334         }
335
336         public static int test_0_ldfld_stfld_mro () {
337                 MRO m = new MRO ();
338                 GenericStruct<int> s = new GenericStruct<int> (5);
339                 // This generates stfld
340                 m.struct_field = s;
341
342                 // This generates ldflda
343                 if (m.struct_field.t != 5)
344                         return 1;
345
346                 // This generates ldfld
347                 GenericStruct<int> s2 = m.struct_field;
348                 if (s2.t != 5)
349                         return 2;
350
351                 if (m.struct_field.t != 5)
352                         return 3;
353
354                 m.class_field = new GenericClass<int> (5);
355                 if (m.class_field.t != 5)
356                         return 4;
357
358                 // gshared
359                 var m2 = new MRO<string> ();
360                 if (m2.stfld_ldfld ("A") != "A")
361                         return 5;
362
363                 return 0;
364         }
365
366         // FIXME:
367         [Category ("!FULLAOT")]
368     public static int test_0_generic_virtual_call_on_vtype_unbox () {
369                 object o = new Object ();
370         IFoo h = new Handler(o);
371
372         if (h.Bar<object> () != o)
373                         return 1;
374                 else
375                         return 0;
376     }
377
378         public static int test_0_box_brtrue_opt () {
379                 Foo<int> f = new Foo<int> (5);
380
381                 f [123] = 5;
382
383                 return 0;
384         }
385
386         public static int test_0_box_brtrue_opt_regress_81102 () {
387                 if (new Foo<int>(5).ToString () == "null")
388                         return 0;
389                 else
390                         return 1;
391         }
392
393         struct S {
394                 public int i;
395         }
396
397         public static int test_0_ldloca_initobj_opt () {
398                 if (new Foo<S> (new S ()).get_default ().i != 0)
399                         return 1;
400                 if (new Foo<object> (null).get_default () != null)
401                         return 2;
402                 return 0;
403         }
404
405 #if !__MOBILE__
406         public static int test_0_variance_reflection () {
407                 // covariance on IEnumerator
408                 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
409                         return 1;
410                 // covariance on IEnumerator and covariance on arrays
411                 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
412                         return 2;
413                 // covariance and implemented interfaces
414                 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
415                         return 3;
416
417                 // contravariance on IComparer
418                 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
419                         return 4;
420                 // contravariance on IComparer, contravariance on arrays
421                 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
422                         return 5;
423                 // contravariance and interface inheritance
424                 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
425                         return 6;
426                 return 0;
427         }
428 #endif
429
430         [Category ("!INTERPRETER")]
431         public static int test_0_ldvirtftn_generic_method () {
432                 new GenericsTests ().ldvirtftn<string> ();
433
434                 return the_type == typeof (string) ? 0 : 1;
435         }
436
437         public static int test_0_throw_dead_this () {
438         new Foo<string> ("").throw_dead_this ();
439                 return 0;
440         }
441
442         struct S<T> {}
443
444         public static int test_0_inline_infinite_polymorphic_recursion () {
445            f<int>(0);
446
447                    return 0;
448         }
449
450         private static void f<T>(int i) {
451                 if(i==42) f<S<T>>(i);
452         }
453
454         // This cannot be made to work with full-aot, since there it is impossible to
455         // statically determine that Foo<string>.Bar <int> is needed, the code only
456         // references IFoo.Bar<int>
457         [Category ("!INTERPRETER")]
458         [Category ("!FULLAOT")]
459         public static int test_0_generic_virtual_on_interfaces () {
460                 Foo<string>.count1 = 0;
461                 Foo<string>.count2 = 0;
462                 Foo<string>.count3 = 0;
463
464                 IFoo f = new Foo<string> ("");
465                 for (int i = 0; i < 1000; ++i) {
466                         f.Bar <int> ();
467                         f.Bar <string> ();
468                         f.NonGeneric ();
469                 }
470
471                 if (Foo<string>.count1 != 1000)
472                         return 1;
473                 if (Foo<string>.count2 != 1000)
474                         return 2;
475                 if (Foo<string>.count3 != 1000)
476                         return 3;
477
478                 VirtualInterfaceCallFromGenericMethod<long> (f);
479
480                 return 0;
481         }
482
483         [Category ("!INTERPRETER")]
484         public static int test_0_generic_virtual_on_interfaces_ref () {
485                 Foo<string>.count1 = 0;
486                 Foo<string>.count2 = 0;
487                 Foo<string>.count3 = 0;
488                 Foo<string>.count4 = 0;
489
490                 IFoo f = new Foo<string> ("");
491                 for (int i = 0; i < 1000; ++i) {
492                         f.Bar <string> ();
493                         f.Bar <object> ();
494                         f.NonGeneric ();
495                 }
496
497                 if (Foo<string>.count2 != 1000)
498                         return 2;
499                 if (Foo<string>.count3 != 1000)
500                         return 3;
501                 if (Foo<string>.count4 != 1000)
502                         return 4;
503
504                 return 0;
505         }
506
507         //repro for #505375
508         [Category ("!FULLAOT")]
509         public static int test_2_cprop_bug () {
510                 int idx = 0;
511                 int a = 1;
512                 var cmp = System.Collections.Generic.Comparer<int>.Default ;
513                 if (cmp.Compare (a, 0) > 0)
514                         a = 0;
515                 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
516                 return idx;
517         }
518
519         enum MyEnumUlong : ulong {
520                 Value_2 = 2
521         }
522
523         [Category ("!INTERPRETER")]
524         public static int test_0_regress_550964_constrained_enum_long () {
525         MyEnumUlong a = MyEnumUlong.Value_2;
526         MyEnumUlong b = MyEnumUlong.Value_2;
527
528         return Pan (a, b) ? 0 : 1;
529         }
530
531     static bool Pan<T> (T a, T b)
532     {
533         return a.Equals (b);
534     }
535
536         public class XElement {
537                 public string Value {
538                         get; set;
539                 }
540         }
541
542         [Category ("!INTERPRETER")]
543         public static int test_0_fullaot_linq () {
544                 var allWords = new XElement [] { new XElement { Value = "one" } };
545                 var filteredWords = allWords.Where(kw => kw.Value.StartsWith("T"));
546                 return filteredWords.Count ();
547         }
548
549         public static int test_0_fullaot_comparer_t () {
550                 var l = new SortedList <TimeSpan, int> ();
551                 return l.Count;
552         }
553
554         public static int test_0_fullaot_comparer_t_2 () {
555                 var l = new Dictionary <TimeSpan, int> ();
556                 return l.Count;
557         }
558
559         static void enumerate<T> (IEnumerable<T> arr) {
560                 foreach (var o in arr)
561                         ;
562                 int c = ((ICollection<T>)arr).Count;
563         }
564
565         [Category ("!INTERPRETER")]
566         /* Test that treating arrays as generic collections works with full-aot */
567         public static int test_0_fullaot_array_wrappers () {
568                 GenericsTests[] arr = new GenericsTests [10];
569                 enumerate<GenericsTests> (arr);
570                 return 0;
571         }
572
573         static int cctor_count = 0;
574
575     public abstract class Beta<TChanged> 
576     {           
577         static Beta()
578         {
579                         cctor_count ++;
580         }
581     }   
582     
583     public class Gamma<T> : Beta<T> 
584     {   
585         static Gamma()
586         {
587         }
588     }
589
590         // #519336    
591         public static int test_2_generic_class_init_gshared_ctor () {
592                 new Gamma<object>();
593                 new Gamma<string>();
594
595                 return cctor_count;
596         }
597
598         static int cctor_count2 = 0;
599
600         class ServiceController<T> {
601                 static ServiceController () {
602                         cctor_count2 ++;
603                 }
604
605                 public ServiceController () {
606                 }
607         }
608
609         static ServiceController<T> Create<T>() {
610                 return new ServiceController<T>();
611         }
612
613         // #631409
614         public static int test_2_generic_class_init_gshared_ctor_from_gshared () {
615                 Create<object> ();
616                 Create<string> ();
617
618                 return cctor_count2;
619         }
620
621         public static Type get_type<T> () {
622                 return typeof (T);
623         }
624
625         [Category ("!INTERPRETER")]
626         public static int test_0_gshared_delegate_rgctx () {
627                 Func<Type> t = new Func<Type> (get_type<string>);
628
629                 if (t () == typeof (string))
630                         return 0;
631                 else
632                         return 1;
633         }
634
635         [Category ("!INTERPRETER")]
636         // Creating a delegate from a generic method from gshared code
637         public static int test_0_gshared_delegate_from_gshared () {
638                 if (gshared_delegate_from_gshared <object> () != 0)
639                         return 1;
640                 if (gshared_delegate_from_gshared <string> () != 0)
641                         return 2;
642                 return 0;
643         }
644
645         public static int gshared_delegate_from_gshared <T> () {
646                 Func<Type> t = new Func<Type> (get_type<T>);
647
648                 return t () == typeof (T) ? 0 : 1;
649         }
650
651         public static int test_0_marshalbyref_call_from_gshared_virt_elim () {
652                 /* Calling a virtual method from gshared code which is changed to a nonvirt call */
653                 Class1<object> o = new Class1<object> ();
654                 o.Do (new Class2<object> ());
655                 return 0;
656         }
657
658         class Pair<TKey, TValue> {
659                 public static KeyValuePair<TKey, TValue> make_pair (TKey key, TValue value)
660                         {
661                                 return new KeyValuePair<TKey, TValue> (key, value);
662                         }
663
664                 public delegate TRet Transform<TRet> (TKey key, TValue value);
665         }
666
667         [Category ("!INTERPRETER")]
668         public static int test_0_bug_620864 () {
669                 var d = new Pair<string, Type>.Transform<KeyValuePair<string, Type>> (Pair<string, Type>.make_pair);
670
671                 var p = d ("FOO", typeof (int));
672                 if (p.Key != "FOO" || p.Value != typeof (int))
673                         return 1;
674
675                 return 0;
676         }
677
678
679         struct RecStruct<T> {
680                 public void foo (RecStruct<RecStruct<T>> baz) {
681                 }
682         }
683
684         public static int test_0_infinite_generic_recursion () {
685                 // Check that the AOT compile can deal with infinite generic recursion through
686                 // parameter types
687                 RecStruct<int> bla;
688
689                 return 0;
690         }
691
692         struct FooStruct {
693         }
694
695         bool IsNull2 <T> (object value) where T : struct {
696                 T? item = (T?) value;
697
698                 if (item.HasValue)
699                         return false;
700
701                 return true;
702         }
703
704         public static int test_0_full_aot_nullable_unbox_from_gshared_code () {
705                 if (!new GenericsTests ().IsNull2<FooStruct> (null))
706                         return 1;
707                 if (new GenericsTests ().IsNull2<FooStruct> (new FooStruct ()))
708                         return 2;
709                 return 0;
710         }
711
712         public static int test_0_partial_sharing () {
713                 if (PartialShared1 (new List<string> (), 1) != typeof (string))
714                         return 1;
715                 if (PartialShared1 (new List<GenericsTests> (), 1) != typeof (GenericsTests))
716                         return 2;
717                 if (PartialShared2 (new List<string> (), 1) != typeof (int))
718                         return 3;
719                 if (PartialShared2 (new List<GenericsTests> (), 1) != typeof (int))
720                         return 4;
721                 return 0;
722         }
723
724         [Category ("!INTERPRETER")]
725         [Category ("GSHAREDVT")]
726         public static int test_6_partial_sharing_linq () {
727                 var messages = new List<Message> ();
728
729                 messages.Add (new Message () { MessageID = 5 });
730                 messages.Add (new Message () { MessageID = 6 });
731
732                 return messages.Max(i => i.MessageID);
733         }
734
735         [Category ("!INTERPRETER")]
736         public static int test_0_partial_shared_method_in_nonshared_class () {
737                 var c = new Class1<double> ();
738                 return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
739         }
740
741         class Message {
742                 public int MessageID {
743                         get; set;
744                 }
745         }
746
747         public static Type PartialShared1<T, K> (List<T> list, K k) {
748                 return typeof (T);
749         }
750
751         public static Type PartialShared2<T, K> (List<T> list, K k) {
752                 return typeof (K);
753         }
754
755     public class Class1<T> {
756                 public virtual void Do (Class2<T> t) {
757                         t.Foo ();
758                 }
759
760                 public virtual object Foo<U> (T t) {
761                         return new Class1<U> ();
762                 }
763         }
764
765         public interface IFace1<T> {
766                 void Foo ();
767         }
768
769         public class Class2<T> : MarshalByRefObject, IFace1<T> {
770                 public void Foo () {
771                 }
772         }
773
774
775
776         public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
777                 f.Bar <T> ();
778         }
779
780         public static Type the_type;
781
782         public void ldvirtftn<T> () {
783                 Foo <T> binding = new Foo <T> (default (T));
784
785                 binding.GenericEvent += event_handler;
786                 binding.fire ();
787         }
788
789         public virtual void event_handler<T> (Foo<T> sender) {
790                 the_type = typeof (T);
791         }
792
793         public interface IFoo {
794                 void NonGeneric ();
795                 object Bar<T>();
796         }
797
798         public class Foo<T1> : IFoo
799         {
800                 public Foo(T1 t1)
801                 {
802                         m_t1 = t1;
803                 }
804                 
805                 public override string ToString()
806                 {
807                         return Bar(m_t1 == null ? "null" : "null");
808                 }
809
810                 public String Bar (String s) {
811                         return s;
812                 }
813
814                 public int this [T1 key] {
815                         set {
816                                 if (key == null)
817                                         throw new ArgumentNullException ("key");
818                         }
819                 }
820
821                 public void throw_dead_this () {
822                         try {
823                                 new SomeClass().ThrowAnException();
824                         }
825                         catch {
826                         }
827                 }
828
829                 public T1 get_default () {
830                         return default (T1);
831                 }
832                 
833                 readonly T1 m_t1;
834
835                 public delegate void GenericEventHandler (Foo<T1> sender);
836
837                 public event GenericEventHandler GenericEvent;
838
839                 public void fire () {
840                         GenericEvent (this);
841                 }
842
843                 public static int count1, count2, count3, count4;
844
845                 public void NonGeneric () {
846                         count3 ++;
847                 }
848
849                 public object Bar <T> () {
850                         if (typeof (T) == typeof (int))
851                                 count1 ++;
852                         else if (typeof (T) == typeof (string))
853                                 count2 ++;
854                         else if (typeof (T) == typeof (object))
855                                 count4 ++;
856                         return null;
857                 }
858         }
859
860         public class SomeClass {
861                 public void ThrowAnException() {
862                         throw new Exception ("Something went wrong");
863                 }
864         }               
865
866         struct Handler : IFoo {
867                 object o;
868
869                 public Handler(object o) {
870                         this.o = o;
871                 }
872
873                 public void NonGeneric () {
874                 }
875
876                 public object Bar<T>() {
877                         return o;
878                 }
879         }
880
881         static bool IsNull<T> (T t)
882         {
883                 if (t == null)
884                         return true;
885                 else
886                         return false;
887         }
888
889         static object Box<T> (T t)
890         {
891                 return t;
892         }
893         
894         static T Unbox <T> (object o) {
895                 return (T) o;
896         }
897
898         interface IDefaultRetriever
899         {
900                 T GetDefault<T>();
901         }
902
903         class DefaultRetriever : IDefaultRetriever
904         {
905                 [MethodImpl(MethodImplOptions.Synchronized)]
906                 public T GetDefault<T>()
907                 {
908                         return default(T);
909                 }
910         }
911
912         [Category ("!INTERPRETER")]
913         [Category ("!FULLAOT")]
914         [Category ("!BITCODE")]
915         public static int test_0_regress_668095_synchronized_gshared () {
916                 return DoSomething (new DefaultRetriever ());
917         }
918
919     static int DoSomething(IDefaultRetriever foo) {
920                 int result = foo.GetDefault<int>();
921                 return result;
922         }
923
924         class SyncClass<T> {
925                 [MethodImpl(MethodImplOptions.Synchronized)]
926                 public Type getInstance() {
927                         return typeof (T);
928                 }
929         }
930
931         [Category ("!INTERPRETER")]
932         [Category ("GSHAREDVT")]
933         static int test_0_synchronized_gshared () {
934                 var c = new SyncClass<string> ();
935                 if (c.getInstance () != typeof (string))
936                         return 1;
937                 return 0;
938         }
939
940         class Response {
941         }
942
943         public static int test_0_687865_isinst_with_cache_wrapper () {
944                 object o = new object ();
945                 if (o is Action<IEnumerable<Response>>)
946                         return 1;
947                 else
948                         return 0;
949         }
950
951         enum DocType {
952                 One,
953                 Two,
954                 Three
955         }
956
957         class Doc {
958                 public string Name {
959                         get; set;
960                 }
961
962                 public DocType Type {
963                         get; set;
964                 }
965         }
966
967         // #2155
968         [Category ("!INTERPRETER")]
969         [Category ("GSHAREDVT")]
970         public static int test_0_fullaot_sflda_cctor () {
971                 List<Doc> documents = new List<Doc>();
972                 documents.Add(new Doc { Name = "Doc1", Type = DocType.One } );
973                 documents.Add(new Doc { Name = "Doc2", Type = DocType.Two } );
974                 documents.Add(new Doc { Name = "Doc3", Type = DocType.Three } );
975                 documents.Add(new Doc { Name = "Doc4", Type = DocType.One } );
976                 documents.Add(new Doc { Name = "Doc5", Type = DocType.Two } );
977                 documents.Add(new Doc { Name = "Doc6", Type = DocType.Three } );
978                 documents.Add(new Doc { Name = "Doc7", Type = DocType.One } );
979                 documents.Add(new Doc { Name = "Doc8", Type = DocType.Two } );
980                 documents.Add(new Doc { Name = "Doc9", Type = DocType.Three } );
981
982                 List<DocType> categories = documents.Select(d=>d.Type).Distinct().ToList<DocType>().OrderBy(d => d).ToList();
983                 foreach(DocType cat in categories) {
984                         List<Doc> catDocs = documents.Where(d => d.Type == cat).OrderBy(d => d.Name).ToList<Doc>();
985                 }
986                 return 0;
987         }
988
989         class A { }
990
991     static List<A> sources = new List<A>();
992
993         // #6112
994         [Category ("!INTERPRETER")]
995     public static int test_0_fullaot_imt () {
996         sources.Add(null);
997         sources.Add(null);
998
999         int a = sources.Count;
1000         var enumerator = sources.GetEnumerator() as IEnumerator<object>;
1001
1002         while (enumerator.MoveNext())
1003         {
1004             object o = enumerator.Current;
1005         }
1006
1007                 return 0;
1008         }
1009
1010         class AClass {
1011         }
1012
1013         class BClass : AClass {
1014         }
1015
1016         [Category ("!INTERPRETER")]
1017         public static int test_0_fullaot_variant_iface () {
1018                 var arr = new BClass [10];
1019                 var enumerable = (IEnumerable<AClass>)arr;
1020                 enumerable.GetEnumerator ();
1021                 return 0;
1022         }
1023
1024         struct Record : Foo2<Record>.IRecord {
1025                 int counter;
1026                 int Foo2<Record>.IRecord.DoSomething () {
1027                         return counter++;
1028                 }
1029         }
1030
1031         class Foo2<T> where T : Foo2<T>.IRecord {
1032                 public interface IRecord {
1033                         int DoSomething ();
1034                 }
1035
1036                 public static int Extract (T[] t) {
1037                         return t[0].DoSomething ();
1038                 }
1039         }
1040
1041         class Foo3<T> where T : IComparable {
1042                 public static int CompareTo (T[] t) {
1043                         // This is a constrained call to Enum.CompareTo ()
1044                         return t[0].CompareTo (t [0]);
1045                 }
1046         }
1047
1048         [Category ("!INTERPRETER")]
1049         public static int test_1_regress_constrained_iface_call_7571 () {
1050         var r = new Record [10];
1051         Foo2<Record>.Extract (r);
1052                 return Foo2<Record>.Extract (r);
1053         }
1054
1055         enum ConstrainedEnum {
1056                 Val = 1
1057         }
1058
1059         [Category ("!INTERPRETER")]
1060         public static int test_0_regress_constrained_iface_call_enum () {
1061                 var r = new ConstrainedEnum [10];
1062                 return Foo3<ConstrainedEnum>.CompareTo (r);
1063         }
1064
1065         public interface IFoo2 {
1066                 void MoveNext ();
1067         }
1068
1069         public struct Foo2 : IFoo2 {
1070                 public void MoveNext () {
1071                 }
1072         }
1073
1074         public static Action Dingus (ref Foo2 f) {
1075                 return new Action (f.MoveNext);
1076         }
1077
1078         public static int test_0_delegate_unbox_full_aot () {
1079                 Foo2 foo = new Foo2 ();
1080                 Dingus (ref foo) ();
1081                 return 0;
1082         }
1083
1084         public static int test_0_arrays_ireadonly () {
1085                 int[] arr = new int [10];
1086                 for (int i = 0; i < 10; ++i)
1087                         arr [i] = i;
1088                 IReadOnlyList<int> a = (IReadOnlyList<int>)(object)arr;
1089                 if (a.Count != 10)
1090                         return 1;
1091                 if (a [0] != 0)
1092                         return 2;
1093                 if (a [1] != 1)
1094                         return 3;
1095                 return 0;
1096         }
1097
1098         public static int test_0_volatile_read_write () {
1099                 string foo = "ABC";
1100                 Volatile.Write (ref foo, "DEF");
1101                 return Volatile.Read (ref foo) == "DEF" ? 0 : 1;
1102         }
1103
1104         // FIXME: Doesn't work with --regression as Interlocked.Add(ref long) is only implemented as an intrinsic
1105 #if FALSE
1106         public static async Task<T> FooAsync<T> (int i, int j) {
1107                 Task<int> t = new Task<int> (delegate () { Console.WriteLine ("HIT!"); return 0; });
1108                 var response = await t;
1109                 return default(T);
1110         }
1111
1112         public static int test_0_fullaot_generic_async () {
1113                 Task<string> t = FooAsync<string> (1, 2);
1114                 t.RunSynchronously ();
1115                 return 0;
1116         }
1117 #endif
1118
1119         [Category ("!INTERPRETER")]
1120         public static int test_0_delegate_callvirt_fullaot () {
1121                 Func<string> f = delegate () { return "A"; };
1122         var f2 = (Func<Func<string>, string>)Delegate.CreateDelegate (typeof
1123 (Func<Func<string>, string>), null, f.GetType ().GetMethod ("Invoke"));
1124
1125         var s = f2 (f);
1126                 return s == "A" ? 0 : 1;
1127         }
1128
1129     public interface ICovariant<out R>
1130     {
1131     }
1132
1133     // Deleting the `out` modifier from this line stop the problem
1134     public interface IExtCovariant<out R> : ICovariant<R>
1135     {
1136     }
1137
1138     public class Sample<R> : ICovariant<R>
1139     {
1140     }
1141
1142     public interface IMyInterface
1143     {
1144     }
1145
1146         public static int test_0_variant_cast_cache () {
1147                 object covariant = new Sample<IMyInterface>();
1148
1149                 var foo = (ICovariant<IMyInterface>)(covariant);
1150
1151                 try {
1152                         var extCovariant = (IExtCovariant<IMyInterface>)covariant;
1153                         return 1;
1154                 } catch {
1155                         return 0;
1156                 }
1157         }
1158
1159         struct FooStruct2 {
1160                 public int a1, a2, a3;
1161         }
1162
1163         class MyClass<T> where T: struct {
1164                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1165                 public MyClass(int a1, int a2, int a3, int a4, int a5, int a6, Nullable<T> a) {
1166                 }
1167
1168                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1169                 public static MyClass<T> foo () {
1170                         Nullable<T> a = new Nullable<T> ();
1171                         return new MyClass<T> (0, 0, 0, 0, 0, 0, a);
1172                 }
1173         }
1174
1175         public static int test_0_newobj_generic_context () {
1176                 MyClass<FooStruct2>.foo ();
1177                 return 0;
1178         }
1179
1180         enum AnEnum {
1181                 A,
1182                 B
1183         }
1184
1185         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1186         public static string constrained_tostring<T> (T t) {
1187                 return t.ToString ();
1188         }
1189
1190         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1191         public static bool constrained_equals<T> (T t1, T t2) {
1192                 var c = EqualityComparer<T>.Default;
1193
1194                 return c.Equals (t1, t2);
1195         }
1196
1197         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1198         public static int constrained_gethashcode<T> (T t) {
1199                 return t.GetHashCode ();
1200         }
1201
1202         [Category ("!INTERPRETER")]
1203         public static int test_0_constrained_partial_sharing () {
1204                 string s;
1205
1206                 s = constrained_tostring<int> (5);
1207                 if (s != "5")
1208                         return 1;
1209                 s = constrained_tostring<AnEnum> (AnEnum.B);
1210                 if (s != "B")
1211                         return 2;
1212
1213                 if (!constrained_equals<int> (1, 1))
1214                         return 3;
1215                 if (constrained_equals<int> (1, 2))
1216                         return 4;
1217                 if (!constrained_equals<AnEnum> (AnEnum.A, AnEnum.A))
1218                         return 5;
1219                 if (constrained_equals<AnEnum> (AnEnum.A, AnEnum.B))
1220                         return 6;
1221
1222                 int i = constrained_gethashcode<int> (5);
1223                 if (i != 5)
1224                         return 7;
1225                 i = constrained_gethashcode<AnEnum> (AnEnum.B);
1226                 if (i != 1)
1227                         return 8;
1228                 return 0;
1229         }
1230
1231         enum Enum1 {
1232                 A,
1233                 B
1234         }
1235
1236         enum Enum2 {
1237                 A,
1238                 B
1239         }
1240
1241         public static int test_0_partial_sharing_ginst () {
1242                 var l1 = new List<KeyValuePair<int, Enum1>> ();
1243                 l1.Add (new KeyValuePair<int, Enum1>(5, Enum1.A));
1244                 if (l1 [0].Key != 5)
1245                         return 1;
1246                 if (l1 [0].Value != Enum1.A)
1247                         return 2;
1248                 var l2 = new List<KeyValuePair<int, Enum2>> ();
1249                 l2.Add (new KeyValuePair<int, Enum2>(5, Enum2.B));
1250                 if (l2 [0].Key != 5)
1251                         return 3;
1252                 if (l2 [0].Value != Enum2.B)
1253                         return 4;
1254                 return 0;
1255         }
1256
1257         static object delegate_8_args_res;
1258
1259         [Category ("!INTERPRETER")]
1260         public static int test_0_delegate_8_args () {
1261                 delegate_8_args_res = null;
1262                 Action<string, string, string, string, string, string, string,
1263                         string> test = (a, b, c, d, e, f, g, h) =>
1264             {
1265                                 delegate_8_args_res = h;
1266             };
1267                 test("a", "b", "c", "d", "e", "f", "g", "h");
1268                 return delegate_8_args_res == "h" ? 0 : 1;
1269         }
1270
1271         static void throw_catch_t<T> () where T: Exception {
1272                 try {
1273                         throw new NotSupportedException ();
1274                 } catch (T) {
1275                 }
1276         }
1277
1278         public static int test_0_gshared_catch_open_type () {
1279                 throw_catch_t<NotSupportedException> ();
1280                 return 0;
1281         }
1282
1283         class ThrowClass<T> where T: Exception {
1284                 public void throw_catch_t () {
1285                         try {
1286                                 throw new NotSupportedException ();
1287                         } catch (T) {
1288                         }
1289                 }
1290         }
1291
1292         public static int test_0_gshared_catch_open_type_instance () {
1293                 var c = new ThrowClass<NotSupportedException> ();
1294                 c.throw_catch_t ();
1295                 return 0;
1296         }
1297 }
1298
1299 #if !__MOBILE__
1300 class GenericsTests : Tests
1301 {
1302 }
1303 #endif