ExpressionTest_AndAlso.cs 11 KB

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