mono-2.6.7-for-amiga /mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_Lambda.cs

Language C# Lines 286
MD5 Hash cb488cc459a76d15e324a27b8fbb3455 Estimated Cost $5,038 (why?)
Repository https://bitbucket.org/steenlund/mono-2.6.7-for-amiga View Raw File View Project SPDX
  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
// 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:
//   Miguel de Icaza (miguel@novell.com)
//   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_Lambda
	{
		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void NonDelegateTypeInCtor ()
		{
			// The first parameter must be a delegate type
			Expression.Lambda (typeof(string), Expression.Constant (1), new ParameterExpression [0]);
		}

		delegate object delegate_object_emtpy ();
		delegate object delegate_object_int (int a);
		delegate object delegate_object_string (string s);
		delegate object delegate_object_object (object s);

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void InvalidConversion ()
		{
			// float to object, invalid
			Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1.0), new ParameterExpression [0]);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void InvalidConversion2 ()
		{
			// float to object, invalid
			Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1), new ParameterExpression [0]);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void InvalidArgCount ()
		{
			// missing a parameter
			Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [0]);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void InvalidArgCount2 ()
		{
			// extra parameter
			ParameterExpression p = Expression.Parameter (typeof (int), "AAA");
			Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("foo"), new ParameterExpression [1] {p});
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void InvalidArgType ()
		{
			// invalid argument type
			ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
			Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [1] {p});
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void InvalidArgType2 ()
		{
			// invalid argument type

			ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
			Expression.Lambda (typeof (delegate_object_object), Expression.Constant ("foo"), new ParameterExpression [1] {p});
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void NullParameter ()
		{
			Expression.Lambda<Func<int, int>> (Expression.Constant (1), new ParameterExpression [] { null });
		}

		[Test]
		public void Assignability ()
		{
			// allowed: string to object
			Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("string"), new ParameterExpression [0]);

			// allowed delegate has string, delegate has base class (object)
			ParameterExpression p = Expression.Parameter (typeof (object), "ParObject");
			var l = Expression.Lambda (typeof (delegate_object_string), Expression.Constant (""), new ParameterExpression [1] {p});

			Assert.AreEqual ("ParObject => \"\"", l.ToString ());
		}

		[Test]
		[ExpectedException(typeof(InvalidOperationException))]
		public void ParameterOutOfScope ()
		{
			ParameterExpression a = Expression.Parameter(typeof (int), "a");
			ParameterExpression second_a = Expression.Parameter(typeof(int), "a");

			// Here we have the same name for the parameter expression, but
			// we pass a different object to the Lambda expression, so they are
			// different, this should throw
			Expression<Func<int,int>> l = Expression.Lambda<Func<int,int>>(a, new ParameterExpression [] { second_a });
			l.Compile ();
		}

		[Test]
		public void ParameterRefTest ()
		{
			ParameterExpression a = Expression.Parameter(typeof(int), "a");
			ParameterExpression b = Expression.Parameter(typeof(int), "b");

			Expression<Func<int,int,int>> l = Expression.Lambda<Func<int,int,int>>(
				Expression.Add (a, b), new ParameterExpression [] { a, b });

			Assert.AreEqual (typeof (Func<int, int, int>), l.Type);
			Assert.AreEqual ("(a, b) => (a + b)", l.ToString ());

			Func<int,int,int> xx = l.Compile ();
			int res = xx (10, 20);
			Assert.AreEqual (res, 30);
		}

		[Test]
		public void Compile ()
		{
			Expression<Func<int>> l = Expression.Lambda<Func<int>> (Expression.Constant (1), new ParameterExpression [0]);
			Assert.AreEqual (typeof (Func<int>), l.Type);
			Assert.AreEqual ("() => 1", l.ToString ());

			Func<int> fi = l.Compile ();
			fi ();
		}

		[Test]
		[ExpectedException(typeof(ArgumentException))]
		public void ReturnValueCheck ()
		{
			ParameterExpression p1 = Expression.Parameter(typeof(int?), "va");
			ParameterExpression p2 = Expression.Parameter(typeof(int?), "vb");
			Expression add = Expression.Add(p1, p2);


			// This should throw, since the add.Type is "int?" and the return
			// type we have here is int.
			Expression.Lambda<Func<int?,int?,int>> (add, p1, p2);
		}

		public static void Foo ()
		{
		}

		[Test]
		public void LambdaType ()
		{
			var l = Expression.Lambda (Expression.Constant (1), new [] { Expression.Parameter (typeof (int), "foo") });

			Assert.AreEqual (typeof (Func<int, int>), l.Type);

			l = Expression.Lambda (Expression.Call (null, GetType ().GetMethod ("Foo")), new [] { Expression.Parameter (typeof (string), "foofoo") });

			Assert.AreEqual (typeof (Action<string>), l.Type);
		}

		[Test]
		public void UnTypedLambdaReturnsExpressionOfDelegateType ()
		{
			var l = Expression.Lambda ("foo".ToConstant ());

			Assert.AreEqual (typeof (Expression<Func<string>>), l.GetType ());
		}

		public static int CallDelegate (Func<int, int> e)
		{
			return e (42);
		}

		[Test]
		public void LambdaPassedAsDelegate ()
		{
			var pi = Expression.Parameter (typeof (int), "i");
			var identity = Expression.Lambda<Func<int, int>> (pi, pi);

			var l = Expression.Lambda<Func<int>> (
				Expression.Call (
					GetType ().GetMethod ("CallDelegate"),
					identity)).Compile ();

			Assert.AreEqual (42, l ());
		}

		[Test]
		[Category ("NotWorking")]
		public void LambdaPassedAsDelegateUsingParentParameter ()
		{
			var a = Expression.Parameter (typeof (int), "a");
			var b = Expression.Parameter (typeof (int), "b");

			var l = Expression.Lambda<Func<int, int>> (
				Expression.Call (
					this.GetType ().GetMethod ("CallDelegate"),
					Expression.Lambda<Func<int, int>> (
							Expression.Multiply (a, b), b)),
				a).Compile ();

			Assert.AreEqual (84, l (2));
		}

		public static int CallFunc (Func<int, int> e, int i)
		{
			return e (i);
		}

		[Test]
		[Category ("NotWorking")]
		public void NestedParentParameterUse ()
		{
			var a = Expression.Parameter (typeof (int), null);
			var b = Expression.Parameter (typeof (int), null);
			var c = Expression.Parameter (typeof (int), null);
			var d = Expression.Parameter (typeof (int), null);

			var l = Expression.Lambda<Func<int, int>> (
				Expression.Call (
					this.GetType ().GetMethod ("CallFunc"),
					Expression.Lambda<Func<int, int>> (
						Expression.Call (
							this.GetType ().GetMethod ("CallFunc"),
							Expression.Lambda<Func<int, int>> (
								Expression.Call (
									this.GetType ().GetMethod ("CallFunc"),
									Expression.Lambda<Func<int, int>> (
										Expression.Add (c, d),
										d),
									Expression.Add (b, c)),
								c),
							Expression.Add (a, b)),
						b),
					a),
				a).Compile ();

			Assert.AreEqual (5, l (1));
		}

		[Test]
		public void LambdaReturningExpression ()
		{
			var l = Expression.Lambda<Func<Expression>> (Expression.Constant (42));
			Assert.AreEqual (ExpressionType.Quote, l.Body.NodeType);

			var quoter = l.Compile ();

			var q = quoter ();

			Assert.AreEqual (ExpressionType.Constant, q.NodeType);
		}
	}
}
Back to Top