Merge pull request #1857 from slluis/fix-assembly-resolver
[mono.git] / mono / mini / aot-tests.cs
1 using System;
2 using System.Text;
3 using System.Reflection;
4 using System.Runtime.InteropServices;
5 using System.Runtime.CompilerServices;
6 using System.Collections.Generic;
7
8 /*
9  * Regression tests for the AOT/FULL-AOT code.
10  */
11
12 #if MOBILE
13 class AotTests
14 #else
15 class Tests
16 #endif
17 {
18 #if !MOBILE
19         static int Main (String[] args) {
20                 return TestDriver.RunTests (typeof (Tests), args);
21         }
22 #endif
23
24         public delegate void ArrayDelegate (int[,] arr);
25
26         static int test_0_array_delegate_full_aot () {
27                 ArrayDelegate d = delegate (int[,] arr) {
28                 };
29                 int[,] a = new int[5, 6];
30                 d.BeginInvoke (a, null, null);
31                 return 0;
32         }
33
34         struct Struct1 {
35                 public double a, b;
36         }
37
38         struct Struct2 {
39                 public float a, b;
40         }
41
42         class Foo<T> {
43                 /* The 'd' argument is used to shift the register indexes so 't' doesn't start at the first reg */
44                 public static T Get_T (double d, T t) {
45                         return t;
46                 }
47         }
48
49         class Foo2<T> {
50                 public static T Get_T (double d, T t) {
51                         return t;
52                 }
53                 public static T Get_T2 (double d, int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, T t) {
54                         return t;
55                 }
56         }
57
58         class Foo3<T> {
59                 public static T Get_T (double d, T t) {
60                         return Foo2<T>.Get_T (d, t);
61                 }
62         }
63
64         [Category ("DYNCALL")]
65         static int test_0_arm64_dyncall_double () {
66                 double arg1 = 1.0f;
67                 double s = 2.0f;
68                 var res = (double)typeof (Foo<double>).GetMethod ("Get_T").Invoke (null, new object [] { arg1, s });
69                 if (res != 2.0f)
70                         return 1;
71                 return 0;
72         }
73
74         [Category ("DYNCALL")]
75         static int test_0_arm64_dyncall_float () {
76                 double arg1 = 1.0f;
77                 float s = 2.0f;
78                 var res = (float)typeof (Foo<float>).GetMethod ("Get_T").Invoke (null, new object [] { arg1, s });
79                 if (res != 2.0f)
80                         return 1;
81                 return 0;
82         }
83
84         [Category ("DYNCALL")]
85         static int test_0_arm64_dyncall_hfa_double () {
86                 double arg1 = 1.0f;
87                 // HFA with double members
88                 var s = new Struct1 ();
89                 s.a = 1.0f;
90                 s.b = 2.0f;
91                 var s_res = (Struct1)typeof (Foo<Struct1>).GetMethod ("Get_T").Invoke (null, new object [] { arg1, s });
92                 if (s_res.a != 1.0f || s_res.b != 2.0f)
93                         return 1;
94                 return 0;
95         }
96
97         [Category ("DYNCALL")]
98         static int test_0_arm64_dyncall_hfa_float () {
99                 double arg1 = 1.0f;
100                 var s = new Struct2 ();
101                 s.a = 1.0f;
102                 s.b = 2.0f;
103                 var s_res = (Struct2)typeof (Foo<Struct2>).GetMethod ("Get_T").Invoke (null, new object [] { arg1, s });
104                 if (s_res.a != 1.0f || s_res.b != 2.0f)
105                         return 1;
106                 return 0;
107         }
108
109         [Category ("GSHAREDVT")]
110         static int test_0_arm64_dyncall_gsharedvt_out_hfa_double () {
111                 /* gsharedvt out trampoline with double hfa argument */
112                 double arg1 = 1.0f;
113
114                 var s = new Struct1 ();
115                 s.a = 1.0f;
116                 s.b = 2.0f;
117                 // Call Foo2.Get_T directly, so its gets an instance
118                 Foo2<Struct1>.Get_T (arg1, s);
119                 Type t = typeof (Foo3<>).MakeGenericType (new Type [] { typeof (Struct1) });
120                 // Call Foo3.Get_T, this will call the gsharedvt instance, which will call the non-gsharedvt instance
121                 var s_res = (Struct1)t.GetMethod ("Get_T").Invoke (null, new object [] { arg1, s });
122                 if (s_res.a != 1.0f || s_res.b != 2.0f)
123                         return 1;
124                 return 0;
125         }
126
127         [Category ("GSHAREDVT")]
128         static int test_0_arm64_dyncall_gsharedvt_out_hfa_float () {
129                 /* gsharedvt out trampoline with double hfa argument */
130                 double arg1 = 1.0f;
131
132                 var s = new Struct2 ();
133                 s.a = 1.0f;
134                 s.b = 2.0f;
135                 // Call Foo2.Get_T directly, so its gets an instance
136                 Foo2<Struct2>.Get_T (arg1, s);
137                 Type t = typeof (Foo3<>).MakeGenericType (new Type [] { typeof (Struct2) });
138                 // Call Foo3.Get_T, this will call the gsharedvt instance, which will call the non-gsharedvt instance
139                 var s_res = (Struct2)t.GetMethod ("Get_T").Invoke (null, new object [] { arg1, s });
140                 if (s_res.a != 1.0f || s_res.b != 2.0f)
141                         return 1;
142                 return 0;
143         }
144
145         interface IFaceFoo4<T> {
146                 T Get_T (double d, T t);
147                 T Get_T2 (double d, T t);
148         }
149
150         class Foo4<T> : IFaceFoo4<T> {
151                 public T Get_T (double d, T t) {
152                         return Foo2<T>.Get_T (d, t);
153                 }
154                 public T Get_T2 (double d, T t) {
155                         return Foo2<T>.Get_T2 (d, 1, 2, 3, 4, 5, 6, 7, 8, t);
156                 }
157         }
158
159         struct VTypeByRefStruct {
160                 public long o1, o2, o3;
161         }
162
163         [Category ("GSHAREDVT")]
164         public static int test_0_arm64_gsharedvt_out_vtypebyref () {
165                 /* gsharedvt out trampoline with vtypebyref argument */
166                 var s = new VTypeByRefStruct () { o1 = 1, o2 = 2, o3 = 3 };
167
168                 // Call Foo2.Get_T directly, so its gets an instance
169                 Foo2<VTypeByRefStruct>.Get_T (1.0f, s);
170                 var o = (IFaceFoo4<VTypeByRefStruct>)Activator.CreateInstance (typeof (Foo4<>).MakeGenericType (new Type [] { typeof (VTypeByRefStruct) }));
171                 // Call Foo4.Get_T, this will call the gsharedvt instance, which will call the non-gsharedvt instance
172                 var s_res = o.Get_T (1.0f, s);
173                 if (s_res.o1 != 1 || s_res.o2 != 2 || s_res.o3 != 3)
174                         return 1;
175                 // Same with the byref argument passed on the stack
176                 s_res = o.Get_T2 (1.0f, s);
177                 if (s_res.o1 != 1 || s_res.o2 != 2 || s_res.o3 != 3)
178                         return 2;
179                 return 0;
180         }
181
182         class Foo5<T> {
183                 public static T Get_T (object o) {
184                         return (T)o;
185                 }
186         }
187
188         [Category ("DYNCALL")]
189         static int test_0_arm64_dyncall_vtypebyref_ret () {
190                 var s = new VTypeByRefStruct () { o1 = 1, o2 = 2, o3 = 3 };
191                 Type t = typeof (Foo5<>).MakeGenericType (new Type [] { typeof (VTypeByRefStruct) });
192                 var o = Activator.CreateInstance (t);
193                 try {
194                         var s_res = (VTypeByRefStruct)t.GetMethod ("Get_T").Invoke (o, new object [] { s });
195                         if (s_res.o1 != 1 || s_res.o2 != 2 || s_res.o3 != 3)
196                                 return 1;
197                 } catch (TargetInvocationException) {
198                         return 2;
199                 }
200                 return 0;
201         }
202
203         static int test_0_partial_sharing_regress_30204 () {
204                 var t = typeof (System.Collections.Generic.Comparer<System.Collections.Generic.KeyValuePair<string, string>>);
205                 var d = new SortedDictionary<string, string> ();
206                 d.Add ("key1", "banana");
207                 return d ["key1"] == "banana" ? 0 : 1;
208         }
209
210         class NullableMethods {
211                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
212                 public static bool GetHasValue<T>(Nullable<T> value) where T : struct {
213                         return value.HasValue;
214                 }
215
216                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
217                 public static T GetValue<T>(Nullable<T> value) where T : struct {
218                         return value.Value;
219                 }
220         }
221
222         [Category ("DYNCALL")]
223         public static int test_0_dyncall_nullable () {
224                 int? v;
225
226                 v = 42;
227                 NullableMethods.GetHasValue (v);
228                 bool b = (bool)typeof (NullableMethods).GetMethod ("GetHasValue").MakeGenericMethod (new Type [] { typeof (int) }).Invoke (null, new object [] { v });
229                 if (!b)
230                         return 1;
231                 v = null;
232                 b = (bool)typeof (NullableMethods).GetMethod ("GetHasValue").MakeGenericMethod (new Type [] { typeof (int) }).Invoke (null, new object [] { v });
233                 if (b)
234                         return 2;
235
236                 v = 42;
237                 NullableMethods.GetValue (v);
238                 var res = (int)typeof (NullableMethods).GetMethod ("GetValue").MakeGenericMethod (new Type [] { typeof (int) }).Invoke (null, new object [] { v });
239                 if (res != 42)
240                         return 3;
241                 return 0;
242         }
243 }