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