Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

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