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