1cdfc2b356782faf915afa28d198ad399159f146
[mono.git] / mono / mini / generics.cs
1 using System;
2 using System.Collections.Generic;
3
4 class Tests {
5
6         struct TestStruct {
7                 public int i;
8                 public int j;
9
10                 public TestStruct (int i, int j) {
11                         this.i = i;
12                         this.j = j;
13                 }
14         }
15
16         class Enumerator <T> : MyIEnumerator <T> {
17                 T MyIEnumerator<T>.Current {
18                         get {
19                                 return default(T);
20                         }
21                 }
22
23                 bool MyIEnumerator<T>.MoveNext () {
24                         return true;
25                 }
26         }
27
28         class Comparer <T> : IComparer <T> {
29                 bool IComparer<T>.Compare (T x, T y) {
30                         return true;
31                 }
32         }
33
34         static int Main (string[] args)
35         {
36                 return TestDriver.RunTests (typeof (Tests), args);
37         }
38
39         public static int test_1_nullable_unbox ()
40         {
41                 return Unbox<int?> (1).Value;
42         }
43
44         public static int test_1_nullable_unbox_null ()
45         {
46                 return Unbox<int?> (null).HasValue ? 0 : 1;
47         }
48
49         public static int test_1_nullable_box ()
50         {
51                 return (int) Box<int?> (1);
52         }
53
54         public static int test_1_nullable_box_null ()
55         {
56                 return Box<int?> (null) == null ? 1 : 0;
57         }
58
59         public static int test_1_isinst_nullable ()
60         {
61                 object o = 1;
62                 return (o is int?) ? 1 : 0;
63         }
64
65         public static int test_1_nullable_unbox_vtype ()
66         {
67                 return Unbox<TestStruct?> (new TestStruct (1, 2)).Value.i;
68         }
69
70         public static int test_1_nullable_unbox_null_vtype ()
71         {
72                 return Unbox<TestStruct?> (null).HasValue ? 0 : 1;
73         }
74
75         public static int test_1_nullable_box_vtype ()
76         {
77                 return ((TestStruct)(Box<TestStruct?> (new TestStruct (1, 2)))).i;
78         }
79
80         public static int test_1_nullable_box_null_vtype ()
81         {
82                 return Box<TestStruct?> (null) == null ? 1 : 0;
83         }
84
85         public static int test_1_isinst_nullable_vtype ()
86         {
87                 object o = new TestStruct (1, 2);
88                 return (o is TestStruct?) ? 1 : 0;
89         }
90
91         public static int test_0_nullable_normal_unbox ()
92         {
93                 int? i = 5;
94
95                 object o = i;
96                 // This uses unbox instead of unbox_any
97                 int? j = (int?)o;
98
99                 if (j != 5)
100                         return 1;
101
102                 return 0;
103         }
104
105         public static void stelem_any<T> (T[] arr, T elem) {
106                 arr [0] = elem;
107         }
108
109         public static T ldelem_any<T> (T[] arr) {
110                 return arr [0];
111         }
112
113         public static int test_1_ldelem_stelem_any_int () {
114                 int[] arr = new int [3];
115                 stelem_any (arr, 1);
116
117                 return ldelem_any (arr);
118         }
119
120         public static T return_ref<T> (ref T t) {
121                 return t;
122         }
123
124         public static T ldelema_any<T> (T[] arr) {
125                 return return_ref<T> (ref arr [0]);
126         }
127
128         public static int test_0_ldelema () {
129                 string[] arr = new string [1];
130
131                 arr [0] = "Hello";
132
133                 if (ldelema_any <string> (arr) == "Hello")
134                         return 0;
135                 else
136                         return 1;
137         }
138
139         public static T[,] newarr_multi<T> () {
140                 return new T [1, 1];
141         }
142
143         public static int test_0_newarr_multi_dim () {
144                 return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
145         }
146
147         interface ITest
148         {
149                 void Foo<T> ();
150         }
151
152         public static int test_0_iface_call_null_bug_77442 () {
153                 ITest test = null;
154
155                 try {
156                         test.Foo<int> ();
157                 }
158                 catch (NullReferenceException) {
159                         return 0;
160                 }
161                 
162                 return 1;
163         }
164
165         public static int test_18_ldobj_stobj_generics () {
166                 GenericClass<int> t = new GenericClass <int> ();
167                 int i = 5;
168                 int j = 6;
169                 return t.ldobj_stobj (ref i, ref j) + i + j;
170         }
171
172         public static int test_5_ldelem_stelem_generics () {
173                 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
174
175                 TestStruct s = new TestStruct (5, 5);
176                 return t.ldelem_stelem (s).i;
177         }
178
179         public static int test_0_constrained_vtype_box () {
180                 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
181
182                 return t.toString (new TestStruct ()) == "Tests+TestStruct" ? 0 : 1;
183         }
184
185         public static int test_0_constrained_vtype () {
186                 GenericClass<int> t = new GenericClass<int> ();
187
188                 return t.toString (1234) == "1234" ? 0 : 1;
189         }
190
191         public static int test_0_constrained_reftype () {
192                 GenericClass<String> t = new GenericClass<String> ();
193
194                 return t.toString ("1234") == "1234" ? 0 : 1;
195         }
196
197         public static int test_0_box_brtrue_optimizations () {
198                 if (IsNull<int>(5))
199                         return 1;
200
201                 if (!IsNull<object>(null))
202                         return 1;
203
204                 return 0;
205         }
206
207         public static int test_0_generic_get_value_optimization_int () {
208                 int[] x = new int[] {100, 200};
209
210                 if (GenericClass<int>.Z (x, 0) != 100)
211                         return 2;
212
213                 if (GenericClass<int>.Z (x, 1) != 200)
214                         return 3;
215
216                 return 0;
217         }
218
219         public static int test_0_generic_get_value_optimization_vtype () {
220                 TestStruct[] arr = new TestStruct[] { new TestStruct (100, 200), new TestStruct (300, 400) };
221                 IEnumerator<TestStruct> enumerator = GenericClass<TestStruct>.Y (arr);
222                 TestStruct s;
223                 int sum = 0;
224                 while (enumerator.MoveNext ()) {
225                         s = enumerator.Current;
226                         sum += s.i + s.j;
227                 }
228
229                 if (sum != 1000)
230                         return 1;
231
232                 s = GenericClass<TestStruct>.Z (arr, 0);
233                 if (s.i != 100 || s.j != 200)
234                         return 2;
235
236                 s = GenericClass<TestStruct>.Z (arr, 1);
237                 if (s.i != 300 || s.j != 400)
238                         return 3;
239
240                 return 0;
241         }
242
243         public static int test_0_nullable_ldflda () {
244                 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
245         }
246
247         public struct GenericStruct<T> {
248                 public T t;
249
250                 public GenericStruct (T t) {
251                         this.t = t;
252                 }
253         }
254
255         public class GenericClass<T> {
256                 public T t;
257
258                 public GenericClass (T t) {
259                         this.t = t;
260                 }
261
262                 public GenericClass () {
263                 }
264
265                 public T ldobj_stobj (ref T t1, ref T t2) {
266                         t1 = t2;
267                         T t = t1;
268
269                         return t;
270                 }
271
272                 public T ldelem_stelem (T t) {
273                         T[] arr = new T [10];
274                         arr [0] = t;
275
276                         return arr [0];
277                 }
278
279                 public String toString (T t) {
280                         return t.ToString ();
281                 }
282
283                 public static IEnumerator<T> Y (IEnumerable <T> x)
284                 {
285                         return x.GetEnumerator ();
286                 }
287
288                 public static T Z (IList<T> x, int index)
289                 {
290                         return x [index];
291                 }
292
293         protected static T NullB = default(T);       
294         private static Nullable<bool>  _BIsA = null;
295         public static bool BIsAClazz {
296             get {
297                 _BIsA = false;
298                 return _BIsA.Value;
299             }
300         }
301         }
302
303         public class MRO : MarshalByRefObject {
304                 public GenericStruct<int> struct_field;
305                 public GenericClass<int> class_field;
306         }
307
308         public static int test_0_ldfld_stfld_mro () {
309                 MRO m = new MRO ();
310                 GenericStruct<int> s = new GenericStruct<int> (5);
311                 // This generates stfld
312                 m.struct_field = s;
313
314                 // This generates ldflda
315                 if (m.struct_field.t != 5)
316                         return 1;
317
318                 // This generates ldfld
319                 GenericStruct<int> s2 = m.struct_field;
320                 if (s2.t != 5)
321                         return 2;
322
323                 if (m.struct_field.t != 5)
324                         return 3;
325
326                 m.class_field = new GenericClass<int> (5);
327                 if (m.class_field.t != 5)
328                         return 4;
329
330                 return 0;
331         }
332
333         // FIXME:
334         [Category ("!FULLAOT")]
335     public static int test_0_generic_virtual_call_on_vtype_unbox () {
336                 object o = new Object ();
337         IFoo h = new Handler(o);
338
339         if (h.Bar<object> () != o)
340                         return 1;
341                 else
342                         return 0;
343     }
344
345         public static int test_0_box_brtrue_opt () {
346                 Foo<int> f = new Foo<int> (5);
347
348                 f [123] = 5;
349
350                 return 0;
351         }
352
353         public static int test_0_box_brtrue_opt_regress_81102 () {
354                 if (new Foo<int>(5).ToString () == "null")
355                         return 0;
356                 else
357                         return 1;
358         }
359
360         struct S {
361                 public int i;
362         }
363
364         public static int test_0_ldloca_initobj_opt () {
365                 if (new Foo<S> (new S ()).get_default ().i != 0)
366                         return 1;
367                 if (new Foo<object> (null).get_default () != null)
368                         return 2;
369                 return 0;
370         }
371
372         public static int test_0_variance_reflection () {
373                 // covariance on IEnumerator
374                 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
375                         return 1;
376                 // covariance on IEnumerator and covariance on arrays
377                 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
378                         return 2;
379                 // covariance and implemented interfaces
380                 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
381                         return 3;
382
383                 // contravariance on IComparer
384                 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
385                         return 4;
386                 // contravariance on IComparer, contravariance on arrays
387                 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
388                         return 5;
389                 // contravariance and interface inheritance
390                 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
391                         return 6;
392                 return 0;
393         }
394
395         public static int test_0_ldvirtftn_generic_method () {
396                 new Tests ().ldvirtftn<string> ();              
397
398                 return the_type == typeof (string) ? 0 : 1;
399         }
400
401         public static int test_0_throw_dead_this () {
402         new Foo<string> ("").throw_dead_this ();
403                 return 0;
404         }
405
406         // This cannot be made to work with full-aot, since there it is impossible to
407         // statically determine that Foo<string>.Bar <int> is needed, the code only
408         // references IFoo.Bar<int>
409         [Category ("!FULLAOT")]
410         public static int test_0_generic_virtual_on_interfaces () {
411                 Foo<string>.count1 = 0;
412                 Foo<string>.count2 = 0;
413                 Foo<string>.count3 = 0;
414
415                 IFoo f = new Foo<string> ("");
416                 for (int i = 0; i < 1000; ++i) {
417                         f.Bar <int> ();
418                         f.Bar <string> ();
419                         f.NonGeneric ();
420                 }
421
422                 if (Foo<string>.count1 != 1000)
423                         return 1;
424                 if (Foo<string>.count2 != 1000)
425                         return 2;
426                 if (Foo<string>.count3 != 1000)
427                         return 3;
428
429                 VirtualInterfaceCallFromGenericMethod<long> (f);
430
431                 return 0;
432         }
433
434         //repro for #505375
435         [Category ("!FULLAOT")]
436         public static int test_2_cprop_bug () {
437                 int idx = 0;
438                 int a = 1;
439                 var cmp = System.Collections.Generic.Comparer<int>.Default ;
440                 if (cmp.Compare (a, 0) > 0)
441                         a = 0;
442                 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
443                 return idx;
444         }
445
446         public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
447                 f.Bar <T> ();
448         }
449
450         public static Type the_type;
451
452         public void ldvirtftn<T> () {
453                 Foo <T> binding = new Foo <T> (default (T));
454
455                 binding.GenericEvent += event_handler;
456                 binding.fire ();
457         }
458
459         public virtual void event_handler<T> (Foo<T> sender) {
460                 the_type = typeof (T);
461         }
462
463         public interface IFoo {
464                 void NonGeneric ();
465                 object Bar<T>();
466         }
467
468         public class Foo<T1> : IFoo
469         {
470                 public Foo(T1 t1)
471                 {
472                         m_t1 = t1;
473                 }
474                 
475                 public override string ToString()
476                 {
477                         return Bar(m_t1 == null ? "null" : "null");
478                 }
479
480                 public String Bar (String s) {
481                         return s;
482                 }
483
484                 public int this [T1 key] {
485                         set {
486                                 if (key == null)
487                                         throw new ArgumentNullException ("key");
488                         }
489                 }
490
491                 public void throw_dead_this () {
492                         try {
493                                 new SomeClass().ThrowAnException();
494                         }
495                         catch {
496                         }
497                 }
498
499                 public T1 get_default () {
500                         return default (T1);
501                 }
502                 
503                 readonly T1 m_t1;
504
505                 public delegate void GenericEventHandler (Foo<T1> sender);
506
507                 public event GenericEventHandler GenericEvent;
508
509                 public void fire () {
510                         GenericEvent (this);
511                 }
512
513                 public static int count1, count2, count3;
514
515                 public void NonGeneric () {
516                         count3 ++;
517                 }
518
519                 public object Bar <T> () {
520                         if (typeof (T) == typeof (int))
521                                 count1 ++;
522                         else if (typeof (T) == typeof (string))
523                                 count2 ++;
524                         return null;
525                 }
526         }
527
528         public class SomeClass {
529                 public void ThrowAnException() {
530                         throw new Exception ("Something went wrong");
531                 }
532         }               
533
534         struct Handler : IFoo {
535                 object o;
536
537                 public Handler(object o) {
538                         this.o = o;
539                 }
540
541                 public void NonGeneric () {
542                 }
543
544                 public object Bar<T>() {
545                         return o;
546                 }
547         }
548
549         static bool IsNull<T> (T t)
550         {
551                 if (t == null)
552                         return true;
553                 else
554                         return false;
555         }
556
557         static object Box<T> (T t)
558         {
559                 return t;
560         }
561         
562         static T Unbox <T> (object o) {
563                 return (T) o;
564         }
565 }