Skip to content

Commit

Permalink
Add explicit testing of corner cases
Browse files Browse the repository at this point in the history
Commit migrated from dotnet/corefx@7cc6531
  • Loading branch information
kant2002 authored and tannergooding committed Sep 11, 2018
1 parent b70b7d8 commit f253d35
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ public static Complex Divide(double dividend, Complex divisor)
double c = right.m_real;
double d = right.m_imaginary;

if (d == 0.0)
{
return left / c;
}

// Computing c * c + d * d will overflow even in cases where the actual result of the division does not overflow.
if (Math.Abs(d) < Math.Abs(c))
{
Expand All @@ -203,6 +208,16 @@ public static Complex Divide(double dividend, Complex divisor)
double c = right.m_real;
double d = right.m_imaginary;

if (d == 0.0)
{
if (c == 0.0)
{
return new Complex(left / c, left / c);
}

return left / c;
}

// Computing c * c + d * d will overflow even in cases where the actual result of the division does not overflow.
if (Math.Abs(d) < Math.Abs(c))
{
Expand Down
62 changes: 58 additions & 4 deletions src/libraries/System.Runtime.Numerics/tests/ComplexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,7 @@ public static void Cosh_Legacy(double real, double imaginary, double expectedRea
public static IEnumerable<object[]> Divide_TestData()
{
yield return new object[] { 0, 0, 10, 50 }; // 0 / x = 0
yield return new object[] { 10, 50, double.NaN, double.NaN }; // 0 / x = NaN
yield return new object[] { 10, 50, 0, 0 }; // x / 0 = 0
yield return new object[] { 10, 50, double.NaN, double.NaN }; // x / NaN = NaN

yield return new object[] { 1, 0, 10, 50 }; // 1 / x = x
yield return new object[] { 10, 50, 1, 0 }; // x / 1 = x
Expand All @@ -743,6 +742,13 @@ public static IEnumerable<object[]> Divide_TestData()
yield return new object[] { double.MinValue, double.MinValue, SmallRandomPositiveDouble(), SmallRandomPositiveDouble() };
}

public static IEnumerable<object[]> Divide_AdvancedTestData()
{
yield return new object[] { 10, 50, double.NaN, double.NaN, double.NaN, double.NaN }; // 0 / NaN = NaN
yield return new object[] { 10, 50, 0, 0, double.PositiveInfinity, double.PositiveInfinity }; // x / 0 = Inf
yield return new object[] { 0, 0, 0, 0, double.NaN, double.NaN }; // 0 / 0 = NaN
}

[Theory]
[MemberData(nameof(Divide_TestData))]
[MemberData(nameof(SmallRandom_4_TestData))]
Expand Down Expand Up @@ -775,9 +781,25 @@ public static void Divide(double realLeft, double imaginaryLeft, double realRigh
}

[Theory]
//[MemberData(nameof(Divide_TestData))]
[MemberData(nameof(Divide_AdvancedTestData))]
public static void Divide_Advanced(double realLeft, double imaginaryLeft, double realRight, double imaginaryRight, double expectedReal, double expectedImaginary)
{
var dividend = new Complex(realLeft, imaginaryLeft);
var divisor = new Complex(realRight, imaginaryRight);

// Operator
Complex result = dividend / divisor;
VerifyRealImaginaryProperties(result, expectedReal, expectedImaginary);

// Static method
result = Complex.Divide(dividend, divisor);
VerifyRealImaginaryProperties(result, expectedReal, expectedImaginary);
}

[Theory]
[MemberData(nameof(Divide_TestData))]
[MemberData(nameof(SmallRandom_4_TestData))]
//[MemberData(nameof(Invalid_4_TestData))]
[MemberData(nameof(Invalid_4_TestData))]
public static void DivideByDouble(double realLeft, double imaginaryLeft, double realRight, double imaginaryRight)
{
var dividend = new Complex(realLeft, imaginaryLeft);
Expand All @@ -796,6 +818,22 @@ public static void DivideByDouble(double realLeft, double imaginaryLeft, double
VerifyRealImaginaryProperties(result, expectedReal, expectedImaginary);
}

[Theory]
[MemberData(nameof(Divide_AdvancedTestData))]
public static void DivideByDouble_Advanced(double realLeft, double imaginaryLeft, double realRight, double imaginaryRight, double expectedReal, double expectedImaginary)
{
var dividend = new Complex(realLeft, imaginaryLeft);
var divisor = realRight;

// Operator
Complex result = dividend / divisor;
VerifyRealImaginaryProperties(result, expectedReal, expectedImaginary);

// Static method
result = Complex.Divide(dividend, divisor);
VerifyRealImaginaryProperties(result, expectedReal, expectedImaginary);
}

[Theory]
[MemberData(nameof(Divide_TestData))]
[MemberData(nameof(SmallRandom_4_TestData))]
Expand All @@ -818,6 +856,22 @@ public static void DivideByComplex(double realLeft, double imaginaryLeft, double
VerifyRealImaginaryProperties(result, expectedReal, expectedImaginary);
}

[Theory]
[MemberData(nameof(Divide_AdvancedTestData))]
public static void DivideByComplex_Advanced(double realLeft, double imaginaryLeft, double realRight, double imaginaryRight, double expectedReal, double expectedImaginary)
{
var dividend = realLeft;
var divisor = new Complex(realRight, imaginaryRight);

// Operator
Complex result = dividend / divisor;
VerifyRealImaginaryProperties(result, expectedReal, expectedImaginary);

// Static method
result = Complex.Divide(dividend, divisor);
VerifyRealImaginaryProperties(result, expectedReal, expectedImaginary);
}

[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
public static void Equals_netcore()
Expand Down

0 comments on commit f253d35

Please sign in to comment.