ExpressionTest_Add.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_Add
  30. {
  31. [Test]
  32. [ExpectedException (typeof (ArgumentNullException))]
  33. public void Arg1Null ()
  34. {
  35. Expression.Add (null, Expression.Constant (1));
  36. }
  37. [Test]
  38. [ExpectedException (typeof (ArgumentNullException))]
  39. public void Arg2Null ()
  40. {
  41. Expression.Add (Expression.Constant (1), null);
  42. }
  43. [Test]
  44. [ExpectedException (typeof (InvalidOperationException))]
  45. public void ArgTypesDifferent ()
  46. {
  47. Expression.Add (Expression.Constant (1), Expression.Constant (2.0));
  48. }
  49. [Test]
  50. [ExpectedException (typeof (InvalidOperationException))]
  51. public void NoOperatorClass ()
  52. {
  53. Expression.Add (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
  54. }
  55. [Test]
  56. [ExpectedException (typeof (InvalidOperationException))]
  57. public void Boolean ()
  58. {
  59. Expression.Add (Expression.Constant (true), Expression.Constant (false));
  60. }
  61. [Test]
  62. public void Numeric ()
  63. {
  64. BinaryExpression expr = Expression.Add (Expression.Constant (1), Expression.Constant (2));
  65. Assert.AreEqual (ExpressionType.Add, expr.NodeType, "Add#01");
  66. Assert.AreEqual (typeof (int), expr.Type, "Add#02");
  67. Assert.IsNull (expr.Method, "Add#03");
  68. Assert.AreEqual ("(1 + 2)", expr.ToString(), "Add#04");
  69. }
  70. [Test]
  71. public void Nullable ()
  72. {
  73. int? a = 1;
  74. int? b = 2;
  75. BinaryExpression expr = Expression.Add (Expression.Constant (a,typeof(int?)),
  76. Expression.Constant (b, typeof(int?)));
  77. Assert.AreEqual (ExpressionType.Add, expr.NodeType, "Add#05");
  78. Assert.AreEqual (typeof (int?), expr.Type, "Add#06");
  79. Assert.IsNull (expr.Method, "Add#07");
  80. Assert.AreEqual ("(1 + 2)", expr.ToString(), "Add#08");
  81. }
  82. [Test]
  83. public void UserDefinedClass ()
  84. {
  85. // We can use the simplest version of GetMethod because we already know only one
  86. // exists in the very simple class we're using for the tests.
  87. MethodInfo mi = typeof (OpClass).GetMethod ("op_Addition");
  88. OpClass left = new OpClass ();
  89. BinaryExpression expr = Expression.Add (Expression.Constant (left), Expression.Constant (new OpClass ()));
  90. Assert.AreEqual (ExpressionType.Add, expr.NodeType, "Add#09");
  91. Assert.AreEqual (typeof (OpClass), expr.Type, "Add#10");
  92. Assert.AreEqual (mi, expr.Method, "Add#11");
  93. Assert.AreEqual ("op_Addition", expr.Method.Name, "Add#12");
  94. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) + value(MonoTests.System.Linq.Expressions.OpClass))",
  95. expr.ToString(), "Add#13");
  96. Expression<Func<OpClass>> l = Expression.Lambda<Func<OpClass>> (expr);
  97. #if false
  98. //
  99. // We do not have support for objects that are not really
  100. // constants, like this case. Need to figure out what to do
  101. // with those
  102. //
  103. Func<OpClass> compiled = l.Compile ();
  104. Assert.AreEqual (left, compiled ());
  105. #endif
  106. }
  107. public class S {
  108. public static int MyAdder (int a, int b){
  109. return 1000;
  110. }
  111. }
  112. [Test]
  113. public void TestMethodAddition ()
  114. {
  115. BinaryExpression expr = Expression.Add (Expression.Constant (1), Expression.Constant (2), typeof(S).GetMethod("MyAdder"));
  116. Expression<Func<int>> l = Expression.Lambda<Func<int>> (expr);
  117. Func<int> compiled = l.Compile ();
  118. Assert.AreEqual (1000, compiled ());
  119. }
  120. [Test]
  121. public void CompileAdd ()
  122. {
  123. ParameterExpression left = Expression.Parameter (typeof (int), "l"), right = Expression.Parameter (typeof (int), "r");
  124. var l = Expression.Lambda<Func<int, int, int>> (
  125. Expression.Add (left, right), left, right);
  126. var be = l.Body as BinaryExpression;
  127. Assert.IsNotNull (be);
  128. Assert.AreEqual (typeof (int), be.Type);
  129. Assert.IsFalse (be.IsLifted);
  130. Assert.IsFalse (be.IsLiftedToNull);
  131. var add = l.Compile ();
  132. Assert.AreEqual (12, add (6, 6));
  133. Assert.AreEqual (0, add (-1, 1));
  134. Assert.AreEqual (-2, add (1, -3));
  135. }
  136. [Test]
  137. public void AddLifted ()
  138. {
  139. var b = Expression.Add (
  140. Expression.Constant (null, typeof (int?)),
  141. Expression.Constant (null, typeof (int?)));
  142. Assert.AreEqual (typeof (int?), b.Type);
  143. Assert.IsTrue (b.IsLifted);
  144. Assert.IsTrue (b.IsLiftedToNull);
  145. }
  146. [Test]
  147. public void AddNotLifted ()
  148. {
  149. var b = Expression.Add (
  150. Expression.Constant (1, typeof (int)),
  151. Expression.Constant (1, typeof (int)));
  152. Assert.AreEqual (typeof (int), b.Type);
  153. Assert.IsFalse (b.IsLifted);
  154. Assert.IsFalse (b.IsLiftedToNull);
  155. }
  156. [Test]
  157. public void AddTestNullable ()
  158. {
  159. ParameterExpression a = Expression.Parameter (typeof (int?), "a"), b = Expression.Parameter (typeof (int?), "b");
  160. var l = Expression.Lambda<Func<int?, int?, int?>> (
  161. Expression.Add (a, b), a, b);
  162. var be = l.Body as BinaryExpression;
  163. Assert.IsNotNull (be);
  164. Assert.AreEqual (typeof (int?), be.Type);
  165. Assert.IsTrue (be.IsLifted);
  166. Assert.IsTrue (be.IsLiftedToNull);
  167. var c = l.Compile ();
  168. Assert.AreEqual (null, c (1, null), "a1");
  169. Assert.AreEqual (null, c (null, null), "a2");
  170. Assert.AreEqual (null, c (null, 2), "a3");
  171. Assert.AreEqual (3, c (1, 2), "a4");
  172. }
  173. struct EineStrukt {
  174. int i;
  175. public int I {
  176. get { return i; }
  177. }
  178. public EineStrukt (int i)
  179. {
  180. this.i = i;
  181. }
  182. public static EineStrukt operator + (EineStrukt a, EineStrukt b)
  183. {
  184. return new EineStrukt (a.i + b.i);
  185. }
  186. }
  187. [Test]
  188. [Category ("NotWorking")]
  189. public void AddNullableStruct ()
  190. {
  191. var a = Expression.Parameter (typeof (EineStrukt?), "a");
  192. var b = Expression.Parameter (typeof (EineStrukt?), "b");
  193. var body = Expression.Add (a, b);
  194. var lambda = Expression.Lambda<Func<EineStrukt?, EineStrukt?, EineStrukt?>> (body, a, b);
  195. Assert.AreEqual (typeof (EineStrukt?), body.Type);
  196. Assert.IsTrue (body.IsLifted);
  197. Assert.IsTrue (body.IsLiftedToNull);
  198. var add = lambda.Compile ();
  199. var res = add (new EineStrukt (2), new EineStrukt (3));
  200. Assert.IsTrue (res.HasValue);
  201. Assert.AreEqual (5, res.Value.I);
  202. res = add (null, null);
  203. Assert.IsFalse (res.HasValue);
  204. }
  205. }
  206. }