Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Tests/Interpreter/Expressions/ExecExpressionTests.cs @ 17714

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

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

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