2009-05-26 Atsushi Enomoto <atsushi@ximian.com>
[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         [Category ("!FULLAOT")]
208         public static int test_0_generic_get_value_optimization_int () {
209                 int[] x = new int[] {100, 200};
210
211                 if (GenericClass<int>.Z (x, 0) != 100)
212                         return 2;
213
214                 if (GenericClass<int>.Z (x, 1) != 200)
215                         return 3;
216
217                 return 0;
218         }
219
220         public static int test_0_generic_get_value_optimization_vtype () {
221                 TestStruct[] arr = new TestStruct[] { new TestStruct (100, 200), new TestStruct (300, 400) };
222                 IEnumerator<TestStruct> enumerator = GenericClass<TestStruct>.Y (arr);
223                 TestStruct s;
224                 int sum = 0;
225                 while (enumerator.MoveNext ()) {
226                         s = enumerator.Current;
227                         sum += s.i + s.j;
228                 }
229
230                 if (sum != 1000)
231                         return 1;
232
233                 s = GenericClass<TestStruct>.Z (arr, 0);
234                 if (s.i != 100 || s.j != 200)
235                         return 2;
236
237                 s = GenericClass<TestStruct>.Z (arr, 1);
238                 if (s.i != 300 || s.j != 400)
239                         return 3;
240
241                 return 0;
242         }
243
244         public static int test_0_nullable_ldflda () {
245                 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
246         }
247
248         public struct GenericStruct<T> {
249                 public T t;
250
251                 public GenericStruct (T t) {
252                         this.t = t;
253                 }
254         }
255
256         public class GenericClass<T> {
257                 public T t;
258
259                 public GenericClass (T t) {
260                         this.t = t;
261                 }
262
263                 public GenericClass () {
264                 }
265
266                 public T ldobj_stobj (ref T t1, ref T t2) {
267                         t1 = t2;
268                         T t = t1;
269
270                         return t;
271                 }
272
273                 public T ldelem_stelem (T t) {
274                         T[] arr = new T [10];
275                         arr [0] = t;
276
277                         return arr [0];
278                 }
279
280                 public String toString (T t) {
281                         return t.ToString ();
282                 }
283
284                 public static IEnumerator<T> Y (IEnumerable <T> x)
285                 {
286                         return x.GetEnumerator ();
287                 }
288
289                 public static T Z (IList<T> x, int index)
290                 {
291                         return x [index];
292                 }
293
294         protected static T NullB = default(T);       
295         private static Nullable<bool>  _BIsA = null;
296         public static bool BIsAClazz {
297             get {
298                 _BIsA = false;
299                 return _BIsA.Value;
300             }
301         }
302         }
303
304         public class MRO : MarshalByRefObject {
305                 public GenericStruct<int> struct_field;
306                 public GenericClass<int> class_field;
307         }
308
309         public static int test_0_ldfld_stfld_mro () {
310                 MRO m = new MRO ();
311                 GenericStruct<int> s = new GenericStruct<int> (5);
312                 // This generates stfld
313                 m.struct_field = s;
314
315                 // This generates ldflda
316                 if (m.struct_field.t != 5)
317                         return 1;
318
319                 // This generates ldfld
320                 GenericStruct<int> s2 = m.struct_field;
321                 if (s2.t != 5)
322                         return 2;
323
324                 if (m.struct_field.t != 5)
325                         return 3;
326
327                 m.class_field = new GenericClass<int> (5);
328                 if (m.class_field.t != 5)
329                         return 4;
330
331                 return 0;
332         }
333
334         // FIXME:
335         [Category ("!FULLAOT")]
336     public static int test_0_generic_virtual_call_on_vtype_unbox () {
337                 object o = new Object ();
338         IFoo h = new Handler(o);
339
340         if (h.Bar<object> () != o)
341                         return 1;
342                 else
343                         return 0;
344     }
345
346         public static int test_0_box_brtrue_opt () {
347                 Foo<int> f = new Foo<int> (5);
348
349                 f [123] = 5;
350
351                 return 0;
352         }
353
354         public static int test_0_box_brtrue_opt_regress_81102 () {
355                 if (new Foo<int>(5).ToString () == "null")
356                         return 0;
357                 else
358                         return 1;
359         }
360
361         struct S {
362                 public int i;
363         }
364
365         public static int test_0_ldloca_initobj_opt () {
366                 if (new Foo<S> (new S ()).get_default ().i != 0)
367                         return 1;
368                 if (new Foo<object> (null).get_default () != null)
369                         return 2;
370                 return 0;
371         }
372
373         public static int test_0_variance_reflection () {
374                 // covariance on IEnumerator
375                 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
376                         return 1;
377                 // covariance on IEnumerator and covariance on arrays
378                 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
379                         return 2;
380                 // covariance and implemented interfaces
381                 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
382                         return 3;
383
384                 // contravariance on IComparer
385                 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
386                         return 4;
387                 // contravariance on IComparer, contravariance on arrays
388                 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
389                         return 5;
390                 // contravariance and interface inheritance
391                 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
392                         return 6;
393                 return 0;
394         }
395
396         public static int test_0_ldvirtftn_generic_method () {
397                 new Tests ().ldvirtftn<string> ();              
398
399                 return the_type == typeof (string) ? 0 : 1;
400         }
401
402         public static int test_0_throw_dead_this () {
403         new Foo<string> ("").throw_dead_this ();
404                 return 0;
405         }
406
407         // This cannot be made to work with full-aot, since there it is impossible to
408         // statically determine that Foo<string>.Bar <int> is needed, the code only
409         // references IFoo.Bar<int>
410         [Category ("!FULLAOT")]
411         public static int test_0_generic_virtual_on_interfaces () {
412                 Foo<string>.count1 = 0;
413                 Foo<string>.count2 = 0;
414                 Foo<string>.count3 = 0;
415
416                 IFoo f = new Foo<string> ("");
417                 for (int i = 0; i < 1000; ++i) {
418                         f.Bar <int> ();
419                         f.Bar <string> ();
420                         f.NonGeneric ();
421                 }
422
423                 if (Foo<string>.count1 != 1000)
424                         return 1;
425                 if (Foo<string>.count2 != 1000)
426                         return 2;
427                 if (Foo<string>.count3 != 1000)
428                         return 3;
429
430                 VirtualInterfaceCallFromGenericMethod<long> (f);
431
432                 return 0;
433         }
434
435         //repro for #505375
436         [Category ("!FULLAOT")]
437         public static int test_2_cprop_bug () {
438                 int idx = 0;
439                 int a = 1;
440                 var cmp = System.Collections.Generic.Comparer<int>.Default ;
441                 if (cmp.Compare (a, 0) > 0)
442                         a = 0;
443                 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
444                 return idx;
445         }
446
447         public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
448                 f.Bar <T> ();
449         }
450
451         public static Type the_type;
452
453         public void ldvirtftn<T> () {
454                 Foo <T> binding = new Foo <T> (default (T));
455
456                 binding.GenericEvent += event_handler;
457                 binding.fire ();
458         }
459
460         public virtual void event_handler<T> (Foo<T> sender) {
461                 the_type = typeof (T);
462         }
463
464         public interface IFoo {
465                 void NonGeneric ();
466                 object Bar<T>();
467         }
468
469         public class Foo<T1> : IFoo
470         {
471                 public Foo(T1 t1)
472                 {
473                         m_t1 = t1;
474                 }
475                 
476                 public override string ToString()
477                 {
478                         return Bar(m_t1 == null ? "null" : "null");
479                 }
480
481                 public String Bar (String s) {
482                         return s;
483                 }
484
485                 public int this [T1 key] {
486                         set {
487                                 if (key == null)
488                                         throw new ArgumentNullException ("key");
489                         }
490                 }
491
492                 public void throw_dead_this () {
493                         try {
494                                 new SomeClass().ThrowAnException();
495                         }
496                         catch {
497                         }
498                 }
499
500                 public T1 get_default () {
501                         return default (T1);
502                 }
503                 
504                 readonly T1 m_t1;
505
506                 public delegate void GenericEventHandler (Foo<T1> sender);
507
508                 public event GenericEventHandler GenericEvent;
509
510                 public void fire () {
511                         GenericEvent (this);
512                 }
513
514                 public static int count1, count2, count3;
515
516                 public void NonGeneric () {
517                         count3 ++;
518                 }
519
520                 public object Bar <T> () {
521                         if (typeof (T) == typeof (int))
522                                 count1 ++;
523                         else if (typeof (T) == typeof (string))
524                                 count2 ++;
525                         return null;
526                 }
527         }
528
529         public class SomeClass {
530                 public void ThrowAnException() {
531                         throw new Exception ("Something went wrong");
532                 }
533         }               
534
535         struct Handler : IFoo {
536                 object o;
537
538                 public Handler(object o) {
539                         this.o = o;
540                 }
541
542                 public void NonGeneric () {
543                 }
544
545                 public object Bar<T>() {
546                         return o;
547                 }
548         }
549
550         static bool IsNull<T> (T t)
551         {
552                 if (t == null)
553                         return true;
554                 else
555                         return false;
556         }
557
558         static object Box<T> (T t)
559         {
560                 return t;
561         }
562         
563         static T Unbox <T> (object o) {
564                 return (T) o;
565         }
566 }