ExpressionTest_LessThan.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // ExpressionTest_LessThan.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. //
  7. // (C) 2008 Novell, Inc. (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Reflection;
  30. using System.Linq;
  31. using System.Linq.Expressions;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Linq.Expressions
  34. {
  35. [TestFixture]
  36. [Category("SRE")]
  37. public class ExpressionTest_LessThan
  38. {
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void Arg1Null ()
  42. {
  43. Expression.LessThan (null, Expression.Constant (1));
  44. }
  45. [Test]
  46. [ExpectedException (typeof (ArgumentNullException))]
  47. public void Arg2Null ()
  48. {
  49. Expression.LessThan (Expression.Constant (1), null);
  50. }
  51. [Test]
  52. [ExpectedException (typeof (InvalidOperationException))]
  53. public void NoOperatorClass ()
  54. {
  55. Expression.LessThan (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
  56. }
  57. [Test]
  58. public void Double ()
  59. {
  60. var expr = Expression.LessThan (Expression.Constant (2.0), Expression.Constant (1.0));
  61. Assert.AreEqual (ExpressionType.LessThan, expr.NodeType);
  62. Assert.AreEqual (typeof (bool), expr.Type);
  63. Assert.IsNull (expr.Method);
  64. Assert.AreEqual ("(2 < 1)", expr.ToString ());
  65. }
  66. [Test]
  67. public void Integer ()
  68. {
  69. var expr = Expression.LessThan (Expression.Constant (2), Expression.Constant (1));
  70. Assert.AreEqual (ExpressionType.LessThan, expr.NodeType);
  71. Assert.AreEqual (typeof (bool), expr.Type);
  72. Assert.IsNull (expr.Method);
  73. Assert.AreEqual ("(2 < 1)", expr.ToString ());
  74. }
  75. [Test]
  76. [ExpectedException (typeof (InvalidOperationException))]
  77. public void MismatchedTypes ()
  78. {
  79. Expression.LessThan (Expression.Constant (new OpClass ()), Expression.Constant (true));
  80. }
  81. [Test]
  82. [ExpectedException (typeof (InvalidOperationException))]
  83. public void Boolean ()
  84. {
  85. Expression.LessThan (Expression.Constant (true), Expression.Constant (false));
  86. }
  87. [Test]
  88. public void UserDefinedClass ()
  89. {
  90. MethodInfo mi = typeof (OpClass).GetMethod ("op_LessThan");
  91. Assert.IsNotNull (mi);
  92. BinaryExpression expr = Expression.LessThan (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
  93. Assert.AreEqual (ExpressionType.LessThan, expr.NodeType);
  94. Assert.AreEqual (typeof (bool), expr.Type);
  95. Assert.AreEqual (mi, expr.Method);
  96. Assert.AreEqual ("op_LessThan", expr.Method.Name);
  97. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) < value(MonoTests.System.Linq.Expressions.OpClass))", expr.ToString ());
  98. }
  99. [Test]
  100. public void NullableInt32LessThan ()
  101. {
  102. var l = Expression.Parameter (typeof (int?), "l");
  103. var r = Expression.Parameter (typeof (int?), "r");
  104. var lt = Expression.Lambda<Func<int?, int?, bool>> (
  105. Expression.LessThan (l, r), l, r).Compile ();
  106. Assert.IsFalse (lt (null, null));
  107. Assert.IsFalse (lt (null, 1));
  108. Assert.IsFalse (lt (null, -1));
  109. Assert.IsFalse (lt (1, null));
  110. Assert.IsFalse (lt (-1, null));
  111. Assert.IsTrue (lt (1, 2));
  112. Assert.IsFalse (lt (2, 1));
  113. Assert.IsFalse (lt (1, 1));
  114. }
  115. [Test]
  116. public void NullableInt32LessThanLiftedToNull ()
  117. {
  118. var l = Expression.Parameter (typeof (int?), "l");
  119. var r = Expression.Parameter (typeof (int?), "r");
  120. var lt = Expression.Lambda<Func<int?, int?, bool?>> (
  121. Expression.LessThan (l, r, true, null), l, r).Compile ();
  122. Assert.AreEqual ((bool?) null, lt (null, null));
  123. Assert.AreEqual ((bool?) null, lt (null, 1));
  124. Assert.AreEqual ((bool?) null, lt (null, -1));
  125. Assert.AreEqual ((bool?) null, lt (1, null));
  126. Assert.AreEqual ((bool?) null, lt (-1, null));
  127. Assert.AreEqual ((bool?) true, lt (1, 2));
  128. Assert.AreEqual ((bool?) false, lt (2, 1));
  129. Assert.AreEqual ((bool?) false, lt (1, 1));
  130. }
  131. struct Slot {
  132. public int Value;
  133. public Slot (int val)
  134. {
  135. Value = val;
  136. }
  137. public static bool operator > (Slot a, Slot b)
  138. {
  139. return a.Value > b.Value;
  140. }
  141. public static bool operator < (Slot a, Slot b)
  142. {
  143. return a.Value < b.Value;
  144. }
  145. }
  146. [Test]
  147. public void UserDefinedLessThanLifted ()
  148. {
  149. var l = Expression.Parameter (typeof (Slot?), "l");
  150. var r = Expression.Parameter (typeof (Slot?), "r");
  151. var node = Expression.LessThan (l, r);
  152. Assert.IsTrue (node.IsLifted);
  153. Assert.IsFalse (node.IsLiftedToNull);
  154. Assert.AreEqual (typeof (bool), node.Type);
  155. Assert.IsNotNull (node.Method);
  156. var lte = Expression.Lambda<Func<Slot?, Slot?, bool>> (node, l, r).Compile ();
  157. Assert.AreEqual (false, lte (new Slot (1), new Slot (0)));
  158. Assert.AreEqual (true, lte (new Slot (-1), new Slot (1)));
  159. Assert.AreEqual (false, lte (new Slot (1), new Slot (1)));
  160. Assert.AreEqual (false, lte (null, new Slot (1)));
  161. Assert.AreEqual (false, lte (new Slot (1), null));
  162. Assert.AreEqual (false, lte (null, null));
  163. }
  164. [Test]
  165. public void UserDefinedLessThanLiftedToNull ()
  166. {
  167. var l = Expression.Parameter (typeof (Slot?), "l");
  168. var r = Expression.Parameter (typeof (Slot?), "r");
  169. var node = Expression.LessThan (l, r, true, null);
  170. Assert.IsTrue (node.IsLifted);
  171. Assert.IsTrue (node.IsLiftedToNull);
  172. Assert.AreEqual (typeof (bool?), node.Type);
  173. Assert.IsNotNull (node.Method);
  174. var lte = Expression.Lambda<Func<Slot?, Slot?, bool?>> (node, l, r).Compile ();
  175. Assert.AreEqual (false, lte (new Slot (1), new Slot (0)));
  176. Assert.AreEqual (true, lte (new Slot (-1), new Slot (1)));
  177. Assert.AreEqual (false, lte (new Slot (1), new Slot (1)));
  178. Assert.AreEqual (null, lte (null, new Slot (1)));
  179. Assert.AreEqual (null, lte (new Slot (1), null));
  180. Assert.AreEqual (null, lte (null, null));
  181. }
  182. }
  183. }