Files
MathsEngine/MathEngine/EngineTests/Parser Tests/ExpressionTreeTests.cs
0xJ1M 9c5bf4c0d5 Feature/initial refactor (#1)
* Done some initial modification of the code, removed a redundant check in GetNextChar. Updated some comments

* Initial refactor: Cleaned up remaning comments
2023-05-11 19:56:39 +01:00

59 lines
2.3 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";
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));
}
/// <summary>
/// Test to see if a simple expression is evaluated correctly
/// </summary>
[TestMethod]
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));
}
/// <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);
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));
}
}
}