public void TestTokenizer14() { var arrExpected = new[] { GenerateTestToken(TokenType.Number, "1"), GenerateTestToken(TokenType.Operator, "+"), GenerateTestToken(TokenType.Number, "2"), GenerateTestToken(TokenType.Operator, "-"), GenerateTestToken(TokenType.Number, "3"), GenerateTestToken(TokenType.Operator, "*"), GenerateTestToken(TokenType.Number, "4"), GenerateTestToken(TokenType.Operator, "+"), GenerateTestToken(TokenType.Number, "5"), GenerateTestToken(TokenType.Operator, "^"), GenerateTestToken(TokenType.Number, "6"), GenerateTestToken(TokenType.Operator, "^"), GenerateTestToken(TokenType.Number, "7"), GenerateTestToken(TokenType.Operator, "*"), GenerateTestToken(TokenType.Number, "8"), GenerateTestToken(TokenType.Operator, "-"), GenerateTestToken(TokenType.Number, "9"), }; var arrResult = MathExpressionTokenizer.Tokenize("1 + 2 - 3*4 + 5^6^7*8 - 9", true).ToArray(); var arrResultT = MathExpressionTokenizer.Tokenize("1 + 2 - 3 4 + 5^6^7*8 - 9", true).ToArray(); Assert.IsTrue(arrResult.SequenceEqual(arrExpected)); Assert.IsTrue(arrResultT.SequenceEqual(arrResult)); }
public void TestTokenizer05() { var arrExpected = new[] { GenerateTestToken(TokenType.Function, "sqrt"), GenerateTestToken(TokenType.Parenthesis, "("), GenerateTestToken(TokenType.Number, "9"), GenerateTestToken(TokenType.Operator, "*"), GenerateTestToken(TokenType.Number, "9"), GenerateTestToken(TokenType.Operator, "+"), GenerateTestToken(TokenType.Function, "sqrt"), GenerateTestToken(TokenType.Parenthesis, "("), GenerateTestToken(TokenType.Number, "2"), GenerateTestToken(TokenType.ArgumentSeparator, ","), GenerateTestToken(TokenType.Number, "2"), GenerateTestToken(TokenType.Parenthesis, ")"), GenerateTestToken(TokenType.ArgumentSeparator, ","), GenerateTestToken(TokenType.Number, "2"), GenerateTestToken(TokenType.Parenthesis, ")"), GenerateTestToken(TokenType.Operator, "="), GenerateTestToken(TokenType.Parenthesis, "("), GenerateTestToken(TokenType.Number, "9"), GenerateTestToken(TokenType.Parenthesis, ")"), }; var arrResult = MathExpressionTokenizer.Tokenize("sqrt(9*9 + sqrt(2)) = (9 )", true).ToArray(); Assert.IsTrue(arrResult.SequenceEqual(arrExpected)); }
public void TestTokenizer08() { var arrExpected = new[] { GenerateTestToken(TokenType.Number, "253"), GenerateTestToken(TokenType.Operator, "*"), GenerateTestToken(TokenType.Variable, "x"), GenerateTestToken(TokenType.Operator, "-"), GenerateTestToken(TokenType.Parenthesis, "("), GenerateTestToken(TokenType.Function, "tan"), GenerateTestToken(TokenType.Parenthesis, "("), GenerateTestToken(TokenType.Number, "5"), GenerateTestToken(TokenType.Operator, "*"), GenerateTestToken(TokenType.Number, "5"), GenerateTestToken(TokenType.Parenthesis, ")"), GenerateTestToken(TokenType.Operator, "*"), GenerateTestToken(TokenType.Number, "3"), GenerateTestToken(TokenType.Parenthesis, ")"), GenerateTestToken(TokenType.Operator, "/"), GenerateTestToken(TokenType.Variable, "y"), }; var arrResult = MathExpressionTokenizer.Tokenize("253 * x-(tan(5 5) 3) / y", true, "x", "y").ToArray(); Assert.IsTrue(arrResult.SequenceEqual(arrExpected)); }
public void TestTokenizer16() { var arrExpected = new[] { GenerateTestToken(TokenType.Function, "sqrt"), GenerateTestToken(TokenType.Parenthesis, "("), GenerateTestToken(TokenType.Variable, "a"), GenerateTestToken(TokenType.Operator, "^"), GenerateTestToken(TokenType.Number, "2"), GenerateTestToken(TokenType.ArgumentSeparator, ","), GenerateTestToken(TokenType.Number, "3"), GenerateTestToken(TokenType.Parenthesis, ")"), GenerateTestToken(TokenType.Operator, "/"), GenerateTestToken(TokenType.Variable, "a"), GenerateTestToken(TokenType.Operator, "^"), GenerateTestToken(TokenType.Parenthesis, "("), GenerateTestToken(TokenType.Number, "1"), GenerateTestToken(TokenType.Operator, "/"), GenerateTestToken(TokenType.Number, "2"), GenerateTestToken(TokenType.Parenthesis, ")"), }; var arrResult = MathExpressionTokenizer.Tokenize("sqrt(a^2, 3) / a^(1/2)", true, "a").ToArray(); Assert.IsTrue(arrResult.SequenceEqual(arrExpected)); }
public void MathExpressionTokenizer_Can_Tokenize_Modulus() { var tokenizer = new MathExpressionTokenizer(); var result = tokenizer.Tokenize("4%2"); Assert.AreEqual(3, result.Count); Assert.AreEqual(TokenType.Modulus, result[1].Type); }
public void WordRewriter_Will_Detect_And_Rewrite_Functions() { MathExpressionTokenizer tokenizer = new MathExpressionTokenizer(); var tokens = tokenizer.Tokenize("sqrt(1)"); WordRewriter rewriter = new WordRewriter(); var result = rewriter.Rewrite(tokens); Assert.AreEqual(TokenType.Function, result[0].Type); Assert.AreEqual("sqrt", result[0].Value); }
public void MathExpressionTokenizer_Can_Tokenize_Float_Values_With_Dot() { var tokenizer = new MathExpressionTokenizer(); var result = tokenizer.Tokenize("3.23+1"); Assert.AreEqual(3, result.Count); Assert.AreEqual(TokenType.Numeric, result[0].Type); Assert.AreEqual(3.23D, result[0].Value); Assert.AreEqual(TokenType.Addition, result[1].Type); Assert.AreEqual(TokenType.Numeric, result[2].Type); Assert.AreEqual(1D, result[2].Value); }
public void TestTokenizer11() { var arrExpected = new[] { GenerateTestToken(TokenType.Number, "1.2042"), GenerateTestToken(TokenType.Operator, "-"), GenerateTestToken(TokenType.Number, "2"), GenerateTestToken(TokenType.Operator, "*"), GenerateTestToken(TokenType.Number, "0"), }; var arrResult = MathExpressionTokenizer.Tokenize("+1.2042 -2 0", true).ToArray(); Assert.IsTrue(arrResult.SequenceEqual(arrExpected)); }
public void TestTokenizer15() { var arrExpected = new[] { GenerateTestToken(TokenType.Number, "5"), GenerateTestToken(TokenType.Operator, "+"), GenerateTestToken(TokenType.Number, "-5"), GenerateTestToken(TokenType.Operator, "+"), GenerateTestToken(TokenType.Number, "5"), }; var arrResult = MathExpressionTokenizer.Tokenize("5 +-5+ 5", true).ToArray(); Assert.IsTrue(arrResult.SequenceEqual(arrExpected)); }
public void TestTokenizer03() { var arrExpected = new[] { GenerateTestToken(TokenType.Number, "1"), GenerateTestToken(TokenType.Number, "0"), GenerateTestToken(TokenType.WhiteSpace, " "), GenerateTestToken(TokenType.Operator, "-"), GenerateTestToken(TokenType.Number, "2"), GenerateTestToken(TokenType.WhiteSpace, " "), GenerateTestToken(TokenType.Number, "0"), }; var arrResult = MathExpressionTokenizer.Tokenize("10 -2 0").ToArray(); Assert.IsTrue(arrResult.SequenceEqual(arrExpected)); }
public void TestTokenizer17() { var arrExpected = new[] { GenerateTestToken(TokenType.Parenthesis, "("), GenerateTestToken(TokenType.Number, "-21"), GenerateTestToken(TokenType.Operator, "+"), GenerateTestToken(TokenType.Number, "4"), GenerateTestToken(TokenType.Parenthesis, ")"), GenerateTestToken(TokenType.Operator, "+"), GenerateTestToken(TokenType.Number, "10"), }; var arrResult = MathExpressionTokenizer.Tokenize("(- 21 + 4) + 10", true).ToArray(); Assert.IsTrue(arrResult.SequenceEqual(arrExpected)); }
public void MathExpressionTokenizer_Can_Tokenize_Simple_Expression() { var tokenizer = new MathExpressionTokenizer(); var result = tokenizer.Tokenize("4+(2*7)/2-7"); Assert.AreEqual(11, result.Count); Assert.AreEqual(TokenType.Numeric, result[0].Type); Assert.AreEqual(4D, result[0].Value); Assert.AreEqual(TokenType.Addition, result[1].Type); Assert.AreEqual(TokenType.OpeningParenthesis, result[2].Type); Assert.AreEqual(TokenType.Numeric, result[3].Type); Assert.AreEqual(2D, result[3].Value); Assert.AreEqual(TokenType.Multiplication, result[4].Type); Assert.AreEqual(TokenType.Numeric, result[5].Type); Assert.AreEqual(7D, result[5].Value); Assert.AreEqual(TokenType.ClosingParenthesis, result[6].Type); Assert.AreEqual(TokenType.Division, result[7].Type); Assert.AreEqual(2D, result[8].Value); Assert.AreEqual(TokenType.Subtraction, result[9].Type); Assert.AreEqual(7D, result[10].Value); }
public void TestTokenizer04() { var arrExpected = new[] { GenerateTestToken(TokenType.Function, "sqrt"), GenerateTestToken(TokenType.Parenthesis, "("), GenerateTestToken(TokenType.Number, "9"), GenerateTestToken(TokenType.Operator, "*"), GenerateTestToken(TokenType.Number, "9"), GenerateTestToken(TokenType.Parenthesis, ")"), GenerateTestToken(TokenType.WhiteSpace, " "), GenerateTestToken(TokenType.Operator, "="), GenerateTestToken(TokenType.WhiteSpace, " "), GenerateTestToken(TokenType.Parenthesis, "("), GenerateTestToken(TokenType.Number, "9"), GenerateTestToken(TokenType.WhiteSpace, " "), GenerateTestToken(TokenType.Parenthesis, ")"), }; var arrResult = MathExpressionTokenizer.Tokenize("sqrt(9*9) = (9 )").ToArray(); Assert.IsTrue(arrResult.SequenceEqual(arrExpected)); }
public void MathExpressionTokenizer_Unknown_Symbol_Throws_Exception() { var tokenizer = new MathExpressionTokenizer(); var exception = Assert.Throws<MathExpressionException>(() => tokenizer.Tokenize("1$2")); Assert.AreEqual("Invalid expression.", exception.Message); Assert.AreEqual("Unknown token '$' in expression.", exception.Details); }
public void MathExpressionTokenizer_Treats_Comma_As_Argument_Separator() { var tokenizer = new MathExpressionTokenizer(); var result = tokenizer.Tokenize("3,23+1"); Assert.AreEqual(5, result.Count); Assert.AreEqual(TokenType.Separator, result[1].Type); }
/// <summary> /// Erstellt eine Baumstruktur, die einen mathematischen Term repräsentiert. /// </summary> /// <param name="readerInfixTerm">Der mathematische Term in der Infix-Notation.</param> /// <param name="environment">Die Umgebung, in dessen Kontext der mathematische Term seine Gültigkeit besitzt.</param> /// <param name="arrVariables">Eine Liste mit Variablen, die in dem Term vorkommen.</param> /// <returns>Eine Baumstruktur, die einen mathematischen Term repräsentiert.</returns> public static Node Get(TextReader readerInfixTerm, MathEnvironment environment, params string[] arrVariables) { return(Get(MathExpressionTokenizer.Tokenize(readerInfixTerm, true, arrVariables), environment, true)); }
public void MathExpressionTokenizer_Can_Tokenize_Wikipedia_Example() { var tokenizer = new MathExpressionTokenizer(); var result = tokenizer.Tokenize("3+4*2/(1-5)^2^3"); Assert.AreEqual(15, result.Count); }
public void MathExpressionTokenizer_Can_Tokenize_Words() { var tokenizer = new MathExpressionTokenizer(); var result = tokenizer.Tokenize("2+x*1"); Assert.AreEqual(5, result.Count); Assert.AreEqual(TokenType.Numeric, result[0].Type); Assert.AreEqual(2D, result[0].Value); Assert.AreEqual(TokenType.Addition, result[1].Type); Assert.AreEqual(TokenType.Word, result[2].Type); Assert.AreEqual(TokenType.Multiplication, result[3].Type); Assert.AreEqual(TokenType.Numeric, result[4].Type); Assert.AreEqual(1D, result[4].Value); }
/// <summary> /// Parst einen mathematischen Term und gibt diesen aufgeteilt in einzelne Bauteile zurück. /// </summary> /// <param name="strTerm">Der mathematische Term als String, z.B. "5 + 5 - sqrt(9)"</param> /// <param name="arrVariables">Eine Liste mit Variablen, die in dem Term vortkommen, beispielsweise "x", "y"</param> /// <returns></returns> public static Token[] TokenizeTerm(string strTerm, params string[] arrVariables) { return(MathExpressionTokenizer.Tokenize(strTerm, true, arrVariables).ToArray()); }
private TokenStream GetPostfixTokenStream(string source) { MathExpressionTokenizer tokenizer = new MathExpressionTokenizer(); TokenStream tokens = tokenizer.Tokenize(source); return new TokenPipeline().Process(tokens); }
/// <summary> /// Parst einen mathematischen Term und gibt diesen aufgeteilt in einzelne Bauteile zurück. /// </summary> /// <param name="reader">Ein StringReader-Objekt, von dem der zu parsende Term ausgelesen wird.</param> /// <param name="arrVariables">Eine Liste mit Variablen, die in dem Term vortkommen, beispielsweise "x", "y"</param> /// <returns></returns> public static Token[] TokenizeTerm(StringReader reader, params string[] arrVariables) { return(MathExpressionTokenizer.Tokenize(reader, true, arrVariables).ToArray()); }