ExpressionTest_Or.cs 7.1 KB

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