mirror of
https://github.com/0xJ1M/MathsEngine.git
synced 2026-06-05 01:10:08 +00:00
Feature/basic evaluator (#3)
* Refactor * Updated TreeNode system to use abstract base class and inheritence * Updated unit test coverage * Improved code coverage * Added missing files
This commit is contained in:
323
MathEngine/EngineTests/Parser Tests/Tokeniser/TokenTests.cs
Normal file
323
MathEngine/EngineTests/Parser Tests/Tokeniser/TokenTests.cs
Normal file
@@ -0,0 +1,323 @@
|
||||
using MathEngine.Parser.Tokeniser;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace EngineTests.Parser_Tests.Tokeniser
|
||||
{
|
||||
/// <summary>
|
||||
/// Class for testing the Token
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class TokenTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Test that Token constructor returns valid token
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestTokenConstructorReturnsToken()
|
||||
{
|
||||
string testTokenValue = "123";
|
||||
Token.Type testTokenType = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType = Token.NumericType.Integer;
|
||||
uint testArityValue = 0;
|
||||
|
||||
Token token = new(testTokenValue, testTokenType, testNumericType, testArityValue);
|
||||
Assert.IsNotNull(token);
|
||||
Assert.AreEqual(token.TokenValue, testTokenValue);
|
||||
Assert.AreEqual(token.Token_Type, testTokenType);
|
||||
Assert.AreEqual(token.NumericalType, testNumericType);
|
||||
Assert.AreEqual(token.FunctionArity, testArityValue);
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
/// <summary>
|
||||
/// Test ToString returns expected string format
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestToStringReturnsExpetedStringFormat()
|
||||
{
|
||||
string testTokenValue = "123";
|
||||
Token.Type testTokenType = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType = Token.NumericType.Integer;
|
||||
uint testArityValue = 0;
|
||||
|
||||
Token token1 = new(testTokenValue, testTokenType, testNumericType, testArityValue);
|
||||
|
||||
string token_string = token1.ToString();
|
||||
Assert.AreEqual(token_string, "123,Numeric,Integer,0");
|
||||
}
|
||||
#endif
|
||||
/// <summary>
|
||||
/// Test for == operator comparing two equal Tokens returns true
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestTokenEqualOperatorReturnsTrueOnEqualTokens()
|
||||
{
|
||||
string testTokenValue = "123";
|
||||
Token.Type testTokenType = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType = Token.NumericType.Integer;
|
||||
uint testArityValue = 0;
|
||||
|
||||
string testTokenValue2 = "123";
|
||||
Token.Type testTokenType2 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType2 = Token.NumericType.Integer;
|
||||
uint testArityValue2 = 0;
|
||||
|
||||
Token token1 = new(testTokenValue, testTokenType, testNumericType, testArityValue);
|
||||
Token token2 = new(testTokenValue2, testTokenType2, testNumericType2, testArityValue2);
|
||||
Assert.AreEqual(token1 == token2, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for == operator comparing unequal Tokens returns false
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestTokenEqualOperatorReturnsFalseOnUnequalTokens()
|
||||
{
|
||||
string testTokenValue1 = "123";
|
||||
Token.Type testTokenType1 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType1 = Token.NumericType.Integer;
|
||||
uint testArityValue1 = 0;
|
||||
|
||||
string testTokenValue2 = "125";
|
||||
Token.Type testTokenType2 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType2 = Token.NumericType.Integer;
|
||||
uint testArityValue2 = 0;
|
||||
|
||||
string testTokenValue3 = "123";
|
||||
Token.Type testTokenType3 = Token.Type.Operator;
|
||||
Token.NumericType testNumericType3 = Token.NumericType.Integer;
|
||||
uint testArityValue3 = 0;
|
||||
|
||||
string testTokenValue4 = "123";
|
||||
Token.Type testTokenType4 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType4 = Token.NumericType.Decimal;
|
||||
uint testArityValue4 = 0;
|
||||
|
||||
string testTokenValue5 = "123";
|
||||
Token.Type testTokenType5 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType5 = Token.NumericType.Integer;
|
||||
uint testArityValue5 = 1;
|
||||
|
||||
Token token1 = new(testTokenValue1, testTokenType1, testNumericType1, testArityValue1);
|
||||
Token token2 = new(testTokenValue2, testTokenType2, testNumericType2, testArityValue2);
|
||||
Token token3 = new(testTokenValue3, testTokenType3, testNumericType3, testArityValue3);
|
||||
Token token4 = new(testTokenValue4, testTokenType4, testNumericType4, testArityValue4);
|
||||
Token token5 = new(testTokenValue5, testTokenType5, testNumericType5, testArityValue5);
|
||||
|
||||
Assert.AreEqual(token1 == token2, false);
|
||||
Assert.AreEqual(token1 == token3, false);
|
||||
Assert.AreEqual(token1 == token4, false);
|
||||
Assert.AreEqual(token1 == token5, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for != operator comparing two equal Tokens returns true
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestTokenUnEqualOperatorReturnsFalseOnEqualTokens()
|
||||
{
|
||||
string testTokenValue = "123";
|
||||
Token.Type testTokenType = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType = Token.NumericType.Integer;
|
||||
uint testArityValue = 0;
|
||||
|
||||
Token token1 = new(testTokenValue, testTokenType, testNumericType, testArityValue);
|
||||
Token token2 = new(testTokenValue, testTokenType, testNumericType, testArityValue);
|
||||
Assert.AreEqual(token1 != token2, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for != operator comparing unequal Tokens returns True
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestTokenUnEqualOperatorReturnsTrueOnUnequalTokens()
|
||||
{
|
||||
string testTokenValue1 = "123";
|
||||
Token.Type testTokenType1 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType1 = Token.NumericType.Integer;
|
||||
uint testArityValue1 = 0;
|
||||
|
||||
string testTokenValue2 = "125";
|
||||
Token.Type testTokenType2 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType2 = Token.NumericType.Integer;
|
||||
uint testArityValue2 = 0;
|
||||
|
||||
string testTokenValue3 = "123";
|
||||
Token.Type testTokenType3 = Token.Type.Operator;
|
||||
Token.NumericType testNumericType3 = Token.NumericType.Integer;
|
||||
uint testArityValue3 = 0;
|
||||
|
||||
string testTokenValue4 = "123";
|
||||
Token.Type testTokenType4 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType4 = Token.NumericType.Decimal;
|
||||
uint testArityValue4 = 0;
|
||||
|
||||
string testTokenValue5 = "123";
|
||||
Token.Type testTokenType5 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType5 = Token.NumericType.Integer;
|
||||
uint testArityValue5 = 1;
|
||||
|
||||
Token token1 = new(testTokenValue1, testTokenType1, testNumericType1, testArityValue1);
|
||||
Token token2 = new(testTokenValue2, testTokenType2, testNumericType2, testArityValue2);
|
||||
Token token3 = new(testTokenValue3, testTokenType3, testNumericType3, testArityValue3);
|
||||
Token token4 = new(testTokenValue4, testTokenType4, testNumericType4, testArityValue4);
|
||||
Token token5 = new(testTokenValue5, testTokenType5, testNumericType5, testArityValue5);
|
||||
|
||||
Assert.AreEqual(token1 != token2, true);
|
||||
Assert.AreEqual(token1 != token3, true);
|
||||
Assert.AreEqual(token1 != token4, true);
|
||||
Assert.AreEqual(token1 != token5, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test Equals method returns True when Tokens are equal
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestTokenEqualsMethodWithTokenObjectsReturnsTrueWhenEqual()
|
||||
{
|
||||
string testTokenValue = "123";
|
||||
Token.Type testTokenType = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType = Token.NumericType.Integer;
|
||||
uint testArityValue = 0;
|
||||
|
||||
string testTokenValue2 = "123";
|
||||
Token.Type testTokenType2 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType2 = Token.NumericType.Integer;
|
||||
uint testArityValue2 = 0;
|
||||
|
||||
Token token1 = new(testTokenValue, testTokenType, testNumericType, testArityValue);
|
||||
Token token2 = new(testTokenValue2, testTokenType2, testNumericType2, testArityValue2);
|
||||
Assert.AreEqual(token1.Equals(token2), true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test Equals method returns False when Tokens are unequal
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestTokenEqualsMethodWithTokenObjectsReturnsFalseWhenUnequal()
|
||||
{
|
||||
string testTokenValue1 = "123";
|
||||
Token.Type testTokenType1 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType1 = Token.NumericType.Integer;
|
||||
uint testArityValue1 = 0;
|
||||
|
||||
string testTokenValue2 = "125";
|
||||
Token.Type testTokenType2 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType2 = Token.NumericType.Integer;
|
||||
uint testArityValue2 = 0;
|
||||
|
||||
string testTokenValue3 = "123";
|
||||
Token.Type testTokenType3 = Token.Type.Operator;
|
||||
Token.NumericType testNumericType3 = Token.NumericType.Integer;
|
||||
uint testArityValue3 = 0;
|
||||
|
||||
string testTokenValue4 = "123";
|
||||
Token.Type testTokenType4 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType4 = Token.NumericType.Decimal;
|
||||
uint testArityValue4 = 0;
|
||||
|
||||
string testTokenValue5 = "123";
|
||||
Token.Type testTokenType5 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType5 = Token.NumericType.Integer;
|
||||
uint testArityValue5 = 1;
|
||||
|
||||
Token token1 = new(testTokenValue1, testTokenType1, testNumericType1, testArityValue1);
|
||||
Token token2 = new(testTokenValue2, testTokenType2, testNumericType2, testArityValue2);
|
||||
Token token3 = new(testTokenValue3, testTokenType3, testNumericType3, testArityValue3);
|
||||
Token token4 = new(testTokenValue4, testTokenType4, testNumericType4, testArityValue4);
|
||||
Token token5 = new(testTokenValue5, testTokenType5, testNumericType5, testArityValue5);
|
||||
|
||||
Assert.AreEqual(token1.Equals(token2), false);
|
||||
Assert.AreEqual(token1.Equals(token3), false);
|
||||
Assert.AreEqual(token1.Equals(token4), false);
|
||||
Assert.AreEqual(token1.Equals(token5), false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test Equals method checks for Token equality when object is a token
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestTokenEqualsMethodWithObjectsThatIsATokenComparesForTokenEquality()
|
||||
{
|
||||
string testTokenValue = "123";
|
||||
Token.Type testTokenType = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType = Token.NumericType.Integer;
|
||||
uint testArityValue = 0;
|
||||
|
||||
string testTokenValue2 = "123";
|
||||
Token.Type testTokenType2 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType2 = Token.NumericType.Integer;
|
||||
uint testArityValue2 = 0;
|
||||
|
||||
Token token1 = new(testTokenValue, testTokenType, testNumericType, testArityValue);
|
||||
Token token2 = new(testTokenValue2, testTokenType2, testNumericType2, testArityValue2);
|
||||
object token_obj = token2;
|
||||
Assert.AreEqual(token1.Equals(token_obj), true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test Equals method checks for returns false when object is not a token
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestTokenEqualsMethodWithObjectsThatIsNotATokenReturnsFalse()
|
||||
{
|
||||
string testTokenValue = "123";
|
||||
Token.Type testTokenType = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType = Token.NumericType.Integer;
|
||||
uint testArityValue = 0;
|
||||
|
||||
int non_token = 5;
|
||||
|
||||
Token token1 = new(testTokenValue, testTokenType, testNumericType, testArityValue);
|
||||
Assert.AreEqual(token1.Equals(non_token), false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test GetHashCode on two tokens with same values is the same
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestTokenGetHashCodeOnTwoTokensWhichHaveSameValuesAreEqual()
|
||||
{
|
||||
string testTokenValue = "123";
|
||||
Token.Type testTokenType = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType = Token.NumericType.Integer;
|
||||
uint testArityValue = 0;
|
||||
|
||||
string testTokenValue2 = "123";
|
||||
Token.Type testTokenType2 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType2 = Token.NumericType.Integer;
|
||||
uint testArityValue2 = 0;
|
||||
|
||||
Token token1 = new(testTokenValue, testTokenType, testNumericType, testArityValue);
|
||||
Token token2 = new(testTokenValue2, testTokenType2, testNumericType2, testArityValue2);
|
||||
|
||||
int hash1 = token1.GetHashCode();
|
||||
int hash2 = token2.GetHashCode();
|
||||
Assert.AreEqual(hash1, hash2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test GetHashCode on two tokens with different values are different
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestTokenGetHashCodeOnTwoTokensWhichHaveDifferentValuesAreUnequal()
|
||||
{
|
||||
string testTokenValue = "123";
|
||||
Token.Type testTokenType = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType = Token.NumericType.Integer;
|
||||
uint testArityValue = 0;
|
||||
|
||||
string testTokenValue2 = "125";
|
||||
Token.Type testTokenType2 = Token.Type.Numeric;
|
||||
Token.NumericType testNumericType2 = Token.NumericType.Integer;
|
||||
uint testArityValue2 = 0;
|
||||
|
||||
Token token1 = new(testTokenValue, testTokenType, testNumericType, testArityValue);
|
||||
Token token2 = new(testTokenValue2, testTokenType2, testNumericType2, testArityValue2);
|
||||
|
||||
int hash1 = token1.GetHashCode();
|
||||
int hash2 = token2.GetHashCode();
|
||||
Assert.AreNotEqual(hash1, hash2);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user