ExpressionTest_LeftShift.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. [Category("SRE")]
  31. public class ExpressionTest_LeftShift
  32. {
  33. [Test]
  34. [ExpectedException (typeof (ArgumentNullException))]
  35. public void Arg1Null ()
  36. {
  37. Expression.LeftShift (null, Expression.Constant (1));
  38. }
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void Arg2Null ()
  42. {
  43. Expression.LeftShift (Expression.Constant (1), null);
  44. }
  45. [Test]
  46. [ExpectedException (typeof (InvalidOperationException))]
  47. public void Arg2WrongType ()
  48. {
  49. Expression.LeftShift (Expression.Constant (1), Expression.Constant (2.0));
  50. }
  51. [Test]
  52. [ExpectedException (typeof (InvalidOperationException))]
  53. public void NoOperatorClass ()
  54. {
  55. Expression.LeftShift (Expression.Constant (new NoOpClass ()), Expression.Constant (1));
  56. }
  57. [Test]
  58. [ExpectedException (typeof (InvalidOperationException))]
  59. public void Boolean ()
  60. {
  61. Expression.LeftShift (Expression.Constant (true), Expression.Constant (1));
  62. }
  63. [Test]
  64. [ExpectedException (typeof (InvalidOperationException))]
  65. public void Double ()
  66. {
  67. Expression.LeftShift (Expression.Constant (2.0), Expression.Constant (1));
  68. }
  69. [Test]
  70. public void Integer ()
  71. {
  72. BinaryExpression expr = Expression.LeftShift (Expression.Constant (2), Expression.Constant (1));
  73. Assert.AreEqual (ExpressionType.LeftShift, expr.NodeType, "LeftShift#01");
  74. Assert.AreEqual (typeof (int), expr.Type, "LeftShift#02");
  75. Assert.IsNull (expr.Method, "LeftShift#03");
  76. Assert.AreEqual ("(2 << 1)", expr.ToString(), "LeftShift#04");
  77. }
  78. [Test]
  79. public void Nullable ()
  80. {
  81. int? a = 1;
  82. int? b = 2;
  83. BinaryExpression expr = Expression.LeftShift (Expression.Constant (a), Expression.Constant (b));
  84. Assert.AreEqual (ExpressionType.LeftShift, expr.NodeType, "LeftShift#05");
  85. Assert.AreEqual (typeof (int), expr.Type, "LeftShift#06");
  86. Assert.IsNull (expr.Method, "LeftShift#07");
  87. Assert.AreEqual ("(1 << 2)", expr.ToString(), "LeftShift#08");
  88. }
  89. [Test]
  90. public void UserDefinedClass ()
  91. {
  92. // We can use the simplest version of GetMethod because we already know only one
  93. // exists in the very simple class we're using for the tests.
  94. MethodInfo mi = typeof (OpClass).GetMethod ("op_LeftShift");
  95. BinaryExpression expr = Expression.LeftShift (Expression.Constant (new OpClass ()), Expression.Constant (1));
  96. Assert.AreEqual (ExpressionType.LeftShift, expr.NodeType, "LeftShift#09");
  97. Assert.AreEqual (typeof (OpClass), expr.Type, "LeftShift#10");
  98. Assert.AreEqual (mi, expr.Method, "LeftShift#11");
  99. Assert.AreEqual ("op_LeftShift", expr.Method.Name, "LeftShift#12");
  100. Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) << 1)",
  101. expr.ToString(), "LeftShift#13");
  102. }
  103. [Test]
  104. public void CompileLeftShift ()
  105. {
  106. ParameterExpression l = Expression.Parameter (typeof (int), "l"), r = Expression.Parameter (typeof (int), "r");
  107. var ls = Expression.Lambda<Func<int, int, int>> (
  108. Expression.LeftShift (l, r), l, r).Compile ();
  109. Assert.AreEqual (12, ls (6, 1));
  110. Assert.AreEqual (96, ls (12, 3));
  111. }
  112. [Test]
  113. public void LeftShiftNullableLongAndInt ()
  114. {
  115. var l = Expression.Parameter (typeof (long?), "l");
  116. var r = Expression.Parameter (typeof (int), "r");
  117. var node = Expression.LeftShift (l, r);
  118. Assert.IsTrue (node.IsLifted);
  119. Assert.IsTrue (node.IsLiftedToNull);
  120. Assert.AreEqual (typeof (long?), node.Type);
  121. var ls = Expression.Lambda<Func<long?, int, long?>> (node, l, r).Compile ();
  122. Assert.AreEqual (null, ls (null, 2));
  123. Assert.AreEqual (2048, ls (1024, 1));
  124. }
  125. }
  126. }