Changeset 14908 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Tests/Interpreter/Expressions/ExampleTests.cs
- Timestamp:
- 05/02/17 22:03:01 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Tests/Interpreter/Expressions/ExampleTests.cs
r14733 r14908 12 12 [TestCategory("ExampleTest")] 13 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);14 interpreter.Run("( 2 3 INTEGER.* 4.1 5.2 FLOAT.+ TRUE FALSE BOOLEAN.OR )"); 15 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); 22 22 } 23 23 … … 26 26 [TestCategory("ExampleTest")] 27 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);28 interpreter.Run("( 5 1.23 INTEGER.+ ( 4 ) INTEGER.- 5.67 FLOAT.* )"); 29 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); 36 36 } 37 37 … … 40 40 [TestCategory("ExampleTest")] 41 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);42 interpreter.Run("( 5 INTEGER.DUP INTEGER.+ )"); 43 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); 50 50 } 51 51 … … 54 54 [TestCategory("ExampleTest")] 55 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);56 interpreter.Run("( 5 CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DO )"); 57 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); 64 64 } 65 65 … … 68 68 [TestCategory("ExampleTest")] 69 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);70 interpreter.Run("( DOUBLE CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DEFINE 5 DOUBLE )"); 71 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); 78 78 } 79 79 … … 82 82 [TestCategory("ExampleTest")] 83 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);84 interpreter.Run("( CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) DOUBLE CODE.DEFINE 5 DOUBLE )"); 85 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); 92 92 } 93 93 … … 96 96 [TestCategory("ExampleTest")] 97 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);98 interpreter.Run("( DOUBLE EXEC.DEFINE ( INTEGER.DUP INTEGER.+ ) 5 DOUBLE )"); 99 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); 106 106 } 107 107 … … 110 110 [TestCategory("ExampleTest")] 111 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 )112 configuration.TopLevelPushCode = true; 113 114 interpreter.IntegerStack.Push(5); 115 interpreter.Run(@"( CODE.QUOTE ( INTEGER.POP 1 ) 116 116 CODE.QUOTE ( CODE.DUP INTEGER.DUP 1 INTEGER.- CODE.DO INTEGER.* ) 117 117 INTEGER.DUP 2 INTEGER.< CODE.IF )"); 118 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);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); 125 125 } 126 126 … … 129 129 [TestCategory("ExampleTest")] 130 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);131 interpreter.IntegerStack.Push(5); 132 interpreter.Run("( 1 INTEGER.MAX 1 EXEC.DO*RANGE INTEGER.* )"); 133 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); 140 140 } 141 141 … … 144 144 [TestCategory("ExampleTest")] 145 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);146 interpreter.IntegerStack.Push(5); 147 interpreter.Run("( 1 INTEGER.MAX 1 EXEC.DO*RANGE ( 2 INTEGER.* ) )"); 148 149 Assert.AreEqual(2, interpreter.IntegerStack.Top); 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); 155 155 } 156 156 … … 161 161 var program = PushParser.Parse("( INTEGER.= CODE.QUOTE FLOAT.* CODE.QUOTE FLOAT./ CODE.IF )"); 162 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);163 interpreter.IntegerStack.Push(1, 1); 164 interpreter.FloatStack.Push(2.1, 0.7); 165 166 interpreter.Run(program); 167 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); 174 175 interpreter.Clear(); 176 interpreter.IntegerStack.Push(1, 2); 177 interpreter.FloatStack.Push(2.1, 0.7); 178 interpreter.Run(program); 179 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); 186 186 } 187 187 … … 192 192 var program = PushParser.Parse("( INTEGER.= EXEC.IF FLOAT.* FLOAT./ )"); 193 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);194 interpreter.IntegerStack.Push(1, 1); 195 interpreter.FloatStack.Push(2.1, 0.7); 196 197 interpreter.Run(program); 198 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); 205 206 interpreter.Clear(); 207 interpreter.IntegerStack.Push(1, 2); 208 interpreter.FloatStack.Push(2.1, 0.7); 209 interpreter.Run(program); 210 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); 217 217 } 218 218 … … 221 221 [TestCategory("ExampleTest")] 222 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);223 interpreter.IntegerStack.Push(5); 224 interpreter.Run("( EXEC.Y ( ( FALSE 2 INTEGER.* ) EXEC.IF ( ) EXEC.POP ) )"); 225 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); 232 232 } 233 233 … … 236 236 [TestCategory("ExampleTest")] 237 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);238 interpreter.IntegerStack.Push(5); 239 interpreter.Run("( EXEC.Y ( ( 2 INTEGER.* INTEGER.DUP 1000 INTEGER.< ) EXEC.IF ( ) EXEC.POP ) )"); 240 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); 247 247 } 248 248 … … 251 251 [TestCategory("ExampleTest")] 252 252 public void Example15() { 253 this.interpreter.IntegerStack.Push(10);254 this.interpreter.FloatStack.Push(2);255 this.interpreter.Run(@"( ARG FLOAT.DEFINE253 interpreter.IntegerStack.Push(10); 254 interpreter.FloatStack.Push(2); 255 interpreter.Run(@"( ARG FLOAT.DEFINE 256 256 EXEC.Y ( 257 257 ARG FLOAT.* … … 262 262 )"); 263 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);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); 270 270 } 271 271 … … 276 276 var list = PushParser.Parse("( A B C )"); 277 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();278 interpreter.CodeStack.Push(list); 279 interpreter.Run("( CODE.DUP 0 CODE.INSERT )"); 280 281 var id = interpreter.CodeStack.Top.GetHashCode(); 282 282 283 283 if (id == default(int)) Assert.Fail();
Note: See TracChangeset
for help on using the changeset viewer.