Feature/basic evaluator (#2)

* Refactor

* Updated TreeNode system to use abstract base class and inheritence

* Updated unit test coverage

* Improved code coverage
This commit is contained in:
0xJ1M
2023-09-08 17:53:13 +01:00
committed by GitHub
parent 9c5bf4c0d5
commit a555a131af
9 changed files with 180 additions and 392 deletions

View File

@@ -16,15 +16,8 @@ namespace EngineTests
public void TestExpressionTreeSimpleExpression()
{
string testExp = "3+4";
TreeNode exptectedTree = new(Token.Plus);
Token tokfour = new("4", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token tokthree = new("3", Token.Type.Numeric, Token.NumericType.Decimal, 0);
TreeNode four = new(tokfour);
TreeNode three = new(tokthree);
exptectedTree.AddChildNode(four);
exptectedTree.AddChildNode(three);
ExpressionTree returnedTree = new(testExp);
Assert.IsTrue(returnedTree.Equals(exptectedTree));
Assert.IsTrue(returnedTree.ToString() == "7");
}
/// <summary>
@@ -34,11 +27,8 @@ namespace EngineTests
public void TestExpressionTreeSimpleExpressionEvaluation()
{
string testExp = "3+4*7";
Token tok31 = new("31", Token.Type.Numeric, Token.NumericType.Decimal, 0);
TreeNode exptectedTree = new(tok31);
ExpressionTree returnedTree = new(testExp);
ExpressionTree? evaluatedTree = returnedTree.Evaluate();
Assert.IsTrue(evaluatedTree.Equals(exptectedTree));
Assert.IsTrue(returnedTree.ToString() == "31");
}
/// <summary>
@@ -49,11 +39,17 @@ namespace EngineTests
{
string testExp = "3+4*7-8/7";
decimal testValue = decimal.Divide(209 , 7);
Token tok31 = new(testValue.ToString(), Token.Type.Numeric, Token.NumericType.Decimal, 0);
TreeNode exptectedTree = new(tok31);
ExpressionTree returnedTree = new(testExp);
ExpressionTree? evaluatedTree = returnedTree.Evaluate();
Assert.IsTrue(evaluatedTree.Equals(exptectedTree));
Assert.IsTrue(returnedTree.ToString() == testValue.ToString());
}
[TestMethod]
public void TestExpressionTreeGetHashCodeReturnsHashCode()
{
string testExp = "1+1";
ExpressionTree returnedTree1 = new(testExp);
int hash = returnedTree1.GetHashCode();
Assert.IsInstanceOfType(hash, typeof(int));
}
}
}