2010-03-12 Jb Evain <jbevain@novell.com>
[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                         Assert.AreEqual ("(True || False)", expr.ToString(), "OrElse#04");
84                 }
85
86                 [Test]
87                 public void UserDefinedClass ()
88                 {
89                         // We can use the simplest version of GetMethod because we already know only one
90                         // exists in the very simple class we're using for the tests.
91                         MethodInfo mi = typeof (OpClass).GetMethod ("op_BitwiseOr");
92
93                         BinaryExpression expr = Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
94                         Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#05");
95                         Assert.AreEqual (typeof (OpClass), expr.Type, "OrElse#06");
96                         Assert.AreEqual (mi, expr.Method, "OrElse#07");
97                         Assert.AreEqual ("op_BitwiseOr", expr.Method.Name, "OrElse#08");
98                         Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) || value(MonoTests.System.Linq.Expressions.OpClass))",
99                                 expr.ToString(), "OrElse#09");
100                 }
101
102                 public class BrokenMethod {
103                         public static int operator | (BrokenMethod a, BrokenMethod b)
104                         {
105                                 return 1;
106                         }
107                 }
108
109                 public class BrokenMethod2 {
110                         public static BrokenMethod2 operator | (BrokenMethod2 a, int b)
111                         {
112                                 return null;
113                         }
114                 }
115
116                 [Test]
117                 [ExpectedException(typeof(ArgumentException))]
118                 public void MethodInfoReturnType ()
119                 {
120                         Expression.OrElse (Expression.Constant (new BrokenMethod ()),
121                                            Expression.Constant (new BrokenMethod ()));
122                 }
123
124                 [Test]
125                 [ExpectedException(typeof(ArgumentException))]
126                 public void MethodInfoReturnType2 ()
127                 {
128                         Expression.OrElse (Expression.Constant (new BrokenMethod2 ()),
129                                            Expression.Constant (1));
130                 }
131
132                 [Test]
133                 public void OrElseNotLifted ()
134                 {
135                         var b = Expression.OrElse (
136                                 Expression.Constant (true, typeof (bool)),
137                                 Expression.Constant (true, typeof (bool)));
138
139                         Assert.AreEqual (typeof (bool), b.Type);
140                         Assert.IsFalse (b.IsLifted);
141                         Assert.IsFalse (b.IsLiftedToNull);
142                 }
143
144                 [Test]
145                 public void OrElseTest ()
146                 {
147                         var a = Expression.Parameter (typeof (bool), "a");
148                         var b = Expression.Parameter (typeof (bool), "b");
149                         var l = Expression.Lambda<Func<bool, bool, bool>> (
150                                 Expression.OrElse (a, b), a, b);
151
152                         var be = l.Body as BinaryExpression;
153                         Assert.IsNotNull (be);
154                         Assert.AreEqual (typeof (bool), be.Type);
155                         Assert.IsFalse (be.IsLifted);
156                         Assert.IsFalse (be.IsLiftedToNull);
157
158                         var c = l.Compile ();
159
160                         Assert.AreEqual (true,  c (true, true), "o1");
161                         Assert.AreEqual (true,  c (true, false), "o2");
162                         Assert.AreEqual (true,  c (false, true), "o3");
163                         Assert.AreEqual (false, c (false, false), "o4");
164                 }
165
166                 [Test]
167                 public void OrElseLifted ()
168                 {
169                         var b = Expression.OrElse (
170                                 Expression.Constant (null, typeof (bool?)),
171                                 Expression.Constant (null, typeof (bool?)));
172
173                         Assert.AreEqual (typeof (bool?), b.Type);
174                         Assert.IsTrue (b.IsLifted);
175                         Assert.IsTrue (b.IsLiftedToNull);
176                 }
177
178                 [Test]
179                 public void OrElseTestNullable ()
180                 {
181                         var a = Expression.Parameter (typeof (bool?), "a");
182                         var b = Expression.Parameter (typeof (bool?), "b");
183                         var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
184                                 Expression.OrElse (a, b), a, b);
185
186                         var be = l.Body as BinaryExpression;
187                         Assert.IsNotNull (be);
188                         Assert.AreEqual (typeof (bool?), be.Type);
189                         Assert.IsTrue (be.IsLifted);
190                         Assert.IsTrue (be.IsLiftedToNull);
191
192                         var c = l.Compile ();
193
194                         Assert.AreEqual (true,  c (true, true),   "o1");
195                         Assert.AreEqual (true,  c (true, false),  "o2");
196                         Assert.AreEqual (true,  c (false, true),  "o3");
197                         Assert.AreEqual (false, c (false, false), "o4");
198
199                         Assert.AreEqual (true, c (true, null),  "o5");
200                         Assert.AreEqual (null, c (false, null), "o6");
201                         Assert.AreEqual (null, c (null, false), "o7");
202                         Assert.AreEqual (true, c (true, null),  "o8");
203                         Assert.AreEqual (null, c (null, null),  "o9");
204                 }
205
206                 [Test]
207                 public void OrElseBoolItem ()
208                 {
209                         var i = Expression.Parameter (typeof (Item<bool>), "i");
210                         var and = Expression.Lambda<Func<Item<bool>, bool>> (
211                                 Expression.OrElse (
212                                         Expression.Property (i, "Left"),
213                                         Expression.Property (i, "Right")), i).Compile ();
214
215                         var item = new Item<bool> (true, false);
216                         Assert.AreEqual (true, and (item));
217                         Assert.IsTrue (item.LeftCalled);
218                         Assert.IsFalse (item.RightCalled);
219                 }
220
221                 [Test]
222                 public void OrElseNullableBoolItem ()
223                 {
224                         var i = Expression.Parameter (typeof (Item<bool?>), "i");
225                         var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
226                                 Expression.OrElse (
227                                         Expression.Property (i, "Left"),
228                                         Expression.Property (i, "Right")), i).Compile ();
229
230                         var item = new Item<bool?> (true, false);
231                         Assert.AreEqual ((bool?) true, and (item));
232                         Assert.IsTrue (item.LeftCalled);
233                         Assert.IsFalse (item.RightCalled);
234                 }
235
236                 struct Slot {
237
238                         public int Value;
239
240                         public Slot (int val)
241                         {
242                                 this.Value = val;
243                         }
244
245                         public static Slot operator | (Slot a, Slot b)
246                         {
247                                 return new Slot (a.Value | b.Value);
248                         }
249
250                         public static bool operator true (Slot a)
251                         {
252                                 return a.Value != 0;
253                         }
254
255                         public static bool operator false (Slot a)
256                         {
257                                 return a.Value == 0;
258                         }
259                 }
260
261                 [Test]
262                 public void UserDefinedOrElse ()
263                 {
264                         var l = Expression.Parameter (typeof (Slot), "l");
265                         var r = Expression.Parameter (typeof (Slot), "r");
266
267                         var method = typeof (Slot).GetMethod ("op_BitwiseOr");
268
269                         var node = Expression.OrElse (l, r, method);
270                         Assert.IsFalse (node.IsLifted);
271                         Assert.IsFalse (node.IsLiftedToNull);
272                         Assert.AreEqual (method, node.Method);
273
274                         var orelse = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
275
276                         Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
277                         Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
278                 }
279
280                 [Test]
281                 public void UserDefinedOrElseLiftedToNull ()
282                 {
283                         var l = Expression.Parameter (typeof (Slot?), "l");
284                         var r = Expression.Parameter (typeof (Slot?), "r");
285
286                         var method = typeof (Slot).GetMethod ("op_BitwiseOr");
287
288                         var node = Expression.OrElse (l, r, method);
289                         Assert.IsTrue (node.IsLifted);
290                         Assert.IsTrue (node.IsLiftedToNull);
291                         Assert.AreEqual (method, node.Method);
292
293                         var orelse = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
294
295                         Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
296                         Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
297                         Assert.AreEqual (new Slot (64), orelse (null, new Slot (64)));
298                         Assert.AreEqual (new Slot (32), orelse (new Slot (32), null));
299                         Assert.AreEqual (null, orelse (null, null));
300                 }
301
302                 [Test]
303                 public void UserDefinedOrElseShortCircuit ()
304                 {
305                         var i = Expression.Parameter (typeof (Item<Slot>), "i");
306                         var orelse = Expression.Lambda<Func<Item<Slot>, Slot>> (
307                                 Expression.OrElse (
308                                         Expression.Property (i, "Left"),
309                                         Expression.Property (i, "Right")), i).Compile ();
310
311                         var item = new Item<Slot> (new Slot (1), new Slot (0));
312                         Assert.AreEqual (new Slot (1), orelse (item));
313                         Assert.IsTrue (item.LeftCalled);
314                         Assert.IsFalse (item.RightCalled);
315                 }
316
317                 [Test]
318                 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
319                 public void UserDefinedLiftedOrElseShortCircuit ()
320                 {
321                         var i = Expression.Parameter (typeof (Item<Slot?>), "i");
322                         var orelse = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
323                                 Expression.OrElse (
324                                         Expression.Property (i, "Left"),
325                                         Expression.Property (i, "Right")), i).Compile ();
326
327                         var item = new Item<Slot?> (new Slot (1), null);
328                         Assert.AreEqual ((Slot?) new Slot (1), orelse (item));
329                         Assert.IsTrue (item.LeftCalled);
330                         Assert.IsFalse (item.RightCalled);
331                 }
332
333                 struct Incomplete {
334                         public int Value;
335
336                         public Incomplete (int val)
337                         {
338                                 Value = val;
339                         }
340
341                         public static Incomplete operator | (Incomplete a, Incomplete b)
342                         {
343                                 return new Incomplete (a.Value | b.Value);
344                         }
345                 }
346
347                 [Test]
348                 [ExpectedException (typeof (ArgumentException))]
349                 public void IncompleteUserDefinedOrElse ()
350                 {
351                         var l = Expression.Parameter (typeof (Incomplete), "l");
352                         var r = Expression.Parameter (typeof (Incomplete), "r");
353
354                         var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");
355
356                         Expression.OrElse (l, r, method);
357                 }
358         }
359 }