ExpressionTest_Or.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_Or
  32. {
  33. [Test]
  34. [ExpectedException (typeof (ArgumentNullException))]
  35. public void Arg1Null ()
  36. {
  37. Expression.Or (null, Expression.Constant (1));
  38. }
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void Arg2Null ()
  42. {
  43. Expression.Or (Expression.Constant (1), null);
  44. }
  45. [Test]
  46. [ExpectedException (typeof (InvalidOperationException))]
  47. public void NoOperatorClass ()
  48. {
  49. Expression.Or (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
  50. }
  51. [Test]
  52. [ExpectedException (typeof (InvalidOperationException))]
  53. public void ArgTypesDifferent ()
  54. {
  55. Expression.Or (Expression.Constant (1), Expression.Constant (true));
  56. }
  57. [Test]
  58. [ExpectedException (typeof (InvalidOperationException))]
  59. public void Double ()
  60. {
  61. Expression.Or (Expression.Constant (1.0), Expression.Constant (2.0));
  62. }
  63. [Test]
  64. public void Integer ()
  65. {
  66. BinaryExpression expr = Expression.Or (Expression.Constant (1), Expression.Constant (2));
  67. Assert.AreEqual (ExpressionType.Or, expr.NodeType, "Or#01");
  68. Assert.AreEqual (typeof (int), expr.Type, "Or#02");
  69. Assert.IsNull (expr.Method, "Or#03");
  70. Assert.AreEqual ("(1 | 2)", expr.ToString(), "Or#04");
  71. }
  72. [Test]
  73. public void Boolean ()
  74. {
  75. BinaryExpression expr = Expression.Or (Expression.Constant (true), Expression.Constant (false));
  76. Assert.AreEqual (ExpressionType.Or, expr.NodeType, "Or#05");
  77. Assert.AreEqual (typeof (bool), expr.Type, "Or#06");
  78. Assert.IsNull (expr.Method, "Or#07");
  79. Assert.AreEqual ("(True Or False)", expr.ToString(), "Or#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_BitwiseOr");
  87. BinaryExpression expr = Expression.Or (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
  88. Assert.AreEqual (ExpressionType.Or, expr.NodeType, "Or#09");
  89. Assert.AreEqual (typeof (OpClass), expr.Type, "Or#10");
  90. Assert.AreEqual (mi, expr.Method, "Or#11");
  91. Assert.AreEqual ("op_BitwiseOr", expr.Method.Name, "Or#12");
  92. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) | value(MonoTests.System.Linq.Expressions.OpClass))",
  93. expr.ToString(), "Or#13");
  94. }
  95. [Test]
  96. public void OrBoolTest ()
  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.Or (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), "o1");
  109. Assert.AreEqual (true, c (true, false), "o2");
  110. Assert.AreEqual (true, c (false, true), "o3");
  111. Assert.AreEqual (false, c (false, false), "o4");
  112. }
  113. [Test]
  114. public void OrBoolNullableTest ()
  115. {
  116. var a = Expression.Parameter (typeof (bool?), "a");
  117. var b = Expression.Parameter (typeof (bool?), "b");
  118. var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
  119. Expression.Or (a, b), a, b);
  120. var be = l.Body as BinaryExpression;
  121. Assert.IsNotNull (be);
  122. Assert.AreEqual (typeof (bool?), be.Type);
  123. Assert.IsTrue (be.IsLifted);
  124. Assert.IsTrue (be.IsLiftedToNull);
  125. var c = l.Compile ();
  126. Assert.AreEqual (true, c (true, true), "o1");
  127. Assert.AreEqual (true, c (true, false), "o2");
  128. Assert.AreEqual (true, c (false, true), "o3");
  129. Assert.AreEqual (false, c (false, false), "o4");
  130. Assert.AreEqual (true, c (true, null), "o5");
  131. Assert.AreEqual (null, c (false, null), "o6");
  132. Assert.AreEqual (null, c (null, false), "o7");
  133. Assert.AreEqual (true, c (true, null), "o8");
  134. Assert.AreEqual (null, c (null, null), "o9");
  135. }
  136. [Test]
  137. public void OrBoolItem ()
  138. {
  139. var i = Expression.Parameter (typeof (Item<bool>), "i");
  140. var and = Expression.Lambda<Func<Item<bool>, bool>> (
  141. Expression.Or (
  142. Expression.Property (i, "Left"),
  143. Expression.Property (i, "Right")), i).Compile ();
  144. var item = new Item<bool> (true, false);
  145. Assert.AreEqual (true, and (item));
  146. Assert.IsTrue (item.LeftCalled);
  147. Assert.IsTrue (item.RightCalled);
  148. }
  149. [Test]
  150. public void OrNullableBoolItem ()
  151. {
  152. var i = Expression.Parameter (typeof (Item<bool?>), "i");
  153. var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
  154. Expression.Or (
  155. Expression.Property (i, "Left"),
  156. Expression.Property (i, "Right")), i).Compile ();
  157. var item = new Item<bool?> (true, false);
  158. Assert.AreEqual ((bool?) true, and (item));
  159. Assert.IsTrue (item.LeftCalled);
  160. Assert.IsTrue (item.RightCalled);
  161. }
  162. [Test]
  163. public void OrIntTest ()
  164. {
  165. var a = Expression.Parameter (typeof (int), "a");
  166. var b = Expression.Parameter (typeof (int), "b");
  167. var or = Expression.Lambda<Func<int, int, int>> (
  168. Expression.Or (a, b), a, b).Compile ();
  169. Assert.AreEqual ((int?) 1, or (1, 1), "o1");
  170. Assert.AreEqual ((int?) 1, or (1, 0), "o2");
  171. Assert.AreEqual ((int?) 1, or (0, 1), "o3");
  172. Assert.AreEqual ((int?) 0, or (0, 0), "o4");
  173. }
  174. [Test]
  175. public void OrIntNullableTest ()
  176. {
  177. var a = Expression.Parameter (typeof (int?), "a");
  178. var b = Expression.Parameter (typeof (int?), "b");
  179. var c = Expression.Lambda<Func<int?, int?, int?>> (
  180. Expression.Or (a, b), a, b).Compile ();
  181. Assert.AreEqual ((int?) 1, c (1, 1), "o1");
  182. Assert.AreEqual ((int?) 1, c (1, 0), "o2");
  183. Assert.AreEqual ((int?) 1, c (0, 1), "o3");
  184. Assert.AreEqual ((int?) 0, c (0, 0), "o4");
  185. Assert.AreEqual ((int?) null, c (1, null), "o5");
  186. Assert.AreEqual ((int?) null, c (0, null), "o6");
  187. Assert.AreEqual ((int?) null, c (null, 0), "o7");
  188. Assert.AreEqual ((int?) null, c (1, null), "o8");
  189. Assert.AreEqual ((int?) null, c (null, null), "o9");
  190. }
  191. }
  192. }