mirror of
https://github.com/0xJ1M/MathsEngine.git
synced 2026-06-05 02:10:08 +00:00
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:
@@ -1,13 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
|
||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Parser Tests\TreeNodeTests.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user