New tests.
[mono.git] / mono / mini / generics.cs
index 372f08b45887b89c42be17e42ea6f1dc1fc087b6..a75e3ad59a8f5c5a21a2c5a0b29374f3787d9914 100644 (file)
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Linq;
 
 class Tests {
 
@@ -31,9 +32,9 @@ class Tests {
                }
        }
 
-       static int Main ()
+       static int Main (string[] args)
        {
-               return TestDriver.RunTests (typeof (Tests));
+               return TestDriver.RunTests (typeof (Tests), args);
        }
 
        public static int test_1_nullable_unbox ()
@@ -117,6 +118,33 @@ class Tests {
                return ldelem_any (arr);
        }
 
+       public static T return_ref<T> (ref T t) {
+               return t;
+       }
+
+       public static T ldelema_any<T> (T[] arr) {
+               return return_ref<T> (ref arr [0]);
+       }
+
+       public static int test_0_ldelema () {
+               string[] arr = new string [1];
+
+               arr [0] = "Hello";
+
+               if (ldelema_any <string> (arr) == "Hello")
+                       return 0;
+               else
+                       return 1;
+       }
+
+       public static T[,] newarr_multi<T> () {
+               return new T [1, 1];
+       }
+
+       public static int test_0_newarr_multi_dim () {
+               return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
+       }
+
        interface ITest
        {
                void Foo<T> ();
@@ -177,6 +205,7 @@ class Tests {
                return 0;
        }
 
+       [Category ("!FULLAOT")]
        public static int test_0_generic_get_value_optimization_int () {
                int[] x = new int[] {100, 200};
 
@@ -213,6 +242,10 @@ class Tests {
                return 0;
        }
 
+       public static int test_0_nullable_ldflda () {
+               return GenericClass<string>.BIsAClazz == false ? 0 : 1;
+       }
+
        public struct GenericStruct<T> {
                public T t;
 
@@ -258,6 +291,15 @@ class Tests {
                {
                        return x [index];
                }
+
+        protected static T NullB = default(T);       
+        private static Nullable<bool>  _BIsA = null;
+        public static bool BIsAClazz {
+            get {
+                _BIsA = false;
+                return _BIsA.Value;
+            }
+        }
        }
 
        public class MRO : MarshalByRefObject {
@@ -290,9 +332,11 @@ class Tests {
                return 0;
        }
 
+       // FIXME:
+       [Category ("!FULLAOT")]
     public static int test_0_generic_virtual_call_on_vtype_unbox () {
                object o = new Object ();
-        IMyHandler h = new Handler(o);
+        IFoo h = new Handler(o);
 
         if (h.Bar<object> () != o)
                        return 1;
@@ -350,7 +394,259 @@ class Tests {
                return 0;
        }
 
-       public class Foo<T1>
+       public static int test_0_ldvirtftn_generic_method () {
+               new Tests ().ldvirtftn<string> ();              
+
+               return the_type == typeof (string) ? 0 : 1;
+       }
+
+       public static int test_0_throw_dead_this () {
+        new Foo<string> ("").throw_dead_this ();
+               return 0;
+       }
+
+       struct S<T> {}
+
+       public static int test_0_inline_infinite_polymorphic_recursion () {
+           f<int>(0);
+
+                  return 0;
+       }
+
+       private static void f<T>(int i) {
+               if(i==42) f<S<T>>(i);
+       }
+
+       // This cannot be made to work with full-aot, since there it is impossible to
+       // statically determine that Foo<string>.Bar <int> is needed, the code only
+       // references IFoo.Bar<int>
+       [Category ("!FULLAOT")]
+       public static int test_0_generic_virtual_on_interfaces () {
+               Foo<string>.count1 = 0;
+               Foo<string>.count2 = 0;
+               Foo<string>.count3 = 0;
+
+               IFoo f = new Foo<string> ("");
+               for (int i = 0; i < 1000; ++i) {
+                       f.Bar <int> ();
+                       f.Bar <string> ();
+                       f.NonGeneric ();
+               }
+
+               if (Foo<string>.count1 != 1000)
+                       return 1;
+               if (Foo<string>.count2 != 1000)
+                       return 2;
+               if (Foo<string>.count3 != 1000)
+                       return 3;
+
+               VirtualInterfaceCallFromGenericMethod<long> (f);
+
+               return 0;
+       }
+
+       //repro for #505375
+       [Category ("!FULLAOT")]
+       public static int test_2_cprop_bug () {
+               int idx = 0;
+               int a = 1;
+               var cmp = System.Collections.Generic.Comparer<int>.Default ;
+               if (cmp.Compare (a, 0) > 0)
+                       a = 0;
+               do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
+               return idx;
+       }
+
+       enum MyEnumUlong : ulong {
+               Value_2 = 2
+       }
+
+       public static int test_0_regress_550964_constrained_enum_long () {
+        MyEnumUlong a = MyEnumUlong.Value_2;
+        MyEnumUlong b = MyEnumUlong.Value_2;
+
+        return Pan (a, b) ? 0 : 1;
+       }
+
+    static bool Pan<T> (T a, T b)
+    {
+        return a.Equals (b);
+    }
+
+       public class XElement {
+               public string Value {
+                       get; set;
+               }
+       }
+
+       public static int test_0_fullaot_linq () {
+               var allWords = new XElement [] { new XElement { Value = "one" } };
+               var filteredWords = allWords.Where(kw => kw.Value.StartsWith("T"));
+               return filteredWords.Count ();
+       }
+
+       public static int test_0_fullaot_comparer_t () {
+               var l = new SortedList <TimeSpan, int> ();
+               return l.Count;
+       }
+
+       static void enumerate<T> (IEnumerable<T> arr) {
+               foreach (var o in arr)
+                       ;
+               int c = ((ICollection<T>)arr).Count;
+       }
+
+       /* Test that treating arrays as generic collections works with full-aot */
+       public static int test_0_fullaot_array_wrappers () {
+               Tests[] arr = new Tests [10];
+               enumerate<Tests> (arr);
+               return 0;
+       }
+
+       static int cctor_count = 0;
+
+    public abstract class Beta<TChanged> 
+    {          
+        static Beta()
+        {
+                       cctor_count ++;
+        }
+    }   
+    
+    public class Gamma<T> : Beta<T> 
+    {   
+        static Gamma()
+        {
+        }
+    }
+
+       // #519336    
+       public static int test_2_generic_class_init_gshared_ctor () {
+               new Gamma<object>();
+               new Gamma<string>();
+
+               return cctor_count;
+       }
+
+       public static Type get_type<T> () {
+               return typeof (T);
+       }
+
+       public static int test_0_gshared_delegate_rgctx () {
+               Func<Type> t = new Func<Type> (get_type<string>);
+
+               if (t () == typeof (string))
+                       return 0;
+               else
+                       return 1;
+       }
+
+       // Creating a delegate from a generic method from gshared code
+       public static int test_0_gshared_delegate_from_gshared () {
+               if (gshared_delegate_from_gshared <object> () != 0)
+                       return 1;
+               if (gshared_delegate_from_gshared <string> () != 0)
+                       return 2;
+               return 0;
+       }
+
+       public static int gshared_delegate_from_gshared <T> () {
+               Func<Type> t = new Func<Type> (get_type<T>);
+
+               return t () == typeof (T) ? 0 : 1;
+       }
+
+       public static int test_0_marshalbyref_call_from_gshared_virt_elim () {
+               /* Calling a virtual method from gshared code which is changed to a nonvirt call */
+               Class1<object> o = new Class1<object> ();
+               o.Do (new Class2<object> ());
+               return 0;
+       }
+
+       public static int test_0_partial_sharing () {
+               if (PartialShared1 (new List<string> (), 1) != typeof (string))
+                       return 1;
+               if (PartialShared1 (new List<Tests> (), 1) != typeof (Tests))
+                       return 2;
+               if (PartialShared2 (new List<string> (), 1) != typeof (int))
+                       return 3;
+               if (PartialShared2 (new List<Tests> (), 1) != typeof (int))
+                       return 4;
+               return 0;
+       }
+
+       public static int test_6_partial_sharing_linq () {
+               var messages = new List<Message> ();
+
+               messages.Add (new Message () { MessageID = 5 });
+               messages.Add (new Message () { MessageID = 6 });
+
+               return messages.Max(i => i.MessageID);
+       }
+
+       public static int test_0_partial_shared_method_in_nonshared_class () {
+               var c = new Class1<double> ();
+               return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
+       }
+
+       class Message {
+               public int MessageID {
+                       get; set;
+               }
+       }
+
+       public static Type PartialShared1<T, K> (List<T> list, K k) {
+               return typeof (T);
+       }
+
+       public static Type PartialShared2<T, K> (List<T> list, K k) {
+               return typeof (K);
+       }
+
+    public class Class1<T> {
+               public virtual void Do (Class2<T> t) {
+                       t.Foo ();
+               }
+
+               public virtual object Foo<U> (T t) {
+                       return new Class1<U> ();
+               }
+       }
+
+       public interface IFace1<T> {
+               void Foo ();
+       }
+
+       public class Class2<T> : MarshalByRefObject, IFace1<T> {
+               public void Foo () {
+               }
+       }
+
+
+
+       public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
+               f.Bar <T> ();
+       }
+
+       public static Type the_type;
+
+       public void ldvirtftn<T> () {
+               Foo <T> binding = new Foo <T> (default (T));
+
+               binding.GenericEvent += event_handler;
+               binding.fire ();
+       }
+
+       public virtual void event_handler<T> (Foo<T> sender) {
+               the_type = typeof (T);
+       }
+
+       public interface IFoo {
+               void NonGeneric ();
+               object Bar<T>();
+       }
+
+       public class Foo<T1> : IFoo
        {
                public Foo(T1 t1)
                {
@@ -373,24 +669,59 @@ class Tests {
                        }
                }
 
+               public void throw_dead_this () {
+                       try {
+                               new SomeClass().ThrowAnException();
+                       }
+                       catch {
+                       }
+               }
+
                public T1 get_default () {
                        return default (T1);
                }
                
                readonly T1 m_t1;
-       }
 
-       public interface IMyHandler {
-               object Bar<T>();
+               public delegate void GenericEventHandler (Foo<T1> sender);
+
+               public event GenericEventHandler GenericEvent;
+
+               public void fire () {
+                       GenericEvent (this);
+               }
+
+               public static int count1, count2, count3;
+
+               public void NonGeneric () {
+                       count3 ++;
+               }
+
+               public object Bar <T> () {
+                       if (typeof (T) == typeof (int))
+                               count1 ++;
+                       else if (typeof (T) == typeof (string))
+                               count2 ++;
+                       return null;
+               }
        }
 
-       struct Handler : IMyHandler {
+       public class SomeClass {
+               public void ThrowAnException() {
+                       throw new Exception ("Something went wrong");
+               }
+       }               
+
+       struct Handler : IFoo {
                object o;
 
                public Handler(object o) {
                        this.o = o;
                }
 
+               public void NonGeneric () {
+               }
+
                public object Bar<T>() {
                        return o;
                }