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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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
20 // Federico Di Gregorio <fog@initd.org>
21 // Jb Evain <jbevain@novell.com>
24 using System.Reflection;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
29 namespace MonoTests.System.Linq.Expressions
32 public class ExpressionTest_RightShift
35 [ExpectedException (typeof (ArgumentNullException))]
36 public void Arg1Null ()
38 Expression.RightShift (null, Expression.Constant (1));
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void Arg2Null ()
45 Expression.RightShift (Expression.Constant (1), null);
49 [ExpectedException (typeof (InvalidOperationException))]
50 public void Arg2WrongType ()
52 Expression.RightShift (Expression.Constant (1), Expression.Constant (2.0));
56 [ExpectedException (typeof (InvalidOperationException))]
57 public void NoOperatorClass ()
59 Expression.RightShift (Expression.Constant (new NoOpClass ()), Expression.Constant (1));
63 [ExpectedException (typeof (InvalidOperationException))]
64 public void Boolean ()
66 Expression.RightShift (Expression.Constant (true), Expression.Constant (1));
70 [ExpectedException (typeof (InvalidOperationException))]
73 Expression.RightShift (Expression.Constant (2.0), Expression.Constant (1));
77 public void Integer ()
79 BinaryExpression expr = Expression.RightShift (Expression.Constant (2), Expression.Constant (1));
80 Assert.AreEqual (ExpressionType.RightShift, expr.NodeType, "RightShift#01");
81 Assert.AreEqual (typeof (int), expr.Type, "RightShift#02");
82 Assert.IsNull (expr.Method, "RightShift#03");
83 Assert.AreEqual ("(2 >> 1)", expr.ToString(), "RightShift#04");
87 public void Nullable ()
92 BinaryExpression expr = Expression.RightShift (Expression.Constant (a), Expression.Constant (b));
93 Assert.AreEqual (ExpressionType.RightShift, expr.NodeType, "RightShift#05");
94 Assert.AreEqual (typeof (int), expr.Type, "RightShift#06");
95 Assert.IsNull (expr.Method, "RightShift#07");
96 Assert.AreEqual ("(1 >> 2)", expr.ToString(), "RightShift#08");
100 public void UserDefinedClass ()
102 // We can use the simplest version of GetMethod because we already know only one
103 // exists in the very simple class we're using for the tests.
104 MethodInfo mi = typeof (OpClass).GetMethod ("op_RightShift");
106 BinaryExpression expr = Expression.RightShift (Expression.Constant (new OpClass ()), Expression.Constant (1));
107 Assert.AreEqual (ExpressionType.RightShift, expr.NodeType, "RightShift#09");
108 Assert.AreEqual (typeof (OpClass), expr.Type, "RightShift#10");
109 Assert.AreEqual (mi, expr.Method, "RightShift#11");
110 Assert.AreEqual ("op_RightShift", expr.Method.Name, "RightShift#12");
111 Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) >> 1)",
112 expr.ToString(), "RightShift#13");
116 public void CompileRightShift ()
118 var l = Expression.Parameter (typeof (int), "l");
119 var r = Expression.Parameter (typeof (int), "r");
121 var rs = Expression.Lambda<Func<int, int, int>> (
122 Expression.RightShift (l, r), l, r).Compile ();
124 Assert.AreEqual (3, rs (6, 1));
125 Assert.AreEqual (1, rs (12, 3));
129 public void RightShiftNullableLongAndInt ()
131 var l = Expression.Parameter (typeof (long?), "l");
132 var r = Expression.Parameter (typeof (int), "r");
134 var node = Expression.RightShift (l, r);
135 Assert.IsTrue (node.IsLifted);
136 Assert.IsTrue (node.IsLiftedToNull);
137 Assert.AreEqual (typeof (long?), node.Type);