Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Interpreter/Expressions/ExampleTests.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: 10.7 KB
Line 
1using HeuristicLab.Algorithms.PushGP.Interpreter;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3
4namespace HeuristicLab.Tests.Interpreter.Expressions
5{
6    [TestClass]
7    public class MixedExpressionTests : InterpreterTest
8    {
9        const double Delta = 0.00000001;
10
11        [TestMethod]
12        public void Example1()
13        {
14            this.interpreter.Interpret("( 2 3 INTEGER.* 4.1 5.2 FLOAT.+ TRUE FALSE BOOLEAN.OR )");
15
16            Assert.AreEqual(6, interpreter.IntegerStack.Top);
17            Assert.AreEqual(9.3, interpreter.FloatStack.Top);
18            Assert.AreEqual(interpreter.BooleanStack.Top, true);
19            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
20            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
21            Assert.IsTrue(interpreter.NameStack.IsEmpty);
22        }
23
24        [TestMethod]
25        public void Example2()
26        {
27            this.interpreter.Interpret("( 5 1.23 INTEGER.+ ( 4 ) INTEGER.- 5.67 FLOAT.* )");
28
29            Assert.AreEqual(1, interpreter.IntegerStack.Top);
30            Assert.AreEqual(6.9741, interpreter.FloatStack.Top);
31            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
32            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
33            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
34            Assert.IsTrue(interpreter.NameStack.IsEmpty);
35        }
36
37        [TestMethod]
38        public void Example3()
39        {
40            this.interpreter.Interpret("( 5 INTEGER.DUP INTEGER.+ )");
41
42            Assert.AreEqual(10, interpreter.IntegerStack.Top);
43            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
44            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
45            Assert.IsTrue(interpreter.NameStack.IsEmpty);
46            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
47            Assert.IsTrue(interpreter.FloatStack.IsEmpty);
48        }
49
50        [TestMethod]
51        public void Example4()
52        {
53            this.interpreter.Interpret("( 5 CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DO )");
54
55            Assert.AreEqual(10, interpreter.IntegerStack.Top);
56            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
57            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
58            Assert.IsTrue(interpreter.NameStack.IsEmpty);
59            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
60            Assert.IsTrue(interpreter.FloatStack.IsEmpty);
61        }
62
63        [TestMethod]
64        public void Example5()
65        {
66            this.interpreter.Interpret("( DOUBLE CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DEFINE 5 DOUBLE )");
67
68            Assert.AreEqual(10, interpreter.IntegerStack.Top);
69            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
70            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
71            Assert.IsTrue(interpreter.NameStack.IsEmpty);
72            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
73            Assert.IsTrue(interpreter.FloatStack.IsEmpty);
74        }
75
76        [TestMethod]
77        public void Example6()
78        {
79            this.interpreter.Interpret("( CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) DOUBLE CODE.DEFINE 5 DOUBLE )");
80
81            Assert.AreEqual(10, interpreter.IntegerStack.Top);
82            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
83            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
84            Assert.IsTrue(interpreter.NameStack.IsEmpty);
85            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
86            Assert.IsTrue(interpreter.FloatStack.IsEmpty);
87        }
88
89        [TestMethod]
90        public void Example7()
91        {
92            this.interpreter.Interpret("( DOUBLE EXEC.DEFINE ( INTEGER.DUP INTEGER.+ ) 5 DOUBLE )");
93
94            Assert.AreEqual(10, interpreter.IntegerStack.Top);
95            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
96            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
97            Assert.IsTrue(interpreter.NameStack.IsEmpty);
98            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
99            Assert.IsTrue(interpreter.FloatStack.IsEmpty);
100        }
101
102        [TestMethod]
103        public void Example8()
104        {
105            this.interpreter.Configuration.TopLevelPushCode = true;
106
107            this.interpreter.IntegerStack.Push(5);
108            this.interpreter.Interpret(@"( CODE.QUOTE ( INTEGER.POP 1 )
109                                           CODE.QUOTE ( CODE.DUP INTEGER.DUP 1 INTEGER.- CODE.DO INTEGER.* )
110                                           INTEGER.DUP 2 INTEGER.< CODE.IF )");
111
112            Assert.AreEqual(120, interpreter.IntegerStack.Top);
113            Assert.IsTrue(interpreter.CodeStack.Count == 1);
114            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
115            Assert.IsTrue(interpreter.NameStack.IsEmpty);
116            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
117            Assert.IsTrue(interpreter.FloatStack.IsEmpty);
118        }
119
120        [TestMethod]
121        public void Example9()
122        {
123            this.interpreter.IntegerStack.Push(5);
124            this.interpreter.Interpret("( 1 INTEGER.MAX 1 EXEC.DO*RANGE INTEGER.* )");
125
126            Assert.AreEqual(120, interpreter.IntegerStack.Top);
127            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
128            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
129            Assert.IsTrue(interpreter.NameStack.IsEmpty);
130            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
131            Assert.IsTrue(interpreter.FloatStack.IsEmpty);
132        }
133
134        [TestMethod]
135        public void Example10()
136        {
137            this.interpreter.IntegerStack.Push(5);
138            this.interpreter.Interpret("( 1 INTEGER.MAX 1 EXEC.DO*RANGE ( 2 INTEGER.* ) )");
139
140            Assert.AreEqual(2, interpreter.IntegerStack.Top);
141            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
142            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
143            Assert.IsTrue(interpreter.NameStack.IsEmpty);
144            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
145            Assert.IsTrue(interpreter.FloatStack.IsEmpty);
146        }
147
148        [TestMethod]
149        public void Example11()
150        {
151            var program = PushGPInterpreter.Encode("( INTEGER.= CODE.QUOTE FLOAT.* CODE.QUOTE FLOAT./ CODE.IF )");
152
153            this.interpreter.IntegerStack.Push(1, 1);
154            this.interpreter.FloatStack.Push(2.1, 0.7);
155
156            this.interpreter.Interpret(program);
157
158            Assert.IsTrue(interpreter.IntegerStack.IsEmpty);
159            Assert.AreEqual(1.47, interpreter.FloatStack.Top, Delta);
160            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
161            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
162            Assert.IsTrue(interpreter.NameStack.IsEmpty);
163            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
164
165            this.interpreter.Clear();
166            this.interpreter.IntegerStack.Push(1, 2);
167            this.interpreter.FloatStack.Push(2.1, 0.7);
168            this.interpreter.Interpret(program);
169
170            Assert.IsTrue(interpreter.IntegerStack.IsEmpty);
171            Assert.AreEqual(3d, interpreter.FloatStack.Top, Delta);
172            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
173            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
174            Assert.IsTrue(interpreter.NameStack.IsEmpty);
175            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
176        }
177
178        [TestMethod]
179        public void Example12()
180        {
181            var program = PushGPInterpreter.Encode("( INTEGER.= EXEC.IF FLOAT.* FLOAT./ )");
182
183            this.interpreter.IntegerStack.Push(1, 1);
184            this.interpreter.FloatStack.Push(2.1, 0.7);
185
186            this.interpreter.Interpret(program);
187
188            Assert.IsTrue(interpreter.IntegerStack.IsEmpty);
189            Assert.AreEqual(1.47, interpreter.FloatStack.Top, Delta);
190            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
191            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
192            Assert.IsTrue(interpreter.NameStack.IsEmpty);
193            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
194
195            this.interpreter.Clear();
196            this.interpreter.IntegerStack.Push(1, 2);
197            this.interpreter.FloatStack.Push(2.1, 0.7);
198            this.interpreter.Interpret(program);
199
200            Assert.IsTrue(interpreter.IntegerStack.IsEmpty);
201            Assert.AreEqual(3d, interpreter.FloatStack.Top, Delta);
202            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
203            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
204            Assert.IsTrue(interpreter.NameStack.IsEmpty);
205            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
206        }
207
208        [TestMethod]
209        public void Example13()
210        {
211            this.interpreter.IntegerStack.Push(5);
212            this.interpreter.Interpret("( EXEC.Y ( ( FALSE 2 INTEGER.* ) EXEC.IF ( ) EXEC.POP ) )");
213
214            Assert.AreEqual(10, interpreter.IntegerStack.Top);
215            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
216            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
217            Assert.IsTrue(interpreter.NameStack.IsEmpty);
218            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
219            Assert.IsTrue(interpreter.FloatStack.IsEmpty);
220        }
221
222        [TestMethod]
223        public void Example14()
224        {
225            this.interpreter.IntegerStack.Push(5);
226            this.interpreter.Interpret("( EXEC.Y ( ( 2 INTEGER.* INTEGER.DUP 1000 INTEGER.< ) EXEC.IF ( ) EXEC.POP ) )");
227
228            Assert.AreEqual(1280, interpreter.IntegerStack.Top);
229            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
230            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
231            Assert.IsTrue(interpreter.NameStack.IsEmpty);
232            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
233            Assert.IsTrue(interpreter.FloatStack.IsEmpty);
234        }
235
236        [TestMethod]
237        public void Example15()
238        {
239            this.interpreter.IntegerStack.Push(10);
240            this.interpreter.FloatStack.Push(2);
241            this.interpreter.Interpret(@"( ARG FLOAT.DEFINE
242                                           EXEC.Y (
243                                           ARG FLOAT.*
244                                           1 INTEGER.-
245                                           INTEGER.DUP 1 INTEGER.>
246                                           EXEC.IF ( ) EXEC.POP
247                                           )
248                                         )");
249
250            Assert.AreEqual(1024, interpreter.FloatStack.Top, Delta);
251            Assert.AreEqual(1, interpreter.IntegerStack.Top);
252            Assert.IsTrue(interpreter.CodeStack.IsEmpty);
253            Assert.IsTrue(interpreter.ExecStack.IsEmpty);
254            Assert.IsTrue(interpreter.NameStack.IsEmpty);
255            Assert.IsTrue(interpreter.BooleanStack.IsEmpty);
256        }
257    }
258}
Note: See TracBrowser for help on using the repository browser.