Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Tests/Interpreter/Expressions/ExampleTests.cs @ 15273

Last change on this file since 15273 was 15273, checked in by pkimmesw, 7 years ago

#2665 Started Plush Encoding, Added Zero Error Individual Count Analyzer

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