complete test
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Constant.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
22 using System;
23 using System.Linq;
24 using System.Linq.Expressions;
25 using NUnit.Framework;
26
27 namespace MonoTests.System.Linq.Expressions
28 {
29         [TestFixture]
30         public class ExpressionTest_Constant
31         {
32                 [Test]
33                 [ExpectedException (typeof (ArgumentException))]
34                 public void Arg2NotNullable ()
35                 {
36                         Expression.Constant(null, typeof(int));
37                 }
38
39                 [Test]
40                 [ExpectedException (typeof (ArgumentNullException))]
41                 public void Arg2Null ()
42                 {
43                         Expression.Constant (1, null);
44                 }
45
46                 [Test]
47                 public void NullValue ()
48                 {
49                         ConstantExpression expr = Expression.Constant (null);
50                         Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#01");
51                         Assert.IsNull (expr.Value, "Constant#02");
52                         Assert.AreEqual (typeof (object), expr.Type, "Constant#03");
53                         Assert.AreEqual ("null", expr.ToString(), "Constant#04");
54                 }
55
56                 [Test]
57                 public void NullableValue1 ()
58                 {
59                         ConstantExpression expr = Expression.Constant (null, typeof(int?));
60                         Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#05");
61                         Assert.IsNull (expr.Value, "Constant#06");
62                         Assert.AreEqual (typeof (int?), expr.Type, "Constant#07");
63                         Assert.AreEqual ("null", expr.ToString(), "Constant#08");
64                 }
65
66                 [Test]
67                 public void NullableValue2 ()
68                 {
69                         ConstantExpression expr = Expression.Constant (1, typeof (int?));
70                         Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#09");
71                         Assert.AreEqual (1, expr.Value, "Constant#10");
72                         Assert.AreEqual (typeof (int?), expr.Type, "Constant#11");
73                         Assert.AreEqual ("1", expr.ToString(), "Constant#12");
74                 }
75
76                 [Test]
77                 public void NullableValue3 ()
78                 {
79                         ConstantExpression expr = Expression.Constant ((int?)1);
80                         Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#13");
81                         Assert.AreEqual (1, expr.Value, "Constant#14");
82                         Assert.AreEqual (typeof (int), expr.Type, "Constant#15");
83                         Assert.AreEqual ("1", expr.ToString(), "Constant#16");
84                 }
85
86                 [Test]
87                 public void IntegerValue ()
88                 {
89                         ConstantExpression expr = Expression.Constant (0);
90                         Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#17");
91                         Assert.AreEqual (0, expr.Value, "Constant#18");
92                         Assert.AreEqual (typeof (int), expr.Type, "Constant#19");
93                         Assert.AreEqual ("0", expr.ToString(), "Constant#20");
94                 }
95
96                 [Test]
97                 public void StringValue ()
98                 {
99                         ConstantExpression expr = Expression.Constant ("a string");
100                         Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#21");
101                         Assert.AreEqual ("a string", expr.Value, "Constant#22");
102                         Assert.AreEqual (typeof (string), expr.Type, "Constant#23");
103                         Assert.AreEqual ("\"a string\"", expr.ToString(), "Constant#24");
104                 }
105
106                 [Test]
107                 public void DateTimeValue ()
108                 {
109                         ConstantExpression expr = Expression.Constant (new DateTime(1971, 10, 19));
110                         Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#25");
111                         Assert.AreEqual (new DateTime(1971, 10, 19), expr.Value, "Constant#26");
112                         Assert.AreEqual (typeof (DateTime), expr.Type, "Constant#27");
113                         Assert.AreEqual (new DateTime(1971, 10, 19).ToString(), expr.ToString(), "Constant#28");
114                 }
115
116                 [Test]
117                 public void UserClassValue ()
118                 {
119                         OpClass oc = new OpClass ();
120                         ConstantExpression expr = Expression.Constant (oc);
121                         Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#29");
122                         Assert.AreEqual (oc, expr.Value, "Constant#30");
123                         Assert.AreEqual (typeof (OpClass), expr.Type, "Constant#31");
124                         Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.OpClass)", expr.ToString(), "Constant#32");
125                 }
126
127                 [Test]
128                 [ExpectedException (typeof (ArgumentException))]
129                 public void TestInvalidCtor_1 ()
130                 {
131                         // null value, type == valuetype is invalid
132                         Expression.Constant (null, typeof (int));
133                 }
134
135                 [Test]
136                 [ExpectedException (typeof (ArgumentException))]
137                 public void TestInvalidCtor_2 ()
138                 {
139                         // type mismatch: int value, type == double
140                         Expression.Constant (0, typeof (double));
141                 }
142
143                 [Test]
144                 [ExpectedException (typeof (ArgumentException))]
145                 public void VoidConstant ()
146                 {
147                         Expression.Constant (null, typeof (void));
148                 }
149
150                 static T Check<T> (T val)
151                 {
152                         Expression<Func<T>> l = Expression.Lambda<Func<T>> (Expression.Constant (val), new ParameterExpression [0]);
153                         Func<T> fi = l.Compile ();
154                         return fi ();
155                 }
156
157                 [Test]
158                 public void NullableConstant_ToConstant ()
159                 {
160                         int? a = 1;
161                         ConstantExpression c = Expression.Constant (a);
162                         Assert.AreEqual (typeof (int), c.Type, "#1");
163                         Assert.AreEqual (1, c.Value, "#2");
164                 }
165
166                 [Test]
167                 public void ConstantCodeGen ()
168                 {
169                         Assert.AreEqual (Check<int> (0), 0, "int");
170                         Assert.AreEqual (Check<int> (128), 128, "int2");
171                         Assert.AreEqual (Check<int> (-128), -128, "int3");
172                         Assert.AreEqual (Check<int> (Int32.MinValue), Int32.MinValue, "int4");
173                         Assert.AreEqual (Check<int> (Int32.MaxValue), Int32.MaxValue, "int5");
174                         Assert.AreEqual (Check<uint> (128), 128, "uint");
175                         Assert.AreEqual (Check<uint> (0), 0, "uint2");
176                         Assert.AreEqual (Check<uint> (UInt32.MinValue), UInt32.MinValue, "uint3");
177                         Assert.AreEqual (Check<uint> (UInt32.MaxValue), UInt32.MaxValue, "uint4");
178                         Assert.AreEqual (Check<byte> (10), 10, "byte");
179                         Assert.AreEqual (Check<byte> (Byte.MinValue), Byte.MinValue, "byte2");
180                         Assert.AreEqual (Check<byte> (Byte.MaxValue), Byte.MaxValue, "byte3");
181                         Assert.AreEqual (Check<short> (128), 128, "short");
182                         Assert.AreEqual (Check<short> (-128), -128, "short");
183                         Assert.AreEqual (Check<short> (Int16.MinValue), Int16.MinValue, "short2");
184                         Assert.AreEqual (Check<short> (Int16.MaxValue), Int16.MaxValue, "short3");
185                         Assert.AreEqual (Check<ushort> (128), 128, "ushort");
186                         Assert.AreEqual (Check<ushort> (UInt16.MinValue), UInt16.MinValue, "short2");
187                         Assert.AreEqual (Check<ushort> (UInt16.MaxValue), UInt16.MaxValue, "short3");
188                         Assert.AreEqual (Check<bool> (true), true, "bool1");
189                         Assert.AreEqual (Check<bool> (false), false, "bool2");
190                         Assert.AreEqual (Check<long> (Int64.MaxValue), Int64.MaxValue, "long");
191                         Assert.AreEqual (Check<long> (Int64.MinValue), Int64.MinValue, "long2");
192                         Assert.AreEqual (Check<ulong> (UInt64.MaxValue), UInt64.MaxValue, "ulong");
193                         Assert.AreEqual (Check<ulong> (UInt64.MinValue), UInt64.MinValue, "ulong2");
194                         Assert.AreEqual (Check<ushort> (200), 200, "ushort");
195                         Assert.AreEqual (Check<float> (2.0f), 2.0f, "float");
196                         Assert.AreEqual (Check<double> (2.312), 2.312, "double");
197                         Assert.AreEqual (Check<string> ("dingus"), "dingus", "string");
198                         Assert.AreEqual (Check<decimal> (1.3m), 1.3m, "");
199
200                         // this forces the other code path for decimal.
201                         Assert.AreEqual (Check<decimal> (3147483647m), 3147483647m, "decimal");
202                 }
203
204                 delegate void Foo ();
205
206                 [Test]
207                 public void DelegateTypeConstant ()
208                 {
209                         Expression.Constant (typeof (Foo), typeof (Type));
210                 }
211
212                 [Test]
213                 public void EmitNullString ()
214                 {
215                         var n = Expression.Lambda<Func<string>> (
216                                 Expression.Constant (null, typeof (string))).Compile ();
217
218                         Assert.IsNull (n ());
219                 }
220
221                 [Test]
222                 public void EmitNullNullableType ()
223                 {
224                         var n = Expression.Lambda<Func<int?>> (
225                                 Expression.Constant (null, typeof (int?))).Compile ();
226
227                         Assert.IsNull (n ());
228                 }
229
230                 interface IBar {}
231                 class Bar : IBar {}
232
233                 interface IBaz<T> {}
234                 class Baz<T> : IBaz<T> {}
235
236                 [Test]
237                 public void ConstantInterface ()
238                 {
239                         var c = Expression.Constant (new Bar (), typeof (IBar));
240                         Assert.AreEqual (typeof (IBar), c.Type);
241                 }
242
243                 [Test]
244                 public void ConstantGenericInterface ()
245                 {
246                         var c = Expression.Constant (new Baz<string> (), typeof (IBaz<string>));
247                         Assert.AreEqual (typeof (IBaz<string>), c.Type);
248                 }
249         }
250 }