ExpressionTest_And.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_And
  32. {
  33. [Test]
  34. [ExpectedException (typeof (ArgumentNullException))]
  35. public void Arg1Null ()
  36. {
  37. Expression.And (null, Expression.Constant (1));
  38. }
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void Arg2Null ()
  42. {
  43. Expression.And (Expression.Constant (1), null);
  44. }
  45. [Test]
  46. [ExpectedException (typeof (InvalidOperationException))]
  47. public void NoOperatorClass ()
  48. {
  49. Expression.And (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
  50. }
  51. [Test]
  52. [ExpectedException (typeof (InvalidOperationException))]
  53. public void ArgTypesDifferent ()
  54. {
  55. Expression.And (Expression.Constant (1), Expression.Constant (true));
  56. }
  57. [Test]
  58. [ExpectedException (typeof (InvalidOperationException))]
  59. public void Double ()
  60. {
  61. Expression.And (Expression.Constant (1.0), Expression.Constant (2.0));
  62. }
  63. [Test]
  64. public void Integer ()
  65. {
  66. BinaryExpression expr = Expression.And (Expression.Constant (1), Expression.Constant (2));
  67. Assert.AreEqual (ExpressionType.And, expr.NodeType, "And#01");
  68. Assert.AreEqual (typeof (int), expr.Type, "And#02");
  69. Assert.IsNull (expr.Method, "And#03");
  70. Assert.AreEqual ("(1 & 2)", expr.ToString(), "And#04");
  71. }
  72. [Test]
  73. public void Boolean ()
  74. {
  75. BinaryExpression expr = Expression.And (Expression.Constant (true), Expression.Constant (false));
  76. Assert.AreEqual (ExpressionType.And, expr.NodeType, "And#05");
  77. Assert.AreEqual (typeof (bool), expr.Type, "And#06");
  78. Assert.IsNull (expr.Method, "And#07");
  79. Assert.AreEqual ("(True And False)", expr.ToString(), "And#08");
  80. }
  81. [Test]
  82. public void UserDefinedClass ()
  83. {
  84. // We can use the simplest version of GetMethod because we already know only one
  85. // exists in the very simple class we're using for the tests.
  86. MethodInfo mi = typeof (OpClass).GetMethod ("op_BitwiseAnd");
  87. BinaryExpression expr = Expression.And (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
  88. Assert.AreEqual (ExpressionType.And, expr.NodeType, "And#09");
  89. Assert.AreEqual (typeof (OpClass), expr.Type, "And#10");
  90. Assert.AreEqual (mi, expr.Method, "And#11");
  91. Assert.AreEqual ("op_BitwiseAnd", expr.Method.Name, "And#12");
  92. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) & value(MonoTests.System.Linq.Expressions.OpClass))",
  93. expr.ToString(), "And#13");
  94. }
  95. [Test]
  96. public void AndBoolTest ()
  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.And (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), "t1");
  109. Assert.AreEqual (false, c (true, false), "t2");
  110. Assert.AreEqual (false, c (false, true), "t3");
  111. Assert.AreEqual (false, c (false, false), "t4");
  112. }
  113. [Test]
  114. public void AndLifted ()
  115. {
  116. var b = Expression.And (
  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 AndBoolNullableTest ()
  125. {
  126. var a = Expression.Parameter (typeof (bool?), "a");
  127. var b = Expression.Parameter (typeof (bool?), "b");
  128. var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
  129. Expression.And (a, b), a, b);
  130. var be = l.Body as BinaryExpression;
  131. Assert.IsNotNull (be);
  132. Assert.AreEqual (typeof (bool?), be.Type);
  133. Assert.IsTrue (be.IsLifted);
  134. Assert.IsTrue (be.IsLiftedToNull);
  135. var c = l.Compile ();
  136. Assert.AreEqual (true, c (true, true), "a1");
  137. Assert.AreEqual (false, c (true, false), "a2");
  138. Assert.AreEqual (false, c (false, true), "a3");
  139. Assert.AreEqual (false, c (false, false), "a4");
  140. Assert.AreEqual (null, c (true, null), "a5");
  141. Assert.AreEqual (false, c (false, null), "a6");
  142. Assert.AreEqual (false, c (null, false), "a7");
  143. Assert.AreEqual (null, c (true, null), "a8");
  144. Assert.AreEqual (null, c (null, null), "a9");
  145. }
  146. [Test]
  147. public void AndBoolItem ()
  148. {
  149. var i = Expression.Parameter (typeof (Item<bool>), "i");
  150. var and = Expression.Lambda<Func<Item<bool>, bool>> (
  151. Expression.And (
  152. Expression.Property (i, "Left"),
  153. Expression.Property (i, "Right")), i).Compile ();
  154. var item = new Item<bool> (false, true);
  155. Assert.AreEqual (false, and (item));
  156. Assert.IsTrue (item.LeftCalled);
  157. Assert.IsTrue (item.RightCalled);
  158. }
  159. [Test]
  160. public void AndNullableBoolItem ()
  161. {
  162. var i = Expression.Parameter (typeof (Item<bool?>), "i");
  163. var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
  164. Expression.And (
  165. Expression.Property (i, "Left"),
  166. Expression.Property (i, "Right")), i).Compile ();
  167. var item = new Item<bool?> (false, true);
  168. Assert.AreEqual ((bool?) false, and (item));
  169. Assert.IsTrue (item.LeftCalled);
  170. Assert.IsTrue (item.RightCalled);
  171. }
  172. [Test]
  173. public void AndIntTest ()
  174. {
  175. var a = Expression.Parameter (typeof (int), "a");
  176. var b = Expression.Parameter (typeof (int), "b");
  177. var and = Expression.Lambda<Func<int, int, int>> (
  178. Expression.And (a, b), a, b).Compile ();
  179. Assert.AreEqual (0, and (0, 0), "t1");
  180. Assert.AreEqual (0, and (0, 1), "t2");
  181. Assert.AreEqual (0, and (1, 0), "t3");
  182. Assert.AreEqual (1, and (1, 1), "t4");
  183. }
  184. [Test]
  185. public void AndIntNullableTest ()
  186. {
  187. var a = Expression.Parameter (typeof (int?), "a");
  188. var b = Expression.Parameter (typeof (int?), "b");
  189. var c = Expression.Lambda<Func<int?, int?, int?>> (
  190. Expression.And (a, b), a, b).Compile ();
  191. Assert.AreEqual ((int?) 1, c (1, 1), "a1");
  192. Assert.AreEqual ((int?) 0, c (1, 0), "a2");
  193. Assert.AreEqual ((int?) 0, c (0, 1), "a3");
  194. Assert.AreEqual ((int?) 0, c (0, 0), "a4");
  195. Assert.AreEqual ((int?) null, c (1, null), "a5");
  196. Assert.AreEqual ((int?) null, c (0, null), "a6");
  197. Assert.AreEqual ((int?) null, c (null, 0), "a7");
  198. Assert.AreEqual ((int?) null, c (1, null), "a8");
  199. Assert.AreEqual ((int?) null, c (null, null), "a9");
  200. }
  201. }
  202. }