New tests.
[mono.git] / mcs / tests / gtest-etree-01.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Linq.Expressions;
5
6 class Tester
7 {
8         static void AssertNodeType (LambdaExpression e, ExpressionType et)
9         {
10                 if (e.Body.NodeType != et)
11                         throw new ApplicationException (e.Body.NodeType + " != " + et);
12         }
13
14         static void Assert<T> (T expected, T value)
15         {
16                 if (!EqualityComparer<T>.Default.Equals (expected, value))
17                         throw new ApplicationException (expected + " != " + value);
18         }
19
20         void AddTest ()
21         {
22                 Expression<Func<int, int, int>> e = (int a, int b) => a + b;
23                 AssertNodeType (e, ExpressionType.Add);
24                 Assert (50, e.Compile ().Invoke (20, 30));
25
26                 Expression<Func<int?, int?, int?>> e2 = (a, b) => a + b;
27                 AssertNodeType (e2, ExpressionType.Add);
28                 Assert (null, e2.Compile ().Invoke (null, 3));
29         }
30
31         void AddCheckedTest ()
32         {
33                 checked {
34                         Expression<Func<int, int, int>> e = (int a, int b) => a + b;
35
36                         AssertNodeType (e, ExpressionType.AddChecked);
37                         Assert (-10, e.Compile ().Invoke (20, -30));
38                 }
39         }
40
41         void AndTest ()
42         {
43                 Expression<Func<bool, bool, bool>> e = (bool a, bool b) => a & b;
44
45                 AssertNodeType (e, ExpressionType.And);
46                 Assert (false, e.Compile ().Invoke (false, true));
47         }
48
49         void AndAlsoTest ()
50         {
51                 Expression<Func<bool, bool, bool>> e = (bool a, bool b) => a && b;
52
53                 AssertNodeType (e, ExpressionType.AndAlso);
54                 Assert (false, e.Compile ().Invoke (true, false));
55         }
56
57         void ArrayIndexTest ()
58         {
59                 Expression<Func<string[], long, string>> e = (string[] a, long i) => a [i];
60                 AssertNodeType (e, ExpressionType.ArrayIndex);
61                 Assert ("b", e.Compile ().Invoke (new string [] { "a", "b", "c" }, 1));
62
63                 Expression<Func<string [], string>> e2 = (string [] a) => a [0];
64                 AssertNodeType (e2, ExpressionType.ArrayIndex);
65                 Assert ("a", e2.Compile ().Invoke (new string [] { "a", "b" }));
66
67                 Expression<Func<object [,], int, int, object>> e3 = (object [,] a, int i, int j) => a [i, j];
68                 AssertNodeType (e3, ExpressionType.Call);
69                 
70                 Assert ("z", e3.Compile ().Invoke (
71                         new object [,] { { 1, 2 }, { "x", "z" } }, 1, 1));
72
73                 Expression<Func<decimal [][], byte, decimal>> e4 = (decimal [][] a, byte b) => a [b][1];
74                 AssertNodeType (e4, ExpressionType.ArrayIndex);
75
76                 decimal [] [] array = { new decimal [] { 1, 9 }, new decimal [] { 10, 90 } };
77                 Assert (90, e4.Compile ().Invoke (array, 1));
78         }
79         
80         void ArrayLengthTest ()
81         {
82                 int o = new int [0].Length;
83
84                 Expression<Func<double [], int>> e = (double [] a) => a.Length;
85                 AssertNodeType (e, ExpressionType.ArrayLength);
86                 Assert (0, e.Compile ().Invoke (new double [0]));
87                 Assert (9, e.Compile ().Invoke (new double [9]));
88
89                 // TODO: implement
90                 //Expression<Func<string [,], int>> e2 = (string [,] a) => a.Length;
91                 //AssertNodeType (e2, ExpressionType.MemberAccess);
92                 //Assert (0, e2.Compile ().Invoke (new string [0, 0]));
93         }       
94         
95         void CallTest ()
96         {
97                 Expression<Func<int, int>> e = (int a) => Math.Max (a, 5);
98                 AssertNodeType (e, ExpressionType.Call);
99                 Assert (5, e.Compile ().Invoke (2));
100                 Assert (9, e.Compile ().Invoke (9));
101                 
102                 Expression<Func<string, string>> e2 = (string a) => InstanceMethod (a);
103                 AssertNodeType (e2, ExpressionType.Call);
104                 Assert ("abc", e2.Compile ().Invoke ("abc"));
105 /*
106                 // TODO: implement
107                 Expression<Func<int, string, int, object>> e3 = (int index, string a, int b) => InstanceParamsMethod (index, a, b);
108                 AssertNodeType (e3, ExpressionType.Call);
109                 Assert<object> (4, e3.Compile ().Invoke (1, "a", 4));
110
111                 Expression<Func<object>> e4 = () => InstanceParamsMethod (0);
112                 AssertNodeType (e4, ExpressionType.Call);
113                 Assert<object> ("<empty>", e4.Compile ().Invoke ());
114 */
115
116                 Expression<Func<int, int>> e5 = (int a) => GenericMethod (a);
117                 AssertNodeType (e5, ExpressionType.Call);
118                 Assert (5, e5.Compile ().Invoke (5));
119         }
120         
121         
122         //
123         // Test helpers
124         //
125         string InstanceMethod (string arg)
126         {
127                 return arg;
128         }
129         
130         object InstanceParamsMethod (int index, params object[] args)
131         {
132                 if (args == null)
133                         return "<null>";
134                 if (args.Length == 0)
135                         return "<empty>";
136                 return args [index];
137         }
138         
139         T GenericMethod<T> (T t)
140         {
141                 return t;
142         }       
143
144
145         public static int Main ()
146         {
147                 Tester e = new Tester ();
148                 e.AddTest ();
149                 e.AddCheckedTest ();
150                 e.AndTest ();
151                 e.AndAlsoTest ();
152                 e.ArrayIndexTest ();
153                 e.CallTest ();
154                 
155                 return 0;
156         }
157 }
158
159