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