Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Tests/Interpreter/Expressions/CommonTests.cs @ 14727

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

#2665 PushGP HL Integration, Views, Parameters

File size: 9.7 KB
Line 
1namespace HeuristicLab.Tests.Interpreter.Expressions {
2  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
3  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
4
5  using Microsoft.VisualStudio.TestTools.UnitTesting;
6
7  public abstract class CommonTests<T> : ExpressionTest {
8    protected const string FullInstructionNameFormat = "{0}.{1}";
9
10    protected abstract string TypeName { get; }
11
12    protected abstract IStack<T> Stack { get; }
13
14    protected abstract T[] GetValues(int count);
15
16    protected virtual T[] Get2Different() {
17      return this.GetValues(2);
18    }
19
20    protected virtual void Test(Expression expression) {
21      this.interpreter.Run(expression);
22    }
23
24    protected abstract void CheckOtherStacksAreEmpty();
25
26    [TestMethod]
27    [TestProperty("Time", "Short")]
28    [TestCategory("ExpressionTest")]
29    [TestCategory("CommonExpressionTest")]
30    public virtual void TestEqualsTrue() {
31      var values = this.GetValues(1);
32      var merged = new[] { values[0], values[0] };
33
34      this.Stack.Push(merged);
35
36      var isBooleanStack = this.interpreter.BooleanStack.Count == 2;
37
38      this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".="));
39
40      if (isBooleanStack) Assert.AreEqual(1, this.Stack.Count);
41      else Assert.AreEqual(0, this.Stack.Count);
42
43      Assert.IsTrue(this.interpreter.BooleanStack.Top);
44    }
45
46    [TestMethod]
47    [TestProperty("Time", "Short")]
48    [TestCategory("ExpressionTest")]
49    [TestCategory("CommonExpressionTest")]
50    public virtual void TestEqualsFalse() {
51      var values = this.Get2Different();
52      this.Stack.Push(values);
53      var isBooleanStack = this.interpreter.BooleanStack.Count == 2;
54
55      this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".="));
56
57      if (isBooleanStack) Assert.AreEqual(1, this.Stack.Count);
58      else Assert.AreEqual(0, this.Stack.Count);
59
60      Assert.IsFalse(this.interpreter.BooleanStack.Top);
61    }
62
63    [TestMethod]
64    [TestProperty("Time", "Short")]
65    [TestCategory("ExpressionTest")]
66    [TestCategory("CommonExpressionTest")]
67    public virtual void TestEqualsWithInsufficientArguments() {
68      this.TestWithInsufficientArguments("=", 1);
69    }
70
71    [TestMethod]
72    [TestProperty("Time", "Short")]
73    [TestCategory("ExpressionTest")]
74    [TestCategory("CommonExpressionTest")]
75    public virtual void TestDuplicate() {
76      var values = this.GetValues(1);
77      this.Stack.Push(values);
78
79      this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".DUP"));
80
81      Assert.AreEqual(this.Stack.Count, 2);
82      Assert.AreEqual(this.Stack[0], values[0]);
83      Assert.AreEqual(this.Stack[1], values[0]);
84      this.CheckOtherStacksAreEmpty();
85    }
86
87    [TestMethod]
88    [TestProperty("Time", "Short")]
89    [TestCategory("ExpressionTest")]
90    [TestCategory("CommonExpressionTest")]
91    public virtual void TestPop() {
92      var values = this.GetValues(2);
93      this.Stack.Push(values);
94
95      this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".POP"));
96
97      this.CheckOtherStacksAreEmpty();
98    }
99
100    [TestMethod]
101    [TestProperty("Time", "Short")]
102    [TestCategory("ExpressionTest")]
103    [TestCategory("CommonExpressionTest")]
104    public virtual void TestSwap() {
105      var values = this.GetValues(3);
106      this.Stack.Push(values);
107
108      this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".SWAP"));
109
110      Assert.AreEqual(values[0], this.Stack[0]);
111      Assert.AreEqual(values[2], this.Stack[1]);
112      Assert.AreEqual(values[1], this.Stack[2]);
113
114      this.CheckOtherStacksAreEmpty();
115    }
116
117    [TestMethod]
118    [TestProperty("Time", "Short")]
119    [TestCategory("ExpressionTest")]
120    [TestCategory("CommonExpressionTest")]
121    public virtual void TestSwapWithInsufficientArguments() {
122      this.TestWithInsufficientArguments("SWAP", 1);
123    }
124
125    [TestMethod]
126    [TestProperty("Time", "Short")]
127    [TestCategory("ExpressionTest")]
128    [TestCategory("CommonExpressionTest")]
129    public virtual void TestRotate() {
130      var values = this.GetValues(4);
131      this.Stack.Push(values);
132
133      this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".ROT"));
134
135      Assert.AreEqual(values[0], this.Stack[0]);
136      Assert.AreEqual(values[3], this.Stack[1]);
137      Assert.AreEqual(values[1], this.Stack[2]);
138      Assert.AreEqual(values[2], this.Stack[3]);
139
140      this.CheckOtherStacksAreEmpty();
141    }
142
143    [TestMethod]
144    [TestProperty("Time", "Short")]
145    [TestCategory("ExpressionTest")]
146    [TestCategory("CommonExpressionTest")]
147    public virtual void TestRotateWithInsufficientArguments() {
148      this.TestWithInsufficientArguments("ROT", 2);
149    }
150
151    [TestMethod]
152    [TestProperty("Time", "Short")]
153    [TestCategory("ExpressionTest")]
154    [TestCategory("CommonExpressionTest")]
155    public virtual void TestShove() {
156      var values = this.GetValues(3);
157      this.Stack.Push(values);
158
159      this.interpreter.IntegerStack.Push(1);
160      this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".SHOVE"));
161
162      Assert.AreEqual(values[0], this.Stack[0]);
163      Assert.AreEqual(values[2], this.Stack[1]);
164      Assert.AreEqual(values[1], this.Stack[2]);
165
166      this.CheckOtherStacksAreEmpty();
167    }
168
169    [TestMethod]
170    [TestProperty("Time", "Short")]
171    [TestCategory("ExpressionTest")]
172    [TestCategory("CommonExpressionTest")]
173    public virtual void TestShoveWithInsufficientArguments() {
174      this.TestWithInsufficientArguments("SHOVE", 1);
175    }
176
177    [TestMethod]
178    [TestProperty("Time", "Short")]
179    [TestCategory("ExpressionTest")]
180    [TestCategory("CommonExpressionTest")]
181    public virtual void TestShoveWithNegativeIndex() {
182      this.TestWithNegativeIndex("SHOVE");
183    }
184
185    [TestMethod]
186    [TestProperty("Time", "Short")]
187    [TestCategory("ExpressionTest")]
188    [TestCategory("CommonExpressionTest")]
189    public virtual void TestYank() {
190      var values = this.GetValues(3);
191      this.Stack.Push(values);
192
193      this.interpreter.IntegerStack.Push(1);
194      this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".YANK"));
195
196      Assert.AreEqual(values[0], this.Stack[0]);
197      Assert.AreEqual(values[2], this.Stack[1]);
198      Assert.AreEqual(values[1], this.Stack[2]);
199
200      this.CheckOtherStacksAreEmpty();
201    }
202
203    [TestMethod]
204    [TestProperty("Time", "Short")]
205    [TestCategory("ExpressionTest")]
206    [TestCategory("CommonExpressionTest")]
207    public virtual void TestYankWithInsufficientArguments() {
208      this.TestWithInsufficientArguments("YANK", 1);
209    }
210
211    [TestMethod]
212    [TestProperty("Time", "Short")]
213    [TestCategory("ExpressionTest")]
214    [TestCategory("CommonExpressionTest")]
215    public virtual void TestYankWithNegativeIndex() {
216      this.TestWithNegativeIndex("YANK");
217    }
218
219    [TestMethod]
220    [TestProperty("Time", "Short")]
221    [TestCategory("ExpressionTest")]
222    [TestCategory("CommonExpressionTest")]
223    public virtual void TestYankDuplicate() {
224      var values = this.GetValues(3);
225      this.Stack.Push(values);
226
227      this.interpreter.IntegerStack.Push(1);
228      this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".YANKDUP"));
229
230      Assert.AreEqual(values[0], this.Stack[0]);
231      Assert.AreEqual(values[1], this.Stack[1]);
232      Assert.AreEqual(values[2], this.Stack[2]);
233      Assert.AreEqual(values[1], this.Stack[3]);
234
235      this.CheckOtherStacksAreEmpty();
236    }
237
238    [TestMethod]
239    [TestProperty("Time", "Short")]
240    [TestCategory("ExpressionTest")]
241    [TestCategory("CommonExpressionTest")]
242    public virtual void TestYankDuplicateWithInsufficientArguments() {
243      this.TestWithInsufficientArguments("YANKDUP", 1);
244    }
245
246    [TestMethod]
247    [TestProperty("Time", "Short")]
248    [TestCategory("ExpressionTest")]
249    [TestCategory("CommonExpressionTest")]
250    public virtual void TestYankDuplicateWithNegativeIndex() {
251      this.TestWithNegativeIndex("YANKDUP");
252    }
253
254    [TestMethod]
255    [TestProperty("Time", "Short")]
256    [TestCategory("ExpressionTest")]
257    [TestCategory("CommonExpressionTest")]
258    public virtual void TestStackdpeth() {
259      var values = this.GetValues(3);
260      this.Stack.Push(values);
261
262      this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".STACKDEPTH"));
263
264      // if stack is integer stack
265      if (this.Stack.Count != values.Length) {
266        Assert.AreEqual(4, this.Stack.Count);
267        this.CheckOtherStacksAreEmpty();
268      } else {
269        Assert.AreEqual(3, this.Stack.Count);
270        Assert.AreEqual(values.Length, this.interpreter.IntegerStack.Top);
271      }
272    }
273
274    protected void TestWithInsufficientArguments(string instructionName, int argCount = 0) {
275      for (var i = 0; i < argCount + 1; i++) {
276        var values = this.GetValues(i);
277        this.Stack.Push(values);
278
279        var fullInstructionname = string.Format(FullInstructionNameFormat, this.TypeName, instructionName);
280        this.Test(ExpressionTable.GetStatelessExpression(fullInstructionname));
281
282        for (var j = 0; j < i; j++) Assert.AreEqual(values[j], this.Stack[j]);
283
284        this.CheckOtherStacksAreEmpty();
285        this.interpreter.Clear();
286      }
287    }
288
289    protected void TestWithNegativeIndex(string instructionName) {
290      var values = this.GetValues(1);
291      this.Stack.Push(values);
292
293      this.interpreter.IntegerStack.Push(-1);
294
295      var fullInstructionname = string.Format(FullInstructionNameFormat, this.TypeName, instructionName);
296      this.Test(ExpressionTable.GetStatelessExpression(fullInstructionname));
297
298      Assert.AreEqual(values[0], this.Stack[0]);
299    }
300  }
301}
Note: See TracBrowser for help on using the repository browser.