ExpressionTest_And.cs 7.4 KB

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