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