Ejemplo n.º 1
0
        public void Run_CorrectData_InvokesMethods()
        {
            // arrange
            var a1           = 1.0;
            var b1           = 2.0;
            var c1           = 3.0;
            var a2           = -0.8;
            var b2           = 2.0;
            var c2           = 3.5;
            var executorMock = new Mock <IExecutorQuadraticEquation>();

            executorMock.Setup(e => e.Execute(It.IsAny <double>(), It.IsAny <double>(), It.IsAny <double>()))
            .Returns(SolutionEquation.CreateWithoutSolutions());

            var readerMock = new Mock <IReaderQuadraticEquations>();

            readerMock.Setup(r => r.Read())
            .Returns(new List <ParamsEquation> {
                new ParamsEquation(a1, b1, c1), new ParamsEquation(a2, b2, c2)
            });
            var factoryMock = new Mock <IFactoryReaderQuadraticEquations>();

            factoryMock.Setup(f => f.CreateReaderQuadraticEquations())
            .Returns(readerMock.Object);
            var process = new ProcessSolvingQuadraticEquations(executorMock.Object, factoryMock.Object);

            // act
            process.Run();

            // assert
            factoryMock.Verify(f => f.CreateReaderQuadraticEquations(), Times.Once);
            readerMock.Verify(f => f.Read(), Times.Once);
            executorMock.Verify(f => f.Execute(It.Is <double>(a => a1 == a), It.Is <double>(b => b1 == b), It.Is <double>(c => c1 == c)), Times.Once);
            executorMock.Verify(f => f.Execute(It.Is <double>(a => a2 == a), It.Is <double>(b => b2 == b), It.Is <double>(c => c2 == c)), Times.Once);
        }
        public SolutionEquation Execute(double a, double b, double c)
        {
            if (a == 0)
            {
                if (b == 0)
                {
                    return(SolutionEquation.CreateInfinityCountSolutions());
                }
                return(SolutionEquation.CreateSolutions(-c / b));
            }

            var descrimenant = b * b - 4 * a * c;

            if (descrimenant < 0)
            {
                return(SolutionEquation.CreateWithoutSolutions());
            }

            if (descrimenant == 0)
            {
                return(SolutionEquation.CreateSolutions(-b / (2 * a)));
            }

            var sqrtDescrimenant = Math.Sqrt(descrimenant);

            var x1 = (-b - sqrtDescrimenant) / (2 * a);
            var x2 = (-b + sqrtDescrimenant) / (2 * a);

            return(SolutionEquation.CreateSolutions(x1, x2));
        }
Ejemplo n.º 3
0
        public void GetViewSolution_InfinityCountSolutions_CorrectPartView()
        {
            // arrange
            var paramsEquation = new ParamsEquation(0, 0, 0);
            var s = SolutionEquation.CreateInfinityCountSolutions();

            // act
            var view = paramsEquation.GetViewSolution(s);

            // assert
            Assert.IsTrue(view.Contains(paramsEquation.ToString()));
            Assert.IsTrue(view.Contains("бесконечное число корней"));
        }
Ejemplo n.º 4
0
        public void GetViewSolution_NotSolutions_CorrectPartView()
        {
            // arrange
            var paramsEquation = new ParamsEquation(0, 0, 0);
            var s = SolutionEquation.CreateWithoutSolutions();

            // act
            var view = paramsEquation.GetViewSolution(s);

            // assert
            Assert.IsTrue(view.Contains(paramsEquation.ToString()));
            Assert.IsTrue(view.Contains("нет корней"));
        }
Ejemplo n.º 5
0
        public void GetViewSolution_OneSolutions_CorrectPartView()
        {
            // arrange
            var x = 1;
            var paramsEquation = new ParamsEquation(0, 0, 0);
            var s = SolutionEquation.CreateSolutions(x);

            // act
            var view = paramsEquation.GetViewSolution(s);

            // assert
            Assert.IsTrue(view.Contains(paramsEquation.ToString()));
            Assert.IsTrue(view.Contains($"один корень: x = {x}"));
        }
Ejemplo n.º 6
0
        public void GetViewSolution_TwoSolutions_CorrectPartView()
        {
            // arrange
            var x1             = 1;
            var x2             = 2;
            var paramsEquation = new ParamsEquation(0, 0, 0);
            var s = SolutionEquation.CreateSolutions(x1, x2);

            // act
            var view = paramsEquation.GetViewSolution(s);

            // assert
            Assert.IsTrue(view.Contains(paramsEquation.ToString()));
            Assert.IsTrue(view.Contains($"два корня: x1 = {x1}, x2 = {x2}"));
        }
        public static string GetViewSolution(this ParamsEquation paramsEquation, SolutionEquation solutionEquation)
        {
            switch (solutionEquation.CountSolutions)
            {
            case CountSolutionsEquation.ONE:
                return($"У уравнения {paramsEquation} один корень: x = {solutionEquation.X1}.");

            case CountSolutionsEquation.TWO:
                return($"У уравнения {paramsEquation} два корня: x1 = {solutionEquation.X1}, x2 = {solutionEquation.X2}.");

            case CountSolutionsEquation.INFINITY_COUNT:
                return($"У уравнения {paramsEquation} бесконечное число корней.");

            default:
                return($"У уравнения {paramsEquation} нет корней.");
            }
        }