Files
MathsEngine/MathEngine/EngineTests/Parser Tests/ExpressionTreeTests.cs
0xJ1M a555a131af Feature/basic evaluator (#2)
* Refactor

* Updated TreeNode system to use abstract base class and inheritence

* Updated unit test coverage

* Improved code coverage
2023-09-08 17:53:13 +01:00

55 lines
1.8 KiB
C#

using MathEngine.Parser.Tokeniser;
using MathEngine.Parser.Parser;
namespace EngineTests
{
/// <summary>
/// Class for testing the ExpressionTree Class
/// </summary>
[TestClass]
public class ExpressionTreeTests
{
/// <summary>
/// Test to see if a simple expression is constructed correctly
/// </summary>
[TestMethod]
public void TestExpressionTreeSimpleExpression()
{
string testExp = "3+4";
ExpressionTree returnedTree = new(testExp);
Assert.IsTrue(returnedTree.ToString() == "7");
}
/// <summary>
/// Test to see if a simple expression is evaluated correctly
/// </summary>
[TestMethod]
public void TestExpressionTreeSimpleExpressionEvaluation()
{
string testExp = "3+4*7";
ExpressionTree returnedTree = new(testExp);
Assert.IsTrue(returnedTree.ToString() == "31");
}
/// <summary>
/// Test to see if a simple expression using all base operators (+,-,*,/) is evaluated correctly
/// </summary>
[TestMethod]
public void TestExpressionTreeSimpleExpressionAllBaseOperatorsEvaluation()
{
string testExp = "3+4*7-8/7";
decimal testValue = decimal.Divide(209 , 7);
ExpressionTree returnedTree = new(testExp);
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));
}
}
}