[runtime] Actually clean up context-static data segments.
[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         [Category ("GSHAREDVT")]
715         public static int test_6_partial_sharing_linq () {
716                 var messages = new List<Message> ();
717
718                 messages.Add (new Message () { MessageID = 5 });
719                 messages.Add (new Message () { MessageID = 6 });
720
721                 return messages.Max(i => i.MessageID);
722         }
723
724         public static int test_0_partial_shared_method_in_nonshared_class () {
725                 var c = new Class1<double> ();
726                 return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
727         }
728
729         class Message {
730                 public int MessageID {
731                         get; set;
732                 }
733         }
734
735         public static Type PartialShared1<T, K> (List<T> list, K k) {
736                 return typeof (T);
737         }
738
739         public static Type PartialShared2<T, K> (List<T> list, K k) {
740                 return typeof (K);
741         }
742
743     public class Class1<T> {
744                 public virtual void Do (Class2<T> t) {
745                         t.Foo ();
746                 }
747
748                 public virtual object Foo<U> (T t) {
749                         return new Class1<U> ();
750                 }
751         }
752
753         public interface IFace1<T> {
754                 void Foo ();
755         }
756
757         public class Class2<T> : MarshalByRefObject, IFace1<T> {
758                 public void Foo () {
759                 }
760         }
761
762
763
764         public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
765                 f.Bar <T> ();
766         }
767
768         public static Type the_type;
769
770         public void ldvirtftn<T> () {
771                 Foo <T> binding = new Foo <T> (default (T));
772
773                 binding.GenericEvent += event_handler;
774                 binding.fire ();
775         }
776
777         public virtual void event_handler<T> (Foo<T> sender) {
778                 the_type = typeof (T);
779         }
780
781         public interface IFoo {
782                 void NonGeneric ();
783                 object Bar<T>();
784         }
785
786         public class Foo<T1> : IFoo
787         {
788                 public Foo(T1 t1)
789                 {
790                         m_t1 = t1;
791                 }
792                 
793                 public override string ToString()
794                 {
795                         return Bar(m_t1 == null ? "null" : "null");
796                 }
797
798                 public String Bar (String s) {
799                         return s;
800                 }
801
802                 public int this [T1 key] {
803                         set {
804                                 if (key == null)
805                                         throw new ArgumentNullException ("key");
806                         }
807                 }
808
809                 public void throw_dead_this () {
810                         try {
811                                 new SomeClass().ThrowAnException();
812                         }
813                         catch {
814                         }
815                 }
816
817                 public T1 get_default () {
818                         return default (T1);
819                 }
820                 
821                 readonly T1 m_t1;
822
823                 public delegate void GenericEventHandler (Foo<T1> sender);
824
825                 public event GenericEventHandler GenericEvent;
826
827                 public void fire () {
828                         GenericEvent (this);
829                 }
830
831                 public static int count1, count2, count3, count4;
832
833                 public void NonGeneric () {
834                         count3 ++;
835                 }
836
837                 public object Bar <T> () {
838                         if (typeof (T) == typeof (int))
839                                 count1 ++;
840                         else if (typeof (T) == typeof (string))
841                                 count2 ++;
842                         else if (typeof (T) == typeof (object))
843                                 count4 ++;
844                         return null;
845                 }
846         }
847
848         public class SomeClass {
849                 public void ThrowAnException() {
850                         throw new Exception ("Something went wrong");
851                 }
852         }               
853
854         struct Handler : IFoo {
855                 object o;
856
857                 public Handler(object o) {
858                         this.o = o;
859                 }
860
861                 public void NonGeneric () {
862                 }
863
864                 public object Bar<T>() {
865                         return o;
866                 }
867         }
868
869         static bool IsNull<T> (T t)
870         {
871                 if (t == null)
872                         return true;
873                 else
874                         return false;
875         }
876
877         static object Box<T> (T t)
878         {
879                 return t;
880         }
881         
882         static T Unbox <T> (object o) {
883                 return (T) o;
884         }
885
886         interface IDefaultRetriever
887         {
888                 T GetDefault<T>();
889         }
890
891         class DefaultRetriever : IDefaultRetriever
892         {
893                 [MethodImpl(MethodImplOptions.Synchronized)]
894                 public T GetDefault<T>()
895                 {
896                         return default(T);
897                 }
898         }
899
900         [Category ("!FULLAOT")]
901         public static int test_0_regress_668095_synchronized_gshared () {
902                 return DoSomething (new DefaultRetriever ());
903         }
904
905     static int DoSomething(IDefaultRetriever foo) {
906                 int result = foo.GetDefault<int>();
907                 return result;
908         }
909
910         class SyncClass<T> {
911                 [MethodImpl(MethodImplOptions.Synchronized)]
912                 public Type getInstance() {
913                         return typeof (T);
914                 }
915         }
916
917         [Category ("GSHAREDVT")]
918         static int test_0_synchronized_gshared () {
919                 var c = new SyncClass<string> ();
920                 if (c.getInstance () != typeof (string))
921                         return 1;
922                 return 0;
923         }
924
925         class Response {
926         }
927
928         public static int test_0_687865_isinst_with_cache_wrapper () {
929                 object o = new object ();
930                 if (o is Action<IEnumerable<Response>>)
931                         return 1;
932                 else
933                         return 0;
934         }
935
936         enum DocType {
937                 One,
938                 Two,
939                 Three
940         }
941
942         class Doc {
943                 public string Name {
944                         get; set;
945                 }
946
947                 public DocType Type {
948                         get; set;
949                 }
950         }
951
952         // #2155
953         [Category ("GSHAREDVT")]
954         public static int test_0_fullaot_sflda_cctor () {
955                 List<Doc> documents = new List<Doc>();
956                 documents.Add(new Doc { Name = "Doc1", Type = DocType.One } );
957                 documents.Add(new Doc { Name = "Doc2", Type = DocType.Two } );
958                 documents.Add(new Doc { Name = "Doc3", Type = DocType.Three } );
959                 documents.Add(new Doc { Name = "Doc4", Type = DocType.One } );
960                 documents.Add(new Doc { Name = "Doc5", Type = DocType.Two } );
961                 documents.Add(new Doc { Name = "Doc6", Type = DocType.Three } );
962                 documents.Add(new Doc { Name = "Doc7", Type = DocType.One } );
963                 documents.Add(new Doc { Name = "Doc8", Type = DocType.Two } );
964                 documents.Add(new Doc { Name = "Doc9", Type = DocType.Three } );
965
966                 List<DocType> categories = documents.Select(d=>d.Type).Distinct().ToList<DocType>().OrderBy(d => d).ToList();
967                 foreach(DocType cat in categories) {
968                         List<Doc> catDocs = documents.Where(d => d.Type == cat).OrderBy(d => d.Name).ToList<Doc>();
969                 }
970                 return 0;
971         }
972
973         class A { }
974
975     static List<A> sources = new List<A>();
976
977         // #6112
978     public static int test_0_fullaot_imt () {
979         sources.Add(null);
980         sources.Add(null);
981
982         int a = sources.Count;
983         var enumerator = sources.GetEnumerator() as IEnumerator<object>;
984
985         while (enumerator.MoveNext())
986         {
987             object o = enumerator.Current;
988         }
989
990                 return 0;
991         }
992
993         struct Record : Foo2<Record>.IRecord {
994                 int counter;
995                 int Foo2<Record>.IRecord.DoSomething () {
996                         return counter++;
997                 }
998         }
999
1000         class Foo2<T> where T : Foo2<T>.IRecord {
1001                 public interface IRecord {
1002                         int DoSomething ();
1003                 }
1004
1005                 public static int Extract (T[] t) {
1006                         return t[0].DoSomething ();
1007                 }
1008         }
1009
1010         class Foo3<T> where T : IComparable {
1011                 public static int CompareTo (T[] t) {
1012                         // This is a constrained call to Enum.CompareTo ()
1013                         return t[0].CompareTo (t [0]);
1014                 }
1015         }
1016
1017         public static int test_1_regress_constrained_iface_call_7571 () {
1018         var r = new Record [10];
1019         Foo2<Record>.Extract (r);
1020                 return Foo2<Record>.Extract (r);
1021         }
1022
1023         enum ConstrainedEnum {
1024                 Val = 1
1025         }
1026
1027         public static int test_0_regress_constrained_iface_call_enum () {
1028                 var r = new ConstrainedEnum [10];
1029                 return Foo3<ConstrainedEnum>.CompareTo (r);
1030         }
1031
1032         public interface IFoo2 {
1033                 void MoveNext ();
1034         }
1035
1036         public struct Foo2 : IFoo2 {
1037                 public void MoveNext () {
1038                 }
1039         }
1040
1041         public static Action Dingus (ref Foo2 f) {
1042                 return new Action (f.MoveNext);
1043         }
1044
1045         public static int test_0_delegate_unbox_full_aot () {
1046                 Foo2 foo = new Foo2 ();
1047                 Dingus (ref foo) ();
1048                 return 0;
1049         }
1050
1051         public static int test_0_arrays_ireadonly () {
1052                 int[] arr = new int [10];
1053                 for (int i = 0; i < 10; ++i)
1054                         arr [i] = i;
1055                 IReadOnlyList<int> a = (IReadOnlyList<int>)(object)arr;
1056                 if (a.Count != 10)
1057                         return 1;
1058                 if (a [0] != 0)
1059                         return 2;
1060                 if (a [1] != 1)
1061                         return 3;
1062                 return 0;
1063         }
1064
1065         public static int test_0_volatile_read_write () {
1066                 string foo = "ABC";
1067                 Volatile.Write (ref foo, "DEF");
1068                 return Volatile.Read (ref foo) == "DEF" ? 0 : 1;
1069         }
1070
1071         // FIXME: Doesn't work with --regression as Interlocked.Add(ref long) is only implemented as an intrinsic
1072 #if FALSE
1073         public static async Task<T> FooAsync<T> (int i, int j) {
1074                 Task<int> t = new Task<int> (delegate () { Console.WriteLine ("HIT!"); return 0; });
1075                 var response = await t;
1076                 return default(T);
1077         }
1078
1079         public static int test_0_fullaot_generic_async () {
1080                 Task<string> t = FooAsync<string> (1, 2);
1081                 t.RunSynchronously ();
1082                 return 0;
1083         }
1084 #endif
1085
1086         public static int test_0_delegate_callvirt_fullaot () {
1087                 Func<string> f = delegate () { return "A"; };
1088         var f2 = (Func<Func<string>, string>)Delegate.CreateDelegate (typeof
1089 (Func<Func<string>, string>), null, f.GetType ().GetMethod ("Invoke"));
1090
1091         var s = f2 (f);
1092                 return s == "A" ? 0 : 1;
1093         }
1094
1095     public interface ICovariant<out R>
1096     {
1097     }
1098
1099     // Deleting the `out` modifier from this line stop the problem
1100     public interface IExtCovariant<out R> : ICovariant<R>
1101     {
1102     }
1103
1104     public class Sample<R> : ICovariant<R>
1105     {
1106     }
1107
1108     public interface IMyInterface
1109     {
1110     }
1111
1112         public static int test_0_variant_cast_cache () {
1113                 object covariant = new Sample<IMyInterface>();
1114
1115                 var foo = (ICovariant<IMyInterface>)(covariant);
1116
1117                 try {
1118                         var extCovariant = (IExtCovariant<IMyInterface>)covariant;
1119                         return 1;
1120                 } catch {
1121                         return 0;
1122                 }
1123         }
1124
1125         struct FooStruct2 {
1126                 public int a1, a2, a3;
1127         }
1128
1129         class MyClass<T> where T: struct {
1130                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1131                 public MyClass(int a1, int a2, int a3, int a4, int a5, int a6, Nullable<T> a) {
1132                 }
1133
1134                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1135                 public static MyClass<T> foo () {
1136                         Nullable<T> a = new Nullable<T> ();
1137                         return new MyClass<T> (0, 0, 0, 0, 0, 0, a);
1138                 }
1139         }
1140
1141         public static int test_0_newobj_generic_context () {
1142                 MyClass<FooStruct2>.foo ();
1143                 return 0;
1144         }
1145
1146         enum AnEnum {
1147                 A,
1148                 B
1149         }
1150
1151         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1152         public static string constrained_tostring<T> (T t) {
1153                 return t.ToString ();
1154         }
1155
1156         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1157         public static bool constrained_equals<T> (T t1, T t2) {
1158                 var c = EqualityComparer<T>.Default;
1159
1160                 return c.Equals (t1, t2);
1161         }
1162
1163         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1164         public static int constrained_gethashcode<T> (T t) {
1165                 return t.GetHashCode ();
1166         }
1167
1168         public static int test_0_constrained_partial_sharing () {
1169                 string s;
1170
1171                 s = constrained_tostring<int> (5);
1172                 if (s != "5")
1173                         return 1;
1174                 s = constrained_tostring<AnEnum> (AnEnum.B);
1175                 if (s != "B")
1176                         return 2;
1177
1178                 if (!constrained_equals<int> (1, 1))
1179                         return 3;
1180                 if (constrained_equals<int> (1, 2))
1181                         return 4;
1182                 if (!constrained_equals<AnEnum> (AnEnum.A, AnEnum.A))
1183                         return 5;
1184                 if (constrained_equals<AnEnum> (AnEnum.A, AnEnum.B))
1185                         return 6;
1186
1187                 int i = constrained_gethashcode<int> (5);
1188                 if (i != 5)
1189                         return 7;
1190                 i = constrained_gethashcode<AnEnum> (AnEnum.B);
1191                 if (i != 1)
1192                         return 8;
1193                 return 0;
1194         }
1195
1196         enum Enum1 {
1197                 A,
1198                 B
1199         }
1200
1201         enum Enum2 {
1202                 A,
1203                 B
1204         }
1205
1206         public static int test_0_partial_sharing_ginst () {
1207                 var l1 = new List<KeyValuePair<int, Enum1>> ();
1208                 l1.Add (new KeyValuePair<int, Enum1>(5, Enum1.A));
1209                 if (l1 [0].Key != 5)
1210                         return 1;
1211                 if (l1 [0].Value != Enum1.A)
1212                         return 2;
1213                 var l2 = new List<KeyValuePair<int, Enum2>> ();
1214                 l2.Add (new KeyValuePair<int, Enum2>(5, Enum2.B));
1215                 if (l2 [0].Key != 5)
1216                         return 3;
1217                 if (l2 [0].Value != Enum2.B)
1218                         return 4;
1219                 return 0;
1220         }
1221
1222         static object delegate_8_args_res;
1223
1224         public static int test_0_delegate_8_args () {
1225                 delegate_8_args_res = null;
1226                 Action<string, string, string, string, string, string, string,
1227                         string> test = (a, b, c, d, e, f, g, h) =>
1228             {
1229                                 delegate_8_args_res = h;
1230             };
1231                 test("a", "b", "c", "d", "e", "f", "g", "h");
1232                 return delegate_8_args_res == "h" ? 0 : 1;
1233         }
1234 }
1235
1236 #if !MOBILE
1237 class GenericsTests : Tests
1238 {
1239 }
1240 #endif