Merge pull request #778 from cmorris98/master
[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                 [Category ("NotWorkingInterpreter")]
184                 public void OrElseTestNullable ()
185                 {
186                         var a = Expression.Parameter (typeof (bool?), "a");
187                         var b = Expression.Parameter (typeof (bool?), "b");
188                         var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
189                                 Expression.OrElse (a, b), a, b);
190
191                         var be = l.Body as BinaryExpression;
192                         Assert.IsNotNull (be);
193                         Assert.AreEqual (typeof (bool?), be.Type);
194                         Assert.IsTrue (be.IsLifted);
195                         Assert.IsTrue (be.IsLiftedToNull);
196
197                         var c = l.Compile ();
198
199                         Assert.AreEqual (true,  c (true, true),   "o1");
200                         Assert.AreEqual (true,  c (true, false),  "o2");
201                         Assert.AreEqual (true,  c (false, true),  "o3");
202                         Assert.AreEqual (false, c (false, false), "o4");
203
204                         Assert.AreEqual (true, c (true, null),  "o5");
205                         Assert.AreEqual (null, c (false, null), "o6");
206                         Assert.AreEqual (null, c (null, false), "o7");
207                         Assert.AreEqual (true, c (true, null),  "o8");
208                         Assert.AreEqual (null, c (null, null),  "o9");
209                 }
210
211                 [Test]
212                 public void OrElseBoolItem ()
213                 {
214                         var i = Expression.Parameter (typeof (Item<bool>), "i");
215                         var and = Expression.Lambda<Func<Item<bool>, bool>> (
216                                 Expression.OrElse (
217                                         Expression.Property (i, "Left"),
218                                         Expression.Property (i, "Right")), i).Compile ();
219
220                         var item = new Item<bool> (true, false);
221                         Assert.AreEqual (true, and (item));
222                         Assert.IsTrue (item.LeftCalled);
223                         Assert.IsFalse (item.RightCalled);
224                 }
225
226                 [Test]
227                 [Category ("NotWorkingInterpreter")]
228                 public void OrElseNullableBoolItem ()
229                 {
230                         var i = Expression.Parameter (typeof (Item<bool?>), "i");
231                         var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
232                                 Expression.OrElse (
233                                         Expression.Property (i, "Left"),
234                                         Expression.Property (i, "Right")), i).Compile ();
235
236                         var item = new Item<bool?> (true, false);
237                         Assert.AreEqual ((bool?) true, and (item));
238                         Assert.IsTrue (item.LeftCalled);
239                         Assert.IsFalse (item.RightCalled);
240                 }
241
242                 struct Slot {
243
244                         public int Value;
245
246                         public Slot (int val)
247                         {
248                                 this.Value = val;
249                         }
250
251                         public static Slot operator | (Slot a, Slot b)
252                         {
253                                 return new Slot (a.Value | b.Value);
254                         }
255
256                         public static bool operator true (Slot a)
257                         {
258                                 return a.Value != 0;
259                         }
260
261                         public static bool operator false (Slot a)
262                         {
263                                 return a.Value == 0;
264                         }
265                 }
266
267                 [Test]
268                 [Category ("NotWorkingInterpreter")]
269                 public void UserDefinedOrElse ()
270                 {
271                         var l = Expression.Parameter (typeof (Slot), "l");
272                         var r = Expression.Parameter (typeof (Slot), "r");
273
274                         var method = typeof (Slot).GetMethod ("op_BitwiseOr");
275
276                         var node = Expression.OrElse (l, r, method);
277                         Assert.IsFalse (node.IsLifted);
278                         Assert.IsFalse (node.IsLiftedToNull);
279                         Assert.AreEqual (method, node.Method);
280
281                         var orelse = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
282
283                         Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
284                         Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
285                 }
286 #if !NET_4_0 // dlr bug 5867
287                 [Test]
288                 public void UserDefinedOrElseLiftedToNull ()
289                 {
290                         var l = Expression.Parameter (typeof (Slot?), "l");
291                         var r = Expression.Parameter (typeof (Slot?), "r");
292
293                         var method = typeof (Slot).GetMethod ("op_BitwiseOr");
294
295                         var node = Expression.OrElse (l, r, method);
296                         Assert.IsTrue (node.IsLifted);
297                         Assert.IsTrue (node.IsLiftedToNull);
298                         Assert.AreEqual (method, node.Method);
299
300                         var orelse = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
301
302                         Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
303                         Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
304                         Assert.AreEqual (new Slot (64), orelse (null, new Slot (64)));
305                         Assert.AreEqual (new Slot (32), orelse (new Slot (32), null));
306                         Assert.AreEqual (null, orelse (null, null));
307                 }
308 #endif
309                 [Test]
310                 [Category ("NotWorkingInterpreter")]
311                 public void UserDefinedOrElseShortCircuit ()
312                 {
313                         var i = Expression.Parameter (typeof (Item<Slot>), "i");
314                         var orelse = Expression.Lambda<Func<Item<Slot>, Slot>> (
315                                 Expression.OrElse (
316                                         Expression.Property (i, "Left"),
317                                         Expression.Property (i, "Right")), i).Compile ();
318
319                         var item = new Item<Slot> (new Slot (1), new Slot (0));
320                         Assert.AreEqual (new Slot (1), orelse (item));
321                         Assert.IsTrue (item.LeftCalled);
322                         Assert.IsFalse (item.RightCalled);
323                 }
324
325                 [Test]
326                 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
327                 [Category ("NotWorkingInterpreter")]
328                 public void UserDefinedLiftedOrElseShortCircuit ()
329                 {
330                         var i = Expression.Parameter (typeof (Item<Slot?>), "i");
331                         var orelse = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
332                                 Expression.OrElse (
333                                         Expression.Property (i, "Left"),
334                                         Expression.Property (i, "Right")), i).Compile ();
335
336                         var item = new Item<Slot?> (new Slot (1), null);
337                         Assert.AreEqual ((Slot?) new Slot (1), orelse (item));
338                         Assert.IsTrue (item.LeftCalled);
339                         Assert.IsFalse (item.RightCalled);
340                 }
341
342                 struct Incomplete {
343                         public int Value;
344
345                         public Incomplete (int val)
346                         {
347                                 Value = val;
348                         }
349
350                         public static Incomplete operator | (Incomplete a, Incomplete b)
351                         {
352                                 return new Incomplete (a.Value | b.Value);
353                         }
354                 }
355
356                 [Test]
357                 [ExpectedException (typeof (ArgumentException))]
358                 public void IncompleteUserDefinedOrElse ()
359                 {
360                         var l = Expression.Parameter (typeof (Incomplete), "l");
361                         var r = Expression.Parameter (typeof (Incomplete), "r");
362
363                         var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");
364
365                         Expression.OrElse (l, r, method);
366                 }
367         }
368 }