Add support for constrained gsharedvt calls to IComparable<T>.CompareTo ()/IEquatable...
[mono.git] / mono / mini / gshared.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Runtime.CompilerServices;
4 using System.Threading.Tasks;
5
6 struct Foo {
7         public int i, j, k, l, m, n;
8 }
9
10 struct GFoo<T> {
11         public T dummy;
12         public T t;
13         public int i;
14         public Foo f;
15         public static T static_dummy;
16         public static T static_t;
17         public static Foo static_f;
18 }
19
20 struct GFoo2<T> {
21         public T t, t2;
22 }
23
24 class GFoo3<T> {
25         public T t, t2;
26
27         public GFoo3 () {
28         }
29
30         [MethodImplAttribute (MethodImplOptions.NoInlining)]
31         public GFoo3 (T i1, T i2) {
32                 t = i1;
33                 t2 = i2;
34         }
35 }
36
37 //
38 // Tests for generic sharing of vtypes.
39 // The tests use arrays to pass/receive values to keep the calling convention of the methods stable, which is a current limitation of the runtime support for gsharedvt.
40 //
41
42 // FIXME: Add mixed ref/noref tests, i.e. Dictionary<string, int>
43
44 #if MOBILE
45 public class GSharedTests
46 #else
47 public class Tests
48 #endif
49 {
50 #if !MOBILE
51         public static int Main (String[] args) {
52                 return TestDriver.RunTests (typeof (Tests), args);
53         }
54 #endif
55
56         [MethodImplAttribute (MethodImplOptions.NoInlining)]
57         static void gshared<T> (T [] array, int i, int j) {
58                 T tmp = array [i];
59                 array [i] = array [j];
60                 array [j] = tmp;
61         }
62
63         // Test that the gshared and gsharedvt versions don't mix
64         public static int test_0_vt_gshared () {
65                 string[] sarr = new string [2] { "A", "B" };
66
67                 gshared<string> (sarr, 0, 1);
68
69                 Foo[] arr = new Foo [2];
70                 arr [0] = new Foo () { i = 1, j = 2 };
71                 arr [1] = new Foo () { i = 3, j = 4 };
72
73                 gshared<Foo> (arr, 0, 1);
74                 if (arr [0].i != 3 || arr [0].j != 4)
75                         return 1;
76                 if (arr [1].i != 1 || arr [1].j != 2)
77                         return 2;
78
79                 return 0;
80         }
81
82         static void ldelem_stelem<T> (T [] array, int i, int j) {
83                 T tmp = array [i];
84                 array [i] = array [j];
85                 array [j] = tmp;
86         }
87
88         public static int test_0_vt_ldelem_stelem () {
89                 Foo[] arr = new Foo [2];
90                 arr [0] = new Foo () { i = 1, j = 2 };
91                 arr [1] = new Foo () { i = 3, j = 4 };
92
93                 ldelem_stelem<Foo> (arr, 0, 1);
94                 if (arr [0].i != 3 || arr [0].j != 4)
95                         return 1;
96                 if (arr [1].i != 1 || arr [1].j != 2)
97                         return 2;
98
99                 int[] arr2 = new int [2] { 1, 2 };
100                 ldelem_stelem<int> (arr2, 0, 1);
101                 if (arr2 [0] !=2 || arr2 [1] != 1)
102                         return 3;
103
104                 return 0;
105         }
106
107         [MethodImplAttribute (MethodImplOptions.NoInlining)]
108         private static void initobj<T> (T [] array, int i, int j) {
109                 T x = default(T);
110                 array [i] = x;
111         }
112
113         public static int test_0_vt_initobj () {
114                 Foo[] arr = new Foo [2];
115                 arr [0] = new Foo () { i = 1, j = 2 };
116                 arr [1] = new Foo () { i = 3, j = 4 };
117
118                 initobj<Foo> (arr, 0, 1);
119                 if (arr [0].i != 0 || arr [0].j != 0)
120                         return 1;
121                 if (arr [1].i != 3 || arr [1].j != 4)
122                         return 2;
123                 return 0;
124         }
125
126         [MethodImplAttribute (MethodImplOptions.NoInlining)]
127         static T ldobj_stobj<T> (ref T t1, ref T t2) {
128                 t1 = t2;
129                 T t = t2;
130                 t2 = default(T);
131                 return t;
132         }
133
134         public static int test_0_vt_ldobj_stobj () {
135                 int i = 5;
136                 int j = 6;
137                 if (ldobj_stobj (ref i, ref j) != 6)
138                         return 1;
139                 if (i != 6 || j != 0)
140                         return 2;
141                 double d1 = 1.0;
142                 double d2 = 2.0;
143                 if (ldobj_stobj (ref d1, ref d2) != 2.0)
144                         return 3;
145                 if (d1 != 2.0 || d2 != 0.0)
146                         return 4;               
147                 return 0;
148         }
149
150         [MethodImplAttribute (MethodImplOptions.NoInlining)]
151         private static void box<T1, T> (T [] array, object[] arr) {
152                 object x = array [0];
153                 arr [0] = x;
154         }
155
156         public static int test_0_vt_box () {
157                 Foo[] arr = new Foo [2];
158                 arr [0] = new Foo () { i = 1, j = 2 };
159
160                 object[] arr2 = new object [16];
161                 box<int, Foo> (arr, arr2);
162                 if (arr2 [0].GetType () != typeof (Foo))
163                         return 1;
164                 Foo f = (Foo)arr2 [0];
165                 if (f.i != 1 || f.j != 2)
166                         return 2;
167                 string[] arr3 = new string [16];
168                 object[] arr4 = new object [16];
169                 arr3 [0] = "OK";
170                 box<int, string> (arr3, arr4);
171                 if (arr4 [0] != (object)arr3 [0])
172                         return 3;
173                 return 0;
174         }
175
176         [MethodImplAttribute (MethodImplOptions.NoInlining)]
177         private static void unbox_any<T> (T [] array, object[] arr) {
178                 T t = (T)arr [0];
179                 array [0] = t;
180         }
181
182         public static int test_0_vt_unbox_any () {
183                 int[] iarr = new int [16];
184                 unbox_any<int> (iarr, new object [] { 12 });
185
186                 Foo[] arr = new Foo [2];
187
188                 object[] arr2 = new object [16];
189                 arr2 [0] = new Foo () { i = 1, j = 2 };
190                 unbox_any<Foo> (arr, arr2);
191                 if (arr [0].i != 1 || arr [0].j != 2)
192                         return 2;
193                 return 0;
194         }
195
196         interface IFaceUnbox {
197                 T Unbox<T, T2> (T t, T2 t2, object o);
198         }
199
200         class ClassUnbox : IFaceUnbox {
201                 public T Unbox<T, T2> (T t, T2 t2, object o) {
202                         return (T)o;
203                 }
204         }
205
206         // unbox.any on a ref type in a gsharedvt method
207         public static int test_0_ref_gsharedvt_aot_unbox_any () {
208                 IFaceUnbox iface = new ClassUnbox ();
209                 string s = iface.Unbox<string, int> ("A", 2, "A");
210                 if (s != "A")
211                         return 1;
212                 return 0;
213         }
214
215         public static int test_0_unbox_any_enum () {
216                 IFaceUnbox iface = new ClassUnbox ();
217                 AnEnum res = iface.Unbox<AnEnum, int> (AnEnum.One, 0, 1);
218                 return res == AnEnum.Two ? 0 : 1;
219         }
220
221         [MethodImplAttribute (MethodImplOptions.NoInlining)]
222         static void ldfld_nongeneric<T> (GFoo<T>[] foo, int[] arr) {
223                 arr [0] = foo [0].i;
224         }
225
226         [MethodImplAttribute (MethodImplOptions.NoInlining)]
227         static void ldfld<T> (GFoo<T>[] foo, T[] arr) {
228                 arr [0] = foo [0].t;
229         }
230
231         [MethodImplAttribute (MethodImplOptions.NoInlining)]
232         static void stfld_nongeneric<T> (GFoo<T>[] foo, int[] arr) {
233                 foo [0].i = arr [0];
234         }
235
236         [MethodImplAttribute (MethodImplOptions.NoInlining)]
237         static void stfld<T> (GFoo<T>[] foo, T[] arr) {
238                 foo [0].t = arr [0];
239         }
240
241         [MethodImplAttribute (MethodImplOptions.NoInlining)]
242         static void ldflda<T> (GFoo<T>[] foo, int[] arr) {
243                 arr [0] = foo [0].f.i;
244         }
245
246         public static int test_0_vt_ldfld_stfld () {
247                 var foo = new GFoo<Foo> () { t = new Foo () { i = 1, j = 2 }, i = 5, f = new Foo () { i = 5, j = 6 } };
248                 var farr = new GFoo<Foo>[] { foo };
249
250                 /* Normal fields with a variable offset */
251                 var iarr = new int [10];
252                 ldfld_nongeneric<Foo> (farr, iarr);
253                 if (iarr [0] != 5)
254                         return 1;
255                 iarr [0] = 16;
256                 stfld_nongeneric<Foo> (farr, iarr);
257                 if (farr [0].i != 16)
258                         return 2;
259
260                 /* Variable type field with a variable offset */
261                 var arr = new Foo [10];
262                 ldfld<Foo> (farr, arr);
263                 if (arr [0].i != 1 || arr [0].j != 2)
264                         return 3;
265                 arr [0] = new Foo () { i = 3, j = 4 };
266                 stfld<Foo> (farr, arr);
267                 if (farr [0].t.i != 3 || farr [0].t.j != 4)
268                         return 4;
269
270                 ldflda<Foo> (farr, iarr);
271                 if (iarr [0] != 5)
272                         return 5;
273
274                 return 0;
275         }
276
277         [MethodImplAttribute (MethodImplOptions.NoInlining)]
278         static void stsfld<T> (T[] arr) {
279                 GFoo<T>.static_t = arr [0];
280         }
281
282         [MethodImplAttribute (MethodImplOptions.NoInlining)]
283         static void ldsfld<T> (T[] arr) {
284                 arr [0] = GFoo<T>.static_t;
285         }
286
287         [MethodImplAttribute (MethodImplOptions.NoInlining)]
288         static void ldsflda<T> (int[] iarr) {
289                 iarr [0] = GFoo<T>.static_f.i;
290         }
291         
292         public static int test_0_stsfld () {
293                 Foo[] farr = new Foo [] { new Foo () { i = 1, j = 2 } };
294                 stsfld<Foo> (farr);
295
296                 if (GFoo<Foo>.static_t.i != 1 || GFoo<Foo>.static_t.j != 2)
297                         return 1;
298
299                 Foo[] farr2 = new Foo [1];
300                 ldsfld<Foo> (farr2);
301                 if (farr2 [0].i != 1 || farr2 [0].j != 2)
302                         return 2;
303
304                 var iarr = new int [10];
305                 GFoo<Foo>.static_f = new Foo () { i = 5, j = 6 };
306                 ldsflda<Foo> (iarr);
307                 if (iarr [0] != 5)
308                         return 3;
309
310                 return 0;
311         }
312
313         [MethodImplAttribute (MethodImplOptions.NoInlining)]
314         static object newarr<T> () {
315                 object o = new T[10];
316                 return o;
317         }
318
319         public static int test_0_vt_newarr () {
320                 object o = newarr<Foo> ();
321                 if (!(o is Foo[]))
322                         return 1;
323                 return 0;
324         }
325
326         [MethodImplAttribute (MethodImplOptions.NoInlining)]
327         static Type ldtoken<T> () {
328                 return typeof (GFoo<T>);
329         }
330
331         public static int test_0_vt_ldtoken () {
332                 Type t = ldtoken<Foo> ();
333                 if (t != typeof (GFoo<Foo>))
334                         return 1;
335                 t = ldtoken<int> ();
336                 if (t != typeof (GFoo<int>))
337                         return 2;
338
339                 return 0;
340         }
341
342         public static int test_0_vtype_list () {
343                 List<int> l = new List<int> ();
344
345                 l.Add (5);
346                 if (l.Count != 1)
347                         return 1;
348                 return 0;
349         }
350
351         [MethodImplAttribute (MethodImplOptions.NoInlining)]
352         static int args_simple<T> (T t, int i) {
353                 return i;
354         }
355
356         [MethodImplAttribute (MethodImplOptions.NoInlining)]
357         static int args_simple<T> (T t, int i, T t2) {
358                 return i;
359         }
360
361         [MethodImplAttribute (MethodImplOptions.NoInlining)]
362         static Type args_rgctx<T> (T t, int i) {
363                 return typeof (T);
364         }
365
366         [MethodImplAttribute (MethodImplOptions.NoInlining)]
367         static Type eh_in<T> (T t, int i) {
368                 throw new OverflowException ();
369         }
370
371         [MethodImplAttribute (MethodImplOptions.NoInlining)]
372         static T return_t<T> (T t) {
373                 return t;
374         }
375
376         [MethodImplAttribute (MethodImplOptions.NoInlining)]
377         T return_this_t<T> (T t) {
378                 return t;
379         }
380
381         public static int test_0_gsharedvt_in () {
382                 // Check that the non-generic argument is passed at the correct stack position
383                 int r = args_simple<bool> (true, 42);
384                 if (r != 42)
385                         return 1;
386                 r = args_simple<Foo> (new Foo (), 43);
387                 if (r != 43)
388                         return 2;
389                 // Check that the proper rgctx is passed to the method
390                 Type t = args_rgctx<int> (5, 42);
391                 if (t != typeof (int))
392                         return 3;
393                 var v = args_simple<GFoo2<int>> (new GFoo2<int> () { t = 11, t2 = 12 }, 44, new GFoo2<int> () { t = 11, t2 = 12 });
394                 if (v != 44)
395                         return 4;
396                 // Check that EH works properly
397                 try {
398                         eh_in<int> (1, 2);
399                 } catch (OverflowException) {
400                 }
401                 return 0;
402         }
403
404         public static int test_0_gsharedvt_in_ret () {
405                 int i = return_t<int> (42);
406                 if (i != 42)
407                         return 1;
408                 long l = return_t<long> (Int64.MaxValue);
409                 if (l != Int64.MaxValue)
410                         return 2;
411                 double d = return_t<double> (3.0);
412                 if (d != 3.0)
413                         return 3;
414                 float f = return_t<float> (3.0f);
415                 if (f != 3.0f)
416                         return 4;
417                 short s = return_t<short> (16);
418                 if (s != 16)
419                         return 5;
420                 var v = new GFoo2<int> () { t = 55, t2 = 32 };
421                 var v2 = return_t<GFoo2<int>> (v);
422                 if (v2.t != 55 || v2.t2 != 32)
423                         return 6;
424                 i = new GSharedTests ().return_this_t<int> (42);
425                 if (i != 42)
426                         return 7;
427                 return 0;
428         }
429
430         public static int test_0_gsharedvt_in_delegates () {
431                 Func<int, int> f = new Func<int, int> (return_t<int>);
432                 if (f (42) != 42)
433                         return 1;
434                 return 0;
435         }
436
437         [MethodImplAttribute (MethodImplOptions.NoInlining)]
438         static T return2_t<T> (T t) {
439                 return return_t (t);
440         }
441
442         public static int test_0_gsharedvt_calls () {
443                 if (return2_t (2) != 2)
444                         return 1;
445                 if (return2_t ("A") != "A")
446                         return 2;
447                 if (return2_t (2.0) != 2.0)
448                         return 3;
449                 return 0;
450         }
451
452         static GFoo3<T> newobj<T> (T t1, T t2) {
453                 return new GFoo3<T> (t1, t2);
454         }
455         
456         public static int test_0_gshared_new () {
457                 var g1 = newobj (1, 2);
458                 if (g1.t != 1 || g1.t2 != 2)
459                         return 1;
460                 var g2 = newobj (1.0, 2.0);
461                 if (g1.t != 1.0 || g1.t2 != 2.0)
462                         return 2;
463
464                 return 0;
465         }
466
467         [MethodImplAttribute (MethodImplOptions.NoInlining)]
468         static GFoo2<T> newobj_vt<T> (T t1, T t2) {
469                 return new GFoo2<T> () { t = t1, t2 = t2 };
470         }
471
472         public static int test_0_gshared_new_vt () {
473                 GFoo2<int> v1 = newobj_vt (1, 2);
474                 if (v1.t != 1 || v1.t2 != 2)
475                         return 1;
476                 GFoo2<double> v2 = newobj_vt (1.0, 2.0);
477                 if (v2.t != 1.0 || v2.t2 != 2.0)
478                         return 2;
479                 return 0;
480         }
481
482         //
483         // Tests for transitioning out of gsharedvt code
484         //
485
486         // T1=Nullable<..> is not currently supported by gsharedvt
487
488         [MethodImplAttribute (MethodImplOptions.NoInlining)]
489         static T return_t_nogshared<T,T1> (T t) {
490                 object o = t;
491                 T t2 = (T)o;
492                 //Console.WriteLine ("X: " + t);
493                 return t;
494         }
495
496         [MethodImplAttribute (MethodImplOptions.NoInlining)]
497         static int return_int_nogshared<T,T1> (T t) {
498                 object o = t;
499                 T t2 = (T)o;
500                 return 2;
501         }
502
503         [MethodImplAttribute (MethodImplOptions.NoInlining)]
504         static A return_vtype_nogshared<T,T1> (T t) {
505                 object o = t;
506                 T t2 = (T)o;
507                 return new A () { a = 1, b = 2, c = 3 };
508         }
509
510         [MethodImplAttribute (MethodImplOptions.NoInlining)]
511         static T return2_t_out<T> (T t) {
512                 return return_t_nogshared<T, int?> (t);
513         }
514
515         [MethodImplAttribute (MethodImplOptions.NoInlining)]
516         static int return2_int_out<T> (T t) {
517                 return return_int_nogshared<T, int?> (t);
518         }
519
520         [MethodImplAttribute (MethodImplOptions.NoInlining)]
521         static A return2_vtype_out<T> (T t) {
522                 return return_vtype_nogshared<T, int?> (t);
523         }
524
525         struct A {
526                 public int a, b, c;
527         }
528
529         [Category ("!FULLAOT")]
530         public static int test_0_gsharedvt_out () {
531                 if (return2_t_out (2) != 2)
532                         return 1;
533                 if (return2_t_out ("A") != "A")
534                         return 2;
535                 if (return2_t_out (2.0) != 2.0)
536                         return 3;
537                 if (return2_t_out (2.0f) != 2.0f)
538                         return 4;
539                 A a = new A () { a = 1, b = 2, c = 3 };
540                 A a2 = return2_t_out (a);
541                 if (a2.a != 1 || a2.b != 2 || a2.c != 3)
542                         return 5;
543                 // Calls with non gsharedvt return types
544                 if (return2_int_out (1) != 2)
545                         return 6;
546                 A c = return2_vtype_out (a);
547                 if (a2.a != 1 || a2.b != 2 || a2.c != 3)
548                         return 7;
549                 return 0;
550         }
551
552         public class GenericClass<T> {
553                 public static T Z (IList<T> x, int index)
554                 {
555                         return x [index];
556                 }
557         }
558
559         public static int test_0_generic_array_helpers () {
560                 int[] x = new int[] {100, 200};
561
562                 // Generic array helpers should be treated as gsharedvt-out
563                 if (GenericClass<int>.Z (x, 0) != 100)
564                         return 1;
565
566                 return 0;
567         }
568
569         internal class IntComparer : IComparer<int>
570         {
571                 public int Compare (int ix, int iy)
572                 {
573                         if (ix == iy)
574                                 return 0;
575
576                         if (((uint) ix) < ((uint) iy))
577                                 return -1;
578                         return 1;
579                 }
580         }
581
582         [MethodImplAttribute (MethodImplOptions.NoInlining)]
583         static int gshared_out_iface<T> (T t1, T t2, IComparer<T> comp) {
584                 return comp.Compare (t1, t2);
585         }
586
587         public static int test_0_gshared_out_iface () {
588                 // Call out from gshared to a nongeneric method through a generic interface method
589                 if (gshared_out_iface (2, 2, new IntComparer ()) != 0)
590                         return 1;
591                 return 0;
592         }
593
594         struct Foo1 {
595                 public int i1, i2, i3;
596         }
597
598         struct Foo2<T> {
599                 int i1, i2, i3, i4, i5;
600                 public T foo;
601         }
602
603         [MethodImplAttribute (MethodImplOptions.NoInlining)]    
604         public static void locals<T> (T t) {
605                 Foo2<T> t2 = new Foo2<T> ();
606                 object o = t2;
607         }
608
609         public static int test_0_locals () {
610                 // Test that instantiations of type parameters are allocated the proper local type
611                 int i = 1;
612                 for (int j = 0; j < 10; ++j)
613                         i ++;
614                 locals<Foo1> (new Foo1 () { i1 = 1, i2 = 2, i3 = 3 });
615                 return 0;
616         }
617
618         public interface IFace<T> {
619                 T return_t_iface (T t);
620         }
621
622         public class Parent<T> {
623                 public virtual T return_t_vcall (T t) {
624                         throw new Exception ();
625                         return t;
626                 }
627         }
628
629         public class Child<T> : Parent<T>, IFace<T> {
630                 public override T return_t_vcall (T t) {
631                         return t;
632                 }
633                 public T return_t_iface (T t) {
634                         return t;
635                 }
636         }
637
638         [MethodImplAttribute (MethodImplOptions.NoInlining)]
639         static T return_t_vcall<T> (Parent<T> r, T t) {
640                 return r.return_t_vcall (t);
641         }
642
643         public static int test_0_vcalls () {
644                 if (return_t_vcall (new Child<int> (), 2) != 2)
645                         return 1;
646                 // Patching
647                 for (int i = 0; i < 10; ++i) {
648                         if (return_t_vcall (new Child<int> (), 2) != 2)
649                                 return 2;
650                 }
651                 if (return_t_vcall (new Child<double> (), 2.0) != 2.0)
652                         return 3;
653                 return 0;
654         }
655
656         [MethodImplAttribute (MethodImplOptions.NoInlining)]
657         static T return_t_iface<T> (IFace<T> r, T t) {
658                 return r.return_t_iface (t);
659         }
660
661         public static int test_0_iface_calls () {
662                 if (return_t_iface (new Child<int> (), 2) != 2)
663                         return 1;
664                 if (return_t_iface (new Child<double> (), 2.0) != 2.0)
665                         return 3;
666                 return 0;
667         }
668
669         interface IFaceKVP {
670                 T do_kvp<T> (T a);
671         }
672
673         static KeyValuePair<T1, T2> make_kvp<T1, T2> (T1 t1, T2 t2) {
674                 return new KeyValuePair<T1, T2> (t1, t2);
675         }
676
677         static T2 use_kvp<T1, T2> (KeyValuePair<T1, T2> kvp) {
678                 return kvp.Value;
679         }
680
681         class ClassKVP : IFaceKVP {
682                 public T do_kvp<T> (T a) {
683                         var t = make_kvp (a, a);
684                         // argument is an instance of a vtype instantiated with gsharedvt type arguments
685                         return use_kvp (t);
686                 }
687         }
688
689         public static int test_0_gsharedvt_ginstvt_constructed_arg () {
690                 IFaceKVP c = new ClassKVP ();
691                 if (c.do_kvp<long> (1) != 1)
692                         return 1;
693                 return 0;
694         }
695
696         interface IGetter
697         {
698                 T Get<T>();
699         }
700
701         class Getter : IGetter
702         {
703                 public T Get<T>() { return default(T); }
704         }
705
706         abstract class Session
707         {
708                 public abstract IGetter Getter { get; }
709         }
710
711         class IosSession : Session
712         {
713                 private IGetter getter = new Getter();
714                 public override IGetter Getter { get { return getter; } }
715         }
716
717         enum ENUM_TYPE {
718         }
719
720         public static int test_0_regress_5156 () {
721                 new IosSession().Getter.Get<ENUM_TYPE>();
722                 return 0;
723         }
724
725         public struct VT
726         {
727                 public Action a;
728         }
729
730         public class D
731         {
732         }
733
734         public class A3
735         {
736                 public void OuterMethod<TArg1>(TArg1 value)
737                 {
738                         this.InnerMethod<TArg1, long>(value, 0);
739                 }
740
741                 private void InnerMethod<TArg1, TArg2>(TArg1 v1, TArg2 v2)
742                 {
743                         //Console.WriteLine("{0} {1}",v1,v2);
744                 }
745         }
746
747         public static int test_0_regress_2096 () {
748                 var a = new A3();
749
750                 // The following work:
751                 a.OuterMethod<int>(1);
752                 a.OuterMethod<DateTime>(DateTime.Now);
753
754                 var v = new VT();
755                 a.OuterMethod<VT>(v);
756
757                 var x = new D();
758                 // Next line will crash with Attempting to JIT compile method on device
759                 //  Attempting to JIT compile method
760                 a.OuterMethod<D>(x);
761                 return 0;
762         }
763
764         public class B
765         {
766                 public void Test<T>()
767                 {
768                         //System.Console.WriteLine(typeof(T));
769                 }
770         }
771
772         public class A<T>
773         {
774                 public void Test()
775                 {
776                         new B().Test<System.Collections.Generic.KeyValuePair<T, T>>();
777                 }
778         }
779
780     public static int test_0_regress_6040 () {
781         //new B().Test<System.Collections.Generic.KeyValuePair<string, string>>();
782         new A<int>().Test();
783         new A<object>().Test();
784         new A<string>().Test();
785                 return 0;
786     }
787
788         class ArrayContainer<T> {
789                 private T[,] container = new T[1,1];
790
791                 public T Prop {
792                         [MethodImplAttribute (MethodImplOptions.NoInlining)]
793                         get {
794                                 return container [0, 0];
795                         }
796                         [MethodImplAttribute (MethodImplOptions.NoInlining)]
797                         set {
798                                 container [0, 0] = value;
799                         }
800                 }
801         }
802
803         [MethodImplAttribute (MethodImplOptions.NoInlining)]
804         public static int test_0_multi_dim_arrays () {
805                 var c = new ArrayContainer<int> ();
806                 c.Prop = 5;
807                 return c.Prop == 5 ? 0 : 1;
808         }
809
810         [MethodImplAttribute (MethodImplOptions.NoInlining)]
811         static T2 rgctx_in_call_innner_inner<T1, T2> (T1 t1, T2 t2) {
812                 return t2;
813         }
814
815         [MethodImplAttribute (MethodImplOptions.NoInlining)]
816         static GFoo3<T> rgctx_in_call_inner<T> (T t) {
817                 return rgctx_in_call_innner_inner (1, new GFoo3<T> ());
818         }
819
820     public static int test_0_rgctx_in_call () {
821                 // The call is made through the rgctx call, and it needs an IN trampoline
822                 var t = rgctx_in_call_inner (1);
823                 if (t is GFoo3<int>)
824                         return 0;
825                 return 1;
826         }
827
828         [MethodImplAttribute (MethodImplOptions.NoInlining)]
829         static void arm_params1<T> (T t1, T t2, T t3, T t4, T t5, T t6) {
830         }
831
832         [MethodImplAttribute (MethodImplOptions.NoInlining)]
833         static void arm_params2<T> (T t1, T t2, T t3, long t4, T t5, T t6) {
834         }
835
836         public static int test_0_arm_param_passing () {
837                 arm_params1<int> (1, 2, 3, 4, 5, 6);
838                 arm_params1<int> (1, 2, 3, 4, 5, 6);
839                 return 0;
840         }
841
842         sealed class ScheduledItem<TAbsolute, TValue> {
843                 private readonly object _scheduler;
844                 private readonly TValue _state;
845                 private readonly object _action;
846
847                 public ScheduledItem(object o, TValue state, object action, TAbsolute dueTime) {
848                         _state = state;
849                 }
850         }
851
852     abstract class VirtualTimeSchedulerBase<TAbsolute, TRelative> {
853         public abstract void ScheduleAbsolute<TState>(TState state, TAbsolute dueTime);
854         }
855
856         class VirtualTimeScheduler<TAbsolute, TRelative> : VirtualTimeSchedulerBase<TAbsolute, TRelative> {
857                 public override void ScheduleAbsolute<TState>(TState state, TAbsolute dueTime) {
858                         var si = new ScheduledItem<TAbsolute, TState>(this, state, null, dueTime);
859                 }
860         }
861
862         public static int test_0_rx_mixed_regress () {
863                 var v = new VirtualTimeScheduler<long, long> ();
864                 v.ScheduleAbsolute<Action> (null, 22);
865                 return 0;
866         }
867
868         public class Base {
869                 public virtual T foo<T> (T t) {
870                         return t;
871                 }
872         }
873
874         class Class1 : Base {
875                 public object o;
876
877                 public override T foo<T> (T t) {
878                         o = t;
879                         return t;
880                 }
881         }
882
883         class Class2 : Base {
884                 public object o;
885
886                 public override T foo<T> (T t) {
887                         o = t;
888                         return t;
889                 }
890         }
891
892         [MethodImplAttribute (MethodImplOptions.NoInlining)]
893         public static void bar<T> (Base b, T t) {
894                 b.foo (t);
895         }
896
897         public static int test_0_virtual_generic () {
898                 Class1 c1 = new Class1 ();
899                 Class2 c2 = new Class2 ();
900                 bar (c1, 5);
901                 if (!(c1.o is int) || ((int)c1.o != 5))
902                         return 1;
903                 bar (c1, 6.0);
904                 if (!(c1.o is double) || ((double)c1.o != 6.0))
905                         return 2;
906                 bar (c1, 7.0f);
907                 if (!(c1.o is float) || ((float)c1.o != 7.0f))
908                         return 3;
909                 bar (c2, 5);
910                 if (!(c2.o is int) || ((int)c2.o != 5))
911                         return 4;
912                 bar (c2, 6.0);
913                 bar (c2, 7.0f);
914                 return 0;
915         }
916
917         [MethodImplAttribute (MethodImplOptions.NoInlining)]
918         static string to_string<T, T2>(T t, T2 t2) {
919                 return t.ToString ();
920         }
921
922         enum AnEnum {
923                 One,
924                 Two
925         };
926
927         public static int test_0_constrained_tostring () {
928                 if (to_string<int, int> (1, 1) != "1")
929                         return 1;
930                 if (to_string<AnEnum, int> (AnEnum.One, 1) != "One")
931                         return 2;
932                 if (to_string<string, int> ("A", 1) != "A")
933                         return 3;
934                 return 0;
935         }
936
937         [MethodImplAttribute (MethodImplOptions.NoInlining)]
938         static int get_hash<T, T2>(T t, T2 t2) {
939                 return t.GetHashCode ();
940         }
941
942         public static int test_0_constrained_get_hash () {
943                 if (get_hash<int, int> (1, 1) != 1.GetHashCode ())
944                         return 1;
945                 if (get_hash<double, int> (1.0, 1) != 1.0.GetHashCode ())
946                         return 2;
947                 if (get_hash<AnEnum, int> (AnEnum.One, 1) != AnEnum.One.GetHashCode ())
948                         return 3;
949                 if (get_hash<string, int> ("A", 1) != "A".GetHashCode ())
950                         return 4;
951                 return 0;
952         }
953
954         [MethodImplAttribute (MethodImplOptions.NoInlining)]
955         static bool equals<T, T2>(T t, T2 t2) {
956                 return t.Equals (t);
957         }
958
959         public static int test_0_constrained_equals () {
960                 if (equals<int, int> (1, 1) != true)
961                         return 1;
962                 if (equals<double, int> (1.0, 1) != true)
963                         return 2;
964                 if (equals<AnEnum, int> (AnEnum.One, 1) != true)
965                         return 3;
966                 if (equals<string, int> ("A", 1) != true)
967                         return 4;
968                 return 0;
969         }
970
971         struct Pair<T1, T2> {
972                 public T1 First;
973                 public T2 Second;
974         }
975
976         [MethodImplAttribute (MethodImplOptions.NoInlining)]
977         public static TState call_del<TState>(TState state, Func<object, TState, TState> action) {
978                 return action(null, state);
979         }
980
981         public static int test_0_delegate_wrappers () {
982                 Func<object, Pair<int, int>, Pair<int, int>> del1 = delegate (object o, Pair<int, int> p) { return p; };
983                 Func<object, Pair<int, int>, Pair<int, int>> del2 = delegate (object o, Pair<int, int> p) { return p; };
984                 Func<object, Pair<double, int>, Pair<double, int>> del3 = delegate (object o, Pair<double, int> p) { return p; };
985                 var r1 = call_del<Pair<int, int>> (new Pair<int, int> { First = 1, Second = 2}, del1);
986                 if (r1.First != 1 || r1.Second != 2)
987                         return 1;
988                 var r2 = call_del<Pair<int, int>> (new Pair<int, int> { First = 3, Second = 4}, del2);
989                 if (r2.First != 3 || r2.Second != 4)
990                         return 2;
991                 var r3 = call_del<Pair<double, int>> (new Pair<double, int> { First = 1.0, Second = 2}, del3);
992                 if (r3.First != 1.0 || r3.Second != 2)
993                         return 3;
994                 return 0;
995         }
996
997         class Base<T> {
998                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
999                 public object foo<T1> (T1 t1, T t, object o) {
1000                         return o;
1001                 }
1002         }
1003
1004         class AClass : Base<long> {
1005
1006                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1007                 public object bar<T> (T t, long time, object o) {
1008                         return foo (t, time, o);
1009                 }
1010         }
1011
1012         public static int test_0_out_in_wrappers () {
1013                 var a = new AClass ();
1014                 object o1 = "A";
1015                 object o2 = a.bar<long> (1024, 0, o1);
1016                 if (o1 != o2)
1017                         return 1;
1018                 return 0;               
1019         }
1020
1021                 interface BIFace {
1022                         object AMethod ();
1023                 }
1024
1025                 class Base<TAbsolute, T2> : BIFace {
1026
1027                         public TAbsolute Clock { get; set; }
1028
1029                         public virtual object AMethod () {
1030                                 return Clock;
1031                         }
1032                 }
1033
1034                 class BClass : Base<long, long> {
1035                 }
1036
1037         public static int test_0_regress_1 () {
1038                 BIFace c = new BClass ();
1039                 object o = c.AMethod ();
1040                 if (!(o is long) || ((long)o != 0))
1041                         return 1;
1042                 return 0;
1043         }
1044
1045         interface IFace3 {
1046                 T unbox_any<T> (object o);
1047         }
1048
1049         class Class3 : IFace3 {
1050                 public virtual T unbox_any<T> (object o) {
1051                         return (T)o;
1052                 }
1053         }
1054
1055         public static int test_0_unbox_any () {
1056                 IFace3 o = new Class3 ();
1057                 if (o.unbox_any<int> (16) != 16)
1058                         return 1;
1059                 if (o.unbox_any<long> ((long)32) != 32)
1060                         return 2;
1061                 if (o.unbox_any<double> (2.0) != 2.0)
1062                         return 3;
1063                 try {
1064                         o.unbox_any<int> (2.0);
1065                         return 4;
1066                 } catch (Exception) {
1067                 }
1068                 return 0;
1069         }
1070
1071         interface IFace4 {
1072                 TSource Catch<TSource, TException>(TSource t)  where TException : Exception;
1073         }
1074
1075         class Class4 : IFace4 {
1076                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1077                         public TSource Catch<TSource, TException>(TSource t)  where TException : Exception {
1078                         return t;
1079                 }
1080         }
1081
1082         // Check that mixed instantiations are correctly created/found in AOT
1083         public static int test_0_constraints () {
1084                 IFace4 o = new Class4 ();
1085                 o.Catch<int, Exception> (1);
1086                 return 0;
1087         }
1088
1089         internal static Type Process<TSource, TElement> (TSource[] arr, Action<TElement, TElement> call) {
1090                 arr [0] = default (TSource);
1091                 return typeof (TSource);
1092         }
1093
1094         interface IFace5 {
1095                 Type foo<T> ();
1096         }
1097
1098         class Class5 : IFace5 {
1099                 public Type foo<T> () {
1100                         return Process<KeyValuePair<long, T>, T> (new KeyValuePair<long, T> [10], null);
1101                 }
1102         }
1103
1104         public static int test_0_rgctx_call_from_gshared_code () {
1105                 var c = new Class5 ();
1106                 if (c.foo<string> () != typeof (KeyValuePair<long, string>))
1107                         return 1;
1108                 return 0;
1109         }
1110
1111         public class Enumbers<T> {
1112                 public object Enumerate (List<KeyValuePair<T, string>> alist)
1113                 {
1114                         return alist.ToArray ();
1115                 }
1116         }
1117
1118         public static int test_0_checkthis_gshared_call () {
1119                 Enumbers<string> e = new Enumbers<string> ();
1120                 try {
1121                         e.Enumerate (null);
1122                         return 1;
1123                 }
1124                 catch (NullReferenceException) {
1125                 }
1126                 return 0;
1127         }
1128
1129         interface IFace6 {
1130                 T[] Del<T> (T t);
1131         }
1132
1133         class Class6 : IFace6 {
1134                 public T[] Del<T> (T t) {
1135                         var res = new T [5];
1136                         Func<T, T, T, T, T> func = delegate(T t1, T t2, T t3, T t4) { res [0] = t1; res [1] = t2; res [2] = t3; res [3] = t4; return t1; };
1137                         var v = func.BeginInvoke(t, t, t, t, null, null);
1138                         res [4] = func.EndInvoke (v);
1139                         return res;
1140                 }
1141         }
1142
1143         // FIXME: The runtime-invoke wrapper used by BeginInvoke is not found
1144         [Category ("!FULLAOT")]
1145         public static int test_0_begin_end_invoke () {
1146                 IFace6 o = new Class6 ();
1147                 var arr1 = o.Del (1);
1148                 if (arr1 [0] != 1 || arr1 [1] != 1 || arr1 [2] != 1 || arr1 [3] != 1 || arr1 [4] != 1)
1149                         return 1;
1150                 var arr2 = o.Del (2.0);
1151                 if (arr2 [0] != 2.0 || arr2 [1] != 2.0 || arr2 [2] != 2.0 || arr2 [3] != 2.0 || arr2 [4] != 2.0)
1152                         return 2;
1153                 return 0;
1154         }
1155
1156         public class TAbstractTableItem<TC> {
1157                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1158                 public static void SetProperty<TV> () {    }
1159
1160                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1161                 public static void Test () {
1162                         SetProperty<bool> ();
1163                 }
1164         }
1165
1166         public static int test_0_gsharedvt_method_on_shared_class () {
1167        TAbstractTableItem<object>.Test ();
1168            return 0;
1169         }
1170
1171         interface IFaceBox {
1172                 object box<T> (T t);
1173         }
1174
1175         class ClassBox : IFaceBox {
1176                 public object box<T> (T t) {
1177                         object o = t;
1178                         return o;
1179                 }
1180         }
1181
1182         public static int test_0_nullable_box () {
1183                 IFaceBox c = new ClassBox ();
1184                 int i = 5;
1185                 object o = c.box<int?> (i);
1186                 if ((int)o != i)
1187                         return 1;
1188                 if (c.box<int?> (null) != null)
1189                         return 2;
1190                 long l = Int64.MaxValue - 1;
1191                 o = c.box<long?> (l);
1192                 if ((long)o != l)
1193                         return 3;
1194                 if (c.box<long?> (null) != null)
1195                         return 4;
1196                 string s = "A";
1197                 if (c.box<string> (s) != (object)s)
1198                         return 5;
1199                 return 0;
1200         }
1201
1202         interface IFaceUnbox2 {
1203                 T unbox<T> (object o);
1204         }
1205
1206         class ClassUnbox2 : IFaceUnbox2 {
1207                 public T unbox<T> (object o) {
1208                         return (T)o;
1209                 }
1210         }
1211
1212         public static int test_0_nullable_unbox () {    
1213                 IFaceUnbox2 c = new ClassUnbox2 ();
1214                 int? i = c.unbox<int?> (5);
1215                 if (i != 5)
1216                         return 1;
1217                 int? j = c.unbox<int?> (null);
1218                 if (j != null)
1219                         return 2;
1220                 return 0;
1221         }
1222
1223         interface IConstrained {
1224                 void foo ();
1225                 void foo_ref_arg (string s);
1226         }
1227
1228         interface IConstrained<T3> {
1229                 void foo_gsharedvt_arg (T3 s);
1230         }
1231
1232         static object constrained_res;
1233
1234         struct ConsStruct : IConstrained {
1235                 public int i;
1236
1237                 public void foo () {
1238                         constrained_res = i;
1239                 }
1240
1241                 public void foo_ref_arg (string s) {
1242                         constrained_res = s == "A" ? 42 : 0;
1243                 }
1244         }
1245
1246         class ConsClass : IConstrained {
1247                 public int i;
1248
1249                 public void foo () {
1250                         constrained_res = i;
1251                 }
1252
1253                 public void foo_ref_arg (string s) {
1254                         constrained_res = s == "A" ? 43 : 0;
1255                 }
1256         }
1257
1258         struct ConsStruct<T> : IConstrained<T> {
1259                 public void foo_gsharedvt_arg (T s) {
1260                         constrained_res = s;
1261                 }
1262         }
1263
1264         interface IFaceConstrained {
1265                 void constrained_void_iface_call<T, T2>(T t, T2 t2) where T2 : IConstrained;
1266                 void constrained_void_iface_call_ref_arg<T, T2>(T t, T2 t2) where T2 : IConstrained;
1267                 void constrained_void_iface_call_gsharedvt_arg<T, T2, T3>(T t, T2 t2, T3 t3) where T2 : IConstrained<T>;
1268         }
1269
1270         class ClassConstrained : IFaceConstrained {
1271                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1272                 public void constrained_void_iface_call<T, T2>(T t, T2 t2) where T2 : IConstrained {
1273                         t2.foo ();
1274                 }
1275
1276                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1277                 public void constrained_void_iface_call_ref_arg<T, T2>(T t, T2 t2) where T2 : IConstrained {
1278                         t2.foo_ref_arg ("A");
1279                 }
1280
1281                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1282                 public void constrained_void_iface_call_gsharedvt_arg<T, T2, T3>(T t, T2 t2, T3 t3) where T2 : IConstrained<T> {
1283                         t2.foo_gsharedvt_arg (t);
1284                 }
1285         }
1286
1287         public static int test_0_constrained_void_iface_call () {
1288                 IFaceConstrained c = new ClassConstrained ();
1289                 var s = new ConsStruct () { i = 42 };
1290                 constrained_res = null;
1291                 c.constrained_void_iface_call<int, ConsStruct> (1, s);
1292                 if (!(constrained_res is int) || ((int)constrained_res) != 42)
1293                         return 1;
1294                 constrained_res = null;
1295                 c.constrained_void_iface_call_ref_arg<int, ConsStruct> (1, s);
1296                 if (!(constrained_res is int) || ((int)constrained_res) != 42)
1297                         return 2;
1298                 var s2 = new ConsClass () { i = 43 };
1299                 constrained_res = null;
1300                 c.constrained_void_iface_call<int, ConsClass> (1, s2);
1301                 if (!(constrained_res is int) || ((int)constrained_res) != 43)
1302                         return 3;
1303                 constrained_res = null;
1304                 c.constrained_void_iface_call_ref_arg<int, ConsClass> (1, s2);
1305                 if (!(constrained_res is int) || ((int)constrained_res) != 43)
1306                         return 4;
1307                 return 0;
1308         }
1309
1310         public static int test_0_constraine_void_iface_call_gsharedvt_arg () {
1311                 // This tests constrained calls through interfaces with one gsharedvt arg, like IComparable<T>.CompareTo ()
1312                 IFaceConstrained c = new ClassConstrained ();
1313
1314                 var s = new ConsStruct<int> ();
1315                 constrained_res = null;
1316                 c.constrained_void_iface_call_gsharedvt_arg<int, ConsStruct<int>, int> (42, s, 55);
1317                 if (!(constrained_res is int) || ((int)constrained_res) != 42)
1318                         return 1;
1319
1320                 var s2 = new ConsStruct<string> ();
1321                 constrained_res = null;
1322                 c.constrained_void_iface_call_gsharedvt_arg<string, ConsStruct<string>, int> ("A", s2, 55);
1323                 if (!(constrained_res is string) || ((string)constrained_res) != "A")
1324                         return 2;
1325
1326                 return 0;
1327         }
1328
1329         public static async Task<T> FooAsync<T> (int i, int j) {
1330                 Task<int> t = new Task<int> (delegate () { return 42; });
1331                 var response = await t;
1332                 return default(T);
1333         }
1334
1335         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1336         public static void call_async<T> (int i, int j) {
1337                 Task<T> t = FooAsync<T> (1, 2);
1338                 t.RunSynchronously ();
1339         }
1340
1341         // In AOT mode, the async infrastructure depends on gsharedvt methods
1342         public static int test_0_async_call_from_generic () {
1343                 call_async<string> (1, 2);
1344                 return 0;
1345         }
1346 }
1347
1348 #if !MOBILE
1349 public class GSharedTests : Tests {
1350 }
1351 #endif