ExpressionTest_Constant.cs 9.3 KB

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