PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_Constant.cs

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