2010-05-19 Jb Evain <jbevain@novell.com>
[mcs.git] / tests / dtest-007.cs
blob65c7a1c7edbfbf05b00a2387a3350e49c5c2560f
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;
80 class Tester
82 static void Assert<T> (T expected, T value, string name)
84 if (!EqualityComparer<T>.Default.Equals (expected, value)) {
85 name += ": ";
86 throw new ApplicationException (name + expected + " != " + value);
90 static void AssertError (Action expected, string name)
92 try {
93 expected ();
94 throw new ApplicationException (name + ": RuntimeBinderException expected");
95 } catch (RuntimeBinderException) {
96 // passed
100 #pragma warning disable 169
102 void GetIndexTest ()
104 dynamic d = new[] { 5, 8, 2 };
105 Assert (8, d[1], "#1");
107 d = new int[,] { { 1, 2 }, { 3, 4 } };
108 Assert (3, d[1, 0], "#2");
110 dynamic d2 = new Class ();
111 Assert<uint> (154, d2[2], "#3");
112 Assert<uint> (154, d2[i:2], "#3a");
115 void GetIndexError_Null ()
117 dynamic d = null;
118 AssertError (() => { var v = d[1]; }, "#1");
121 void InvokeTest ()
123 Func<string, string> f = new Class().Method;
124 dynamic d = f;
125 Assert ("bar", d ("bar"), "#1");
127 Action<bool> f2 = Class.GenericVoid;
128 d = f2;
129 d (true);
131 Func<string, int> f3 = (s) => 3;
132 d = f3;
133 d ("go");
136 void InvokeMember ()
138 dynamic d = new Class ();
139 Assert ("vv", d.Method ("vv"), "#1");
141 byte b = 2;
142 Assert (6, d.Method (b: b++, a: b), "#1a");
144 var arg1 = 1;
145 var arg2 = "a";
146 d.MethodInOut (ref arg1, out arg2);
148 d = 2;
149 Assert (2, Class.StaticMethod (d), "#2");
150 Class.StaticMethod (d);
153 void InvokeConstructor ()
155 dynamic d = (sbyte) 8;
156 var r = new Class (d);
157 Assert (8, r.IntValue, "#1");
159 D2 method = (decimal e) => { };
160 d = method;
161 var r2 = new D2 (d);
164 event Func<int> e;
165 int field;
166 void IsEvent ()
168 dynamic d = this;
169 d.e += new Func<int> (() => 3);
171 // FIXME:
172 //Assert (3, d.e (), "#1");
174 d.field += 5;
175 Assert (5, d.field, "#2");
178 void MemberGetTest ()
180 dynamic d = new Class ();
181 Assert (5, d.IntValue, "#1");
182 Assert (180, d.Prop, "#1a");
184 Assert ("hi", d.StringStatic, "#2");
186 d = new int[4];
187 Assert (4, d.Length, "#3");
190 void MemberGetError_Null ()
192 dynamic d = null;
193 AssertError (() => { var v = d.Foo; }, "#1");
196 void MemberSetTest ()
198 dynamic d = new Class ();
200 d.IntValue = 22;
201 Assert (22, d.IntValue, "#1");
202 d.Prop = 19;
203 Assert (19, d.Prop, "#1a");
205 d.Prop = byte.MaxValue;
206 Assert (byte.MaxValue, d.Prop++, "#1b");
207 Assert (1, ++d.Prop, "#1c");
208 d.Prop++;
209 Assert (2, d.Prop, "#1d");
211 d.Prop += 5;
212 Assert (7, d.Prop, "#1e");
214 d.StringStatic = "no";
215 Assert ("no", d.StringStatic, "#2");
217 var r = d.FixedValue = 44;
218 Assert (44, r, "#3");
221 void MemberSetError_Null ()
223 dynamic d = null;
224 AssertError (() => { d.Fo1 = 1; }, "#1");
227 void SetIndexTest ()
229 dynamic d = new[] { "b", "v" };
230 d[1] = "c";
231 Assert ("c", d[1], "#1");
233 d = new int[,] { { 1, 2 }, { 3, 4 } };
234 d[1, 0] = 100;
235 Assert (100, d[1, 0], "#2");
236 d[1, 0]++;
237 Assert (101, d[1, 0], "#2a");
239 d [0, 0] = d [1, 0] = 55;
240 Assert (55, d [0, 0], "#2a");
242 dynamic d2 = new Class ();
243 d2[2] = 500;
244 Assert<uint> (1000, d2[2], "#3");
245 d2[2]++;
246 Assert<uint> (2002, d2[2], "#3a");
247 d2[i:1] = 3;
248 Assert<uint> (3, d2[1], "#3b");
250 uint r = d2 [1] = 200;
251 Assert<uint> (200, r, "#4");
254 void SetIndexError_Null ()
256 dynamic d = null;
257 AssertError (() => { d [1] = 0; }, "#1");
260 #pragma warning restore 169
262 static bool RunTest (MethodInfo test)
264 Console.Write ("Running test {0, -25}", test.Name);
265 try {
266 test.Invoke (new Tester (), null);
267 Console.WriteLine ("OK");
268 return true;
269 } catch (Exception e) {
270 Console.WriteLine ("FAILED");
271 Console.WriteLine (e.ToString ());
272 // Console.WriteLine (e.InnerException.Message);
273 return false;
277 public static int Main ()
279 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
280 where test.GetParameters ().Length == 0
281 orderby test.Name
282 select RunTest (test);
284 int failures = tests.Count (a => !a);
285 Console.WriteLine (failures + " tests failed");
286 return failures;