[interp] Fall back to old implementation when calling on proxy
[mono-project.git] / mcs / tests / dtest-007.cs
blobec92b623dbf4639437df32248944c3eb0d267e40
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 ();
10 delegate void D2 (decimal d);
12 class Class
14 internal int IntValue = 5;
15 internal string StringStatic = "hi";
17 public const decimal Decimal = -0.3m;
19 public Class ()
23 public Class (sbyte extra)
25 IntValue = extra;
28 uint s = 77;
29 protected internal uint this[byte i] {
30 get {
31 return s * i;
33 set {
34 s = value;
38 byte b = 180;
39 internal byte Prop {
40 get {
41 return b;
43 set {
44 b = value;
48 public int FixedValue
50 set { }
51 get { return 823; }
54 internal string Method (string value)
56 return value;
59 public int Method (int a, byte b)
61 return a * b;
64 public void MethodInOut (ref int refValue, out string outValue)
66 refValue = 3;
67 outValue = "4";
70 public static void GenericVoid<T> (T i)
74 public static int StaticMethod (params int[] i)
76 return i [0] * i.Length;
79 public static void ArglistMethod (__arglist)
84 class EventClass
86 internal event Func<int> OutEvent;
88 public int CallEvent ()
90 return OutEvent ();
94 class Tester
96 static void Assert<T> (T expected, T value, string name)
98 if (!EqualityComparer<T>.Default.Equals (expected, value)) {
99 name += ": ";
100 throw new ApplicationException (name + expected + " != " + value);
104 static void AssertError (Action expected, string name)
106 try {
107 expected ();
108 throw new ApplicationException (name + ": RuntimeBinderException expected");
109 } catch (RuntimeBinderException) {
110 // passed
114 event Func<int> e;
115 int field;
118 #pragma warning disable 169
120 void GetIndexTest ()
122 dynamic d = new[] { 5, 8, 2 };
123 Assert (8, d[1], "#1");
125 d = new int[,] { { 1, 2 }, { 3, 4 } };
126 Assert (3, d[1, 0], "#2");
128 dynamic d2 = new Class ();
129 Assert<uint> (154, d2[2], "#3");
130 Assert<uint> (154, d2[i:2], "#3a");
133 void GetIndexError_Null ()
135 dynamic d = null;
136 AssertError (() => { var v = d[1]; }, "#1");
139 void InvokeTest ()
141 Func<string, string> f = new Class().Method;
142 dynamic d = f;
143 Assert ("bar", d ("bar"), "#1");
145 Action<bool> f2 = Class.GenericVoid;
146 d = f2;
147 d (true);
149 Func<string, int> f3 = (s) => 3;
150 d = f3;
151 d ("go");
154 void InvokeMember ()
156 dynamic d = new Class ();
157 Assert ("vv", d.Method ("vv"), "#1");
159 byte b = 2;
160 Assert (6, d.Method (b: b++, a: b), "#1a");
162 var arg1 = 1;
163 var arg2 = "a";
164 d.MethodInOut (ref arg1, out arg2);
166 d = 2;
167 Assert (2, Class.StaticMethod (d), "#2");
168 Class.StaticMethod (d);
171 void InvokeMember_Error ()
173 AssertError (() => {
174 dynamic d_arg = "a";
175 Class.ArglistMethod (d_arg);
176 }, "#1");
179 void InvokeConstructor ()
181 dynamic d = (sbyte) 8;
182 var r = new Class (d);
183 Assert (8, r.IntValue, "#1");
185 D2 method = (decimal e) => { };
186 d = method;
187 var r2 = new D2 (d);
190 void IsEvent ()
192 dynamic d = this;
193 d.e += new Func<int> (() => 3);
194 Assert (3, d.e (), "#1");
196 d.field += 5;
197 Assert (5, d.field, "#2");
199 int r = d.field += 7;
200 Assert (12, r, "#2a");
201 Assert (12, d.field, "#2b");
203 d = new EventClass ();
204 d.OutEvent += new Func<int> (() => 100);
205 Assert (100, d.CallEvent (), "#3");
208 void MemberGetTest ()
210 dynamic d = new Class ();
211 Assert (5, d.IntValue, "#1");
212 Assert (180, d.Prop, "#1a");
214 Assert ("hi", d.StringStatic, "#2");
216 d = new int[4];
217 Assert (4, d.Length, "#3");
220 void MemberGetError_Null ()
222 dynamic d = null;
223 AssertError (() => { var v = d.Foo; }, "#1");
226 void MemberSetTest ()
228 dynamic d = new Class ();
230 d.IntValue = 22;
231 Assert (22, d.IntValue, "#1");
232 d.Prop = 19;
233 Assert (19, d.Prop, "#1a");
235 d.Prop = byte.MaxValue;
236 Assert (byte.MaxValue, d.Prop++, "#1b");
237 Assert (1, ++d.Prop, "#1c");
238 d.Prop++;
239 Assert (2, d.Prop, "#1d");
241 d.Prop += 5;
242 Assert (7, d.Prop, "#1e");
244 d.StringStatic = "no";
245 Assert ("no", d.StringStatic, "#2");
247 var r = d.FixedValue = 44;
248 Assert (44, r, "#3");
251 void MemberSetError_Null ()
253 dynamic d = null;
254 AssertError (() => { d.Fo1 = 1; }, "#1");
257 void SetIndexTest ()
259 dynamic d = new[] { "b", "v" };
260 d[1] = "c";
261 Assert ("c", d[1], "#1");
263 d = new int[,] { { 1, 2 }, { 3, 4 } };
264 d[1, 0] = 100;
265 Assert (100, d[1, 0], "#2");
266 d[1, 0]++;
267 Assert (101, d[1, 0], "#2a");
269 d [0, 0] = d [1, 0] = 55;
270 Assert (55, d [0, 0], "#2a");
272 dynamic d2 = new Class ();
273 d2[2] = 500;
274 Assert<uint> (1000, d2[2], "#3");
275 d2[2]++;
276 Assert<uint> (2002, d2[2], "#3a");
277 d2[i:1] = 3;
278 Assert<uint> (3, d2[1], "#3b");
280 uint r = d2 [1] = 200;
281 Assert<uint> (200, r, "#4");
284 void SetIndexError_Null ()
286 dynamic d = null;
287 AssertError (() => { d [1] = 0; }, "#1");
290 #pragma warning restore 169
292 static bool RunTest (MethodInfo test)
294 Console.Write ("Running test {0, -25}", test.Name);
295 try {
296 test.Invoke (new Tester (), null);
297 Console.WriteLine ("OK");
298 return true;
299 } catch (Exception e) {
300 Console.WriteLine ("FAILED");
301 Console.WriteLine (e.ToString ());
302 // Console.WriteLine (e.InnerException.Message);
303 return false;
307 public static int Main ()
309 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
310 where test.GetParameters ().Length == 0
311 orderby test.Name
312 select RunTest (test);
314 int failures = tests.Count (a => !a);
315 Console.WriteLine (failures + " tests failed");
316 return failures;