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 interpreter.ExecStack; } } protected override void Test(Expression expression) { interpreter.Run(expression, true); interpreter.Step(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestIfTrue() { interpreter.BooleanStack.Push(true); interpreter.Run("( EXEC.IF WAHR FALSCH )"); Assert.AreEqual("WAHR", interpreter.NameStack.Top); TestStackCounts(nameStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestIfFalse() { interpreter.BooleanStack.Push(false); interpreter.Run("( EXEC.IF WAHR FALSCH )"); Assert.AreEqual("FALSCH", interpreter.NameStack.Top); 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 )"); interpreter.ExecStack.Push(third, second, first); Test(new ExecSExpression()); Assert.AreEqual(result, interpreter.ExecStack[2]); Assert.AreEqual(third, interpreter.ExecStack[1]); Assert.AreEqual(first, interpreter.ExecStack.Top); 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 )"); interpreter.ExecStack.Push(first); Test(new ExecYExpression()); Assert.AreEqual(first, interpreter.ExecStack.Top); Assert.AreEqual(result, interpreter.ExecStack[1]); TestStackCounts(2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestNestedDoRange() { interpreter.Run( "( 1 0 2 EXEC.DO*RANGE ( INTEGER.POP 1 1 4 EXEC.DO*RANGE INTEGER.* INTEGER.* ) )"); Assert.AreEqual(13824, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestNestedDoCount() { interpreter.Run( "( 2 EXEC.DO*COUNT ( 1 INTEGER.+ 3 EXEC.DO*COUNT ( 1 INTEGER.+ INTEGER.* ) INTEGER.+ )"); Assert.AreEqual(144, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("ExecExpressionTest")] public void TestNestedDoTimes() { interpreter.Run("( 4 EXEC.DO*TIMES ( 2 4 EXEC.DO*TIMES ( 2 INTEGER.* ) INTEGER.+ )"); Assert.AreEqual(128, interpreter.IntegerStack.Top); 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() { TestStackCounts(null); } } }