ExpressionTest_NotEqual.cs 7.6 KB

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