Merge pull request #4152 from BrzVlad/misc-gc-altstack
[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                 public void AddTestNullable ()
187                 {
188                         var a = Expression.Parameter (typeof (int?), "a");
189                         var b = Expression.Parameter (typeof (int?), "b");
190                         var l = Expression.Lambda<Func<int?, int?, int?>> (
191                                 Expression.Add (a, b), a, b);
192
193                         var be = l.Body as BinaryExpression;
194                         Assert.IsNotNull (be);
195                         Assert.AreEqual (typeof (int?), be.Type);
196                         Assert.IsTrue (be.IsLifted);
197                         Assert.IsTrue (be.IsLiftedToNull);
198
199                         var c = l.Compile ();
200
201                         Assert.AreEqual (null, c (1, null), "a1");
202                         Assert.AreEqual (null, c (null, null), "a2");
203                         Assert.AreEqual (null, c (null, 2), "a3");
204                         Assert.AreEqual (3,    c (1, 2), "a4");
205                 }
206
207                 struct Slot {
208                         public int Value;
209
210                         public Slot (int value)
211                         {
212                                 this.Value = value;
213                         }
214
215                         public static Slot operator + (Slot a, Slot b)
216                         {
217                                 return new Slot (a.Value + b.Value);
218                         }
219                 }
220
221                 [Test]
222                 public void UserDefinedAdd ()
223                 {
224                         var l = Expression.Parameter (typeof (Slot), "l");
225                         var r = Expression.Parameter (typeof (Slot), "r");
226
227                         var node = Expression.Add (l, r);
228
229                         Assert.IsFalse (node.IsLifted);
230                         Assert.IsFalse (node.IsLiftedToNull);
231                         Assert.AreEqual (typeof (Slot), node.Type);
232
233                         var add = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
234
235                         Assert.AreEqual (new Slot (42), add (new Slot (21), new Slot (21)));
236                         Assert.AreEqual (new Slot (0), add (new Slot (1), new Slot (-1)));
237                 }
238
239                 [Test]
240                 public void UserDefinedAddLifted ()
241                 {
242                         var l = Expression.Parameter (typeof (Slot?), "l");
243                         var r = Expression.Parameter (typeof (Slot?), "r");
244
245                         var node = Expression.Add (l, r);
246
247                         Assert.IsTrue (node.IsLifted);
248                         Assert.IsTrue (node.IsLiftedToNull);
249                         Assert.AreEqual (typeof (Slot?), node.Type);
250
251                         var add = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
252
253                         Assert.AreEqual (null, add (null, null));
254                         Assert.AreEqual ((Slot?) new Slot (42), add ((Slot?) new Slot (21), (Slot?) new Slot (21)));
255                 }
256
257                 struct SlotToNullable {
258                         public int Value;
259
260                         public SlotToNullable (int value)
261                         {
262                                 this.Value = value;
263                         }
264
265                         public static SlotToNullable? operator + (SlotToNullable a, SlotToNullable b)
266                         {
267                                 return new SlotToNullable (a.Value + b.Value);
268                         }
269                 }
270
271                 [Test]
272                 [ExpectedException (typeof (InvalidOperationException))]
273                 public void UserDefinedToNullableAddFromNullable ()
274                 {
275                         Expression.Add (
276                                 Expression.Parameter (typeof (SlotToNullable?), "l"),
277                                 Expression.Parameter (typeof (SlotToNullable?), "r"));
278                 }
279
280                 [Test]
281                 public void UserDefinedToNullableAdd ()
282                 {
283                         var l = Expression.Parameter (typeof (SlotToNullable), "l");
284                         var r = Expression.Parameter (typeof (SlotToNullable), "r");
285
286                         var node = Expression.Add (l, r);
287
288                         Assert.IsFalse (node.IsLifted);
289                         Assert.IsFalse (node.IsLiftedToNull);
290                         Assert.AreEqual (typeof (SlotToNullable?), node.Type);
291                         Assert.IsNotNull (node.Method);
292
293                         var add = Expression.Lambda<Func<SlotToNullable, SlotToNullable, SlotToNullable?>> (node, l, r).Compile ();
294
295                         Assert.AreEqual ((SlotToNullable?) new SlotToNullable (4), add (new SlotToNullable (2), new SlotToNullable (2)));
296                         Assert.AreEqual ((SlotToNullable?) new SlotToNullable (0), add (new SlotToNullable (2), new SlotToNullable (-2)));
297                 }
298
299                 /*struct SlotFromNullableToNullable {
300                         public int Value;
301
302                         public SlotFromNullableToNullable (int value)
303                         {
304                                 this.Value = value;
305                         }
306
307                         public static SlotFromNullableToNullable? operator + (SlotFromNullableToNullable? a, SlotFromNullableToNullable? b)
308                         {
309                                 if (a.HasValue && b.HasValue)
310                                         return (SlotFromNullableToNullable?) new SlotFromNullableToNullable (
311                                                 a.Value.Value + b.Value.Value);
312                                 else
313                                         return null;
314                         }
315                 }
316
317                 [Test]
318                 public void UserDefinedFromNullableToNullableAdd ()
319                 {
320                         var l = Expression.Parameter (typeof (SlotFromNullableToNullable?), "l");
321                         var r = Expression.Parameter (typeof (SlotFromNullableToNullable?), "r");
322
323                         var node = Expression.Add (l, r);
324
325                         Assert.IsFalse (node.IsLifted);
326                         Assert.IsFalse (node.IsLiftedToNull);
327                         Assert.AreEqual (typeof (SlotFromNullableToNullable?), node.Type);
328                         Assert.IsNotNull (node.Method);
329
330                         var add = Expression.Lambda<Func<SlotFromNullableToNullable?, SlotFromNullableToNullable?, SlotFromNullableToNullable?>> (node, l, r).Compile ();
331
332                         Assert.AreEqual ((SlotFromNullableToNullable?) null, add (null, null));
333                         Assert.AreEqual ((SlotFromNullableToNullable?) null, add (new SlotFromNullableToNullable (2), null));
334                         Assert.AreEqual ((SlotFromNullableToNullable?) null, add (null, new SlotFromNullableToNullable (2)));
335                         Assert.AreEqual ((SlotFromNullableToNullable?) new SlotFromNullableToNullable (4), add (new SlotFromNullableToNullable (2), new SlotFromNullableToNullable (2)));
336                         Assert.AreEqual ((SlotFromNullableToNullable?) new SlotFromNullableToNullable (0), add (new SlotFromNullableToNullable (2), new SlotFromNullableToNullable (-2)));
337                 }*/
338
339                 [Test]
340                 public void AddStrings ()
341                 {
342                         var l = Expression.Parameter (typeof (string), "l");
343                         var r = Expression.Parameter (typeof (string), "r");
344
345                         var meth = typeof (string).GetMethod ("Concat", new [] { typeof (object), typeof (object) });
346
347                         var node = Expression.Add (l, r, meth);
348                         Assert.IsFalse (node.IsLifted);
349                         Assert.IsFalse (node.IsLiftedToNull);
350                         Assert.AreEqual (typeof (string), node.Type);
351                         Assert.AreEqual (meth, node.Method);
352
353                         var concat = Expression.Lambda<Func<string, string, string>> (node, l, r).Compile ();
354
355                         Assert.AreEqual (string.Empty, concat (null, null));
356                         Assert.AreEqual ("foobar", concat ("foo", "bar"));
357                 }
358
359                 [Test]
360                 public void AddDecimals ()
361                 {
362                         var l = Expression.Parameter (typeof (decimal), "l");
363                         var r = Expression.Parameter (typeof (decimal), "r");
364
365                         var meth = typeof (decimal).GetMethod ("op_Addition", new [] { typeof (decimal), typeof (decimal) });
366
367                         var node = Expression.Add (l, r);
368                         Assert.IsFalse (node.IsLifted);
369                         Assert.IsFalse (node.IsLiftedToNull);
370                         Assert.AreEqual (typeof (decimal), node.Type);
371                         Assert.AreEqual (meth, node.Method);
372
373                         var add = Expression.Lambda<Func<decimal, decimal, decimal>> (node, l, r).Compile ();
374
375                         Assert.AreEqual (2m, add (1m, 1m));
376                 }
377
378                 [Test]
379                 public void AddLiftedDecimals ()
380                 {
381                         var l = Expression.Parameter (typeof (decimal?), "l");
382                         var r = Expression.Parameter (typeof (decimal?), "r");
383
384                         var meth = typeof (decimal).GetMethod ("op_Addition", new [] { typeof (decimal), typeof (decimal) });
385
386                         var node = Expression.Add (l, r);
387                         Assert.IsTrue (node.IsLifted);
388                         Assert.IsTrue (node.IsLiftedToNull);
389                         Assert.AreEqual (typeof (decimal?), node.Type);
390                         Assert.AreEqual (meth, node.Method);
391
392                         var add = Expression.Lambda<Func<decimal?, decimal?, decimal?>> (node, l, r).Compile ();
393
394                         Assert.AreEqual (2m, add (1m, 1m));
395                         Assert.AreEqual (null, add (1m, null));
396                         Assert.AreEqual (null, add (null, null));
397                 }
398         }
399 }