Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Interpreter/Expressions/ExecExpressionTests.cs @ 14392

Last change on this file since 14392 was 14392, checked in by pkimmesw, 8 years ago

#2665 Full Push 3.0 instruction set and tests; Added first benchmark test (count odds) for random walk tests;

File size: 4.4 KB
Line 
1using HeuristicLab.Algorithms.PushGP;
2using HeuristicLab.Algorithms.PushGP.Expressions;
3using HeuristicLab.Algorithms.PushGP.Stack;
4using Microsoft.VisualStudio.TestTools.UnitTesting;
5
6namespace HeuristicLab.Tests.Interpreter.Expressions
7{
8    [TestClass]
9    public class ExecExpressionTests : CommonTests<Expression>
10    {
11        protected override string TypeName { get { return "EXEC"; } }
12
13        protected override IStack<Expression> Stack { get { return interpreter.ExecStack; } }
14
15        protected override void Test(Expression expression)
16        {
17            this.interpreter.InterpretAsync(expression, true).Wait();
18        }
19
20        [TestMethod]
21        public void TestIfTrue()
22        {
23            interpreter.BooleanStack.Push(true);
24            interpreter.Interpret("( EXEC.IF WAHR FALSCH )");
25
26            Assert.AreEqual("WAHR", interpreter.NameStack.Top);
27            this.TestStackCounts(nameStack: 1);
28        }
29
30        [TestMethod]
31        public void TestIfFalse()
32        {
33            interpreter.BooleanStack.Push(false);
34            interpreter.Interpret("( EXEC.IF WAHR FALSCH )");
35
36            Assert.AreEqual("FALSCH", interpreter.NameStack.Top);
37            this.TestStackCounts(nameStack: 1);
38        }
39
40        [TestMethod]
41        public void TestK()
42        {
43            var first = Parser.Parse("A");
44            var second = Parser.Parse("B");
45            var third = Parser.Parse("C");
46
47            interpreter.ExecStack.Push(third, second, first);
48            this.Test(new ExecKExpression());
49
50            Assert.AreEqual(first, interpreter.ExecStack.Top);
51            Assert.AreEqual(third, interpreter.ExecStack.Bottom);
52            this.TestStackCounts(execStack: 2);
53        }
54
55        [TestMethod]
56        public void TestS()
57        {
58            var first = Parser.Parse("A");
59            var second = Parser.Parse("B");
60            var third = Parser.Parse("C");
61            var result = Parser.Parse("( B C )");
62
63            interpreter.ExecStack.Push(third, second, first);
64            this.Test(new ExecSExpression());
65
66            Assert.AreEqual(result, interpreter.ExecStack.ReverseElementAt(2));
67            Assert.AreEqual(third, interpreter.ExecStack.ReverseElementAt(1));
68            Assert.AreEqual(first, interpreter.ExecStack.Top);
69            this.TestStackCounts(execStack: 3);
70        }
71
72        [TestMethod]
73        public void TestY()
74        {
75            var first = Parser.Parse("A");
76            var result = Parser.Parse("( EXEC.Y A )");
77
78            interpreter.ExecStack.Push(first);
79            this.Test(new ExecYExpression());
80
81            Assert.AreEqual(first, interpreter.ExecStack.Top);
82            Assert.AreEqual(result, interpreter.ExecStack.ReverseElementAt(1));
83            this.TestStackCounts(execStack: 2);
84        }
85
86        [TestMethod]
87        public void TestNestedDoRange()
88        {
89            interpreter.Interpret("( 0 2 EXEC.DO*RANGE ( 1 INTEGER.+ 0 3 EXEC.DO*RANGE ( 1 INTEGER.+ INTEGER.* ) INTEGER.+ )");
90
91            Assert.AreEqual(144, interpreter.IntegerStack.Top);
92            this.TestStackCounts(integerStack: 1);
93        }
94
95        [TestMethod]
96        public void TestNestedDoCount()
97        {
98            interpreter.Interpret("( 2 EXEC.DO*COUNT ( 1 INTEGER.+ 3 EXEC.DO*COUNT ( 1 INTEGER.+ INTEGER.* ) INTEGER.+ )");
99
100            Assert.AreEqual(144, interpreter.IntegerStack.Top);
101            this.TestStackCounts(integerStack: 1);
102        }
103
104        [TestMethod]
105        public void TestNestedDoTimes()
106        {
107            interpreter.Interpret("( 3 EXEC.DO*TIMES ( 2 3 EXEC.DO*TIMES ( 2 INTEGER.* ) INTEGER.+ )");
108
109            Assert.AreEqual(128, interpreter.IntegerStack.Top);
110            this.TestStackCounts(integerStack: 1);
111        }
112
113        protected override Expression[] GetValues(int count)
114        {
115            var values = new Expression[count];
116
117            for (var i = 0; i < count; i++)
118            {
119                values[i] = new CodeNoopExpression();
120            }
121
122            return values;
123        }
124
125        protected override Expression[] Get2Different()
126        {
127            return new Expression[]
128            {
129                new CodeNoopExpression(),
130                new CodeNullExpression()
131            };
132        }
133
134        protected override void CheckOtherStacksAreEmpty()
135        {
136            this.TestStackCounts(execStack: null);
137        }
138    }
139}
Note: See TracBrowser for help on using the repository browser.