mirror of
https://github.com/0xJ1M/MathsEngine.git
synced 2026-06-04 23:10:09 +00:00
12
MathEngine/EngineTests/Parser Tests/Nodes/BaseNodeTests.cs
Normal file
12
MathEngine/EngineTests/Parser 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
|
||||
{
|
||||
}
|
||||
}
|
||||
122
MathEngine/EngineTests/Parser Tests/Nodes/NodeFactoryTests.cs
Normal file
122
MathEngine/EngineTests/Parser Tests/Nodes/NodeFactoryTests.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
using MathEngine.Parser.Parser;
|
||||
using MathEngine.Parser.Parser.Nodes;
|
||||
using MathEngine.Parser.Tokeniser;
|
||||
using static MathEngine.Parser.Tokeniser.Token;
|
||||
|
||||
namespace EngineTests.Parser_Tests.Nodes
|
||||
{
|
||||
/// <summary>
|
||||
/// Class for testing the NodeFactory
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class NodeFactoryTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Test the NodeFactory test BinaryNode creation on all defined operations
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
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>
|
||||
[TestMethod]
|
||||
public void TestNodeFactoryBinaryNodesOnExponentiationRaisesException()
|
||||
{
|
||||
NumericNode<decimal> node1 = new(200);
|
||||
NumericNode<decimal> node2 = new(100);
|
||||
|
||||
Token exp = new("^", Token.Type.Exponentiation, NumericType.NaN, 0);
|
||||
try
|
||||
{
|
||||
BaseNode expBiinary = NodeFactory.CreateBinaryNode(exp, node1, node2);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
Assert.AreEqual(ex.Message, "Exponentiation is not implemented at this time");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test the NodeFactory test BinaryNode creation on invalid operation token raises exception
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestNodeFactoryBinaryNodesOnInvalidOperationTokenRaisesException()
|
||||
{
|
||||
NumericNode<decimal> node1 = new(200);
|
||||
NumericNode<decimal> node2 = new(100);
|
||||
|
||||
Token invalid = new("(", Token.Type.OpenBracket, NumericType.NaN, 0);
|
||||
try
|
||||
{
|
||||
BaseNode expBiinary = NodeFactory.CreateBinaryNode(invalid, node1, node2);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
Assert.AreEqual(ex.Message, "Attempted to create a BinaryNode with an invalid operation!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test the NodeFactory test NumericNode creation on all defined types
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestNodeFactoryNumericNodesOnDefinedTypes()
|
||||
{
|
||||
Token test_token1 = new("100", Token.Type.Numeric, Token.NumericType.Integer, 0);
|
||||
Token test_token2 = new("100.5", Token.Type.Numeric, Token.NumericType.Decimal, 0);
|
||||
|
||||
BaseNode testNode1 = NodeFactory.CreateNumericNode(test_token1);
|
||||
BaseNode testNode2 = NodeFactory.CreateNumericNode(test_token2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test the NodeFactory test NumericNode creation on Complex type RaisesException
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestNodeFactoryNumericNodesOnComplexTypeRaisesException()
|
||||
{
|
||||
try
|
||||
{
|
||||
Token comp = new("1+i", Token.Type.Numeric, Token.NumericType.Complex, 0);
|
||||
BaseNode testNode = NodeFactory.CreateNumericNode(comp);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
Assert.AreEqual(ex.Message, "Complex Numbers are not implemented at this time");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test the NodeFactory test NumericNode creation on invalid type Raises Exception
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestNodeFactoryNumericNodesOnInvalidTypeRaisesException()
|
||||
{
|
||||
try
|
||||
{
|
||||
Token comp = new("(", Token.Type.CloseBrace, Token.NumericType.NaN, 0);
|
||||
BaseNode testNode = NodeFactory.CreateNumericNode(comp);
|
||||
}
|
||||
catch (InvalidDataException ex)
|
||||
{
|
||||
Assert.AreEqual(ex.Message, "Attempted to create a NumericNode with non numeric data!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EngineTests.Parser_Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class for testing the TreeNodes
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class NodesTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Test the Parser on a basic List of tokens
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestParserBasicExpression()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
140
MathEngine/EngineTests/Parser Tests/Nodes/NumericNodeTests.cs
Normal file
140
MathEngine/EngineTests/Parser Tests/Nodes/NumericNodeTests.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
using MathEngine.Parser.Parser;
|
||||
using MathEngine.Parser.Parser.Nodes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EngineTests.Parser_Tests.Nodes
|
||||
{
|
||||
/// <summary>
|
||||
/// Class for testing the TreeNodes
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class NumericNodeTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Test two NumericNodes with same generic type can be added
|
||||
/// /// </summary>
|
||||
[TestMethod]
|
||||
public void TestNumericNodeAdd()
|
||||
{
|
||||
NumericNode<decimal> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
BaseNode result = testNode1 + testNode2;
|
||||
Assert.AreEqual(result.ToString(), "200");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes being added of different generic types raises exception
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestNumericNodeAddDifferentTypesRaisesException()
|
||||
{
|
||||
NumericNode<int> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
try
|
||||
{
|
||||
BaseNode result = testNode1 + testNode2;
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
Assert.AreEqual(ex.Message, "Attempted Invalid operation");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes with same generic type can be subtracted
|
||||
/// /// </summary>
|
||||
[TestMethod]
|
||||
public void TestNumericNodeSubtract()
|
||||
{
|
||||
NumericNode<decimal> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
BaseNode result = testNode1 - testNode2;
|
||||
Assert.AreEqual(result.ToString(), "0");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes being subtracted of different generic types raises exception
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestNumericNodeSubtractDifferentTypesRaisesException()
|
||||
{
|
||||
NumericNode<int> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
try
|
||||
{
|
||||
BaseNode result = testNode1 - testNode2;
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
Assert.AreEqual(ex.Message, "Attempted Invalid operation");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes with same generic type can be multiplied
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestNumericNodeMultiply()
|
||||
{
|
||||
NumericNode<decimal> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
BaseNode result = testNode1 * testNode2;
|
||||
Assert.AreEqual(result.ToString(), "10000");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes being multiplied of different generic types raises exception
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestNumericNodeMultiplyDifferentTypesRaisesException()
|
||||
{
|
||||
NumericNode<int> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
try
|
||||
{
|
||||
BaseNode result = testNode1 * testNode2;
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
Assert.AreEqual(ex.Message, "Attempted Invalid operation");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes with same generic type can be divided
|
||||
/// /// </summary>
|
||||
[TestMethod]
|
||||
public void TestNumericNodeDivide()
|
||||
{
|
||||
NumericNode<decimal> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
BaseNode result = testNode1 / testNode2;
|
||||
Assert.AreEqual(result.ToString(), "1");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test two NumericNodes being divided of different generic types raises exception
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestNumericNodeDividedDifferentTypesRaisesException()
|
||||
{
|
||||
NumericNode<int> testNode1 = new(100);
|
||||
NumericNode<decimal> testNode2 = new(100);
|
||||
try
|
||||
{
|
||||
BaseNode result = testNode1 / testNode2;
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
Assert.AreEqual(ex.Message, "Attempted Invalid operation");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ namespace EngineTests
|
||||
[TestClass]
|
||||
public class ParserTests
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Test the Parser on a basic List of tokens
|
||||
/// </summary>
|
||||
|
||||
@@ -44,33 +44,33 @@ namespace MathEngine.Parser.Parser
|
||||
/// </summary>
|
||||
/// <param name="other">The object to compare to the current instance</param>
|
||||
/// <returns>True if they are equal, False otherwise</returns>
|
||||
public override bool Equals(object? other)
|
||||
{
|
||||
if (other is BaseNode)
|
||||
{
|
||||
ExpressionTree otherTree = new((BaseNode)other);
|
||||
return this.Equals(otherTree);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//public override bool Equals(object? other)
|
||||
//{
|
||||
// if (other is BaseNode)
|
||||
// {
|
||||
// ExpressionTree otherTree = new((BaseNode)other);
|
||||
// return this.Equals(otherTree);
|
||||
//}
|
||||
// return false;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// Compares the current ExpressionTree instance for equality with the given ExpressionTree
|
||||
/// </summary>
|
||||
/// <param name="other">The ExpressionTree to compare to the current instance</param>
|
||||
/// <returns>True if the expression trees are equal and False otherwise</returns>
|
||||
public bool Equals(ExpressionTree other)
|
||||
{
|
||||
if (this == null || other == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (this.rootNode == null || other.rootNode == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return this.rootNode.Equals(other.rootNode);
|
||||
}
|
||||
// public bool Equals(ExpressionTree other)
|
||||
//{
|
||||
// if (this == null || other == null)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
// if (this.rootNode == null || other.rootNode == null)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
// return this.rootNode.Equals(other.rootNode);
|
||||
//}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user