Merge pull request #717 from mono/client_websockets_impl
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Or.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_Or
33         {
34                 [Test]
35                 [ExpectedException (typeof (ArgumentNullException))]
36                 public void Arg1Null ()
37                 {
38                         Expression.Or (null, Expression.Constant (1));
39                 }
40
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void Arg2Null ()
44                 {
45                         Expression.Or (Expression.Constant (1), null);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (InvalidOperationException))]
50                 public void NoOperatorClass ()
51                 {
52                         Expression.Or (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (InvalidOperationException))]
57                 public void ArgTypesDifferent ()
58                 {
59                         Expression.Or (Expression.Constant (1), Expression.Constant (true));
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (InvalidOperationException))]
64                 public void Double ()
65                 {
66                         Expression.Or (Expression.Constant (1.0), Expression.Constant (2.0));
67                 }
68
69                 [Test]
70                 public void Integer ()
71                 {
72                         BinaryExpression expr = Expression.Or (Expression.Constant (1), Expression.Constant (2));
73                         Assert.AreEqual (ExpressionType.Or, expr.NodeType, "Or#01");
74                         Assert.AreEqual (typeof (int), expr.Type, "Or#02");
75                         Assert.IsNull (expr.Method, "Or#03");
76                         Assert.AreEqual ("(1 | 2)", expr.ToString(), "Or#04");
77                 }
78
79                 [Test]
80                 public void Boolean ()
81                 {
82                         BinaryExpression expr = Expression.Or (Expression.Constant (true), Expression.Constant (false));
83                         Assert.AreEqual (ExpressionType.Or, expr.NodeType, "Or#05");
84                         Assert.AreEqual (typeof (bool), expr.Type, "Or#06");
85                         Assert.IsNull (expr.Method, "Or#07");
86                         Assert.AreEqual ("(True Or False)", expr.ToString(), "Or#08");
87                 }
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_BitwiseOr");
95
96                         BinaryExpression expr = Expression.Or (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
97                         Assert.AreEqual (ExpressionType.Or, expr.NodeType, "Or#09");
98                         Assert.AreEqual (typeof (OpClass), expr.Type, "Or#10");
99                         Assert.AreEqual (mi, expr.Method, "Or#11");
100                         Assert.AreEqual ("op_BitwiseOr", expr.Method.Name, "Or#12");
101                         Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) | value(MonoTests.System.Linq.Expressions.OpClass))",
102                                 expr.ToString(), "Or#13");
103                 }
104
105                 [Test]
106                 public void OrBoolTest ()
107                 {
108                         var a = Expression.Parameter (typeof (bool), "a");
109                         var b = Expression.Parameter (typeof (bool), "b");
110                         var l = Expression.Lambda<Func<bool, bool, bool>> (
111                                 Expression.Or (a, b), a, b);
112
113                         var be = l.Body as BinaryExpression;
114                         Assert.IsNotNull (be);
115                         Assert.AreEqual (typeof (bool), be.Type);
116                         Assert.IsFalse (be.IsLifted);
117                         Assert.IsFalse (be.IsLiftedToNull);
118
119                         var c = l.Compile ();
120
121                         Assert.AreEqual (true,  c (true, true), "o1");
122                         Assert.AreEqual (true,  c (true, false), "o2");
123                         Assert.AreEqual (true,  c (false, true), "o3");
124                         Assert.AreEqual (false, c (false, false), "o4");
125                 }
126
127                 [Test]
128                 public void OrBoolNullableTest ()
129                 {
130                         var a = Expression.Parameter (typeof (bool?), "a");
131                         var b = Expression.Parameter (typeof (bool?), "b");
132                         var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
133                                 Expression.Or (a, b), a, b);
134
135                         var be = l.Body as BinaryExpression;
136                         Assert.IsNotNull (be);
137                         Assert.AreEqual (typeof (bool?), be.Type);
138                         Assert.IsTrue (be.IsLifted);
139                         Assert.IsTrue (be.IsLiftedToNull);
140
141                         var c = l.Compile ();
142
143                         Assert.AreEqual (true,  c (true, true),   "o1");
144                         Assert.AreEqual (true,  c (true, false),  "o2");
145                         Assert.AreEqual (true,  c (false, true),  "o3");
146                         Assert.AreEqual (false, c (false, false), "o4");
147
148                         Assert.AreEqual (true, c (true, null),  "o5");
149                         Assert.AreEqual (null, c (false, null), "o6");
150                         Assert.AreEqual (null, c (null, false), "o7");
151                         Assert.AreEqual (true, c (true, null),  "o8");
152                         Assert.AreEqual (null, c (null, null),  "o9");
153                 }
154
155                 [Test]
156                 public void OrBoolItem ()
157                 {
158                         var i = Expression.Parameter (typeof (Item<bool>), "i");
159                         var and = Expression.Lambda<Func<Item<bool>, bool>> (
160                                 Expression.Or (
161                                         Expression.Property (i, "Left"),
162                                         Expression.Property (i, "Right")), i).Compile ();
163
164                         var item = new Item<bool> (true, false);
165                         Assert.AreEqual (true, and (item));
166                         Assert.IsTrue (item.LeftCalled);
167                         Assert.IsTrue (item.RightCalled);
168                 }
169
170                 [Test]
171                 public void OrNullableBoolItem ()
172                 {
173                         var i = Expression.Parameter (typeof (Item<bool?>), "i");
174                         var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
175                                 Expression.Or (
176                                         Expression.Property (i, "Left"),
177                                         Expression.Property (i, "Right")), i).Compile ();
178
179                         var item = new Item<bool?> (true, false);
180                         Assert.AreEqual ((bool?) true, and (item));
181                         Assert.IsTrue (item.LeftCalled);
182                         Assert.IsTrue (item.RightCalled);
183                 }
184
185                 [Test]
186                 public void OrIntTest ()
187                 {
188                         var a = Expression.Parameter (typeof (int), "a");
189                         var b = Expression.Parameter (typeof (int), "b");
190                         var or = Expression.Lambda<Func<int, int, int>> (
191                                 Expression.Or (a, b), a, b).Compile ();
192
193                         Assert.AreEqual ((int?) 1, or (1, 1), "o1");
194                         Assert.AreEqual ((int?) 1, or (1, 0), "o2");
195                         Assert.AreEqual ((int?) 1, or (0, 1), "o3");
196                         Assert.AreEqual ((int?) 0, or (0, 0), "o4");
197                 }
198
199                 [Test]
200                 public void OrIntNullableTest ()
201                 {
202                         var a = Expression.Parameter (typeof (int?), "a");
203                         var b = Expression.Parameter (typeof (int?), "b");
204                         var c = Expression.Lambda<Func<int?, int?, int?>> (
205                                 Expression.Or (a, b), a, b).Compile ();
206
207                         Assert.AreEqual ((int?) 1, c (1, 1), "o1");
208                         Assert.AreEqual ((int?) 1, c (1, 0), "o2");
209                         Assert.AreEqual ((int?) 1, c (0, 1), "o3");
210                         Assert.AreEqual ((int?) 0, c (0, 0), "o4");
211
212                         Assert.AreEqual ((int?) null, c (1, null), "o5");
213                         Assert.AreEqual ((int?) null, c (0, null), "o6");
214                         Assert.AreEqual ((int?) null, c (null, 0), "o7");
215                         Assert.AreEqual ((int?) null, c (1, null), "o8");
216                         Assert.AreEqual ((int?) null, c (null, null), "o9");
217                 }
218         }
219 }