PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/System.Linq.Expressions/tests/BinaryOperators/Arithmetic/BinaryNullableSubtractTests.cs

https://gitlab.com/0072016/0072016-corefx-
C# | 330 lines | 288 code | 39 blank | 3 comment | 25 complexity | d6603f36bf223f9f45d74c312793abff MD5 | raw file
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using Xunit;
  6. namespace System.Linq.Expressions.Tests
  7. {
  8. public static class BinaryNullableSubtractTests
  9. {
  10. #region Test methods
  11. [Fact]
  12. public static void CheckNullableByteSubtractTest()
  13. {
  14. byte?[] array = new byte?[] { 0, 1, byte.MaxValue };
  15. for (int i = 0; i < array.Length; i++)
  16. {
  17. for (int j = 0; j < array.Length; j++)
  18. {
  19. VerifyNullableByteSubtract(array[i], array[j]);
  20. }
  21. }
  22. }
  23. [Fact]
  24. public static void CheckNullableSByteSubtractTest()
  25. {
  26. sbyte?[] array = new sbyte?[] { 0, 1, -1, sbyte.MinValue, sbyte.MaxValue };
  27. for (int i = 0; i < array.Length; i++)
  28. {
  29. for (int j = 0; j < array.Length; j++)
  30. {
  31. VerifyNullableSByteSubtract(array[i], array[j]);
  32. }
  33. }
  34. }
  35. [Theory, ClassData(typeof(CompilationTypes))]
  36. public static void CheckNullableUShortSubtractTest(bool useInterpreter)
  37. {
  38. ushort?[] array = new ushort?[] { 0, 1, ushort.MaxValue };
  39. for (int i = 0; i < array.Length; i++)
  40. {
  41. for (int j = 0; j < array.Length; j++)
  42. {
  43. VerifyNullableUShortSubtract(array[i], array[j], useInterpreter);
  44. }
  45. }
  46. }
  47. [Theory, ClassData(typeof(CompilationTypes))]
  48. public static void CheckNullableShortSubtractTest(bool useInterpreter)
  49. {
  50. short?[] array = new short?[] { 0, 1, -1, short.MinValue, short.MaxValue };
  51. for (int i = 0; i < array.Length; i++)
  52. {
  53. for (int j = 0; j < array.Length; j++)
  54. {
  55. VerifyNullableShortSubtract(array[i], array[j], useInterpreter);
  56. }
  57. }
  58. }
  59. [Theory, ClassData(typeof(CompilationTypes))]
  60. public static void CheckNullableUIntSubtractTest(bool useInterpreter)
  61. {
  62. uint?[] array = new uint?[] { 0, 1, uint.MaxValue };
  63. for (int i = 0; i < array.Length; i++)
  64. {
  65. for (int j = 0; j < array.Length; j++)
  66. {
  67. VerifyNullableUIntSubtract(array[i], array[j], useInterpreter);
  68. }
  69. }
  70. }
  71. [Theory, ClassData(typeof(CompilationTypes))]
  72. public static void CheckNullableIntSubtractTest(bool useInterpreter)
  73. {
  74. int?[] array = new int?[] { 0, 1, -1, int.MinValue, int.MaxValue };
  75. for (int i = 0; i < array.Length; i++)
  76. {
  77. for (int j = 0; j < array.Length; j++)
  78. {
  79. VerifyNullableIntSubtract(array[i], array[j], useInterpreter);
  80. }
  81. }
  82. }
  83. [Theory, ClassData(typeof(CompilationTypes))]
  84. public static void CheckNullableULongSubtractTest(bool useInterpreter)
  85. {
  86. ulong?[] array = new ulong?[] { 0, 1, ulong.MaxValue };
  87. for (int i = 0; i < array.Length; i++)
  88. {
  89. for (int j = 0; j < array.Length; j++)
  90. {
  91. VerifyNullableULongSubtract(array[i], array[j], useInterpreter);
  92. }
  93. }
  94. }
  95. [Theory, ClassData(typeof(CompilationTypes))]
  96. public static void CheckNullableLongSubtractTest(bool useInterpreter)
  97. {
  98. long?[] array = new long?[] { 0, 1, -1, long.MinValue, long.MaxValue };
  99. for (int i = 0; i < array.Length; i++)
  100. {
  101. for (int j = 0; j < array.Length; j++)
  102. {
  103. VerifyNullableLongSubtract(array[i], array[j], useInterpreter);
  104. }
  105. }
  106. }
  107. [Theory, ClassData(typeof(CompilationTypes))]
  108. public static void CheckNullableFloatSubtractTest(bool useInterpreter)
  109. {
  110. float?[] array = new float?[] { 0, 1, -1, float.MinValue, float.MaxValue, float.Epsilon, float.NegativeInfinity, float.PositiveInfinity, float.NaN };
  111. for (int i = 0; i < array.Length; i++)
  112. {
  113. for (int j = 0; j < array.Length; j++)
  114. {
  115. VerifyNullableFloatSubtract(array[i], array[j], useInterpreter);
  116. }
  117. }
  118. }
  119. [Theory, ClassData(typeof(CompilationTypes))]
  120. public static void CheckNullableDoubleSubtractTest(bool useInterpreter)
  121. {
  122. double?[] array = new double?[] { 0, 1, -1, double.MinValue, double.MaxValue, double.Epsilon, double.NegativeInfinity, double.PositiveInfinity, double.NaN };
  123. for (int i = 0; i < array.Length; i++)
  124. {
  125. for (int j = 0; j < array.Length; j++)
  126. {
  127. VerifyNullableDoubleSubtract(array[i], array[j], useInterpreter);
  128. }
  129. }
  130. }
  131. [Theory, ClassData(typeof(CompilationTypes))]
  132. public static void CheckNullableDecimalSubtractTest(bool useInterpreter)
  133. {
  134. decimal?[] array = new decimal?[] { decimal.Zero, decimal.One, decimal.MinusOne, decimal.MinValue, decimal.MaxValue };
  135. for (int i = 0; i < array.Length; i++)
  136. {
  137. for (int j = 0; j < array.Length; j++)
  138. {
  139. VerifyNullableDecimalSubtract(array[i], array[j], useInterpreter);
  140. }
  141. }
  142. }
  143. [Fact]
  144. public static void CheckNullableCharSubtractTest()
  145. {
  146. char?[] array = new char?[] { '\0', '\b', 'A', '\uffff' };
  147. for (int i = 0; i < array.Length; i++)
  148. {
  149. for (int j = 0; j < array.Length; j++)
  150. {
  151. VerifyNullableCharSubtract(array[i], array[j]);
  152. }
  153. }
  154. }
  155. #endregion
  156. #region Test verifiers
  157. private static void VerifyNullableByteSubtract(byte? a, byte? b)
  158. {
  159. Expression aExp = Expression.Constant(a, typeof(byte?));
  160. Expression bExp = Expression.Constant(b, typeof(byte?));
  161. Assert.Throws<InvalidOperationException>(() => Expression.Subtract(aExp, bExp));
  162. }
  163. private static void VerifyNullableSByteSubtract(sbyte? a, sbyte? b)
  164. {
  165. Expression aExp = Expression.Constant(a, typeof(sbyte?));
  166. Expression bExp = Expression.Constant(b, typeof(sbyte?));
  167. Assert.Throws<InvalidOperationException>(() => Expression.Subtract(aExp, bExp));
  168. }
  169. private static void VerifyNullableUShortSubtract(ushort? a, ushort? b, bool useInterpreter)
  170. {
  171. Expression<Func<ushort?>> e =
  172. Expression.Lambda<Func<ushort?>>(
  173. Expression.Subtract(
  174. Expression.Constant(a, typeof(ushort?)),
  175. Expression.Constant(b, typeof(ushort?))),
  176. Enumerable.Empty<ParameterExpression>());
  177. Func<ushort?> f = e.Compile(useInterpreter);
  178. Assert.Equal((ushort?)(a - b), f());
  179. }
  180. private static void VerifyNullableShortSubtract(short? a, short? b, bool useInterpreter)
  181. {
  182. Expression<Func<short?>> e =
  183. Expression.Lambda<Func<short?>>(
  184. Expression.Subtract(
  185. Expression.Constant(a, typeof(short?)),
  186. Expression.Constant(b, typeof(short?))),
  187. Enumerable.Empty<ParameterExpression>());
  188. Func<short?> f = e.Compile(useInterpreter);
  189. Assert.Equal((short?)(a - b), f());
  190. }
  191. private static void VerifyNullableUIntSubtract(uint? a, uint? b, bool useInterpreter)
  192. {
  193. Expression<Func<uint?>> e =
  194. Expression.Lambda<Func<uint?>>(
  195. Expression.Subtract(
  196. Expression.Constant(a, typeof(uint?)),
  197. Expression.Constant(b, typeof(uint?))),
  198. Enumerable.Empty<ParameterExpression>());
  199. Func<uint?> f = e.Compile(useInterpreter);
  200. Assert.Equal(a - b, f());
  201. }
  202. private static void VerifyNullableIntSubtract(int? a, int? b, bool useInterpreter)
  203. {
  204. Expression<Func<int?>> e =
  205. Expression.Lambda<Func<int?>>(
  206. Expression.Subtract(
  207. Expression.Constant(a, typeof(int?)),
  208. Expression.Constant(b, typeof(int?))),
  209. Enumerable.Empty<ParameterExpression>());
  210. Func<int?> f = e.Compile(useInterpreter);
  211. Assert.Equal(a - b, f());
  212. }
  213. private static void VerifyNullableULongSubtract(ulong? a, ulong? b, bool useInterpreter)
  214. {
  215. Expression<Func<ulong?>> e =
  216. Expression.Lambda<Func<ulong?>>(
  217. Expression.Subtract(
  218. Expression.Constant(a, typeof(ulong?)),
  219. Expression.Constant(b, typeof(ulong?))),
  220. Enumerable.Empty<ParameterExpression>());
  221. Func<ulong?> f = e.Compile(useInterpreter);
  222. Assert.Equal(a - b, f());
  223. }
  224. private static void VerifyNullableLongSubtract(long? a, long? b, bool useInterpreter)
  225. {
  226. Expression<Func<long?>> e =
  227. Expression.Lambda<Func<long?>>(
  228. Expression.Subtract(
  229. Expression.Constant(a, typeof(long?)),
  230. Expression.Constant(b, typeof(long?))),
  231. Enumerable.Empty<ParameterExpression>());
  232. Func<long?> f = e.Compile(useInterpreter);
  233. Assert.Equal(a - b, f());
  234. }
  235. private static void VerifyNullableFloatSubtract(float? a, float? b, bool useInterpreter)
  236. {
  237. Expression<Func<float?>> e =
  238. Expression.Lambda<Func<float?>>(
  239. Expression.Subtract(
  240. Expression.Constant(a, typeof(float?)),
  241. Expression.Constant(b, typeof(float?))),
  242. Enumerable.Empty<ParameterExpression>());
  243. Func<float?> f = e.Compile(useInterpreter);
  244. Assert.Equal(a - b, f());
  245. }
  246. private static void VerifyNullableDoubleSubtract(double? a, double? b, bool useInterpreter)
  247. {
  248. Expression<Func<double?>> e =
  249. Expression.Lambda<Func<double?>>(
  250. Expression.Subtract(
  251. Expression.Constant(a, typeof(double?)),
  252. Expression.Constant(b, typeof(double?))),
  253. Enumerable.Empty<ParameterExpression>());
  254. Func<double?> f = e.Compile(useInterpreter);
  255. Assert.Equal(a - b, f());
  256. }
  257. private static void VerifyNullableDecimalSubtract(decimal? a, decimal? b, bool useInterpreter)
  258. {
  259. Expression<Func<decimal?>> e =
  260. Expression.Lambda<Func<decimal?>>(
  261. Expression.Subtract(
  262. Expression.Constant(a, typeof(decimal?)),
  263. Expression.Constant(b, typeof(decimal?))),
  264. Enumerable.Empty<ParameterExpression>());
  265. Func<decimal?> f = e.Compile(useInterpreter);
  266. if (a.HasValue & b.HasValue)
  267. {
  268. decimal? expected = null;
  269. try
  270. {
  271. expected = a - b;
  272. }
  273. catch (OverflowException)
  274. {
  275. Assert.Throws<OverflowException>(() => f());
  276. return;
  277. }
  278. Assert.Equal(expected, f());
  279. }
  280. else
  281. Assert.Null(f());
  282. }
  283. private static void VerifyNullableCharSubtract(char? a, char? b)
  284. {
  285. Expression aExp = Expression.Constant(a, typeof(char?));
  286. Expression bExp = Expression.Constant(b, typeof(char?));
  287. Assert.Throws<InvalidOperationException>(() => Expression.Subtract(aExp, bExp));
  288. }
  289. #endregion
  290. }
  291. }