Flush (work in progress)
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_OrElse.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_OrElse
33         {
34                 [Test]
35                 [ExpectedException (typeof (ArgumentNullException))]
36                 public void Arg1Null ()
37                 {
38                         Expression.OrElse (null, Expression.Constant (1));
39                 }
40
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void Arg2Null ()
44                 {
45                         Expression.OrElse (Expression.Constant (1), null);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (InvalidOperationException))]
50                 public void NoOperatorClass ()
51                 {
52                         Expression.OrElse (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (InvalidOperationException))]
57                 public void Double ()
58                 {
59                         Expression.OrElse (Expression.Constant (1.0), Expression.Constant (2.0));
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (InvalidOperationException))]
64                 public void Integer ()
65                 {
66                         Expression.OrElse (Expression.Constant (1), Expression.Constant (2));
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (InvalidOperationException))]
71                 public void MismatchedTypes ()
72                 {
73                         Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (true));
74                 }
75
76                 [Test]
77                 public void Boolean ()
78                 {
79                         BinaryExpression expr = Expression.OrElse (Expression.Constant (true), Expression.Constant (false));
80                         Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#01");
81                         Assert.AreEqual (typeof (bool), expr.Type, "OrElse#02");
82                         Assert.IsNull (expr.Method, "OrElse#03");
83 #if !NET_4_0
84                         Assert.AreEqual ("(True || False)", expr.ToString(), "OrElse#04");
85 #endif
86                 }
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_BitwiseOr");
94
95                         BinaryExpression expr = Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
96                         Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#05");
97                         Assert.AreEqual (typeof (OpClass), expr.Type, "OrElse#06");
98                         Assert.AreEqual (mi, expr.Method, "OrElse#07");
99                         Assert.AreEqual ("op_BitwiseOr", expr.Method.Name, "OrElse#08");
100 #if !NET_4_0
101                         Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) || value(MonoTests.System.Linq.Expressions.OpClass))",
102                                 expr.ToString(), "OrElse#09");
103 #endif
104                 }
105
106                 public class BrokenMethod {
107                         public static int operator | (BrokenMethod a, BrokenMethod b)
108                         {
109                                 return 1;
110                         }
111                 }
112
113                 public class BrokenMethod2 {
114                         public static BrokenMethod2 operator | (BrokenMethod2 a, int b)
115                         {
116                                 return null;
117                         }
118                 }
119
120                 [Test]
121                 [ExpectedException(typeof(ArgumentException))]
122                 public void MethodInfoReturnType ()
123                 {
124                         Expression.OrElse (Expression.Constant (new BrokenMethod ()),
125                                            Expression.Constant (new BrokenMethod ()));
126                 }
127
128                 [Test]
129                 [ExpectedException(typeof(ArgumentException))]
130                 public void MethodInfoReturnType2 ()
131                 {
132                         Expression.OrElse (Expression.Constant (new BrokenMethod2 ()),
133                                            Expression.Constant (1));
134                 }
135
136                 [Test]
137                 public void OrElseNotLifted ()
138                 {
139                         var b = Expression.OrElse (
140                                 Expression.Constant (true, typeof (bool)),
141                                 Expression.Constant (true, typeof (bool)));
142
143                         Assert.AreEqual (typeof (bool), b.Type);
144                         Assert.IsFalse (b.IsLifted);
145                         Assert.IsFalse (b.IsLiftedToNull);
146                 }
147
148                 [Test]
149                 public void OrElseTest ()
150                 {
151                         var a = Expression.Parameter (typeof (bool), "a");
152                         var b = Expression.Parameter (typeof (bool), "b");
153                         var l = Expression.Lambda<Func<bool, bool, bool>> (
154                                 Expression.OrElse (a, b), a, b);
155
156                         var be = l.Body as BinaryExpression;
157                         Assert.IsNotNull (be);
158                         Assert.AreEqual (typeof (bool), be.Type);
159                         Assert.IsFalse (be.IsLifted);
160                         Assert.IsFalse (be.IsLiftedToNull);
161
162                         var c = l.Compile ();
163
164                         Assert.AreEqual (true,  c (true, true), "o1");
165                         Assert.AreEqual (true,  c (true, false), "o2");
166                         Assert.AreEqual (true,  c (false, true), "o3");
167                         Assert.AreEqual (false, c (false, false), "o4");
168                 }
169
170                 [Test]
171                 public void OrElseLifted ()
172                 {
173                         var b = Expression.OrElse (
174                                 Expression.Constant (null, typeof (bool?)),
175                                 Expression.Constant (null, typeof (bool?)));
176
177                         Assert.AreEqual (typeof (bool?), b.Type);
178                         Assert.IsTrue (b.IsLifted);
179                         Assert.IsTrue (b.IsLiftedToNull);
180                 }
181
182                 [Test]
183                 public void OrElseTestNullable ()
184                 {
185                         var a = Expression.Parameter (typeof (bool?), "a");
186                         var b = Expression.Parameter (typeof (bool?), "b");
187                         var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
188                                 Expression.OrElse (a, b), a, b);
189
190                         var be = l.Body as BinaryExpression;
191                         Assert.IsNotNull (be);
192                         Assert.AreEqual (typeof (bool?), be.Type);
193                         Assert.IsTrue (be.IsLifted);
194                         Assert.IsTrue (be.IsLiftedToNull);
195
196                         var c = l.Compile ();
197
198                         Assert.AreEqual (true,  c (true, true),   "o1");
199                         Assert.AreEqual (true,  c (true, false),  "o2");
200                         Assert.AreEqual (true,  c (false, true),  "o3");
201                         Assert.AreEqual (false, c (false, false), "o4");
202
203                         Assert.AreEqual (true, c (true, null),  "o5");
204                         Assert.AreEqual (null, c (false, null), "o6");
205                         Assert.AreEqual (null, c (null, false), "o7");
206                         Assert.AreEqual (true, c (true, null),  "o8");
207                         Assert.AreEqual (null, c (null, null),  "o9");
208                 }
209
210                 [Test]
211                 public void OrElseBoolItem ()
212                 {
213                         var i = Expression.Parameter (typeof (Item<bool>), "i");
214                         var and = Expression.Lambda<Func<Item<bool>, bool>> (
215                                 Expression.OrElse (
216                                         Expression.Property (i, "Left"),
217                                         Expression.Property (i, "Right")), i).Compile ();
218
219                         var item = new Item<bool> (true, false);
220                         Assert.AreEqual (true, and (item));
221                         Assert.IsTrue (item.LeftCalled);
222                         Assert.IsFalse (item.RightCalled);
223                 }
224
225                 [Test]
226                 public void OrElseNullableBoolItem ()
227                 {
228                         var i = Expression.Parameter (typeof (Item<bool?>), "i");
229                         var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
230                                 Expression.OrElse (
231                                         Expression.Property (i, "Left"),
232                                         Expression.Property (i, "Right")), i).Compile ();
233
234                         var item = new Item<bool?> (true, false);
235                         Assert.AreEqual ((bool?) true, and (item));
236                         Assert.IsTrue (item.LeftCalled);
237                         Assert.IsFalse (item.RightCalled);
238                 }
239
240                 struct Slot {
241
242                         public int Value;
243
244                         public Slot (int val)
245                         {
246                                 this.Value = val;
247                         }
248
249                         public static Slot operator | (Slot a, Slot b)
250                         {
251                                 return new Slot (a.Value | b.Value);
252                         }
253
254                         public static bool operator true (Slot a)
255                         {
256                                 return a.Value != 0;
257                         }
258
259                         public static bool operator false (Slot a)
260                         {
261                                 return a.Value == 0;
262                         }
263                 }
264
265                 [Test]
266                 public void UserDefinedOrElse ()
267                 {
268                         var l = Expression.Parameter (typeof (Slot), "l");
269                         var r = Expression.Parameter (typeof (Slot), "r");
270
271                         var method = typeof (Slot).GetMethod ("op_BitwiseOr");
272
273                         var node = Expression.OrElse (l, r, method);
274                         Assert.IsFalse (node.IsLifted);
275                         Assert.IsFalse (node.IsLiftedToNull);
276                         Assert.AreEqual (method, node.Method);
277
278                         var orelse = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
279
280                         Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
281                         Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
282                 }
283 #if !NET_4_0 // dlr bug 5867
284                 [Test]
285                 public void UserDefinedOrElseLiftedToNull ()
286                 {
287                         var l = Expression.Parameter (typeof (Slot?), "l");
288                         var r = Expression.Parameter (typeof (Slot?), "r");
289
290                         var method = typeof (Slot).GetMethod ("op_BitwiseOr");
291
292                         var node = Expression.OrElse (l, r, method);
293                         Assert.IsTrue (node.IsLifted);
294                         Assert.IsTrue (node.IsLiftedToNull);
295                         Assert.AreEqual (method, node.Method);
296
297                         var orelse = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
298
299                         Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
300                         Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
301                         Assert.AreEqual (new Slot (64), orelse (null, new Slot (64)));
302                         Assert.AreEqual (new Slot (32), orelse (new Slot (32), null));
303                         Assert.AreEqual (null, orelse (null, null));
304                 }
305 #endif
306                 [Test]
307                 public void UserDefinedOrElseShortCircuit ()
308                 {
309                         var i = Expression.Parameter (typeof (Item<Slot>), "i");
310                         var orelse = Expression.Lambda<Func<Item<Slot>, Slot>> (
311                                 Expression.OrElse (
312                                         Expression.Property (i, "Left"),
313                                         Expression.Property (i, "Right")), i).Compile ();
314
315                         var item = new Item<Slot> (new Slot (1), new Slot (0));
316                         Assert.AreEqual (new Slot (1), orelse (item));
317                         Assert.IsTrue (item.LeftCalled);
318                         Assert.IsFalse (item.RightCalled);
319                 }
320
321                 [Test]
322                 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
323                 public void UserDefinedLiftedOrElseShortCircuit ()
324                 {
325                         var i = Expression.Parameter (typeof (Item<Slot?>), "i");
326                         var orelse = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
327                                 Expression.OrElse (
328                                         Expression.Property (i, "Left"),
329                                         Expression.Property (i, "Right")), i).Compile ();
330
331                         var item = new Item<Slot?> (new Slot (1), null);
332                         Assert.AreEqual ((Slot?) new Slot (1), orelse (item));
333                         Assert.IsTrue (item.LeftCalled);
334                         Assert.IsFalse (item.RightCalled);
335                 }
336
337                 struct Incomplete {
338                         public int Value;
339
340                         public Incomplete (int val)
341                         {
342                                 Value = val;
343                         }
344
345                         public static Incomplete operator | (Incomplete a, Incomplete b)
346                         {
347                                 return new Incomplete (a.Value | b.Value);
348                         }
349                 }
350
351                 [Test]
352                 [ExpectedException (typeof (ArgumentException))]
353                 public void IncompleteUserDefinedOrElse ()
354                 {
355                         var l = Expression.Parameter (typeof (Incomplete), "l");
356                         var r = Expression.Parameter (typeof (Incomplete), "r");
357
358                         var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");
359
360                         Expression.OrElse (l, r, method);
361                 }
362         }
363 }