namespace HeuristicLab.Tests.Interpreter.Expressions { using HeuristicLab.Problems.ProgramSynthesis.Push.Parser; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class MixedExpressionTests : ExpressionTest { private const double Delta = 0.00000001; [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example1() { this.interpreter.Run("( 2 3 INTEGER.* 4.1 5.2 FLOAT.+ TRUE FALSE BOOLEAN.OR )"); Assert.AreEqual(6, this.interpreter.IntegerStack.Top); Assert.AreEqual(9.3, this.interpreter.FloatStack.Top); Assert.IsTrue(this.interpreter.BooleanStack.Top); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example2() { this.interpreter.Run("( 5 1.23 INTEGER.+ ( 4 ) INTEGER.- 5.67 FLOAT.* )"); Assert.AreEqual(1, this.interpreter.IntegerStack.Top); Assert.AreEqual(6.9741, this.interpreter.FloatStack.Top); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example3() { this.interpreter.Run("( 5 INTEGER.DUP INTEGER.+ )"); Assert.AreEqual(10, this.interpreter.IntegerStack.Top); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example4() { this.interpreter.Run("( 5 CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DO )"); Assert.AreEqual(10, this.interpreter.IntegerStack.Top); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example5() { this.interpreter.Run("( DOUBLE CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DEFINE 5 DOUBLE )"); Assert.AreEqual(10, this.interpreter.IntegerStack.Top); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example6() { this.interpreter.Run("( CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) DOUBLE CODE.DEFINE 5 DOUBLE )"); Assert.AreEqual(10, this.interpreter.IntegerStack.Top); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example7() { this.interpreter.Run("( DOUBLE EXEC.DEFINE ( INTEGER.DUP INTEGER.+ ) 5 DOUBLE )"); Assert.AreEqual(10, this.interpreter.IntegerStack.Top); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example8() { this.configuration.TopLevelPushCode = true; this.interpreter.IntegerStack.Push(5); this.interpreter.Run(@"( CODE.QUOTE ( INTEGER.POP 1 ) CODE.QUOTE ( CODE.DUP INTEGER.DUP 1 INTEGER.- CODE.DO INTEGER.* ) INTEGER.DUP 2 INTEGER.< CODE.IF )"); Assert.AreEqual(120, this.interpreter.IntegerStack.Top); Assert.IsTrue(this.interpreter.CodeStack.Count == 1); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example9() { this.interpreter.IntegerStack.Push(5); this.interpreter.Run("( 1 INTEGER.MAX 1 EXEC.DO*RANGE INTEGER.* )"); Assert.AreEqual(120, this.interpreter.IntegerStack.Top); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example10() { this.interpreter.IntegerStack.Push(5); this.interpreter.Run("( 1 INTEGER.MAX 1 EXEC.DO*RANGE ( 2 INTEGER.* ) )"); Assert.AreEqual(2, this.interpreter.IntegerStack.Top); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example11() { var program = PushParser.Parse("( INTEGER.= CODE.QUOTE FLOAT.* CODE.QUOTE FLOAT./ CODE.IF )"); this.interpreter.IntegerStack.Push(1, 1); this.interpreter.FloatStack.Push(2.1, 0.7); this.interpreter.Run(program); Assert.IsTrue(this.interpreter.IntegerStack.IsEmpty); Assert.AreEqual(1.47, this.interpreter.FloatStack.Top, Delta); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); this.interpreter.Clear(); this.interpreter.IntegerStack.Push(1, 2); this.interpreter.FloatStack.Push(2.1, 0.7); this.interpreter.Run(program); Assert.IsTrue(this.interpreter.IntegerStack.IsEmpty); Assert.AreEqual(3d, this.interpreter.FloatStack.Top, Delta); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example12() { var program = PushParser.Parse("( INTEGER.= EXEC.IF FLOAT.* FLOAT./ )"); this.interpreter.IntegerStack.Push(1, 1); this.interpreter.FloatStack.Push(2.1, 0.7); this.interpreter.Run(program); Assert.IsTrue(this.interpreter.IntegerStack.IsEmpty); Assert.AreEqual(1.47, this.interpreter.FloatStack.Top, Delta); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); this.interpreter.Clear(); this.interpreter.IntegerStack.Push(1, 2); this.interpreter.FloatStack.Push(2.1, 0.7); this.interpreter.Run(program); Assert.IsTrue(this.interpreter.IntegerStack.IsEmpty); Assert.AreEqual(3d, this.interpreter.FloatStack.Top, Delta); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example13() { this.interpreter.IntegerStack.Push(5); this.interpreter.Run("( EXEC.Y ( ( FALSE 2 INTEGER.* ) EXEC.IF ( ) EXEC.POP ) )"); Assert.AreEqual(10, this.interpreter.IntegerStack.Top); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example14() { this.interpreter.IntegerStack.Push(5); this.interpreter.Run("( EXEC.Y ( ( 2 INTEGER.* INTEGER.DUP 1000 INTEGER.< ) EXEC.IF ( ) EXEC.POP ) )"); Assert.AreEqual(1280, this.interpreter.IntegerStack.Top); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example15() { this.interpreter.IntegerStack.Push(10); this.interpreter.FloatStack.Push(2); this.interpreter.Run(@"( ARG FLOAT.DEFINE EXEC.Y ( ARG FLOAT.* 1 INTEGER.- INTEGER.DUP 1 INTEGER.> EXEC.IF ( ) EXEC.POP ) )"); Assert.AreEqual(1024, this.interpreter.FloatStack.Top, Delta); Assert.AreEqual(1, this.interpreter.IntegerStack.Top); Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); Assert.IsTrue(this.interpreter.NameStack.IsEmpty); Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExampleTest")] public void Example16() { var list = PushParser.Parse("( A B C )"); this.interpreter.CodeStack.Push(list); this.interpreter.Run("( CODE.DUP 0 CODE.INSERT )"); var id = this.interpreter.CodeStack.Top.GetHashCode(); if (id == default(int)) Assert.Fail(); } } }