/mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_Negate.cs

https://github.com/t-ashula/mono · C# · 315 lines · 236 code · 52 blank · 27 comment · 2 complexity · 16a78bc796998521e3cfda07707fa367 MD5 · raw file

  1. //
  2. // ExpressionTest_Negate.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@novell.com)
  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.Negate (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. [Category ("NotWorkingInterpreter")]
  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. var d = Expression.Parameter (typeof (decimal), "l");
  238. var meth = typeof (decimal).GetMethod ("op_UnaryNegation", new [] { typeof (decimal) });
  239. var node = Expression.Negate (d);
  240. Assert.IsFalse (node.IsLifted);
  241. Assert.IsFalse (node.IsLiftedToNull);
  242. Assert.AreEqual (typeof (decimal), node.Type);
  243. Assert.AreEqual (meth, node.Method);
  244. var neg = Expression.Lambda<Func<decimal, decimal>> (node, d).Compile ();
  245. Assert.AreEqual (-2m, neg (2m));
  246. }
  247. [Test]
  248. [Category ("NotWorkingInterpreter")]
  249. public void NegateLiftedDecimal ()
  250. {
  251. var d = Expression.Parameter (typeof (decimal?), "l");
  252. var meth = typeof (decimal).GetMethod ("op_UnaryNegation", new [] { typeof (decimal) });
  253. var node = Expression.Negate (d);
  254. Assert.IsTrue (node.IsLifted);
  255. Assert.IsTrue (node.IsLiftedToNull);
  256. Assert.AreEqual (typeof (decimal?), node.Type);
  257. Assert.AreEqual (meth, node.Method);
  258. var neg = Expression.Lambda<Func<decimal?, decimal?>> (node, d).Compile ();
  259. Assert.AreEqual (-2m, neg (2m));
  260. Assert.AreEqual (null, neg (null));
  261. }
  262. }
  263. }