0d6c24f42858349bcebeb3205d4cd4fc21aa5586
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_RightShift.cs
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
23 using System;
24 using System.Reflection;
25 using System.Linq;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
28
29 namespace MonoTests.System.Linq.Expressions
30 {
31         [TestFixture]
32         public class ExpressionTest_RightShift
33         {
34                 [Test]
35                 [ExpectedException (typeof (ArgumentNullException))]
36                 public void Arg1Null ()
37                 {
38                         Expression.RightShift (null, Expression.Constant (1));
39                 }
40
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void Arg2Null ()
44                 {
45                         Expression.RightShift (Expression.Constant (1), null);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (InvalidOperationException))]
50                 public void Arg2WrongType ()
51                 {
52                         Expression.RightShift (Expression.Constant (1), Expression.Constant (2.0));
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (InvalidOperationException))]
57                 public void NoOperatorClass ()
58                 {
59                         Expression.RightShift (Expression.Constant (new NoOpClass ()), Expression.Constant (1));
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (InvalidOperationException))]
64                 public void Boolean ()
65                 {
66                         Expression.RightShift (Expression.Constant (true), Expression.Constant (1));
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (InvalidOperationException))]
71                 public void Double ()
72                 {
73                         Expression.RightShift (Expression.Constant (2.0), Expression.Constant (1));
74                 }
75
76                 [Test]
77                 public void Integer ()
78                 {
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");
84                 }
85
86                 [Test]
87                 public void Nullable ()
88                 {
89                         int? a = 1;
90                         int? b = 2;
91
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");
97                 }
98
99                 [Test]
100                 public void UserDefinedClass ()
101                 {
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");
105
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");
113                 }
114
115                 [Test]
116                 public void CompileRightShift ()
117                 {
118                         var l = Expression.Parameter (typeof (int), "l");
119                         var r = Expression.Parameter (typeof (int), "r");
120
121                         var rs = Expression.Lambda<Func<int, int, int>> (
122                                 Expression.RightShift (l, r), l, r).Compile ();
123
124                         Assert.AreEqual (3, rs (6, 1));
125                         Assert.AreEqual (1, rs (12, 3));
126                 }
127
128                 [Test]
129                 public void RightShiftNullableLongAndInt ()
130                 {
131                         var l = Expression.Parameter (typeof (long?), "l");
132                         var r = Expression.Parameter (typeof (int), "r");
133
134                         var node = Expression.RightShift (l, r);
135                         Assert.IsTrue (node.IsLifted);
136                         Assert.IsTrue (node.IsLiftedToNull);
137                         Assert.AreEqual (typeof (long?), node.Type);
138                 }
139         }
140 }