|
|
|
@ -5,16 +5,9 @@ |
|
|
|
|
|
|
|
using MbUnit.Framework; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The complex test.
|
|
|
|
/// </summary>
|
|
|
|
[TestFixture] |
|
|
|
public class ComplexTest |
|
|
|
{ |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can add complex number and double using operartor.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanAddComplexNumberAndDoubleUsingOperartor() |
|
|
|
@ -29,9 +22,6 @@ |
|
|
|
AssertEx.That(() => -2.2 + new Complex(-1.1, 2.2) == new Complex(-3.3, 2.2)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can add subtract complex numbers using operartor.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanAddSubtractComplexNumbersUsingOperartor() |
|
|
|
@ -42,9 +32,6 @@ |
|
|
|
AssertEx.That(() => (new Complex(1.1, -2.2) - new Complex(1.1, -2.2)) == Complex.Zero); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can add two complex numbers.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanAddTwoComplexNumbers() |
|
|
|
@ -55,9 +42,6 @@ |
|
|
|
AssertEx.That(() => new Complex(1.1, -2.2).Add(new Complex(-1.1, 2.2)) == Complex.Zero); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can add two complex numbers using operartor.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanAddTwoComplexNumbersUsingOperartor() |
|
|
|
@ -68,9 +52,6 @@ |
|
|
|
AssertEx.That(() => (new Complex(1.1, -2.2) + new Complex(-1.1, 2.2)) == Complex.Zero); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can calculate hash code.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanCalculateHashCode() |
|
|
|
@ -83,21 +64,6 @@ |
|
|
|
Assert.AreEqual(-2097152, complex.GetHashCode()); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can compute exponential.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="real">
|
|
|
|
/// The real.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="imag">
|
|
|
|
/// The imag.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="expectedReal">
|
|
|
|
/// The expected real.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="expectedImag">
|
|
|
|
/// The expected imag.
|
|
|
|
/// </param>
|
|
|
|
[Test] |
|
|
|
[Row(0.0, 0.0, 1.0, 0.0)] |
|
|
|
[Row(0.0, 1.0, 0.54030230586813977, 0.8414709848078965)] |
|
|
|
@ -110,21 +76,6 @@ |
|
|
|
AssertHelpers.AlmostEqual(expected, value.Exponential(), 15); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can compute natural logarithm.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="real">
|
|
|
|
/// The real.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="imag">
|
|
|
|
/// The imag.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="expectedReal">
|
|
|
|
/// The expected real.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="expectedImag">
|
|
|
|
/// The expected imag.
|
|
|
|
/// </param>
|
|
|
|
[Test] |
|
|
|
[Row(0.0, 0.0, double.NegativeInfinity, 0.0)] |
|
|
|
[Row(0.0, 1.0, 0.0, 1.5707963267948966)] |
|
|
|
@ -138,9 +89,6 @@ |
|
|
|
AssertHelpers.AlmostEqual(expected, value.NaturalLogarithm(), 15); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can compute power.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanComputePower() |
|
|
|
@ -167,11 +115,24 @@ |
|
|
|
a = new Complex(0.0, -8.388608e6); |
|
|
|
b = new Complex(1.19209289550780998537e-7, 0.0); |
|
|
|
AssertHelpers.AlmostEqual(new Complex(1.00000190048219620166, -1.87253870018168043834e-7), a.Power(b), 15); |
|
|
|
a = new Complex(0.0, 0.0); |
|
|
|
b = new Complex(0.0, 0.0); |
|
|
|
AssertHelpers.AlmostEqual(new Complex(1.0, 0.0), a.Power(b), 15); |
|
|
|
a = new Complex(0.0, 0.0); |
|
|
|
b = new Complex(1.0, 0.0); |
|
|
|
AssertHelpers.AlmostEqual(new Complex(0.0, 0.0), a.Power(b), 15); |
|
|
|
a = new Complex(0.0, 0.0); |
|
|
|
b = new Complex(-1.0, 0.0); |
|
|
|
AssertHelpers.AlmostEqual(new Complex(double.PositiveInfinity, 0.0), a.Power(b), 15); |
|
|
|
a = new Complex(0.0, 0.0); |
|
|
|
b = new Complex(-1.0, 1.0); |
|
|
|
AssertHelpers.AlmostEqual(new Complex(double.PositiveInfinity, double.PositiveInfinity), a.Power(b), 15); |
|
|
|
a = new Complex(0.0, 0.0); |
|
|
|
b = new Complex(0.0, 1.0); |
|
|
|
AssertEx.That(()=>a.Power(b).IsNaN); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can compute root.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanComputeRoot() |
|
|
|
@ -196,9 +157,6 @@ |
|
|
|
AssertHelpers.AlmostEqual(new Complex(double.PositiveInfinity, double.NegativeInfinity), a.Root(b), 15); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can compute square.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanComputeSquare() |
|
|
|
@ -217,9 +175,6 @@ |
|
|
|
AssertHelpers.AlmostEqual(new Complex(-70368744177664.0, 0.0), complex.Square(), 15); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can compute square root.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanComputeSquareRoot() |
|
|
|
@ -241,11 +196,22 @@ |
|
|
|
AssertHelpers.AlmostEqual(new Complex(2048.0, -2048.0), complex.SquareRoot(), 15); |
|
|
|
complex = new Complex(8.388608e6, 1.19209289550780998537e-7); |
|
|
|
AssertHelpers.AlmostEqual(new Complex(2896.3093757400989, 2.0579515874459933e-11), complex.SquareRoot(), 15); |
|
|
|
complex = new Complex(0.0, 0.0); |
|
|
|
AssertHelpers.AlmostEqual(Complex.Zero, complex.SquareRoot(), 15); |
|
|
|
} |
|
|
|
|
|
|
|
[Test] |
|
|
|
[Row(1, -2, "1 -2i")] |
|
|
|
[Row(1, 2, "1 + 2i")] |
|
|
|
[Row(1, 0, "1")] |
|
|
|
[Row(0, -2, "-2i")] |
|
|
|
[Row(0, 2, "2i")] |
|
|
|
public void CanConvertComplexToString(double real, double imag, string expected) |
|
|
|
{ |
|
|
|
var a = new Complex(real, imag); |
|
|
|
Assert.AreEqual(expected, a.ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can convert double to complex.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanConvertDoubleToComplex() |
|
|
|
@ -255,9 +221,68 @@ |
|
|
|
Assert.AreEqual(1.1, new Complex(1.1, 0)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can create complex number using the constructor.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[Row("-1", -1, 0)] |
|
|
|
[Row("-i", 0, -1)] |
|
|
|
[Row("i", 0, 1)] |
|
|
|
[Row("2i", 0, 2)] |
|
|
|
[Row("1 + 2i", 1, 2)] |
|
|
|
[Row("1+2i", 1, 2)] |
|
|
|
[Row("1 - 2i", 1, -2)] |
|
|
|
[Row("1-2i", 1, -2)] |
|
|
|
[Row("1,2", 1, 2)] |
|
|
|
[Row("1 , 2", 1, 2)] |
|
|
|
[Row("1,2i", 1, 2)] |
|
|
|
[Row("-1, -2i", -1, -2)] |
|
|
|
[Row("(+1,2i)", 1, 2)] |
|
|
|
[Row("(-1 , -2)", -1, -2)] |
|
|
|
[Row("(-1 , -2i)", -1, -2)] |
|
|
|
[Row("(+1e1 , -2e-2i)", 10, -0.02)] |
|
|
|
[Row("(-1E1 -2e2i)", -10, -200)] |
|
|
|
[Row("(-1e+1 -2e2i)", -10, -200)] |
|
|
|
[Row("(-1e1 -2e+2i)", -10, -200)] |
|
|
|
[Row("(-1e-1 -2E2i)", -0.1, -200)] |
|
|
|
[Row("(-1e1 -2e-2i)", -10, -0.02)] |
|
|
|
[Row("(-1E+1 -2e+2i)", -10, -200)] |
|
|
|
[Row("(-1e-1,-2e-2i)", -0.1, -0.02)] |
|
|
|
[Row("(+1 +2i)", 1, 2)] |
|
|
|
public void CanConvertStringToComplexUsingTryParse(string str, double expectedReal, double expectedImag) |
|
|
|
{ |
|
|
|
Complex z; |
|
|
|
var ret = Complex.TryParse(str, out z); |
|
|
|
Assert.IsTrue(ret); |
|
|
|
Assert.AreEqual(expectedReal, z.Real); |
|
|
|
Assert.AreEqual(expectedImag, z.Imaginary); |
|
|
|
|
|
|
|
ret = Complex.TryParse("(-1E+1 -2e+2i)", out z); |
|
|
|
Assert.IsTrue(ret); |
|
|
|
Assert.AreEqual(-10, z.Real); |
|
|
|
Assert.AreEqual(-200, z.Imaginary); |
|
|
|
|
|
|
|
ret = Complex.TryParse("(-1e-1,-2e-2i)", out z); |
|
|
|
Assert.IsTrue(ret); |
|
|
|
Assert.AreEqual(-.1, z.Real); |
|
|
|
Assert.AreEqual(-.02, z.Imaginary); |
|
|
|
|
|
|
|
ret = Complex.TryParse("(+1 +2i)", out z); |
|
|
|
Assert.IsTrue(ret); |
|
|
|
Assert.AreEqual(1, z.Real); |
|
|
|
Assert.AreEqual(2, z.Imaginary); |
|
|
|
} |
|
|
|
|
|
|
|
[Test] |
|
|
|
public void CanParseStringToComplex() |
|
|
|
{ |
|
|
|
var actual = Complex.Parse("-1 -2i"); |
|
|
|
Assert.AreEqual(new Complex(-1,-2), actual); |
|
|
|
} |
|
|
|
|
|
|
|
[Test] |
|
|
|
public void ParseThrowsFormatExceptionIfMissingClosingParen() |
|
|
|
{ |
|
|
|
Assert.Throws<FormatException>(() => Complex.Parse("(1,2")); |
|
|
|
} |
|
|
|
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanCreateComplexNumberUsingTheConstructor() |
|
|
|
@ -267,9 +292,6 @@ |
|
|
|
Assert.AreEqual(-2.2, complex.Imaginary, "Imaginary part is -2.2."); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can create complex number with modulus argument.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanCreateComplexNumberWithModulusArgument() |
|
|
|
@ -279,9 +301,6 @@ |
|
|
|
Assert.AreApproximatelyEqual(-1, complex.Imaginary, 1e-15, "Imaginary part is -1."); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can create complex number with real imaginary intializer.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanCreateComplexNumberWithRealImaginaryIntializer() |
|
|
|
@ -291,9 +310,6 @@ |
|
|
|
Assert.AreEqual(-2.2, complex.Imaginary, "Imaginary part is -2.2."); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can create string from complex number.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanCreateStringFromComplexNumber() |
|
|
|
@ -306,9 +322,6 @@ |
|
|
|
Assert.AreEqual("1.1 + 1.1i", new Complex(1.1, 1.1).ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can create string using format provider.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanCreateStringUsingFormatProvider() |
|
|
|
@ -322,9 +335,6 @@ |
|
|
|
Assert.AreEqual("1,1 + 1,1i", new Complex(1.1, 1.1).ToString(provider)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can create string using number format.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanCreateStringUsingNumberFormat() |
|
|
|
@ -337,9 +347,6 @@ |
|
|
|
Assert.AreEqual("1.100 + 1.100i", new Complex(1.1, 1.1).ToString("#.000")); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can determine if imaginary unit.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
public void CanDetermineIfImaginaryUnit() |
|
|
|
{ |
|
|
|
@ -347,9 +354,6 @@ |
|
|
|
Assert.IsTrue(complex.IsI, "Imaginary unit"); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can determine if infinity.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanDetermineIfInfinity() |
|
|
|
@ -362,9 +366,6 @@ |
|
|
|
Assert.IsTrue(complex.IsInfinity, "Both parts are infinity."); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can determine if na n.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanDetermineIfNaN() |
|
|
|
@ -377,9 +378,6 @@ |
|
|
|
Assert.IsTrue(complex.IsNaN, "Both parts are NaN."); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can determine if one value complex number.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
public void CanDetermineIfOneValueComplexNumber() |
|
|
|
{ |
|
|
|
@ -387,9 +385,6 @@ |
|
|
|
Assert.IsTrue(complex.IsOne, "Complex number with a value of one."); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can determine if real non negative number.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
public void CanDetermineIfRealNonNegativeNumber() |
|
|
|
{ |
|
|
|
@ -397,9 +392,6 @@ |
|
|
|
Assert.IsTrue(complex.IsReal, "Is a real non-negative number."); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can determine if real number.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
public void CanDetermineIfRealNumber() |
|
|
|
{ |
|
|
|
@ -407,9 +399,6 @@ |
|
|
|
Assert.IsTrue(complex.IsReal, "Is a real number."); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can determine if zero value complex number.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
public void CanDetermineIfZeroValueComplexNumber() |
|
|
|
{ |
|
|
|
@ -417,9 +406,6 @@ |
|
|
|
Assert.IsTrue(complex.IsZero, "Zero complex number."); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can divide complex number and double using operators.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanDivideComplexNumberAndDoubleUsingOperators() |
|
|
|
@ -427,12 +413,10 @@ |
|
|
|
AssertEx.That(() => (Complex.NaN * 1.0).IsNaN); |
|
|
|
Assert.AreEqual(new Complex(-2, 2), new Complex(4, -4) / -2); |
|
|
|
Assert.AreEqual(new Complex(0.25, 0.25), 2 / new Complex(4, -4)); |
|
|
|
Assert.AreEqual(Complex.Infinity, 2.0 / Complex.Zero); |
|
|
|
Assert.AreEqual(Complex.Infinity, Complex.One / 0); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can divide two complex numbers.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanDivideTwoComplexNumbers() |
|
|
|
@ -442,9 +426,6 @@ |
|
|
|
Assert.AreEqual(Complex.Infinity, Complex.One.Divide(Complex.Zero)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can divide two complex numbers using operators.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanDivideTwoComplexNumbersUsingOperators() |
|
|
|
@ -454,9 +435,6 @@ |
|
|
|
Assert.AreEqual(Complex.Infinity, Complex.One / Complex.Zero); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can multiple complex number and double using operators.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanMultipleComplexNumberAndDoubleUsingOperators() |
|
|
|
@ -466,9 +444,6 @@ |
|
|
|
Assert.AreEqual(new Complex(8, -8), 2 * new Complex(4, -4)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can multiple two complex numbers.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanMultipleTwoComplexNumbers() |
|
|
|
@ -477,9 +452,6 @@ |
|
|
|
Assert.AreEqual(new Complex(0, 16), new Complex(4, -4).Multiply(new Complex(-2, 2))); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can multiple two complex numbers using operators.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanMultipleTwoComplexNumbersUsingOperators() |
|
|
|
@ -488,9 +460,6 @@ |
|
|
|
Assert.AreEqual(new Complex(0, 16), new Complex(4, -4) * new Complex(-2, 2)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can negate value.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
public void CanNegateValue() |
|
|
|
{ |
|
|
|
@ -498,9 +467,6 @@ |
|
|
|
Assert.AreEqual(new Complex(-1.1, 2.2), complex.Negate()); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can negate value using operator.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
public void CanNegateValueUsingOperator() |
|
|
|
{ |
|
|
|
@ -508,9 +474,6 @@ |
|
|
|
Assert.AreEqual(new Complex(-1.1, 2.2), -complex); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can subtract complex number and double using operartor.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanSubtractComplexNumberAndDoubleUsingOperartor() |
|
|
|
@ -525,9 +488,6 @@ |
|
|
|
AssertEx.That(() => -2.2 - new Complex(-1.1, 2.2) == new Complex(-1.1, -2.2)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can subtract two complex numbers.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanSubtractTwoComplexNumbers() |
|
|
|
@ -538,9 +498,6 @@ |
|
|
|
AssertEx.That(() => new Complex(1.1, -2.2).Subtract(new Complex(1.1, -2.2)) == Complex.Zero); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can test for equality.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanTestForEquality() |
|
|
|
@ -551,9 +508,6 @@ |
|
|
|
Assert.AreNotEqual(new Complex(-1.1, 2.2), new Complex(1.1, -2.2)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can test for equality using operators.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
[MultipleAsserts] |
|
|
|
public void CanTestForEqualityUsingOperators() |
|
|
|
@ -564,9 +518,6 @@ |
|
|
|
AssertEx.That(() => new Complex(-1.1, 2.2) != new Complex(1.1, -2.2)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can use plus.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
public void CanUsePlus() |
|
|
|
{ |
|
|
|
@ -574,9 +525,6 @@ |
|
|
|
Assert.AreEqual(complex, complex.Plus()); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The can use plus operator.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
public void CanUsePlusOperator() |
|
|
|
{ |
|
|
|
@ -584,43 +532,6 @@ |
|
|
|
Assert.AreEqual(complex, +complex); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The with modulus argument throws argument out of range exception.
|
|
|
|
/// </summary>
|
|
|
|
[Test] |
|
|
|
public void WithModulusArgumentThrowsArgumentOutOfRangeException() |
|
|
|
{ |
|
|
|
Assert.Throws<ArgumentOutOfRangeException>( |
|
|
|
() => Complex.WithModulusArgument(-1, 1), "Throws exception because modulus is negative."); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
[Row(1,-2,"1 -2i")] |
|
|
|
[Row(1, 2, "1 + 2i")] |
|
|
|
[Row(1, 0, "1")] |
|
|
|
[Row(0, -2, "-2i")] |
|
|
|
[Row(0, 2, "2i")] |
|
|
|
public void CanConvertComplexToString(double real, double imag, string expected) |
|
|
|
{ |
|
|
|
var a = new Complex(real, imag); |
|
|
|
Assert.AreEqual<string>(expected, a.ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
[Test] |
|
|
|
[Row("")] |
|
|
|
[Row("+")] |
|
|
|
[Row("1i+2")] |
|
|
|
[Row(null)] |
|
|
|
public void TryParseReturnsFalseWhenGiveBadValue(string str) |
|
|
|
{ |
|
|
|
Complex z; |
|
|
|
bool ret = Complex.TryParse(str, out z); |
|
|
|
Assert.IsFalse(ret); |
|
|
|
Assert.AreEqual(0, z.Real); |
|
|
|
Assert.AreEqual(0, z.Imaginary); |
|
|
|
} |
|
|
|
|
|
|
|
[Test] |
|
|
|
public void TryParseCanHandleSymbols() |
|
|
|
{ |
|
|
|
@ -650,57 +561,50 @@ |
|
|
|
Assert.IsTrue(ret); |
|
|
|
Assert.AreEqual(double.MaxValue, z.Real); |
|
|
|
Assert.AreEqual(double.MinValue, z.Imaginary); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
[Test] |
|
|
|
[Row("-1", -1, 0)] |
|
|
|
[Row("-i", 0, -1)] |
|
|
|
[Row("i", 0, 1)] |
|
|
|
[Row("2i", 0, 2)] |
|
|
|
[Row("1 + 2i", 1, 2)] |
|
|
|
[Row("1+2i", 1, 2)] |
|
|
|
[Row("1 - 2i", 1, -2)] |
|
|
|
[Row("1-2i", 1, -2)] |
|
|
|
[Row("1,2", 1, 2)] |
|
|
|
[Row("1 , 2", 1, 2)] |
|
|
|
[Row("1,2i", 1, 2)] |
|
|
|
[Row("-1, -2i", -1, -2)] |
|
|
|
[Row("(+1,2i)", 1, 2)] |
|
|
|
[Row("(-1 , -2)", -1, -2)] |
|
|
|
[Row("(-1 , -2i)", -1, -2)] |
|
|
|
[Row("(+1e1 , -2e-2i)", 10, -0.02)] |
|
|
|
[Row("(-1E1 -2e2i)", -10, -200)] |
|
|
|
[Row("(-1e+1 -2e2i)", -10, -200)] |
|
|
|
[Row("(-1e1 -2e+2i)", -10, -200)] |
|
|
|
[Row("(-1e-1 -2E2i)", -0.1, -200)] |
|
|
|
[Row("(-1e1 -2e-2i)", -10, -0.02)] |
|
|
|
[Row("(-1E+1 -2e+2i)", -10, -200)] |
|
|
|
[Row("(-1e-1,-2e-2i)", -0.1, -0.02)] |
|
|
|
[Row("(+1 +2i)", 1, 2)] |
|
|
|
public void CanConvertStringToComplexUsingTryParse(string str, double expectedReal, double expectedImag) |
|
|
|
[Row("")] |
|
|
|
[Row("+")] |
|
|
|
[Row("1i+2")] |
|
|
|
[Row(null)] |
|
|
|
public void TryParseReturnsFalseWhenGiveBadValue(string str) |
|
|
|
{ |
|
|
|
Complex z; |
|
|
|
var ret = Complex.TryParse(str, out z); |
|
|
|
Assert.IsTrue(ret); |
|
|
|
Assert.AreEqual(expectedReal, z.Real); |
|
|
|
Assert.AreEqual(expectedImag, z.Imaginary); |
|
|
|
|
|
|
|
ret = Complex.TryParse("(-1E+1 -2e+2i)", out z); |
|
|
|
Assert.IsTrue(ret); |
|
|
|
Assert.AreEqual(-10, z.Real); |
|
|
|
Assert.AreEqual(-200, z.Imaginary); |
|
|
|
Assert.IsFalse(ret); |
|
|
|
Assert.AreEqual(0, z.Real); |
|
|
|
Assert.AreEqual(0, z.Imaginary); |
|
|
|
} |
|
|
|
|
|
|
|
ret = Complex.TryParse("(-1e-1,-2e-2i)", out z); |
|
|
|
Assert.IsTrue(ret); |
|
|
|
Assert.AreEqual(-.1, z.Real); |
|
|
|
Assert.AreEqual(-.02, z.Imaginary); |
|
|
|
[Test] |
|
|
|
public void WithModulusArgumentThrowsArgumentOutOfRangeException() |
|
|
|
{ |
|
|
|
Assert.Throws<ArgumentOutOfRangeException>( |
|
|
|
() => Complex.WithModulusArgument(-1, 1), "Throws exception because modulus is negative."); |
|
|
|
} |
|
|
|
|
|
|
|
ret = Complex.TryParse("(+1 +2i)", out z); |
|
|
|
Assert.IsTrue(ret); |
|
|
|
Assert.AreEqual(1, z.Real); |
|
|
|
Assert.AreEqual(2, z.Imaginary); |
|
|
|
[Test] |
|
|
|
[Row(0.0, 0.0, 0.0)] |
|
|
|
[Row(0.0, 1.0, 1.0)] |
|
|
|
[Row(-1.0, 1.0, 1.4142135623730951)] |
|
|
|
[Row(-111.1, 111.1, 157.11912677965086)] |
|
|
|
public void CanComputeModulus(double real, double imag, double expected) |
|
|
|
{ |
|
|
|
Assert.AreEqual(expected, new Complex(real, imag).Modulus); |
|
|
|
} |
|
|
|
|
|
|
|
[Test] |
|
|
|
[Row(double.PositiveInfinity, double.PositiveInfinity, Constants.Sqrt1Over2, Constants.Sqrt1Over2)] |
|
|
|
[Row(double.PositiveInfinity, double.NegativeInfinity, Constants.Sqrt1Over2, -Constants.Sqrt1Over2)] |
|
|
|
[Row(double.NegativeInfinity, double.PositiveInfinity, -Constants.Sqrt1Over2, -Constants.Sqrt1Over2)] |
|
|
|
[Row(double.NegativeInfinity, double.NegativeInfinity, -Constants.Sqrt1Over2, Constants.Sqrt1Over2)] |
|
|
|
[Row(0.0, 0.0, 0.0, 0.0)] |
|
|
|
[Row(-1.0, 1.0, -0.70710678118654746, 0.70710678118654746)] |
|
|
|
[Row(-111.1, 111.1, -0.70710678118654746, 0.70710678118654746)] |
|
|
|
public void CanComputeSign(double real, double imag, double expectedReal, double expectedImag) |
|
|
|
{ |
|
|
|
Assert.AreEqual(new Complex(expectedReal, expectedImag), new Complex(real, imag).Sign); |
|
|
|
} |
|
|
|
} |
|
|
|
} |