[14727] | 1 | namespace HeuristicLab.Tests.Interpreter.Expressions {
|
---|
| 2 | using HeuristicLab.Problems.ProgramSynthesis.Push.Parser;
|
---|
| 3 |
|
---|
| 4 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 5 |
|
---|
| 6 | [TestClass]
|
---|
| 7 | public class MixedExpressionTests : ExpressionTest {
|
---|
| 8 | private const double Delta = 0.00000001;
|
---|
| 9 |
|
---|
| 10 | [TestMethod]
|
---|
| 11 | [TestProperty("Time", "Short")]
|
---|
| 12 | [TestCategory("ExampleTest")]
|
---|
| 13 | public void Example1() {
|
---|
| 14 | this.interpreter.Run("( 2 3 INTEGER.* 4.1 5.2 FLOAT.+ TRUE FALSE BOOLEAN.OR )");
|
---|
| 15 |
|
---|
| 16 | Assert.AreEqual(6, this.interpreter.IntegerStack.Top);
|
---|
| 17 | Assert.AreEqual(9.3, this.interpreter.FloatStack.Top);
|
---|
| 18 | Assert.IsTrue(this.interpreter.BooleanStack.Top);
|
---|
| 19 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 20 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 21 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | [TestMethod]
|
---|
| 25 | [TestProperty("Time", "Short")]
|
---|
| 26 | [TestCategory("ExampleTest")]
|
---|
| 27 | public void Example2() {
|
---|
| 28 | this.interpreter.Run("( 5 1.23 INTEGER.+ ( 4 ) INTEGER.- 5.67 FLOAT.* )");
|
---|
| 29 |
|
---|
| 30 | Assert.AreEqual(1, this.interpreter.IntegerStack.Top);
|
---|
| 31 | Assert.AreEqual(6.9741, this.interpreter.FloatStack.Top);
|
---|
| 32 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 33 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 34 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 35 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | [TestMethod]
|
---|
| 39 | [TestProperty("Time", "Short")]
|
---|
| 40 | [TestCategory("ExampleTest")]
|
---|
| 41 | public void Example3() {
|
---|
| 42 | this.interpreter.Run("( 5 INTEGER.DUP INTEGER.+ )");
|
---|
| 43 |
|
---|
| 44 | Assert.AreEqual(10, this.interpreter.IntegerStack.Top);
|
---|
| 45 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 46 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 47 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 48 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 49 | Assert.IsTrue(this.interpreter.FloatStack.IsEmpty);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | [TestMethod]
|
---|
| 53 | [TestProperty("Time", "Short")]
|
---|
| 54 | [TestCategory("ExampleTest")]
|
---|
| 55 | public void Example4() {
|
---|
| 56 | this.interpreter.Run("( 5 CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DO )");
|
---|
| 57 |
|
---|
| 58 | Assert.AreEqual(10, this.interpreter.IntegerStack.Top);
|
---|
| 59 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 60 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 61 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 62 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 63 | Assert.IsTrue(this.interpreter.FloatStack.IsEmpty);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | [TestMethod]
|
---|
| 67 | [TestProperty("Time", "Short")]
|
---|
| 68 | [TestCategory("ExampleTest")]
|
---|
| 69 | public void Example5() {
|
---|
| 70 | this.interpreter.Run("( DOUBLE CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DEFINE 5 DOUBLE )");
|
---|
| 71 |
|
---|
| 72 | Assert.AreEqual(10, this.interpreter.IntegerStack.Top);
|
---|
| 73 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 74 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 75 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 76 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 77 | Assert.IsTrue(this.interpreter.FloatStack.IsEmpty);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | [TestMethod]
|
---|
| 81 | [TestProperty("Time", "Short")]
|
---|
| 82 | [TestCategory("ExampleTest")]
|
---|
| 83 | public void Example6() {
|
---|
| 84 | this.interpreter.Run("( CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) DOUBLE CODE.DEFINE 5 DOUBLE )");
|
---|
| 85 |
|
---|
| 86 | Assert.AreEqual(10, this.interpreter.IntegerStack.Top);
|
---|
| 87 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 88 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 89 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 90 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 91 | Assert.IsTrue(this.interpreter.FloatStack.IsEmpty);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | [TestMethod]
|
---|
| 95 | [TestProperty("Time", "Short")]
|
---|
| 96 | [TestCategory("ExampleTest")]
|
---|
| 97 | public void Example7() {
|
---|
| 98 | this.interpreter.Run("( DOUBLE EXEC.DEFINE ( INTEGER.DUP INTEGER.+ ) 5 DOUBLE )");
|
---|
| 99 |
|
---|
| 100 | Assert.AreEqual(10, this.interpreter.IntegerStack.Top);
|
---|
| 101 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 102 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 103 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 104 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 105 | Assert.IsTrue(this.interpreter.FloatStack.IsEmpty);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | [TestMethod]
|
---|
| 109 | [TestProperty("Time", "Short")]
|
---|
| 110 | [TestCategory("ExampleTest")]
|
---|
| 111 | public void Example8() {
|
---|
| 112 | this.configuration.TopLevelPushCode = true;
|
---|
| 113 |
|
---|
| 114 | this.interpreter.IntegerStack.Push(5);
|
---|
| 115 | this.interpreter.Run(@"( CODE.QUOTE ( INTEGER.POP 1 )
|
---|
| 116 | CODE.QUOTE ( CODE.DUP INTEGER.DUP 1 INTEGER.- CODE.DO INTEGER.* )
|
---|
| 117 | INTEGER.DUP 2 INTEGER.< CODE.IF )");
|
---|
| 118 |
|
---|
| 119 | Assert.AreEqual(120, this.interpreter.IntegerStack.Top);
|
---|
| 120 | Assert.IsTrue(this.interpreter.CodeStack.Count == 1);
|
---|
| 121 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 122 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 123 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 124 | Assert.IsTrue(this.interpreter.FloatStack.IsEmpty);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | [TestMethod]
|
---|
| 128 | [TestProperty("Time", "Short")]
|
---|
| 129 | [TestCategory("ExampleTest")]
|
---|
| 130 | public void Example9() {
|
---|
| 131 | this.interpreter.IntegerStack.Push(5);
|
---|
| 132 | this.interpreter.Run("( 1 INTEGER.MAX 1 EXEC.DO*RANGE INTEGER.* )");
|
---|
| 133 |
|
---|
| 134 | Assert.AreEqual(120, this.interpreter.IntegerStack.Top);
|
---|
| 135 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 136 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 137 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 138 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 139 | Assert.IsTrue(this.interpreter.FloatStack.IsEmpty);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | [TestMethod]
|
---|
| 143 | [TestProperty("Time", "Short")]
|
---|
| 144 | [TestCategory("ExampleTest")]
|
---|
| 145 | public void Example10() {
|
---|
| 146 | this.interpreter.IntegerStack.Push(5);
|
---|
| 147 | this.interpreter.Run("( 1 INTEGER.MAX 1 EXEC.DO*RANGE ( 2 INTEGER.* ) )");
|
---|
| 148 |
|
---|
| 149 | Assert.AreEqual(2, this.interpreter.IntegerStack.Top);
|
---|
| 150 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 151 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 152 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 153 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 154 | Assert.IsTrue(this.interpreter.FloatStack.IsEmpty);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | [TestMethod]
|
---|
| 158 | [TestProperty("Time", "Short")]
|
---|
| 159 | [TestCategory("ExampleTest")]
|
---|
| 160 | public void Example11() {
|
---|
[14733] | 161 | var program = PushParser.Parse("( INTEGER.= CODE.QUOTE FLOAT.* CODE.QUOTE FLOAT./ CODE.IF )");
|
---|
[14727] | 162 |
|
---|
| 163 | this.interpreter.IntegerStack.Push(1, 1);
|
---|
| 164 | this.interpreter.FloatStack.Push(2.1, 0.7);
|
---|
| 165 |
|
---|
| 166 | this.interpreter.Run(program);
|
---|
| 167 |
|
---|
| 168 | Assert.IsTrue(this.interpreter.IntegerStack.IsEmpty);
|
---|
| 169 | Assert.AreEqual(1.47, this.interpreter.FloatStack.Top, Delta);
|
---|
| 170 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 171 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 172 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 173 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 174 |
|
---|
| 175 | this.interpreter.Clear();
|
---|
| 176 | this.interpreter.IntegerStack.Push(1, 2);
|
---|
| 177 | this.interpreter.FloatStack.Push(2.1, 0.7);
|
---|
| 178 | this.interpreter.Run(program);
|
---|
| 179 |
|
---|
| 180 | Assert.IsTrue(this.interpreter.IntegerStack.IsEmpty);
|
---|
| 181 | Assert.AreEqual(3d, this.interpreter.FloatStack.Top, Delta);
|
---|
| 182 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 183 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 184 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 185 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | [TestMethod]
|
---|
| 189 | [TestProperty("Time", "Short")]
|
---|
| 190 | [TestCategory("ExampleTest")]
|
---|
| 191 | public void Example12() {
|
---|
[14733] | 192 | var program = PushParser.Parse("( INTEGER.= EXEC.IF FLOAT.* FLOAT./ )");
|
---|
[14727] | 193 |
|
---|
| 194 | this.interpreter.IntegerStack.Push(1, 1);
|
---|
| 195 | this.interpreter.FloatStack.Push(2.1, 0.7);
|
---|
| 196 |
|
---|
| 197 | this.interpreter.Run(program);
|
---|
| 198 |
|
---|
| 199 | Assert.IsTrue(this.interpreter.IntegerStack.IsEmpty);
|
---|
| 200 | Assert.AreEqual(1.47, this.interpreter.FloatStack.Top, Delta);
|
---|
| 201 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 202 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 203 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 204 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 205 |
|
---|
| 206 | this.interpreter.Clear();
|
---|
| 207 | this.interpreter.IntegerStack.Push(1, 2);
|
---|
| 208 | this.interpreter.FloatStack.Push(2.1, 0.7);
|
---|
| 209 | this.interpreter.Run(program);
|
---|
| 210 |
|
---|
| 211 | Assert.IsTrue(this.interpreter.IntegerStack.IsEmpty);
|
---|
| 212 | Assert.AreEqual(3d, this.interpreter.FloatStack.Top, Delta);
|
---|
| 213 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 214 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 215 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 216 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | [TestMethod]
|
---|
| 220 | [TestProperty("Time", "Short")]
|
---|
| 221 | [TestCategory("ExampleTest")]
|
---|
| 222 | public void Example13() {
|
---|
| 223 | this.interpreter.IntegerStack.Push(5);
|
---|
| 224 | this.interpreter.Run("( EXEC.Y ( ( FALSE 2 INTEGER.* ) EXEC.IF ( ) EXEC.POP ) )");
|
---|
| 225 |
|
---|
| 226 | Assert.AreEqual(10, this.interpreter.IntegerStack.Top);
|
---|
| 227 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 228 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 229 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 230 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 231 | Assert.IsTrue(this.interpreter.FloatStack.IsEmpty);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | [TestMethod]
|
---|
| 235 | [TestProperty("Time", "Short")]
|
---|
| 236 | [TestCategory("ExampleTest")]
|
---|
| 237 | public void Example14() {
|
---|
| 238 | this.interpreter.IntegerStack.Push(5);
|
---|
| 239 | this.interpreter.Run("( EXEC.Y ( ( 2 INTEGER.* INTEGER.DUP 1000 INTEGER.< ) EXEC.IF ( ) EXEC.POP ) )");
|
---|
| 240 |
|
---|
| 241 | Assert.AreEqual(1280, this.interpreter.IntegerStack.Top);
|
---|
| 242 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 243 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 244 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 245 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 246 | Assert.IsTrue(this.interpreter.FloatStack.IsEmpty);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | [TestMethod]
|
---|
| 250 | [TestProperty("Time", "Short")]
|
---|
| 251 | [TestCategory("ExampleTest")]
|
---|
| 252 | public void Example15() {
|
---|
| 253 | this.interpreter.IntegerStack.Push(10);
|
---|
| 254 | this.interpreter.FloatStack.Push(2);
|
---|
| 255 | this.interpreter.Run(@"( ARG FLOAT.DEFINE
|
---|
| 256 | EXEC.Y (
|
---|
| 257 | ARG FLOAT.*
|
---|
| 258 | 1 INTEGER.-
|
---|
| 259 | INTEGER.DUP 1 INTEGER.>
|
---|
| 260 | EXEC.IF ( ) EXEC.POP
|
---|
| 261 | )
|
---|
| 262 | )");
|
---|
| 263 |
|
---|
| 264 | Assert.AreEqual(1024, this.interpreter.FloatStack.Top, Delta);
|
---|
| 265 | Assert.AreEqual(1, this.interpreter.IntegerStack.Top);
|
---|
| 266 | Assert.IsTrue(this.interpreter.CodeStack.IsEmpty);
|
---|
| 267 | Assert.IsTrue(this.interpreter.ExecStack.IsEmpty);
|
---|
| 268 | Assert.IsTrue(this.interpreter.NameStack.IsEmpty);
|
---|
| 269 | Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | [TestMethod]
|
---|
| 273 | [TestProperty("Time", "Short")]
|
---|
| 274 | [TestCategory("ExampleTest")]
|
---|
| 275 | public void Example16() {
|
---|
[14733] | 276 | var list = PushParser.Parse("( A B C )");
|
---|
[14727] | 277 |
|
---|
| 278 | this.interpreter.CodeStack.Push(list);
|
---|
| 279 | this.interpreter.Run("( CODE.DUP 0 CODE.INSERT )");
|
---|
| 280 |
|
---|
| 281 | var id = this.interpreter.CodeStack.Top.GetHashCode();
|
---|
| 282 |
|
---|
| 283 | if (id == default(int)) Assert.Fail();
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 | } |
---|