From 2f3f6f81fcb4194e76bdba7d1b4e2efee9d31dc8 Mon Sep 17 00:00:00 2001 From: hamish-rose Date: Tue, 5 Mar 2019 05:03:27 +1300 Subject: [PATCH] Add tests for dotnet/coreclrdotnet/corefx#20815 - new Math and MathF rounding modes (dotnet/corefx#33297) * add tests for new midpointrounding modes new tests added for Math.cs double Round methods * add tests for float, decimal convert to theory * Update tests tests now reflect correct rounding behavior * add tests for existing modes * code review feedback - refactor tests to use member data - move MathF tests to MathF.netcoreapp.cs - add tests for new modes with digits and special cases * move math tests to mathtests.netcoreapp.cs netcoreapp specific tests moved Commit migrated from https://github.com/dotnet/corefx/commit/cc42bb95b0877a653d4f556a0839d261ea3563b7 --- .../tests/System/MathF.netcoreapp.cs | 65 ++++++++-- .../tests/System/MathTests.netcoreapp.cs | 131 +++++++++++++++++++++ 2 files changed, 188 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Runtime.Extensions/tests/System/MathF.netcoreapp.cs b/src/libraries/System.Runtime.Extensions/tests/System/MathF.netcoreapp.cs index a898d70..905ebf9 100644 --- a/src/libraries/System.Runtime.Extensions/tests/System/MathF.netcoreapp.cs +++ b/src/libraries/System.Runtime.Extensions/tests/System/MathF.netcoreapp.cs @@ -4,6 +4,7 @@ using Xunit; using Xunit.Sdk; +using System.Collections.Generic; namespace System.Tests { @@ -1426,6 +1427,47 @@ namespace System.Tests AssertEqual(expectedResult, MathF.Pow(x, y), allowedVariance); } + public static IEnumerable Round_Digits_TestData + { + get + { + yield return new object[] {float.NaN, float.NaN, 3, MidpointRounding.ToEven}; + yield return new object[] {float.PositiveInfinity, float.PositiveInfinity, 3, MidpointRounding.ToEven}; + yield return new object[] {float.NegativeInfinity, float.NegativeInfinity, 3, MidpointRounding.ToEven}; + yield return new object[] {0, 0, 3, MidpointRounding.ToEven}; + yield return new object[] {3.42156f, 3.422f, 3, MidpointRounding.ToEven}; + yield return new object[] {-3.42156f, -3.422f, 3, MidpointRounding.ToEven}; + + yield return new object[] {float.NaN, float.NaN, 3, MidpointRounding.AwayFromZero}; + yield return new object[] {float.PositiveInfinity, float.PositiveInfinity, 3, MidpointRounding.AwayFromZero}; + yield return new object[] {float.NegativeInfinity, float.NegativeInfinity, 3, MidpointRounding.AwayFromZero}; + yield return new object[] {0, 0, 3, MidpointRounding.AwayFromZero}; + yield return new object[] {3.42156f, 3.422f, 3, MidpointRounding.AwayFromZero}; + yield return new object[] {-3.42156f, -3.422f, 3, MidpointRounding.AwayFromZero}; + + yield return new object[] {float.NaN, float.NaN, 3, MidpointRounding.ToZero}; + yield return new object[] {float.PositiveInfinity, float.PositiveInfinity, 3, MidpointRounding.ToZero}; + yield return new object[] {float.NegativeInfinity, float.NegativeInfinity, 3, MidpointRounding.ToZero}; + yield return new object[] {0, 0, 3, MidpointRounding.ToZero}; + yield return new object[] {3.42156f, 3.421f, 3, MidpointRounding.ToZero}; + yield return new object[] {-3.42156f, -3.421f, 3, MidpointRounding.ToZero}; + + yield return new object[] {float.NaN, float.NaN, 3, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {float.PositiveInfinity, float.PositiveInfinity, 3, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {float.NegativeInfinity, float.NegativeInfinity, 3, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {0, 0, 3, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {3.42156f, 3.421f, 3, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {-3.42156f, -3.422f, 3, MidpointRounding.ToNegativeInfinity}; + + yield return new object[] {float.NaN, float.NaN, 3, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {float.PositiveInfinity, float.PositiveInfinity, 3, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {float.NegativeInfinity, float.NegativeInfinity, 3, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {0, 0, 3, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {3.42156f, 3.422f, 3, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {-3.42156f, -3.421f, 3, MidpointRounding.ToPositiveInfinity}; + } + } + [Fact] public static void Round() { @@ -1439,15 +1481,22 @@ namespace System.Tests Assert.Equal(-2e7f, MathF.Round(-2e7f)); } - [Fact] - public static void Round_Digits() + [Theory] + [InlineData(MidpointRounding.ToEven)] + [InlineData(MidpointRounding.AwayFromZero)] + [InlineData(MidpointRounding.ToNegativeInfinity)] + [InlineData(MidpointRounding.ToPositiveInfinity)] + public static void Round_Digits(MidpointRounding mode) + { + Assert.Equal(float.PositiveInfinity, MathF.Round(float.PositiveInfinity, 3, mode)); + Assert.Equal(float.NegativeInfinity, MathF.Round(float.NegativeInfinity, 3, mode)); + } + + [Theory] + [MemberData(nameof(Round_Digits_TestData))] + public static void Round_Digits(float x, float expected, int digits, MidpointRounding mode) { - AssertEqual(3.422f, MathF.Round(3.42156f, 3, MidpointRounding.AwayFromZero), CrossPlatformMachineEpsilon * 10); - AssertEqual(-3.422f, MathF.Round(-3.42156f, 3, MidpointRounding.AwayFromZero), CrossPlatformMachineEpsilon * 10); - Assert.Equal(0.0f, MathF.Round(0.0f, 3, MidpointRounding.AwayFromZero)); - Assert.Equal(float.NaN, MathF.Round(float.NaN, 3, MidpointRounding.AwayFromZero)); - Assert.Equal(float.PositiveInfinity, MathF.Round(float.PositiveInfinity, 3, MidpointRounding.AwayFromZero)); - Assert.Equal(float.NegativeInfinity, MathF.Round(float.NegativeInfinity, 3, MidpointRounding.AwayFromZero)); + AssertEqual(expected, MathF.Round(x, digits, mode), CrossPlatformMachineEpsilon * 10); } [Theory] diff --git a/src/libraries/System.Runtime.Extensions/tests/System/MathTests.netcoreapp.cs b/src/libraries/System.Runtime.Extensions/tests/System/MathTests.netcoreapp.cs index 1ae8ff9..682c8a9 100644 --- a/src/libraries/System.Runtime.Extensions/tests/System/MathTests.netcoreapp.cs +++ b/src/libraries/System.Runtime.Extensions/tests/System/MathTests.netcoreapp.cs @@ -658,5 +658,136 @@ namespace System.Tests { AssertEqual(expectedResult, Math.ScaleB(x, n), allowedVariance); } + + + public static IEnumerable Round_Digits_TestData + { + get + { + yield return new object[] {0, 0, 3, MidpointRounding.ToEven}; + yield return new object[] {3.42156, 3.422, 3, MidpointRounding.ToEven}; + yield return new object[] {-3.42156, -3.422, 3, MidpointRounding.ToEven}; + + yield return new object[] {0, 0, 3, MidpointRounding.AwayFromZero}; + yield return new object[] {3.42156, 3.422, 3, MidpointRounding.AwayFromZero}; + yield return new object[] {-3.42156, -3.422, 3, MidpointRounding.AwayFromZero}; + + yield return new object[] {0, 0, 3, MidpointRounding.ToZero}; + yield return new object[] {3.42156, 3.421, 3, MidpointRounding.ToZero}; + yield return new object[] {-3.42156, -3.421, 3, MidpointRounding.ToZero}; + + yield return new object[] {0, 0, 3, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {3.42156, 3.421, 3, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {-3.42156, -3.422, 3, MidpointRounding.ToNegativeInfinity}; + + yield return new object[] {0, 0, 3, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {3.42156, 3.422, 3, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {-3.42156, -3.421, 3, MidpointRounding.ToPositiveInfinity}; + } + } + + [Theory] + [InlineData(MidpointRounding.ToEven)] + [InlineData(MidpointRounding.AwayFromZero)] + [InlineData(MidpointRounding.ToZero)] + [InlineData(MidpointRounding.ToNegativeInfinity)] + [InlineData(MidpointRounding.ToPositiveInfinity)] + public static void Round_Double_Digits(MidpointRounding mode) + { + Assert.Equal(double.NaN, Math.Round(double.NaN, 3, mode)); + Assert.Equal(double.PositiveInfinity, Math.Round(double.PositiveInfinity, 3, mode)); + Assert.Equal(double.NegativeInfinity, Math.Round(double.NegativeInfinity, 3, mode)); + } + + [Theory] + [MemberData(nameof(Round_Digits_TestData))] + public static void Round_Double_Digits(double x, double expected, int digits, MidpointRounding mode) + { + Assert.Equal(expected, Math.Round(x, digits, mode)); + } + + [Theory] + [MemberData(nameof(Round_Digits_TestData))] + public static void Round_Decimal_Digits(decimal x, decimal expected, int digits, MidpointRounding mode) + { + Assert.Equal(expected, Math.Round(x, digits, mode)); + } + + [Theory] + [InlineData(MidpointRounding.ToEven)] + [InlineData(MidpointRounding.AwayFromZero)] + [InlineData(MidpointRounding.ToZero)] + [InlineData(MidpointRounding.ToNegativeInfinity)] + [InlineData(MidpointRounding.ToPositiveInfinity)] + public static void Round_Decimal_Digits(MidpointRounding mode) + { + Assert.Equal(decimal.Zero, Math.Round(decimal.Zero, 3, mode)); + } + + public static IEnumerable Round_Modes_TestData + { + get + { + yield return new object[] {11, 11, MidpointRounding.ToEven}; + yield return new object[] {11.4, 11, MidpointRounding.ToEven}; + yield return new object[] {11.5, 12, MidpointRounding.ToEven}; + yield return new object[] {11.6, 12, MidpointRounding.ToEven}; + yield return new object[] {-11, -11, MidpointRounding.ToEven}; + yield return new object[] {-11.4, -11, MidpointRounding.ToEven}; + yield return new object[] {-11.5, -12, MidpointRounding.ToEven}; + yield return new object[] {-11.6, -12, MidpointRounding.ToEven}; + yield return new object[] {11, 11, MidpointRounding.AwayFromZero}; + yield return new object[] {11.4, 11, MidpointRounding.AwayFromZero}; + yield return new object[] {11.5, 12, MidpointRounding.AwayFromZero}; + yield return new object[] {11.6, 12, MidpointRounding.AwayFromZero}; + yield return new object[] {-11, -11, MidpointRounding.AwayFromZero}; + yield return new object[] {-11.4, -11, MidpointRounding.AwayFromZero}; + yield return new object[] {-11.5, -12, MidpointRounding.AwayFromZero}; + yield return new object[] {-11.6, -12, MidpointRounding.AwayFromZero}; + yield return new object[] {11, 11, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {11.4, 12, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {11.5, 12, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {11.6, 12, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {-11, -11, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {-11.4, -11, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {-11.5, -11, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {-11.6, -11, MidpointRounding.ToPositiveInfinity}; + yield return new object[] {11.0, 11, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {11.4, 11, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {11.5, 11, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {11.6, 11, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {-11.0, -11, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {-11.4, -12, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {-11.5, -12, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {-11.6, -12, MidpointRounding.ToNegativeInfinity}; + yield return new object[] {11.0, 11, MidpointRounding.ToZero}; + yield return new object[] {11.4, 11, MidpointRounding.ToZero}; + yield return new object[] {11.5, 11, MidpointRounding.ToZero}; + yield return new object[] {11.6, 11, MidpointRounding.ToZero}; + yield return new object[] {-11.0, -11, MidpointRounding.ToZero}; + yield return new object[] {-11.4, -11, MidpointRounding.ToZero}; + yield return new object[] {-11.5, -11, MidpointRounding.ToZero}; + yield return new object[] {-11.6, -11, MidpointRounding.ToZero}; + } + } + + [MemberData(nameof(Round_Modes_TestData))] + public static void Round_Double_Modes(double x, double expected, MidpointRounding mode) + { + Assert.Equal(expected, Math.Round(x, 0, mode)); + } + + [MemberData(nameof(Round_Modes_TestData))] + public static void Round_Float_Modes(float x, float expected, MidpointRounding mode) + { + Assert.Equal(expected, MathF.Round(x, 0, mode)); + } + + [MemberData(nameof(Round_Modes_TestData))] + public static void Round_Decimal_Modes(decimal x, decimal expected, MidpointRounding mode) + { + Assert.Equal(expected, Math.Round(x, 0, mode)); + Assert.Equal(expected, decimal.Round(x, 0, mode)); + } } } -- 2.7.4