Increase an aot buffer size again.
[mono.git] / mono / mini / generics.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4
5 class Tests {
6
7         struct TestStruct {
8                 public int i;
9                 public int j;
10
11                 public TestStruct (int i, int j) {
12                         this.i = i;
13                         this.j = j;
14                 }
15         }
16
17         class Enumerator <T> : MyIEnumerator <T> {
18                 T MyIEnumerator<T>.Current {
19                         get {
20                                 return default(T);
21                         }
22                 }
23
24                 bool MyIEnumerator<T>.MoveNext () {
25                         return true;
26                 }
27         }
28
29         class Comparer <T> : IComparer <T> {
30                 bool IComparer<T>.Compare (T x, T y) {
31                         return true;
32                 }
33         }
34
35         static int Main (string[] args)
36         {
37                 return TestDriver.RunTests (typeof (Tests), args);
38         }
39
40         public static int test_1_nullable_unbox ()
41         {
42                 return Unbox<int?> (1).Value;
43         }
44
45         public static int test_1_nullable_unbox_null ()
46         {
47                 return Unbox<int?> (null).HasValue ? 0 : 1;
48         }
49
50         public static int test_1_nullable_box ()
51         {
52                 return (int) Box<int?> (1);
53         }
54
55         public static int test_1_nullable_box_null ()
56         {
57                 return Box<int?> (null) == null ? 1 : 0;
58         }
59
60         public static int test_1_isinst_nullable ()
61         {
62                 object o = 1;
63                 return (o is int?) ? 1 : 0;
64         }
65
66         public static int test_1_nullable_unbox_vtype ()
67         {
68                 return Unbox<TestStruct?> (new TestStruct (1, 2)).Value.i;
69         }
70
71         public static int test_1_nullable_unbox_null_vtype ()
72         {
73                 return Unbox<TestStruct?> (null).HasValue ? 0 : 1;
74         }
75
76         public static int test_1_nullable_box_vtype ()
77         {
78                 return ((TestStruct)(Box<TestStruct?> (new TestStruct (1, 2)))).i;
79         }
80
81         public static int test_1_nullable_box_null_vtype ()
82         {
83                 return Box<TestStruct?> (null) == null ? 1 : 0;
84         }
85
86         public static int test_1_isinst_nullable_vtype ()
87         {
88                 object o = new TestStruct (1, 2);
89                 return (o is TestStruct?) ? 1 : 0;
90         }
91
92         public static int test_0_nullable_normal_unbox ()
93         {
94                 int? i = 5;
95
96                 object o = i;
97                 // This uses unbox instead of unbox_any
98                 int? j = (int?)o;
99
100                 if (j != 5)
101                         return 1;
102
103                 return 0;
104         }
105
106         public static void stelem_any<T> (T[] arr, T elem) {
107                 arr [0] = elem;
108         }
109
110         public static T ldelem_any<T> (T[] arr) {
111                 return arr [0];
112         }
113
114         public static int test_1_ldelem_stelem_any_int () {
115                 int[] arr = new int [3];
116                 stelem_any (arr, 1);
117
118                 return ldelem_any (arr);
119         }
120
121         public static T return_ref<T> (ref T t) {
122                 return t;
123         }
124
125         public static T ldelema_any<T> (T[] arr) {
126                 return return_ref<T> (ref arr [0]);
127         }
128
129         public static int test_0_ldelema () {
130                 string[] arr = new string [1];
131
132                 arr [0] = "Hello";
133
134                 if (ldelema_any <string> (arr) == "Hello")
135                         return 0;
136                 else
137                         return 1;
138         }
139
140         public static T[,] newarr_multi<T> () {
141                 return new T [1, 1];
142         }
143
144         public static int test_0_newarr_multi_dim () {
145                 return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
146         }
147
148         interface ITest
149         {
150                 void Foo<T> ();
151         }
152
153         public static int test_0_iface_call_null_bug_77442 () {
154                 ITest test = null;
155
156                 try {
157                         test.Foo<int> ();
158                 }
159                 catch (NullReferenceException) {
160                         return 0;
161                 }
162                 
163                 return 1;
164         }
165
166         public static int test_18_ldobj_stobj_generics () {
167                 GenericClass<int> t = new GenericClass <int> ();
168                 int i = 5;
169                 int j = 6;
170                 return t.ldobj_stobj (ref i, ref j) + i + j;
171         }
172
173         public static int test_5_ldelem_stelem_generics () {
174                 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
175
176                 TestStruct s = new TestStruct (5, 5);
177                 return t.ldelem_stelem (s).i;
178         }
179
180         public static int test_0_constrained_vtype_box () {
181                 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
182
183                 return t.toString (new TestStruct ()) == "Tests+TestStruct" ? 0 : 1;
184         }
185
186         public static int test_0_constrained_vtype () {
187                 GenericClass<int> t = new GenericClass<int> ();
188
189                 return t.toString (1234) == "1234" ? 0 : 1;
190         }
191
192         public static int test_0_constrained_reftype () {
193                 GenericClass<String> t = new GenericClass<String> ();
194
195                 return t.toString ("1234") == "1234" ? 0 : 1;
196         }
197
198         public static int test_0_box_brtrue_optimizations () {
199                 if (IsNull<int>(5))
200                         return 1;
201
202                 if (!IsNull<object>(null))
203                         return 1;
204
205                 return 0;
206         }
207
208         [Category ("!FULLAOT")]
209         public static int test_0_generic_get_value_optimization_int () {
210                 int[] x = new int[] {100, 200};
211
212                 if (GenericClass<int>.Z (x, 0) != 100)
213                         return 2;
214
215                 if (GenericClass<int>.Z (x, 1) != 200)
216                         return 3;
217
218                 return 0;
219         }
220
221         public static int test_0_generic_get_value_optimization_vtype () {
222                 TestStruct[] arr = new TestStruct[] { new TestStruct (100, 200), new TestStruct (300, 400) };
223                 IEnumerator<TestStruct> enumerator = GenericClass<TestStruct>.Y (arr);
224                 TestStruct s;
225                 int sum = 0;
226                 while (enumerator.MoveNext ()) {
227                         s = enumerator.Current;
228                         sum += s.i + s.j;
229                 }
230
231                 if (sum != 1000)
232                         return 1;
233
234                 s = GenericClass<TestStruct>.Z (arr, 0);
235                 if (s.i != 100 || s.j != 200)
236                         return 2;
237
238                 s = GenericClass<TestStruct>.Z (arr, 1);
239                 if (s.i != 300 || s.j != 400)
240                         return 3;
241
242                 return 0;
243         }
244
245         public static int test_0_nullable_ldflda () {
246                 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
247         }
248
249         public struct GenericStruct<T> {
250                 public T t;
251
252                 public GenericStruct (T t) {
253                         this.t = t;
254                 }
255         }
256
257         public class GenericClass<T> {
258                 public T t;
259
260                 public GenericClass (T t) {
261                         this.t = t;
262                 }
263
264                 public GenericClass () {
265                 }
266
267                 public T ldobj_stobj (ref T t1, ref T t2) {
268                         t1 = t2;
269                         T t = t1;
270
271                         return t;
272                 }
273
274                 public T ldelem_stelem (T t) {
275                         T[] arr = new T [10];
276                         arr [0] = t;
277
278                         return arr [0];
279                 }
280
281                 public String toString (T t) {
282                         return t.ToString ();
283                 }
284
285                 public static IEnumerator<T> Y (IEnumerable <T> x)
286                 {
287                         return x.GetEnumerator ();
288                 }
289
290                 public static T Z (IList<T> x, int index)
291                 {
292                         return x [index];
293                 }
294
295         protected static T NullB = default(T);       
296         private static Nullable<bool>  _BIsA = null;
297         public static bool BIsAClazz {
298             get {
299                 _BIsA = false;
300                 return _BIsA.Value;
301             }
302         }
303         }
304
305         public class MRO : MarshalByRefObject {
306                 public GenericStruct<int> struct_field;
307                 public GenericClass<int> class_field;
308         }
309
310         public static int test_0_ldfld_stfld_mro () {
311                 MRO m = new MRO ();
312                 GenericStruct<int> s = new GenericStruct<int> (5);
313                 // This generates stfld
314                 m.struct_field = s;
315
316                 // This generates ldflda
317                 if (m.struct_field.t != 5)
318                         return 1;
319
320                 // This generates ldfld
321                 GenericStruct<int> s2 = m.struct_field;
322                 if (s2.t != 5)
323                         return 2;
324
325                 if (m.struct_field.t != 5)
326                         return 3;
327
328                 m.class_field = new GenericClass<int> (5);
329                 if (m.class_field.t != 5)
330                         return 4;
331
332                 return 0;
333         }
334
335         // FIXME:
336         [Category ("!FULLAOT")]
337     public static int test_0_generic_virtual_call_on_vtype_unbox () {
338                 object o = new Object ();
339         IFoo h = new Handler(o);
340
341         if (h.Bar<object> () != o)
342                         return 1;
343                 else
344                         return 0;
345     }
346
347         public static int test_0_box_brtrue_opt () {
348                 Foo<int> f = new Foo<int> (5);
349
350                 f [123] = 5;
351
352                 return 0;
353         }
354
355         public static int test_0_box_brtrue_opt_regress_81102 () {
356                 if (new Foo<int>(5).ToString () == "null")
357                         return 0;
358                 else
359                         return 1;
360         }
361
362         struct S {
363                 public int i;
364         }
365
366         public static int test_0_ldloca_initobj_opt () {
367                 if (new Foo<S> (new S ()).get_default ().i != 0)
368                         return 1;
369                 if (new Foo<object> (null).get_default () != null)
370                         return 2;
371                 return 0;
372         }
373
374         public static int test_0_variance_reflection () {
375                 // covariance on IEnumerator
376                 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
377                         return 1;
378                 // covariance on IEnumerator and covariance on arrays
379                 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
380                         return 2;
381                 // covariance and implemented interfaces
382                 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
383                         return 3;
384
385                 // contravariance on IComparer
386                 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
387                         return 4;
388                 // contravariance on IComparer, contravariance on arrays
389                 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
390                         return 5;
391                 // contravariance and interface inheritance
392                 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
393                         return 6;
394                 return 0;
395         }
396
397         public static int test_0_ldvirtftn_generic_method () {
398                 new Tests ().ldvirtftn<string> ();              
399
400                 return the_type == typeof (string) ? 0 : 1;
401         }
402
403         public static int test_0_throw_dead_this () {
404         new Foo<string> ("").throw_dead_this ();
405                 return 0;
406         }
407
408         struct S<T> {}
409
410         public static int test_0_inline_infinite_polymorphic_recursion () {
411            f<int>(0);
412
413                    return 0;
414         }
415
416         private static void f<T>(int i) {
417                 if(i==42) f<S<T>>(i);
418         }
419
420         // This cannot be made to work with full-aot, since there it is impossible to
421         // statically determine that Foo<string>.Bar <int> is needed, the code only
422         // references IFoo.Bar<int>
423         [Category ("!FULLAOT")]
424         public static int test_0_generic_virtual_on_interfaces () {
425                 Foo<string>.count1 = 0;
426                 Foo<string>.count2 = 0;
427                 Foo<string>.count3 = 0;
428
429                 IFoo f = new Foo<string> ("");
430                 for (int i = 0; i < 1000; ++i) {
431                         f.Bar <int> ();
432                         f.Bar <string> ();
433                         f.NonGeneric ();
434                 }
435
436                 if (Foo<string>.count1 != 1000)
437                         return 1;
438                 if (Foo<string>.count2 != 1000)
439                         return 2;
440                 if (Foo<string>.count3 != 1000)
441                         return 3;
442
443                 VirtualInterfaceCallFromGenericMethod<long> (f);
444
445                 return 0;
446         }
447
448         //repro for #505375
449         [Category ("!FULLAOT")]
450         public static int test_2_cprop_bug () {
451                 int idx = 0;
452                 int a = 1;
453                 var cmp = System.Collections.Generic.Comparer<int>.Default ;
454                 if (cmp.Compare (a, 0) > 0)
455                         a = 0;
456                 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
457                 return idx;
458         }
459
460         enum MyEnumUlong : ulong {
461                 Value_2 = 2
462         }
463
464         public static int test_0_regress_550964_constrained_enum_long () {
465         MyEnumUlong a = MyEnumUlong.Value_2;
466         MyEnumUlong b = MyEnumUlong.Value_2;
467
468         return Pan (a, b) ? 0 : 1;
469         }
470
471     static bool Pan<T> (T a, T b)
472     {
473         return a.Equals (b);
474     }
475
476         public class XElement {
477                 public string Value {
478                         get; set;
479                 }
480         }
481
482         public static int test_0_fullaot_linq () {
483                 var allWords = new XElement [] { new XElement { Value = "one" } };
484                 var filteredWords = allWords.Where(kw => kw.Value.StartsWith("T"));
485                 return filteredWords.Count ();
486         }
487
488         public static int test_0_fullaot_comparer_t () {
489                 var l = new SortedList <TimeSpan, int> ();
490                 return l.Count;
491         }
492
493         public static int test_0_fullaot_comparer_t_2 () {
494                 var l = new Dictionary <TimeSpan, int> ();
495                 return l.Count;
496         }
497
498         static void enumerate<T> (IEnumerable<T> arr) {
499                 foreach (var o in arr)
500                         ;
501                 int c = ((ICollection<T>)arr).Count;
502         }
503
504         /* Test that treating arrays as generic collections works with full-aot */
505         public static int test_0_fullaot_array_wrappers () {
506                 Tests[] arr = new Tests [10];
507                 enumerate<Tests> (arr);
508                 return 0;
509         }
510
511         static int cctor_count = 0;
512
513     public abstract class Beta<TChanged> 
514     {           
515         static Beta()
516         {
517                         cctor_count ++;
518         }
519     }   
520     
521     public class Gamma<T> : Beta<T> 
522     {   
523         static Gamma()
524         {
525         }
526     }
527
528         // #519336    
529         public static int test_2_generic_class_init_gshared_ctor () {
530                 new Gamma<object>();
531                 new Gamma<string>();
532
533                 return cctor_count;
534         }
535
536         static int cctor_count2 = 0;
537
538         class ServiceController<T> {
539                 static ServiceController () {
540                         cctor_count2 ++;
541                 }
542
543                 public ServiceController () {
544                 }
545         }
546
547         static ServiceController<T> Create<T>() {
548                 return new ServiceController<T>();
549         }
550
551         // #631409
552         public static int test_2_generic_class_init_gshared_ctor_from_gshared () {
553                 Create<object> ();
554                 Create<string> ();
555
556                 return cctor_count2;
557         }
558
559         public static Type get_type<T> () {
560                 return typeof (T);
561         }
562
563         public static int test_0_gshared_delegate_rgctx () {
564                 Func<Type> t = new Func<Type> (get_type<string>);
565
566                 if (t () == typeof (string))
567                         return 0;
568                 else
569                         return 1;
570         }
571
572         // Creating a delegate from a generic method from gshared code
573         public static int test_0_gshared_delegate_from_gshared () {
574                 if (gshared_delegate_from_gshared <object> () != 0)
575                         return 1;
576                 if (gshared_delegate_from_gshared <string> () != 0)
577                         return 2;
578                 return 0;
579         }
580
581         public static int gshared_delegate_from_gshared <T> () {
582                 Func<Type> t = new Func<Type> (get_type<T>);
583
584                 return t () == typeof (T) ? 0 : 1;
585         }
586
587         public static int test_0_marshalbyref_call_from_gshared_virt_elim () {
588                 /* Calling a virtual method from gshared code which is changed to a nonvirt call */
589                 Class1<object> o = new Class1<object> ();
590                 o.Do (new Class2<object> ());
591                 return 0;
592         }
593
594         class Pair<TKey, TValue> {
595                 public static KeyValuePair<TKey, TValue> make_pair (TKey key, TValue value)
596                         {
597                                 return new KeyValuePair<TKey, TValue> (key, value);
598                         }
599
600                 public delegate TRet Transform<TRet> (TKey key, TValue value);
601         }
602
603         public static int test_0_bug_620864 () {
604                 var d = new Pair<string, Type>.Transform<KeyValuePair<string, Type>> (Pair<string, Type>.make_pair);
605
606                 var p = d ("FOO", typeof (int));
607                 if (p.Key != "FOO" || p.Value != typeof (int))
608                         return 1;
609
610                 return 0;
611         }
612
613
614         struct RecStruct<T> {
615                 public void foo (RecStruct<RecStruct<T>> baz) {
616                 }
617         }
618
619         public static int test_0_infinite_generic_recursion () {
620                 // Check that the AOT compile can deal with infinite generic recursion through
621                 // parameter types
622                 RecStruct<int> bla;
623
624                 return 0;
625         }
626
627         struct FooStruct {
628         }
629
630         bool IsNull2 <T> (object value) where T : struct {
631                 T? item = (T?) value;
632
633                 if (item.HasValue)
634                         return false;
635
636                 return true;
637         }
638
639         public static int test_0_full_aot_nullable_unbox_from_gshared_code () {
640                 if (!new Tests ().IsNull2<FooStruct> (null))
641                         return 1;
642                 if (new Tests ().IsNull2<FooStruct> (new FooStruct ()))
643                         return 2;
644                 return 0;
645         }
646
647         public static int test_0_partial_sharing () {
648                 if (PartialShared1 (new List<string> (), 1) != typeof (string))
649                         return 1;
650                 if (PartialShared1 (new List<Tests> (), 1) != typeof (Tests))
651                         return 2;
652                 if (PartialShared2 (new List<string> (), 1) != typeof (int))
653                         return 3;
654                 if (PartialShared2 (new List<Tests> (), 1) != typeof (int))
655                         return 4;
656                 return 0;
657         }
658
659         public static int test_6_partial_sharing_linq () {
660                 var messages = new List<Message> ();
661
662                 messages.Add (new Message () { MessageID = 5 });
663                 messages.Add (new Message () { MessageID = 6 });
664
665                 return messages.Max(i => i.MessageID);
666         }
667
668         public static int test_0_partial_shared_method_in_nonshared_class () {
669                 var c = new Class1<double> ();
670                 return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
671         }
672
673         class Message {
674                 public int MessageID {
675                         get; set;
676                 }
677         }
678
679         public static Type PartialShared1<T, K> (List<T> list, K k) {
680                 return typeof (T);
681         }
682
683         public static Type PartialShared2<T, K> (List<T> list, K k) {
684                 return typeof (K);
685         }
686
687     public class Class1<T> {
688                 public virtual void Do (Class2<T> t) {
689                         t.Foo ();
690                 }
691
692                 public virtual object Foo<U> (T t) {
693                         return new Class1<U> ();
694                 }
695         }
696
697         public interface IFace1<T> {
698                 void Foo ();
699         }
700
701         public class Class2<T> : MarshalByRefObject, IFace1<T> {
702                 public void Foo () {
703                 }
704         }
705
706
707
708         public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
709                 f.Bar <T> ();
710         }
711
712         public static Type the_type;
713
714         public void ldvirtftn<T> () {
715                 Foo <T> binding = new Foo <T> (default (T));
716
717                 binding.GenericEvent += event_handler;
718                 binding.fire ();
719         }
720
721         public virtual void event_handler<T> (Foo<T> sender) {
722                 the_type = typeof (T);
723         }
724
725         public interface IFoo {
726                 void NonGeneric ();
727                 object Bar<T>();
728         }
729
730         public class Foo<T1> : IFoo
731         {
732                 public Foo(T1 t1)
733                 {
734                         m_t1 = t1;
735                 }
736                 
737                 public override string ToString()
738                 {
739                         return Bar(m_t1 == null ? "null" : "null");
740                 }
741
742                 public String Bar (String s) {
743                         return s;
744                 }
745
746                 public int this [T1 key] {
747                         set {
748                                 if (key == null)
749                                         throw new ArgumentNullException ("key");
750                         }
751                 }
752
753                 public void throw_dead_this () {
754                         try {
755                                 new SomeClass().ThrowAnException();
756                         }
757                         catch {
758                         }
759                 }
760
761                 public T1 get_default () {
762                         return default (T1);
763                 }
764                 
765                 readonly T1 m_t1;
766
767                 public delegate void GenericEventHandler (Foo<T1> sender);
768
769                 public event GenericEventHandler GenericEvent;
770
771                 public void fire () {
772                         GenericEvent (this);
773                 }
774
775                 public static int count1, count2, count3;
776
777                 public void NonGeneric () {
778                         count3 ++;
779                 }
780
781                 public object Bar <T> () {
782                         if (typeof (T) == typeof (int))
783                                 count1 ++;
784                         else if (typeof (T) == typeof (string))
785                                 count2 ++;
786                         return null;
787                 }
788         }
789
790         public class SomeClass {
791                 public void ThrowAnException() {
792                         throw new Exception ("Something went wrong");
793                 }
794         }               
795
796         struct Handler : IFoo {
797                 object o;
798
799                 public Handler(object o) {
800                         this.o = o;
801                 }
802
803                 public void NonGeneric () {
804                 }
805
806                 public object Bar<T>() {
807                         return o;
808                 }
809         }
810
811         static bool IsNull<T> (T t)
812         {
813                 if (t == null)
814                         return true;
815                 else
816                         return false;
817         }
818
819         static object Box<T> (T t)
820         {
821                 return t;
822         }
823         
824         static T Unbox <T> (object o) {
825                 return (T) o;
826         }
827 }