ExpressionTest_AndAlso.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 <[email protected]>
  21. // Jb Evain <[email protected]>
  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. #if !NET_4_0
  76. Assert.AreEqual ("(True && False)", expr.ToString(), "AndAlso#04");
  77. #endif
  78. }
  79. [Test]
  80. public void UserDefinedClass ()
  81. {
  82. // We can use the simplest version of GetMethod because we already know only one
  83. // exists in the very simple class we're using for the tests.
  84. MethodInfo mi = typeof (OpClass).GetMethod ("op_BitwiseAnd");
  85. BinaryExpression expr = Expression.AndAlso (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
  86. Assert.AreEqual (ExpressionType.AndAlso, expr.NodeType, "AndAlso#05");
  87. Assert.AreEqual (typeof (OpClass), expr.Type, "AndAlso#06");
  88. Assert.AreEqual (mi, expr.Method, "AndAlso#07");
  89. Assert.AreEqual ("op_BitwiseAnd", expr.Method.Name, "AndAlso#08");
  90. #if !NET_4_0
  91. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) && value(MonoTests.System.Linq.Expressions.OpClass))",
  92. expr.ToString(), "AndAlso#09");
  93. #endif
  94. }
  95. [Test]
  96. public void AndAlsoTest ()
  97. {
  98. var a = Expression.Parameter (typeof (bool), "a");
  99. var b = Expression.Parameter (typeof (bool), "b");
  100. var l = Expression.Lambda<Func<bool, bool, bool>> (
  101. Expression.AndAlso (a, b), a, b);
  102. var be = l.Body as BinaryExpression;
  103. Assert.IsNotNull (be);
  104. Assert.AreEqual (typeof (bool), be.Type);
  105. Assert.IsFalse (be.IsLifted);
  106. Assert.IsFalse (be.IsLiftedToNull);
  107. var c = l.Compile ();
  108. Assert.AreEqual (true, c (true, true), "a1");
  109. Assert.AreEqual (false, c (true, false), "a2");
  110. Assert.AreEqual (false, c (false, true), "a3");
  111. Assert.AreEqual (false, c (false, false), "a4");
  112. }
  113. [Test]
  114. public void AndAlsoLifted ()
  115. {
  116. var b = Expression.AndAlso (
  117. Expression.Constant (null, typeof (bool?)),
  118. Expression.Constant (null, typeof (bool?)));
  119. Assert.AreEqual (typeof (bool?), b.Type);
  120. Assert.IsTrue (b.IsLifted);
  121. Assert.IsTrue (b.IsLiftedToNull);
  122. }
  123. [Test]
  124. public void AndAlsoNotLifted ()
  125. {
  126. var b = Expression.AndAlso (
  127. Expression.Constant (true, typeof (bool)),
  128. Expression.Constant (true, typeof (bool)));
  129. Assert.AreEqual (typeof (bool), b.Type);
  130. Assert.IsFalse (b.IsLifted);
  131. Assert.IsFalse (b.IsLiftedToNull);
  132. }
  133. [Test]
  134. [Category ("NotWorkingInterpreter")]
  135. public void AndAlsoTestNullable ()
  136. {
  137. var a = Expression.Parameter (typeof (bool?), "a");
  138. var b = Expression.Parameter (typeof (bool?), "b");
  139. var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
  140. Expression.AndAlso (a, b), a, b);
  141. var be = l.Body as BinaryExpression;
  142. Assert.IsNotNull (be);
  143. Assert.AreEqual (typeof (bool?), be.Type);
  144. Assert.IsTrue (be.IsLifted);
  145. Assert.IsTrue (be.IsLiftedToNull);
  146. var c = l.Compile ();
  147. Assert.AreEqual (true, c (true, true), "a1");
  148. Assert.AreEqual (false, c (true, false), "a2");
  149. Assert.AreEqual (false, c (false, true), "a3");
  150. Assert.AreEqual (false, c (false, false), "a4");
  151. Assert.AreEqual (null, c (true, null), "a5");
  152. Assert.AreEqual (false, c (false, null), "a6");
  153. Assert.AreEqual (false, c (null, false), "a7");
  154. Assert.AreEqual (null, c (true, null), "a8");
  155. Assert.AreEqual (null, c (null, null), "a9");
  156. }
  157. [Test]
  158. public void AndAlsoBoolItem ()
  159. {
  160. var i = Expression.Parameter (typeof (Item<bool>), "i");
  161. var and = Expression.Lambda<Func<Item<bool>, bool>> (
  162. Expression.AndAlso (
  163. Expression.Property (i, "Left"),
  164. Expression.Property (i, "Right")), i).Compile ();
  165. var item = new Item<bool> (false, true);
  166. Assert.AreEqual (false, and (item));
  167. Assert.IsTrue (item.LeftCalled);
  168. Assert.IsFalse (item.RightCalled);
  169. }
  170. [Test]
  171. [Category ("NotWorkingInterpreter")]
  172. public void AndAlsoNullableBoolItem ()
  173. {
  174. var i = Expression.Parameter (typeof (Item<bool?>), "i");
  175. var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
  176. Expression.AndAlso (
  177. Expression.Property (i, "Left"),
  178. Expression.Property (i, "Right")), i).Compile ();
  179. var item = new Item<bool?> (false, true);
  180. Assert.AreEqual ((bool?) false, and (item));
  181. Assert.IsTrue (item.LeftCalled);
  182. Assert.IsFalse (item.RightCalled);
  183. }
  184. struct Slot {
  185. public int Value;
  186. public Slot (int val)
  187. {
  188. this.Value = val;
  189. }
  190. public static Slot operator & (Slot a, Slot b)
  191. {
  192. return new Slot (a.Value & b.Value);
  193. }
  194. public static bool operator true (Slot a)
  195. {
  196. return a.Value != 0;
  197. }
  198. public static bool operator false (Slot a)
  199. {
  200. return a.Value == 0;
  201. }
  202. public override string ToString ()
  203. {
  204. return Value.ToString ();
  205. }
  206. }
  207. [Test]
  208. [Category ("NotWorkingInterpreter")]
  209. public void UserDefinedAndAlso ()
  210. {
  211. var l = Expression.Parameter (typeof (Slot), "l");
  212. var r = Expression.Parameter (typeof (Slot), "r");
  213. var method = typeof (Slot).GetMethod ("op_BitwiseAnd");
  214. var node = Expression.AndAlso (l, r, method);
  215. Assert.IsFalse (node.IsLifted);
  216. Assert.IsFalse (node.IsLiftedToNull);
  217. Assert.AreEqual (method, node.Method);
  218. var andalso = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
  219. Assert.AreEqual (new Slot (64), andalso (new Slot (64), new Slot (64)));
  220. Assert.AreEqual (new Slot (0), andalso (new Slot (32), new Slot (64)));
  221. Assert.AreEqual (new Slot (0), andalso (new Slot (64), new Slot (32)));
  222. }
  223. [Test]
  224. [Category ("NotWorkingInterpreter")]
  225. public void UserDefinedAndAlsoShortCircuit ()
  226. {
  227. var i = Expression.Parameter (typeof (Item<Slot>), "i");
  228. var and = Expression.Lambda<Func<Item<Slot>, Slot>> (
  229. Expression.AndAlso (
  230. Expression.Property (i, "Left"),
  231. Expression.Property (i, "Right")), i).Compile ();
  232. var item = new Item<Slot> (new Slot (0), new Slot (1));
  233. Assert.AreEqual (new Slot (0), and (item));
  234. Assert.IsTrue (item.LeftCalled);
  235. Assert.IsFalse (item.RightCalled);
  236. }
  237. [Test]
  238. [Category ("NotWorkingInterpreter")]
  239. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
  240. public void UserDefinedLiftedAndAlsoShortCircuit ()
  241. {
  242. var i = Expression.Parameter (typeof (Item<Slot?>), "i");
  243. var and = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
  244. Expression.AndAlso (
  245. Expression.Property (i, "Left"),
  246. Expression.Property (i, "Right")), i).Compile ();
  247. var item = new Item<Slot?> (null, new Slot (1));
  248. Assert.AreEqual ((Slot?) null, and (item));
  249. Assert.IsTrue (item.LeftCalled);
  250. Assert.IsFalse (item.RightCalled);
  251. }
  252. [Test]
  253. [Category ("NotWorkingInterpreter")]
  254. public void UserDefinedAndAlsoLiftedToNull ()
  255. {
  256. var l = Expression.Parameter (typeof (Slot?), "l");
  257. var r = Expression.Parameter (typeof (Slot?), "r");
  258. var method = typeof (Slot).GetMethod ("op_BitwiseAnd");
  259. var node = Expression.AndAlso (l, r, method);
  260. Assert.IsTrue (node.IsLifted);
  261. Assert.IsTrue (node.IsLiftedToNull);
  262. Assert.AreEqual (method, node.Method);
  263. var andalso = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
  264. Assert.AreEqual (new Slot (64), andalso (new Slot (64), new Slot (64)));
  265. Assert.AreEqual (new Slot (0), andalso (new Slot (32), new Slot (64)));
  266. Assert.AreEqual (new Slot (0), andalso (new Slot (64), new Slot (32)));
  267. Assert.AreEqual (null, andalso (null, new Slot (32)));
  268. Assert.AreEqual (null, andalso (new Slot (64), null));
  269. Assert.AreEqual (null, andalso (null, null));
  270. }
  271. struct Incomplete {
  272. public int Value;
  273. public Incomplete (int val)
  274. {
  275. Value = val;
  276. }
  277. public static Incomplete operator & (Incomplete a, Incomplete b)
  278. {
  279. return new Incomplete (a.Value & b.Value);
  280. }
  281. }
  282. [Test]
  283. [ExpectedException (typeof (ArgumentException))]
  284. public void IncompleteUserDefinedAndAlso ()
  285. {
  286. var l = Expression.Parameter (typeof (Incomplete), "l");
  287. var r = Expression.Parameter (typeof (Incomplete), "r");
  288. var method = typeof (Incomplete).GetMethod ("op_BitwiseAnd");
  289. Expression.AndAlso (l, r, method);
  290. }
  291. class A {
  292. public static bool operator true (A x)
  293. {
  294. return true;
  295. }
  296. public static bool operator false (A x)
  297. {
  298. return false;
  299. }
  300. }
  301. class B : A {
  302. public static B operator & (B x, B y)
  303. {
  304. return new B ();
  305. }
  306. public static bool op_True<T> (B x)
  307. {
  308. return true;
  309. }
  310. public static bool op_False (B x)
  311. {
  312. return false;
  313. }
  314. }
  315. [Test] // from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350487
  316. [Category ("NotWorkingInterpreter")]
  317. public void Connect350487 ()
  318. {
  319. var p = Expression.Parameter (typeof (B), "b");
  320. var l = Expression.Lambda<Func<B, A>> (
  321. Expression.AndAlso (p, p), p).Compile ();
  322. Assert.IsNotNull (l (null));
  323. }
  324. }
  325. }