ExpressionTest_And.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. using System;
  22. using System.Reflection;
  23. using System.Linq;
  24. using System.Linq.Expressions;
  25. using NUnit.Framework;
  26. namespace MonoTests.System.Linq.Expressions
  27. {
  28. [TestFixture]
  29. public class ExpressionTest_And
  30. {
  31. [Test]
  32. [ExpectedException (typeof (ArgumentNullException))]
  33. public void Arg1Null ()
  34. {
  35. Expression.And (null, Expression.Constant (1));
  36. }
  37. [Test]
  38. [ExpectedException (typeof (ArgumentNullException))]
  39. public void Arg2Null ()
  40. {
  41. Expression.And (Expression.Constant (1), null);
  42. }
  43. [Test]
  44. [ExpectedException (typeof (InvalidOperationException))]
  45. public void NoOperatorClass ()
  46. {
  47. Expression.And (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
  48. }
  49. [Test]
  50. [ExpectedException (typeof (InvalidOperationException))]
  51. public void ArgTypesDifferent ()
  52. {
  53. Expression.And (Expression.Constant (1), Expression.Constant (true));
  54. }
  55. [Test]
  56. [ExpectedException (typeof (InvalidOperationException))]
  57. public void Double ()
  58. {
  59. Expression.And (Expression.Constant (1.0), Expression.Constant (2.0));
  60. }
  61. [Test]
  62. public void Integer ()
  63. {
  64. BinaryExpression expr = Expression.And (Expression.Constant (1), Expression.Constant (2));
  65. Assert.AreEqual (ExpressionType.And, expr.NodeType, "And#01");
  66. Assert.AreEqual (typeof (int), expr.Type, "And#02");
  67. Assert.IsNull (expr.Method, "And#03");
  68. Assert.AreEqual ("(1 & 2)", expr.ToString(), "And#04");
  69. }
  70. [Test]
  71. public void Boolean ()
  72. {
  73. BinaryExpression expr = Expression.And (Expression.Constant (true), Expression.Constant (false));
  74. Assert.AreEqual (ExpressionType.And, expr.NodeType, "And#05");
  75. Assert.AreEqual (typeof (bool), expr.Type, "And#06");
  76. Assert.IsNull (expr.Method, "And#07");
  77. Assert.AreEqual ("(True And False)", expr.ToString(), "And#08");
  78. }
  79. [Test]
  80. public void UserDefinedClass ()
  81. {
  82. // We can use the simplest version of GetMethod because we already know only one
  83. // exists in the very simple class we're using for the tests.
  84. MethodInfo mi = typeof (OpClass).GetMethod ("op_BitwiseAnd");
  85. BinaryExpression expr = Expression.And (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
  86. Assert.AreEqual (ExpressionType.And, expr.NodeType, "And#09");
  87. Assert.AreEqual (typeof (OpClass), expr.Type, "And#10");
  88. Assert.AreEqual (mi, expr.Method, "And#11");
  89. Assert.AreEqual ("op_BitwiseAnd", expr.Method.Name, "And#12");
  90. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) & value(MonoTests.System.Linq.Expressions.OpClass))",
  91. expr.ToString(), "And#13");
  92. }
  93. [Test]
  94. public void AndTest ()
  95. {
  96. ParameterExpression a = Expression.Parameter (typeof (bool), "a"), b = Expression.Parameter (typeof (bool), "b");
  97. var l = Expression.Lambda<Func<bool, bool, bool>> (
  98. Expression.And (a, b), a, b);
  99. var be = l.Body as BinaryExpression;
  100. Assert.IsNotNull (be);
  101. Assert.AreEqual (typeof (bool), be.Type);
  102. Assert.IsFalse (be.IsLifted);
  103. Assert.IsFalse (be.IsLiftedToNull);
  104. var c = l.Compile ();
  105. Assert.AreEqual (true, c (true, true), "t1");
  106. Assert.AreEqual (false, c (true, false), "t2");
  107. Assert.AreEqual (false, c (false, true), "t3");
  108. Assert.AreEqual (false, c (false, false), "t4");
  109. }
  110. [Test]
  111. public void AndLifted ()
  112. {
  113. var b = Expression.And (
  114. Expression.Constant (null, typeof (bool?)),
  115. Expression.Constant (null, typeof (bool?)));
  116. Assert.AreEqual (typeof (bool?), b.Type);
  117. Assert.IsTrue (b.IsLifted);
  118. Assert.IsTrue (b.IsLiftedToNull);
  119. }
  120. [Test]
  121. public void AndNullableTest ()
  122. {
  123. ParameterExpression a = Expression.Parameter (typeof (bool?), "a"), b = Expression.Parameter (typeof (bool?), "b");
  124. var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
  125. Expression.And (a, b), a, b);
  126. var be = l.Body as BinaryExpression;
  127. Assert.IsNotNull (be);
  128. Assert.AreEqual (typeof (bool?), be.Type);
  129. Assert.IsTrue (be.IsLifted);
  130. Assert.IsTrue (be.IsLiftedToNull);
  131. var c = l.Compile ();
  132. Assert.AreEqual (true, c (true, true), "a1");
  133. Assert.AreEqual (false, c (true, false), "a2");
  134. Assert.AreEqual (false, c (false, true), "a3");
  135. Assert.AreEqual (false, c (false, false), "a4");
  136. Assert.AreEqual (null, c (true, null), "a5");
  137. Assert.AreEqual (false, c (false, null), "a6");
  138. Assert.AreEqual (false, c (null, false), "a7");
  139. Assert.AreEqual (null, c (true, null), "a8");
  140. Assert.AreEqual (null, c (null, null), "a9");
  141. }
  142. }
  143. }