2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Constant.cs
blobb8c3bdc9470e3515b144a7d0dbbe48af254bb27a
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.
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
19 // Authors:
20 // Federico Di Gregorio <fog@initd.org>
22 using System;
23 using System.Linq;
24 using System.Linq.Expressions;
25 using NUnit.Framework;
27 namespace MonoTests.System.Linq.Expressions
29 [TestFixture]
30 public class ExpressionTest_Constant
32 [Test]
33 [ExpectedException (typeof (ArgumentException))]
34 public void Arg2NotNullable ()
36 Expression.Constant(null, typeof(int));
39 [Test]
40 [ExpectedException (typeof (ArgumentNullException))]
41 public void Arg2Null ()
43 Expression.Constant (1, null);
46 [Test]
47 public void NullValue ()
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");
56 [Test]
57 public void NullableValue1 ()
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");
66 [Test]
67 public void NullableValue2 ()
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");
76 [Test]
77 public void NullableValue3 ()
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");
86 [Test]
87 public void IntegerValue ()
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");
96 [Test]
97 public void StringValue ()
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");
106 [Test]
107 public void DateTimeValue ()
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");
116 [Test]
117 public void UserClassValue ()
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");
127 [Test]
128 [ExpectedException (typeof (ArgumentException))]
129 public void TestInvalidCtor_1 ()
131 // null value, type == valuetype is invalid
132 Expression.Constant (null, typeof (int));
135 [Test]
136 [ExpectedException (typeof (ArgumentException))]
137 public void TestInvalidCtor_2 ()
139 // type mismatch: int value, type == double
140 Expression.Constant (0, typeof (double));
143 [Test]
144 [ExpectedException (typeof (ArgumentException))]
145 public void VoidConstant ()
147 Expression.Constant (null, typeof (void));
150 static T Check<T> (T val)
152 Expression<Func<T>> l = Expression.Lambda<Func<T>> (Expression.Constant (val), new ParameterExpression [0]);
153 Func<T> fi = l.Compile ();
154 return fi ();
157 [Test]
158 public void NullableConstant_ToConstant ()
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");
166 [Test]
167 public void ConstantCodeGen ()
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, "");
200 // this forces the other code path for decimal.
201 Assert.AreEqual (Check<decimal> (3147483647m), 3147483647m, "decimal");
204 delegate void Foo ();
206 [Test]
207 public void DelegateTypeConstant ()
209 Expression.Constant (typeof (Foo), typeof (Type));
212 [Test]
213 public void EmitDateTimeConstant ()
215 var date = new DateTime (1983, 2, 6);
217 var lambda = Expression.Lambda<Func<DateTime>> (Expression.Constant (date)).Compile ();
219 Assert.AreEqual (date, lambda ());
222 [Test]
223 public void EmitDBNullConstant ()
225 var lambda = Expression.Lambda<Func<DBNull>> (Expression.Constant (DBNull.Value)).Compile ();
227 Assert.AreEqual (DBNull.Value, lambda ());
230 [Test]
231 public void EmitNullString ()
233 var n = Expression.Lambda<Func<string>> (
234 Expression.Constant (null, typeof (string))).Compile ();
236 Assert.IsNull (n ());
239 [Test]
240 public void EmitNullNullableType ()
242 var n = Expression.Lambda<Func<int?>> (
243 Expression.Constant (null, typeof (int?))).Compile ();
245 Assert.IsNull (n ());
248 [Test]
249 public void EmitNullableInt ()
251 var i = Expression.Lambda<Func<int?>> (
252 Expression.Constant ((int?) 42, typeof (int?))).Compile ();
254 Assert.AreEqual ((int?) 42, i ());
257 [Test]
258 public void EmitNullableEnum ()
260 var e = Expression.Lambda<Func<Chose?>> (
261 Expression.Constant ((Chose?) Chose.Moche, typeof (Chose?))).Compile ();
263 Assert.AreEqual ((Chose?) Chose.Moche, e ());
266 enum Chose { Moche }
268 interface IBar {}
269 class Bar : IBar {}
271 interface IBaz<T> {}
272 class Baz<T> : IBaz<T> {}
274 [Test]
275 public void ConstantInterface ()
277 var c = Expression.Constant (new Bar (), typeof (IBar));
278 Assert.AreEqual (typeof (IBar), c.Type);
281 [Test]
282 public void ConstantGenericInterface ()
284 var c = Expression.Constant (new Baz<string> (), typeof (IBaz<string>));
285 Assert.AreEqual (typeof (IBaz<string>), c.Type);