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