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