[14727] | 1 | namespace HeuristicLab.Tests.Interpreter.Expressions {
|
---|
| 2 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
| 3 | using HeuristicLab.Problems.ProgramSynthesis.Push.Parser;
|
---|
| 4 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
| 5 |
|
---|
| 6 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 7 |
|
---|
| 8 | [TestClass]
|
---|
| 9 | public class ExecExpressionTests : CommonTests<Expression> {
|
---|
| 10 | protected override string TypeName
|
---|
| 11 | {
|
---|
| 12 | get
|
---|
| 13 | {
|
---|
| 14 | return "EXEC";
|
---|
| 15 | }
|
---|
| 16 | }
|
---|
| 17 |
|
---|
[14834] | 18 | protected override IPushStack<Expression> Stack
|
---|
[14727] | 19 | {
|
---|
| 20 | get
|
---|
| 21 | {
|
---|
| 22 | return this.interpreter.ExecStack;
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | protected override void Test(Expression expression) {
|
---|
[14834] | 27 | this.interpreter.Run(expression, true);
|
---|
| 28 | this.interpreter.Step();
|
---|
[14727] | 29 | }
|
---|
| 30 |
|
---|
| 31 | [TestMethod]
|
---|
| 32 | [TestProperty("Time", "Short")]
|
---|
| 33 | [TestCategory("ExpressionTest")]
|
---|
| 34 | [TestCategory("ExecExpressionTest")]
|
---|
| 35 | public void TestIfTrue() {
|
---|
| 36 | this.interpreter.BooleanStack.Push(true);
|
---|
| 37 | this.interpreter.Run("( EXEC.IF WAHR FALSCH )");
|
---|
| 38 |
|
---|
| 39 | Assert.AreEqual("WAHR", this.interpreter.NameStack.Top);
|
---|
| 40 | this.TestStackCounts(nameStack: 1);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | [TestMethod]
|
---|
| 44 | [TestProperty("Time", "Short")]
|
---|
| 45 | [TestCategory("ExpressionTest")]
|
---|
| 46 | [TestCategory("ExecExpressionTest")]
|
---|
| 47 | public void TestIfFalse() {
|
---|
| 48 | this.interpreter.BooleanStack.Push(false);
|
---|
| 49 | this.interpreter.Run("( EXEC.IF WAHR FALSCH )");
|
---|
| 50 |
|
---|
| 51 | Assert.AreEqual("FALSCH", this.interpreter.NameStack.Top);
|
---|
| 52 | this.TestStackCounts(nameStack: 1);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | [TestMethod]
|
---|
| 56 | [TestProperty("Time", "Short")]
|
---|
| 57 | [TestCategory("ExpressionTest")]
|
---|
| 58 | [TestCategory("ExecExpressionTest")]
|
---|
| 59 | public void TestK() {
|
---|
[14733] | 60 | var first = PushParser.Parse("A");
|
---|
| 61 | var second = PushParser.Parse("B");
|
---|
| 62 | var third = PushParser.Parse("C");
|
---|
[14727] | 63 |
|
---|
[14834] | 64 | interpreter.ExecStack.Push(third, second, first);
|
---|
| 65 | Test(new ExecKExpression());
|
---|
[14727] | 66 |
|
---|
[14834] | 67 | Assert.AreEqual(first, interpreter.ExecStack.Top);
|
---|
| 68 | Assert.AreEqual(third, interpreter.ExecStack.Bottom);
|
---|
| 69 | TestStackCounts(2);
|
---|
[14727] | 70 | }
|
---|
| 71 |
|
---|
| 72 | [TestMethod]
|
---|
| 73 | [TestProperty("Time", "Short")]
|
---|
| 74 | [TestCategory("ExpressionTest")]
|
---|
| 75 | [TestCategory("ExecExpressionTest")]
|
---|
| 76 | public void TestS() {
|
---|
[14733] | 77 | var first = PushParser.Parse("A");
|
---|
| 78 | var second = PushParser.Parse("B");
|
---|
| 79 | var third = PushParser.Parse("C");
|
---|
| 80 | var result = PushParser.Parse("( B C )");
|
---|
[14727] | 81 |
|
---|
| 82 | this.interpreter.ExecStack.Push(third, second, first);
|
---|
| 83 | this.Test(new ExecSExpression());
|
---|
| 84 |
|
---|
[14875] | 85 | Assert.AreEqual(result, this.interpreter.ExecStack[2]);
|
---|
| 86 | Assert.AreEqual(third, this.interpreter.ExecStack[1]);
|
---|
[14727] | 87 | Assert.AreEqual(first, this.interpreter.ExecStack.Top);
|
---|
| 88 | this.TestStackCounts(3);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | [TestMethod]
|
---|
| 92 | [TestProperty("Time", "Short")]
|
---|
| 93 | [TestCategory("ExpressionTest")]
|
---|
| 94 | [TestCategory("ExecExpressionTest")]
|
---|
| 95 | public void TestY() {
|
---|
[14733] | 96 | var first = PushParser.Parse("A");
|
---|
| 97 | var result = PushParser.Parse("( EXEC.Y A )");
|
---|
[14727] | 98 |
|
---|
| 99 | this.interpreter.ExecStack.Push(first);
|
---|
| 100 | this.Test(new ExecYExpression());
|
---|
| 101 |
|
---|
| 102 | Assert.AreEqual(first, this.interpreter.ExecStack.Top);
|
---|
[14875] | 103 | Assert.AreEqual(result, this.interpreter.ExecStack[1]);
|
---|
[14727] | 104 | this.TestStackCounts(2);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | [TestMethod]
|
---|
| 108 | [TestProperty("Time", "Short")]
|
---|
| 109 | [TestCategory("ExpressionTest")]
|
---|
| 110 | [TestCategory("ExecExpressionTest")]
|
---|
| 111 | public void TestNestedDoRange() {
|
---|
| 112 | this.interpreter.Run(
|
---|
| 113 | "( 0 2 EXEC.DO*RANGE ( 1 INTEGER.+ 0 3 EXEC.DO*RANGE ( 1 INTEGER.+ INTEGER.* ) INTEGER.+ )");
|
---|
| 114 |
|
---|
| 115 | Assert.AreEqual(144, this.interpreter.IntegerStack.Top);
|
---|
| 116 | this.TestStackCounts(integerStack: 1);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | [TestMethod]
|
---|
| 120 | [TestProperty("Time", "Short")]
|
---|
| 121 | [TestCategory("ExpressionTest")]
|
---|
| 122 | [TestCategory("ExecExpressionTest")]
|
---|
| 123 | public void TestNestedDoCount() {
|
---|
| 124 | this.interpreter.Run(
|
---|
| 125 | "( 2 EXEC.DO*COUNT ( 1 INTEGER.+ 3 EXEC.DO*COUNT ( 1 INTEGER.+ INTEGER.* ) INTEGER.+ )");
|
---|
| 126 |
|
---|
| 127 | Assert.AreEqual(144, this.interpreter.IntegerStack.Top);
|
---|
| 128 | this.TestStackCounts(integerStack: 1);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | [TestMethod]
|
---|
| 132 | [TestProperty("Time", "Short")]
|
---|
| 133 | [TestCategory("ExpressionTest")]
|
---|
| 134 | [TestCategory("ExecExpressionTest")]
|
---|
| 135 | public void TestNestedDoTimes() {
|
---|
| 136 | this.interpreter.Run("( 3 EXEC.DO*TIMES ( 2 3 EXEC.DO*TIMES ( 2 INTEGER.* ) INTEGER.+ )");
|
---|
| 137 |
|
---|
| 138 | Assert.AreEqual(128, this.interpreter.IntegerStack.Top);
|
---|
| 139 | this.TestStackCounts(integerStack: 1);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | protected override Expression[] GetValues(int count) {
|
---|
| 143 | var values = new Expression[count];
|
---|
| 144 |
|
---|
| 145 | for (var i = 0; i < count; i++) values[i] = new CodeNoopExpression();
|
---|
| 146 |
|
---|
| 147 | return values;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | protected override Expression[] Get2Different() {
|
---|
| 151 | return new Expression[] { new CodeNoopExpression(), new CodeNullExpression() };
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | protected override void CheckOtherStacksAreEmpty() {
|
---|
| 155 | this.TestStackCounts(null);
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | } |
---|