From be0d7f8619967350008b3ffc9ec33c4a57c3fd2f Mon Sep 17 00:00:00 2001 From: omyrosnikova Date: Sun, 20 Mar 2022 21:07:31 +0100 Subject: [PATCH 1/4] fixed comments --- QuadraticHelper.cs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/QuadraticHelper.cs b/QuadraticHelper.cs index ecffd6e..4144d8e 100644 --- a/QuadraticHelper.cs +++ b/QuadraticHelper.cs @@ -13,19 +13,35 @@ public static double[] SolveIncompleteQuadraticEquation(double b) public static double[] Solve(double a, double b, double c) { - if (a == default) - throw new ArgumentException("Argument a can not be 0"); + + try { + ParametersValidation(a, b, c); + + double d = b * b - 4 * a * c; + + var x1 = (-b + Math.Sqrt(d)) / 2 * a; + var x2 = (-b - Math.Sqrt(d)) / 2 * a; + + return new double[] { x1, x2 }; + } catch() + { + } + } - double d = b * b - 4 * a * c; + private static void ParametersValidation(double a, double b, double c) + { + if (a.EqualsExact(0)) + throw new ArgumentException("Argument a can not be 0"); - if (d.EqualsExact(0)) - throw new ArgumentException("Diskriminant is 0"); + if (double.IsFinite(a) || double.IsNaN(a)) + throw new ArgumentException("Argument a is incorrect"); - var x1 = (-b + Math.Sqrt(d)) / 2 * a; - var x2 = (-b - Math.Sqrt(d)) / 2 * a; + if (double.IsFinite(b) || double.IsNaN(b)) + throw new ArgumentException("Argument b is incorrect"); - return new double[] { x1, x2 }; + if (double.IsFinite(c) || double.IsNaN(c)) + throw new ArgumentException("Argument c is incorrect"); } } } From 29ddf3d7081e0512faa52e1727252cc90e732c11 Mon Sep 17 00:00:00 2001 From: omyrosnikova Date: Sun, 20 Mar 2022 21:13:57 +0100 Subject: [PATCH 2/4] save changes --- QuadraticHelper.cs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/QuadraticHelper.cs b/QuadraticHelper.cs index 4144d8e..d20279c 100644 --- a/QuadraticHelper.cs +++ b/QuadraticHelper.cs @@ -14,18 +14,14 @@ public static double[] SolveIncompleteQuadraticEquation(double b) public static double[] Solve(double a, double b, double c) { - try { - ParametersValidation(a, b, c); + ParametersValidation(a, b, c); - double d = b * b - 4 * a * c; + double d = b * b - 4 * a * c; - var x1 = (-b + Math.Sqrt(d)) / 2 * a; - var x2 = (-b - Math.Sqrt(d)) / 2 * a; + var x1 = (-b + Math.Sqrt(d)) / 2 * a; + var x2 = (-b - Math.Sqrt(d)) / 2 * a; - return new double[] { x1, x2 }; - } catch() - { - } + return new double[] { x1, x2 }; } @@ -34,13 +30,13 @@ private static void ParametersValidation(double a, double b, double c) if (a.EqualsExact(0)) throw new ArgumentException("Argument a can not be 0"); - if (double.IsFinite(a) || double.IsNaN(a)) + if (!double.IsFinite(a) || double.IsNaN(a)) throw new ArgumentException("Argument a is incorrect"); - if (double.IsFinite(b) || double.IsNaN(b)) + if (!double.IsFinite(b) || double.IsNaN(b)) throw new ArgumentException("Argument b is incorrect"); - if (double.IsFinite(c) || double.IsNaN(c)) + if (!double.IsFinite(c) || double.IsNaN(c)) throw new ArgumentException("Argument c is incorrect"); } } From e6ca09addd480313c5d3bc3aaffd69b1fcb703ba Mon Sep 17 00:00:00 2001 From: omyrosnikova Date: Sun, 20 Mar 2022 21:26:20 +0100 Subject: [PATCH 3/4] fixed tests --- MathTests.cs | 21 +++++++++++++++++++++ QuadraticHelper.cs | 19 +++++++++++-------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/MathTests.cs b/MathTests.cs index ab05ffb..d0161a4 100644 --- a/MathTests.cs +++ b/MathTests.cs @@ -74,4 +74,25 @@ public void SolveQaudratic_WithPositive_A_Argument_Should_NotBeNull() } } + + [Theory] + [Trait("Category", "Unit")] + [InlineData(double.NaN, 2, 3, "Argument a is incorrect")] + [InlineData(4, double.NaN, 3, "Argument b is incorrect")] + [InlineData(5, 2, double.NaN, "Argument c is incorrect")] + public void SolveQaudratic_With_A_NaN_Argument_Should_ThrowException(double a, double b, double c, string messageError) + { + + // Act + try + { + var result = QuadraticHelper.Solve(a, b, c); + } + catch (Exception e) + { + // Assert + e.Message.ShouldBe(messageError); + } + + } } diff --git a/QuadraticHelper.cs b/QuadraticHelper.cs index d20279c..27d5dc6 100644 --- a/QuadraticHelper.cs +++ b/QuadraticHelper.cs @@ -4,7 +4,9 @@ public static class QuadraticHelper { public static double[] SolveIncompleteQuadraticEquation(double b) { - if (b < 0) + ValidateDoubleParameter(b, "Argument b is incorrect"); + + if (b.CompareTo(0) < 0) return new double[0]; var res = Math.Sqrt(b); @@ -30,14 +32,15 @@ private static void ParametersValidation(double a, double b, double c) if (a.EqualsExact(0)) throw new ArgumentException("Argument a can not be 0"); - if (!double.IsFinite(a) || double.IsNaN(a)) - throw new ArgumentException("Argument a is incorrect"); - - if (!double.IsFinite(b) || double.IsNaN(b)) - throw new ArgumentException("Argument b is incorrect"); + ValidateDoubleParameter(a, "Argument a is incorrect"); + ValidateDoubleParameter(b, "Argument b is incorrect"); + ValidateDoubleParameter(c, "Argument c is incorrect"); + } - if (!double.IsFinite(c) || double.IsNaN(c)) - throw new ArgumentException("Argument c is incorrect"); + private static void ValidateDoubleParameter(double val, string errorMessage) + { + if (!double.IsFinite(val) || double.IsNaN(val)) + throw new ArgumentException(errorMessage); } } } From 4569eb330b6c4797fbf3e99c787ca01b1704e5d1 Mon Sep 17 00:00:00 2001 From: omyrosnikova Date: Thu, 24 Mar 2022 20:22:29 +0100 Subject: [PATCH 4/4] fixed bug --- MathTests.cs | 5 ++--- QuadraticHelper.cs | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/MathTests.cs b/MathTests.cs index d0161a4..fdcb9c4 100644 --- a/MathTests.cs +++ b/MathTests.cs @@ -49,8 +49,8 @@ public void SolveQaudratic_WithPositive_C_Argument_Should_ReturnOneSameRoot() var result = QuadraticHelper.Solve(a, b, c); // Assert - result.Length.ShouldBe(2); - result[1].ShouldBe(result[0]); + result.Length.ShouldBe(1); + result[0].ShouldBe(result[0]); } [Fact] @@ -93,6 +93,5 @@ public void SolveQaudratic_With_A_NaN_Argument_Should_ThrowException(double a, d // Assert e.Message.ShouldBe(messageError); } - } } diff --git a/QuadraticHelper.cs b/QuadraticHelper.cs index 27d5dc6..03644d5 100644 --- a/QuadraticHelper.cs +++ b/QuadraticHelper.cs @@ -20,11 +20,18 @@ public static double[] Solve(double a, double b, double c) double d = b * b - 4 * a * c; - var x1 = (-b + Math.Sqrt(d)) / 2 * a; - var x2 = (-b - Math.Sqrt(d)) / 2 * a; - - return new double[] { x1, x2 }; - + if (d.CompareTo(0) < 0) + return new double[0]; + else if (d.CompareTo(0) == 0) + { + var x1 = (-b) / 2 * a; + return new double[] { x1 }; + } else + { + var x1 = (-b + Math.Sqrt(d)) / 2 * a; + var x2 = (-b - Math.Sqrt(d)) / 2 * a; + return new double[] { x1, x2 }; + } } private static void ParametersValidation(double a, double b, double c)