Inital Commit

This commit is contained in:
Jim
2023-05-09 20:31:11 +01:00
parent 9f57a4ab4d
commit b5c318f865
13 changed files with 1174 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
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 ExpressionTree(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 ExpressionTree(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 ExpressionTree(testExp);
ExpressionTree evaluatedTree = returnedTree.Evaluate();
Assert.IsTrue(evaluatedTree.Equals(exptectedTree));
}
}
}

View File

@@ -0,0 +1,92 @@
using MathEngine.Parser.Tokeniser;
using MathEngine.Parser.Parser;
namespace EngineTests
{
/// <summary>
/// Class for testing the Parser
/// </summary>
[TestClass]
public class ParserTests
{
/// <summary>
/// Test the Parser on a basic List of tokens
/// </summary>
[TestMethod]
public void TestParserBasicExpression()
{
//Arrange
string testString = "3+4";
List<Token> testList = Tokeniser.Tokenise(testString);
Token three = new("3", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token four = new("4", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Assert.IsNotNull(testList);
Stack<Token> expectedStack = new();
expectedStack.Push(Token.Plus);
expectedStack.Push(four);
expectedStack.Push(three);
//Act
Stack<Token> returnedStack = Parser.Parse(testList);
//Assert
if (returnedStack.Count != expectedStack.Count)
{
Assert.Fail();
}
else
{
while (returnedStack.Count > 0)
{
if (!returnedStack.Pop().Equals(expectedStack.Pop()))
{
Assert.Fail();
}
}
}
}
/// <summary>
/// Test the Parser on a more compilicated basic expression to see if operator precedence is respected
/// </summary>
[TestMethod]
public void TestParserBasicExpressionAllOperators()
{
//Arrange
string testString = "3+4*8-47.2/9";
List<Token> testList = Tokeniser.Tokenise(testString);
Token three = new("3", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token four = new("4", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token eight = new("8", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token nine = new("9", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token fourSevenPoint2 = new("47.2", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Assert.IsNotNull(testList);
Stack<Token> expectedStack = new();
expectedStack.Push(Token.Minus);
expectedStack.Push(Token.Divide);
expectedStack.Push(nine);
expectedStack.Push(fourSevenPoint2);
expectedStack.Push(Token.Plus);
expectedStack.Push(Token.Multiply);
expectedStack.Push(eight);
expectedStack.Push(four);
expectedStack.Push(three);
//Act
Stack<Token> returnedStack = Parser.Parse(testList);
//Assert
if (returnedStack.Count != expectedStack.Count)
{
Assert.Fail();
}
else
{
while (returnedStack.Count > 0)
{
if (!returnedStack.Pop().Equals(expectedStack.Pop()))
{
Assert.Fail();
}
}
}
}
}
}

View File

@@ -0,0 +1,65 @@
using MathEngine.Parser.Tokeniser;
namespace EngineTests
{
/// <summary>
/// Class for testing the Tokeniser
/// </summary>
[TestClass]
public class TokeniserTests
{
/// <summary>
/// Test the tokeniser on a basic string
/// </summary>
[TestMethod]
public void TestTokeniseBasicString()
{
//Arrange
string testString = "1+1";
Token one = new("1", Token.Type.Numeric, Token.NumericType.Decimal, 0);
List<Token> expectedValue = new()
{
one,
Token.Plus,
one
};
//Act
List<Token> returnedValue = Tokeniser.Tokenise(testString);
//Assert
Assert.IsTrue(expectedValue.SequenceEqual(returnedValue));
}
/// <summary>
/// Test the tokeniser on a basic string, but with significant ammounts of whitespace
/// </summary>
[TestMethod]
public void TestTokeniseBasicStringWithWhiteSpace()
{
//Arrange
string testString = " 1 + 1 ";
Token one = new("1", Token.Type.Numeric, Token.NumericType.Decimal, 0);
List<Token> expectedValue = new()
{
one,
Token.Plus,
one
};
//Act
List<Token> returnedValue = Tokeniser.Tokenise(testString);
//Assert
Assert.IsTrue(expectedValue.SequenceEqual(returnedValue));
}
/// <summary>
/// Test the tokeniser on a string which contains a number which is not formatted correctly
/// </summary>
[TestMethod]
public void TestTokeniseStringWithInvalidNumbr()
{
//Arrange
string testString = "1+11.2.5";
//Act and Assert
Assert.ThrowsException<Exception>(() => Tokeniser.Tokenise(testString));
}
}
}

View File

@@ -0,0 +1,30 @@
using MathEngine.Parser.Tokeniser;
using MathEngine.Parser.Parser;
namespace EngineTests
{
/// <summary>
/// Class for testing the TreeNode class
/// </summary>
[TestClass]
public class TreeNodeTests
{
/// <summary>
/// Test to see if a simple expression is constructed correctly
/// </summary>
[TestMethod]
public void TestTreeNodexpression()
{
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(exptectedTree.Equals(returnedTree));
}
}
}