Pinvoke symbols according to the new moonlight naming convention.
[mono-project.git] / mono / mini / generics.cs
blobef5f3ac69d0bf8a6102581cb8de0223039b0809b
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Runtime.CompilerServices;
6 class Tests {
8 struct TestStruct {
9 public int i;
10 public int j;
12 public TestStruct (int i, int j) {
13 this.i = i;
14 this.j = j;
18 class Enumerator <T> : MyIEnumerator <T> {
19 T MyIEnumerator<T>.Current {
20 get {
21 return default(T);
25 bool MyIEnumerator<T>.MoveNext () {
26 return true;
30 class Comparer <T> : IComparer <T> {
31 bool IComparer<T>.Compare (T x, T y) {
32 return true;
36 static int Main (string[] args)
38 return TestDriver.RunTests (typeof (Tests), args);
41 public static int test_1_nullable_unbox ()
43 return Unbox<int?> (1).Value;
46 public static int test_1_nullable_unbox_null ()
48 return Unbox<int?> (null).HasValue ? 0 : 1;
51 public static int test_1_nullable_box ()
53 return (int) Box<int?> (1);
56 public static int test_1_nullable_box_null ()
58 return Box<int?> (null) == null ? 1 : 0;
61 public static int test_1_isinst_nullable ()
63 object o = 1;
64 return (o is int?) ? 1 : 0;
67 public static int test_1_nullable_unbox_vtype ()
69 return Unbox<TestStruct?> (new TestStruct (1, 2)).Value.i;
72 public static int test_1_nullable_unbox_null_vtype ()
74 return Unbox<TestStruct?> (null).HasValue ? 0 : 1;
77 public static int test_1_nullable_box_vtype ()
79 return ((TestStruct)(Box<TestStruct?> (new TestStruct (1, 2)))).i;
82 public static int test_1_nullable_box_null_vtype ()
84 return Box<TestStruct?> (null) == null ? 1 : 0;
87 public static int test_1_isinst_nullable_vtype ()
89 object o = new TestStruct (1, 2);
90 return (o is TestStruct?) ? 1 : 0;
93 public static int test_0_nullable_normal_unbox ()
95 int? i = 5;
97 object o = i;
98 // This uses unbox instead of unbox_any
99 int? j = (int?)o;
101 if (j != 5)
102 return 1;
104 return 0;
107 public static void stelem_any<T> (T[] arr, T elem) {
108 arr [0] = elem;
111 public static T ldelem_any<T> (T[] arr) {
112 return arr [0];
115 public static int test_1_ldelem_stelem_any_int () {
116 int[] arr = new int [3];
117 stelem_any (arr, 1);
119 return ldelem_any (arr);
122 public static T return_ref<T> (ref T t) {
123 return t;
126 public static T ldelema_any<T> (T[] arr) {
127 return return_ref<T> (ref arr [0]);
130 public static int test_0_ldelema () {
131 string[] arr = new string [1];
133 arr [0] = "Hello";
135 if (ldelema_any <string> (arr) == "Hello")
136 return 0;
137 else
138 return 1;
141 public static T[,] newarr_multi<T> () {
142 return new T [1, 1];
145 public static int test_0_newarr_multi_dim () {
146 return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
149 interface ITest
151 void Foo<T> ();
154 public static int test_0_iface_call_null_bug_77442 () {
155 ITest test = null;
157 try {
158 test.Foo<int> ();
160 catch (NullReferenceException) {
161 return 0;
164 return 1;
167 public static int test_18_ldobj_stobj_generics () {
168 GenericClass<int> t = new GenericClass <int> ();
169 int i = 5;
170 int j = 6;
171 return t.ldobj_stobj (ref i, ref j) + i + j;
174 public static int test_5_ldelem_stelem_generics () {
175 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
177 TestStruct s = new TestStruct (5, 5);
178 return t.ldelem_stelem (s).i;
181 public static int test_0_constrained_vtype_box () {
182 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
184 return t.toString (new TestStruct ()) == "Tests+TestStruct" ? 0 : 1;
187 public static int test_0_constrained_vtype () {
188 GenericClass<int> t = new GenericClass<int> ();
190 return t.toString (1234) == "1234" ? 0 : 1;
193 public static int test_0_constrained_reftype () {
194 GenericClass<String> t = new GenericClass<String> ();
196 return t.toString ("1234") == "1234" ? 0 : 1;
199 public static int test_0_box_brtrue_optimizations () {
200 if (IsNull<int>(5))
201 return 1;
203 if (!IsNull<object>(null))
204 return 1;
206 return 0;
209 [Category ("!FULLAOT")]
210 public static int test_0_generic_get_value_optimization_int () {
211 int[] x = new int[] {100, 200};
213 if (GenericClass<int>.Z (x, 0) != 100)
214 return 2;
216 if (GenericClass<int>.Z (x, 1) != 200)
217 return 3;
219 return 0;
222 public static int test_0_generic_get_value_optimization_vtype () {
223 TestStruct[] arr = new TestStruct[] { new TestStruct (100, 200), new TestStruct (300, 400) };
224 IEnumerator<TestStruct> enumerator = GenericClass<TestStruct>.Y (arr);
225 TestStruct s;
226 int sum = 0;
227 while (enumerator.MoveNext ()) {
228 s = enumerator.Current;
229 sum += s.i + s.j;
232 if (sum != 1000)
233 return 1;
235 s = GenericClass<TestStruct>.Z (arr, 0);
236 if (s.i != 100 || s.j != 200)
237 return 2;
239 s = GenericClass<TestStruct>.Z (arr, 1);
240 if (s.i != 300 || s.j != 400)
241 return 3;
243 return 0;
246 public static int test_0_nullable_ldflda () {
247 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
250 public struct GenericStruct<T> {
251 public T t;
253 public GenericStruct (T t) {
254 this.t = t;
258 public class GenericClass<T> {
259 public T t;
261 public GenericClass (T t) {
262 this.t = t;
265 public GenericClass () {
268 public T ldobj_stobj (ref T t1, ref T t2) {
269 t1 = t2;
270 T t = t1;
272 return t;
275 public T ldelem_stelem (T t) {
276 T[] arr = new T [10];
277 arr [0] = t;
279 return arr [0];
282 public String toString (T t) {
283 return t.ToString ();
286 public static IEnumerator<T> Y (IEnumerable <T> x)
288 return x.GetEnumerator ();
291 public static T Z (IList<T> x, int index)
293 return x [index];
296 protected static T NullB = default(T);
297 private static Nullable<bool> _BIsA = null;
298 public static bool BIsAClazz {
299 get {
300 _BIsA = false;
301 return _BIsA.Value;
306 public class MRO : MarshalByRefObject {
307 public GenericStruct<int> struct_field;
308 public GenericClass<int> class_field;
311 public static int test_0_ldfld_stfld_mro () {
312 MRO m = new MRO ();
313 GenericStruct<int> s = new GenericStruct<int> (5);
314 // This generates stfld
315 m.struct_field = s;
317 // This generates ldflda
318 if (m.struct_field.t != 5)
319 return 1;
321 // This generates ldfld
322 GenericStruct<int> s2 = m.struct_field;
323 if (s2.t != 5)
324 return 2;
326 if (m.struct_field.t != 5)
327 return 3;
329 m.class_field = new GenericClass<int> (5);
330 if (m.class_field.t != 5)
331 return 4;
333 return 0;
336 // FIXME:
337 [Category ("!FULLAOT")]
338 public static int test_0_generic_virtual_call_on_vtype_unbox () {
339 object o = new Object ();
340 IFoo h = new Handler(o);
342 if (h.Bar<object> () != o)
343 return 1;
344 else
345 return 0;
348 public static int test_0_box_brtrue_opt () {
349 Foo<int> f = new Foo<int> (5);
351 f [123] = 5;
353 return 0;
356 public static int test_0_box_brtrue_opt_regress_81102 () {
357 if (new Foo<int>(5).ToString () == "null")
358 return 0;
359 else
360 return 1;
363 struct S {
364 public int i;
367 public static int test_0_ldloca_initobj_opt () {
368 if (new Foo<S> (new S ()).get_default ().i != 0)
369 return 1;
370 if (new Foo<object> (null).get_default () != null)
371 return 2;
372 return 0;
375 public static int test_0_variance_reflection () {
376 // covariance on IEnumerator
377 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
378 return 1;
379 // covariance on IEnumerator and covariance on arrays
380 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
381 return 2;
382 // covariance and implemented interfaces
383 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
384 return 3;
386 // contravariance on IComparer
387 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
388 return 4;
389 // contravariance on IComparer, contravariance on arrays
390 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
391 return 5;
392 // contravariance and interface inheritance
393 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
394 return 6;
395 return 0;
398 public static int test_0_ldvirtftn_generic_method () {
399 new Tests ().ldvirtftn<string> ();
401 return the_type == typeof (string) ? 0 : 1;
404 public static int test_0_throw_dead_this () {
405 new Foo<string> ("").throw_dead_this ();
406 return 0;
409 struct S<T> {}
411 public static int test_0_inline_infinite_polymorphic_recursion () {
412 f<int>(0);
414 return 0;
417 private static void f<T>(int i) {
418 if(i==42) f<S<T>>(i);
421 // This cannot be made to work with full-aot, since there it is impossible to
422 // statically determine that Foo<string>.Bar <int> is needed, the code only
423 // references IFoo.Bar<int>
424 [Category ("!FULLAOT")]
425 public static int test_0_generic_virtual_on_interfaces () {
426 Foo<string>.count1 = 0;
427 Foo<string>.count2 = 0;
428 Foo<string>.count3 = 0;
430 IFoo f = new Foo<string> ("");
431 for (int i = 0; i < 1000; ++i) {
432 f.Bar <int> ();
433 f.Bar <string> ();
434 f.NonGeneric ();
437 if (Foo<string>.count1 != 1000)
438 return 1;
439 if (Foo<string>.count2 != 1000)
440 return 2;
441 if (Foo<string>.count3 != 1000)
442 return 3;
444 VirtualInterfaceCallFromGenericMethod<long> (f);
446 return 0;
449 //repro for #505375
450 [Category ("!FULLAOT")]
451 public static int test_2_cprop_bug () {
452 int idx = 0;
453 int a = 1;
454 var cmp = System.Collections.Generic.Comparer<int>.Default ;
455 if (cmp.Compare (a, 0) > 0)
456 a = 0;
457 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
458 return idx;
461 enum MyEnumUlong : ulong {
462 Value_2 = 2
465 public static int test_0_regress_550964_constrained_enum_long () {
466 MyEnumUlong a = MyEnumUlong.Value_2;
467 MyEnumUlong b = MyEnumUlong.Value_2;
469 return Pan (a, b) ? 0 : 1;
472 static bool Pan<T> (T a, T b)
474 return a.Equals (b);
477 public class XElement {
478 public string Value {
479 get; set;
483 public static int test_0_fullaot_linq () {
484 var allWords = new XElement [] { new XElement { Value = "one" } };
485 var filteredWords = allWords.Where(kw => kw.Value.StartsWith("T"));
486 return filteredWords.Count ();
489 public static int test_0_fullaot_comparer_t () {
490 var l = new SortedList <TimeSpan, int> ();
491 return l.Count;
494 public static int test_0_fullaot_comparer_t_2 () {
495 var l = new Dictionary <TimeSpan, int> ();
496 return l.Count;
499 static void enumerate<T> (IEnumerable<T> arr) {
500 foreach (var o in arr)
502 int c = ((ICollection<T>)arr).Count;
505 /* Test that treating arrays as generic collections works with full-aot */
506 public static int test_0_fullaot_array_wrappers () {
507 Tests[] arr = new Tests [10];
508 enumerate<Tests> (arr);
509 return 0;
512 static int cctor_count = 0;
514 public abstract class Beta<TChanged>
516 static Beta()
518 cctor_count ++;
522 public class Gamma<T> : Beta<T>
524 static Gamma()
529 // #519336
530 public static int test_2_generic_class_init_gshared_ctor () {
531 new Gamma<object>();
532 new Gamma<string>();
534 return cctor_count;
537 static int cctor_count2 = 0;
539 class ServiceController<T> {
540 static ServiceController () {
541 cctor_count2 ++;
544 public ServiceController () {
548 static ServiceController<T> Create<T>() {
549 return new ServiceController<T>();
552 // #631409
553 public static int test_2_generic_class_init_gshared_ctor_from_gshared () {
554 Create<object> ();
555 Create<string> ();
557 return cctor_count2;
560 public static Type get_type<T> () {
561 return typeof (T);
564 public static int test_0_gshared_delegate_rgctx () {
565 Func<Type> t = new Func<Type> (get_type<string>);
567 if (t () == typeof (string))
568 return 0;
569 else
570 return 1;
573 // Creating a delegate from a generic method from gshared code
574 public static int test_0_gshared_delegate_from_gshared () {
575 if (gshared_delegate_from_gshared <object> () != 0)
576 return 1;
577 if (gshared_delegate_from_gshared <string> () != 0)
578 return 2;
579 return 0;
582 public static int gshared_delegate_from_gshared <T> () {
583 Func<Type> t = new Func<Type> (get_type<T>);
585 return t () == typeof (T) ? 0 : 1;
588 public static int test_0_marshalbyref_call_from_gshared_virt_elim () {
589 /* Calling a virtual method from gshared code which is changed to a nonvirt call */
590 Class1<object> o = new Class1<object> ();
591 o.Do (new Class2<object> ());
592 return 0;
595 class Pair<TKey, TValue> {
596 public static KeyValuePair<TKey, TValue> make_pair (TKey key, TValue value)
598 return new KeyValuePair<TKey, TValue> (key, value);
601 public delegate TRet Transform<TRet> (TKey key, TValue value);
604 public static int test_0_bug_620864 () {
605 var d = new Pair<string, Type>.Transform<KeyValuePair<string, Type>> (Pair<string, Type>.make_pair);
607 var p = d ("FOO", typeof (int));
608 if (p.Key != "FOO" || p.Value != typeof (int))
609 return 1;
611 return 0;
615 struct RecStruct<T> {
616 public void foo (RecStruct<RecStruct<T>> baz) {
620 public static int test_0_infinite_generic_recursion () {
621 // Check that the AOT compile can deal with infinite generic recursion through
622 // parameter types
623 RecStruct<int> bla;
625 return 0;
628 struct FooStruct {
631 bool IsNull2 <T> (object value) where T : struct {
632 T? item = (T?) value;
634 if (item.HasValue)
635 return false;
637 return true;
640 public static int test_0_full_aot_nullable_unbox_from_gshared_code () {
641 if (!new Tests ().IsNull2<FooStruct> (null))
642 return 1;
643 if (new Tests ().IsNull2<FooStruct> (new FooStruct ()))
644 return 2;
645 return 0;
648 public static int test_0_partial_sharing () {
649 if (PartialShared1 (new List<string> (), 1) != typeof (string))
650 return 1;
651 if (PartialShared1 (new List<Tests> (), 1) != typeof (Tests))
652 return 2;
653 if (PartialShared2 (new List<string> (), 1) != typeof (int))
654 return 3;
655 if (PartialShared2 (new List<Tests> (), 1) != typeof (int))
656 return 4;
657 return 0;
660 public static int test_6_partial_sharing_linq () {
661 var messages = new List<Message> ();
663 messages.Add (new Message () { MessageID = 5 });
664 messages.Add (new Message () { MessageID = 6 });
666 return messages.Max(i => i.MessageID);
669 public static int test_0_partial_shared_method_in_nonshared_class () {
670 var c = new Class1<double> ();
671 return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
674 class Message {
675 public int MessageID {
676 get; set;
680 public static Type PartialShared1<T, K> (List<T> list, K k) {
681 return typeof (T);
684 public static Type PartialShared2<T, K> (List<T> list, K k) {
685 return typeof (K);
688 public class Class1<T> {
689 public virtual void Do (Class2<T> t) {
690 t.Foo ();
693 public virtual object Foo<U> (T t) {
694 return new Class1<U> ();
698 public interface IFace1<T> {
699 void Foo ();
702 public class Class2<T> : MarshalByRefObject, IFace1<T> {
703 public void Foo () {
709 public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
710 f.Bar <T> ();
713 public static Type the_type;
715 public void ldvirtftn<T> () {
716 Foo <T> binding = new Foo <T> (default (T));
718 binding.GenericEvent += event_handler;
719 binding.fire ();
722 public virtual void event_handler<T> (Foo<T> sender) {
723 the_type = typeof (T);
726 public interface IFoo {
727 void NonGeneric ();
728 object Bar<T>();
731 public class Foo<T1> : IFoo
733 public Foo(T1 t1)
735 m_t1 = t1;
738 public override string ToString()
740 return Bar(m_t1 == null ? "null" : "null");
743 public String Bar (String s) {
744 return s;
747 public int this [T1 key] {
748 set {
749 if (key == null)
750 throw new ArgumentNullException ("key");
754 public void throw_dead_this () {
755 try {
756 new SomeClass().ThrowAnException();
758 catch {
762 public T1 get_default () {
763 return default (T1);
766 readonly T1 m_t1;
768 public delegate void GenericEventHandler (Foo<T1> sender);
770 public event GenericEventHandler GenericEvent;
772 public void fire () {
773 GenericEvent (this);
776 public static int count1, count2, count3;
778 public void NonGeneric () {
779 count3 ++;
782 public object Bar <T> () {
783 if (typeof (T) == typeof (int))
784 count1 ++;
785 else if (typeof (T) == typeof (string))
786 count2 ++;
787 return null;
791 public class SomeClass {
792 public void ThrowAnException() {
793 throw new Exception ("Something went wrong");
797 struct Handler : IFoo {
798 object o;
800 public Handler(object o) {
801 this.o = o;
804 public void NonGeneric () {
807 public object Bar<T>() {
808 return o;
812 static bool IsNull<T> (T t)
814 if (t == null)
815 return true;
816 else
817 return false;
820 static object Box<T> (T t)
822 return t;
825 static T Unbox <T> (object o) {
826 return (T) o;
829 interface IDefaultRetriever
831 T GetDefault<T>();
834 class DefaultRetriever : IDefaultRetriever
836 [MethodImpl(MethodImplOptions.Synchronized)]
837 public T GetDefault<T>()
839 return default(T);
843 [Category ("!FULLAOT")]
844 public static int test_0_regress_668095_synchronized_gshared () {
845 return DoSomething (new DefaultRetriever ());
848 static int DoSomething(IDefaultRetriever foo) {
849 int result = foo.GetDefault<int>();
850 return result;