diff --git a/build.ps1 b/build.ps1 new file mode 100644 index 0000000..e349ce2 --- /dev/null +++ b/build.ps1 @@ -0,0 +1,18 @@ +<# +.SYNOPSIS + +Builds and runs tests for the Complex Calculator project +#> +if (-not (Get-Command MsBuild -ErrorAction SilentlyContinue)) +{ + Get-VsVars32 +} + +MSBuild .\src\ComplexCalculator\ComplexCalculator.sln + +if ($LASTEXITCODE -eq 0) +{ + .\src\ComplexCalculator\packages\xunit.runner.console.2.3.0-beta3-build3705\tools\net452\xunit.console.exe .\src\ComplexCalculator.Tests\bin\Debug\ComplexCalculator.Tests.dll +} + +exit $LASTEXITCODE diff --git a/src/ComplexCalculator.Tests/ComplexCalculator.Tests.csproj b/src/ComplexCalculator.Tests/ComplexCalculator.Tests.csproj index e81e4ec..4a81871 100644 --- a/src/ComplexCalculator.Tests/ComplexCalculator.Tests.csproj +++ b/src/ComplexCalculator.Tests/ComplexCalculator.Tests.csproj @@ -1,5 +1,6 @@  + @@ -56,7 +57,7 @@ - + @@ -68,6 +69,12 @@ + + + {75d70f12-d503-450e-9ec8-d32be6491aef} + ComplexCalculator + + @@ -76,6 +83,7 @@ + \ No newline at end of file diff --git a/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs b/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs new file mode 100644 index 0000000..e26e68d --- /dev/null +++ b/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs @@ -0,0 +1,79 @@ +using Xunit; + +namespace ComplexCalculator.Tests +{ + public class ComplexNumberTestClass + { + [Fact] + public void WhenRealPartSet_ItCanBeReturned() + { + // Arrange + var num = new ComplexNumber(); + + // Act + num.Real = 42; + + // Assert + Assert.Equal(42, num.Real); + } + + [Fact] + public void WhenImaginaryPartSet_ItCanBeReturned() + { + // Arrange + var num = new ComplexNumber(); + + // Act + num.Imaginary = 13; + + // Assert + Assert.Equal(13, num.Imaginary); + } + + [Fact] + public void WhenModulousIsCalled_ItIsCalculatedAndReturned() + { + // Arrange + var num = new ComplexNumber { Real = 3, Imaginary = 4 }; + + // Act + var mod = num.Modulous(); + + // Assert + Assert.Equal(5, mod); + } + + [Theory()] + [InlineData(3.5, -2.8, "3.5-2.8i")] + [InlineData(7.2, 4.1, "7.2+4.1i")] + public void WhenToStringCalled_ReturnsStringREpresentationOfComplexNumber(double real, double imaginary, string result) + { + // Arrange + var num = new ComplexNumber { Real = real, Imaginary = imaginary }; + + // Act + var str = num.ToString(); + + // Assert + Assert.Equal(result, str); + } + + [Theory()] + [InlineData(1, 2, 1, 2, 2, 4)] + [InlineData(1, 1, -1, -1, 0, 0)] + [InlineData(1, 3, -2, -5, -1, -2)] + void WhenComplexNumbersAdded_TheirRealPartsAndImaginaryPartsAreAdded(double r1, double i1, double r2, double i2, double r3, double i3) + { + // Arrange + var n1 = new ComplexNumber { Real = r1, Imaginary = i1 }; + var n2 = new ComplexNumber { Real = r2, Imaginary = i2 }; + + // Act + var n3 = n1.Add(n2); + + // Assert + Assert.Equal(r3, n3.Real); + Assert.Equal(i3, n3.Imaginary); + } + } +} diff --git a/src/ComplexCalculator.Tests/TestClass.cs b/src/ComplexCalculator.Tests/TestClass.cs deleted file mode 100644 index fadd9a0..0000000 --- a/src/ComplexCalculator.Tests/TestClass.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Xunit; - -namespace ComplexCalculator.Tests -{ - public class TestClass - { - [Fact] - public void FirstTest() - { - Assert.True(true); - } - } -} diff --git a/src/ComplexCalculator.Tests/packages.config b/src/ComplexCalculator.Tests/packages.config index 21c654a..918f657 100644 --- a/src/ComplexCalculator.Tests/packages.config +++ b/src/ComplexCalculator.Tests/packages.config @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/src/ComplexCalculator/ComplexCalculator.csproj b/src/ComplexCalculator/ComplexCalculator.csproj index 0e5c7c8..89d86b3 100644 --- a/src/ComplexCalculator/ComplexCalculator.csproj +++ b/src/ComplexCalculator/ComplexCalculator.csproj @@ -42,6 +42,7 @@ + diff --git a/src/ComplexCalculator/ComplexNumber.cs b/src/ComplexCalculator/ComplexNumber.cs new file mode 100644 index 0000000..4c5119c --- /dev/null +++ b/src/ComplexCalculator/ComplexNumber.cs @@ -0,0 +1,31 @@ +using System; + +namespace ComplexCalculator +{ + public class ComplexNumber + { + public double Real { get; set; } + + public double Imaginary { get; set; } + + public double Modulous() + { + return Math.Sqrt(Real * Real + Imaginary * Imaginary); + } + + public override string ToString() + { + var sign = Imaginary > 0 ? "+" : ""; + return $"{Real}{sign}{Imaginary}i"; + } + + public ComplexNumber Add(ComplexNumber num) + { + return new ComplexNumber + { + Real = this.Real + num.Real, + Imaginary = this.Imaginary + num.Imaginary + }; + } + } +} diff --git a/src/ComplexCalculator/Program.cs b/src/ComplexCalculator/Program.cs index 0fc8f5c..e2253aa 100644 --- a/src/ComplexCalculator/Program.cs +++ b/src/ComplexCalculator/Program.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ComplexCalculator { @@ -10,6 +6,41 @@ class Program { static void Main(string[] args) { + Console.WriteLine("This program adds 2 complex numbers"); + ComplexNumber c1 = new ComplexNumber + { + Real = ReadDouble("Enter Real Part of first complex number"), + Imaginary = ReadDouble("Enter Imaginary part of first complex number") + }; + ComplexNumber c2 = new ComplexNumber + { + Real = ReadDouble("Enter Real Part of second complex number"), + Imaginary = ReadDouble("Enter Imaginary part of second complex number") + }; + + var c3 = c1.Add(c2); + + Console.WriteLine("{0} + {1} = {2} with modulous {3}", c1, c2, c3, c3.Modulous()); + } + + private static double ReadDouble(string message) + { + double val; + string s; + bool success = false; + do + { + Console.Write(message + ":"); + s = Console.ReadLine(); + success = double.TryParse(s, out val); + if (!success) + { + Console.WriteLine("ERROR please try again."); + } + } while (!success); + + return val; + } } }