ExpressionTest_Negate.cs 9.1 KB

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