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