Added missing files

This commit is contained in:
Jim
2023-09-08 17:56:24 +01:00
parent 6ef53e7a96
commit 663005977d
7 changed files with 749 additions and 0 deletions

View File

@@ -0,0 +1,119 @@
using MathEngine.Parser.Tokeniser;
namespace EngineTests
{
/// <summary>
/// Class for testing the Tokeniser
/// </summary>
[TestClass]
public class TokeniserTests
{
/// <summary>
/// Test the tokeniser on an empty string
/// </summary>
[TestMethod]
public void TestTokeniseEmptystringReturnsEmptyList()
{
//Arrange
string testString = "";
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.AreEqual(returnedValue.Count, 0);
}
/// <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));
}
/// <summary>
/// Test the tokeniser with all operators
/// </summary>
[TestMethod]
public void TestTokeniseStringWithAllOperators()
{
//Arrange
string testString = "1+2-3*4/5";
Token one = new("1", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token two = new("2", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token three = new("3", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token four = new("4", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token five = new("5", Token.Type.Numeric, Token.NumericType.Decimal, 0);
List<Token> expectedValue = new()
{
one,
Token.Plus,
two,
Token.Minus,
three,
Token.Multiply,
four,
Token.Divide,
five
};
//Act
List<Token> returnedValue = Tokeniser.Tokenise(testString);
//Assert
Assert.IsTrue(expectedValue.SequenceEqual(returnedValue));
}
}
}

View 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);
}
}
}