ExpressionTest_Add.cs 12 KB

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