Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14909 was 14908, checked in by pkimmesw, 8 years ago

#2665 Removed "this" qualifier

File size: 10.2 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 IPushStack<T> Stack { get; }
13
14    protected abstract T[] GetValues(int count);
15
16    protected virtual T[] Get2Different() {
17      return GetValues(2);
18    }
19
20    protected virtual void Test(Expression expression) {
21      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 = GetValues(1);
32      var merged = new[] { values[0], values[0] };
33
34      Stack.Push(merged);
35
36      var isBooleanStack = interpreter.BooleanStack.Count == 2;
37
38      Test(ExpressionTable.GetStatelessExpression(TypeName + ".="));
39
40      if (isBooleanStack) Assert.AreEqual(1, Stack.Count);
41      else Assert.AreEqual(0, Stack.Count);
42
43      Assert.IsTrue(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 = Get2Different();
52      Stack.Push(values);
53      var isBooleanStack = interpreter.BooleanStack.Count == 2;
54
55      Test(ExpressionTable.GetStatelessExpression(TypeName + ".="));
56
57      if (isBooleanStack) Assert.AreEqual(1, Stack.Count);
58      else Assert.AreEqual(0, Stack.Count);
59
60      Assert.IsFalse(interpreter.BooleanStack.Top);
61    }
62
63    [TestMethod]
64    [TestProperty("Time", "Short")]
65    [TestCategory("ExpressionTest")]
66    [TestCategory("CommonExpressionTest")]
67    public virtual void TestEqualsWithInsufficientArguments() {
68      TestWithInsufficientArguments("=", 1);
69    }
70
71    [TestMethod]
72    [TestProperty("Time", "Short")]
73    [TestCategory("ExpressionTest")]
74    [TestCategory("CommonExpressionTest")]
75    public virtual void TestDuplicate() {
76      var values = GetValues(1);
77      Stack.Push(values);
78
79      Test(ExpressionTable.GetStatelessExpression(TypeName + ".DUP"));
80
81      Assert.AreEqual(Stack.Count, 2);
82      Assert.AreEqual(Stack[0], values[0]);
83      Assert.AreEqual(Stack[1], values[0]);
84      CheckOtherStacksAreEmpty();
85    }
86
87    [TestMethod]
88    [TestProperty("Time", "Short")]
89    [TestCategory("ExpressionTest")]
90    [TestCategory("CommonExpressionTest")]
91    public virtual void TestPop() {
92      var values = GetValues(2);
93      Stack.Push(values);
94
95      Test(ExpressionTable.GetStatelessExpression(TypeName + ".POP"));
96
97      CheckOtherStacksAreEmpty();
98    }
99
100    [TestMethod]
101    [TestProperty("Time", "Short")]
102    [TestCategory("ExpressionTest")]
103    [TestCategory("CommonExpressionTest")]
104    public virtual void TestSwap() {
105      var values = GetValues(3);
106      Stack.Push(values);
107
108      Test(ExpressionTable.GetStatelessExpression(TypeName + ".SWAP"));
109
110      Assert.AreEqual(values[0], Stack[2]);
111      Assert.AreEqual(values[1], Stack[0]);
112      Assert.AreEqual(values[2], Stack[1]);
113
114      CheckOtherStacksAreEmpty();
115    }
116
117    [TestMethod]
118    [TestProperty("Time", "Short")]
119    [TestCategory("ExpressionTest")]
120    [TestCategory("CommonExpressionTest")]
121    public virtual void TestSwapWithInsufficientArguments() {
122      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 = GetValues(4);
131      Stack.Push(values);
132
133      Test(ExpressionTable.GetStatelessExpression(TypeName + ".ROT"));
134
135      Assert.AreEqual(values[0], Stack[3]);
136      Assert.AreEqual(values[3], Stack[2]);
137      Assert.AreEqual(values[1], Stack[1]);
138      Assert.AreEqual(values[2], Stack[0]);
139
140      CheckOtherStacksAreEmpty();
141    }
142
143    [TestMethod]
144    [TestProperty("Time", "Short")]
145    [TestCategory("ExpressionTest")]
146    [TestCategory("CommonExpressionTest")]
147    public virtual void TestRotateWithInsufficientArguments() {
148      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 = GetValues(3);
157      Stack.Push(values);
158
159      interpreter.IntegerStack.Push(1);
160      Test(ExpressionTable.GetStatelessExpression(TypeName + ".SHOVE"));
161
162      Assert.AreEqual(values[0], Stack[2]);
163      Assert.AreEqual(values[1], Stack[0]);
164      Assert.AreEqual(values[2], Stack[1]);
165
166      CheckOtherStacksAreEmpty();
167    }
168
169    [TestMethod]
170    [TestProperty("Time", "Short")]
171    [TestCategory("ExpressionTest")]
172    [TestCategory("CommonExpressionTest")]
173    public virtual void TestShoveWithInsufficientArguments() {
174      TestWithInsufficientArguments("SHOVE", 1);
175    }
176
177    [TestMethod]
178    [TestProperty("Time", "Short")]
179    [TestCategory("ExpressionTest")]
180    [TestCategory("CommonExpressionTest")]
181    public virtual void TestShoveWithNegativeIndex() {
182      var values = GetValues(3);
183      Stack.Push(values);
184
185      interpreter.IntegerStack.Push(-1);
186      Test(ExpressionTable.GetStatelessExpression(TypeName + ".SHOVE"));
187
188      Assert.AreEqual(values[0], Stack[2]);
189      Assert.AreEqual(values[1], Stack[0]);
190      Assert.AreEqual(values[2], Stack[1]);
191
192      CheckOtherStacksAreEmpty();
193    }
194
195    [TestMethod]
196    [TestProperty("Time", "Short")]
197    [TestCategory("ExpressionTest")]
198    [TestCategory("CommonExpressionTest")]
199    public virtual void TestYank() {
200      var values = GetValues(3);
201      Stack.Push(values);
202
203      interpreter.IntegerStack.Push(1);
204      Test(ExpressionTable.GetStatelessExpression(TypeName + ".YANK"));
205
206      Assert.AreEqual(values[0], Stack[2]);
207      Assert.AreEqual(values[1], Stack[0]);
208      Assert.AreEqual(values[2], Stack[1]);
209
210      CheckOtherStacksAreEmpty();
211    }
212
213    [TestMethod]
214    [TestProperty("Time", "Short")]
215    [TestCategory("ExpressionTest")]
216    [TestCategory("CommonExpressionTest")]
217    public virtual void TestYankWithInsufficientArguments() {
218      TestWithInsufficientArguments("YANK", 1);
219    }
220
221    [TestMethod]
222    [TestProperty("Time", "Short")]
223    [TestCategory("ExpressionTest")]
224    [TestCategory("CommonExpressionTest")]
225    public virtual void TestYankWithNegativeIndex() {
226      var values = GetValues(3);
227      Stack.Push(values);
228
229      interpreter.IntegerStack.Push(-1);
230      Test(ExpressionTable.GetStatelessExpression(TypeName + ".YANK"));
231
232      Assert.AreEqual(values[0], Stack[2]);
233      Assert.AreEqual(values[1], Stack[0]);
234      Assert.AreEqual(values[2], Stack[1]);
235
236      CheckOtherStacksAreEmpty();
237    }
238
239    [TestMethod]
240    [TestProperty("Time", "Short")]
241    [TestCategory("ExpressionTest")]
242    [TestCategory("CommonExpressionTest")]
243    public virtual void TestYankDuplicate() {
244      var values = GetValues(3);
245      Stack.Push(values);
246
247      interpreter.IntegerStack.Push(1);
248      Test(ExpressionTable.GetStatelessExpression(TypeName + ".YANKDUP"));
249
250      Assert.AreEqual(values[0], Stack[3]);
251      Assert.AreEqual(values[1], Stack[2]);
252      Assert.AreEqual(values[2], Stack[1]);
253      Assert.AreEqual(values[1], Stack[0]);
254
255      CheckOtherStacksAreEmpty();
256    }
257
258    [TestMethod]
259    [TestProperty("Time", "Short")]
260    [TestCategory("ExpressionTest")]
261    [TestCategory("CommonExpressionTest")]
262    public virtual void TestYankDuplicateWithInsufficientArguments() {
263      TestWithInsufficientArguments("YANKDUP", 1);
264    }
265
266    [TestMethod]
267    [TestProperty("Time", "Short")]
268    [TestCategory("ExpressionTest")]
269    [TestCategory("CommonExpressionTest")]
270    public virtual void TestYankDuplicateWithNegativeIndex() {
271      var values = GetValues(3);
272      Stack.Push(values);
273
274      interpreter.IntegerStack.Push(-1);
275      Test(ExpressionTable.GetStatelessExpression(TypeName + ".YANKDUP"));
276
277      Assert.AreEqual(values[0], Stack[3]);
278      Assert.AreEqual(values[1], Stack[2]);
279      Assert.AreEqual(values[2], Stack[1]);
280      Assert.AreEqual(values[1], Stack[0]);
281
282      CheckOtherStacksAreEmpty();
283    }
284
285    [TestMethod]
286    [TestProperty("Time", "Short")]
287    [TestCategory("ExpressionTest")]
288    [TestCategory("CommonExpressionTest")]
289    public virtual void TestStackdpeth() {
290      var values = GetValues(3);
291      Stack.Push(values);
292
293      Test(ExpressionTable.GetStatelessExpression(TypeName + ".STACKDEPTH"));
294
295      // if stack is integer stack
296      if (Stack.Count != values.Length) {
297        Assert.AreEqual(4, Stack.Count);
298        CheckOtherStacksAreEmpty();
299      } else {
300        Assert.AreEqual(3, Stack.Count);
301        Assert.AreEqual(values.Length, interpreter.IntegerStack.Top);
302      }
303    }
304
305    protected void TestWithInsufficientArguments(string instructionName, int argCount = 0) {
306      for (var i = 0; i < argCount + 1; i++) {
307        var values = GetValues(i);
308        Stack.Push(values);
309
310        var fullInstructionName = string.Format(FullInstructionNameFormat, TypeName, instructionName);
311        Test(ExpressionTable.GetStatelessExpression(fullInstructionName));
312
313        for (var j = 0; j < i; j++) Assert.AreEqual(values[j], Stack.ReverseElementAt(j));
314
315        CheckOtherStacksAreEmpty();
316        interpreter.Clear();
317      }
318    }
319
320    protected void TestWithNegativeIndex(string instructionName) {
321      var values = GetValues(3);
322      Stack.Push(values);
323
324      interpreter.IntegerStack.Push(-1);
325
326      var fullInstructionname = string.Format(FullInstructionNameFormat, TypeName, instructionName);
327      Test(ExpressionTable.GetStatelessExpression(fullInstructionname));
328
329      Assert.AreEqual(values[0], Stack[2]);
330      Assert.AreEqual(values[1], Stack[0]);
331      Assert.AreEqual(values[2], Stack[1]);
332    }
333  }
334}
Note: See TracBrowser for help on using the repository browser.