mirror of
https://github.com/0xJ1M/MathsEngine.git
synced 2026-06-05 02:10:08 +00:00
Major refactor
This commit is contained in:
89
MathEngine/EngineTests/AST Tests/ExpressionTreeTests.cs
Normal file
89
MathEngine/EngineTests/AST Tests/ExpressionTreeTests.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Xunit;
|
||||
|
||||
using MathEngine.Tokenizer;
|
||||
using static MathEngine.Tokenizer.Token;
|
||||
using System.Collections.Generic;
|
||||
using Xunit.Sdk;
|
||||
using MathEngine.Parser;
|
||||
using MathEngine.AST.Nodes;
|
||||
using MathEngine.AST;
|
||||
|
||||
namespace EngineTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class for testing the ExpressionTree Class
|
||||
/// </summary>
|
||||
public class ExpressionTreeTests
|
||||
{
|
||||
|
||||
public static IEnumerable<object[]> TestASTConstructionCases
|
||||
{
|
||||
get
|
||||
{
|
||||
Token one = new("1", TokenType.Numeric);
|
||||
Token two = new("2", TokenType.Numeric);
|
||||
Token three = new("3", TokenType.Numeric);
|
||||
Token four = new("4", TokenType.Numeric);
|
||||
Token five = new("5", TokenType.Numeric);
|
||||
|
||||
return new List<object[]> {
|
||||
new object[] { "1+1", true },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test to see if a simple expression is constructed correctly
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[MemberData(nameof(TestASTConstructionCases))]
|
||||
public void TestExpressionTreeSimpleExpression(string expression, object expected_result)
|
||||
{
|
||||
Assert.True(true);
|
||||
//List<Token> tokens = ExpressionTokenizer.Tokenize(expression);
|
||||
//Queue<Token> rpn_form = Parser.Parse(tokens);
|
||||
|
||||
//BaseNode returned_reuslt = TreeGenerator.TreeFromRPN(rpn_form);
|
||||
|
||||
//Assert.Equal(expected_result, returned_reuslt);
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// Test to see if a simple expression is evaluated correctly
|
||||
///// </summary>
|
||||
//[Fact]
|
||||
//public void TestExpressionTreeSimpleExpressionEvaluation()
|
||||
//{
|
||||
// Assert.Equal(true, true);
|
||||
// return;
|
||||
// /*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>
|
||||
//[Fact]
|
||||
//public void TestExpressionTreeSimpleExpressionAllBaseOperatorsEvaluation()
|
||||
//{
|
||||
// Assert.Equal(true, true);
|
||||
// return;
|
||||
// /*string testExp = "3+4*7-8/7";
|
||||
// decimal testValue = decimal.Divide(209 , 7);
|
||||
// ExpressionTree returnedTree = new(testExp);
|
||||
// Assert.IsTrue(returnedTree.ToString() == testValue.ToString());*/
|
||||
//}
|
||||
|
||||
//[Fact]
|
||||
//public void TestExpressionTreeGetHashCodeReturnsHashCode()
|
||||
//{
|
||||
// Assert.Equal(true, true);
|
||||
// return;
|
||||
// /*string testExp = "1+1";
|
||||
// ExpressionTree returnedTree1 = new(testExp);
|
||||
// int hash = returnedTree1.GetHashCode();
|
||||
// Assert.IsInstanceOfType(hash, typeof(int));*/
|
||||
//}
|
||||
}
|
||||
}
|
||||
12
MathEngine/EngineTests/AST Tests/Nodes/BaseNodeTests.cs
Normal file
12
MathEngine/EngineTests/AST Tests/Nodes/BaseNodeTests.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EngineTests.Parser_Tests.Nodes
|
||||
{
|
||||
internal class BaseNodeTests
|
||||
{
|
||||
}
|
||||
}
|
||||
127
MathEngine/EngineTests/AST Tests/Nodes/NodeFactoryTests.cs
Normal file
127
MathEngine/EngineTests/AST Tests/Nodes/NodeFactoryTests.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using Xunit;
|
||||
|
||||
using MathEngine.AST.Nodes;
|
||||
using MathEngine.Tokenizer;
|
||||
|
||||
using static MathEngine.Tokenizer.Token;
|
||||
|
||||
namespace EngineTests.Parser_Tests.Nodes
|
||||
{
|
||||
/// <summary>
|
||||
/// Class for testing the NodeFactory
|
||||
/// </summary>
|
||||
public class NodeFactoryTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Test the NodeFactory test BinaryNode creation on all defined operations
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestNodeFactoryBinaryNodesOnDefinedOperations()
|
||||
{
|
||||
NumericNode<decimal> node1 = new(200);
|
||||
NumericNode<decimal> node2 = new(100);
|
||||
|
||||
Token plus = Token.Plus;
|
||||
Token minus = Token.Minus;
|
||||
Token multiply = Token.Multiply;
|
||||
Token divide = Token.Divide;
|
||||
|
||||
BaseNode addNode = NodeFactory.CreateBinaryNode(plus, node1, node2);
|
||||
BaseNode subtractNode = NodeFactory.CreateBinaryNode(minus, node1, node2);
|
||||
BaseNode multiplyNode = NodeFactory.CreateBinaryNode(multiply, node1, node2);
|
||||
BaseNode divideNode = NodeFactory.CreateBinaryNode(divide, node1, node2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test the NodeFactory test BinaryNode creation on exponentiation raises exception
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestNodeFactoryBinaryNodesOnExponentiationRaisesException()
|
||||
{
|
||||
NumericNode<decimal> node1 = new(200);
|
||||
NumericNode<decimal> node2 = new(100);
|
||||
|
||||
Token exp = new("^", TokenType.Exponentiation);
|
||||
try
|
||||
{
|
||||
BaseNode expBiinary = NodeFactory.CreateBinaryNode(exp, node1, node2);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
Assert.Equal("Exponentiation is not supported at this time!", ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test the NodeFactory test BinaryNode creation on invalid operation token raises exception
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestNodeFactoryBinaryNodesOnInvalidOperationTokenRaisesException()
|
||||
{
|
||||
NumericNode<decimal> node1 = new(200);
|
||||
NumericNode<decimal> node2 = new(100);
|
||||
|
||||
Token invalid = new("(", TokenType.OpenBracket);
|
||||
try
|
||||
{
|
||||
BaseNode expBiinary = NodeFactory.CreateBinaryNode(invalid, node1, node2);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
Assert.Equal("Attempted to create a BinaryNode with an invalid operation!", ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test the NodeFactory test NumericNode creation on all defined types
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestNodeFactoryNumericNodesOnDefinedTypes()
|
||||
{
|
||||
Assert.True(true);
|
||||
return;
|
||||
//Token test_token2 = new("100.5", TokenType.Numeric);
|
||||
|
||||
//BaseNode testNode2 = NodeFactory.CreateNumericNode();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test the NodeFactory test NumericNode creation on Complex type RaisesException
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestNodeFactoryNumericNodesOnComplexTypeRaisesException()
|
||||
{
|
||||
Assert.True(true);
|
||||
return;
|
||||
//try
|
||||
//{
|
||||
// Token comp = new("1+i", TokenType.Numeric);
|
||||
// BaseNode testNode = NodeFactory.CreateNumericNode();
|
||||
//}
|
||||
//catch (NotImplementedException ex)
|
||||
//{
|
||||
// Assert.Equal(ex.Message, "Complex Numbers are not implemented at this time");
|
||||
//}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test the NodeFactory test NumericNode creation on invalid type Raises Exception
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestNodeFactoryNumericNodesOnInvalidTypeRaisesException()
|
||||
{
|
||||
Assert.True(true);
|
||||
return;
|
||||
//try
|
||||
//{
|
||||
// Token comp = new("(", TokenType.CloseBrace);
|
||||
// BaseNode testNode = NodeFactory.CreateNumericNode();
|
||||
//}
|
||||
//catch (InvalidDataException ex)
|
||||
//{
|
||||
// Assert.Equal(ex.Message, "Attempted to create a NumericNode with non numeric data!");
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
135
MathEngine/EngineTests/AST Tests/Nodes/NumericNodeTests.cs
Normal file
135
MathEngine/EngineTests/AST Tests/Nodes/NumericNodeTests.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
using Xunit;
|
||||
|
||||
using MathEngine.AST.Nodes;
|
||||
|
||||
namespace EngineTests.Parser_Tests.Nodes
|
||||
{
|
||||
/// <summary>
|
||||
/// Class for testing the TreeNodes
|
||||
/// </summary>
|
||||
public class NumericNodeTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Test two NumericNodes with same generic type can be added
|
||||
/// /// </summary>
|
||||
[Fact]
|
||||
public void TestNumericNodeAdd()
|
||||
{
|
||||
NumericNode<decimal> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
BaseNode result = testNode1 + testNode2;
|
||||
Assert.Equal("200", result.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes being added of different generic types raises exception
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestNumericNodeAddDifferentTypesRaisesException()
|
||||
{
|
||||
NumericNode<int> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
try
|
||||
{
|
||||
BaseNode result = testNode1 + testNode2;
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
Assert.Equal("Attempted Invalid operation", ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes with same generic type can be subtracted
|
||||
/// /// </summary>
|
||||
[Fact]
|
||||
public void TestNumericNodeSubtract()
|
||||
{
|
||||
NumericNode<decimal> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
BaseNode result = testNode1 - testNode2;
|
||||
Assert.Equal("0", result.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes being subtracted of different generic types raises exception
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestNumericNodeSubtractDifferentTypesRaisesException()
|
||||
{
|
||||
NumericNode<int> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
try
|
||||
{
|
||||
BaseNode result = testNode1 - testNode2;
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
Assert.Equal("Attempted Invalid operation", ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes with same generic type can be multiplied
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestNumericNodeMultiply()
|
||||
{
|
||||
NumericNode<decimal> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
BaseNode result = testNode1 * testNode2;
|
||||
Assert.Equal("10000", result.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes being multiplied of different generic types raises exception
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestNumericNodeMultiplyDifferentTypesRaisesException()
|
||||
{
|
||||
NumericNode<int> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
try
|
||||
{
|
||||
BaseNode result = testNode1 * testNode2;
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
Assert.Equal("Attempted Invalid operation", ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes with same generic type can be divided
|
||||
/// /// </summary>
|
||||
[Fact]
|
||||
public void TestNumericNodeDivide()
|
||||
{
|
||||
NumericNode<decimal> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
BaseNode result = testNode1 / testNode2;
|
||||
Assert.Equal("1", result.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes being divided of different generic types raises exception
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestNumericNodeDividedDifferentTypesRaisesException()
|
||||
{
|
||||
NumericNode<int> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
try
|
||||
{
|
||||
BaseNode result = testNode1 / testNode2;
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
Assert.Equal("Attempted Invalid operation", ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user