From 461a60ff776cfc1caf44d904390a07c513b7d572 Mon Sep 17 00:00:00 2001
From: 0xJ1M <112640460+0xJ1M@users.noreply.github.com>
Date: Mon, 11 Sep 2023 21:48:27 +0100
Subject: [PATCH] Bugfix/improve code coverage (#4)
* Improved coverage
* Finished
---
.../Parser Tests/Nodes/BaseNodeTests.cs | 12 ++
.../Parser Tests/Nodes/NodeFactoryTests.cs | 122 +++++++++++++++
.../Parser Tests/Nodes/NodesTests.cs | 24 ---
.../Parser Tests/Nodes/NumericNodeTests.cs | 140 ++++++++++++++++++
.../EngineTests/Parser Tests/ParserTests.cs | 1 +
.../Parser/Parser/ExpressionTree.cs | 42 +++---
6 files changed, 296 insertions(+), 45 deletions(-)
create mode 100644 MathEngine/EngineTests/Parser Tests/Nodes/BaseNodeTests.cs
create mode 100644 MathEngine/EngineTests/Parser Tests/Nodes/NodeFactoryTests.cs
delete mode 100644 MathEngine/EngineTests/Parser Tests/Nodes/NodesTests.cs
create mode 100644 MathEngine/EngineTests/Parser Tests/Nodes/NumericNodeTests.cs
diff --git a/MathEngine/EngineTests/Parser Tests/Nodes/BaseNodeTests.cs b/MathEngine/EngineTests/Parser Tests/Nodes/BaseNodeTests.cs
new file mode 100644
index 0000000..be483f5
--- /dev/null
+++ b/MathEngine/EngineTests/Parser Tests/Nodes/BaseNodeTests.cs
@@ -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
+ {
+ }
+}
diff --git a/MathEngine/EngineTests/Parser Tests/Nodes/NodeFactoryTests.cs b/MathEngine/EngineTests/Parser Tests/Nodes/NodeFactoryTests.cs
new file mode 100644
index 0000000..9fbd311
--- /dev/null
+++ b/MathEngine/EngineTests/Parser Tests/Nodes/NodeFactoryTests.cs
@@ -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
+{
+ ///
+ /// Class for testing the NodeFactory
+ ///
+ [TestClass]
+ public class NodeFactoryTests
+ {
+ ///
+ /// Test the NodeFactory test BinaryNode creation on all defined operations
+ ///
+ [TestMethod]
+ public void TestNodeFactoryBinaryNodesOnDefinedOperations()
+ {
+ NumericNode node1 = new(200);
+ NumericNode 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);
+ }
+
+ ///
+ /// Test the NodeFactory test BinaryNode creation on exponentiation raises exception
+ ///
+ [TestMethod]
+ public void TestNodeFactoryBinaryNodesOnExponentiationRaisesException()
+ {
+ NumericNode node1 = new(200);
+ NumericNode 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");
+ }
+ }
+
+ ///
+ /// Test the NodeFactory test BinaryNode creation on invalid operation token raises exception
+ ///
+ [TestMethod]
+ public void TestNodeFactoryBinaryNodesOnInvalidOperationTokenRaisesException()
+ {
+ NumericNode node1 = new(200);
+ NumericNode 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!");
+ }
+ }
+
+
+ ///
+ /// Test the NodeFactory test NumericNode creation on all defined types
+ ///
+ [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);
+ }
+
+ ///
+ /// Test the NodeFactory test NumericNode creation on Complex type RaisesException
+ ///
+ [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");
+ }
+ }
+
+ ///
+ /// Test the NodeFactory test NumericNode creation on invalid type Raises Exception
+ ///
+ [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!");
+ }
+ }
+ }
+}
diff --git a/MathEngine/EngineTests/Parser Tests/Nodes/NodesTests.cs b/MathEngine/EngineTests/Parser Tests/Nodes/NodesTests.cs
deleted file mode 100644
index 13d1b69..0000000
--- a/MathEngine/EngineTests/Parser Tests/Nodes/NodesTests.cs
+++ /dev/null
@@ -1,24 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace EngineTests.Parser_Tests
-{
- ///
- /// Class for testing the TreeNodes
- ///
- [TestClass]
- public class NodesTests
- {
- ///
- /// Test the Parser on a basic List of tokens
- ///
- [TestMethod]
- public void TestParserBasicExpression()
- {
-
- }
- }
-}
diff --git a/MathEngine/EngineTests/Parser Tests/Nodes/NumericNodeTests.cs b/MathEngine/EngineTests/Parser Tests/Nodes/NumericNodeTests.cs
new file mode 100644
index 0000000..965951d
--- /dev/null
+++ b/MathEngine/EngineTests/Parser Tests/Nodes/NumericNodeTests.cs
@@ -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
+{
+ ///
+ /// Class for testing the TreeNodes
+ ///
+ [TestClass]
+ public class NumericNodeTests
+ {
+ ///
+ /// Test two NumericNodes with same generic type can be added
+ /// ///
+ [TestMethod]
+ public void TestNumericNodeAdd()
+ {
+ NumericNode testNode1 = new(100);
+ NumericNode testNode2 = new(100);
+ BaseNode result = testNode1 + testNode2;
+ Assert.AreEqual(result.ToString(), "200");
+ }
+
+ ///
+ /// Test two NumericNodes being added of different generic types raises exception
+ ///
+ [TestMethod]
+ public void TestNumericNodeAddDifferentTypesRaisesException()
+ {
+ NumericNode testNode1 = new(100);
+ NumericNode testNode2 = new(100);
+ try
+ {
+ BaseNode result = testNode1 + testNode2;
+ }
+ catch (InvalidOperationException ex)
+ {
+ Assert.AreEqual(ex.Message, "Attempted Invalid operation");
+ }
+ }
+
+
+ ///
+ /// Test two NumericNodes with same generic type can be subtracted
+ /// ///
+ [TestMethod]
+ public void TestNumericNodeSubtract()
+ {
+ NumericNode testNode1 = new(100);
+ NumericNode testNode2 = new(100);
+ BaseNode result = testNode1 - testNode2;
+ Assert.AreEqual(result.ToString(), "0");
+ }
+
+ ///
+ /// Test two NumericNodes being subtracted of different generic types raises exception
+ ///
+ [TestMethod]
+ public void TestNumericNodeSubtractDifferentTypesRaisesException()
+ {
+ NumericNode testNode1 = new(100);
+ NumericNode testNode2 = new(100);
+ try
+ {
+ BaseNode result = testNode1 - testNode2;
+ }
+ catch (InvalidOperationException ex)
+ {
+ Assert.AreEqual(ex.Message, "Attempted Invalid operation");
+ }
+ }
+
+
+ ///
+ /// Test two NumericNodes with same generic type can be multiplied
+ ///
+ [TestMethod]
+ public void TestNumericNodeMultiply()
+ {
+ NumericNode testNode1 = new(100);
+ NumericNode testNode2 = new(100);
+ BaseNode result = testNode1 * testNode2;
+ Assert.AreEqual(result.ToString(), "10000");
+ }
+
+ ///
+ /// Test two NumericNodes being multiplied of different generic types raises exception
+ ///
+ [TestMethod]
+ public void TestNumericNodeMultiplyDifferentTypesRaisesException()
+ {
+ NumericNode testNode1 = new(100);
+ NumericNode testNode2 = new(100);
+ try
+ {
+ BaseNode result = testNode1 * testNode2;
+ }
+ catch (InvalidOperationException ex)
+ {
+ Assert.AreEqual(ex.Message, "Attempted Invalid operation");
+ }
+ }
+
+
+ ///
+ /// Test two NumericNodes with same generic type can be divided
+ /// ///
+ [TestMethod]
+ public void TestNumericNodeDivide()
+ {
+ NumericNode testNode1 = new(100);
+ NumericNode testNode2 = new(100);
+ BaseNode result = testNode1 / testNode2;
+ Assert.AreEqual(result.ToString(), "1");
+ }
+
+ ///
+ /// Test two NumericNodes being divided of different generic types raises exception
+ ///
+ [TestMethod]
+ public void TestNumericNodeDividedDifferentTypesRaisesException()
+ {
+ NumericNode testNode1 = new(100);
+ NumericNode testNode2 = new(100);
+ try
+ {
+ BaseNode result = testNode1 / testNode2;
+ }
+ catch (InvalidOperationException ex)
+ {
+ Assert.AreEqual(ex.Message, "Attempted Invalid operation");
+ }
+ }
+ }
+}
diff --git a/MathEngine/EngineTests/Parser Tests/ParserTests.cs b/MathEngine/EngineTests/Parser Tests/ParserTests.cs
index 4975f79..ab27493 100644
--- a/MathEngine/EngineTests/Parser Tests/ParserTests.cs
+++ b/MathEngine/EngineTests/Parser Tests/ParserTests.cs
@@ -8,6 +8,7 @@ namespace EngineTests
[TestClass]
public class ParserTests
{
+
///
/// Test the Parser on a basic List of tokens
///
diff --git a/MathEngine/MathEngine/Parser/Parser/ExpressionTree.cs b/MathEngine/MathEngine/Parser/Parser/ExpressionTree.cs
index 91f3e80..cb5b137 100644
--- a/MathEngine/MathEngine/Parser/Parser/ExpressionTree.cs
+++ b/MathEngine/MathEngine/Parser/Parser/ExpressionTree.cs
@@ -44,33 +44,33 @@ namespace MathEngine.Parser.Parser
///
/// The object to compare to the current instance
/// True if they are equal, False otherwise
- 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;
+ //}
///
/// Compares the current ExpressionTree instance for equality with the given ExpressionTree
///
/// The ExpressionTree to compare to the current instance
/// True if the expression trees are equal and False otherwise
- 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()
{