PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 359 lines | 279 code | 57 blank | 23 comment | 2 complexity | ab9c6ea6f7e471e0abb11c008a425eb5 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  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 <fog@initd.org>
  21. // Jb Evain <jbevain@novell.com>
  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_OrElse
  31. {
  32. [Test]
  33. [ExpectedException (typeof (ArgumentNullException))]
  34. public void Arg1Null ()
  35. {
  36. Expression.OrElse (null, Expression.Constant (1));
  37. }
  38. [Test]
  39. [ExpectedException (typeof (ArgumentNullException))]
  40. public void Arg2Null ()
  41. {
  42. Expression.OrElse (Expression.Constant (1), null);
  43. }
  44. [Test]
  45. [ExpectedException (typeof (InvalidOperationException))]
  46. public void NoOperatorClass ()
  47. {
  48. Expression.OrElse (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
  49. }
  50. [Test]
  51. [ExpectedException (typeof (InvalidOperationException))]
  52. public void Double ()
  53. {
  54. Expression.OrElse (Expression.Constant (1.0), Expression.Constant (2.0));
  55. }
  56. [Test]
  57. [ExpectedException (typeof (InvalidOperationException))]
  58. public void Integer ()
  59. {
  60. Expression.OrElse (Expression.Constant (1), Expression.Constant (2));
  61. }
  62. [Test]
  63. [ExpectedException (typeof (InvalidOperationException))]
  64. public void MismatchedTypes ()
  65. {
  66. Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (true));
  67. }
  68. [Test]
  69. public void Boolean ()
  70. {
  71. BinaryExpression expr = Expression.OrElse (Expression.Constant (true), Expression.Constant (false));
  72. Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#01");
  73. Assert.AreEqual (typeof (bool), expr.Type, "OrElse#02");
  74. Assert.IsNull (expr.Method, "OrElse#03");
  75. Assert.AreEqual ("(True || False)", expr.ToString(), "OrElse#04");
  76. }
  77. [Test]
  78. public void UserDefinedClass ()
  79. {
  80. // We can use the simplest version of GetMethod because we already know only one
  81. // exists in the very simple class we're using for the tests.
  82. MethodInfo mi = typeof (OpClass).GetMethod ("op_BitwiseOr");
  83. BinaryExpression expr = Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
  84. Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#05");
  85. Assert.AreEqual (typeof (OpClass), expr.Type, "OrElse#06");
  86. Assert.AreEqual (mi, expr.Method, "OrElse#07");
  87. Assert.AreEqual ("op_BitwiseOr", expr.Method.Name, "OrElse#08");
  88. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) || value(MonoTests.System.Linq.Expressions.OpClass))",
  89. expr.ToString(), "OrElse#09");
  90. }
  91. public class BrokenMethod {
  92. public static int operator | (BrokenMethod a, BrokenMethod b)
  93. {
  94. return 1;
  95. }
  96. }
  97. public class BrokenMethod2 {
  98. public static BrokenMethod2 operator | (BrokenMethod2 a, int b)
  99. {
  100. return null;
  101. }
  102. }
  103. [Test]
  104. [ExpectedException(typeof(ArgumentException))]
  105. public void MethodInfoReturnType ()
  106. {
  107. Expression.OrElse (Expression.Constant (new BrokenMethod ()),
  108. Expression.Constant (new BrokenMethod ()));
  109. }
  110. [Test]
  111. [ExpectedException(typeof(ArgumentException))]
  112. public void MethodInfoReturnType2 ()
  113. {
  114. Expression.OrElse (Expression.Constant (new BrokenMethod2 ()),
  115. Expression.Constant (1));
  116. }
  117. [Test]
  118. public void OrElseNotLifted ()
  119. {
  120. var b = Expression.OrElse (
  121. Expression.Constant (true, typeof (bool)),
  122. Expression.Constant (true, typeof (bool)));
  123. Assert.AreEqual (typeof (bool), b.Type);
  124. Assert.IsFalse (b.IsLifted);
  125. Assert.IsFalse (b.IsLiftedToNull);
  126. }
  127. [Test]
  128. public void OrElseTest ()
  129. {
  130. var a = Expression.Parameter (typeof (bool), "a");
  131. var b = Expression.Parameter (typeof (bool), "b");
  132. var l = Expression.Lambda<Func<bool, bool, bool>> (
  133. Expression.OrElse (a, b), a, b);
  134. var be = l.Body as BinaryExpression;
  135. Assert.IsNotNull (be);
  136. Assert.AreEqual (typeof (bool), be.Type);
  137. Assert.IsFalse (be.IsLifted);
  138. Assert.IsFalse (be.IsLiftedToNull);
  139. var c = l.Compile ();
  140. Assert.AreEqual (true, c (true, true), "o1");
  141. Assert.AreEqual (true, c (true, false), "o2");
  142. Assert.AreEqual (true, c (false, true), "o3");
  143. Assert.AreEqual (false, c (false, false), "o4");
  144. }
  145. [Test]
  146. public void OrElseLifted ()
  147. {
  148. var b = Expression.OrElse (
  149. Expression.Constant (null, typeof (bool?)),
  150. Expression.Constant (null, typeof (bool?)));
  151. Assert.AreEqual (typeof (bool?), b.Type);
  152. Assert.IsTrue (b.IsLifted);
  153. Assert.IsTrue (b.IsLiftedToNull);
  154. }
  155. [Test]
  156. public void OrElseTestNullable ()
  157. {
  158. var a = Expression.Parameter (typeof (bool?), "a");
  159. var b = Expression.Parameter (typeof (bool?), "b");
  160. var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
  161. Expression.OrElse (a, b), a, b);
  162. var be = l.Body as BinaryExpression;
  163. Assert.IsNotNull (be);
  164. Assert.AreEqual (typeof (bool?), be.Type);
  165. Assert.IsTrue (be.IsLifted);
  166. Assert.IsTrue (be.IsLiftedToNull);
  167. var c = l.Compile ();
  168. Assert.AreEqual (true, c (true, true), "o1");
  169. Assert.AreEqual (true, c (true, false), "o2");
  170. Assert.AreEqual (true, c (false, true), "o3");
  171. Assert.AreEqual (false, c (false, false), "o4");
  172. Assert.AreEqual (true, c (true, null), "o5");
  173. Assert.AreEqual (null, c (false, null), "o6");
  174. Assert.AreEqual (null, c (null, false), "o7");
  175. Assert.AreEqual (true, c (true, null), "o8");
  176. Assert.AreEqual (null, c (null, null), "o9");
  177. }
  178. [Test]
  179. public void OrElseBoolItem ()
  180. {
  181. var i = Expression.Parameter (typeof (Item<bool>), "i");
  182. var and = Expression.Lambda<Func<Item<bool>, bool>> (
  183. Expression.OrElse (
  184. Expression.Property (i, "Left"),
  185. Expression.Property (i, "Right")), i).Compile ();
  186. var item = new Item<bool> (true, false);
  187. Assert.AreEqual (true, and (item));
  188. Assert.IsTrue (item.LeftCalled);
  189. Assert.IsFalse (item.RightCalled);
  190. }
  191. [Test]
  192. public void OrElseNullableBoolItem ()
  193. {
  194. var i = Expression.Parameter (typeof (Item<bool?>), "i");
  195. var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
  196. Expression.OrElse (
  197. Expression.Property (i, "Left"),
  198. Expression.Property (i, "Right")), i).Compile ();
  199. var item = new Item<bool?> (true, false);
  200. Assert.AreEqual ((bool?) true, and (item));
  201. Assert.IsTrue (item.LeftCalled);
  202. Assert.IsFalse (item.RightCalled);
  203. }
  204. struct Slot {
  205. public int Value;
  206. public Slot (int val)
  207. {
  208. this.Value = val;
  209. }
  210. public static Slot operator | (Slot a, Slot b)
  211. {
  212. return new Slot (a.Value | b.Value);
  213. }
  214. public static bool operator true (Slot a)
  215. {
  216. return a.Value != 0;
  217. }
  218. public static bool operator false (Slot a)
  219. {
  220. return a.Value == 0;
  221. }
  222. }
  223. [Test]
  224. public void UserDefinedOrElse ()
  225. {
  226. var l = Expression.Parameter (typeof (Slot), "l");
  227. var r = Expression.Parameter (typeof (Slot), "r");
  228. var method = typeof (Slot).GetMethod ("op_BitwiseOr");
  229. var node = Expression.OrElse (l, r, method);
  230. Assert.IsFalse (node.IsLifted);
  231. Assert.IsFalse (node.IsLiftedToNull);
  232. Assert.AreEqual (method, node.Method);
  233. var orelse = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
  234. Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
  235. Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
  236. }
  237. [Test]
  238. public void UserDefinedOrElseLiftedToNull ()
  239. {
  240. var l = Expression.Parameter (typeof (Slot?), "l");
  241. var r = Expression.Parameter (typeof (Slot?), "r");
  242. var method = typeof (Slot).GetMethod ("op_BitwiseOr");
  243. var node = Expression.OrElse (l, r, method);
  244. Assert.IsTrue (node.IsLifted);
  245. Assert.IsTrue (node.IsLiftedToNull);
  246. Assert.AreEqual (method, node.Method);
  247. var orelse = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
  248. Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
  249. Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
  250. Assert.AreEqual (new Slot (64), orelse (null, new Slot (64)));
  251. Assert.AreEqual (new Slot (32), orelse (new Slot (32), null));
  252. Assert.AreEqual (null, orelse (null, null));
  253. }
  254. [Test]
  255. public void UserDefinedOrElseShortCircuit ()
  256. {
  257. var i = Expression.Parameter (typeof (Item<Slot>), "i");
  258. var orelse = Expression.Lambda<Func<Item<Slot>, Slot>> (
  259. Expression.OrElse (
  260. Expression.Property (i, "Left"),
  261. Expression.Property (i, "Right")), i).Compile ();
  262. var item = new Item<Slot> (new Slot (1), new Slot (0));
  263. Assert.AreEqual (new Slot (1), orelse (item));
  264. Assert.IsTrue (item.LeftCalled);
  265. Assert.IsFalse (item.RightCalled);
  266. }
  267. [Test]
  268. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
  269. public void UserDefinedLiftedOrElseShortCircuit ()
  270. {
  271. var i = Expression.Parameter (typeof (Item<Slot?>), "i");
  272. var orelse = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
  273. Expression.OrElse (
  274. Expression.Property (i, "Left"),
  275. Expression.Property (i, "Right")), i).Compile ();
  276. var item = new Item<Slot?> (new Slot (1), null);
  277. Assert.AreEqual ((Slot?) new Slot (1), orelse (item));
  278. Assert.IsTrue (item.LeftCalled);
  279. Assert.IsFalse (item.RightCalled);
  280. }
  281. struct Incomplete {
  282. public int Value;
  283. public Incomplete (int val)
  284. {
  285. Value = val;
  286. }
  287. public static Incomplete operator | (Incomplete a, Incomplete b)
  288. {
  289. return new Incomplete (a.Value | b.Value);
  290. }
  291. }
  292. [Test]
  293. [ExpectedException (typeof (ArgumentException))]
  294. public void IncompleteUserDefinedOrElse ()
  295. {
  296. var l = Expression.Parameter (typeof (Incomplete), "l");
  297. var r = Expression.Parameter (typeof (Incomplete), "r");
  298. var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");
  299. Expression.OrElse (l, r, method);
  300. }
  301. }
  302. }