From 1b76d7b77cfb34c3f901ec7407a8814191253b80 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 17 Mar 2023 21:38:47 +0300 Subject: [PATCH 1/5] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D1=8B?= =?UTF-8?q?=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D1=81?= =?UTF-8?q?=D1=82=D0=B5=D0=BA=D0=B0=20=D0=B8=20=D1=81=D1=82=D0=B5=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=B0=D0=BB=D1=8C=D0=BA?= =?UTF-8?q?=D1=83=D0=BB=D1=8F=D1=82=D0=BE=D1=80=D0=B0,=20=D0=B2=D1=8B?= =?UTF-8?q?=D1=8F=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=B8=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88?= =?UTF-8?q?=D0=B8=D0=B1=D0=BA=D0=B0=20=D0=B2=20=D1=85=D0=BE=D0=B4=D0=B5=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NewPostfixCalculatorWithModuleTests.sln | 37 ++++++ .../InterfaceForStack.cs | 17 +++ ...NewPostfixCalculatorWithModuleTests.csproj | 10 ++ .../Program.cs | 19 +++ .../Stack.cs | 17 +++ .../StackCalculator.cs | 73 ++++++++++++ .../StackWIthArray.cs | 56 +++++++++ .../StackWithList.cs | 58 +++++++++ .../TestsForStack/TestsForStack.cs | 75 ++++++++++++ .../TestsForStackCalculator.cs | 110 ++++++++++++++++++ 10 files changed, 472 insertions(+) create mode 100644 NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests.sln create mode 100644 NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/InterfaceForStack.cs create mode 100644 NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests.csproj create mode 100644 NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Program.cs create mode 100644 NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Stack.cs create mode 100644 NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackCalculator.cs create mode 100644 NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWIthArray.cs create mode 100644 NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWithList.cs create mode 100644 NewPostfixCalculatorWithModuleTests/TestsForStack/TestsForStack.cs create mode 100644 NewPostfixCalculatorWithModuleTests/TestsForStackCalculator/TestsForStackCalculator.cs diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests.sln b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests.sln new file mode 100644 index 0000000..af23481 --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33403.182 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewPostfixCalculatorWithModuleTests", "NewPostfixCalculatorWithModuleTests\NewPostfixCalculatorWithModuleTests.csproj", "{6D1F274D-7F63-4D3D-A1C1-39AD50776432}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForStack", "TestsForStack\TestsForStack.csproj", "{FC7B71F8-A8D8-480C-985C-1104B79919E3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForStackCalculator", "TestsForStackCalculator\TestsForStackCalculator.csproj", "{CC67F651-06BB-4014-9E19-64E4B715B95A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6D1F274D-7F63-4D3D-A1C1-39AD50776432}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6D1F274D-7F63-4D3D-A1C1-39AD50776432}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6D1F274D-7F63-4D3D-A1C1-39AD50776432}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6D1F274D-7F63-4D3D-A1C1-39AD50776432}.Release|Any CPU.Build.0 = Release|Any CPU + {FC7B71F8-A8D8-480C-985C-1104B79919E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FC7B71F8-A8D8-480C-985C-1104B79919E3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FC7B71F8-A8D8-480C-985C-1104B79919E3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FC7B71F8-A8D8-480C-985C-1104B79919E3}.Release|Any CPU.Build.0 = Release|Any CPU + {CC67F651-06BB-4014-9E19-64E4B715B95A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CC67F651-06BB-4014-9E19-64E4B715B95A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CC67F651-06BB-4014-9E19-64E4B715B95A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CC67F651-06BB-4014-9E19-64E4B715B95A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B6FC0766-8CAA-4DB9-B5D4-B9458F46BC15} + EndGlobalSection +EndGlobal diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/InterfaceForStack.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/InterfaceForStack.cs new file mode 100644 index 0000000..3941104 --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/InterfaceForStack.cs @@ -0,0 +1,17 @@ +namespace StackCalculator; + +// Interface for the stack +interface IOperationsWithStack +{ + // Add element to stack + void AddElement(double value); + + // Remove element in stack and return deleted item + (bool, double) RemoveElement(); + + // Print all elements + void PrintTheElements(); + + // Checking that the stack is empty + bool IsEmpty(); +} \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests.csproj b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Program.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Program.cs new file mode 100644 index 0000000..62eb0f1 --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Program.cs @@ -0,0 +1,19 @@ +using StackCalculator; + +Console.WriteLine("Enter an example in the postfix form"); +var stringWithExpression = Console.ReadLine(); + +PostfixCalculator calculator = new PostfixCalculator(); +if (stringWithExpression == null) +{ + return; +} + +var stackList = new StackList(); +(bool isCorrectWork, double result) = calculator.ConvertToAResponse(stringWithExpression, stackList); +if (!isCorrectWork) +{ + Console.WriteLine("Problems with expression or you tried to divide by zero!"); + return; +} +Console.WriteLine(result); \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Stack.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Stack.cs new file mode 100644 index 0000000..ecb7f2f --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Stack.cs @@ -0,0 +1,17 @@ +namespace StackCalculator; + +//Standart stack +abstract public class Stack : IOperationsWithStack +{ + // Add element to stack + virtual public void AddElement(double value) { } + + // Remove element in stack and return deleted item + virtual public (bool, double) RemoveElement() { return (false, 0); } + + // Print all elements + virtual public void PrintTheElements() { } + + // Checking that the stack is empty + virtual public bool IsEmpty() { return false; } +} \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackCalculator.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackCalculator.cs new file mode 100644 index 0000000..6a7008d --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackCalculator.cs @@ -0,0 +1,73 @@ +namespace StackCalculator; + +// Calculator that counts algebraic expressions in postfix form +public class PostfixCalculator +{ + private const double delta = 0.0000000000001; + + // Receives the input string in which the expression is written in postfix form, finds the result + public (bool, double) ConvertToAResponse(string stringWithExpression, Stack stackExpression) + { + int i = 0; + string[] expressionArray = stringWithExpression.Split(' '); + while (i < expressionArray.Length) + { + var isCorrectNumber = Int32.TryParse(expressionArray[i], out var number); + if (isCorrectNumber) + { + stackExpression.AddElement(number); + } + else + { + if (expressionArray[i].Length != 1) + { + return (false, 0); + } + double numberAfter = 0; + (var isCorrect, var firstNumber) = stackExpression.RemoveElement(); + + if (!isCorrect) + { + return (false, 0); + } + + (isCorrect, var secondNumber) = stackExpression.RemoveElement(); + + if (!isCorrect) + { + return (false, 0); + } + + switch (expressionArray[i][0]) + { + case '*': + numberAfter = firstNumber * secondNumber; + break; + case '+': + numberAfter = firstNumber + secondNumber; + break; + case '-': + numberAfter = secondNumber - firstNumber; + break; + case '/': + if (Math.Abs(firstNumber) < delta) + { + return (false, 0); + } + numberAfter = secondNumber / firstNumber; + break; + default: + return (false, 0); + } + stackExpression.AddElement(numberAfter); + } + ++i; + } + (var isCorrectExpression, var result) = stackExpression.RemoveElement(); + if (!isCorrectExpression) + { + return (false, 0); + } + return stackExpression.IsEmpty() ? (true, result) : (false, 0); + } +} \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWIthArray.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWIthArray.cs new file mode 100644 index 0000000..d26f993 --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWIthArray.cs @@ -0,0 +1,56 @@ +namespace StackCalculator; + +// Stack implemented on an array +public class StackWithArray : Stack +{ + private double[] stackArray; + private int numberOfElements; + private int sizeStack = 10; + + public StackWithArray() + { + stackArray = new double[sizeStack]; + } + + public bool ChangeStackSize(int size) + { + if (size < sizeStack) + { + return false; + } + Array.Resize(ref stackArray, size); + return true; + } + + public override void AddElement(double value) + { + if (numberOfElements == sizeStack) + { + ChangeStackSize(sizeStack * 2); + } + stackArray[numberOfElements] = value; + ++numberOfElements; + } + + public override (bool, double) RemoveElement() + { + if (numberOfElements == 0) + { + return (false, 0); + } + double result = stackArray[numberOfElements - 1]; + --numberOfElements; + return (true, result); + } + + public override void PrintTheElements() + { + for (int i = 0; i < numberOfElements; i++) + { + Console.WriteLine(stackArray[i]); + } + } + + public override bool IsEmpty() + => numberOfElements == 0; +} \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWithList.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWithList.cs new file mode 100644 index 0000000..e221c0c --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWithList.cs @@ -0,0 +1,58 @@ +namespace StackCalculator; + +// Stack implemented on list +public class StackList : Stack +{ + private StackElement headStack; + + public override void AddElement(double value) + { + var item = new StackElement(value); + if (headStack == null) + { + headStack = item; + } + else + { + StackElement copy = headStack; + headStack = item; + headStack.Next = copy; + } + } + + public override (bool, double) RemoveElement() + { + if (headStack == null) + { + return (false, 0); + } + double item = headStack.ValueStack; + StackElement copy = headStack.Next; + headStack = copy; + return (true, item); + } + + public override void PrintTheElements() + { + StackElement walker = headStack; + while (walker != null) + { + Console.WriteLine(walker.ValueStack); + walker = walker.Next; + } + } + + public override bool IsEmpty() => headStack == null; + + private class StackElement + { + public StackElement(double value) + { + ValueStack = value; + Next = null; + } + + public double ValueStack { get; set; } + public StackElement Next { get; set; } + } +} \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/TestsForStack/TestsForStack.cs b/NewPostfixCalculatorWithModuleTests/TestsForStack/TestsForStack.cs new file mode 100644 index 0000000..46d80b4 --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/TestsForStack/TestsForStack.cs @@ -0,0 +1,75 @@ +namespace TestsForStack; + +using StackCalculator; + +public class Tests +{ + [TestCaseSource(nameof(Stacks))] + public void StackShouldNotEmptyAfterPush(Stack stack) + { + stack.AddElement(1); + Assert.IsFalse(stack.IsEmpty()); + } + + [TestCaseSource(nameof(Stacks))] + public void StackShouldEmptyWhenCreated(Stack stack) + { + Assert.IsTrue(stack.IsEmpty()); + } + + [TestCaseSource(nameof(Stacks))] + public void StackShouldCorrectlyDeleteTheValue(Stack stack) + { + stack.AddElement(1); + var (isCorrect, _) = stack.RemoveElement(); + Assert.IsTrue(isCorrect); + } + + [TestCaseSource(nameof(Stacks))] + public void StackShouldReturnTheLastValueThatWasAddedAndThenDeleted(Stack stack) + { + stack.AddElement(1); + var (_, number) = stack.RemoveElement(); + Assert.IsTrue(number == 1); + } + + [TestCaseSource(nameof(Stacks))] + public void WhenRemovedFromTheTopOfTheStackTheElementShouldBeErasedFromTheTop(Stack stack) + { + stack.AddElement(1); + var (_, _) = stack.RemoveElement(); + Assert.IsTrue(stack.IsEmpty()); + } + + [TestCaseSource(nameof(Stacks))] + public void DeletingFromAnEmptyStackShouldCauseAnError(Stack stack) + { + var (isCorrect, _) = stack.RemoveElement(); + Assert.IsFalse(isCorrect); + } + + [TestCaseSource(nameof(Stacks))] + public void WhenAddingConsecutiveValuesTheTopValueShouldBeTheLastOneAdded(Stack stack) + { + stack.AddElement(1); + stack.AddElement(2); + var (_, number) = stack.RemoveElement(); + Assert.IsTrue(number == 2); + } + + [TestCaseSource(nameof(Stacks))] + public void DeletingInAStackOfMultipleItemsShouldBeSuccessful(Stack stack) + { + stack.AddElement(1); + stack.AddElement(2); + var (isCorrect, _) = stack.RemoveElement(); + Assert.IsTrue(isCorrect); + } + + private static IEnumerable Stacks + => new TestCaseData[] + { + new TestCaseData(new StackWithArray()), + new TestCaseData(new StackList()), + }; +} \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/TestsForStackCalculator/TestsForStackCalculator.cs b/NewPostfixCalculatorWithModuleTests/TestsForStackCalculator/TestsForStackCalculator.cs new file mode 100644 index 0000000..bdd8f21 --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/TestsForStackCalculator/TestsForStackCalculator.cs @@ -0,0 +1,110 @@ +namespace TestsForStackCalculator; + +using StackCalculator; +using System.Numerics; + +public class Tests +{ + private const double delta = 0.0000000000001; + PostfixCalculator calculator; + [SetUp] + public void Setup() + { + calculator= new PostfixCalculator(); + } + + [TestCaseSource(nameof(Stacks))] + public void TheCalculatorShouldWorkCorrectlyToReturnTheCorrectValueOnASimpleExample(Stack stack) + { + Setup(); + var (isCorrect, number) = calculator.ConvertToAResponse("1 2 +", stack); + Assert.IsTrue(isCorrect && number == 3); + } + + [TestCaseSource(nameof(Stacks))] + public void OnTheWrongLineTheCalculatorShouldReturnAnError(Stack stack) + { + Setup(); + var (isCorrect, _) = calculator.ConvertToAResponse("1 2", stack); + Assert.IsFalse(isCorrect); + } + + [TestCaseSource(nameof(Stacks))] + public void TheFirstValueIsPositiveTheSecondIsNegativeTheCalculatorShouldReturnTheCorrectValue(Stack stack) + { + Setup(); + var (isCorrect, number) = calculator.ConvertToAResponse("1 -2 +", stack); + Assert.IsTrue(isCorrect && number == -1); + } + + [TestCaseSource(nameof(Stacks))] + public void TheSecondValueIsPositiveTheFirstIsNegativeTheCalculatorShouldReturnTheCorrectValue(Stack stack) + { + Setup(); + var (isCorrect, number) = calculator.ConvertToAResponse("-1 2 +", stack); + Assert.IsTrue(isCorrect && number == 1); + } + + [TestCaseSource(nameof(Stacks))] + public void TheSecondValueIsNegativeTheFirstIsNegativeTheCalculatorShouldReturnTheCorrectValue(Stack stack) + { + Setup(); + var (isCorrect, number) = calculator.ConvertToAResponse("-1 -2 +", stack); + Assert.IsTrue(isCorrect && number == -3); + } + + [TestCaseSource(nameof(Stacks))] + public void TheStackCalculatorShouldWorkCorrectlyWithTheDifference(Stack stack) + { + Setup(); + var (isCorrect, number) = calculator.ConvertToAResponse("1 2 -", stack); + Assert.IsTrue(isCorrect && number == -1); + } + + [TestCaseSource(nameof(Stacks))] + public void TheStackCalculatorShouldWorkCorrectlyWithTheMultiplication(Stack stack) + { + Setup(); + var (isCorrect, number) = calculator.ConvertToAResponse("2 3 *", stack); + Assert.IsTrue(isCorrect && number == 6); + } + + [TestCaseSource(nameof(Stacks))] + public void TheStackCalculatorShouldWorkCorrectlyWithTheDivision(Stack stack) + { + Setup(); + var (isCorrect, number) = calculator.ConvertToAResponse("5 2 /", stack); + Assert.IsTrue(isCorrect && Math.Abs(number - 2.5) < delta); + } + + [TestCaseSource(nameof(Stacks))] + public void TheStackCalculatorShouldGiveAnErrorWhenReceivingAnemptyString (Stack stack) + { + Setup(); + var (isCorrect, _) = calculator.ConvertToAResponse("", stack); + Assert.IsFalse(isCorrect); + } + + [TestCaseSource(nameof(Stacks))] + public void TheStackalculatorShouldCorrectlyCalculateComplexExpressions(Stack stack) + { + Setup(); + var (isCorrect, number) = calculator.ConvertToAResponse("1 2 + 3 *", stack); + Assert.IsTrue(isCorrect && number == 9); + } + + [TestCaseSource(nameof(Stacks))] + public void TheStackCalculatorShouldGiveAnErrorWhenReceivingAStringWithCharactersThatWereNotExpected(Stack stack) + { + Setup(); + var (isCorrect, _) = calculator.ConvertToAResponse("1 2 ` 3 *", stack); + Assert.IsFalse(isCorrect); + } + + private static IEnumerable Stacks + => new TestCaseData[] + { + new TestCaseData(new StackWithArray()), + new TestCaseData(new StackList()), + }; +} \ No newline at end of file From 41cfaf07912f5907ed656f4cce533ff0c79fe63c Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 31 Mar 2023 09:03:17 +0300 Subject: [PATCH 2/5] +csproj, +usings --- .../NewPostfixCalculatorWithModuleTests.sln | 20 ++++++++-------- .../TestsStack.cs} | 0 .../TestsStack/TestsStack.csproj | 23 +++++++++++++++++++ .../TestsStack/Usings.cs | 1 + .../TestsStackCalculator.cs} | 4 ++-- .../TestsStackCalculator.csproj | 23 +++++++++++++++++++ .../TestsStackCalculator/Usings.cs | 1 + 7 files changed, 60 insertions(+), 12 deletions(-) rename NewPostfixCalculatorWithModuleTests/{TestsForStack/TestsForStack.cs => TestsStack/TestsStack.cs} (100%) create mode 100644 NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.csproj create mode 100644 NewPostfixCalculatorWithModuleTests/TestsStack/Usings.cs rename NewPostfixCalculatorWithModuleTests/{TestsForStackCalculator/TestsForStackCalculator.cs => TestsStackCalculator/TestsStackCalculator.cs} (98%) create mode 100644 NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.csproj create mode 100644 NewPostfixCalculatorWithModuleTests/TestsStackCalculator/Usings.cs diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests.sln b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests.sln index af23481..2d10881 100644 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests.sln +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests.sln @@ -5,9 +5,9 @@ VisualStudioVersion = 17.4.33403.182 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewPostfixCalculatorWithModuleTests", "NewPostfixCalculatorWithModuleTests\NewPostfixCalculatorWithModuleTests.csproj", "{6D1F274D-7F63-4D3D-A1C1-39AD50776432}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForStack", "TestsForStack\TestsForStack.csproj", "{FC7B71F8-A8D8-480C-985C-1104B79919E3}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsStack", "TestsStack\TestsStack.csproj", "{1D426654-9F9F-48C0-B933-D02296C8D15C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForStackCalculator", "TestsForStackCalculator\TestsForStackCalculator.csproj", "{CC67F651-06BB-4014-9E19-64E4B715B95A}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsStackCalculator", "TestsStackCalculator\TestsStackCalculator.csproj", "{6C39F55D-D4A9-438C-8CAA-7273737D1223}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -19,14 +19,14 @@ Global {6D1F274D-7F63-4D3D-A1C1-39AD50776432}.Debug|Any CPU.Build.0 = Debug|Any CPU {6D1F274D-7F63-4D3D-A1C1-39AD50776432}.Release|Any CPU.ActiveCfg = Release|Any CPU {6D1F274D-7F63-4D3D-A1C1-39AD50776432}.Release|Any CPU.Build.0 = Release|Any CPU - {FC7B71F8-A8D8-480C-985C-1104B79919E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FC7B71F8-A8D8-480C-985C-1104B79919E3}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FC7B71F8-A8D8-480C-985C-1104B79919E3}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FC7B71F8-A8D8-480C-985C-1104B79919E3}.Release|Any CPU.Build.0 = Release|Any CPU - {CC67F651-06BB-4014-9E19-64E4B715B95A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CC67F651-06BB-4014-9E19-64E4B715B95A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CC67F651-06BB-4014-9E19-64E4B715B95A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CC67F651-06BB-4014-9E19-64E4B715B95A}.Release|Any CPU.Build.0 = Release|Any CPU + {1D426654-9F9F-48C0-B933-D02296C8D15C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1D426654-9F9F-48C0-B933-D02296C8D15C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1D426654-9F9F-48C0-B933-D02296C8D15C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1D426654-9F9F-48C0-B933-D02296C8D15C}.Release|Any CPU.Build.0 = Release|Any CPU + {6C39F55D-D4A9-438C-8CAA-7273737D1223}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6C39F55D-D4A9-438C-8CAA-7273737D1223}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6C39F55D-D4A9-438C-8CAA-7273737D1223}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6C39F55D-D4A9-438C-8CAA-7273737D1223}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/NewPostfixCalculatorWithModuleTests/TestsForStack/TestsForStack.cs b/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs similarity index 100% rename from NewPostfixCalculatorWithModuleTests/TestsForStack/TestsForStack.cs rename to NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs diff --git a/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.csproj b/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.csproj new file mode 100644 index 0000000..564a5d7 --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + diff --git a/NewPostfixCalculatorWithModuleTests/TestsStack/Usings.cs b/NewPostfixCalculatorWithModuleTests/TestsStack/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/TestsStack/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/TestsForStackCalculator/TestsForStackCalculator.cs b/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs similarity index 98% rename from NewPostfixCalculatorWithModuleTests/TestsForStackCalculator/TestsForStackCalculator.cs rename to NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs index bdd8f21..0dd2ad0 100644 --- a/NewPostfixCalculatorWithModuleTests/TestsForStackCalculator/TestsForStackCalculator.cs +++ b/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs @@ -10,7 +10,7 @@ public class Tests [SetUp] public void Setup() { - calculator= new PostfixCalculator(); + calculator = new PostfixCalculator(); } [TestCaseSource(nameof(Stacks))] @@ -78,7 +78,7 @@ public void TheStackCalculatorShouldWorkCorrectlyWithTheDivision(Stack stack) } [TestCaseSource(nameof(Stacks))] - public void TheStackCalculatorShouldGiveAnErrorWhenReceivingAnemptyString (Stack stack) + public void TheStackCalculatorShouldGiveAnErrorWhenReceivingAnemptyString(Stack stack) { Setup(); var (isCorrect, _) = calculator.ConvertToAResponse("", stack); diff --git a/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.csproj b/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.csproj new file mode 100644 index 0000000..564a5d7 --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + diff --git a/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/Usings.cs b/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file From e61c31a6717c50fef402533cdc2d3c4b86058397 Mon Sep 17 00:00:00 2001 From: Artem Date: Tue, 23 May 2023 16:43:28 +0300 Subject: [PATCH 3/5] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../InterfaceForStack.cs | 6 +- .../Program.cs | 3 +- .../Stack.cs | 8 +-- .../StackCalculator.cs | 14 ++--- .../StackWIthArray.cs | 6 +- .../StackWithList.cs | 7 +-- .../TestsStack/TestsStack.cs | 28 ++++----- .../TestsStackCalculator.cs | 61 ++++++++----------- 8 files changed, 61 insertions(+), 72 deletions(-) diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/InterfaceForStack.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/InterfaceForStack.cs index 3941104..2e5c5e3 100644 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/InterfaceForStack.cs +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/InterfaceForStack.cs @@ -1,13 +1,13 @@ namespace StackCalculator; // Interface for the stack -interface IOperationsWithStack +interface IStack { // Add element to stack - void AddElement(double value); + void Push(double value); // Remove element in stack and return deleted item - (bool, double) RemoveElement(); + (bool, double) Pop(); // Print all elements void PrintTheElements(); diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Program.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Program.cs index 62eb0f1..3e7202a 100644 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Program.cs +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Program.cs @@ -3,14 +3,13 @@ Console.WriteLine("Enter an example in the postfix form"); var stringWithExpression = Console.ReadLine(); -PostfixCalculator calculator = new PostfixCalculator(); if (stringWithExpression == null) { return; } var stackList = new StackList(); -(bool isCorrectWork, double result) = calculator.ConvertToAResponse(stringWithExpression, stackList); +(bool isCorrectWork, double result) = PostfixCalculator.Calculate(stringWithExpression, stackList); if (!isCorrectWork) { Console.WriteLine("Problems with expression or you tried to divide by zero!"); diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Stack.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Stack.cs index ecb7f2f..536553a 100644 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Stack.cs +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Stack.cs @@ -1,13 +1,13 @@ -namespace StackCalculator; +namespace StackCalculator; //Класс оставил иначе с тестами неудобно работать //Standart stack -abstract public class Stack : IOperationsWithStack +abstract public class Stack : IStack { // Add element to stack - virtual public void AddElement(double value) { } + virtual public void Push(double value) { } // Remove element in stack and return deleted item - virtual public (bool, double) RemoveElement() { return (false, 0); } + virtual public (bool, double) Pop() { return (false, 0); } // Print all elements virtual public void PrintTheElements() { } diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackCalculator.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackCalculator.cs index 6a7008d..49dca9f 100644 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackCalculator.cs +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackCalculator.cs @@ -1,12 +1,12 @@ namespace StackCalculator; // Calculator that counts algebraic expressions in postfix form -public class PostfixCalculator +public static class PostfixCalculator { private const double delta = 0.0000000000001; // Receives the input string in which the expression is written in postfix form, finds the result - public (bool, double) ConvertToAResponse(string stringWithExpression, Stack stackExpression) + public static (bool, double) Calculate(string stringWithExpression, Stack stackExpression) { int i = 0; string[] expressionArray = stringWithExpression.Split(' '); @@ -15,7 +15,7 @@ public class PostfixCalculator var isCorrectNumber = Int32.TryParse(expressionArray[i], out var number); if (isCorrectNumber) { - stackExpression.AddElement(number); + stackExpression.Push(number); } else { @@ -24,14 +24,14 @@ public class PostfixCalculator return (false, 0); } double numberAfter = 0; - (var isCorrect, var firstNumber) = stackExpression.RemoveElement(); + (var isCorrect, var firstNumber) = stackExpression.Pop(); if (!isCorrect) { return (false, 0); } - (isCorrect, var secondNumber) = stackExpression.RemoveElement(); + (isCorrect, var secondNumber) = stackExpression.Pop(); if (!isCorrect) { @@ -59,11 +59,11 @@ public class PostfixCalculator default: return (false, 0); } - stackExpression.AddElement(numberAfter); + stackExpression.Push(numberAfter); } ++i; } - (var isCorrectExpression, var result) = stackExpression.RemoveElement(); + (var isCorrectExpression, var result) = stackExpression.Pop(); if (!isCorrectExpression) { return (false, 0); diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWIthArray.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWIthArray.cs index d26f993..c67a4d9 100644 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWIthArray.cs +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWIthArray.cs @@ -12,7 +12,7 @@ public StackWithArray() stackArray = new double[sizeStack]; } - public bool ChangeStackSize(int size) + private bool ChangeStackSize(int size) { if (size < sizeStack) { @@ -22,7 +22,7 @@ public bool ChangeStackSize(int size) return true; } - public override void AddElement(double value) + public override void Push(double value) { if (numberOfElements == sizeStack) { @@ -32,7 +32,7 @@ public override void AddElement(double value) ++numberOfElements; } - public override (bool, double) RemoveElement() + public override (bool, double) Pop() { if (numberOfElements == 0) { diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWithList.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWithList.cs index e221c0c..eda3d5a 100644 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWithList.cs +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWithList.cs @@ -3,9 +3,9 @@ // Stack implemented on list public class StackList : Stack { - private StackElement headStack; + private StackElement? headStack; - public override void AddElement(double value) + public override void Push(double value) { var item = new StackElement(value); if (headStack == null) @@ -20,7 +20,7 @@ public override void AddElement(double value) } } - public override (bool, double) RemoveElement() + public override (bool, double) Pop() { if (headStack == null) { @@ -49,7 +49,6 @@ private class StackElement public StackElement(double value) { ValueStack = value; - Next = null; } public double ValueStack { get; set; } diff --git a/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs b/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs index 46d80b4..d9118dc 100644 --- a/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs +++ b/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs @@ -7,7 +7,7 @@ public class Tests [TestCaseSource(nameof(Stacks))] public void StackShouldNotEmptyAfterPush(Stack stack) { - stack.AddElement(1); + stack.Push(1); Assert.IsFalse(stack.IsEmpty()); } @@ -20,49 +20,49 @@ public void StackShouldEmptyWhenCreated(Stack stack) [TestCaseSource(nameof(Stacks))] public void StackShouldCorrectlyDeleteTheValue(Stack stack) { - stack.AddElement(1); - var (isCorrect, _) = stack.RemoveElement(); + stack.Push(1); + var (isCorrect, _) = stack.Pop(); Assert.IsTrue(isCorrect); } [TestCaseSource(nameof(Stacks))] public void StackShouldReturnTheLastValueThatWasAddedAndThenDeleted(Stack stack) { - stack.AddElement(1); - var (_, number) = stack.RemoveElement(); + stack.Push(1); + var (_, number) = stack.Pop(); Assert.IsTrue(number == 1); } [TestCaseSource(nameof(Stacks))] public void WhenRemovedFromTheTopOfTheStackTheElementShouldBeErasedFromTheTop(Stack stack) { - stack.AddElement(1); - var (_, _) = stack.RemoveElement(); + stack.Push(1); + var (_, _) = stack.Pop(); Assert.IsTrue(stack.IsEmpty()); } [TestCaseSource(nameof(Stacks))] public void DeletingFromAnEmptyStackShouldCauseAnError(Stack stack) { - var (isCorrect, _) = stack.RemoveElement(); + var (isCorrect, _) = stack.Pop(); Assert.IsFalse(isCorrect); } [TestCaseSource(nameof(Stacks))] public void WhenAddingConsecutiveValuesTheTopValueShouldBeTheLastOneAdded(Stack stack) { - stack.AddElement(1); - stack.AddElement(2); - var (_, number) = stack.RemoveElement(); + stack.Push(1); + stack.Push(2); + var (_, number) = stack.Pop(); Assert.IsTrue(number == 2); } [TestCaseSource(nameof(Stacks))] public void DeletingInAStackOfMultipleItemsShouldBeSuccessful(Stack stack) { - stack.AddElement(1); - stack.AddElement(2); - var (isCorrect, _) = stack.RemoveElement(); + stack.Push(1); + stack.Push(2); + var (isCorrect, _) = stack.Pop(); Assert.IsTrue(isCorrect); } diff --git a/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs b/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs index 0dd2ad0..e52ccdc 100644 --- a/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs +++ b/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs @@ -6,98 +6,89 @@ namespace TestsForStackCalculator; public class Tests { private const double delta = 0.0000000000001; - PostfixCalculator calculator; - [SetUp] - public void Setup() - { - calculator = new PostfixCalculator(); - } + [TestCaseSource(nameof(Stacks))] public void TheCalculatorShouldWorkCorrectlyToReturnTheCorrectValueOnASimpleExample(Stack stack) { - Setup(); - var (isCorrect, number) = calculator.ConvertToAResponse("1 2 +", stack); + var (isCorrect, number) = PostfixCalculator.Calculate("1 2 +", stack); Assert.IsTrue(isCorrect && number == 3); } [TestCaseSource(nameof(Stacks))] public void OnTheWrongLineTheCalculatorShouldReturnAnError(Stack stack) { - Setup(); - var (isCorrect, _) = calculator.ConvertToAResponse("1 2", stack); + var (isCorrect, _) = PostfixCalculator.Calculate("1 2", stack); Assert.IsFalse(isCorrect); } [TestCaseSource(nameof(Stacks))] public void TheFirstValueIsPositiveTheSecondIsNegativeTheCalculatorShouldReturnTheCorrectValue(Stack stack) { - Setup(); - var (isCorrect, number) = calculator.ConvertToAResponse("1 -2 +", stack); - Assert.IsTrue(isCorrect && number == -1); + var (isCorrect, number) = PostfixCalculator.Calculate("1 -2 +", stack); + Assert.That(isCorrect); + Assert.That(number == -1); } [TestCaseSource(nameof(Stacks))] public void TheSecondValueIsPositiveTheFirstIsNegativeTheCalculatorShouldReturnTheCorrectValue(Stack stack) { - Setup(); - var (isCorrect, number) = calculator.ConvertToAResponse("-1 2 +", stack); - Assert.IsTrue(isCorrect && number == 1); + var (isCorrect, number) = PostfixCalculator.Calculate("-1 2 +", stack); + Assert.That(isCorrect); + Assert.That(number == 1); } [TestCaseSource(nameof(Stacks))] public void TheSecondValueIsNegativeTheFirstIsNegativeTheCalculatorShouldReturnTheCorrectValue(Stack stack) { - Setup(); - var (isCorrect, number) = calculator.ConvertToAResponse("-1 -2 +", stack); - Assert.IsTrue(isCorrect && number == -3); + var (isCorrect, number) = PostfixCalculator.Calculate("-1 -2 +", stack); + Assert.That(isCorrect); + Assert.That(number == -3); } [TestCaseSource(nameof(Stacks))] public void TheStackCalculatorShouldWorkCorrectlyWithTheDifference(Stack stack) { - Setup(); - var (isCorrect, number) = calculator.ConvertToAResponse("1 2 -", stack); - Assert.IsTrue(isCorrect && number == -1); + var (isCorrect, number) = PostfixCalculator.Calculate("1 2 -", stack); + Assert.That(isCorrect); + Assert.That(number == -1); } [TestCaseSource(nameof(Stacks))] public void TheStackCalculatorShouldWorkCorrectlyWithTheMultiplication(Stack stack) { - Setup(); - var (isCorrect, number) = calculator.ConvertToAResponse("2 3 *", stack); - Assert.IsTrue(isCorrect && number == 6); + var (isCorrect, number) = PostfixCalculator.Calculate("2 3 *", stack); + Assert.That(isCorrect); + Assert.That(number == 6); } [TestCaseSource(nameof(Stacks))] public void TheStackCalculatorShouldWorkCorrectlyWithTheDivision(Stack stack) { - Setup(); - var (isCorrect, number) = calculator.ConvertToAResponse("5 2 /", stack); - Assert.IsTrue(isCorrect && Math.Abs(number - 2.5) < delta); + var (isCorrect, number) = PostfixCalculator.Calculate("5 2 /", stack); + Assert.That(isCorrect); + Assert.That(Math.Abs(number - 2.5) < delta); } [TestCaseSource(nameof(Stacks))] public void TheStackCalculatorShouldGiveAnErrorWhenReceivingAnemptyString(Stack stack) { - Setup(); - var (isCorrect, _) = calculator.ConvertToAResponse("", stack); + var (isCorrect, _) = PostfixCalculator.Calculate("", stack); Assert.IsFalse(isCorrect); } [TestCaseSource(nameof(Stacks))] public void TheStackalculatorShouldCorrectlyCalculateComplexExpressions(Stack stack) { - Setup(); - var (isCorrect, number) = calculator.ConvertToAResponse("1 2 + 3 *", stack); - Assert.IsTrue(isCorrect && number == 9); + var (isCorrect, number) = PostfixCalculator.Calculate("1 2 + 3 *", stack); + Assert.That(isCorrect); + Assert.That(number == 9); } [TestCaseSource(nameof(Stacks))] public void TheStackCalculatorShouldGiveAnErrorWhenReceivingAStringWithCharactersThatWereNotExpected(Stack stack) { - Setup(); - var (isCorrect, _) = calculator.ConvertToAResponse("1 2 ` 3 *", stack); + var (isCorrect, _) = PostfixCalculator.Calculate("1 2 ` 3 *", stack); Assert.IsFalse(isCorrect); } From 44dac736c34850ce07308034946672cf50c1fa41 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 26 May 2023 11:51:48 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TestsStack/TestsStack.cs | 4 ++-- .../TestsStackCalculator/TestsStackCalculator.cs | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs b/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs index d9118dc..50a55db 100644 --- a/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs +++ b/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs @@ -30,7 +30,7 @@ public void StackShouldReturnTheLastValueThatWasAddedAndThenDeleted(Stack stack) { stack.Push(1); var (_, number) = stack.Pop(); - Assert.IsTrue(number == 1); + Assert.That(1, Is.EqualTo(number)); } [TestCaseSource(nameof(Stacks))] @@ -54,7 +54,7 @@ public void WhenAddingConsecutiveValuesTheTopValueShouldBeTheLastOneAdded(Stack stack.Push(1); stack.Push(2); var (_, number) = stack.Pop(); - Assert.IsTrue(number == 2); + Assert.That(2, Is.EqualTo(number)); } [TestCaseSource(nameof(Stacks))] diff --git a/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs b/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs index e52ccdc..9cce9cd 100644 --- a/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs +++ b/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs @@ -12,7 +12,8 @@ public class Tests public void TheCalculatorShouldWorkCorrectlyToReturnTheCorrectValueOnASimpleExample(Stack stack) { var (isCorrect, number) = PostfixCalculator.Calculate("1 2 +", stack); - Assert.IsTrue(isCorrect && number == 3); + Assert.That(true, Is.EqualTo(isCorrect)); + Assert.That(3, Is.EqualTo(number)); } [TestCaseSource(nameof(Stacks))] @@ -27,7 +28,7 @@ public void TheFirstValueIsPositiveTheSecondIsNegativeTheCalculatorShouldReturnT { var (isCorrect, number) = PostfixCalculator.Calculate("1 -2 +", stack); Assert.That(isCorrect); - Assert.That(number == -1); + Assert.That(-1, Is.EqualTo(number)); } [TestCaseSource(nameof(Stacks))] @@ -35,7 +36,7 @@ public void TheSecondValueIsPositiveTheFirstIsNegativeTheCalculatorShouldReturnT { var (isCorrect, number) = PostfixCalculator.Calculate("-1 2 +", stack); Assert.That(isCorrect); - Assert.That(number == 1); + Assert.That(1, Is.EqualTo(number)); } [TestCaseSource(nameof(Stacks))] @@ -43,7 +44,7 @@ public void TheSecondValueIsNegativeTheFirstIsNegativeTheCalculatorShouldReturnT { var (isCorrect, number) = PostfixCalculator.Calculate("-1 -2 +", stack); Assert.That(isCorrect); - Assert.That(number == -3); + Assert.That(-3, Is.EqualTo(number)); } [TestCaseSource(nameof(Stacks))] @@ -51,7 +52,7 @@ public void TheStackCalculatorShouldWorkCorrectlyWithTheDifference(Stack stack) { var (isCorrect, number) = PostfixCalculator.Calculate("1 2 -", stack); Assert.That(isCorrect); - Assert.That(number == -1); + Assert.That(-1, Is.EqualTo(number)); } [TestCaseSource(nameof(Stacks))] @@ -59,7 +60,7 @@ public void TheStackCalculatorShouldWorkCorrectlyWithTheMultiplication(Stack sta { var (isCorrect, number) = PostfixCalculator.Calculate("2 3 *", stack); Assert.That(isCorrect); - Assert.That(number == 6); + Assert.That(6, Is.EqualTo(number)); } [TestCaseSource(nameof(Stacks))] @@ -82,7 +83,7 @@ public void TheStackCalculatorShouldGiveAnErrorWhenReceivingAnemptyString(Stack { var (isCorrect, number) = PostfixCalculator.Calculate("1 2 + 3 *", stack); Assert.That(isCorrect); - Assert.That(number == 9); + Assert.That(9, Is.EqualTo(number)); } [TestCaseSource(nameof(Stacks))] From cbafc28d672354717a0492d91a4bcbcff1e35217 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 26 May 2023 13:11:27 +0300 Subject: [PATCH 5/5] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../InterfaceForStack.cs | 17 -------- .../Program.cs | 2 +- .../Stack.cs | 17 -------- .../StackCalculator.cs | 2 +- .../StackWIthArray.cs | 10 ++--- .../StackWithList.cs | 31 +++++--------- .../TestsStack/TestsStack.cs | 22 +++++----- .../TestsStackCalculator.cs | 40 +++++++++---------- 8 files changed, 49 insertions(+), 92 deletions(-) delete mode 100644 NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/InterfaceForStack.cs delete mode 100644 NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Stack.cs diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/InterfaceForStack.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/InterfaceForStack.cs deleted file mode 100644 index 2e5c5e3..0000000 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/InterfaceForStack.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace StackCalculator; - -// Interface for the stack -interface IStack -{ - // Add element to stack - void Push(double value); - - // Remove element in stack and return deleted item - (bool, double) Pop(); - - // Print all elements - void PrintTheElements(); - - // Checking that the stack is empty - bool IsEmpty(); -} \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Program.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Program.cs index 3e7202a..1ad0a2d 100644 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Program.cs +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Program.cs @@ -8,7 +8,7 @@ return; } -var stackList = new StackList(); +var stackList = new StackWithList(); (bool isCorrectWork, double result) = PostfixCalculator.Calculate(stringWithExpression, stackList); if (!isCorrectWork) { diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Stack.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Stack.cs deleted file mode 100644 index 536553a..0000000 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/Stack.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace StackCalculator; //Класс оставил иначе с тестами неудобно работать - -//Standart stack -abstract public class Stack : IStack -{ - // Add element to stack - virtual public void Push(double value) { } - - // Remove element in stack and return deleted item - virtual public (bool, double) Pop() { return (false, 0); } - - // Print all elements - virtual public void PrintTheElements() { } - - // Checking that the stack is empty - virtual public bool IsEmpty() { return false; } -} \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackCalculator.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackCalculator.cs index 49dca9f..eb4515e 100644 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackCalculator.cs +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackCalculator.cs @@ -6,7 +6,7 @@ public static class PostfixCalculator private const double delta = 0.0000000000001; // Receives the input string in which the expression is written in postfix form, finds the result - public static (bool, double) Calculate(string stringWithExpression, Stack stackExpression) + public static (bool, double) Calculate(string stringWithExpression, IStack stackExpression) { int i = 0; string[] expressionArray = stringWithExpression.Split(' '); diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWIthArray.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWIthArray.cs index c67a4d9..5efee76 100644 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWIthArray.cs +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWIthArray.cs @@ -1,7 +1,7 @@ namespace StackCalculator; // Stack implemented on an array -public class StackWithArray : Stack +public class StackWithArray : IStack { private double[] stackArray; private int numberOfElements; @@ -22,7 +22,7 @@ private bool ChangeStackSize(int size) return true; } - public override void Push(double value) + public void Push(double value) { if (numberOfElements == sizeStack) { @@ -32,7 +32,7 @@ public override void Push(double value) ++numberOfElements; } - public override (bool, double) Pop() + public (bool, double) Pop() { if (numberOfElements == 0) { @@ -43,7 +43,7 @@ public override (bool, double) Pop() return (true, result); } - public override void PrintTheElements() + public void PrintTheElements() { for (int i = 0; i < numberOfElements; i++) { @@ -51,6 +51,6 @@ public override void PrintTheElements() } } - public override bool IsEmpty() + public bool IsEmpty() => numberOfElements == 0; } \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWithList.cs b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWithList.cs index eda3d5a..54b71c0 100644 --- a/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWithList.cs +++ b/NewPostfixCalculatorWithModuleTests/NewPostfixCalculatorWithModuleTests/StackWithList.cs @@ -1,40 +1,30 @@ namespace StackCalculator; // Stack implemented on list -public class StackList : Stack +public class StackWithList : IStack { private StackElement? headStack; - public override void Push(double value) + public void Push(double value) { - var item = new StackElement(value); - if (headStack == null) - { - headStack = item; - } - else - { - StackElement copy = headStack; - headStack = item; - headStack.Next = copy; - } + headStack = new StackElement(value, headStack); } - public override (bool, double) Pop() + public (bool, double) Pop() { if (headStack == null) { return (false, 0); } double item = headStack.ValueStack; - StackElement copy = headStack.Next; + StackElement? copy = headStack.Next; headStack = copy; return (true, item); } - public override void PrintTheElements() + public void PrintTheElements() { - StackElement walker = headStack; + StackElement? walker = headStack; while (walker != null) { Console.WriteLine(walker.ValueStack); @@ -42,16 +32,17 @@ public override void PrintTheElements() } } - public override bool IsEmpty() => headStack == null; + public bool IsEmpty() => headStack == null; private class StackElement { - public StackElement(double value) + public StackElement(double value, StackElement? next) { ValueStack = value; + Next = next; } public double ValueStack { get; set; } - public StackElement Next { get; set; } + public StackElement? Next { get; set; } } } \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs b/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs index 50a55db..2d050cf 100644 --- a/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs +++ b/NewPostfixCalculatorWithModuleTests/TestsStack/TestsStack.cs @@ -5,20 +5,20 @@ namespace TestsForStack; public class Tests { [TestCaseSource(nameof(Stacks))] - public void StackShouldNotEmptyAfterPush(Stack stack) + public void StackShouldNotEmptyAfterPush(IStack stack) { stack.Push(1); Assert.IsFalse(stack.IsEmpty()); } [TestCaseSource(nameof(Stacks))] - public void StackShouldEmptyWhenCreated(Stack stack) + public void StackShouldEmptyWhenCreated(IStack stack) { Assert.IsTrue(stack.IsEmpty()); } [TestCaseSource(nameof(Stacks))] - public void StackShouldCorrectlyDeleteTheValue(Stack stack) + public void StackShouldCorrectlyDeleteTheValue(IStack stack) { stack.Push(1); var (isCorrect, _) = stack.Pop(); @@ -26,15 +26,15 @@ public void StackShouldCorrectlyDeleteTheValue(Stack stack) } [TestCaseSource(nameof(Stacks))] - public void StackShouldReturnTheLastValueThatWasAddedAndThenDeleted(Stack stack) + public void StackShouldReturnTheLastValueThatWasAddedAndThenDeleted(IStack stack) { stack.Push(1); var (_, number) = stack.Pop(); - Assert.That(1, Is.EqualTo(number)); + Assert.That(number, Is.EqualTo(1)); } [TestCaseSource(nameof(Stacks))] - public void WhenRemovedFromTheTopOfTheStackTheElementShouldBeErasedFromTheTop(Stack stack) + public void WhenRemovedFromTheTopOfTheStackTheElementShouldBeErasedFromTheTop(IStack stack) { stack.Push(1); var (_, _) = stack.Pop(); @@ -42,23 +42,23 @@ public void WhenRemovedFromTheTopOfTheStackTheElementShouldBeErasedFromTheTop(St } [TestCaseSource(nameof(Stacks))] - public void DeletingFromAnEmptyStackShouldCauseAnError(Stack stack) + public void DeletingFromAnEmptyStackShouldCauseAnError(IStack stack) { var (isCorrect, _) = stack.Pop(); Assert.IsFalse(isCorrect); } [TestCaseSource(nameof(Stacks))] - public void WhenAddingConsecutiveValuesTheTopValueShouldBeTheLastOneAdded(Stack stack) + public void WhenAddingConsecutiveValuesTheTopValueShouldBeTheLastOneAdded(IStack stack) { stack.Push(1); stack.Push(2); var (_, number) = stack.Pop(); - Assert.That(2, Is.EqualTo(number)); + Assert.That(number, Is.EqualTo(2)); } [TestCaseSource(nameof(Stacks))] - public void DeletingInAStackOfMultipleItemsShouldBeSuccessful(Stack stack) + public void DeletingInAStackOfMultipleItemsShouldBeSuccessful(IStack stack) { stack.Push(1); stack.Push(2); @@ -70,6 +70,6 @@ private static IEnumerable Stacks => new TestCaseData[] { new TestCaseData(new StackWithArray()), - new TestCaseData(new StackList()), + new TestCaseData(new StackWithList()), }; } \ No newline at end of file diff --git a/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs b/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs index 9cce9cd..d5429ce 100644 --- a/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs +++ b/NewPostfixCalculatorWithModuleTests/TestsStackCalculator/TestsStackCalculator.cs @@ -9,62 +9,62 @@ public class Tests [TestCaseSource(nameof(Stacks))] - public void TheCalculatorShouldWorkCorrectlyToReturnTheCorrectValueOnASimpleExample(Stack stack) + public void TheCalculatorShouldWorkCorrectlyToReturnTheCorrectValueOnASimpleExample(IStack stack) { var (isCorrect, number) = PostfixCalculator.Calculate("1 2 +", stack); - Assert.That(true, Is.EqualTo(isCorrect)); - Assert.That(3, Is.EqualTo(number)); + Assert.True(isCorrect); + Assert.That(number, Is.EqualTo(3)); } [TestCaseSource(nameof(Stacks))] - public void OnTheWrongLineTheCalculatorShouldReturnAnError(Stack stack) + public void OnTheWrongLineTheCalculatorShouldReturnAnError(IStack stack) { var (isCorrect, _) = PostfixCalculator.Calculate("1 2", stack); Assert.IsFalse(isCorrect); } [TestCaseSource(nameof(Stacks))] - public void TheFirstValueIsPositiveTheSecondIsNegativeTheCalculatorShouldReturnTheCorrectValue(Stack stack) + public void TheFirstValueIsPositiveTheSecondIsNegativeTheCalculatorShouldReturnTheCorrectValue(IStack stack) { var (isCorrect, number) = PostfixCalculator.Calculate("1 -2 +", stack); Assert.That(isCorrect); - Assert.That(-1, Is.EqualTo(number)); + Assert.That(number, Is.EqualTo(-1)); } [TestCaseSource(nameof(Stacks))] - public void TheSecondValueIsPositiveTheFirstIsNegativeTheCalculatorShouldReturnTheCorrectValue(Stack stack) + public void TheSecondValueIsPositiveTheFirstIsNegativeTheCalculatorShouldReturnTheCorrectValue(IStack stack) { var (isCorrect, number) = PostfixCalculator.Calculate("-1 2 +", stack); Assert.That(isCorrect); - Assert.That(1, Is.EqualTo(number)); + Assert.That(number, Is.EqualTo(1)); } [TestCaseSource(nameof(Stacks))] - public void TheSecondValueIsNegativeTheFirstIsNegativeTheCalculatorShouldReturnTheCorrectValue(Stack stack) + public void TheSecondValueIsNegativeTheFirstIsNegativeTheCalculatorShouldReturnTheCorrectValue(IStack stack) { var (isCorrect, number) = PostfixCalculator.Calculate("-1 -2 +", stack); Assert.That(isCorrect); - Assert.That(-3, Is.EqualTo(number)); + Assert.That(number, Is.EqualTo(-3)); } [TestCaseSource(nameof(Stacks))] - public void TheStackCalculatorShouldWorkCorrectlyWithTheDifference(Stack stack) + public void TheStackCalculatorShouldWorkCorrectlyWithTheDifference(IStack stack) { var (isCorrect, number) = PostfixCalculator.Calculate("1 2 -", stack); Assert.That(isCorrect); - Assert.That(-1, Is.EqualTo(number)); + Assert.That(number, Is.EqualTo(-1)); } [TestCaseSource(nameof(Stacks))] - public void TheStackCalculatorShouldWorkCorrectlyWithTheMultiplication(Stack stack) + public void TheStackCalculatorShouldWorkCorrectlyWithTheMultiplication(IStack stack) { var (isCorrect, number) = PostfixCalculator.Calculate("2 3 *", stack); Assert.That(isCorrect); - Assert.That(6, Is.EqualTo(number)); + Assert.That(number, Is.EqualTo(6)); } [TestCaseSource(nameof(Stacks))] - public void TheStackCalculatorShouldWorkCorrectlyWithTheDivision(Stack stack) + public void TheStackCalculatorShouldWorkCorrectlyWithTheDivision(IStack stack) { var (isCorrect, number) = PostfixCalculator.Calculate("5 2 /", stack); Assert.That(isCorrect); @@ -72,22 +72,22 @@ public void TheStackCalculatorShouldWorkCorrectlyWithTheDivision(Stack stack) } [TestCaseSource(nameof(Stacks))] - public void TheStackCalculatorShouldGiveAnErrorWhenReceivingAnemptyString(Stack stack) + public void TheStackCalculatorShouldGiveAnErrorWhenReceivingAnemptyString(IStack stack) { var (isCorrect, _) = PostfixCalculator.Calculate("", stack); Assert.IsFalse(isCorrect); } [TestCaseSource(nameof(Stacks))] - public void TheStackalculatorShouldCorrectlyCalculateComplexExpressions(Stack stack) + public void TheStackalculatorShouldCorrectlyCalculateComplexExpressions(IStack stack) { var (isCorrect, number) = PostfixCalculator.Calculate("1 2 + 3 *", stack); Assert.That(isCorrect); - Assert.That(9, Is.EqualTo(number)); + Assert.That(number, Is.EqualTo(9)); } [TestCaseSource(nameof(Stacks))] - public void TheStackCalculatorShouldGiveAnErrorWhenReceivingAStringWithCharactersThatWereNotExpected(Stack stack) + public void TheStackCalculatorShouldGiveAnErrorWhenReceivingAStringWithCharactersThatWereNotExpected(IStack stack) { var (isCorrect, _) = PostfixCalculator.Calculate("1 2 ` 3 *", stack); Assert.IsFalse(isCorrect); @@ -97,6 +97,6 @@ private static IEnumerable Stacks => new TestCaseData[] { new TestCaseData(new StackWithArray()), - new TestCaseData(new StackList()), + new TestCaseData(new StackWithList()), }; } \ No newline at end of file