Ejemplo n.º 1
0
        [Test] public void InfixToPostfixNotation2()
        {
            var expressionString = "expmpl = (pow(-1, -1.18 ^ 2 ^ 3 + z) - 1 / (-x + 1))";

            var lexer  = new Lexer();
            var tokens = lexer.Tokenize(expressionString);

            var translator = new ExpressionTranslator(tokens, new HashedVariables());

            translator.CreateCalculationStack();

            var lexicalQueue = new Queue <Lexema>();

            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema("-", Token.Unar));
            lexicalQueue.Enqueue(new Lexema("1.18", Token.Value));
            lexicalQueue.Enqueue(new Lexema("-", Token.Unar));
            lexicalQueue.Enqueue(new Lexema("2", Token.Value));
            lexicalQueue.Enqueue(new Lexema("3", Token.Value));
            lexicalQueue.Enqueue(new Lexema("^", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("^", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("z", Token.Variable));
            lexicalQueue.Enqueue(new Lexema("+", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("pow", Token.Function));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema("x", Token.Variable));
            lexicalQueue.Enqueue(new Lexema("-", Token.Unar));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema("+", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("/", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("-", Token.Binar));

            Assert.IsTrue(SequenceTester.IsEqual(translator.LexicalQueue, lexicalQueue));
        }
Ejemplo n.º 2
0
        [Test] public void InfixToPostfixNotation1()
        {
            var expressionString = "expmpl += sin(cos(13 - 3)) + 0.8 ^ 11";

            var lexer  = new Lexer();
            var tokens = lexer.Tokenize(expressionString);

            var translator = new ExpressionTranslator(tokens, new HashedVariables());

            translator.CreateCalculationStack();

            var lexicalQueue = new Queue <Lexema>();

            lexicalQueue.Enqueue(new Lexema("expmpl", Token.Variable));
            lexicalQueue.Enqueue(new Lexema("13", Token.Value));
            lexicalQueue.Enqueue(new Lexema("3", Token.Value));
            lexicalQueue.Enqueue(new Lexema("-", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("cos", Token.Function));
            lexicalQueue.Enqueue(new Lexema("sin", Token.Function));
            lexicalQueue.Enqueue(new Lexema("+", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("0.8", Token.Value));
            lexicalQueue.Enqueue(new Lexema("11", Token.Value));
            lexicalQueue.Enqueue(new Lexema("^", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("+", Token.Binar));

            Assert.IsTrue(SequenceTester.IsEqual(translator.LexicalQueue, lexicalQueue));
        }
Ejemplo n.º 3
0
        [Test] public void CorrectExpression()
        {
            var expression1 = "exmpl+=0.1+0.8*(-1-1/(bstr_grow_restart_boost+1))";

            var lexer  = new Lexer();
            var tokens = lexer.Tokenize(expression1);

            var lexicalQueue = new Queue <Lexema>();

            lexicalQueue.Enqueue(new Lexema("exmpl", Token.Variable));
            lexicalQueue.Enqueue(new Lexema("+=", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("0.1", Token.Value));
            lexicalQueue.Enqueue(new Lexema("+", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("0.8", Token.Value));
            lexicalQueue.Enqueue(new Lexema("*", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("(", Token.LeftBracket));
            lexicalQueue.Enqueue(new Lexema("-", Token.Unar));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema("-", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema("/", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("(", Token.LeftBracket));
            lexicalQueue.Enqueue(new Lexema("bstr_grow_restart_boost", Token.Variable));
            lexicalQueue.Enqueue(new Lexema("+", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema(")", Token.RightBracket));
            lexicalQueue.Enqueue(new Lexema(")", Token.RightBracket));

            Assert.IsTrue(SequenceTester.IsEqual(tokens.LexicalQueue, lexicalQueue));
        }
Ejemplo n.º 4
0
        [Test] public void FunctionCorrectAsVariableName()
        {
            var expression1 = "exp += sin + 0.8 * (-1 - cos / (x + 1))";

            var lexer  = new Lexer();
            var tokens = lexer.Tokenize(expression1);

            var lexicalQueue = new Queue <Lexema>();

            lexicalQueue.Enqueue(new Lexema("exp", Token.Variable));
            lexicalQueue.Enqueue(new Lexema("+=", Token.Binar));

            lexicalQueue.Enqueue(new Lexema("sin", Token.Variable));

            lexicalQueue.Enqueue(new Lexema("+", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("0.8", Token.Value));
            lexicalQueue.Enqueue(new Lexema("*", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("(", Token.LeftBracket));
            lexicalQueue.Enqueue(new Lexema("-", Token.Unar));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema("-", Token.Binar));

            lexicalQueue.Enqueue(new Lexema("cos", Token.Variable));

            lexicalQueue.Enqueue(new Lexema("/", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("(", Token.LeftBracket));
            lexicalQueue.Enqueue(new Lexema("x", Token.Variable));
            lexicalQueue.Enqueue(new Lexema("+", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema(")", Token.RightBracket));
            lexicalQueue.Enqueue(new Lexema(")", Token.RightBracket));

            Assert.IsTrue(SequenceTester.IsEqual(tokens.LexicalQueue, lexicalQueue));
        }
Ejemplo n.º 5
0
        [Test] public void CorrectIterator()
        {
            var iterator = "expmpl = [1, sin(13 - 3) + 0.8 % 11 * (pow(-1, -1.18) - 1 / (-x + 1))]";

            var lexer  = new Lexer();
            var tokens = lexer.Tokenize(iterator);

            var lexicalQueue = new Queue <Lexema>();

            lexicalQueue.Enqueue(new Lexema("expmpl", Token.Variable));
            lexicalQueue.Enqueue(new Lexema("=", Token.Binar));

            lexicalQueue.Enqueue(new Lexema("[", Token.SqLeftBracket));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema(",", Token.Divider));
            lexicalQueue.Enqueue(new Lexema("sin", Token.Function));
            lexicalQueue.Enqueue(new Lexema("(", Token.LeftBracket));
            lexicalQueue.Enqueue(new Lexema("13", Token.Value));
            lexicalQueue.Enqueue(new Lexema("-", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("3", Token.Value));
            lexicalQueue.Enqueue(new Lexema(")", Token.RightBracket));
            lexicalQueue.Enqueue(new Lexema("+", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("0.8", Token.Value));
            lexicalQueue.Enqueue(new Lexema("%", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("11", Token.Value));
            lexicalQueue.Enqueue(new Lexema("*", Token.Binar));

            lexicalQueue.Enqueue(new Lexema("(", Token.LeftBracket));
            lexicalQueue.Enqueue(new Lexema("pow", Token.Function));
            lexicalQueue.Enqueue(new Lexema("(", Token.LeftBracket));
            lexicalQueue.Enqueue(new Lexema("-", Token.Unar));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema(",", Token.Divider));
            lexicalQueue.Enqueue(new Lexema("-", Token.Unar));
            lexicalQueue.Enqueue(new Lexema("1.18", Token.Value));
            lexicalQueue.Enqueue(new Lexema(")", Token.RightBracket));

            lexicalQueue.Enqueue(new Lexema("-", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema("/", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("(", Token.LeftBracket));
            lexicalQueue.Enqueue(new Lexema("-", Token.Unar));
            lexicalQueue.Enqueue(new Lexema("x", Token.Variable));
            lexicalQueue.Enqueue(new Lexema("+", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema(")", Token.RightBracket));
            lexicalQueue.Enqueue(new Lexema(")", Token.RightBracket));
            lexicalQueue.Enqueue(new Lexema("]", Token.SqRightBracket));

            Assert.IsTrue(SequenceTester.IsEqual(tokens.LexicalQueue, lexicalQueue));
        }
Ejemplo n.º 6
0
        [Test] public void CorrectExpressionWithFunctions()
        {
            var expression1 = "expmpl += sin(0.1) + 0.8 * (pow(-1, 2) - 1 / (x + 1))";

            var lexer  = new Lexer();
            var tokens = lexer.Tokenize(expression1);

            var lexicalQueue = new Queue <Lexema>();

            lexicalQueue.Enqueue(new Lexema("expmpl", Token.Variable));
            lexicalQueue.Enqueue(new Lexema("+=", Token.Binar));

            lexicalQueue.Enqueue(new Lexema("sin", Token.Function));
            lexicalQueue.Enqueue(new Lexema("(", Token.LeftBracket));
            lexicalQueue.Enqueue(new Lexema("0.1", Token.Value));
            lexicalQueue.Enqueue(new Lexema(")", Token.RightBracket));

            lexicalQueue.Enqueue(new Lexema("+", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("0.8", Token.Value));
            lexicalQueue.Enqueue(new Lexema("*", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("(", Token.LeftBracket));

            lexicalQueue.Enqueue(new Lexema("pow", Token.Function));
            lexicalQueue.Enqueue(new Lexema("(", Token.LeftBracket));
            lexicalQueue.Enqueue(new Lexema("-", Token.Unar));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema(",", Token.Divider));
            lexicalQueue.Enqueue(new Lexema("2", Token.Value));
            lexicalQueue.Enqueue(new Lexema(")", Token.RightBracket));

            lexicalQueue.Enqueue(new Lexema("-", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema("/", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("(", Token.LeftBracket));
            lexicalQueue.Enqueue(new Lexema("x", Token.Variable));
            lexicalQueue.Enqueue(new Lexema("+", Token.Binar));
            lexicalQueue.Enqueue(new Lexema("1", Token.Value));
            lexicalQueue.Enqueue(new Lexema(")", Token.RightBracket));
            lexicalQueue.Enqueue(new Lexema(")", Token.RightBracket));

            Assert.IsTrue(SequenceTester.IsEqual(tokens.LexicalQueue, lexicalQueue));
        }