PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 375 lines | 291 code | 61 blank | 23 comment | 2 complexity | ff02383ff7e30d93091d54db7457eaa6 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  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. Assert.AreEqual ("(True && False)", expr.ToString(), "AndAlso#04");
  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. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) && value(MonoTests.System.Linq.Expressions.OpClass))",
  89. expr.ToString(), "AndAlso#09");
  90. }
  91. [Test]
  92. public void AndAlsoTest ()
  93. {
  94. var a = Expression.Parameter (typeof (bool), "a");
  95. var b = Expression.Parameter (typeof (bool), "b");
  96. var l = Expression.Lambda<Func<bool, bool, bool>> (
  97. Expression.AndAlso (a, b), a, b);
  98. var be = l.Body as BinaryExpression;
  99. Assert.IsNotNull (be);
  100. Assert.AreEqual (typeof (bool), be.Type);
  101. Assert.IsFalse (be.IsLifted);
  102. Assert.IsFalse (be.IsLiftedToNull);
  103. var c = l.Compile ();
  104. Assert.AreEqual (true, c (true, true), "a1");
  105. Assert.AreEqual (false, c (true, false), "a2");
  106. Assert.AreEqual (false, c (false, true), "a3");
  107. Assert.AreEqual (false, c (false, false), "a4");
  108. }
  109. [Test]
  110. public void AndAlsoLifted ()
  111. {
  112. var b = Expression.AndAlso (
  113. Expression.Constant (null, typeof (bool?)),
  114. Expression.Constant (null, typeof (bool?)));
  115. Assert.AreEqual (typeof (bool?), b.Type);
  116. Assert.IsTrue (b.IsLifted);
  117. Assert.IsTrue (b.IsLiftedToNull);
  118. }
  119. [Test]
  120. public void AndAlsoNotLifted ()
  121. {
  122. var b = Expression.AndAlso (
  123. Expression.Constant (true, typeof (bool)),
  124. Expression.Constant (true, typeof (bool)));
  125. Assert.AreEqual (typeof (bool), b.Type);
  126. Assert.IsFalse (b.IsLifted);
  127. Assert.IsFalse (b.IsLiftedToNull);
  128. }
  129. [Test]
  130. public void AndAlsoTestNullable ()
  131. {
  132. var a = Expression.Parameter (typeof (bool?), "a");
  133. var b = Expression.Parameter (typeof (bool?), "b");
  134. var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
  135. Expression.AndAlso (a, b), a, b);
  136. var be = l.Body as BinaryExpression;
  137. Assert.IsNotNull (be);
  138. Assert.AreEqual (typeof (bool?), be.Type);
  139. Assert.IsTrue (be.IsLifted);
  140. Assert.IsTrue (be.IsLiftedToNull);
  141. var c = l.Compile ();
  142. Assert.AreEqual (true, c (true, true), "a1");
  143. Assert.AreEqual (false, c (true, false), "a2");
  144. Assert.AreEqual (false, c (false, true), "a3");
  145. Assert.AreEqual (false, c (false, false), "a4");
  146. Assert.AreEqual (null, c (true, null), "a5");
  147. Assert.AreEqual (false, c (false, null), "a6");
  148. Assert.AreEqual (false, c (null, false), "a7");
  149. Assert.AreEqual (null, c (true, null), "a8");
  150. Assert.AreEqual (null, c (null, null), "a9");
  151. }
  152. [Test]
  153. public void AndAlsoBoolItem ()
  154. {
  155. var i = Expression.Parameter (typeof (Item<bool>), "i");
  156. var and = Expression.Lambda<Func<Item<bool>, bool>> (
  157. Expression.AndAlso (
  158. Expression.Property (i, "Left"),
  159. Expression.Property (i, "Right")), i).Compile ();
  160. var item = new Item<bool> (false, true);
  161. Assert.AreEqual (false, and (item));
  162. Assert.IsTrue (item.LeftCalled);
  163. Assert.IsFalse (item.RightCalled);
  164. }
  165. [Test]
  166. public void AndAlsoNullableBoolItem ()
  167. {
  168. var i = Expression.Parameter (typeof (Item<bool?>), "i");
  169. var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
  170. Expression.AndAlso (
  171. Expression.Property (i, "Left"),
  172. Expression.Property (i, "Right")), i).Compile ();
  173. var item = new Item<bool?> (false, true);
  174. Assert.AreEqual ((bool?) false, and (item));
  175. Assert.IsTrue (item.LeftCalled);
  176. Assert.IsFalse (item.RightCalled);
  177. }
  178. struct Slot {
  179. public int Value;
  180. public Slot (int val)
  181. {
  182. this.Value = val;
  183. }
  184. public static Slot operator & (Slot a, Slot b)
  185. {
  186. return new Slot (a.Value & b.Value);
  187. }
  188. public static bool operator true (Slot a)
  189. {
  190. return a.Value != 0;
  191. }
  192. public static bool operator false (Slot a)
  193. {
  194. return a.Value == 0;
  195. }
  196. public override string ToString ()
  197. {
  198. return Value.ToString ();
  199. }
  200. }
  201. [Test]
  202. public void UserDefinedAndAlso ()
  203. {
  204. var l = Expression.Parameter (typeof (Slot), "l");
  205. var r = Expression.Parameter (typeof (Slot), "r");
  206. var method = typeof (Slot).GetMethod ("op_BitwiseAnd");
  207. var node = Expression.AndAlso (l, r, method);
  208. Assert.IsFalse (node.IsLifted);
  209. Assert.IsFalse (node.IsLiftedToNull);
  210. Assert.AreEqual (method, node.Method);
  211. var andalso = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
  212. Assert.AreEqual (new Slot (64), andalso (new Slot (64), new Slot (64)));
  213. Assert.AreEqual (new Slot (0), andalso (new Slot (32), new Slot (64)));
  214. Assert.AreEqual (new Slot (0), andalso (new Slot (64), new Slot (32)));
  215. }
  216. [Test]
  217. public void UserDefinedAndAlsoShortCircuit ()
  218. {
  219. var i = Expression.Parameter (typeof (Item<Slot>), "i");
  220. var and = Expression.Lambda<Func<Item<Slot>, Slot>> (
  221. Expression.AndAlso (
  222. Expression.Property (i, "Left"),
  223. Expression.Property (i, "Right")), i).Compile ();
  224. var item = new Item<Slot> (new Slot (0), new Slot (1));
  225. Assert.AreEqual (new Slot (0), and (item));
  226. Assert.IsTrue (item.LeftCalled);
  227. Assert.IsFalse (item.RightCalled);
  228. }
  229. [Test]
  230. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
  231. public void UserDefinedLiftedAndAlsoShortCircuit ()
  232. {
  233. var i = Expression.Parameter (typeof (Item<Slot?>), "i");
  234. var and = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
  235. Expression.AndAlso (
  236. Expression.Property (i, "Left"),
  237. Expression.Property (i, "Right")), i).Compile ();
  238. var item = new Item<Slot?> (null, new Slot (1));
  239. Assert.AreEqual ((Slot?) null, and (item));
  240. Assert.IsTrue (item.LeftCalled);
  241. Assert.IsFalse (item.RightCalled);
  242. }
  243. [Test]
  244. public void UserDefinedAndAlsoLiftedToNull ()
  245. {
  246. var l = Expression.Parameter (typeof (Slot?), "l");
  247. var r = Expression.Parameter (typeof (Slot?), "r");
  248. var method = typeof (Slot).GetMethod ("op_BitwiseAnd");
  249. var node = Expression.AndAlso (l, r, method);
  250. Assert.IsTrue (node.IsLifted);
  251. Assert.IsTrue (node.IsLiftedToNull);
  252. Assert.AreEqual (method, node.Method);
  253. var andalso = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
  254. Assert.AreEqual (new Slot (64), andalso (new Slot (64), new Slot (64)));
  255. Assert.AreEqual (new Slot (0), andalso (new Slot (32), new Slot (64)));
  256. Assert.AreEqual (new Slot (0), andalso (new Slot (64), new Slot (32)));
  257. Assert.AreEqual (null, andalso (null, new Slot (32)));
  258. Assert.AreEqual (null, andalso (new Slot (64), null));
  259. Assert.AreEqual (null, andalso (null, null));
  260. }
  261. struct Incomplete {
  262. public int Value;
  263. public Incomplete (int val)
  264. {
  265. Value = val;
  266. }
  267. public static Incomplete operator & (Incomplete a, Incomplete b)
  268. {
  269. return new Incomplete (a.Value & b.Value);
  270. }
  271. }
  272. [Test]
  273. [ExpectedException (typeof (ArgumentException))]
  274. public void IncompleteUserDefinedAndAlso ()
  275. {
  276. var l = Expression.Parameter (typeof (Incomplete), "l");
  277. var r = Expression.Parameter (typeof (Incomplete), "r");
  278. var method = typeof (Incomplete).GetMethod ("op_BitwiseAnd");
  279. Expression.AndAlso (l, r, method);
  280. }
  281. class A {
  282. public static bool operator true (A x)
  283. {
  284. return true;
  285. }
  286. public static bool operator false (A x)
  287. {
  288. return false;
  289. }
  290. }
  291. class B : A {
  292. public static B operator & (B x, B y)
  293. {
  294. return new B ();
  295. }
  296. public static bool op_True<T> (B x)
  297. {
  298. return true;
  299. }
  300. public static bool op_False (B x)
  301. {
  302. return false;
  303. }
  304. }
  305. [Test] // from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350487
  306. public void Connect350487 ()
  307. {
  308. var p = Expression.Parameter (typeof (B), "b");
  309. var l = Expression.Lambda<Func<B, A>> (
  310. Expression.AndAlso (p, p), p).Compile ();
  311. Assert.IsNotNull (l (null));
  312. }
  313. }
  314. }