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