Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Tests/Interpreter/Expressions/ExecExpressionTests.cs @ 14733

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

#2665 Storable problem data, Renamings due to typos, Removed GP from class names

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