/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
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- using System;
- using Xunit;
- namespace System.Linq.Expressions.Tests
- {
- public static class BinaryNullableSubtractTests
- {
- #region Test methods
- [Fact]
- public static void CheckNullableByteSubtractTest()
- {
- byte?[] array = new byte?[] { 0, 1, byte.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyNullableByteSubtract(array[i], array[j]);
- }
- }
- }
- [Fact]
- public static void CheckNullableSByteSubtractTest()
- {
- sbyte?[] array = new sbyte?[] { 0, 1, -1, sbyte.MinValue, sbyte.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyNullableSByteSubtract(array[i], array[j]);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckNullableUShortSubtractTest(bool useInterpreter)
- {
- ushort?[] array = new ushort?[] { 0, 1, ushort.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyNullableUShortSubtract(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckNullableShortSubtractTest(bool useInterpreter)
- {
- short?[] array = new short?[] { 0, 1, -1, short.MinValue, short.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyNullableShortSubtract(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckNullableUIntSubtractTest(bool useInterpreter)
- {
- uint?[] array = new uint?[] { 0, 1, uint.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyNullableUIntSubtract(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckNullableIntSubtractTest(bool useInterpreter)
- {
- int?[] array = new int?[] { 0, 1, -1, int.MinValue, int.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyNullableIntSubtract(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckNullableULongSubtractTest(bool useInterpreter)
- {
- ulong?[] array = new ulong?[] { 0, 1, ulong.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyNullableULongSubtract(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckNullableLongSubtractTest(bool useInterpreter)
- {
- long?[] array = new long?[] { 0, 1, -1, long.MinValue, long.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyNullableLongSubtract(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckNullableFloatSubtractTest(bool useInterpreter)
- {
- float?[] array = new float?[] { 0, 1, -1, float.MinValue, float.MaxValue, float.Epsilon, float.NegativeInfinity, float.PositiveInfinity, float.NaN };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyNullableFloatSubtract(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckNullableDoubleSubtractTest(bool useInterpreter)
- {
- double?[] array = new double?[] { 0, 1, -1, double.MinValue, double.MaxValue, double.Epsilon, double.NegativeInfinity, double.PositiveInfinity, double.NaN };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyNullableDoubleSubtract(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckNullableDecimalSubtractTest(bool useInterpreter)
- {
- decimal?[] array = new decimal?[] { decimal.Zero, decimal.One, decimal.MinusOne, decimal.MinValue, decimal.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyNullableDecimalSubtract(array[i], array[j], useInterpreter);
- }
- }
- }
- [Fact]
- public static void CheckNullableCharSubtractTest()
- {
- char?[] array = new char?[] { '\0', '\b', 'A', '\uffff' };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyNullableCharSubtract(array[i], array[j]);
- }
- }
- }
- #endregion
- #region Test verifiers
- private static void VerifyNullableByteSubtract(byte? a, byte? b)
- {
- Expression aExp = Expression.Constant(a, typeof(byte?));
- Expression bExp = Expression.Constant(b, typeof(byte?));
- Assert.Throws<InvalidOperationException>(() => Expression.Subtract(aExp, bExp));
- }
- private static void VerifyNullableSByteSubtract(sbyte? a, sbyte? b)
- {
- Expression aExp = Expression.Constant(a, typeof(sbyte?));
- Expression bExp = Expression.Constant(b, typeof(sbyte?));
- Assert.Throws<InvalidOperationException>(() => Expression.Subtract(aExp, bExp));
- }
- private static void VerifyNullableUShortSubtract(ushort? a, ushort? b, bool useInterpreter)
- {
- Expression<Func<ushort?>> e =
- Expression.Lambda<Func<ushort?>>(
- Expression.Subtract(
- Expression.Constant(a, typeof(ushort?)),
- Expression.Constant(b, typeof(ushort?))),
- Enumerable.Empty<ParameterExpression>());
- Func<ushort?> f = e.Compile(useInterpreter);
- Assert.Equal((ushort?)(a - b), f());
- }
- private static void VerifyNullableShortSubtract(short? a, short? b, bool useInterpreter)
- {
- Expression<Func<short?>> e =
- Expression.Lambda<Func<short?>>(
- Expression.Subtract(
- Expression.Constant(a, typeof(short?)),
- Expression.Constant(b, typeof(short?))),
- Enumerable.Empty<ParameterExpression>());
- Func<short?> f = e.Compile(useInterpreter);
- Assert.Equal((short?)(a - b), f());
- }
- private static void VerifyNullableUIntSubtract(uint? a, uint? b, bool useInterpreter)
- {
- Expression<Func<uint?>> e =
- Expression.Lambda<Func<uint?>>(
- Expression.Subtract(
- Expression.Constant(a, typeof(uint?)),
- Expression.Constant(b, typeof(uint?))),
- Enumerable.Empty<ParameterExpression>());
- Func<uint?> f = e.Compile(useInterpreter);
- Assert.Equal(a - b, f());
- }
- private static void VerifyNullableIntSubtract(int? a, int? b, bool useInterpreter)
- {
- Expression<Func<int?>> e =
- Expression.Lambda<Func<int?>>(
- Expression.Subtract(
- Expression.Constant(a, typeof(int?)),
- Expression.Constant(b, typeof(int?))),
- Enumerable.Empty<ParameterExpression>());
- Func<int?> f = e.Compile(useInterpreter);
- Assert.Equal(a - b, f());
- }
- private static void VerifyNullableULongSubtract(ulong? a, ulong? b, bool useInterpreter)
- {
- Expression<Func<ulong?>> e =
- Expression.Lambda<Func<ulong?>>(
- Expression.Subtract(
- Expression.Constant(a, typeof(ulong?)),
- Expression.Constant(b, typeof(ulong?))),
- Enumerable.Empty<ParameterExpression>());
- Func<ulong?> f = e.Compile(useInterpreter);
- Assert.Equal(a - b, f());
- }
- private static void VerifyNullableLongSubtract(long? a, long? b, bool useInterpreter)
- {
- Expression<Func<long?>> e =
- Expression.Lambda<Func<long?>>(
- Expression.Subtract(
- Expression.Constant(a, typeof(long?)),
- Expression.Constant(b, typeof(long?))),
- Enumerable.Empty<ParameterExpression>());
- Func<long?> f = e.Compile(useInterpreter);
- Assert.Equal(a - b, f());
- }
- private static void VerifyNullableFloatSubtract(float? a, float? b, bool useInterpreter)
- {
- Expression<Func<float?>> e =
- Expression.Lambda<Func<float?>>(
- Expression.Subtract(
- Expression.Constant(a, typeof(float?)),
- Expression.Constant(b, typeof(float?))),
- Enumerable.Empty<ParameterExpression>());
- Func<float?> f = e.Compile(useInterpreter);
- Assert.Equal(a - b, f());
- }
- private static void VerifyNullableDoubleSubtract(double? a, double? b, bool useInterpreter)
- {
- Expression<Func<double?>> e =
- Expression.Lambda<Func<double?>>(
- Expression.Subtract(
- Expression.Constant(a, typeof(double?)),
- Expression.Constant(b, typeof(double?))),
- Enumerable.Empty<ParameterExpression>());
- Func<double?> f = e.Compile(useInterpreter);
- Assert.Equal(a - b, f());
- }
- private static void VerifyNullableDecimalSubtract(decimal? a, decimal? b, bool useInterpreter)
- {
- Expression<Func<decimal?>> e =
- Expression.Lambda<Func<decimal?>>(
- Expression.Subtract(
- Expression.Constant(a, typeof(decimal?)),
- Expression.Constant(b, typeof(decimal?))),
- Enumerable.Empty<ParameterExpression>());
- Func<decimal?> f = e.Compile(useInterpreter);
- if (a.HasValue & b.HasValue)
- {
- decimal? expected = null;
- try
- {
- expected = a - b;
- }
- catch (OverflowException)
- {
- Assert.Throws<OverflowException>(() => f());
- return;
- }
- Assert.Equal(expected, f());
- }
- else
- Assert.Null(f());
- }
- private static void VerifyNullableCharSubtract(char? a, char? b)
- {
- Expression aExp = Expression.Constant(a, typeof(char?));
- Expression bExp = Expression.Constant(b, typeof(char?));
- Assert.Throws<InvalidOperationException>(() => Expression.Subtract(aExp, bExp));
- }
- #endregion
- }
- }