Merge pull request #1812 from masterofjellyfish/mvc5-missingmethods
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Add.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 //
19 // Authors:
20 //              Federico Di Gregorio <fog@initd.org>
21 //              Jb Evain <jbevain@novell.com>
22
23 using System;
24 using System.Reflection;
25 using System.Linq;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
28
29 namespace MonoTests.System.Linq.Expressions
30 {
31         [TestFixture]
32         public class ExpressionTest_Add
33         {
34                 [Test]
35                 [ExpectedException (typeof (ArgumentNullException))]
36                 public void Arg1Null ()
37                 {
38                         Expression.Add (null, Expression.Constant (1));
39                 }
40
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void Arg2Null ()
44                 {
45                         Expression.Add (Expression.Constant (1), null);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (InvalidOperationException))]
50                 public void ArgTypesDifferent ()
51                 {
52                         Expression.Add (Expression.Constant (1), Expression.Constant (2.0));
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (InvalidOperationException))]
57                 public void NoOperatorClass ()
58                 {
59                         Expression.Add (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (InvalidOperationException))]
64                 public void Boolean ()
65                 {
66                         Expression.Add (Expression.Constant (true), Expression.Constant (false));
67                 }
68
69                 [Test]
70                 public void Numeric ()
71                 {
72                         BinaryExpression expr = Expression.Add (Expression.Constant (1), Expression.Constant (2));
73                         Assert.AreEqual (ExpressionType.Add, expr.NodeType, "Add#01");
74                         Assert.AreEqual (typeof (int), expr.Type, "Add#02");
75                         Assert.IsNull (expr.Method, "Add#03");
76                         Assert.AreEqual ("(1 + 2)", expr.ToString(), "Add#04");
77                 }
78
79                 [Test]
80                 public void Nullable ()
81                 {
82                         int? a = 1;
83                         int? b = 2;
84
85                         BinaryExpression expr = Expression.Add (Expression.Constant (a,typeof(int?)),
86                                                                 Expression.Constant (b, typeof(int?)));
87                         Assert.AreEqual (ExpressionType.Add, expr.NodeType, "Add#05");
88                         Assert.AreEqual (typeof (int?), expr.Type, "Add#06");
89                         Assert.IsNull (expr.Method, "Add#07");
90                         Assert.AreEqual ("(1 + 2)", expr.ToString(), "Add#08");
91                 }
92
93                 [Test]
94                 public void UserDefinedClass ()
95                 {
96                         // We can use the simplest version of GetMethod because we already know only one
97                         // exists in the very simple class we're using for the tests.
98                         MethodInfo mi = typeof (OpClass).GetMethod ("op_Addition");
99
100                         OpClass left = new OpClass ();
101                         BinaryExpression expr = Expression.Add (Expression.Constant (left), Expression.Constant (new OpClass ()));
102                         Assert.AreEqual (ExpressionType.Add, expr.NodeType, "Add#09");
103                         Assert.AreEqual (typeof (OpClass), expr.Type, "Add#10");
104                         Assert.AreEqual (mi, expr.Method, "Add#11");
105                         Assert.AreEqual ("op_Addition", expr.Method.Name, "Add#12");
106                         Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) + value(MonoTests.System.Linq.Expressions.OpClass))",
107                                 expr.ToString(), "Add#13");
108
109                         Expression.Lambda<Func<OpClass>> (expr);
110
111 #if false
112
113         //
114         // We do not have support for objects that are not really
115         // constants, like this case.   Need to figure out what to do
116         // with those
117         //
118
119                         Func<OpClass> compiled = l.Compile ();
120                         Assert.AreEqual (left, compiled  ());
121 #endif
122                 }
123
124                 public class S {
125                         public static int MyAdder (int a, int b){
126                                 return 1000;
127                         }
128                 }
129
130                 [Test]
131                 public void TestMethodAddition ()
132                 {
133                         BinaryExpression expr = Expression.Add (Expression.Constant (1), Expression.Constant (2), typeof(S).GetMethod("MyAdder"));
134                         Expression<Func<int>> l = Expression.Lambda<Func<int>> (expr);
135
136                         Func<int> compiled = l.Compile ();
137                         Assert.AreEqual (1000, compiled ());
138                 }
139
140                 [Test]
141                 public void CompileAdd ()
142                 {
143                         var left = Expression.Parameter (typeof (int), "l");
144                         var right = Expression.Parameter (typeof (int), "r");
145                         var l = Expression.Lambda<Func<int, int, int>> (
146                                 Expression.Add (left, right), left, right);
147
148                         var be = l.Body as BinaryExpression;
149                         Assert.IsNotNull (be);
150                         Assert.AreEqual (typeof (int), be.Type);
151                         Assert.IsFalse (be.IsLifted);
152                         Assert.IsFalse (be.IsLiftedToNull);
153
154                         var add = l.Compile ();
155
156                         Assert.AreEqual (12, add (6, 6));
157                         Assert.AreEqual (0, add (-1, 1));
158                         Assert.AreEqual (-2, add (1, -3));
159                 }
160
161                 [Test]
162                 public void AddLifted ()
163                 {
164                         var b = Expression.Add (
165                                 Expression.Constant (null, typeof (int?)),
166                                 Expression.Constant (null, typeof (int?)));
167
168                         Assert.AreEqual (typeof (int?), b.Type);
169                         Assert.IsTrue (b.IsLifted);
170                         Assert.IsTrue (b.IsLiftedToNull);
171                 }
172
173                 [Test]
174                 public void AddNotLifted ()
175                 {
176                         var b = Expression.Add (
177                                 Expression.Constant (1, typeof (int)),
178                                 Expression.Constant (1, typeof (int)));
179
180                         Assert.AreEqual (typeof (int), b.Type);
181                         Assert.IsFalse (b.IsLifted);
182                         Assert.IsFalse (b.IsLiftedToNull);
183                 }
184
185                 [Test]
186                 [Category ("NotWorkingInterpreter")]
187                 public void AddTestNullable ()
188                 {
189                         var a = Expression.Parameter (typeof (int?), "a");
190                         var b = Expression.Parameter (typeof (int?), "b");
191                         var l = Expression.Lambda<Func<int?, int?, int?>> (
192                                 Expression.Add (a, b), a, b);
193
194                         var be = l.Body as BinaryExpression;
195                         Assert.IsNotNull (be);
196                         Assert.AreEqual (typeof (int?), be.Type);
197                         Assert.IsTrue (be.IsLifted);
198                         Assert.IsTrue (be.IsLiftedToNull);
199
200                         var c = l.Compile ();
201
202                         Assert.AreEqual (null, c (1, null), "a1");
203                         Assert.AreEqual (null, c (null, null), "a2");
204                         Assert.AreEqual (null, c (null, 2), "a3");
205                         Assert.AreEqual (3,    c (1, 2), "a4");
206                 }
207
208                 struct Slot {
209                         public int Value;
210
211                         public Slot (int value)
212                         {
213                                 this.Value = value;
214                         }
215
216                         public static Slot operator + (Slot a, Slot b)
217                         {
218                                 return new Slot (a.Value + b.Value);
219                         }
220                 }
221
222                 [Test]
223                 public void UserDefinedAdd ()
224                 {
225                         var l = Expression.Parameter (typeof (Slot), "l");
226                         var r = Expression.Parameter (typeof (Slot), "r");
227
228                         var node = Expression.Add (l, r);
229
230                         Assert.IsFalse (node.IsLifted);
231                         Assert.IsFalse (node.IsLiftedToNull);
232                         Assert.AreEqual (typeof (Slot), node.Type);
233
234                         var add = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
235
236                         Assert.AreEqual (new Slot (42), add (new Slot (21), new Slot (21)));
237                         Assert.AreEqual (new Slot (0), add (new Slot (1), new Slot (-1)));
238                 }
239
240                 [Test]
241                 [Category ("NotWorkingInterpreter")]
242                 public void UserDefinedAddLifted ()
243                 {
244                         var l = Expression.Parameter (typeof (Slot?), "l");
245                         var r = Expression.Parameter (typeof (Slot?), "r");
246
247                         var node = Expression.Add (l, r);
248
249                         Assert.IsTrue (node.IsLifted);
250                         Assert.IsTrue (node.IsLiftedToNull);
251                         Assert.AreEqual (typeof (Slot?), node.Type);
252
253                         var add = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
254
255                         Assert.AreEqual (null, add (null, null));
256                         Assert.AreEqual ((Slot?) new Slot (42), add ((Slot?) new Slot (21), (Slot?) new Slot (21)));
257                 }
258
259                 struct SlotToNullable {
260                         public int Value;
261
262                         public SlotToNullable (int value)
263                         {
264                                 this.Value = value;
265                         }
266
267                         public static SlotToNullable? operator + (SlotToNullable a, SlotToNullable b)
268                         {
269                                 return new SlotToNullable (a.Value + b.Value);
270                         }
271                 }
272
273                 [Test]
274                 [ExpectedException (typeof (InvalidOperationException))]
275                 public void UserDefinedToNullableAddFromNullable ()
276                 {
277                         Expression.Add (
278                                 Expression.Parameter (typeof (SlotToNullable?), "l"),
279                                 Expression.Parameter (typeof (SlotToNullable?), "r"));
280                 }
281
282                 [Test]
283                 public void UserDefinedToNullableAdd ()
284                 {
285                         var l = Expression.Parameter (typeof (SlotToNullable), "l");
286                         var r = Expression.Parameter (typeof (SlotToNullable), "r");
287
288                         var node = Expression.Add (l, r);
289
290                         Assert.IsFalse (node.IsLifted);
291                         Assert.IsFalse (node.IsLiftedToNull);
292                         Assert.AreEqual (typeof (SlotToNullable?), node.Type);
293                         Assert.IsNotNull (node.Method);
294
295                         var add = Expression.Lambda<Func<SlotToNullable, SlotToNullable, SlotToNullable?>> (node, l, r).Compile ();
296
297                         Assert.AreEqual ((SlotToNullable?) new SlotToNullable (4), add (new SlotToNullable (2), new SlotToNullable (2)));
298                         Assert.AreEqual ((SlotToNullable?) new SlotToNullable (0), add (new SlotToNullable (2), new SlotToNullable (-2)));
299                 }
300
301                 /*struct SlotFromNullableToNullable {
302                         public int Value;
303
304                         public SlotFromNullableToNullable (int value)
305                         {
306                                 this.Value = value;
307                         }
308
309                         public static SlotFromNullableToNullable? operator + (SlotFromNullableToNullable? a, SlotFromNullableToNullable? b)
310                         {
311                                 if (a.HasValue && b.HasValue)
312                                         return (SlotFromNullableToNullable?) new SlotFromNullableToNullable (
313                                                 a.Value.Value + b.Value.Value);
314                                 else
315                                         return null;
316                         }
317                 }
318
319                 [Test]
320                 public void UserDefinedFromNullableToNullableAdd ()
321                 {
322                         var l = Expression.Parameter (typeof (SlotFromNullableToNullable?), "l");
323                         var r = Expression.Parameter (typeof (SlotFromNullableToNullable?), "r");
324
325                         var node = Expression.Add (l, r);
326
327                         Assert.IsFalse (node.IsLifted);
328                         Assert.IsFalse (node.IsLiftedToNull);
329                         Assert.AreEqual (typeof (SlotFromNullableToNullable?), node.Type);
330                         Assert.IsNotNull (node.Method);
331
332                         var add = Expression.Lambda<Func<SlotFromNullableToNullable?, SlotFromNullableToNullable?, SlotFromNullableToNullable?>> (node, l, r).Compile ();
333
334                         Assert.AreEqual ((SlotFromNullableToNullable?) null, add (null, null));
335                         Assert.AreEqual ((SlotFromNullableToNullable?) null, add (new SlotFromNullableToNullable (2), null));
336                         Assert.AreEqual ((SlotFromNullableToNullable?) null, add (null, new SlotFromNullableToNullable (2)));
337                         Assert.AreEqual ((SlotFromNullableToNullable?) new SlotFromNullableToNullable (4), add (new SlotFromNullableToNullable (2), new SlotFromNullableToNullable (2)));
338                         Assert.AreEqual ((SlotFromNullableToNullable?) new SlotFromNullableToNullable (0), add (new SlotFromNullableToNullable (2), new SlotFromNullableToNullable (-2)));
339                 }*/
340
341                 [Test]
342                 public void AddStrings ()
343                 {
344                         var l = Expression.Parameter (typeof (string), "l");
345                         var r = Expression.Parameter (typeof (string), "r");
346
347                         var meth = typeof (string).GetMethod ("Concat", new [] { typeof (object), typeof (object) });
348
349                         var node = Expression.Add (l, r, meth);
350                         Assert.IsFalse (node.IsLifted);
351                         Assert.IsFalse (node.IsLiftedToNull);
352                         Assert.AreEqual (typeof (string), node.Type);
353                         Assert.AreEqual (meth, node.Method);
354
355                         var concat = Expression.Lambda<Func<string, string, string>> (node, l, r).Compile ();
356
357                         Assert.AreEqual (string.Empty, concat (null, null));
358                         Assert.AreEqual ("foobar", concat ("foo", "bar"));
359                 }
360
361                 [Test]
362                 public void AddDecimals ()
363                 {
364                         var l = Expression.Parameter (typeof (decimal), "l");
365                         var r = Expression.Parameter (typeof (decimal), "r");
366
367                         var meth = typeof (decimal).GetMethod ("op_Addition", new [] { typeof (decimal), typeof (decimal) });
368
369                         var node = Expression.Add (l, r);
370                         Assert.IsFalse (node.IsLifted);
371                         Assert.IsFalse (node.IsLiftedToNull);
372                         Assert.AreEqual (typeof (decimal), node.Type);
373                         Assert.AreEqual (meth, node.Method);
374
375                         var add = Expression.Lambda<Func<decimal, decimal, decimal>> (node, l, r).Compile ();
376
377                         Assert.AreEqual (2m, add (1m, 1m));
378                 }
379
380                 [Test]
381                 [Category ("NotWorkingInterpreter")]
382                 public void AddLiftedDecimals ()
383                 {
384                         var l = Expression.Parameter (typeof (decimal?), "l");
385                         var r = Expression.Parameter (typeof (decimal?), "r");
386
387                         var meth = typeof (decimal).GetMethod ("op_Addition", new [] { typeof (decimal), typeof (decimal) });
388
389                         var node = Expression.Add (l, r);
390                         Assert.IsTrue (node.IsLifted);
391                         Assert.IsTrue (node.IsLiftedToNull);
392                         Assert.AreEqual (typeof (decimal?), node.Type);
393                         Assert.AreEqual (meth, node.Method);
394
395                         var add = Expression.Lambda<Func<decimal?, decimal?, decimal?>> (node, l, r).Compile ();
396
397                         Assert.AreEqual (2m, add (1m, 1m));
398                         Assert.AreEqual (null, add (1m, null));
399                         Assert.AreEqual (null, add (null, null));
400                 }
401         }
402 }