Bug 15572. Lookup KnownTypeCollection element types in MSSimpleNamespace
[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                 public void AndAlsoTestNullable ()
154                 {
155                         var a = Expression.Parameter (typeof (bool?), "a");
156                         var b = Expression.Parameter (typeof (bool?), "b");
157                         var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
158                                 Expression.AndAlso (a, b), a, b);
159
160                         var be = l.Body as BinaryExpression;
161                         Assert.IsNotNull (be);
162                         Assert.AreEqual (typeof (bool?), be.Type);
163                         Assert.IsTrue (be.IsLifted);
164                         Assert.IsTrue (be.IsLiftedToNull);
165
166                         var c = l.Compile ();
167
168                         Assert.AreEqual (true,  c (true, true), "a1");
169                         Assert.AreEqual (false, c (true, false), "a2");
170                         Assert.AreEqual (false, c (false, true), "a3");
171                         Assert.AreEqual (false, c (false, false), "a4");
172
173                         Assert.AreEqual (null,  c (true, null), "a5");
174                         Assert.AreEqual (false, c (false, null), "a6");
175                         Assert.AreEqual (false, c (null, false), "a7");
176                         Assert.AreEqual (null,  c (true, null), "a8");
177                         Assert.AreEqual (null,  c (null, null), "a9");
178                 }
179
180                 [Test]
181                 public void AndAlsoBoolItem ()
182                 {
183                         var i = Expression.Parameter (typeof (Item<bool>), "i");
184                         var and = Expression.Lambda<Func<Item<bool>, bool>> (
185                                 Expression.AndAlso (
186                                         Expression.Property (i, "Left"),
187                                         Expression.Property (i, "Right")), i).Compile ();
188
189                         var item = new Item<bool> (false, true);
190                         Assert.AreEqual (false, and (item));
191                         Assert.IsTrue (item.LeftCalled);
192                         Assert.IsFalse (item.RightCalled);
193                 }
194
195                 [Test]
196                 public void AndAlsoNullableBoolItem ()
197                 {
198                         var i = Expression.Parameter (typeof (Item<bool?>), "i");
199                         var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
200                                 Expression.AndAlso (
201                                         Expression.Property (i, "Left"),
202                                         Expression.Property (i, "Right")), i).Compile ();
203
204                         var item = new Item<bool?> (false, true);
205                         Assert.AreEqual ((bool?) false, and (item));
206                         Assert.IsTrue (item.LeftCalled);
207                         Assert.IsFalse (item.RightCalled);
208                 }
209
210                 struct Slot {
211
212                         public int Value;
213
214                         public Slot (int val)
215                         {
216                                 this.Value = val;
217                         }
218
219                         public static Slot operator & (Slot a, Slot b)
220                         {
221                                 return new Slot (a.Value & b.Value);
222                         }
223
224                         public static bool operator true (Slot a)
225                         {
226                                 return a.Value != 0;
227                         }
228
229                         public static bool operator false (Slot a)
230                         {
231                                 return a.Value == 0;
232                         }
233
234                         public override string ToString ()
235                         {
236                                 return Value.ToString ();
237                         }
238                 }
239
240                 [Test]
241                 public void UserDefinedAndAlso ()
242                 {
243                         var l = Expression.Parameter (typeof (Slot), "l");
244                         var r = Expression.Parameter (typeof (Slot), "r");
245
246                         var method = typeof (Slot).GetMethod ("op_BitwiseAnd");
247
248                         var node = Expression.AndAlso (l, r, method);
249                         Assert.IsFalse (node.IsLifted);
250                         Assert.IsFalse (node.IsLiftedToNull);
251                         Assert.AreEqual (method, node.Method);
252
253                         var andalso = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
254
255                         Assert.AreEqual (new Slot (64), andalso (new Slot (64), new Slot (64)));
256                         Assert.AreEqual (new Slot (0), andalso (new Slot (32), new Slot (64)));
257                         Assert.AreEqual (new Slot (0), andalso (new Slot (64), new Slot (32)));
258                 }
259
260                 [Test]
261                 public void UserDefinedAndAlsoShortCircuit ()
262                 {
263                         var i = Expression.Parameter (typeof (Item<Slot>), "i");
264                         var and = Expression.Lambda<Func<Item<Slot>, Slot>> (
265                                 Expression.AndAlso (
266                                         Expression.Property (i, "Left"),
267                                         Expression.Property (i, "Right")), i).Compile ();
268
269                         var item = new Item<Slot> (new Slot (0), new Slot (1));
270                         Assert.AreEqual (new Slot (0), and (item));
271                         Assert.IsTrue (item.LeftCalled);
272                         Assert.IsFalse (item.RightCalled);
273                 }
274
275                 [Test]
276                 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
277                 public void UserDefinedLiftedAndAlsoShortCircuit ()
278                 {
279                         var i = Expression.Parameter (typeof (Item<Slot?>), "i");
280                         var and = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
281                                 Expression.AndAlso (
282                                         Expression.Property (i, "Left"),
283                                         Expression.Property (i, "Right")), i).Compile ();
284
285                         var item = new Item<Slot?> (null, new Slot (1));
286                         Assert.AreEqual ((Slot?) null, and (item));
287                         Assert.IsTrue (item.LeftCalled);
288                         Assert.IsFalse (item.RightCalled);
289                 }
290
291                 [Test]
292                 public void UserDefinedAndAlsoLiftedToNull ()
293                 {
294                         var l = Expression.Parameter (typeof (Slot?), "l");
295                         var r = Expression.Parameter (typeof (Slot?), "r");
296
297                         var method = typeof (Slot).GetMethod ("op_BitwiseAnd");
298
299                         var node = Expression.AndAlso (l, r, method);
300                         Assert.IsTrue (node.IsLifted);
301                         Assert.IsTrue (node.IsLiftedToNull);
302                         Assert.AreEqual (method, node.Method);
303
304                         var andalso = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
305
306                         Assert.AreEqual (new Slot (64), andalso (new Slot (64), new Slot (64)));
307                         Assert.AreEqual (new Slot (0), andalso (new Slot (32), new Slot (64)));
308                         Assert.AreEqual (new Slot (0), andalso (new Slot (64), new Slot (32)));
309                         Assert.AreEqual (null, andalso (null, new Slot (32)));
310                         Assert.AreEqual (null, andalso (new Slot (64), null));
311                         Assert.AreEqual (null, andalso (null, null));
312                 }
313
314                 struct Incomplete {
315                         public int Value;
316
317                         public Incomplete (int val)
318                         {
319                                 Value = val;
320                         }
321
322                         public static Incomplete operator & (Incomplete a, Incomplete b)
323                         {
324                                 return new Incomplete (a.Value & b.Value);
325                         }
326                 }
327
328                 [Test]
329                 [ExpectedException (typeof (ArgumentException))]
330                 public void IncompleteUserDefinedAndAlso ()
331                 {
332                         var l = Expression.Parameter (typeof (Incomplete), "l");
333                         var r = Expression.Parameter (typeof (Incomplete), "r");
334
335                         var method = typeof (Incomplete).GetMethod ("op_BitwiseAnd");
336
337                         Expression.AndAlso (l, r, method);
338                 }
339
340                 class A {
341                         public static bool operator true (A x)
342                         {
343                                 return true;
344                         }
345
346                         public static bool operator false (A x)
347                         {
348                                 return false;
349                         }
350                 }
351
352                 class B : A {
353                         public static B operator & (B x, B y)
354                         {
355                                 return new B ();
356                         }
357
358                         public static bool op_True<T> (B x)
359                         {
360                                 return true;
361                         }
362
363                         public static bool op_False (B x)
364                         {
365                                 return false;
366                         }
367                 }
368
369                 [Test] // from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350487
370                 public void Connect350487 ()
371                 {
372                         var p = Expression.Parameter (typeof (B), "b");
373                         var l = Expression.Lambda<Func<B, A>> (
374                                 Expression.AndAlso (p, p), p).Compile ();
375
376                         Assert.IsNotNull (l (null));
377                 }
378         }
379 }