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