different check
[mono.git] / mcs / tests / dtest-007.cs
index 740cabdf84bd6a08fb915ca1a5d5ab1d70a21ab7..65c7a1c7edbfbf05b00a2387a3350e49c5c2560f 100644 (file)
@@ -6,14 +6,8 @@ using Microsoft.CSharp.RuntimeBinder;
 
 // Dynamic member lookup tests
 
-
-enum MyEnum : byte
-{
-       Value_1 = 1,
-       Value_2 = 2
-}
-
 delegate void D ();
+delegate void D2 (decimal d);
 
 class Class
 {
@@ -21,13 +15,70 @@ class Class
        internal string StringStatic = "hi";
 
        public const decimal Decimal = -0.3m;
+
+       public Class ()
+       {
+       }
+
+       public Class (sbyte extra)
+       {
+               IntValue = extra;
+       }
+
+       uint s = 77;
+       protected internal uint this[byte i] {
+               get {
+                       return s * i;
+               }
+               set {
+                       s = value;
+               }
+       }
+
+       byte b = 180;
+       internal byte Prop {
+               get {
+                       return b;
+               }
+               set {
+                       b = value;
+               }
+       }
+
+       public int FixedValue
+       {
+               set { }
+               get { return 823; }
+       }
+
+       internal string Method (string value)
+       {
+               return value;
+       }
+       
+       public int Method (int a, byte b)
+       {
+               return a * b;
+       }
+
+       public void MethodInOut (ref int refValue, out string outValue)
+       {
+               refValue = 3;
+               outValue = "4";
+       }
+
+       public static void GenericVoid<T> (T i)
+       {
+       }
+
+       public static int StaticMethod (params int[] i)
+       {
+               return i [0] * i.Length;
+       }
 }
 
 class Tester
 {
-       delegate void EmptyDelegate ();
-       delegate int IntDelegate ();
-
        static void Assert<T> (T expected, T value, string name)
        {
                if (!EqualityComparer<T>.Default.Equals (expected, value)) {
@@ -48,14 +99,92 @@ class Tester
 
 #pragma warning disable 169
 
+       void GetIndexTest ()
+       {
+               dynamic d = new[] { 5, 8, 2 };
+               Assert (8, d[1], "#1");
+
+               d = new int[,] { { 1, 2 }, { 3, 4 } };
+               Assert (3, d[1, 0], "#2");
+
+               dynamic d2 = new Class ();
+               Assert<uint> (154, d2[2], "#3");
+               Assert<uint> (154, d2[i:2], "#3a");
+       }
+
+       void GetIndexError_Null ()
+       {
+               dynamic d = null;
+               AssertError (() => { var v = d[1]; }, "#1");
+       }
+
+       void InvokeTest ()
+       {
+               Func<string, string> f = new Class().Method;
+               dynamic d = f;
+               Assert ("bar", d ("bar"), "#1");
+
+               Action<bool> f2 = Class.GenericVoid;
+               d = f2;
+               d (true);
+               
+               Func<string, int> f3 = (s) => 3;
+               d = f3;
+               d ("go");
+       }
+
+       void InvokeMember ()
+       {
+               dynamic d = new Class ();
+               Assert ("vv", d.Method ("vv"), "#1");
+               
+               byte b = 2;
+               Assert (6, d.Method (b: b++, a: b), "#1a");
+
+               var arg1 = 1;
+               var arg2 = "a";
+               d.MethodInOut (ref arg1, out arg2);
+
+               d = 2;
+               Assert (2, Class.StaticMethod (d), "#2");
+               Class.StaticMethod (d); 
+       }
+
+       void InvokeConstructor ()
+       {
+               dynamic d = (sbyte) 8;
+               var r = new Class (d);
+               Assert (8, r.IntValue, "#1");
+
+               D2 method = (decimal e) => { };
+               d = method;
+               var r2 = new D2 (d);
+       }
+
+       event Func<int> e;
+       int field;
+       void IsEvent ()
+       {
+               dynamic d = this;
+               d.e += new Func<int> (() => 3);
+               
+               // FIXME:
+               //Assert (3, d.e (), "#1");
+               
+               d.field += 5;
+               Assert (5, d.field, "#2");
+       }
+
        void MemberGetTest ()
        {
                dynamic d = new Class ();
                Assert (5, d.IntValue, "#1");
+               Assert (180, d.Prop, "#1a");
 
                Assert ("hi", d.StringStatic, "#2");
 
-               // d.Event += delegate () { }; CS0019
+               d = new int[4];
+               Assert (4, d.Length, "#3");
        }
 
        void MemberGetError_Null ()
@@ -67,11 +196,26 @@ class Tester
        void MemberSetTest ()
        {
                dynamic d = new Class ();
+
                d.IntValue = 22;
                Assert (22, d.IntValue, "#1");
+               d.Prop = 19;
+               Assert (19, d.Prop, "#1a");
+
+               d.Prop = byte.MaxValue;
+               Assert (byte.MaxValue, d.Prop++, "#1b");
+               Assert (1, ++d.Prop, "#1c");
+               d.Prop++;
+               Assert (2, d.Prop, "#1d");
+               
+               d.Prop += 5;
+               Assert (7, d.Prop, "#1e");
 
                d.StringStatic = "no";
                Assert ("no", d.StringStatic, "#2");
+
+               var r = d.FixedValue = 44;
+               Assert (44, r, "#3");
        }
 
        void MemberSetError_Null ()
@@ -80,6 +224,39 @@ class Tester
                AssertError (() => { d.Fo1 = 1; }, "#1");
        }
 
+       void SetIndexTest ()
+       {
+               dynamic d = new[] { "b", "v" };
+               d[1] = "c";
+               Assert ("c", d[1], "#1");
+
+               d = new int[,] { { 1, 2 }, { 3, 4 } };
+               d[1, 0] = 100;
+               Assert (100, d[1, 0], "#2");
+               d[1, 0]++;
+               Assert (101, d[1, 0], "#2a");
+
+               d [0, 0] = d [1, 0] = 55;
+               Assert (55, d [0, 0], "#2a");
+
+               dynamic d2 = new Class ();
+               d2[2] = 500;
+               Assert<uint> (1000, d2[2], "#3");
+               d2[2]++;
+               Assert<uint> (2002, d2[2], "#3a");
+               d2[i:1] = 3;
+               Assert<uint> (3, d2[1], "#3b");
+               
+               uint r = d2 [1] = 200;
+               Assert<uint> (200, r, "#4");
+       }
+
+       void SetIndexError_Null ()
+       {
+               dynamic d = null;
+               AssertError (() => { d [1] = 0; }, "#1");
+       }
+
 #pragma warning restore 169
 
        static bool RunTest (MethodInfo test)
@@ -92,7 +269,7 @@ class Tester
                } catch (Exception e) {
                        Console.WriteLine ("FAILED");
                        Console.WriteLine (e.ToString ());
-//                     Console.WriteLine (e.InnerException.Message);
+                       //                      Console.WriteLine (e.InnerException.Message);
                        return false;
                }
        }