ExpressionTest_Negate.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //
  2. // ExpressionTest_Negate.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_Negate
  38. {
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void Arg1Null ()
  42. {
  43. Expression.Negate (null);
  44. }
  45. [Test]
  46. [ExpectedException (typeof (ArgumentException))]
  47. public void MethodArgNotStatic ()
  48. {
  49. Expression.Negate (Expression.Constant (new object ()), typeof (OpClass).GetMethod ("WrongUnaryNotStatic"));
  50. }
  51. [Test]
  52. [ExpectedException (typeof (ArgumentException))]
  53. public void MethodArgParameterCount ()
  54. {
  55. Expression.Negate (Expression.Constant (new object ()), typeof (OpClass).GetMethod ("WrongUnaryParameterCount"));
  56. }
  57. [Test]
  58. [ExpectedException (typeof (ArgumentException))]
  59. public void MethodArgReturnsVoid ()
  60. {
  61. Expression.Negate (Expression.Constant (new object ()), typeof (OpClass).GetMethod ("WrongUnaryReturnVoid"));
  62. }
  63. [Test]
  64. [ExpectedException (typeof (InvalidOperationException))]
  65. public void NegateBool ()
  66. {
  67. Expression.Negate (true.ToConstant ());
  68. }
  69. [Test]
  70. public void Number ()
  71. {
  72. var up = Expression.Negate (Expression.Constant (1));
  73. Assert.AreEqual ("-1", up.ToString ());
  74. }
  75. [Test]
  76. public void UserDefinedClass ()
  77. {
  78. var mi = typeof (OpClass).GetMethod ("op_UnaryNegation");
  79. var expr = Expression.Negate (Expression.Constant (new OpClass ()));
  80. Assert.AreEqual (ExpressionType.Negate, expr.NodeType);
  81. Assert.AreEqual (typeof (OpClass), expr.Type);
  82. Assert.AreEqual (mi, expr.Method);
  83. Assert.AreEqual ("op_UnaryNegation", expr.Method.Name);
  84. Assert.AreEqual ("-value(MonoTests.System.Linq.Expressions.OpClass)", expr.ToString ());
  85. }
  86. [Test]
  87. public void NegateNullableInt32 ()
  88. {
  89. var n = Expression.Negate (Expression.Parameter (typeof (int?), ""));
  90. Assert.AreEqual (typeof (int?), n.Type);
  91. Assert.IsTrue (n.IsLifted);
  92. Assert.IsTrue (n.IsLiftedToNull);
  93. Assert.IsNull (n.Method);
  94. }
  95. [Test]
  96. public void CompileNegateInt32 ()
  97. {
  98. var p = Expression.Parameter (typeof (int), "i");
  99. var negate = Expression.Lambda<Func<int, int>> (Expression.Negate (p), p).Compile ();
  100. Assert.AreEqual (-2, negate (2));
  101. Assert.AreEqual (0, negate (0));
  102. Assert.AreEqual (3, negate (-3));
  103. }
  104. [Test]
  105. public void CompiledNegateNullableInt32 ()
  106. {
  107. var p = Expression.Parameter (typeof (int?), "i");
  108. var negate = Expression.Lambda<Func<int?, int?>> (Expression.Negate (p), p).Compile ();
  109. Assert.AreEqual (null, negate (null));
  110. Assert.AreEqual ((int?) -2, negate (2));
  111. Assert.AreEqual ((int?) 0, negate (0));
  112. Assert.AreEqual ((int?) 3, negate (-3));
  113. }
  114. struct Slot {
  115. public int Value;
  116. public Slot (int value)
  117. {
  118. this.Value = value;
  119. }
  120. public static Slot operator - (Slot s)
  121. {
  122. return new Slot (-s.Value);
  123. }
  124. }
  125. [Test]
  126. public void UserDefinedNegate ()
  127. {
  128. var s = Expression.Parameter (typeof (Slot), "s");
  129. var node = Expression.Negate (s);
  130. Assert.IsFalse (node.IsLifted);
  131. Assert.IsFalse (node.IsLiftedToNull);
  132. Assert.AreEqual (typeof (Slot), node.Type);
  133. var negate = Expression.Lambda<Func<Slot, Slot>> (node, s).Compile ();
  134. Assert.AreEqual (new Slot (-2), negate (new Slot (2)));
  135. Assert.AreEqual (new Slot (42), negate (new Slot (-42)));
  136. }
  137. [Test]
  138. public void UserDefinedNotNullableNegateNullable ()
  139. {
  140. var s = Expression.Parameter (typeof (Slot?), "s");
  141. var node = Expression.Negate (s);
  142. Assert.IsTrue (node.IsLifted);
  143. Assert.IsTrue (node.IsLiftedToNull);
  144. Assert.AreEqual (typeof (Slot?), node.Type);
  145. var negate = Expression.Lambda<Func<Slot?, Slot?>> (node, s).Compile ();
  146. Assert.AreEqual (null, negate (null));
  147. Assert.AreEqual (new Slot (42), negate (new Slot (-42)));
  148. Assert.AreEqual (new Slot (-2), negate (new Slot (2)));
  149. }
  150. struct SlotToNullable {
  151. public int Value;
  152. public SlotToNullable (int value)
  153. {
  154. this.Value = value;
  155. }
  156. public static SlotToNullable? operator - (SlotToNullable s)
  157. {
  158. return new SlotToNullable (-s.Value);
  159. }
  160. }
  161. [Test]
  162. [ExpectedException (typeof (InvalidOperationException))]
  163. public void UserDefinedToNullableNegateFromNullable ()
  164. {
  165. Expression.Negate (Expression.Parameter (typeof (SlotToNullable?), "s"));
  166. }
  167. [Test]
  168. public void UserDefinedToNullableNegateNullable ()
  169. {
  170. var s = Expression.Parameter (typeof (SlotToNullable), "s");
  171. var node = Expression.Negate (s);
  172. Assert.IsFalse (node.IsLifted);
  173. Assert.IsFalse (node.IsLiftedToNull);
  174. Assert.AreEqual (typeof (SlotToNullable?), node.Type);
  175. var negate = Expression.Lambda<Func<SlotToNullable, SlotToNullable?>> (node, s).Compile ();
  176. Assert.AreEqual ((SlotToNullable?) new SlotToNullable (42), negate (new SlotToNullable (-42)));
  177. Assert.AreEqual ((SlotToNullable?) new SlotToNullable (-2), negate (new SlotToNullable (2)));
  178. }
  179. struct SlotFromNullable {
  180. public int Value;
  181. public SlotFromNullable (int value)
  182. {
  183. this.Value = value;
  184. }
  185. public static SlotFromNullable operator - (SlotFromNullable? s)
  186. {
  187. if (s.HasValue)
  188. return new SlotFromNullable (-s.Value.Value);
  189. else
  190. return new SlotFromNullable (-1);
  191. }
  192. }
  193. [Test]
  194. public void UserDefinedNegateFromNullable ()
  195. {
  196. var s = Expression.Parameter (typeof (SlotFromNullable?), "s");
  197. var node = Expression.Negate (s);
  198. Assert.IsFalse (node.IsLifted);
  199. Assert.IsFalse (node.IsLiftedToNull);
  200. Assert.AreEqual (typeof (SlotFromNullable), node.Type);
  201. var negate = Expression.Lambda<Func<SlotFromNullable?, SlotFromNullable>> (node, s).Compile ();
  202. Assert.AreEqual (new SlotFromNullable (-2), negate (new SlotFromNullable (2)));
  203. Assert.AreEqual (new SlotFromNullable (42), negate (new SlotFromNullable (-42)));
  204. Assert.AreEqual (new SlotFromNullable (-1), negate (null));
  205. }
  206. struct SlotFromNullableToNullable {
  207. public int Value;
  208. public SlotFromNullableToNullable (int value)
  209. {
  210. this.Value = value;
  211. }
  212. public static SlotFromNullableToNullable? operator - (SlotFromNullableToNullable? s)
  213. {
  214. if (s.HasValue)
  215. return new SlotFromNullableToNullable (-s.Value.Value);
  216. else
  217. return s;
  218. }
  219. }
  220. [Test]
  221. public void UserDefinedNegateFromNullableNotNullable ()
  222. {
  223. var s = Expression.Parameter (typeof (SlotFromNullableToNullable?), "s");
  224. var node = Expression.Negate (s);
  225. Assert.IsFalse (node.IsLifted);
  226. Assert.IsFalse (node.IsLiftedToNull);
  227. Assert.AreEqual (typeof (SlotFromNullableToNullable?), node.Type);
  228. var negate = Expression.Lambda<Func<SlotFromNullableToNullable?, SlotFromNullableToNullable?>> (
  229. node, s).Compile ();
  230. Assert.AreEqual (new SlotFromNullableToNullable (-2), negate (new SlotFromNullableToNullable (2)));
  231. Assert.AreEqual (new SlotFromNullableToNullable (42), negate (new SlotFromNullableToNullable (-42)));
  232. Assert.AreEqual (null, negate (null));
  233. }
  234. [Test]
  235. public void NegateDecimal ()
  236. {
  237. // Make sure the negate method is not linked away
  238. decimal d1 = 1;
  239. decimal d2 = -d1;
  240. var d = Expression.Parameter (typeof (decimal), "l");
  241. var meth = typeof (decimal).GetMethod ("op_UnaryNegation", new [] { typeof (decimal) });
  242. var node = Expression.Negate (d);
  243. Assert.IsFalse (node.IsLifted);
  244. Assert.IsFalse (node.IsLiftedToNull);
  245. Assert.AreEqual (typeof (decimal), node.Type);
  246. Assert.AreEqual (meth, node.Method);
  247. var neg = Expression.Lambda<Func<decimal, decimal>> (node, d).Compile ();
  248. Assert.AreEqual (-2m, neg (2m));
  249. }
  250. [Test]
  251. public void NegateLiftedDecimal ()
  252. {
  253. var d = Expression.Parameter (typeof (decimal?), "l");
  254. var meth = typeof (decimal).GetMethod ("op_UnaryNegation", new [] { typeof (decimal) });
  255. var node = Expression.Negate (d);
  256. Assert.IsTrue (node.IsLifted);
  257. Assert.IsTrue (node.IsLiftedToNull);
  258. Assert.AreEqual (typeof (decimal?), node.Type);
  259. Assert.AreEqual (meth, node.Method);
  260. var neg = Expression.Lambda<Func<decimal?, decimal?>> (node, d).Compile ();
  261. Assert.AreEqual (-2m, neg (2m));
  262. Assert.AreEqual (null, neg (null));
  263. }
  264. }
  265. }