Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Tests/Interpreter/Expressions/CommonTests.cs @ 18242

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

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

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