Skip to content

Commit

Permalink
comments, ToString tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamendov-maxim committed Apr 24, 2024
1 parent 86b2807 commit f7c5cf5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ParseTree/ParseTree.Src/Nodes/OperationNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override string ToString()
{
if (LeftChild is not null && RightChild is not null)
{
return $"( {operation} {LeftChild.ToString()} {RightChild.ToString()})";
return $"({operation} {LeftChild.ToString()} {RightChild.ToString()})";
}
throw new IncorrectTreeException();
}
Expand Down
12 changes: 12 additions & 0 deletions ParseTree/ParseTree.Src/Operation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

namespace ParseTree.Dependencies;

/// <summary>
/// Class implementing arithmetic operations based on char input
/// </summary>
class Operation
{
/// <summary>
/// Do the arithmetic operation corresponding to char
/// </summary>
/// <param name="operation">Char '*', '+', '-' or '/'</param>
/// <param name="operandLeft">Left operand</param>
/// <param name="operandRight">Right operand</param>
/// <returns>Result of the operation</returns>
/// <exception cref="DivideByZeroException">Thrown if operandRight is 0 and operation is '/'</exception>
/// <exception cref="IncorrectExpressionException">thrown if argument operation is something different to '*', '+', '-' or '/'</exception>
public static double Calculate(char operation, double operandLeft, double operandRight)
{
switch (operation)
Expand Down
1 change: 1 addition & 0 deletions ParseTree/ParseTree.Src/Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private static INode Parse(string expression, ref int index)
if (currentChar == '(')
{
++index;
SkipSpaces(expression, ref index);
newNode = new OperationNode(expression[index]);
++index;
newNode.LeftChild = Parse(expression, ref index);
Expand Down
13 changes: 12 additions & 1 deletion ParseTree/ParseTree.Tests/ParseTree.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ParseTree.Tests;

public class Tests
{
[TestCase("(* (+ 1 1) 2)", 4)]
[TestCase("( * (+ 1 1 ) 2 )", 4)]
[TestCase("5", 5)]
[TestCase("(/ 5 2)", 2.5)]
public void TestEvaluation(string expression, double expectedResult)
Expand All @@ -24,5 +24,16 @@ public void IncorrectInputTest(string expression)
{
Assert.Throws<IncorrectExpressionException>(() => new Tree(expression));
}

[TestCase("(* (+ 1 1) 2)")]
[TestCase("5")]
[TestCase("(/ 5 2)")]
public void ToStringTests(string expression)
{
var tree = new Tree(expression);
var result = tree.ToString();

Assert.That(result, Is.EqualTo(expression));
}
}

0 comments on commit f7c5cf5

Please sign in to comment.