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