2
0

ExpressionTest_OrElse.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 <[email protected]>
  21. // Jb Evain <[email protected]>
  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. #if !NET_4_0
  76. Assert.AreEqual ("(True || False)", expr.ToString(), "OrElse#04");
  77. #endif
  78. }
  79. [Test]
  80. public void UserDefinedClass ()
  81. {
  82. // We can use the simplest version of GetMethod because we already know only one
  83. // exists in the very simple class we're using for the tests.
  84. MethodInfo mi = typeof (OpClass).GetMethod ("op_BitwiseOr");
  85. BinaryExpression expr = Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
  86. Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#05");
  87. Assert.AreEqual (typeof (OpClass), expr.Type, "OrElse#06");
  88. Assert.AreEqual (mi, expr.Method, "OrElse#07");
  89. Assert.AreEqual ("op_BitwiseOr", expr.Method.Name, "OrElse#08");
  90. #if !NET_4_0
  91. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) || value(MonoTests.System.Linq.Expressions.OpClass))",
  92. expr.ToString(), "OrElse#09");
  93. #endif
  94. }
  95. public class BrokenMethod {
  96. public static int operator | (BrokenMethod a, BrokenMethod b)
  97. {
  98. return 1;
  99. }
  100. }
  101. public class BrokenMethod2 {
  102. public static BrokenMethod2 operator | (BrokenMethod2 a, int b)
  103. {
  104. return null;
  105. }
  106. }
  107. [Test]
  108. [ExpectedException(typeof(ArgumentException))]
  109. public void MethodInfoReturnType ()
  110. {
  111. Expression.OrElse (Expression.Constant (new BrokenMethod ()),
  112. Expression.Constant (new BrokenMethod ()));
  113. }
  114. [Test]
  115. [ExpectedException(typeof(ArgumentException))]
  116. public void MethodInfoReturnType2 ()
  117. {
  118. Expression.OrElse (Expression.Constant (new BrokenMethod2 ()),
  119. Expression.Constant (1));
  120. }
  121. [Test]
  122. public void OrElseNotLifted ()
  123. {
  124. var b = Expression.OrElse (
  125. Expression.Constant (true, typeof (bool)),
  126. Expression.Constant (true, typeof (bool)));
  127. Assert.AreEqual (typeof (bool), b.Type);
  128. Assert.IsFalse (b.IsLifted);
  129. Assert.IsFalse (b.IsLiftedToNull);
  130. }
  131. [Test]
  132. public void OrElseTest ()
  133. {
  134. var a = Expression.Parameter (typeof (bool), "a");
  135. var b = Expression.Parameter (typeof (bool), "b");
  136. var l = Expression.Lambda<Func<bool, bool, bool>> (
  137. Expression.OrElse (a, b), a, b);
  138. var be = l.Body as BinaryExpression;
  139. Assert.IsNotNull (be);
  140. Assert.AreEqual (typeof (bool), be.Type);
  141. Assert.IsFalse (be.IsLifted);
  142. Assert.IsFalse (be.IsLiftedToNull);
  143. var c = l.Compile ();
  144. Assert.AreEqual (true, c (true, true), "o1");
  145. Assert.AreEqual (true, c (true, false), "o2");
  146. Assert.AreEqual (true, c (false, true), "o3");
  147. Assert.AreEqual (false, c (false, false), "o4");
  148. }
  149. [Test]
  150. public void OrElseLifted ()
  151. {
  152. var b = Expression.OrElse (
  153. Expression.Constant (null, typeof (bool?)),
  154. Expression.Constant (null, typeof (bool?)));
  155. Assert.AreEqual (typeof (bool?), b.Type);
  156. Assert.IsTrue (b.IsLifted);
  157. Assert.IsTrue (b.IsLiftedToNull);
  158. }
  159. [Test]
  160. [Category ("NotWorkingInterpreter")]
  161. public void OrElseTestNullable ()
  162. {
  163. var a = Expression.Parameter (typeof (bool?), "a");
  164. var b = Expression.Parameter (typeof (bool?), "b");
  165. var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
  166. Expression.OrElse (a, b), a, b);
  167. var be = l.Body as BinaryExpression;
  168. Assert.IsNotNull (be);
  169. Assert.AreEqual (typeof (bool?), be.Type);
  170. Assert.IsTrue (be.IsLifted);
  171. Assert.IsTrue (be.IsLiftedToNull);
  172. var c = l.Compile ();
  173. Assert.AreEqual (true, c (true, true), "o1");
  174. Assert.AreEqual (true, c (true, false), "o2");
  175. Assert.AreEqual (true, c (false, true), "o3");
  176. Assert.AreEqual (false, c (false, false), "o4");
  177. Assert.AreEqual (true, c (true, null), "o5");
  178. Assert.AreEqual (null, c (false, null), "o6");
  179. Assert.AreEqual (null, c (null, false), "o7");
  180. Assert.AreEqual (true, c (true, null), "o8");
  181. Assert.AreEqual (null, c (null, null), "o9");
  182. }
  183. [Test]
  184. public void OrElseBoolItem ()
  185. {
  186. var i = Expression.Parameter (typeof (Item<bool>), "i");
  187. var and = Expression.Lambda<Func<Item<bool>, bool>> (
  188. Expression.OrElse (
  189. Expression.Property (i, "Left"),
  190. Expression.Property (i, "Right")), i).Compile ();
  191. var item = new Item<bool> (true, false);
  192. Assert.AreEqual (true, and (item));
  193. Assert.IsTrue (item.LeftCalled);
  194. Assert.IsFalse (item.RightCalled);
  195. }
  196. [Test]
  197. [Category ("NotWorkingInterpreter")]
  198. public void OrElseNullableBoolItem ()
  199. {
  200. var i = Expression.Parameter (typeof (Item<bool?>), "i");
  201. var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
  202. Expression.OrElse (
  203. Expression.Property (i, "Left"),
  204. Expression.Property (i, "Right")), i).Compile ();
  205. var item = new Item<bool?> (true, false);
  206. Assert.AreEqual ((bool?) true, and (item));
  207. Assert.IsTrue (item.LeftCalled);
  208. Assert.IsFalse (item.RightCalled);
  209. }
  210. struct Slot {
  211. public int Value;
  212. public Slot (int val)
  213. {
  214. this.Value = val;
  215. }
  216. public static Slot operator | (Slot a, Slot b)
  217. {
  218. return new Slot (a.Value | b.Value);
  219. }
  220. public static bool operator true (Slot a)
  221. {
  222. return a.Value != 0;
  223. }
  224. public static bool operator false (Slot a)
  225. {
  226. return a.Value == 0;
  227. }
  228. }
  229. [Test]
  230. [Category ("NotWorkingInterpreter")]
  231. public void UserDefinedOrElse ()
  232. {
  233. var l = Expression.Parameter (typeof (Slot), "l");
  234. var r = Expression.Parameter (typeof (Slot), "r");
  235. var method = typeof (Slot).GetMethod ("op_BitwiseOr");
  236. var node = Expression.OrElse (l, r, method);
  237. Assert.IsFalse (node.IsLifted);
  238. Assert.IsFalse (node.IsLiftedToNull);
  239. Assert.AreEqual (method, node.Method);
  240. var orelse = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
  241. Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
  242. Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
  243. }
  244. #if !NET_4_0 // dlr bug 5867
  245. [Test]
  246. public void UserDefinedOrElseLiftedToNull ()
  247. {
  248. var l = Expression.Parameter (typeof (Slot?), "l");
  249. var r = Expression.Parameter (typeof (Slot?), "r");
  250. var method = typeof (Slot).GetMethod ("op_BitwiseOr");
  251. var node = Expression.OrElse (l, r, method);
  252. Assert.IsTrue (node.IsLifted);
  253. Assert.IsTrue (node.IsLiftedToNull);
  254. Assert.AreEqual (method, node.Method);
  255. var orelse = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
  256. Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
  257. Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
  258. Assert.AreEqual (new Slot (64), orelse (null, new Slot (64)));
  259. Assert.AreEqual (new Slot (32), orelse (new Slot (32), null));
  260. Assert.AreEqual (null, orelse (null, null));
  261. }
  262. #endif
  263. [Test]
  264. [Category ("NotWorkingInterpreter")]
  265. public void UserDefinedOrElseShortCircuit ()
  266. {
  267. var i = Expression.Parameter (typeof (Item<Slot>), "i");
  268. var orelse = Expression.Lambda<Func<Item<Slot>, Slot>> (
  269. Expression.OrElse (
  270. Expression.Property (i, "Left"),
  271. Expression.Property (i, "Right")), i).Compile ();
  272. var item = new Item<Slot> (new Slot (1), new Slot (0));
  273. Assert.AreEqual (new Slot (1), orelse (item));
  274. Assert.IsTrue (item.LeftCalled);
  275. Assert.IsFalse (item.RightCalled);
  276. }
  277. [Test]
  278. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
  279. [Category ("NotWorkingInterpreter")]
  280. public void UserDefinedLiftedOrElseShortCircuit ()
  281. {
  282. var i = Expression.Parameter (typeof (Item<Slot?>), "i");
  283. var orelse = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
  284. Expression.OrElse (
  285. Expression.Property (i, "Left"),
  286. Expression.Property (i, "Right")), i).Compile ();
  287. var item = new Item<Slot?> (new Slot (1), null);
  288. Assert.AreEqual ((Slot?) new Slot (1), orelse (item));
  289. Assert.IsTrue (item.LeftCalled);
  290. Assert.IsFalse (item.RightCalled);
  291. }
  292. struct Incomplete {
  293. public int Value;
  294. public Incomplete (int val)
  295. {
  296. Value = val;
  297. }
  298. public static Incomplete operator | (Incomplete a, Incomplete b)
  299. {
  300. return new Incomplete (a.Value | b.Value);
  301. }
  302. }
  303. [Test]
  304. [ExpectedException (typeof (ArgumentException))]
  305. public void IncompleteUserDefinedOrElse ()
  306. {
  307. var l = Expression.Parameter (typeof (Incomplete), "l");
  308. var r = Expression.Parameter (typeof (Incomplete), "r");
  309. var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");
  310. Expression.OrElse (l, r, method);
  311. }
  312. }
  313. }