[System] UriKind.RelativeOrAbsolute workaround.
[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 ("DYNCALL")]
110         [Category ("GSHAREDVT")]
111         static int test_0_arm64_dyncall_gsharedvt_out_hfa_double () {
112                 /* gsharedvt out trampoline with double hfa argument */
113                 double arg1 = 1.0f;
114
115                 var s = new Struct1 ();
116                 s.a = 1.0f;
117                 s.b = 2.0f;
118                 // Call Foo2.Get_T directly, so its gets an instance
119                 Foo2<Struct1>.Get_T (arg1, s);
120                 Type t = typeof (Foo3<>).MakeGenericType (new Type [] { typeof (Struct1) });
121                 // Call Foo3.Get_T, this will call the gsharedvt instance, which will call the non-gsharedvt instance
122                 var s_res = (Struct1)t.GetMethod ("Get_T").Invoke (null, new object [] { arg1, s });
123                 if (s_res.a != 1.0f || s_res.b != 2.0f)
124                         return 1;
125                 return 0;
126         }
127
128         [Category ("DYNCALL")]
129         [Category ("GSHAREDVT")]
130         static int test_0_arm64_dyncall_gsharedvt_out_hfa_float () {
131                 /* gsharedvt out trampoline with double hfa argument */
132                 double arg1 = 1.0f;
133
134                 var s = new Struct2 ();
135                 s.a = 1.0f;
136                 s.b = 2.0f;
137                 // Call Foo2.Get_T directly, so its gets an instance
138                 Foo2<Struct2>.Get_T (arg1, s);
139                 Type t = typeof (Foo3<>).MakeGenericType (new Type [] { typeof (Struct2) });
140                 // Call Foo3.Get_T, this will call the gsharedvt instance, which will call the non-gsharedvt instance
141                 var s_res = (Struct2)t.GetMethod ("Get_T").Invoke (null, new object [] { arg1, s });
142                 if (s_res.a != 1.0f || s_res.b != 2.0f)
143                         return 1;
144                 return 0;
145         }
146
147         interface IFaceFoo4<T> {
148                 T Get_T (double d, T t);
149                 T Get_T2 (double d, T t);
150         }
151
152         class Foo4<T> : IFaceFoo4<T> {
153                 public T Get_T (double d, T t) {
154                         return Foo2<T>.Get_T (d, t);
155                 }
156                 public T Get_T2 (double d, T t) {
157                         return Foo2<T>.Get_T2 (d, 1, 2, 3, 4, 5, 6, 7, 8, t);
158                 }
159         }
160
161         struct VTypeByRefStruct {
162                 public long o1, o2, o3;
163         }
164
165         [Category ("GSHAREDVT")]
166         public static int test_0_arm64_gsharedvt_out_vtypebyref () {
167                 /* gsharedvt out trampoline with vtypebyref argument */
168                 var s = new VTypeByRefStruct () { o1 = 1, o2 = 2, o3 = 3 };
169
170                 // Call Foo2.Get_T directly, so its gets an instance
171                 Foo2<VTypeByRefStruct>.Get_T (1.0f, s);
172                 var o = (IFaceFoo4<VTypeByRefStruct>)Activator.CreateInstance (typeof (Foo4<>).MakeGenericType (new Type [] { typeof (VTypeByRefStruct) }));
173                 // Call Foo4.Get_T, this will call the gsharedvt instance, which will call the non-gsharedvt instance
174                 var s_res = o.Get_T (1.0f, s);
175                 if (s_res.o1 != 1 || s_res.o2 != 2 || s_res.o3 != 3)
176                         return 1;
177                 // Same with the byref argument passed on the stack
178                 s_res = o.Get_T2 (1.0f, s);
179                 if (s_res.o1 != 1 || s_res.o2 != 2 || s_res.o3 != 3)
180                         return 2;
181                 return 0;
182         }
183
184         class Foo5<T> {
185                 public static T Get_T (object o) {
186                         return (T)o;
187                 }
188         }
189
190         [Category ("DYNCALL")]
191         static int test_0_arm64_dyncall_vtypebyref_ret () {
192                 var s = new VTypeByRefStruct () { o1 = 1, o2 = 2, o3 = 3 };
193                 Type t = typeof (Foo5<>).MakeGenericType (new Type [] { typeof (VTypeByRefStruct) });
194                 var o = Activator.CreateInstance (t);
195                 try {
196                         var s_res = (VTypeByRefStruct)t.GetMethod ("Get_T").Invoke (o, new object [] { s });
197                         if (s_res.o1 != 1 || s_res.o2 != 2 || s_res.o3 != 3)
198                                 return 1;
199                 } catch (TargetInvocationException) {
200                         return 2;
201                 }
202                 return 0;
203         }
204
205         static int test_0_partial_sharing_regress_30204 () {
206                 var t = typeof (System.Collections.Generic.Comparer<System.Collections.Generic.KeyValuePair<string, string>>);
207                 var d = new SortedDictionary<string, string> ();
208                 d.Add ("key1", "banana");
209                 return d ["key1"] == "banana" ? 0 : 1;
210         }
211
212         class NullableMethods {
213                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
214                 public static bool GetHasValue<T>(Nullable<T> value) where T : struct {
215                         return value.HasValue;
216                 }
217
218                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
219                 public static T GetValue<T>(Nullable<T> value) where T : struct {
220                         return value.Value;
221                 }
222         }
223
224         [Category ("DYNCALL")]
225         public static int test_0_dyncall_nullable () {
226                 int? v;
227
228                 v = 42;
229                 NullableMethods.GetHasValue (v);
230                 bool b = (bool)typeof (NullableMethods).GetMethod ("GetHasValue").MakeGenericMethod (new Type [] { typeof (int) }).Invoke (null, new object [] { v });
231                 if (!b)
232                         return 1;
233                 v = null;
234                 b = (bool)typeof (NullableMethods).GetMethod ("GetHasValue").MakeGenericMethod (new Type [] { typeof (int) }).Invoke (null, new object [] { v });
235                 if (b)
236                         return 2;
237
238                 v = 42;
239                 NullableMethods.GetValue (v);
240                 var res = (int)typeof (NullableMethods).GetMethod ("GetValue").MakeGenericMethod (new Type [] { typeof (int) }).Invoke (null, new object [] { v });
241                 if (res != 42)
242                         return 3;
243                 return 0;
244         }
245
246         enum AnEnum {
247                 A = 0,
248                 B = 1
249         }
250
251         public static int test_0_enum_eq_comparer () {
252                 var c = EqualityComparer<AnEnum>.Default;
253                 return (!c.Equals (AnEnum.A, AnEnum.B) && c.Equals (AnEnum.A, AnEnum.A)) ? 0 : 1;
254         }
255
256         public static int test_0_enum_comparer () {
257                 var c = Comparer<AnEnum>.Default;
258                 return c.Compare (AnEnum.A, AnEnum.A);
259         }
260 }