Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Tests/Interpreter/Expressions/ExecExpressionTests.cs @ 17432

Last change on this file since 17432 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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