ExpressionTest_Negate.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. [Category ("NotWorking")]
  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. [Category ("NotWorking")]
  163. [ExpectedException (typeof (InvalidOperationException))]
  164. public void UserDefinedToNullableNegateFromNullable ()
  165. {
  166. Expression.Negate (Expression.Parameter (typeof (SlotToNullable?), "s"));
  167. }
  168. [Test]
  169. [Category ("NotWorking")]
  170. public void UserDefinedToNullableNegateNullable ()
  171. {
  172. var s = Expression.Parameter (typeof (SlotToNullable), "s");
  173. var node = Expression.Negate (s);
  174. Assert.IsFalse (node.IsLifted);
  175. Assert.IsFalse (node.IsLiftedToNull);
  176. Assert.AreEqual (typeof (SlotToNullable?), node.Type);
  177. var negate = Expression.Lambda<Func<SlotToNullable, SlotToNullable?>> (node, s).Compile ();
  178. Assert.AreEqual ((SlotToNullable?) new SlotToNullable (42), negate (new SlotToNullable (-42)));
  179. Assert.AreEqual ((SlotToNullable?) new SlotToNullable (-2), negate (new SlotToNullable (2)));
  180. }
  181. struct SlotFromNullable {
  182. public int Value;
  183. public SlotFromNullable (int value)
  184. {
  185. this.Value = value;
  186. }
  187. public static SlotFromNullable operator - (SlotFromNullable? s)
  188. {
  189. if (s.HasValue)
  190. return new SlotFromNullable (-s.Value.Value);
  191. else
  192. return new SlotFromNullable (-1);
  193. }
  194. }
  195. [Test]
  196. [Category ("NotWorking")]
  197. public void UserDefinedNegateFromNullable ()
  198. {
  199. var s = Expression.Parameter (typeof (SlotFromNullable?), "s");
  200. var node = Expression.Negate (s);
  201. Assert.IsFalse (node.IsLifted);
  202. Assert.IsFalse (node.IsLiftedToNull);
  203. Assert.AreEqual (typeof (SlotFromNullable), node.Type);
  204. var negate = Expression.Lambda<Func<SlotFromNullable?, SlotFromNullable>> (node, s).Compile ();
  205. Assert.AreEqual (new SlotFromNullable (-2), negate (new SlotFromNullable (2)));
  206. Assert.AreEqual (new SlotFromNullable (42), negate (new SlotFromNullable (-42)));
  207. Assert.AreEqual (new SlotFromNullable (-1), negate (null));
  208. }
  209. struct SlotFromNullableToNullable {
  210. public int Value;
  211. public SlotFromNullableToNullable (int value)
  212. {
  213. this.Value = value;
  214. }
  215. public static SlotFromNullableToNullable? operator - (SlotFromNullableToNullable? s)
  216. {
  217. if (s.HasValue)
  218. return new SlotFromNullableToNullable (-s.Value.Value);
  219. else
  220. return s;
  221. }
  222. }
  223. [Test]
  224. [Category ("NotWorking")]
  225. public void UserDefinedNegateFromNullableNotNullable ()
  226. {
  227. var s = Expression.Parameter (typeof (SlotFromNullableToNullable?), "s");
  228. var node = Expression.Negate (s);
  229. Assert.IsFalse (node.IsLifted);
  230. Assert.IsFalse (node.IsLiftedToNull);
  231. Assert.AreEqual (typeof (SlotFromNullableToNullable?), node.Type);
  232. var negate = Expression.Lambda<Func<SlotFromNullableToNullable?, SlotFromNullableToNullable?>> (
  233. node, s).Compile ();
  234. Assert.AreEqual (new SlotFromNullableToNullable (-2), negate (new SlotFromNullableToNullable (2)));
  235. Assert.AreEqual (new SlotFromNullableToNullable (42), negate (new SlotFromNullableToNullable (-42)));
  236. Assert.AreEqual (null, negate (null));
  237. }
  238. }
  239. }