File: ExpressionTest_Add.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (402 lines) | stat: -rw-r--r-- 12,603 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
//
// Authors:
//		Federico Di Gregorio <fog@initd.org>
//		Jb Evain <jbevain@novell.com>

using System;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using NUnit.Framework;

namespace MonoTests.System.Linq.Expressions
{
	[TestFixture]
	public class ExpressionTest_Add
	{
		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void Arg1Null ()
		{
			Expression.Add (null, Expression.Constant (1));
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void Arg2Null ()
		{
			Expression.Add (Expression.Constant (1), null);
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void ArgTypesDifferent ()
		{
			Expression.Add (Expression.Constant (1), Expression.Constant (2.0));
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void NoOperatorClass ()
		{
			Expression.Add (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void Boolean ()
		{
			Expression.Add (Expression.Constant (true), Expression.Constant (false));
		}

		[Test]
		public void Numeric ()
		{
			BinaryExpression expr = Expression.Add (Expression.Constant (1), Expression.Constant (2));
			Assert.AreEqual (ExpressionType.Add, expr.NodeType, "Add#01");
			Assert.AreEqual (typeof (int), expr.Type, "Add#02");
			Assert.IsNull (expr.Method, "Add#03");
			Assert.AreEqual ("(1 + 2)", expr.ToString(), "Add#04");
		}

		[Test]
		public void Nullable ()
		{
			int? a = 1;
			int? b = 2;

			BinaryExpression expr = Expression.Add (Expression.Constant (a,typeof(int?)),
								Expression.Constant (b, typeof(int?)));
			Assert.AreEqual (ExpressionType.Add, expr.NodeType, "Add#05");
			Assert.AreEqual (typeof (int?), expr.Type, "Add#06");
			Assert.IsNull (expr.Method, "Add#07");
			Assert.AreEqual ("(1 + 2)", expr.ToString(), "Add#08");
		}

		[Test]
		public void UserDefinedClass ()
		{
			// We can use the simplest version of GetMethod because we already know only one
			// exists in the very simple class we're using for the tests.
			MethodInfo mi = typeof (OpClass).GetMethod ("op_Addition");

			OpClass left = new OpClass ();
			BinaryExpression expr = Expression.Add (Expression.Constant (left), Expression.Constant (new OpClass ()));
			Assert.AreEqual (ExpressionType.Add, expr.NodeType, "Add#09");
			Assert.AreEqual (typeof (OpClass), expr.Type, "Add#10");
			Assert.AreEqual (mi, expr.Method, "Add#11");
			Assert.AreEqual ("op_Addition", expr.Method.Name, "Add#12");
			Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) + value(MonoTests.System.Linq.Expressions.OpClass))",
				expr.ToString(), "Add#13");

			Expression.Lambda<Func<OpClass>> (expr);

#if false

	//
	// We do not have support for objects that are not really
	// constants, like this case.   Need to figure out what to do
	// with those
	//

			Func<OpClass> compiled = l.Compile ();
			Assert.AreEqual (left, compiled  ());
#endif
		}

		public class S {
			public static int MyAdder (int a, int b){
				return 1000;
			}
		}

		[Test]
		public void TestMethodAddition ()
		{
			BinaryExpression expr = Expression.Add (Expression.Constant (1), Expression.Constant (2), typeof(S).GetMethod("MyAdder"));
			Expression<Func<int>> l = Expression.Lambda<Func<int>> (expr);

			Func<int> compiled = l.Compile ();
			Assert.AreEqual (1000, compiled ());
		}

		[Test]
		public void CompileAdd ()
		{
			var left = Expression.Parameter (typeof (int), "l");
			var right = Expression.Parameter (typeof (int), "r");
			var l = Expression.Lambda<Func<int, int, int>> (
				Expression.Add (left, right), left, right);

			var be = l.Body as BinaryExpression;
			Assert.IsNotNull (be);
			Assert.AreEqual (typeof (int), be.Type);
			Assert.IsFalse (be.IsLifted);
			Assert.IsFalse (be.IsLiftedToNull);

			var add = l.Compile ();

			Assert.AreEqual (12, add (6, 6));
			Assert.AreEqual (0, add (-1, 1));
			Assert.AreEqual (-2, add (1, -3));
		}

		[Test]
		public void AddLifted ()
		{
			var b = Expression.Add (
				Expression.Constant (null, typeof (int?)),
				Expression.Constant (null, typeof (int?)));

			Assert.AreEqual (typeof (int?), b.Type);
			Assert.IsTrue (b.IsLifted);
			Assert.IsTrue (b.IsLiftedToNull);
		}

		[Test]
		public void AddNotLifted ()
		{
			var b = Expression.Add (
				Expression.Constant (1, typeof (int)),
				Expression.Constant (1, typeof (int)));

			Assert.AreEqual (typeof (int), b.Type);
			Assert.IsFalse (b.IsLifted);
			Assert.IsFalse (b.IsLiftedToNull);
		}

		[Test]
		[Category ("NotWorkingInterpreter")]
		public void AddTestNullable ()
		{
			var a = Expression.Parameter (typeof (int?), "a");
			var b = Expression.Parameter (typeof (int?), "b");
			var l = Expression.Lambda<Func<int?, int?, int?>> (
				Expression.Add (a, b), a, b);

			var be = l.Body as BinaryExpression;
			Assert.IsNotNull (be);
			Assert.AreEqual (typeof (int?), be.Type);
			Assert.IsTrue (be.IsLifted);
			Assert.IsTrue (be.IsLiftedToNull);

			var c = l.Compile ();

			Assert.AreEqual (null, c (1, null), "a1");
			Assert.AreEqual (null, c (null, null), "a2");
			Assert.AreEqual (null, c (null, 2), "a3");
			Assert.AreEqual (3,    c (1, 2), "a4");
		}

		struct Slot {
			public int Value;

			public Slot (int value)
			{
				this.Value = value;
			}

			public static Slot operator + (Slot a, Slot b)
			{
				return new Slot (a.Value + b.Value);
			}
		}

		[Test]
		public void UserDefinedAdd ()
		{
			var l = Expression.Parameter (typeof (Slot), "l");
			var r = Expression.Parameter (typeof (Slot), "r");

			var node = Expression.Add (l, r);

			Assert.IsFalse (node.IsLifted);
			Assert.IsFalse (node.IsLiftedToNull);
			Assert.AreEqual (typeof (Slot), node.Type);

			var add = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();

			Assert.AreEqual (new Slot (42), add (new Slot (21), new Slot (21)));
			Assert.AreEqual (new Slot (0), add (new Slot (1), new Slot (-1)));
		}

		[Test]
		[Category ("NotWorkingInterpreter")]
		public void UserDefinedAddLifted ()
		{
			var l = Expression.Parameter (typeof (Slot?), "l");
			var r = Expression.Parameter (typeof (Slot?), "r");

			var node = Expression.Add (l, r);

			Assert.IsTrue (node.IsLifted);
			Assert.IsTrue (node.IsLiftedToNull);
			Assert.AreEqual (typeof (Slot?), node.Type);

			var add = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();

			Assert.AreEqual (null, add (null, null));
			Assert.AreEqual ((Slot?) new Slot (42), add ((Slot?) new Slot (21), (Slot?) new Slot (21)));
		}

		struct SlotToNullable {
			public int Value;

			public SlotToNullable (int value)
			{
				this.Value = value;
			}

			public static SlotToNullable? operator + (SlotToNullable a, SlotToNullable b)
			{
				return new SlotToNullable (a.Value + b.Value);
			}
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void UserDefinedToNullableAddFromNullable ()
		{
			Expression.Add (
				Expression.Parameter (typeof (SlotToNullable?), "l"),
				Expression.Parameter (typeof (SlotToNullable?), "r"));
		}

		[Test]
		public void UserDefinedToNullableAdd ()
		{
			var l = Expression.Parameter (typeof (SlotToNullable), "l");
			var r = Expression.Parameter (typeof (SlotToNullable), "r");

			var node = Expression.Add (l, r);

			Assert.IsFalse (node.IsLifted);
			Assert.IsFalse (node.IsLiftedToNull);
			Assert.AreEqual (typeof (SlotToNullable?), node.Type);
			Assert.IsNotNull (node.Method);

			var add = Expression.Lambda<Func<SlotToNullable, SlotToNullable, SlotToNullable?>> (node, l, r).Compile ();

			Assert.AreEqual ((SlotToNullable?) new SlotToNullable (4), add (new SlotToNullable (2), new SlotToNullable (2)));
			Assert.AreEqual ((SlotToNullable?) new SlotToNullable (0), add (new SlotToNullable (2), new SlotToNullable (-2)));
		}

		/*struct SlotFromNullableToNullable {
			public int Value;

			public SlotFromNullableToNullable (int value)
			{
				this.Value = value;
			}

			public static SlotFromNullableToNullable? operator + (SlotFromNullableToNullable? a, SlotFromNullableToNullable? b)
			{
				if (a.HasValue && b.HasValue)
					return (SlotFromNullableToNullable?) new SlotFromNullableToNullable (
						a.Value.Value + b.Value.Value);
				else
					return null;
			}
		}

		[Test]
		public void UserDefinedFromNullableToNullableAdd ()
		{
			var l = Expression.Parameter (typeof (SlotFromNullableToNullable?), "l");
			var r = Expression.Parameter (typeof (SlotFromNullableToNullable?), "r");

			var node = Expression.Add (l, r);

			Assert.IsFalse (node.IsLifted);
			Assert.IsFalse (node.IsLiftedToNull);
			Assert.AreEqual (typeof (SlotFromNullableToNullable?), node.Type);
			Assert.IsNotNull (node.Method);

			var add = Expression.Lambda<Func<SlotFromNullableToNullable?, SlotFromNullableToNullable?, SlotFromNullableToNullable?>> (node, l, r).Compile ();

			Assert.AreEqual ((SlotFromNullableToNullable?) null, add (null, null));
			Assert.AreEqual ((SlotFromNullableToNullable?) null, add (new SlotFromNullableToNullable (2), null));
			Assert.AreEqual ((SlotFromNullableToNullable?) null, add (null, new SlotFromNullableToNullable (2)));
			Assert.AreEqual ((SlotFromNullableToNullable?) new SlotFromNullableToNullable (4), add (new SlotFromNullableToNullable (2), new SlotFromNullableToNullable (2)));
			Assert.AreEqual ((SlotFromNullableToNullable?) new SlotFromNullableToNullable (0), add (new SlotFromNullableToNullable (2), new SlotFromNullableToNullable (-2)));
		}*/

		[Test]
		public void AddStrings ()
		{
			var l = Expression.Parameter (typeof (string), "l");
			var r = Expression.Parameter (typeof (string), "r");

			var meth = typeof (string).GetMethod ("Concat", new [] { typeof (object), typeof (object) });

			var node = Expression.Add (l, r, meth);
			Assert.IsFalse (node.IsLifted);
			Assert.IsFalse (node.IsLiftedToNull);
			Assert.AreEqual (typeof (string), node.Type);
			Assert.AreEqual (meth, node.Method);

			var concat = Expression.Lambda<Func<string, string, string>> (node, l, r).Compile ();

			Assert.AreEqual (string.Empty, concat (null, null));
			Assert.AreEqual ("foobar", concat ("foo", "bar"));
		}

		[Test]
		public void AddDecimals ()
		{
			var l = Expression.Parameter (typeof (decimal), "l");
			var r = Expression.Parameter (typeof (decimal), "r");

			var meth = typeof (decimal).GetMethod ("op_Addition", new [] { typeof (decimal), typeof (decimal) });

			var node = Expression.Add (l, r);
			Assert.IsFalse (node.IsLifted);
			Assert.IsFalse (node.IsLiftedToNull);
			Assert.AreEqual (typeof (decimal), node.Type);
			Assert.AreEqual (meth, node.Method);

			var add = Expression.Lambda<Func<decimal, decimal, decimal>> (node, l, r).Compile ();

			Assert.AreEqual (2m, add (1m, 1m));
		}

		[Test]
		[Category ("NotWorkingInterpreter")]
		public void AddLiftedDecimals ()
		{
			var l = Expression.Parameter (typeof (decimal?), "l");
			var r = Expression.Parameter (typeof (decimal?), "r");

			var meth = typeof (decimal).GetMethod ("op_Addition", new [] { typeof (decimal), typeof (decimal) });

			var node = Expression.Add (l, r);
			Assert.IsTrue (node.IsLifted);
			Assert.IsTrue (node.IsLiftedToNull);
			Assert.AreEqual (typeof (decimal?), node.Type);
			Assert.AreEqual (meth, node.Method);

			var add = Expression.Lambda<Func<decimal?, decimal?, decimal?>> (node, l, r).Compile ();

			Assert.AreEqual (2m, add (1m, 1m));
			Assert.AreEqual (null, add (1m, null));
			Assert.AreEqual (null, add (null, null));
		}
	}
}