Updated.
[mcs.git] / tests / dtest-007.cs
blob8ef69011fbdbc18ea165003ff6a2087d27d0f637
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Reflection;
5 using Microsoft.CSharp.RuntimeBinder;
7 // Dynamic member lookup tests
9 delegate void D ();
11 class Class
13 internal int IntValue = 5;
14 internal string StringStatic = "hi";
16 public const decimal Decimal = -0.3m;
18 uint s = 77;
19 protected internal uint this[byte i] {
20 get {
21 return s * i;
23 set {
24 s = value;
28 byte b = 180;
29 internal byte Prop {
30 get {
31 return b;
33 set {
34 b = value;
38 public int FixedValue
40 set { }
41 get { return 823; }
44 internal string Method (string value)
46 return value;
49 public static void GenericVoid<T> (T i)
53 public static int StaticMethod (params int[] i)
55 return i [0] * i.Length;
59 class Tester
61 delegate void EmptyDelegate ();
62 delegate int IntDelegate ();
64 static void Assert<T> (T expected, T value, string name)
66 if (!EqualityComparer<T>.Default.Equals (expected, value)) {
67 name += ": ";
68 throw new ApplicationException (name + expected + " != " + value);
72 static void AssertError (Action expected, string name)
74 try {
75 expected ();
76 throw new ApplicationException (name + ": RuntimeBinderException expected");
77 } catch (RuntimeBinderException) {
78 // passed
82 #pragma warning disable 169
84 void GetIndexTest ()
86 dynamic d = new[] { 5, 8, 2 };
87 Assert (8, d[1], "#1");
89 d = new int[,] { { 1, 2 }, { 3, 4 } };
90 Assert (3, d[1, 0], "#2");
92 dynamic d2 = new Class ();
93 Assert<uint> (154, d2[2], "#3");
94 Assert<uint> (154, d2[i:2], "#3a");
97 void GetIndexError_Null ()
99 dynamic d = null;
100 AssertError (() => { var v = d[1]; }, "#1");
103 void InvokeTest ()
105 Func<string, string> f = new Class().Method;
106 dynamic d = f;
107 Assert ("bar", d ("bar"), "#1");
109 Action<bool> f2 = Class.GenericVoid;
110 d = f2;
111 Assert<object> (null, d (true), "#2");
114 void InvokeMember ()
116 // dynamic d = new Class ();
117 // Assert ("vv", d.Method ("vv"), "#1");
119 dynamic d = 2;
120 Assert (2, Class.StaticMethod (d), "#2");
123 void MemberGetTest ()
125 dynamic d = new Class ();
126 Assert (5, d.IntValue, "#1");
127 Assert (180, d.Prop, "#1a");
129 Assert ("hi", d.StringStatic, "#2");
131 d = new int[4];
132 Assert (4, d.Length, "#3");
134 // d.Event += delegate () { }; CS0019
137 void MemberGetError_Null ()
139 dynamic d = null;
140 AssertError (() => { var v = d.Foo; }, "#1");
143 void MemberSetTest ()
145 dynamic d = new Class ();
147 d.IntValue = 22;
148 Assert (22, d.IntValue, "#1");
149 d.Prop = 19;
150 Assert (19, d.Prop, "#1a");
152 d.Prop = byte.MaxValue;
153 d.Prop++;
154 Assert (0, d.Prop, "#1b");
156 d.StringStatic = "no";
157 Assert ("no", d.StringStatic, "#2");
159 var r = d.FixedValue = 44;
160 Assert (44, r, "#3");
163 void MemberSetError_Null ()
165 dynamic d = null;
166 AssertError (() => { d.Fo1 = 1; }, "#1");
169 void SetIndexTest ()
171 dynamic d = new[] { "b", "v" };
172 d[1] = "c";
173 Assert ("c", d[1], "#1");
175 d = new int[,] { { 1, 2 }, { 3, 4 } };
176 d[1, 0] = 100;
177 Assert (100, d[1, 0], "#2");
178 d[1, 0]++;
179 Assert (101, d[1, 0], "#2a");
181 d [0, 0] = d [1, 0] = 55;
182 Assert (55, d [0, 0], "#2a");
184 dynamic d2 = new Class ();
185 d2[2] = 500;
186 Assert<uint> (1000, d2[2], "#3");
187 d2[2]++;
188 Assert<uint> (2002, d2[2], "#3a");
189 d2[i:1] = 3;
190 Assert<uint> (3, d2[1], "#3b");
192 uint r = d2 [1] = 200;
193 Assert<uint> (200, r, "#4");
196 void SetIndexError_Null ()
198 dynamic d = null;
199 AssertError (() => { d [1] = 0; }, "#1");
202 #pragma warning restore 169
204 static bool RunTest (MethodInfo test)
206 Console.Write ("Running test {0, -25}", test.Name);
207 try {
208 test.Invoke (new Tester (), null);
209 Console.WriteLine ("OK");
210 return true;
211 } catch (Exception e) {
212 Console.WriteLine ("FAILED");
213 Console.WriteLine (e.ToString ());
214 // Console.WriteLine (e.InnerException.Message);
215 return false;
219 public static int Main ()
221 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
222 where test.GetParameters ().Length == 0
223 orderby test.Name
224 select RunTest (test);
226 int failures = tests.Count (a => !a);
227 Console.WriteLine (failures + " tests failed");
228 return failures;