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