Merge pull request #1691 from esdrubal/exitevent
[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         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;
244                 }
245
246                 if (sum != 1000)
247                         return 1;
248
249                 s = GenericClass<TestStruct>.Z (arr, 0);
250                 if (s.i != 100 || s.j != 200)
251                         return 2;
252
253                 s = GenericClass<TestStruct>.Z (arr, 1);
254                 if (s.i != 300 || s.j != 400)
255                         return 3;
256
257                 return 0;
258         }
259
260         public static int test_0_nullable_ldflda () {
261                 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
262         }
263
264         public struct GenericStruct<T> {
265                 public T t;
266
267                 public GenericStruct (T t) {
268                         this.t = t;
269                 }
270         }
271
272         public class GenericClass<T> {
273                 public T t;
274
275                 public GenericClass (T t) {
276                         this.t = t;
277                 }
278
279                 public GenericClass () {
280                 }
281
282                 public T ldobj_stobj (ref T t1, ref T t2) {
283                         t1 = t2;
284                         T t = t1;
285
286                         return t;
287                 }
288
289                 public T ldelem_stelem (T t) {
290                         T[] arr = new T [10];
291                         arr [0] = t;
292
293                         return arr [0];
294                 }
295
296                 public String toString (T t) {
297                         return t.ToString ();
298                 }
299
300                 public static IEnumerator<T> Y (IEnumerable <T> x)
301                 {
302                         return x.GetEnumerator ();
303                 }
304
305                 public static T Z (IList<T> x, int index)
306                 {
307                         return x [index];
308                 }
309
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;
316             }
317         }
318         }
319
320         public class MRO : MarshalByRefObject {
321                 public GenericStruct<int> struct_field;
322                 public GenericClass<int> class_field;
323         }
324
325         public class MRO<T> : MarshalByRefObject {
326                 public T gen_field;
327
328                 public T stfld_ldfld (T t) {
329                         var m = this;
330                         m.gen_field = t;
331                         return m.gen_field;
332                 }
333         }
334
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;
340
341                 // This generates ldflda
342                 if (m.struct_field.t != 5)
343                         return 1;
344
345                 // This generates ldfld
346                 GenericStruct<int> s2 = m.struct_field;
347                 if (s2.t != 5)
348                         return 2;
349
350                 if (m.struct_field.t != 5)
351                         return 3;
352
353                 m.class_field = new GenericClass<int> (5);
354                 if (m.class_field.t != 5)
355                         return 4;
356
357                 // gshared
358                 var m2 = new MRO<string> ();
359                 if (m2.stfld_ldfld ("A") != "A")
360                         return 5;
361
362                 return 0;
363         }
364
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);
370
371         if (h.Bar<object> () != o)
372                         return 1;
373                 else
374                         return 0;
375     }
376
377         public static int test_0_box_brtrue_opt () {
378                 Foo<int> f = new Foo<int> (5);
379
380                 f [123] = 5;
381
382                 return 0;
383         }
384
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;
390         }
391
392         struct S {
393                 public int i;
394         }
395
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;
402         }
403
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;
415
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;
426         }
427 #endif
428
429         public static int test_0_ldvirtftn_generic_method () {
430                 new GenericsTests ().ldvirtftn<string> ();
431
432                 return the_type == typeof (string) ? 0 : 1;
433         }
434
435         public static int test_0_throw_dead_this () {
436         new Foo<string> ("").throw_dead_this ();
437                 return 0;
438         }
439
440         struct S<T> {}
441
442         public static int test_0_inline_infinite_polymorphic_recursion () {
443            f<int>(0);
444
445                    return 0;
446         }
447
448         private static void f<T>(int i) {
449                 if(i==42) f<S<T>>(i);
450         }
451
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;
460
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 ();
466                 }
467
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;
474
475                 VirtualInterfaceCallFromGenericMethod<long> (f);
476
477                 return 0;
478         }
479
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;
485
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 ();
491                 }
492
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;
499
500                 return 0;
501         }
502
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;
513         }
514
515         enum MyEnumUlong : ulong {
516                 Value_2 = 2
517         }
518
519         public static int test_0_regress_550964_constrained_enum_long () {
520         MyEnumUlong a = MyEnumUlong.Value_2;
521         MyEnumUlong b = MyEnumUlong.Value_2;
522
523         return Pan (a, b) ? 0 : 1;
524         }
525
526     static bool Pan<T> (T a, T b)
527     {
528         return a.Equals (b);
529     }
530
531         public class XElement {
532                 public string Value {
533                         get; set;
534                 }
535         }
536
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 ();
541         }
542
543         public static int test_0_fullaot_comparer_t () {
544                 var l = new SortedList <TimeSpan, int> ();
545                 return l.Count;
546         }
547
548         public static int test_0_fullaot_comparer_t_2 () {
549                 var l = new Dictionary <TimeSpan, int> ();
550                 return l.Count;
551         }
552
553         static void enumerate<T> (IEnumerable<T> arr) {
554                 foreach (var o in arr)
555                         ;
556                 int c = ((ICollection<T>)arr).Count;
557         }
558
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;
564         }
565
566         static int cctor_count = 0;
567
568     public abstract class Beta<TChanged> 
569     {           
570         static Beta()
571         {
572                         cctor_count ++;
573         }
574     }   
575     
576     public class Gamma<T> : Beta<T> 
577     {   
578         static Gamma()
579         {
580         }
581     }
582
583         // #519336    
584         public static int test_2_generic_class_init_gshared_ctor () {
585                 new Gamma<object>();
586                 new Gamma<string>();
587
588                 return cctor_count;
589         }
590
591         static int cctor_count2 = 0;
592
593         class ServiceController<T> {
594                 static ServiceController () {
595                         cctor_count2 ++;
596                 }
597
598                 public ServiceController () {
599                 }
600         }
601
602         static ServiceController<T> Create<T>() {
603                 return new ServiceController<T>();
604         }
605
606         // #631409
607         public static int test_2_generic_class_init_gshared_ctor_from_gshared () {
608                 Create<object> ();
609                 Create<string> ();
610
611                 return cctor_count2;
612         }
613
614         public static Type get_type<T> () {
615                 return typeof (T);
616         }
617
618         public static int test_0_gshared_delegate_rgctx () {
619                 Func<Type> t = new Func<Type> (get_type<string>);
620
621                 if (t () == typeof (string))
622                         return 0;
623                 else
624                         return 1;
625         }
626
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;
634         }
635
636         public static int gshared_delegate_from_gshared <T> () {
637                 Func<Type> t = new Func<Type> (get_type<T>);
638
639                 return t () == typeof (T) ? 0 : 1;
640         }
641
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;
647         }
648
649         class Pair<TKey, TValue> {
650                 public static KeyValuePair<TKey, TValue> make_pair (TKey key, TValue value)
651                         {
652                                 return new KeyValuePair<TKey, TValue> (key, value);
653                         }
654
655                 public delegate TRet Transform<TRet> (TKey key, TValue value);
656         }
657
658         public static int test_0_bug_620864 () {
659                 var d = new Pair<string, Type>.Transform<KeyValuePair<string, Type>> (Pair<string, Type>.make_pair);
660
661                 var p = d ("FOO", typeof (int));
662                 if (p.Key != "FOO" || p.Value != typeof (int))
663                         return 1;
664
665                 return 0;
666         }
667
668
669         struct RecStruct<T> {
670                 public void foo (RecStruct<RecStruct<T>> baz) {
671                 }
672         }
673
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;
678
679                 return 0;
680         }
681
682         struct FooStruct {
683         }
684
685         bool IsNull2 <T> (object value) where T : struct {
686                 T? item = (T?) value;
687
688                 if (item.HasValue)
689                         return false;
690
691                 return true;
692         }
693
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;
700         }
701
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;
712         }
713
714         public static int test_6_partial_sharing_linq () {
715                 var messages = new List<Message> ();
716
717                 messages.Add (new Message () { MessageID = 5 });
718                 messages.Add (new Message () { MessageID = 6 });
719
720                 return messages.Max(i => i.MessageID);
721         }
722
723         public static int test_0_partial_shared_method_in_nonshared_class () {
724                 var c = new Class1<double> ();
725                 return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
726         }
727
728         class Message {
729                 public int MessageID {
730                         get; set;
731                 }
732         }
733
734         public static Type PartialShared1<T, K> (List<T> list, K k) {
735                 return typeof (T);
736         }
737
738         public static Type PartialShared2<T, K> (List<T> list, K k) {
739                 return typeof (K);
740         }
741
742     public class Class1<T> {
743                 public virtual void Do (Class2<T> t) {
744                         t.Foo ();
745                 }
746
747                 public virtual object Foo<U> (T t) {
748                         return new Class1<U> ();
749                 }
750         }
751
752         public interface IFace1<T> {
753                 void Foo ();
754         }
755
756         public class Class2<T> : MarshalByRefObject, IFace1<T> {
757                 public void Foo () {
758                 }
759         }
760
761
762
763         public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
764                 f.Bar <T> ();
765         }
766
767         public static Type the_type;
768
769         public void ldvirtftn<T> () {
770                 Foo <T> binding = new Foo <T> (default (T));
771
772                 binding.GenericEvent += event_handler;
773                 binding.fire ();
774         }
775
776         public virtual void event_handler<T> (Foo<T> sender) {
777                 the_type = typeof (T);
778         }
779
780         public interface IFoo {
781                 void NonGeneric ();
782                 object Bar<T>();
783         }
784
785         public class Foo<T1> : IFoo
786         {
787                 public Foo(T1 t1)
788                 {
789                         m_t1 = t1;
790                 }
791                 
792                 public override string ToString()
793                 {
794                         return Bar(m_t1 == null ? "null" : "null");
795                 }
796
797                 public String Bar (String s) {
798                         return s;
799                 }
800
801                 public int this [T1 key] {
802                         set {
803                                 if (key == null)
804                                         throw new ArgumentNullException ("key");
805                         }
806                 }
807
808                 public void throw_dead_this () {
809                         try {
810                                 new SomeClass().ThrowAnException();
811                         }
812                         catch {
813                         }
814                 }
815
816                 public T1 get_default () {
817                         return default (T1);
818                 }
819                 
820                 readonly T1 m_t1;
821
822                 public delegate void GenericEventHandler (Foo<T1> sender);
823
824                 public event GenericEventHandler GenericEvent;
825
826                 public void fire () {
827                         GenericEvent (this);
828                 }
829
830                 public static int count1, count2, count3, count4;
831
832                 public void NonGeneric () {
833                         count3 ++;
834                 }
835
836                 public object Bar <T> () {
837                         if (typeof (T) == typeof (int))
838                                 count1 ++;
839                         else if (typeof (T) == typeof (string))
840                                 count2 ++;
841                         else if (typeof (T) == typeof (object))
842                                 count4 ++;
843                         return null;
844                 }
845         }
846
847         public class SomeClass {
848                 public void ThrowAnException() {
849                         throw new Exception ("Something went wrong");
850                 }
851         }               
852
853         struct Handler : IFoo {
854                 object o;
855
856                 public Handler(object o) {
857                         this.o = o;
858                 }
859
860                 public void NonGeneric () {
861                 }
862
863                 public object Bar<T>() {
864                         return o;
865                 }
866         }
867
868         static bool IsNull<T> (T t)
869         {
870                 if (t == null)
871                         return true;
872                 else
873                         return false;
874         }
875
876         static object Box<T> (T t)
877         {
878                 return t;
879         }
880         
881         static T Unbox <T> (object o) {
882                 return (T) o;
883         }
884
885         interface IDefaultRetriever
886         {
887                 T GetDefault<T>();
888         }
889
890         class DefaultRetriever : IDefaultRetriever
891         {
892                 [MethodImpl(MethodImplOptions.Synchronized)]
893                 public T GetDefault<T>()
894                 {
895                         return default(T);
896                 }
897         }
898
899         [Category ("!FULLAOT")]
900         public static int test_0_regress_668095_synchronized_gshared () {
901                 return DoSomething (new DefaultRetriever ());
902         }
903
904     static int DoSomething(IDefaultRetriever foo) {
905                 int result = foo.GetDefault<int>();
906                 return result;
907         }
908
909         class SyncClass<T> {
910                 [MethodImpl(MethodImplOptions.Synchronized)]
911                 public Type getInstance() {
912                         return typeof (T);
913                 }
914         }
915
916         [Category ("GSHAREDVT")]
917         static int test_0_synchronized_gshared () {
918                 var c = new SyncClass<string> ();
919                 if (c.getInstance () != typeof (string))
920                         return 1;
921                 return 0;
922         }
923
924         class Response {
925         }
926
927         public static int test_0_687865_isinst_with_cache_wrapper () {
928                 object o = new object ();
929                 if (o is Action<IEnumerable<Response>>)
930                         return 1;
931                 else
932                         return 0;
933         }
934
935         enum DocType {
936                 One,
937                 Two,
938                 Three
939         }
940
941         class Doc {
942                 public string Name {
943                         get; set;
944                 }
945
946                 public DocType Type {
947                         get; set;
948                 }
949         }
950
951         // #2155
952         public static int test_0_fullaot_sflda_cctor () {
953                 List<Doc> documents = new List<Doc>();
954                 documents.Add(new Doc { Name = "Doc1", Type = DocType.One } );
955                 documents.Add(new Doc { Name = "Doc2", Type = DocType.Two } );
956                 documents.Add(new Doc { Name = "Doc3", Type = DocType.Three } );
957                 documents.Add(new Doc { Name = "Doc4", Type = DocType.One } );
958                 documents.Add(new Doc { Name = "Doc5", Type = DocType.Two } );
959                 documents.Add(new Doc { Name = "Doc6", Type = DocType.Three } );
960                 documents.Add(new Doc { Name = "Doc7", Type = DocType.One } );
961                 documents.Add(new Doc { Name = "Doc8", Type = DocType.Two } );
962                 documents.Add(new Doc { Name = "Doc9", Type = DocType.Three } );
963
964                 List<DocType> categories = documents.Select(d=>d.Type).Distinct().ToList<DocType>().OrderBy(d => d).ToList();
965                 foreach(DocType cat in categories) {
966                         List<Doc> catDocs = documents.Where(d => d.Type == cat).OrderBy(d => d.Name).ToList<Doc>();
967                 }
968                 return 0;
969         }
970
971         class A { }
972
973     static List<A> sources = new List<A>();
974
975         // #6112
976     public static int test_0_fullaot_imt () {
977         sources.Add(null);
978         sources.Add(null);
979
980         int a = sources.Count;
981         var enumerator = sources.GetEnumerator() as IEnumerator<object>;
982
983         while (enumerator.MoveNext())
984         {
985             object o = enumerator.Current;
986         }
987
988                 return 0;
989         }
990
991         struct Record : Foo2<Record>.IRecord {
992                 int counter;
993                 int Foo2<Record>.IRecord.DoSomething () {
994                         return counter++;
995                 }
996         }
997
998         class Foo2<T> where T : Foo2<T>.IRecord {
999                 public interface IRecord {
1000                         int DoSomething ();
1001                 }
1002
1003                 public static int Extract (T[] t) {
1004                         return t[0].DoSomething ();
1005                 }
1006         }
1007
1008         class Foo3<T> where T : IComparable {
1009                 public static int CompareTo (T[] t) {
1010                         // This is a constrained call to Enum.CompareTo ()
1011                         return t[0].CompareTo (t [0]);
1012                 }
1013         }
1014
1015         public static int test_1_regress_constrained_iface_call_7571 () {
1016         var r = new Record [10];
1017         Foo2<Record>.Extract (r);
1018                 return Foo2<Record>.Extract (r);
1019         }
1020
1021         enum ConstrainedEnum {
1022                 Val = 1
1023         }
1024
1025         public static int test_0_regress_constrained_iface_call_enum () {
1026                 var r = new ConstrainedEnum [10];
1027                 return Foo3<ConstrainedEnum>.CompareTo (r);
1028         }
1029
1030         public interface IFoo2 {
1031                 void MoveNext ();
1032         }
1033
1034         public struct Foo2 : IFoo2 {
1035                 public void MoveNext () {
1036                 }
1037         }
1038
1039         public static Action Dingus (ref Foo2 f) {
1040                 return new Action (f.MoveNext);
1041         }
1042
1043         public static int test_0_delegate_unbox_full_aot () {
1044                 Foo2 foo = new Foo2 ();
1045                 Dingus (ref foo) ();
1046                 return 0;
1047         }
1048
1049         public static int test_0_arrays_ireadonly () {
1050                 int[] arr = new int [10];
1051                 for (int i = 0; i < 10; ++i)
1052                         arr [i] = i;
1053                 IReadOnlyList<int> a = (IReadOnlyList<int>)(object)arr;
1054                 if (a.Count != 10)
1055                         return 1;
1056                 if (a [0] != 0)
1057                         return 2;
1058                 if (a [1] != 1)
1059                         return 3;
1060                 return 0;
1061         }
1062
1063         public static int test_0_volatile_read_write () {
1064                 string foo = "ABC";
1065                 Volatile.Write (ref foo, "DEF");
1066                 return Volatile.Read (ref foo) == "DEF" ? 0 : 1;
1067         }
1068
1069         // FIXME: Doesn't work with --regression as Interlocked.Add(ref long) is only implemented as an intrinsic
1070 #if FALSE
1071         public static async Task<T> FooAsync<T> (int i, int j) {
1072                 Task<int> t = new Task<int> (delegate () { Console.WriteLine ("HIT!"); return 0; });
1073                 var response = await t;
1074                 return default(T);
1075         }
1076
1077         public static int test_0_fullaot_generic_async () {
1078                 Task<string> t = FooAsync<string> (1, 2);
1079                 t.RunSynchronously ();
1080                 return 0;
1081         }
1082 #endif
1083
1084         public static int test_0_delegate_callvirt_fullaot () {
1085                 Func<string> f = delegate () { return "A"; };
1086         var f2 = (Func<Func<string>, string>)Delegate.CreateDelegate (typeof
1087 (Func<Func<string>, string>), null, f.GetType ().GetMethod ("Invoke"));
1088
1089         var s = f2 (f);
1090                 return s == "A" ? 0 : 1;
1091         }
1092
1093     public interface ICovariant<out R>
1094     {
1095     }
1096
1097     // Deleting the `out` modifier from this line stop the problem
1098     public interface IExtCovariant<out R> : ICovariant<R>
1099     {
1100     }
1101
1102     public class Sample<R> : ICovariant<R>
1103     {
1104     }
1105
1106     public interface IMyInterface
1107     {
1108     }
1109
1110         public static int test_0_variant_cast_cache () {
1111                 object covariant = new Sample<IMyInterface>();
1112
1113                 var foo = (ICovariant<IMyInterface>)(covariant);
1114
1115                 try {
1116                         var extCovariant = (IExtCovariant<IMyInterface>)covariant;
1117                         return 1;
1118                 } catch {
1119                         return 0;
1120                 }
1121         }
1122
1123         struct FooStruct2 {
1124                 public int a1, a2, a3;
1125         }
1126
1127         class MyClass<T> where T: struct {
1128                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1129                 public MyClass(int a1, int a2, int a3, int a4, int a5, int a6, Nullable<T> a) {
1130                 }
1131
1132                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1133                 public static MyClass<T> foo () {
1134                         Nullable<T> a = new Nullable<T> ();
1135                         return new MyClass<T> (0, 0, 0, 0, 0, 0, a);
1136                 }
1137         }
1138
1139         public static int test_0_newobj_generic_context () {
1140                 MyClass<FooStruct2>.foo ();
1141                 return 0;
1142         }
1143
1144         enum AnEnum {
1145                 A,
1146                 B
1147         }
1148
1149         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1150         public static string constrained_tostring<T> (T t) {
1151                 return t.ToString ();
1152         }
1153
1154         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1155         public static bool constrained_equals<T> (T t1, T t2) {
1156                 var c = EqualityComparer<T>.Default;
1157
1158                 return c.Equals (t1, t2);
1159         }
1160
1161         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1162         public static int constrained_gethashcode<T> (T t) {
1163                 return t.GetHashCode ();
1164         }
1165
1166         public static int test_0_constrained_partial_sharing () {
1167                 string s;
1168
1169                 s = constrained_tostring<int> (5);
1170                 if (s != "5")
1171                         return 1;
1172                 s = constrained_tostring<AnEnum> (AnEnum.B);
1173                 if (s != "B")
1174                         return 2;
1175
1176                 if (!constrained_equals<int> (1, 1))
1177                         return 3;
1178                 if (constrained_equals<int> (1, 2))
1179                         return 4;
1180                 if (!constrained_equals<AnEnum> (AnEnum.A, AnEnum.A))
1181                         return 5;
1182                 if (constrained_equals<AnEnum> (AnEnum.A, AnEnum.B))
1183                         return 6;
1184
1185                 int i = constrained_gethashcode<int> (5);
1186                 if (i != 5)
1187                         return 7;
1188                 i = constrained_gethashcode<AnEnum> (AnEnum.B);
1189                 if (i != 1)
1190                         return 8;
1191                 return 0;
1192         }
1193
1194         enum Enum1 {
1195                 A,
1196                 B
1197         }
1198
1199         enum Enum2 {
1200                 A,
1201                 B
1202         }
1203
1204         public static int test_0_partial_sharing_ginst () {
1205                 var l1 = new List<KeyValuePair<int, Enum1>> ();
1206                 l1.Add (new KeyValuePair<int, Enum1>(5, Enum1.A));
1207                 if (l1 [0].Key != 5)
1208                         return 1;
1209                 if (l1 [0].Value != Enum1.A)
1210                         return 2;
1211                 var l2 = new List<KeyValuePair<int, Enum2>> ();
1212                 l2.Add (new KeyValuePair<int, Enum2>(5, Enum2.B));
1213                 if (l2 [0].Key != 5)
1214                         return 3;
1215                 if (l2 [0].Value != Enum2.B)
1216                         return 4;
1217                 return 0;
1218         }
1219
1220 }
1221
1222 #if !MOBILE
1223 class GenericsTests : Tests
1224 {
1225 }
1226 #endif