ExpressionTest_Add.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. // Jb Evain <[email protected]>
  22. using System;
  23. using System.Reflection;
  24. using System.Linq;
  25. using System.Linq.Expressions;
  26. using NUnit.Framework;
  27. namespace MonoTests.System.Linq.Expressions
  28. {
  29. [TestFixture]
  30. [Category("SRE")]
  31. public class ExpressionTest_Add
  32. {
  33. [Test]
  34. [ExpectedException (typeof (ArgumentNullException))]
  35. public void Arg1Null ()
  36. {
  37. Expression.Add (null, Expression.Constant (1));
  38. }
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void Arg2Null ()
  42. {
  43. Expression.Add (Expression.Constant (1), null);
  44. }
  45. [Test]
  46. [ExpectedException (typeof (InvalidOperationException))]
  47. public void ArgTypesDifferent ()
  48. {
  49. Expression.Add (Expression.Constant (1), Expression.Constant (2.0));
  50. }
  51. [Test]
  52. [ExpectedException (typeof (InvalidOperationException))]
  53. public void NoOperatorClass ()
  54. {
  55. Expression.Add (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
  56. }
  57. [Test]
  58. [ExpectedException (typeof (InvalidOperationException))]
  59. public void Boolean ()
  60. {
  61. Expression.Add (Expression.Constant (true), Expression.Constant (false));
  62. }
  63. [Test]
  64. public void Numeric ()
  65. {
  66. BinaryExpression expr = Expression.Add (Expression.Constant (1), Expression.Constant (2));
  67. Assert.AreEqual (ExpressionType.Add, expr.NodeType, "Add#01");
  68. Assert.AreEqual (typeof (int), expr.Type, "Add#02");
  69. Assert.IsNull (expr.Method, "Add#03");
  70. Assert.AreEqual ("(1 + 2)", expr.ToString(), "Add#04");
  71. }
  72. [Test]
  73. public void Nullable ()
  74. {
  75. int? a = 1;
  76. int? b = 2;
  77. BinaryExpression expr = Expression.Add (Expression.Constant (a,typeof(int?)),
  78. Expression.Constant (b, typeof(int?)));
  79. Assert.AreEqual (ExpressionType.Add, expr.NodeType, "Add#05");
  80. Assert.AreEqual (typeof (int?), expr.Type, "Add#06");
  81. Assert.IsNull (expr.Method, "Add#07");
  82. Assert.AreEqual ("(1 + 2)", expr.ToString(), "Add#08");
  83. }
  84. [Test]
  85. public void UserDefinedClass ()
  86. {
  87. // We can use the simplest version of GetMethod because we already know only one
  88. // exists in the very simple class we're using for the tests.
  89. MethodInfo mi = typeof (OpClass).GetMethod ("op_Addition");
  90. OpClass left = new OpClass ();
  91. BinaryExpression expr = Expression.Add (Expression.Constant (left), Expression.Constant (new OpClass ()));
  92. Assert.AreEqual (ExpressionType.Add, expr.NodeType, "Add#09");
  93. Assert.AreEqual (typeof (OpClass), expr.Type, "Add#10");
  94. Assert.AreEqual (mi, expr.Method, "Add#11");
  95. Assert.AreEqual ("op_Addition", expr.Method.Name, "Add#12");
  96. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) + value(MonoTests.System.Linq.Expressions.OpClass))",
  97. expr.ToString(), "Add#13");
  98. Expression.Lambda<Func<OpClass>> (expr);
  99. #if false
  100. //
  101. // We do not have support for objects that are not really
  102. // constants, like this case. Need to figure out what to do
  103. // with those
  104. //
  105. Func<OpClass> compiled = l.Compile ();
  106. Assert.AreEqual (left, compiled ());
  107. #endif
  108. }
  109. public class S {
  110. public static int MyAdder (int a, int b){
  111. return 1000;
  112. }
  113. }
  114. [Test]
  115. public void TestMethodAddition ()
  116. {
  117. BinaryExpression expr = Expression.Add (Expression.Constant (1), Expression.Constant (2), typeof(S).GetMethod("MyAdder"));
  118. Expression<Func<int>> l = Expression.Lambda<Func<int>> (expr);
  119. Func<int> compiled = l.Compile ();
  120. Assert.AreEqual (1000, compiled ());
  121. }
  122. [Test]
  123. public void CompileAdd ()
  124. {
  125. var left = Expression.Parameter (typeof (int), "l");
  126. var right = Expression.Parameter (typeof (int), "r");
  127. var l = Expression.Lambda<Func<int, int, int>> (
  128. Expression.Add (left, right), left, right);
  129. var be = l.Body as BinaryExpression;
  130. Assert.IsNotNull (be);
  131. Assert.AreEqual (typeof (int), be.Type);
  132. Assert.IsFalse (be.IsLifted);
  133. Assert.IsFalse (be.IsLiftedToNull);
  134. var add = l.Compile ();
  135. Assert.AreEqual (12, add (6, 6));
  136. Assert.AreEqual (0, add (-1, 1));
  137. Assert.AreEqual (-2, add (1, -3));
  138. }
  139. [Test]
  140. public void AddLifted ()
  141. {
  142. var b = Expression.Add (
  143. Expression.Constant (null, typeof (int?)),
  144. Expression.Constant (null, typeof (int?)));
  145. Assert.AreEqual (typeof (int?), b.Type);
  146. Assert.IsTrue (b.IsLifted);
  147. Assert.IsTrue (b.IsLiftedToNull);
  148. }
  149. [Test]
  150. public void AddNotLifted ()
  151. {
  152. var b = Expression.Add (
  153. Expression.Constant (1, typeof (int)),
  154. Expression.Constant (1, typeof (int)));
  155. Assert.AreEqual (typeof (int), b.Type);
  156. Assert.IsFalse (b.IsLifted);
  157. Assert.IsFalse (b.IsLiftedToNull);
  158. }
  159. [Test]
  160. public void AddTestNullable ()
  161. {
  162. var a = Expression.Parameter (typeof (int?), "a");
  163. var b = Expression.Parameter (typeof (int?), "b");
  164. var l = Expression.Lambda<Func<int?, int?, int?>> (
  165. Expression.Add (a, b), a, b);
  166. var be = l.Body as BinaryExpression;
  167. Assert.IsNotNull (be);
  168. Assert.AreEqual (typeof (int?), be.Type);
  169. Assert.IsTrue (be.IsLifted);
  170. Assert.IsTrue (be.IsLiftedToNull);
  171. var c = l.Compile ();
  172. Assert.AreEqual (null, c (1, null), "a1");
  173. Assert.AreEqual (null, c (null, null), "a2");
  174. Assert.AreEqual (null, c (null, 2), "a3");
  175. Assert.AreEqual (3, c (1, 2), "a4");
  176. }
  177. struct Slot {
  178. public int Value;
  179. public Slot (int value)
  180. {
  181. this.Value = value;
  182. }
  183. public static Slot operator + (Slot a, Slot b)
  184. {
  185. return new Slot (a.Value + b.Value);
  186. }
  187. }
  188. [Test]
  189. public void UserDefinedAdd ()
  190. {
  191. var l = Expression.Parameter (typeof (Slot), "l");
  192. var r = Expression.Parameter (typeof (Slot), "r");
  193. var node = Expression.Add (l, r);
  194. Assert.IsFalse (node.IsLifted);
  195. Assert.IsFalse (node.IsLiftedToNull);
  196. Assert.AreEqual (typeof (Slot), node.Type);
  197. var add = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
  198. Assert.AreEqual (new Slot (42), add (new Slot (21), new Slot (21)));
  199. Assert.AreEqual (new Slot (0), add (new Slot (1), new Slot (-1)));
  200. }
  201. [Test]
  202. public void UserDefinedAddLifted ()
  203. {
  204. var l = Expression.Parameter (typeof (Slot?), "l");
  205. var r = Expression.Parameter (typeof (Slot?), "r");
  206. var node = Expression.Add (l, r);
  207. Assert.IsTrue (node.IsLifted);
  208. Assert.IsTrue (node.IsLiftedToNull);
  209. Assert.AreEqual (typeof (Slot?), node.Type);
  210. var add = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
  211. Assert.AreEqual (null, add (null, null));
  212. Assert.AreEqual ((Slot?) new Slot (42), add ((Slot?) new Slot (21), (Slot?) new Slot (21)));
  213. }
  214. struct SlotToNullable {
  215. public int Value;
  216. public SlotToNullable (int value)
  217. {
  218. this.Value = value;
  219. }
  220. public static SlotToNullable? operator + (SlotToNullable a, SlotToNullable b)
  221. {
  222. return new SlotToNullable (a.Value + b.Value);
  223. }
  224. }
  225. [Test]
  226. [ExpectedException (typeof (InvalidOperationException))]
  227. public void UserDefinedToNullableAddFromNullable ()
  228. {
  229. Expression.Add (
  230. Expression.Parameter (typeof (SlotToNullable?), "l"),
  231. Expression.Parameter (typeof (SlotToNullable?), "r"));
  232. }
  233. [Test]
  234. public void UserDefinedToNullableAdd ()
  235. {
  236. var l = Expression.Parameter (typeof (SlotToNullable), "l");
  237. var r = Expression.Parameter (typeof (SlotToNullable), "r");
  238. var node = Expression.Add (l, r);
  239. Assert.IsFalse (node.IsLifted);
  240. Assert.IsFalse (node.IsLiftedToNull);
  241. Assert.AreEqual (typeof (SlotToNullable?), node.Type);
  242. Assert.IsNotNull (node.Method);
  243. var add = Expression.Lambda<Func<SlotToNullable, SlotToNullable, SlotToNullable?>> (node, l, r).Compile ();
  244. Assert.AreEqual ((SlotToNullable?) new SlotToNullable (4), add (new SlotToNullable (2), new SlotToNullable (2)));
  245. Assert.AreEqual ((SlotToNullable?) new SlotToNullable (0), add (new SlotToNullable (2), new SlotToNullable (-2)));
  246. }
  247. /*struct SlotFromNullableToNullable {
  248. public int Value;
  249. public SlotFromNullableToNullable (int value)
  250. {
  251. this.Value = value;
  252. }
  253. public static SlotFromNullableToNullable? operator + (SlotFromNullableToNullable? a, SlotFromNullableToNullable? b)
  254. {
  255. if (a.HasValue && b.HasValue)
  256. return (SlotFromNullableToNullable?) new SlotFromNullableToNullable (
  257. a.Value.Value + b.Value.Value);
  258. else
  259. return null;
  260. }
  261. }
  262. [Test]
  263. public void UserDefinedFromNullableToNullableAdd ()
  264. {
  265. var l = Expression.Parameter (typeof (SlotFromNullableToNullable?), "l");
  266. var r = Expression.Parameter (typeof (SlotFromNullableToNullable?), "r");
  267. var node = Expression.Add (l, r);
  268. Assert.IsFalse (node.IsLifted);
  269. Assert.IsFalse (node.IsLiftedToNull);
  270. Assert.AreEqual (typeof (SlotFromNullableToNullable?), node.Type);
  271. Assert.IsNotNull (node.Method);
  272. var add = Expression.Lambda<Func<SlotFromNullableToNullable?, SlotFromNullableToNullable?, SlotFromNullableToNullable?>> (node, l, r).Compile ();
  273. Assert.AreEqual ((SlotFromNullableToNullable?) null, add (null, null));
  274. Assert.AreEqual ((SlotFromNullableToNullable?) null, add (new SlotFromNullableToNullable (2), null));
  275. Assert.AreEqual ((SlotFromNullableToNullable?) null, add (null, new SlotFromNullableToNullable (2)));
  276. Assert.AreEqual ((SlotFromNullableToNullable?) new SlotFromNullableToNullable (4), add (new SlotFromNullableToNullable (2), new SlotFromNullableToNullable (2)));
  277. Assert.AreEqual ((SlotFromNullableToNullable?) new SlotFromNullableToNullable (0), add (new SlotFromNullableToNullable (2), new SlotFromNullableToNullable (-2)));
  278. }*/
  279. [Test]
  280. public void AddStrings ()
  281. {
  282. var l = Expression.Parameter (typeof (string), "l");
  283. var r = Expression.Parameter (typeof (string), "r");
  284. var meth = typeof (string).GetMethod ("Concat", new [] { typeof (object), typeof (object) });
  285. var node = Expression.Add (l, r, meth);
  286. Assert.IsFalse (node.IsLifted);
  287. Assert.IsFalse (node.IsLiftedToNull);
  288. Assert.AreEqual (typeof (string), node.Type);
  289. Assert.AreEqual (meth, node.Method);
  290. var concat = Expression.Lambda<Func<string, string, string>> (node, l, r).Compile ();
  291. Assert.AreEqual (string.Empty, concat (null, null));
  292. Assert.AreEqual ("foobar", concat ("foo", "bar"));
  293. }
  294. [Test]
  295. public void AddDecimals ()
  296. {
  297. var l = Expression.Parameter (typeof (decimal), "l");
  298. var r = Expression.Parameter (typeof (decimal), "r");
  299. var meth = typeof (decimal).GetMethod ("op_Addition", new [] { typeof (decimal), typeof (decimal) });
  300. var node = Expression.Add (l, r);
  301. Assert.IsFalse (node.IsLifted);
  302. Assert.IsFalse (node.IsLiftedToNull);
  303. Assert.AreEqual (typeof (decimal), node.Type);
  304. Assert.AreEqual (meth, node.Method);
  305. var add = Expression.Lambda<Func<decimal, decimal, decimal>> (node, l, r).Compile ();
  306. Assert.AreEqual (2m, add (1m, 1m));
  307. }
  308. [Test]
  309. public void AddLiftedDecimals ()
  310. {
  311. var l = Expression.Parameter (typeof (decimal?), "l");
  312. var r = Expression.Parameter (typeof (decimal?), "r");
  313. var meth = typeof (decimal).GetMethod ("op_Addition", new [] { typeof (decimal), typeof (decimal) });
  314. var node = Expression.Add (l, r);
  315. Assert.IsTrue (node.IsLifted);
  316. Assert.IsTrue (node.IsLiftedToNull);
  317. Assert.AreEqual (typeof (decimal?), node.Type);
  318. Assert.AreEqual (meth, node.Method);
  319. var add = Expression.Lambda<Func<decimal?, decimal?, decimal?>> (node, l, r).Compile ();
  320. Assert.AreEqual (2m, add (1m, 1m));
  321. Assert.AreEqual (null, add (1m, null));
  322. Assert.AreEqual (null, add (null, null));
  323. }
  324. }
  325. }