namespace HeuristicLab.Tests.Interpreter.Expressions { using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; using HeuristicLab.Problems.ProgramSynthesis.Push.Parser; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class ExecExpressionTests : CommonTests { protected override string TypeName { get { return "EXEC"; } } protected override IPushStack Stack { get { return this.interpreter.ExecStack; } } protected override void Test(Expression expression) { this.interpreter.Run(expression, true); this.interpreter.Step(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestIfTrue() { this.interpreter.BooleanStack.Push(true); this.interpreter.Run("( EXEC.IF WAHR FALSCH )"); Assert.AreEqual("WAHR", this.interpreter.NameStack.Top); this.TestStackCounts(nameStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestIfFalse() { this.interpreter.BooleanStack.Push(false); this.interpreter.Run("( EXEC.IF WAHR FALSCH )"); Assert.AreEqual("FALSCH", this.interpreter.NameStack.Top); this.TestStackCounts(nameStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestK() { var first = PushParser.Parse("A"); var second = PushParser.Parse("B"); var third = PushParser.Parse("C"); interpreter.ExecStack.Push(third, second, first); Test(new ExecKExpression()); Assert.AreEqual(first, interpreter.ExecStack.Top); Assert.AreEqual(third, interpreter.ExecStack.Bottom); TestStackCounts(2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestS() { var first = PushParser.Parse("A"); var second = PushParser.Parse("B"); var third = PushParser.Parse("C"); var result = PushParser.Parse("( B C )"); this.interpreter.ExecStack.Push(third, second, first); this.Test(new ExecSExpression()); Assert.AreEqual(result, this.interpreter.ExecStack[2]); Assert.AreEqual(third, this.interpreter.ExecStack[1]); Assert.AreEqual(first, this.interpreter.ExecStack.Top); this.TestStackCounts(3); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestY() { var first = PushParser.Parse("A"); var result = PushParser.Parse("( EXEC.Y A )"); this.interpreter.ExecStack.Push(first); this.Test(new ExecYExpression()); Assert.AreEqual(first, this.interpreter.ExecStack.Top); Assert.AreEqual(result, this.interpreter.ExecStack[1]); this.TestStackCounts(2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestNestedDoRange() { this.interpreter.Run( "( 0 2 EXEC.DO*RANGE ( 1 INTEGER.+ 0 3 EXEC.DO*RANGE ( 1 INTEGER.+ INTEGER.* ) INTEGER.+ )"); Assert.AreEqual(144, this.interpreter.IntegerStack.Top); this.TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestNestedDoCount() { this.interpreter.Run( "( 2 EXEC.DO*COUNT ( 1 INTEGER.+ 3 EXEC.DO*COUNT ( 1 INTEGER.+ INTEGER.* ) INTEGER.+ )"); Assert.AreEqual(144, this.interpreter.IntegerStack.Top); this.TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestNestedDoTimes() { this.interpreter.Run("( 3 EXEC.DO*TIMES ( 2 3 EXEC.DO*TIMES ( 2 INTEGER.* ) INTEGER.+ )"); Assert.AreEqual(128, this.interpreter.IntegerStack.Top); this.TestStackCounts(integerStack: 1); } protected override Expression[] GetValues(int count) { var values = new Expression[count]; for (var i = 0; i < count; i++) values[i] = new CodeNoopExpression(); return values; } protected override Expression[] Get2Different() { return new Expression[] { new CodeNoopExpression(), new CodeNullExpression() }; } protected override void CheckOtherStacksAreEmpty() { this.TestStackCounts(null); } } }