Merge pull request #684 from jack-pappas/patch-7
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_NotEqual.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 //      Miguel de Icaza (miguel@novell.com)
21 //      Jb Evain (jbevain@novell.com)
22 //
23
24 using System;
25 using System.Reflection;
26 using System.Linq;
27 using System.Linq.Expressions;
28 using NUnit.Framework;
29
30 namespace MonoTests.System.Linq.Expressions
31 {
32         [TestFixture]
33         public class ExpressionTest_NotEqual
34         {
35                 [Test]
36                 [ExpectedException (typeof (ArgumentNullException))]
37                 public void Arg1Null ()
38                 {
39                         Expression.NotEqual (null, Expression.Constant (1));
40                 }
41
42                 [Test]
43                 [ExpectedException (typeof (ArgumentNullException))]
44                 public void Arg2Null ()
45                 {
46                         Expression.NotEqual (Expression.Constant (1), null);
47                 }
48
49                 [Test]
50                 [ExpectedException (typeof (InvalidOperationException))]
51                 public void ArgTypesDifferent ()
52                 {
53                         Expression.NotEqual (Expression.Constant (1), Expression.Constant (2.0));
54                 }
55
56                 [Test]
57                 public void ReferenceCompare ()
58                 {
59                         Expression.NotEqual (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
60                 }
61
62                 public struct D {
63                 }
64
65                 [Test]
66                 [ExpectedException (typeof (InvalidOperationException))]
67                 public void NoOperatorClass ()
68                 {
69                         Expression.NotEqual (Expression.Constant (new D ()), Expression.Constant (new D ()));
70                 }
71
72                 [Test]
73                 public void Numeric ()
74                 {
75                         BinaryExpression expr = Expression.NotEqual (Expression.Constant (1), Expression.Constant (2));
76                         Assert.AreEqual (ExpressionType.NotEqual, expr.NodeType);
77                         Assert.AreEqual (typeof (bool), expr.Type);
78                         Assert.IsNull (expr.Method);
79                         Assert.AreEqual ("(1 != 2)", expr.ToString ());
80                 }
81
82                 [Test]
83                 public void Nullable_LiftToNull_SetToFalse ()
84                 {
85                         int? a = 1;
86                         int? b = 2;
87
88                         BinaryExpression expr = Expression.NotEqual (Expression.Constant (a, typeof(int?)),
89                                                                   Expression.Constant (b, typeof(int?)),
90                                                                   false, null);
91                         Assert.AreEqual (ExpressionType.NotEqual, expr.NodeType);
92                         Assert.AreEqual (typeof (bool), expr.Type);
93                         Assert.AreEqual (true, expr.IsLifted);
94                         Assert.AreEqual (false, expr.IsLiftedToNull);
95                         Assert.IsNull (expr.Method);
96                         Assert.AreEqual ("(1 != 2)", expr.ToString ());
97                 }
98
99                 [Test]
100                 public void Nullable_LiftToNull_SetToTrue ()
101                 {
102                         int? a = 1;
103                         int? b = 2;
104
105                         BinaryExpression expr = Expression.NotEqual (Expression.Constant (a, typeof(int?)),
106                                                                   Expression.Constant (b, typeof(int?)),
107                                                                   true, null);
108                         Assert.AreEqual (ExpressionType.NotEqual, expr.NodeType);
109                         Assert.AreEqual (typeof (bool?), expr.Type);
110                         Assert.AreEqual (true, expr.IsLifted);
111                         Assert.AreEqual (true, expr.IsLiftedToNull);
112                         Assert.IsNull (expr.Method);
113                         Assert.AreEqual ("(1 != 2)", expr.ToString ());
114                 }
115
116                 [Test]
117                 [ExpectedException(typeof (InvalidOperationException))]
118                 public void Nullable_Mixed ()
119                 {
120                         int? a = 1;
121                         int b = 2;
122
123                         Expression.NotEqual (Expression.Constant (a, typeof (int?)),
124                                           Expression.Constant (b, typeof (int)));
125                 }
126
127                 [Test]
128                 public void UserDefinedClass ()
129                 {
130                         // We can use the simplest version of GetMethod because we already know only one
131                         // exists in the very simple class we're using for the tests.
132                         MethodInfo mi = typeof (OpClass).GetMethod ("op_Inequality");
133
134                         BinaryExpression expr = Expression.NotEqual (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
135                         Assert.AreEqual (ExpressionType.NotEqual, expr.NodeType);
136                         Assert.AreEqual (typeof (bool), expr.Type);
137                         Assert.AreEqual (mi, expr.Method);
138                         Assert.AreEqual ("op_Inequality", expr.Method.Name);
139
140                         Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) != value(MonoTests.System.Linq.Expressions.OpClass))", expr.ToString ());
141                 }
142
143                 [Test]
144                 public void NullableInt32NotEqual ()
145                 {
146                         var l = Expression.Parameter (typeof (int?), "l");
147                         var r = Expression.Parameter (typeof (int?), "r");
148
149                         var neq = Expression.Lambda<Func<int?, int?, bool>> (
150                                 Expression.NotEqual (l, r), l, r).Compile ();
151
152                         Assert.IsFalse (neq (null, null));
153                         Assert.IsTrue (neq (null, 1));
154                         Assert.IsTrue (neq (1, null));
155                         Assert.IsTrue (neq (1, 2));
156                         Assert.IsFalse (neq (1, 1));
157                         Assert.IsTrue (neq (null, 0));
158                         Assert.IsTrue (neq (0, null));
159                 }
160
161                 [Test]
162                 public void NullableInt32NotEqualLiftedToNull ()
163                 {
164                         var l = Expression.Parameter (typeof (int?), "l");
165                         var r = Expression.Parameter (typeof (int?), "r");
166
167                         var neq = Expression.Lambda<Func<int?, int?, bool?>> (
168                                 Expression.NotEqual (l, r, true, null), l, r).Compile ();
169
170                         Assert.AreEqual ((bool?) null, neq (null, null));
171                         Assert.AreEqual ((bool?) null, neq (null, 1));
172                         Assert.AreEqual ((bool?) null, neq (1, null));
173                         Assert.AreEqual ((bool?) true, neq (1, 2));
174                         Assert.AreEqual ((bool?) false, neq (1, 1));
175                         Assert.AreEqual ((bool?) null, neq (null, 0));
176                         Assert.AreEqual ((bool?) null, neq (0, null));
177                 }
178
179
180                 public enum Foo {
181                         Bar,
182                         Baz,
183                 }
184
185                 [Test]
186                 public void EnumNotEqual ()
187                 {
188                         var l = Expression.Parameter (typeof (Foo), "l");
189                         var r = Expression.Parameter (typeof (Foo), "r");
190
191                         var node = Expression.NotEqual (l, r);
192                         Assert.IsFalse (node.IsLifted);
193                         Assert.IsFalse (node.IsLiftedToNull);
194                         Assert.AreEqual (typeof (bool), node.Type);
195                         Assert.IsNull (node.Method);
196
197                         var neq = Expression.Lambda<Func<Foo, Foo, bool>> (node, l, r).Compile ();
198
199                         Assert.AreEqual (false, neq (Foo.Bar, Foo.Bar));
200                         Assert.AreEqual (true, neq (Foo.Bar, Foo.Baz));
201                 }
202
203                 [Test]
204                 public void LiftedEnumNotEqual ()
205                 {
206                         var l = Expression.Parameter (typeof (Foo?), "l");
207                         var r = Expression.Parameter (typeof (Foo?), "r");
208
209                         var node = Expression.NotEqual (l, r);
210                         Assert.IsTrue (node.IsLifted);
211                         Assert.IsFalse (node.IsLiftedToNull);
212                         Assert.AreEqual (typeof (bool), node.Type);
213                         Assert.IsNull (node.Method);
214
215                         var neq = Expression.Lambda<Func<Foo?, Foo?, bool>> (node, l, r).Compile ();
216
217                         Assert.AreEqual (false, neq (Foo.Bar, Foo.Bar));
218                         Assert.AreEqual (true, neq (Foo.Bar, Foo.Baz));
219                         Assert.AreEqual (true, neq (Foo.Bar, null));
220                         Assert.AreEqual (false, neq (null, null));
221                 }
222         }
223 }