PageRenderTime 69ms CodeModel.GetById 26ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_AndAlso.cs

https://github.com/pruiz/mono
C# | 372 lines | 288 code | 61 blank | 23 comment | 2 complexity | 3e72fafed0cef35cdb3a4dbfa2bf22d2 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  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
 23using System;
 24using System.Reflection;
 25using System.Linq;
 26using System.Linq.Expressions;
 27using NUnit.Framework;
 28
 29namespace MonoTests.System.Linq.Expressions
 30{
 31	[TestFixture]
 32	public class ExpressionTest_AndAlso
 33	{
 34		[Test]
 35		[ExpectedException (typeof (ArgumentNullException))]
 36		public void Arg1Null ()
 37		{
 38			Expression.AndAlso (null, Expression.Constant (1));
 39		}
 40
 41		[Test]
 42		[ExpectedException (typeof (ArgumentNullException))]
 43		public void Arg2Null ()
 44		{
 45			Expression.AndAlso (Expression.Constant (1), null);
 46		}
 47
 48		[Test]
 49		[ExpectedException (typeof (InvalidOperationException))]
 50		public void NoOperatorClass ()
 51		{
 52			Expression.AndAlso (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
 53		}
 54
 55		[Test]
 56		[ExpectedException (typeof (InvalidOperationException))]
 57		public void Double ()
 58		{
 59			Expression.AndAlso (Expression.Constant (1.0), Expression.Constant (2.0));
 60		}
 61
 62		[Test]
 63		[ExpectedException (typeof (InvalidOperationException))]
 64		public void Integer ()
 65		{
 66			Expression.AndAlso (Expression.Constant (1), Expression.Constant (2));
 67		}
 68
 69		[Test]
 70		[ExpectedException (typeof (InvalidOperationException))]
 71		public void MismatchedTypes ()
 72		{
 73			Expression.AndAlso (Expression.Constant (new OpClass ()), Expression.Constant (true));
 74		}
 75
 76		[Test]
 77		public void Boolean ()
 78		{
 79			BinaryExpression expr = Expression.AndAlso (Expression.Constant (true), Expression.Constant (false));
 80			Assert.AreEqual (ExpressionType.AndAlso, expr.NodeType, "AndAlso#01");
 81			Assert.AreEqual (typeof (bool), expr.Type, "AndAlso#02");
 82			Assert.IsNull (expr.Method, "AndAlso#03");
 83		}
 84
 85		[Test]
 86		public void UserDefinedClass ()
 87		{
 88			// We can use the simplest version of GetMethod because we already know only one
 89			// exists in the very simple class we're using for the tests.
 90			MethodInfo mi = typeof (OpClass).GetMethod ("op_BitwiseAnd");
 91
 92			BinaryExpression expr = Expression.AndAlso (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
 93			Assert.AreEqual (ExpressionType.AndAlso, expr.NodeType, "AndAlso#05");
 94			Assert.AreEqual (typeof (OpClass), expr.Type, "AndAlso#06");
 95			Assert.AreEqual (mi, expr.Method, "AndAlso#07");
 96			Assert.AreEqual ("op_BitwiseAnd", expr.Method.Name, "AndAlso#08");
 97		}
 98
 99		[Test]
100		public void AndAlsoTest ()
101		{
102			var a = Expression.Parameter (typeof (bool), "a");
103			var b = Expression.Parameter (typeof (bool), "b");
104			var l = Expression.Lambda<Func<bool, bool, bool>> (
105				Expression.AndAlso (a, b), a, b);
106
107			var be = l.Body as BinaryExpression;
108			Assert.IsNotNull (be);
109			Assert.AreEqual (typeof (bool), be.Type);
110			Assert.IsFalse (be.IsLifted);
111			Assert.IsFalse (be.IsLiftedToNull);
112
113			var c = l.Compile ();
114
115			Assert.AreEqual (true,  c (true, true), "a1");
116			Assert.AreEqual (false, c (true, false), "a2");
117			Assert.AreEqual (false, c (false, true), "a3");
118			Assert.AreEqual (false, c (false, false), "a4");
119		}
120
121		[Test]
122		public void AndAlsoLifted ()
123		{
124			var b = Expression.AndAlso (
125				Expression.Constant (null, typeof (bool?)),
126				Expression.Constant (null, typeof (bool?)));
127
128			Assert.AreEqual (typeof (bool?), b.Type);
129			Assert.IsTrue (b.IsLifted);
130			Assert.IsTrue (b.IsLiftedToNull);
131		}
132
133		[Test]
134		public void AndAlsoNotLifted ()
135		{
136			var b = Expression.AndAlso (
137				Expression.Constant (true, typeof (bool)),
138				Expression.Constant (true, typeof (bool)));
139
140			Assert.AreEqual (typeof (bool), b.Type);
141			Assert.IsFalse (b.IsLifted);
142			Assert.IsFalse (b.IsLiftedToNull);
143		}
144
145		[Test]
146		public void AndAlsoTestNullable ()
147		{
148			var a = Expression.Parameter (typeof (bool?), "a");
149			var b = Expression.Parameter (typeof (bool?), "b");
150			var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
151				Expression.AndAlso (a, b), a, b);
152
153			var be = l.Body as BinaryExpression;
154			Assert.IsNotNull (be);
155			Assert.AreEqual (typeof (bool?), be.Type);
156			Assert.IsTrue (be.IsLifted);
157			Assert.IsTrue (be.IsLiftedToNull);
158
159			var c = l.Compile ();
160
161			Assert.AreEqual (true,  c (true, true), "a1");
162			Assert.AreEqual (false, c (true, false), "a2");
163			Assert.AreEqual (false, c (false, true), "a3");
164			Assert.AreEqual (false, c (false, false), "a4");
165
166			Assert.AreEqual (null,  c (true, null), "a5");
167			Assert.AreEqual (false, c (false, null), "a6");
168			Assert.AreEqual (false, c (null, false), "a7");
169			Assert.AreEqual (null,  c (true, null), "a8");
170			Assert.AreEqual (null,  c (null, null), "a9");
171		}
172
173		[Test]
174		public void AndAlsoBoolItem ()
175		{
176			var i = Expression.Parameter (typeof (Item<bool>), "i");
177			var and = Expression.Lambda<Func<Item<bool>, bool>> (
178				Expression.AndAlso (
179					Expression.Property (i, "Left"),
180					Expression.Property (i, "Right")), i).Compile ();
181
182			var item = new Item<bool> (false, true);
183			Assert.AreEqual (false, and (item));
184			Assert.IsTrue (item.LeftCalled);
185			Assert.IsFalse (item.RightCalled);
186		}
187
188		[Test]
189		public void AndAlsoNullableBoolItem ()
190		{
191			var i = Expression.Parameter (typeof (Item<bool?>), "i");
192			var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
193				Expression.AndAlso (
194					Expression.Property (i, "Left"),
195					Expression.Property (i, "Right")), i).Compile ();
196
197			var item = new Item<bool?> (false, true);
198			Assert.AreEqual ((bool?) false, and (item));
199			Assert.IsTrue (item.LeftCalled);
200			Assert.IsFalse (item.RightCalled);
201		}
202
203		struct Slot {
204
205			public int Value;
206
207			public Slot (int val)
208			{
209				this.Value = val;
210			}
211
212			public static Slot operator & (Slot a, Slot b)
213			{
214				return new Slot (a.Value & b.Value);
215			}
216
217			public static bool operator true (Slot a)
218			{
219				return a.Value != 0;
220			}
221
222			public static bool operator false (Slot a)
223			{
224				return a.Value == 0;
225			}
226
227			public override string ToString ()
228			{
229				return Value.ToString ();
230			}
231		}
232
233		[Test]
234		public void UserDefinedAndAlso ()
235		{
236			var l = Expression.Parameter (typeof (Slot), "l");
237			var r = Expression.Parameter (typeof (Slot), "r");
238
239			var method = typeof (Slot).GetMethod ("op_BitwiseAnd");
240
241			var node = Expression.AndAlso (l, r, method);
242			Assert.IsFalse (node.IsLifted);
243			Assert.IsFalse (node.IsLiftedToNull);
244			Assert.AreEqual (method, node.Method);
245
246			var andalso = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
247
248			Assert.AreEqual (new Slot (64), andalso (new Slot (64), new Slot (64)));
249			Assert.AreEqual (new Slot (0), andalso (new Slot (32), new Slot (64)));
250			Assert.AreEqual (new Slot (0), andalso (new Slot (64), new Slot (32)));
251		}
252
253		[Test]
254		public void UserDefinedAndAlsoShortCircuit ()
255		{
256			var i = Expression.Parameter (typeof (Item<Slot>), "i");
257			var and = Expression.Lambda<Func<Item<Slot>, Slot>> (
258				Expression.AndAlso (
259					Expression.Property (i, "Left"),
260					Expression.Property (i, "Right")), i).Compile ();
261
262			var item = new Item<Slot> (new Slot (0), new Slot (1));
263			Assert.AreEqual (new Slot (0), and (item));
264			Assert.IsTrue (item.LeftCalled);
265			Assert.IsFalse (item.RightCalled);
266		}
267
268		[Test]
269		[Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
270		public void UserDefinedLiftedAndAlsoShortCircuit ()
271		{
272			var i = Expression.Parameter (typeof (Item<Slot?>), "i");
273			var and = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
274				Expression.AndAlso (
275					Expression.Property (i, "Left"),
276					Expression.Property (i, "Right")), i).Compile ();
277
278			var item = new Item<Slot?> (null, new Slot (1));
279			Assert.AreEqual ((Slot?) null, and (item));
280			Assert.IsTrue (item.LeftCalled);
281			Assert.IsFalse (item.RightCalled);
282		}
283
284		[Test]
285		public void UserDefinedAndAlsoLiftedToNull ()
286		{
287			var l = Expression.Parameter (typeof (Slot?), "l");
288			var r = Expression.Parameter (typeof (Slot?), "r");
289
290			var method = typeof (Slot).GetMethod ("op_BitwiseAnd");
291
292			var node = Expression.AndAlso (l, r, method);
293			Assert.IsTrue (node.IsLifted);
294			Assert.IsTrue (node.IsLiftedToNull);
295			Assert.AreEqual (method, node.Method);
296
297			var andalso = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
298
299			Assert.AreEqual (new Slot (64), andalso (new Slot (64), new Slot (64)));
300			Assert.AreEqual (new Slot (0), andalso (new Slot (32), new Slot (64)));
301			Assert.AreEqual (new Slot (0), andalso (new Slot (64), new Slot (32)));
302			Assert.AreEqual (null, andalso (null, new Slot (32)));
303			Assert.AreEqual (null, andalso (new Slot (64), null));
304			Assert.AreEqual (null, andalso (null, null));
305		}
306
307		struct Incomplete {
308			public int Value;
309
310			public Incomplete (int val)
311			{
312				Value = val;
313			}
314
315			public static Incomplete operator & (Incomplete a, Incomplete b)
316			{
317				return new Incomplete (a.Value & b.Value);
318			}
319		}
320
321		[Test]
322		[ExpectedException (typeof (ArgumentException))]
323		public void IncompleteUserDefinedAndAlso ()
324		{
325			var l = Expression.Parameter (typeof (Incomplete), "l");
326			var r = Expression.Parameter (typeof (Incomplete), "r");
327
328			var method = typeof (Incomplete).GetMethod ("op_BitwiseAnd");
329
330			Expression.AndAlso (l, r, method);
331		}
332
333		class A {
334			public static bool operator true (A x)
335			{
336				return true;
337			}
338
339			public static bool operator false (A x)
340			{
341				return false;
342			}
343		}
344
345		class B : A {
346			public static B operator & (B x, B y)
347			{
348				return new B ();
349			}
350
351			public static bool op_True<T> (B x)
352			{
353				return true;
354			}
355
356			public static bool op_False (B x)
357			{
358				return false;
359			}
360		}
361
362		[Test] // from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350487
363		public void Connect350487 ()
364		{
365			var p = Expression.Parameter (typeof (B), "b");
366			var l = Expression.Lambda<Func<B, A>> (
367				Expression.AndAlso (p, p), p).Compile ();
368
369			Assert.IsNotNull (l (null));
370		}
371	}
372}