ExpressionTest_NotEqual.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // Miguel de Icaza ([email protected])
  21. // Jb Evain ([email protected])
  22. //
  23. using System;
  24. using System.Reflection;
  25. using System.Linq;
  26. using System.Linq.Expressions;
  27. using NUnit.Framework;
  28. namespace MonoTests.System.Linq.Expressions
  29. {
  30. [TestFixture]
  31. public class ExpressionTest_NotEqual
  32. {
  33. [Test]
  34. [ExpectedException (typeof (ArgumentNullException))]
  35. public void Arg1Null ()
  36. {
  37. Expression.NotEqual (null, Expression.Constant (1));
  38. }
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void Arg2Null ()
  42. {
  43. Expression.NotEqual (Expression.Constant (1), null);
  44. }
  45. [Test]
  46. [ExpectedException (typeof (InvalidOperationException))]
  47. public void ArgTypesDifferent ()
  48. {
  49. Expression.NotEqual (Expression.Constant (1), Expression.Constant (2.0));
  50. }
  51. [Test]
  52. public void ReferenceCompare ()
  53. {
  54. Expression.NotEqual (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
  55. }
  56. public struct D {
  57. }
  58. [Test]
  59. [ExpectedException (typeof (InvalidOperationException))]
  60. public void NoOperatorClass ()
  61. {
  62. Expression.NotEqual (Expression.Constant (new D ()), Expression.Constant (new D ()));
  63. }
  64. [Test]
  65. public void Numeric ()
  66. {
  67. BinaryExpression expr = Expression.NotEqual (Expression.Constant (1), Expression.Constant (2));
  68. Assert.AreEqual (ExpressionType.NotEqual, expr.NodeType);
  69. Assert.AreEqual (typeof (bool), expr.Type);
  70. Assert.IsNull (expr.Method);
  71. Assert.AreEqual ("(1 != 2)", expr.ToString ());
  72. }
  73. [Test]
  74. public void Nullable_LiftToNull_SetToFalse ()
  75. {
  76. int? a = 1;
  77. int? b = 2;
  78. BinaryExpression expr = Expression.NotEqual (Expression.Constant (a, typeof(int?)),
  79. Expression.Constant (b, typeof(int?)),
  80. false, null);
  81. Assert.AreEqual (ExpressionType.NotEqual, expr.NodeType);
  82. Assert.AreEqual (typeof (bool), expr.Type);
  83. Assert.AreEqual (true, expr.IsLifted);
  84. Assert.AreEqual (false, expr.IsLiftedToNull);
  85. Assert.IsNull (expr.Method);
  86. Assert.AreEqual ("(1 != 2)", expr.ToString ());
  87. }
  88. [Test]
  89. public void Nullable_LiftToNull_SetToTrue ()
  90. {
  91. int? a = 1;
  92. int? b = 2;
  93. BinaryExpression expr = Expression.NotEqual (Expression.Constant (a, typeof(int?)),
  94. Expression.Constant (b, typeof(int?)),
  95. true, null);
  96. Assert.AreEqual (ExpressionType.NotEqual, expr.NodeType);
  97. Assert.AreEqual (typeof (bool?), expr.Type);
  98. Assert.AreEqual (true, expr.IsLifted);
  99. Assert.AreEqual (true, expr.IsLiftedToNull);
  100. Assert.IsNull (expr.Method);
  101. Assert.AreEqual ("(1 != 2)", expr.ToString ());
  102. }
  103. [Test]
  104. [ExpectedException(typeof (InvalidOperationException))]
  105. public void Nullable_Mixed ()
  106. {
  107. int? a = 1;
  108. int b = 2;
  109. Expression.NotEqual (Expression.Constant (a, typeof (int?)),
  110. Expression.Constant (b, typeof (int)));
  111. }
  112. [Test]
  113. public void UserDefinedClass ()
  114. {
  115. // We can use the simplest version of GetMethod because we already know only one
  116. // exists in the very simple class we're using for the tests.
  117. MethodInfo mi = typeof (OpClass).GetMethod ("op_Inequality");
  118. BinaryExpression expr = Expression.NotEqual (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
  119. Assert.AreEqual (ExpressionType.NotEqual, expr.NodeType);
  120. Assert.AreEqual (typeof (bool), expr.Type);
  121. Assert.AreEqual (mi, expr.Method);
  122. Assert.AreEqual ("op_Inequality", expr.Method.Name);
  123. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) != value(MonoTests.System.Linq.Expressions.OpClass))", expr.ToString ());
  124. }
  125. [Test]
  126. public void NullableInt32NotEqual ()
  127. {
  128. var l = Expression.Parameter (typeof (int?), "l");
  129. var r = Expression.Parameter (typeof (int?), "r");
  130. var neq = Expression.Lambda<Func<int?, int?, bool>> (
  131. Expression.NotEqual (l, r), l, r).Compile ();
  132. Assert.IsFalse (neq (null, null));
  133. Assert.IsTrue (neq (null, 1));
  134. Assert.IsTrue (neq (1, null));
  135. Assert.IsTrue (neq (1, 2));
  136. Assert.IsFalse (neq (1, 1));
  137. Assert.IsTrue (neq (null, 0));
  138. Assert.IsTrue (neq (0, null));
  139. }
  140. [Test]
  141. public void NullableInt32NotEqualLiftedToNull ()
  142. {
  143. var l = Expression.Parameter (typeof (int?), "l");
  144. var r = Expression.Parameter (typeof (int?), "r");
  145. var neq = Expression.Lambda<Func<int?, int?, bool?>> (
  146. Expression.NotEqual (l, r, true, null), l, r).Compile ();
  147. Assert.AreEqual ((bool?) null, neq (null, null));
  148. Assert.AreEqual ((bool?) null, neq (null, 1));
  149. Assert.AreEqual ((bool?) null, neq (1, null));
  150. Assert.AreEqual ((bool?) true, neq (1, 2));
  151. Assert.AreEqual ((bool?) false, neq (1, 1));
  152. Assert.AreEqual ((bool?) null, neq (null, 0));
  153. Assert.AreEqual ((bool?) null, neq (0, null));
  154. }
  155. public enum Foo {
  156. Bar,
  157. Baz,
  158. }
  159. [Test]
  160. public void EnumNotEqual ()
  161. {
  162. var l = Expression.Parameter (typeof (Foo), "l");
  163. var r = Expression.Parameter (typeof (Foo), "r");
  164. var node = Expression.NotEqual (l, r);
  165. Assert.IsFalse (node.IsLifted);
  166. Assert.IsFalse (node.IsLiftedToNull);
  167. Assert.AreEqual (typeof (bool), node.Type);
  168. Assert.IsNull (node.Method);
  169. var neq = Expression.Lambda<Func<Foo, Foo, bool>> (node, l, r).Compile ();
  170. Assert.AreEqual (false, neq (Foo.Bar, Foo.Bar));
  171. Assert.AreEqual (true, neq (Foo.Bar, Foo.Baz));
  172. }
  173. [Test]
  174. public void LiftedEnumNotEqual ()
  175. {
  176. var l = Expression.Parameter (typeof (Foo?), "l");
  177. var r = Expression.Parameter (typeof (Foo?), "r");
  178. var node = Expression.NotEqual (l, r);
  179. Assert.IsTrue (node.IsLifted);
  180. Assert.IsFalse (node.IsLiftedToNull);
  181. Assert.AreEqual (typeof (bool), node.Type);
  182. Assert.IsNull (node.Method);
  183. var neq = Expression.Lambda<Func<Foo?, Foo?, bool>> (node, l, r).Compile ();
  184. Assert.AreEqual (false, neq (Foo.Bar, Foo.Bar));
  185. Assert.AreEqual (true, neq (Foo.Bar, Foo.Baz));
  186. Assert.AreEqual (true, neq (Foo.Bar, null));
  187. Assert.AreEqual (false, neq (null, null));
  188. }
  189. }
  190. }