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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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
20 // Federico Di Gregorio <fog@initd.org>
21 // Jb Evain <jbevain@novell.com>
24 using System.Reflection;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
29 namespace MonoTests.System.Linq.Expressions
32 public class ExpressionTest_Add
35 [ExpectedException (typeof (ArgumentNullException))]
36 public void Arg1Null ()
38 Expression.Add (null, Expression.Constant (1));
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void Arg2Null ()
45 Expression.Add (Expression.Constant (1), null);
49 [ExpectedException (typeof (InvalidOperationException))]
50 public void ArgTypesDifferent ()
52 Expression.Add (Expression.Constant (1), Expression.Constant (2.0));
56 [ExpectedException (typeof (InvalidOperationException))]
57 public void NoOperatorClass ()
59 Expression.Add (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
63 [ExpectedException (typeof (InvalidOperationException))]
64 public void Boolean ()
66 Expression.Add (Expression.Constant (true), Expression.Constant (false));
70 public void Numeric ()
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");
80 public void Nullable ()
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");
94 public void UserDefinedClass ()
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");
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");
109 Expression.Lambda<Func<OpClass>> (expr);
114 // We do not have support for objects that are not really
115 // constants, like this case. Need to figure out what to do
119 Func<OpClass> compiled = l.Compile ();
120 Assert.AreEqual (left, compiled ());
125 public static int MyAdder (int a, int b){
131 public void TestMethodAddition ()
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);
136 Func<int> compiled = l.Compile ();
137 Assert.AreEqual (1000, compiled ());
141 public void CompileAdd ()
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);
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);
154 var add = l.Compile ();
156 Assert.AreEqual (12, add (6, 6));
157 Assert.AreEqual (0, add (-1, 1));
158 Assert.AreEqual (-2, add (1, -3));
162 public void AddLifted ()
164 var b = Expression.Add (
165 Expression.Constant (null, typeof (int?)),
166 Expression.Constant (null, typeof (int?)));
168 Assert.AreEqual (typeof (int?), b.Type);
169 Assert.IsTrue (b.IsLifted);
170 Assert.IsTrue (b.IsLiftedToNull);
174 public void AddNotLifted ()
176 var b = Expression.Add (
177 Expression.Constant (1, typeof (int)),
178 Expression.Constant (1, typeof (int)));
180 Assert.AreEqual (typeof (int), b.Type);
181 Assert.IsFalse (b.IsLifted);
182 Assert.IsFalse (b.IsLiftedToNull);
186 public void AddTestNullable ()
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);
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);
199 var c = l.Compile ();
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");
210 public Slot (int value)
215 public static Slot operator + (Slot a, Slot b)
217 return new Slot (a.Value + b.Value);
222 public void UserDefinedAdd ()
224 var l = Expression.Parameter (typeof (Slot), "l");
225 var r = Expression.Parameter (typeof (Slot), "r");
227 var node = Expression.Add (l, r);
229 Assert.IsFalse (node.IsLifted);
230 Assert.IsFalse (node.IsLiftedToNull);
231 Assert.AreEqual (typeof (Slot), node.Type);
233 var add = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
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)));
240 public void UserDefinedAddLifted ()
242 var l = Expression.Parameter (typeof (Slot?), "l");
243 var r = Expression.Parameter (typeof (Slot?), "r");
245 var node = Expression.Add (l, r);
247 Assert.IsTrue (node.IsLifted);
248 Assert.IsTrue (node.IsLiftedToNull);
249 Assert.AreEqual (typeof (Slot?), node.Type);
251 var add = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
253 Assert.AreEqual (null, add (null, null));
254 Assert.AreEqual ((Slot?) new Slot (42), add ((Slot?) new Slot (21), (Slot?) new Slot (21)));
257 struct SlotToNullable {
260 public SlotToNullable (int value)
265 public static SlotToNullable? operator + (SlotToNullable a, SlotToNullable b)
267 return new SlotToNullable (a.Value + b.Value);
272 [ExpectedException (typeof (InvalidOperationException))]
273 public void UserDefinedToNullableAddFromNullable ()
276 Expression.Parameter (typeof (SlotToNullable?), "l"),
277 Expression.Parameter (typeof (SlotToNullable?), "r"));
281 public void UserDefinedToNullableAdd ()
283 var l = Expression.Parameter (typeof (SlotToNullable), "l");
284 var r = Expression.Parameter (typeof (SlotToNullable), "r");
286 var node = Expression.Add (l, r);
288 Assert.IsFalse (node.IsLifted);
289 Assert.IsFalse (node.IsLiftedToNull);
290 Assert.AreEqual (typeof (SlotToNullable?), node.Type);
291 Assert.IsNotNull (node.Method);
293 var add = Expression.Lambda<Func<SlotToNullable, SlotToNullable, SlotToNullable?>> (node, l, r).Compile ();
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)));
299 /*struct SlotFromNullableToNullable {
302 public SlotFromNullableToNullable (int value)
307 public static SlotFromNullableToNullable? operator + (SlotFromNullableToNullable? a, SlotFromNullableToNullable? b)
309 if (a.HasValue && b.HasValue)
310 return (SlotFromNullableToNullable?) new SlotFromNullableToNullable (
311 a.Value.Value + b.Value.Value);
318 public void UserDefinedFromNullableToNullableAdd ()
320 var l = Expression.Parameter (typeof (SlotFromNullableToNullable?), "l");
321 var r = Expression.Parameter (typeof (SlotFromNullableToNullable?), "r");
323 var node = Expression.Add (l, r);
325 Assert.IsFalse (node.IsLifted);
326 Assert.IsFalse (node.IsLiftedToNull);
327 Assert.AreEqual (typeof (SlotFromNullableToNullable?), node.Type);
328 Assert.IsNotNull (node.Method);
330 var add = Expression.Lambda<Func<SlotFromNullableToNullable?, SlotFromNullableToNullable?, SlotFromNullableToNullable?>> (node, l, r).Compile ();
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)));
340 public void AddStrings ()
342 var l = Expression.Parameter (typeof (string), "l");
343 var r = Expression.Parameter (typeof (string), "r");
345 var meth = typeof (string).GetMethod ("Concat", new [] { typeof (object), typeof (object) });
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);
353 var concat = Expression.Lambda<Func<string, string, string>> (node, l, r).Compile ();
355 Assert.AreEqual (string.Empty, concat (null, null));
356 Assert.AreEqual ("foobar", concat ("foo", "bar"));
360 public void AddDecimals ()
362 var l = Expression.Parameter (typeof (decimal), "l");
363 var r = Expression.Parameter (typeof (decimal), "r");
365 var meth = typeof (decimal).GetMethod ("op_Addition", new [] { typeof (decimal), typeof (decimal) });
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);
373 var add = Expression.Lambda<Func<decimal, decimal, decimal>> (node, l, r).Compile ();
375 Assert.AreEqual (2m, add (1m, 1m));
379 public void AddLiftedDecimals ()
381 var l = Expression.Parameter (typeof (decimal?), "l");
382 var r = Expression.Parameter (typeof (decimal?), "r");
384 var meth = typeof (decimal).GetMethod ("op_Addition", new [] { typeof (decimal), typeof (decimal) });
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);
392 var add = Expression.Lambda<Func<decimal?, decimal?, decimal?>> (node, l, r).Compile ();
394 Assert.AreEqual (2m, add (1m, 1m));
395 Assert.AreEqual (null, add (1m, null));
396 Assert.AreEqual (null, add (null, null));