Пример #1
0
        public void ItReadsDataFromInput()
        {
            //Arrange
            var i          = new IdentifierToken("i");
            var expression = new Token[]
            {
                i, new ReadOperation()
            };

            var store = new VariableStore()
            {
                [i] = new ConstantToken <float>(0)
            };

            using (var memoryStream = new MemoryStream())
            {
                var writer = new StreamWriter(memoryStream);
                writer.WriteLine("5");
                writer.Flush();
                memoryStream.Position = 0;

                var executor = new PrnExpressionExecutor(memoryStream);

                //Act
                var result = executor.ComputeExpression(expression, store);

                //Assert
                memoryStream.Position = 0;
                Assert.AreEqual(5, store[i].Value);
            }
        }
Пример #2
0
        public void ItWritesDataToOutput()
        {
            //Arrange
            var i          = new IdentifierToken("i");
            var expression = new Token[]
            {
                i, new WriteOperation()
            };

            var store = new VariableStore()
            {
                [i] = new ConstantToken <float>(5)
            };

            using (var memoryStream = new MemoryStream())
            {
                var executor = new PrnExpressionExecutor(null, memoryStream);

                //Act
                var result = executor.ComputeExpression(expression, store);

                //Assert
                memoryStream.Position = 0;
                Assert.AreEqual("i = 5", new StreamReader(memoryStream).ReadToEnd());
            }
        }
Пример #3
0
        public void ItComputesExpressionWithUnarySubtraction()
        {
            var expression = new Token[]
            {
                new ConstantToken <float>(2), new ConstantToken <float>(4), new StringToken("@"), new StringToken("+")
            };

            var executor = new PrnExpressionExecutor();
            var result   = executor.ComputeExpression(expression, new VariableStore());

            Assert.AreEqual(-2, result);
        }
Пример #4
0
        public void ItComputesNegativeConstantExpression()
        {
            var expression = new Token[]
            {
                new ConstantToken <float>(2), new ConstantToken <float>(4), new StringToken("-")
            };

            var executor = new PrnExpressionExecutor();
            var result   = executor.ComputeExpression(expression, new VariableStore());

            Assert.AreEqual(-2, result);
        }
Пример #5
0
        public void ItComputesConstantExpression(float operand1, float operand2, string operation, float expected)
        {
            var expression = new Token[]
            {
                new ConstantToken <float>(operand1), new ConstantToken <float>(operand2), new StringToken(operation)
            };

            var executor = new PrnExpressionExecutor();
            var result   = executor.ComputeExpression(expression, new VariableStore());

            Assert.AreEqual(expected, result);
        }
Пример #6
0
        public void ItMakesAssignments()
        {
            var i          = new IdentifierToken("i");
            var expression = new Token[]
            {
                i, new ConstantToken <float>(2), new StringToken("=")
            };

            var store = new VariableStore()
            {
                [i] = new ConstantToken <float>(0)
            };
            var executor = new PrnExpressionExecutor();
            var result   = executor.ComputeExpression(expression, store);

            Assert.AreEqual(2, store[i].Value);
        }
Пример #7
0
        public void ItComputesExpressionWithIdentifiers()
        {
            var i          = new IdentifierToken("i");
            var expression = new Token[]
            {
                i, i, new StringToken("+")
            };

            var executor = new PrnExpressionExecutor();
            var result   = executor.ComputeExpression(expression, new VariableStore()
            {
                [i] = new ConstantToken <float>(2)
            });

            ;

            Assert.AreEqual(4, result);
        }
Пример #8
0
        public void ItComputesNegatedLogicalExpressions()
        {
            var i          = new IdentifierToken("i");
            var res        = new IdentifierToken("res");
            var expression = new Token[]
            {
                res, i, new ConstantToken <float>(2), new StringToken("=="), new StringToken("!"), new StringToken("=")
            };

            var store = new VariableStore()
            {
                [i]   = new ConstantToken <float>(2),
                [res] = new ConstantToken <float>(1)
            };
            var executor = new PrnExpressionExecutor();
            var result   = executor.ComputeExpression(expression, store);

            Assert.AreEqual(0, store[res].Value);
        }
Пример #9
0
        public void ItMakesUnconditionalJumps()
        {
            var i          = new IdentifierToken("i");
            var expression = new Token[]
            {
                new LabelToken("t"), new StringToken(":"),
                new LabelToken("test"), new UnconditionalJumpOperation(),
                i, new ConstantToken <float>(1), new StringToken("="),
                new LabelToken("test"), new StringToken(":")
            };

            var store = new VariableStore()
            {
                [i] = new ConstantToken <float>(0)
            };
            var executor = new PrnExpressionExecutor();
            var result   = executor.ComputeExpression(expression, store);

            Assert.AreEqual(0, store[i].Value);
        }
Пример #10
0
        public void ItComputesComplexExpressions()
        {
            var i          = new IdentifierToken("i");
            var res        = new IdentifierToken("res");
            var expression = new Token[]
            {
                res, new StringToken("["), i, new ConstantToken <float>(2), new StringToken("=="), i, new ConstantToken <float>(3), new StringToken("<"), new StringToken("and"),
                new StringToken("]"), new StringToken("=")
            };

            var store = new VariableStore()
            {
                [i]   = new ConstantToken <float>(2),
                [res] = new ConstantToken <float>(0)
            };
            var executor = new PrnExpressionExecutor();
            var result   = executor.ComputeExpression(expression, store);

            Assert.AreEqual(1, store[res].Value);
        }
Пример #11
0
        public void ItExecutesLoop()
        {
            var logger = new LoggerConfiguration().CreateLogger();

            //Arrange
            var i        = new IdentifierToken("i");
            var j        = new IdentifierToken("j");
            var res      = new IdentifierToken("sum");
            var lexer    = new Lexer(logger);
            var sequence = lexer.ParseTokens(new StringReader(@"
begin
    do i=1 to 5
        do j=2 to 7
            sum = sum + i*j
        next
    next
end")).ToList();

            var store = new VariableStore()
            {
                [i]   = new ConstantToken <float>(0),
                [j]   = new ConstantToken <float>(0),
                [res] = new ConstantToken <float>(0)
            };

            var prnProvider = new BasicExecutor(logger);
            var labels      = lexer.Labels.ToList();
            var prn         = prnProvider.PrnComposer.GetPrn(sequence, labels, store);


            var executor = new PrnExpressionExecutor();

            //Act
            var result = executor.ComputeExpression(prn, store);

            //Assert
            Assert.AreEqual(5 + 1, store[i].Value);
            Assert.AreEqual(7 + 1, store[j].Value);
            Assert.AreEqual(405, store[res].Value);
        }