fix MONOTOUCH on trunk
[mono.git] / mcs / tests / dtest-007.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Reflection;
5 using Microsoft.CSharp.RuntimeBinder;
6
7 // Dynamic member lookup tests
8
9 delegate void D ();
10
11 class Class
12 {
13         internal int IntValue = 5;
14         internal string StringStatic = "hi";
15
16         public const decimal Decimal = -0.3m;
17
18         uint s = 77;
19         protected internal uint this[byte i] {
20                 get {
21                         return s * i;
22                 }
23                 set {
24                         s = value;
25                 }
26         }
27
28         byte b = 180;
29         internal byte Prop {
30                 get {
31                         return b;
32                 }
33                 set {
34                         b = value;
35                 }
36         }
37
38         public int FixedValue
39         {
40                 set { }
41                 get { return 823; }
42         }
43 }
44
45 class Tester
46 {
47         delegate void EmptyDelegate ();
48         delegate int IntDelegate ();
49
50         static void Assert<T> (T expected, T value, string name)
51         {
52                 if (!EqualityComparer<T>.Default.Equals (expected, value)) {
53                         name += ": ";
54                         throw new ApplicationException (name + expected + " != " + value);
55                 }
56         }
57
58         static void AssertError (Action expected, string name)
59         {
60                 try {
61                         expected ();
62                         throw new ApplicationException (name + ": RuntimeBinderException expected");
63                 } catch (RuntimeBinderException) {
64                         // passed
65                 }
66         }
67
68 #pragma warning disable 169
69
70         void GetIndexTest ()
71         {
72                 dynamic d = new[] { 5, 8, 2 };
73                 Assert (8, d[1], "#1");
74
75                 d = new int[,] { { 1, 2 }, { 3, 4 } };
76                 Assert (3, d[1, 0], "#2");
77
78                 dynamic d2 = new Class ();
79                 Assert<uint> (154, d2[2], "#3");
80                 Assert<uint> (154, d2[i:2], "#3a");
81         }
82
83         void GetIndexError_Null ()
84         {
85                 dynamic d = null;
86                 AssertError (() => { var v = d[1]; }, "#1");
87         }
88
89         void MemberGetTest ()
90         {
91                 dynamic d = new Class ();
92                 Assert (5, d.IntValue, "#1");
93                 Assert (180, d.Prop, "#1a");
94
95                 Assert ("hi", d.StringStatic, "#2");
96
97                 d = new int[4];
98                 Assert (4, d.Length, "#3");
99
100                 // d.Event += delegate () { }; CS0019
101         }
102
103         void MemberGetError_Null ()
104         {
105                 dynamic d = null;
106                 AssertError (() => { var v = d.Foo; }, "#1");
107         }
108
109         void MemberSetTest ()
110         {
111                 dynamic d = new Class ();
112                 d.IntValue = 22;
113                 Assert (22, d.IntValue, "#1");
114                 d.Prop = 19;
115                 Assert (19, d.Prop, "#1a");
116
117                 d.Prop = byte.MaxValue;
118                 d.Prop++;
119                 Assert (0, d.Prop, "#1b");
120
121                 d.StringStatic = "no";
122                 Assert ("no", d.StringStatic, "#2");
123
124                 var r = d.FixedValue = 44;
125                 Assert (44, r, "#3");
126         }
127
128         void MemberSetError_Null ()
129         {
130                 dynamic d = null;
131                 AssertError (() => { d.Fo1 = 1; }, "#1");
132         }
133
134         void SetIndexTest ()
135         {
136                 dynamic d = new[] { "b", "v" };
137                 d[1] = "c";
138                 Assert ("c", d[1], "#1");
139
140                 d = new int[,] { { 1, 2 }, { 3, 4 } };
141                 d[1, 0] = 100;
142                 Assert (100, d[1, 0], "#2");
143                 d[1, 0]++;
144                 Assert (101, d[1, 0], "#2a");
145
146                 d [0, 0] = d [1, 0] = 55;
147                 Assert (55, d [0, 0], "#2a");
148
149                 dynamic d2 = new Class ();
150                 d2[2] = 500;
151                 Assert<uint> (1000, d2[2], "#3");
152                 d2[2]++;
153                 Assert<uint> (2002, d2[2], "#3a");
154                 d2[i:1] = 3;
155                 Assert<uint> (3, d2[1], "#3b");
156                 
157                 uint r = d2 [1] = 200;
158                 Assert<uint> (200, r, "#4");
159         }
160
161         void SetIndexError_Null ()
162         {
163                 dynamic d = null;
164                 AssertError (() => { d [1] = 0; }, "#1");
165         }
166
167 #pragma warning restore 169
168
169         static bool RunTest (MethodInfo test)
170         {
171                 Console.Write ("Running test {0, -25}", test.Name);
172                 try {
173                         test.Invoke (new Tester (), null);
174                         Console.WriteLine ("OK");
175                         return true;
176                 } catch (Exception e) {
177                         Console.WriteLine ("FAILED");
178                         Console.WriteLine (e.ToString ());
179                         //                      Console.WriteLine (e.InnerException.Message);
180                         return false;
181                 }
182         }
183
184         public static int Main ()
185         {
186                 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
187                                         where test.GetParameters ().Length == 0
188                                         orderby test.Name
189                                         select RunTest (test);
190
191                 int failures = tests.Count (a => !a);
192                 Console.WriteLine (failures + " tests failed");
193                 return failures;
194         }
195 }
196