Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Tests/Interpreter/Expressions/CommonTests.cs @ 16958

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

#2665 Started Plush Encoding, Added Zero Error Individual Count Analyzer

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