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

Language C# Lines 360
MD5 Hash ab9c6ea6f7e471e0abb11c008a425eb5 Estimated Cost $7,072 (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
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
// 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_OrElse
	{
		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void Arg1Null ()
		{
			Expression.OrElse (null, Expression.Constant (1));
		}

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

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

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void Double ()
		{
			Expression.OrElse (Expression.Constant (1.0), Expression.Constant (2.0));
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void Integer ()
		{
			Expression.OrElse (Expression.Constant (1), Expression.Constant (2));
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void MismatchedTypes ()
		{
			Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (true));
		}

		[Test]
		public void Boolean ()
		{
			BinaryExpression expr = Expression.OrElse (Expression.Constant (true), Expression.Constant (false));
			Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#01");
			Assert.AreEqual (typeof (bool), expr.Type, "OrElse#02");
			Assert.IsNull (expr.Method, "OrElse#03");
			Assert.AreEqual ("(True || False)", expr.ToString(), "OrElse#04");
		}

		[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_BitwiseOr");

			BinaryExpression expr = Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
			Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#05");
			Assert.AreEqual (typeof (OpClass), expr.Type, "OrElse#06");
			Assert.AreEqual (mi, expr.Method, "OrElse#07");
			Assert.AreEqual ("op_BitwiseOr", expr.Method.Name, "OrElse#08");
			Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) || value(MonoTests.System.Linq.Expressions.OpClass))",
				expr.ToString(), "OrElse#09");
		}

		public class BrokenMethod {
			public static int operator | (BrokenMethod a, BrokenMethod b)
			{
				return 1;
			}
		}

		public class BrokenMethod2 {
			public static BrokenMethod2 operator | (BrokenMethod2 a, int b)
			{
				return null;
			}
		}

		[Test]
		[ExpectedException(typeof(ArgumentException))]
		public void MethodInfoReturnType ()
		{
			Expression.OrElse (Expression.Constant (new BrokenMethod ()),
					   Expression.Constant (new BrokenMethod ()));
		}

		[Test]
		[ExpectedException(typeof(ArgumentException))]
		public void MethodInfoReturnType2 ()
		{
			Expression.OrElse (Expression.Constant (new BrokenMethod2 ()),
					   Expression.Constant (1));
		}

		[Test]
		public void OrElseNotLifted ()
		{
			var b = Expression.OrElse (
				Expression.Constant (true, typeof (bool)),
				Expression.Constant (true, typeof (bool)));

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

		[Test]
		public void OrElseTest ()
		{
			var a = Expression.Parameter (typeof (bool), "a");
			var b = Expression.Parameter (typeof (bool), "b");
			var l = Expression.Lambda<Func<bool, bool, bool>> (
				Expression.OrElse (a, b), a, b);

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

			var c = l.Compile ();

			Assert.AreEqual (true,  c (true, true), "o1");
			Assert.AreEqual (true,  c (true, false), "o2");
			Assert.AreEqual (true,  c (false, true), "o3");
			Assert.AreEqual (false, c (false, false), "o4");
		}

		[Test]
		public void OrElseLifted ()
		{
			var b = Expression.OrElse (
				Expression.Constant (null, typeof (bool?)),
				Expression.Constant (null, typeof (bool?)));

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

		[Test]
		public void OrElseTestNullable ()
		{
			var a = Expression.Parameter (typeof (bool?), "a");
			var b = Expression.Parameter (typeof (bool?), "b");
			var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
				Expression.OrElse (a, b), a, b);

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

			var c = l.Compile ();

			Assert.AreEqual (true,  c (true, true),   "o1");
			Assert.AreEqual (true,  c (true, false),  "o2");
			Assert.AreEqual (true,  c (false, true),  "o3");
			Assert.AreEqual (false, c (false, false), "o4");

			Assert.AreEqual (true, c (true, null),  "o5");
			Assert.AreEqual (null, c (false, null), "o6");
			Assert.AreEqual (null, c (null, false), "o7");
			Assert.AreEqual (true, c (true, null),  "o8");
			Assert.AreEqual (null, c (null, null),  "o9");
		}

		[Test]
		public void OrElseBoolItem ()
		{
			var i = Expression.Parameter (typeof (Item<bool>), "i");
			var and = Expression.Lambda<Func<Item<bool>, bool>> (
				Expression.OrElse (
					Expression.Property (i, "Left"),
					Expression.Property (i, "Right")), i).Compile ();

			var item = new Item<bool> (true, false);
			Assert.AreEqual (true, and (item));
			Assert.IsTrue (item.LeftCalled);
			Assert.IsFalse (item.RightCalled);
		}

		[Test]
		public void OrElseNullableBoolItem ()
		{
			var i = Expression.Parameter (typeof (Item<bool?>), "i");
			var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
				Expression.OrElse (
					Expression.Property (i, "Left"),
					Expression.Property (i, "Right")), i).Compile ();

			var item = new Item<bool?> (true, false);
			Assert.AreEqual ((bool?) true, and (item));
			Assert.IsTrue (item.LeftCalled);
			Assert.IsFalse (item.RightCalled);
		}

		struct Slot {

			public int Value;

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

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

			public static bool operator true (Slot a)
			{
				return a.Value != 0;
			}

			public static bool operator false (Slot a)
			{
				return a.Value == 0;
			}
		}

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

			var method = typeof (Slot).GetMethod ("op_BitwiseOr");

			var node = Expression.OrElse (l, r, method);
			Assert.IsFalse (node.IsLifted);
			Assert.IsFalse (node.IsLiftedToNull);
			Assert.AreEqual (method, node.Method);

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

			Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
			Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
		}

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

			var method = typeof (Slot).GetMethod ("op_BitwiseOr");

			var node = Expression.OrElse (l, r, method);
			Assert.IsTrue (node.IsLifted);
			Assert.IsTrue (node.IsLiftedToNull);
			Assert.AreEqual (method, node.Method);

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

			Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
			Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
			Assert.AreEqual (new Slot (64), orelse (null, new Slot (64)));
			Assert.AreEqual (new Slot (32), orelse (new Slot (32), null));
			Assert.AreEqual (null, orelse (null, null));
		}

		[Test]
		public void UserDefinedOrElseShortCircuit ()
		{
			var i = Expression.Parameter (typeof (Item<Slot>), "i");
			var orelse = Expression.Lambda<Func<Item<Slot>, Slot>> (
				Expression.OrElse (
					Expression.Property (i, "Left"),
					Expression.Property (i, "Right")), i).Compile ();

			var item = new Item<Slot> (new Slot (1), new Slot (0));
			Assert.AreEqual (new Slot (1), orelse (item));
			Assert.IsTrue (item.LeftCalled);
			Assert.IsFalse (item.RightCalled);
		}

		[Test]
		[Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
		public void UserDefinedLiftedOrElseShortCircuit ()
		{
			var i = Expression.Parameter (typeof (Item<Slot?>), "i");
			var orelse = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
				Expression.OrElse (
					Expression.Property (i, "Left"),
					Expression.Property (i, "Right")), i).Compile ();

			var item = new Item<Slot?> (new Slot (1), null);
			Assert.AreEqual ((Slot?) new Slot (1), orelse (item));
			Assert.IsTrue (item.LeftCalled);
			Assert.IsFalse (item.RightCalled);
		}

		struct Incomplete {
			public int Value;

			public Incomplete (int val)
			{
				Value = val;
			}

			public static Incomplete operator | (Incomplete a, Incomplete b)
			{
				return new Incomplete (a.Value | b.Value);
			}
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void IncompleteUserDefinedOrElse ()
		{
			var l = Expression.Parameter (typeof (Incomplete), "l");
			var r = Expression.Parameter (typeof (Incomplete), "r");

			var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");

			Expression.OrElse (l, r, method);
		}
	}
}
Back to Top