Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Interpreter/Expressions/CodeExpressionTests.cs @ 14513

Last change on this file since 14513 was 14513, checked in by pkimmesw, 7 years ago

#2665 Added Problem.ProgramSynthesis Project, Fixed Expression Issues, Fixed Code Generation

File size: 35.3 KB
Line 
1namespace HeuristicLab.Tests.Interpreter.Expressions {
2  using HeuristicLab.Algorithms.PushGP;
3  using HeuristicLab.Algorithms.PushGP.Expressions;
4  using HeuristicLab.Algorithms.PushGP.Parser;
5  using HeuristicLab.Algorithms.PushGP.Stack;
6
7  using Microsoft.VisualStudio.TestTools.UnitTesting;
8
9  [TestClass]
10  public class CodeExpressionTests : CommonTests<Expression> {
11    protected override string TypeName
12    {
13      get
14      {
15        return "CODE";
16      }
17    }
18
19    protected override IStack<Expression> Stack
20    {
21      get
22      {
23        return this.interpreter.CodeStack;
24      }
25    }
26
27    [TestMethod]
28    [TestProperty("Time", "Short")]
29    [TestCategory("ExpressionTest")]
30    [TestCategory("CodeExpressionTest")]
31    public void TestAppendLists() {
32      var first = PushGPParser.Parse("( A )");
33      var second = PushGPParser.Parse("( B )");
34      var result = PushGPParser.Parse("( A B )");
35
36      this.interpreter.CodeStack.Push(second, first);
37      this.interpreter.Interpret(new CodeAppendExpression());
38
39      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
40      this.CheckOtherStacksAreEmpty();
41    }
42
43    [TestMethod]
44    [TestProperty("Time", "Short")]
45    [TestCategory("ExpressionTest")]
46    [TestCategory("CodeExpressionTest")]
47    public void TestAppendListAndLiteral() {
48      var first = PushGPParser.Parse("( A )");
49      var second = PushGPParser.Parse("5");
50      var result = PushGPParser.Parse("( A 5 )");
51
52      this.interpreter.CodeStack.Push(second, first);
53      this.interpreter.Interpret(new CodeAppendExpression());
54
55      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
56      this.CheckOtherStacksAreEmpty();
57    }
58
59    [TestMethod]
60    [TestProperty("Time", "Short")]
61    [TestCategory("ExpressionTest")]
62    [TestCategory("CodeExpressionTest")]
63    public void TestAppendLiterals() {
64      var first = PushGPParser.Parse("4");
65      var second = PushGPParser.Parse("5");
66      var result = PushGPParser.Parse("( 4 5 )");
67
68      this.interpreter.CodeStack.Push(second, first);
69      this.interpreter.Interpret(new CodeAppendExpression());
70
71      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
72      this.CheckOtherStacksAreEmpty();
73    }
74
75    [TestMethod]
76    [TestProperty("Time", "Short")]
77    [TestCategory("ExpressionTest")]
78    [TestCategory("CodeExpressionTest")]
79    public void TestDuplicateList() {
80      var list = PushGPParser.Parse("( A B C )");
81      var result = PushGPParser.Parse("( A B C )");
82
83      this.interpreter.CodeStack.Push(list);
84      this.interpreter.Interpret(new CodeDuplicateExpression());
85
86      Assert.AreEqual(this.Stack.Count, 2);
87      Assert.AreEqual(this.Stack[0], list);
88      Assert.AreEqual(this.Stack[1], result);
89
90      this.CheckOtherStacksAreEmpty();
91    }
92
93    [TestMethod]
94    [TestProperty("Time", "Short")]
95    [TestCategory("ExpressionTest")]
96    [TestCategory("CodeExpressionTest")]
97    public void TestAtomList() {
98      var first = PushGPParser.Parse("( A )");
99
100      this.interpreter.CodeStack.Push(first);
101      this.interpreter.Interpret(new CodeAtomExpression());
102
103      Assert.AreEqual(false, this.interpreter.BooleanStack.Top);
104      this.TestStackCounts(booleanStack: 1);
105    }
106
107    [TestMethod]
108    [TestProperty("Time", "Short")]
109    [TestCategory("ExpressionTest")]
110    [TestCategory("CodeExpressionTest")]
111    public void TestAtomLiteral() {
112      var first = PushGPParser.Parse("5");
113
114      this.interpreter.CodeStack.Push(first);
115      this.interpreter.Interpret(new CodeAtomExpression());
116
117      Assert.AreEqual(true, this.interpreter.BooleanStack.Top);
118      this.TestStackCounts(booleanStack: 1);
119    }
120
121    [TestMethod]
122    [TestProperty("Time", "Short")]
123    [TestCategory("ExpressionTest")]
124    [TestCategory("CodeExpressionTest")]
125    public void TestAtomSingleInstruction() {
126      var first = PushGPParser.Parse("INTEGER.+");
127
128      this.interpreter.CodeStack.Push(first);
129      this.interpreter.Interpret(new CodeAtomExpression());
130
131      Assert.AreEqual(true, this.interpreter.BooleanStack.Top);
132      this.TestStackCounts(booleanStack: 1);
133    }
134
135    [TestMethod]
136    [TestProperty("Time", "Short")]
137    [TestCategory("ExpressionTest")]
138    [TestCategory("CodeExpressionTest")]
139    public void TestCarList() {
140      var first = PushGPParser.Parse("( A B C )");
141      var result = PushGPParser.Parse("A");
142
143      this.interpreter.CodeStack.Push(first);
144      this.interpreter.Interpret(new CodeCarExpression());
145
146      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
147      this.CheckOtherStacksAreEmpty();
148    }
149
150    [TestMethod]
151    [TestProperty("Time", "Short")]
152    [TestCategory("ExpressionTest")]
153    [TestCategory("CodeExpressionTest")]
154    public void TestCarNoList() {
155      var first = PushGPParser.Parse("5");
156      var result = PushGPParser.Parse("5");
157
158      this.interpreter.CodeStack.Push(first);
159      this.interpreter.Interpret(new CodeCarExpression());
160
161      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
162      this.CheckOtherStacksAreEmpty();
163    }
164
165    [TestMethod]
166    [TestProperty("Time", "Short")]
167    [TestCategory("ExpressionTest")]
168    [TestCategory("CodeExpressionTest")]
169    public void TestCdrList() {
170      var first = PushGPParser.Parse("( A B C )");
171      var result = PushGPParser.Parse("( B C )");
172
173      this.interpreter.CodeStack.Push(first);
174      this.interpreter.Interpret(new CodeCdrExpression());
175
176      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
177      this.CheckOtherStacksAreEmpty();
178    }
179
180    [TestMethod]
181    [TestProperty("Time", "Short")]
182    [TestCategory("ExpressionTest")]
183    [TestCategory("CodeExpressionTest")]
184    public void TestCdrNoList() {
185      var first = PushGPParser.Parse("5");
186      var result = PushGPParser.Parse("( )");
187
188      this.interpreter.CodeStack.Push(first);
189      this.interpreter.Interpret(new CodeCdrExpression());
190
191      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
192      this.CheckOtherStacksAreEmpty();
193    }
194
195    [TestMethod]
196    [TestProperty("Time", "Short")]
197    [TestCategory("ExpressionTest")]
198    [TestCategory("CodeExpressionTest")]
199    public void TestCons() {
200      var first = PushGPParser.Parse("( B C )");
201      var second = PushGPParser.Parse("A");
202      var result = PushGPParser.Parse("( A B C )");
203
204      this.interpreter.CodeStack.Push(second, first);
205      this.interpreter.Interpret(new CodeConsExpression());
206
207      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
208      this.CheckOtherStacksAreEmpty();
209    }
210
211    [TestMethod]
212    [TestProperty("Time", "Short")]
213    [TestCategory("ExpressionTest")]
214    [TestCategory("CodeExpressionTest")]
215    public void TestContainsTrue() {
216      var first = PushGPParser.Parse("A");
217      var second = PushGPParser.Parse("( A B C )");
218
219      this.interpreter.CodeStack.Push(second, first);
220      this.interpreter.Interpret(new CodeContainsExpression());
221
222      Assert.AreEqual(true, this.interpreter.BooleanStack.Top);
223      this.TestStackCounts(booleanStack: 1);
224    }
225
226    [TestMethod]
227    [TestProperty("Time", "Short")]
228    [TestCategory("ExpressionTest")]
229    [TestCategory("CodeExpressionTest")]
230    public void TestContainsFalse() {
231      var first = PushGPParser.Parse("D");
232      var second = PushGPParser.Parse("( A B C )");
233
234      this.interpreter.CodeStack.Push(second, first);
235      this.interpreter.Interpret(new CodeContainsExpression());
236
237      Assert.AreEqual(false, this.interpreter.BooleanStack.Top);
238      this.TestStackCounts(booleanStack: 1);
239    }
240
241    [TestMethod]
242    [TestProperty("Time", "Short")]
243    [TestCategory("ExpressionTest")]
244    [TestCategory("CodeExpressionTest")]
245    public void TestContainer() {
246      var first = PushGPParser.Parse("( A )");
247      var second = PushGPParser.Parse("( B ( C ( A ) ) ( D ( A ) ) )");
248      var result = PushGPParser.Parse("( C ( A ) )");
249
250      this.interpreter.CodeStack.Push(second, first);
251      this.interpreter.Interpret(new CodeContainerExpression());
252
253      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
254      this.CheckOtherStacksAreEmpty();
255    }
256
257    [TestMethod]
258    [TestProperty("Time", "Short")]
259    [TestCategory("ExpressionTest")]
260    [TestCategory("CodeExpressionTest")]
261    public void TestDefinition() {
262      var code = PushGPParser.Parse("( A )");
263
264      this.interpreter.CustomExpressions.Add("Test", code);
265      this.interpreter.NameStack.Push("Test");
266
267      this.interpreter.Interpret(new CodeDefinitionExpression());
268
269      Assert.AreEqual(code, this.interpreter.CodeStack.Top);
270      this.CheckOtherStacksAreEmpty();
271    }
272
273    [TestMethod]
274    [TestProperty("Time", "Short")]
275    [TestCategory("ExpressionTest")]
276    [TestCategory("CodeExpressionTest")]
277    public void TestDefinitionWithoutMatchingName() {
278      var code = PushGPParser.Parse("( A )");
279      var program = new CodeDefinitionExpression();
280
281      this.interpreter.CustomExpressions.Add("Test1", code);
282      this.interpreter.NameStack.Push("Test2");
283      this.interpreter.Interpret(program);
284
285      this.TestStackCounts(nameStack: 1);
286    }
287
288    [TestMethod]
289    [TestProperty("Time", "Short")]
290    [TestCategory("ExpressionTest")]
291    [TestCategory("CodeExpressionTest")]
292    public void TestDiscrepancy() {
293      var first = PushGPParser.Parse("( A B )");
294      var second = PushGPParser.Parse("( B C )");
295
296      this.interpreter.CodeStack.Push(second, first);
297      this.interpreter.Interpret(new CodeDiscrepancyExpression());
298
299      Assert.AreEqual(2, this.interpreter.IntegerStack.Top);
300      this.TestStackCounts(integerStack: 1);
301    }
302
303    [TestMethod]
304    [TestProperty("Time", "Short")]
305    [TestCategory("ExpressionTest")]
306    [TestCategory("CodeExpressionTest")]
307    public void TestDiscrepancyAllEqual() {
308      var first = PushGPParser.Parse("( A B )");
309      var second = PushGPParser.Parse("( B A )");
310
311      this.interpreter.CodeStack.Push(second, first);
312      this.interpreter.Interpret(new CodeDiscrepancyExpression());
313
314      Assert.AreEqual(0, this.interpreter.IntegerStack.Top);
315      this.TestStackCounts(integerStack: 1);
316    }
317
318    [TestMethod]
319    [TestProperty("Time", "Short")]
320    [TestCategory("ExpressionTest")]
321    [TestCategory("CodeExpressionTest")]
322    public void TestDiscrepancyComplex() {
323      var first = PushGPParser.Parse("( A A B C D A )");
324      var second = PushGPParser.Parse("( A A B D E )");
325
326      this.interpreter.CodeStack.Push(second, first);
327      this.interpreter.Interpret(new CodeDiscrepancyExpression());
328
329      Assert.AreEqual(3, this.interpreter.IntegerStack.Top);
330      this.TestStackCounts(integerStack: 1);
331    }
332
333    [TestMethod]
334    [TestProperty("Time", "Short")]
335    [TestCategory("ExpressionTest")]
336    [TestCategory("CodeExpressionTest")]
337    public void TestDiscrepancWithLists() {
338      var first = PushGPParser.Parse("( ( A ) A ( B ) C D A )");
339      var second = PushGPParser.Parse("( A ( A ) B D E )");
340
341      this.interpreter.CodeStack.Push(second, first);
342      this.interpreter.Interpret(new CodeDiscrepancyExpression());
343
344      Assert.AreEqual(5, this.interpreter.IntegerStack.Top);
345      this.TestStackCounts(integerStack: 1);
346    }
347
348    [TestMethod]
349    [TestProperty("Time", "Short")]
350    [TestCategory("ExpressionTest")]
351    [TestCategory("CodeExpressionTest")]
352    public void TestDo() {
353      var prog = PushGPParser.Parse("( 10 )");
354
355      this.interpreter.CodeStack.Push(prog);
356      this.interpreter.Interpret(new CodeDoExpression());
357
358      Assert.AreEqual(10, this.interpreter.IntegerStack.Top);
359      this.TestStackCounts(integerStack: 1);
360    }
361
362    [TestMethod]
363    [TestProperty("Time", "Short")]
364    [TestCategory("ExpressionTest")]
365    [TestCategory("CodeExpressionTest")]
366    public void TestDoX() {
367      var prog = PushGPParser.Parse("( 10 )");
368
369      this.interpreter.CodeStack.Push(prog);
370      this.interpreter.Interpret(new CodeDoXExpression());
371
372      Assert.AreEqual(10, this.interpreter.IntegerStack.Top);
373      this.TestStackCounts(integerStack: 1);
374    }
375
376    [TestMethod]
377    [TestProperty("Time", "Short")]
378    [TestCategory("ExpressionTest")]
379    [TestCategory("CodeExpressionTest")]
380    public void TestDoCount() {
381      var prog = PushGPParser.Parse("INTEGER.+");
382
383      this.interpreter.IntegerStack.Push(3);
384      this.interpreter.CodeStack.Push(prog);
385      this.interpreter.Interpret(new CodeDoCountExpression());
386
387      Assert.AreEqual(6, this.interpreter.IntegerStack.Top);
388      this.TestStackCounts(integerStack: 1);
389    }
390
391    [TestMethod]
392    [TestProperty("Time", "Short")]
393    [TestCategory("ExpressionTest")]
394    [TestCategory("CodeExpressionTest")]
395    public void TestDoCountWithNegativeCounter() {
396      var prog = PushGPParser.Parse("INTEGER.+");
397
398      this.interpreter.IntegerStack.Push(-1);
399      this.interpreter.CodeStack.Push(prog);
400      this.interpreter.Interpret(new CodeDoCountExpression());
401
402      Assert.AreEqual(-1, this.interpreter.IntegerStack.Top);
403      this.TestStackCounts(codeStack: 1, integerStack: 1);
404    }
405
406    [TestMethod]
407    [TestProperty("Time", "Short")]
408    [TestCategory("ExpressionTest")]
409    [TestCategory("CodeExpressionTest")]
410    public void TestDoRange() {
411      var prog = PushGPParser.Parse("INTEGER.+");
412
413      this.interpreter.IntegerStack.Push(3, 5);
414      this.interpreter.CodeStack.Push(prog);
415      this.interpreter.Interpret(new CodeDoRangeExpression());
416
417      Assert.AreEqual(12, this.interpreter.IntegerStack.Top);
418      this.TestStackCounts(integerStack: 1);
419    }
420
421    [TestMethod]
422    [TestProperty("Time", "Short")]
423    [TestCategory("ExpressionTest")]
424    [TestCategory("CodeExpressionTest")]
425    public void TestDoRangeWithEqualIndecators() {
426      var prog = PushGPParser.Parse("INTEGER.+");
427
428      this.interpreter.IntegerStack.Push(3, 3);
429      this.interpreter.CodeStack.Push(prog);
430      this.interpreter.Interpret(new CodeDoRangeExpression());
431
432      Assert.AreEqual(3, this.interpreter.IntegerStack.Top);
433      this.TestStackCounts(codeStack: 1, integerStack: 2);
434    }
435
436    [TestMethod]
437    [TestProperty("Time", "Short")]
438    [TestCategory("ExpressionTest")]
439    [TestCategory("CodeExpressionTest")]
440    public void TestDoRangeWithNegativeIndecators() {
441      var prog = PushGPParser.Parse("INTEGER.+");
442
443      this.interpreter.IntegerStack.Push(-3, -5);
444      this.interpreter.CodeStack.Push(prog);
445      this.interpreter.Interpret(new CodeDoRangeExpression());
446
447      Assert.AreEqual(-12, this.interpreter.IntegerStack.Top);
448      this.TestStackCounts(integerStack: 1);
449    }
450
451    [TestMethod]
452    [TestProperty("Time", "Short")]
453    [TestCategory("ExpressionTest")]
454    [TestCategory("CodeExpressionTest")]
455    public void TestDoTimes() {
456      var prog = PushGPParser.Parse("( INTEGER.DUP INTEGER.+ )");
457
458      this.interpreter.IntegerStack.Push(2, 3);
459      this.interpreter.CodeStack.Push(prog);
460      this.interpreter.Interpret(new CodeDoTimesExpression());
461
462      Assert.AreEqual(32, this.interpreter.IntegerStack.Top);
463      this.TestStackCounts(integerStack: 1);
464    }
465
466    [TestMethod]
467    [TestProperty("Time", "Short")]
468    [TestCategory("ExpressionTest")]
469    [TestCategory("CodeExpressionTest")]
470    public void TestDoTimesWithNegativeCounter() {
471      var prog = PushGPParser.Parse("( INTEGER.DUP INTEGER.+ )");
472
473      this.interpreter.IntegerStack.Push(2, -3);
474      this.interpreter.CodeStack.Push(prog);
475      this.interpreter.Interpret(new CodeDoTimesExpression());
476
477      Assert.AreEqual(-3, this.interpreter.IntegerStack.Top);
478      this.TestStackCounts(codeStack: 1, integerStack: 2);
479    }
480
481    [TestMethod]
482    [TestProperty("Time", "Short")]
483    [TestCategory("ExpressionTest")]
484    [TestCategory("CodeExpressionTest")]
485    public void TestNestedDoRange() {
486      this.interpreter.Interpret(
487        "( 0 2 CODE.QUOTE ( 1 INTEGER.+ 0 3 CODE.QUOTE ( 1 INTEGER.+ INTEGER.* ) CODE.DO*RANGE INTEGER.+ ) CODE.DO*RANGE )");
488
489      Assert.AreEqual(144, this.interpreter.IntegerStack.Top);
490      this.TestStackCounts(integerStack: 1);
491    }
492
493    [TestMethod]
494    [TestProperty("Time", "Short")]
495    [TestCategory("ExpressionTest")]
496    [TestCategory("CodeExpressionTest")]
497    public void TestNestedDoCount() {
498      this.interpreter.Interpret(
499        "( 2 CODE.QUOTE ( 1 INTEGER.+ 3 CODE.QUOTE ( 1 INTEGER.+ INTEGER.* ) CODE.DO*COUNT INTEGER.+ ) CODE.DO*COUNT )");
500
501      Assert.AreEqual(144, this.interpreter.IntegerStack.Top);
502      this.TestStackCounts(integerStack: 1);
503    }
504
505    [TestMethod]
506    [TestProperty("Time", "Short")]
507    [TestCategory("ExpressionTest")]
508    [TestCategory("CodeExpressionTest")]
509    public void TestNestedDoTimes() {
510      this.interpreter.Interpret(
511        "( 3 CODE.QUOTE ( 2 3 CODE.QUOTE ( 2 INTEGER.* ) CODE.DO*TIMES INTEGER.+ ) CODE.DO*TIMES )");
512
513      Assert.AreEqual(128, this.interpreter.IntegerStack.Top);
514      this.TestStackCounts(integerStack: 1);
515    }
516
517    [TestMethod]
518    [TestProperty("Time", "Short")]
519    [TestCategory("ExpressionTest")]
520    [TestCategory("CodeExpressionTest")]
521    public void TestExtract() {
522      var prog = PushGPParser.Parse("( A ( B C ( D ) E ) F G H I J ( K ) L )");
523      var result = PushGPParser.Parse("( D )");
524
525      this.interpreter.IntegerStack.Push(5);
526      this.interpreter.CodeStack.Push(prog);
527      this.interpreter.Interpret(new CodeExtractExpression());
528
529      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
530      this.CheckOtherStacksAreEmpty();
531    }
532
533    [TestMethod]
534    [TestProperty("Time", "Short")]
535    [TestCategory("ExpressionTest")]
536    [TestCategory("CodeExpressionTest")]
537    public void TestExtractInteger() {
538      var prog = PushGPParser.Parse("( A ( 5 C ( 4.2 ) INTEGER.+ ) FALSE )");
539      var result = PushGPParser.Parse("5");
540
541      this.interpreter.IntegerStack.Push(3);
542      this.interpreter.CodeStack.Push(prog);
543      this.interpreter.Interpret(new CodeExtractExpression());
544
545      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
546      this.CheckOtherStacksAreEmpty();
547    }
548
549    [TestMethod]
550    [TestProperty("Time", "Short")]
551    [TestCategory("ExpressionTest")]
552    [TestCategory("CodeExpressionTest")]
553    public void TestExtractFloat() {
554      var prog = PushGPParser.Parse("( A ( 5 C ( 4.2 ) INTEGER.+ ) FALSE )");
555      var result = PushGPParser.Parse("4.2");
556
557      this.interpreter.IntegerStack.Push(6);
558      this.interpreter.CodeStack.Push(prog);
559      this.interpreter.Interpret(new CodeExtractExpression());
560
561      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
562      this.CheckOtherStacksAreEmpty();
563    }
564
565    [TestMethod]
566    [TestProperty("Time", "Short")]
567    [TestCategory("ExpressionTest")]
568    [TestCategory("CodeExpressionTest")]
569    public void TestExtractBoolean() {
570      var prog = PushGPParser.Parse("( A ( 5 C ( 4.2 ) INTEGER.+ ) FALSE )");
571      var result = PushGPParser.Parse("FALSE");
572
573      this.interpreter.IntegerStack.Push(8);
574      this.interpreter.CodeStack.Push(prog);
575      this.interpreter.Interpret(new CodeExtractExpression());
576
577      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
578      this.CheckOtherStacksAreEmpty();
579    }
580
581    [TestMethod]
582    [TestProperty("Time", "Short")]
583    [TestCategory("ExpressionTest")]
584    [TestCategory("CodeExpressionTest")]
585    public void TestExtractInstruction() {
586      var prog = PushGPParser.Parse("( A ( 5 C ( 4.2 ) INTEGER.+ ) FALSE )");
587      var result = PushGPParser.Parse("INTEGER.+");
588
589      this.interpreter.IntegerStack.Push(7);
590      this.interpreter.CodeStack.Push(prog);
591      this.interpreter.Interpret(new CodeExtractExpression());
592
593      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
594      this.CheckOtherStacksAreEmpty();
595    }
596
597    [TestMethod]
598    [TestProperty("Time", "Short")]
599    [TestCategory("ExpressionTest")]
600    [TestCategory("CodeExpressionTest")]
601    public void TestFromBoolean() {
602      var result = PushGPParser.Parse("FALSE");
603      this.interpreter.BooleanStack.Push(false);
604      this.interpreter.Interpret(new CodeFromBooleanExpression());
605
606      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
607      this.CheckOtherStacksAreEmpty();
608    }
609
610    [TestMethod]
611    [TestProperty("Time", "Short")]
612    [TestCategory("ExpressionTest")]
613    [TestCategory("CodeExpressionTest")]
614    public void TestFromFloat() {
615      var result = PushGPParser.Parse("4.1");
616      this.interpreter.FloatStack.Push(4.1);
617      this.interpreter.Interpret(new CodeFromFloatExpression());
618
619      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
620      this.CheckOtherStacksAreEmpty();
621    }
622
623    [TestMethod]
624    [TestProperty("Time", "Short")]
625    [TestCategory("ExpressionTest")]
626    [TestCategory("CodeExpressionTest")]
627    public void TestFromInteger() {
628      var result = PushGPParser.Parse("4");
629      this.interpreter.IntegerStack.Push(4);
630      this.interpreter.Interpret(new CodeFromIntegerExpression());
631
632      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
633      this.CheckOtherStacksAreEmpty();
634    }
635
636    [TestMethod]
637    [TestProperty("Time", "Short")]
638    [TestCategory("ExpressionTest")]
639    [TestCategory("CodeExpressionTest")]
640    public void TestFromName() {
641      var result = PushGPParser.Parse("A");
642      this.interpreter.NameStack.Push("A");
643      this.interpreter.Interpret(new CodeFromNameExpression());
644
645      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
646      this.CheckOtherStacksAreEmpty();
647    }
648
649    [TestMethod]
650    [TestProperty("Time", "Short")]
651    [TestCategory("ExpressionTest")]
652    [TestCategory("CodeExpressionTest")]
653    public void TestIfTrue() {
654      var first = PushGPParser.Parse("FALSCH");
655      var second = PushGPParser.Parse("WAHR");
656
657      this.interpreter.BooleanStack.Push(true);
658      this.interpreter.CodeStack.Push(second, first);
659      this.interpreter.Interpret(new CodeIfExpression());
660
661      Assert.AreEqual("WAHR", this.interpreter.NameStack.Top);
662      this.TestStackCounts(nameStack: 1);
663    }
664
665    [TestMethod]
666    [TestProperty("Time", "Short")]
667    [TestCategory("ExpressionTest")]
668    [TestCategory("CodeExpressionTest")]
669    public void TestIfFalse() {
670      var first = PushGPParser.Parse("FALSCH");
671      var second = PushGPParser.Parse("WAHR");
672
673      this.interpreter.BooleanStack.Push(false);
674      this.interpreter.CodeStack.Push(second, first);
675      this.interpreter.Interpret(new CodeIfExpression());
676
677      Assert.AreEqual("FALSCH", this.interpreter.NameStack.Top);
678      this.TestStackCounts(nameStack: 1);
679    }
680
681    [TestMethod]
682    [TestProperty("Time", "Short")]
683    [TestCategory("ExpressionTest")]
684    [TestCategory("CodeExpressionTest")]
685    public void TestInsert() {
686      var first = PushGPParser.Parse("( A ( B ) C D )");
687      var second = PushGPParser.Parse("( E )");
688      var result = PushGPParser.Parse("( A ( B ) ( E ) D )");
689
690      this.interpreter.IntegerStack.Push(2);
691      this.interpreter.CodeStack.Push(second, first);
692      this.interpreter.Interpret(new CodeInsertExpression());
693
694      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
695      this.CheckOtherStacksAreEmpty();
696    }
697
698    [TestMethod]
699    [TestProperty("Time", "Short")]
700    [TestCategory("ExpressionTest")]
701    [TestCategory("CodeExpressionTest")]
702    public void TestInsertWithNegativeN() {
703      var first = PushGPParser.Parse("( A ( B ) C D )");
704      var second = PushGPParser.Parse("( E )");
705      var result = PushGPParser.Parse("( A ( B ) ( E ) D )");
706
707      this.interpreter.IntegerStack.Push(-2);
708      this.interpreter.CodeStack.Push(second, first);
709      this.interpreter.Interpret(new CodeInsertExpression());
710
711      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
712      this.CheckOtherStacksAreEmpty();
713    }
714
715    [TestMethod]
716    [TestProperty("Time", "Short")]
717    [TestCategory("ExpressionTest")]
718    [TestCategory("CodeExpressionTest")]
719    public void TestInsertWithTooBigN() {
720      var first = PushGPParser.Parse("( A ( B ) C D )");
721      var second = PushGPParser.Parse("( E )");
722      var result = PushGPParser.Parse("( A ( B ) ( E ) D )");
723
724      this.interpreter.IntegerStack.Push(10);
725      this.interpreter.CodeStack.Push(second, first);
726      this.interpreter.Interpret(new CodeInsertExpression());
727
728      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
729      this.CheckOtherStacksAreEmpty();
730    }
731
732    [TestMethod]
733    [TestProperty("Time", "Short")]
734    [TestCategory("ExpressionTest")]
735    [TestCategory("CodeExpressionTest")]
736    public void TestList() {
737      var first = PushGPParser.Parse("A");
738      var second = PushGPParser.Parse("( B )");
739      var result = PushGPParser.Parse("( A ( B ) )");
740
741      this.interpreter.CodeStack.Push(second, first);
742      this.interpreter.Interpret(new CodeListExpression());
743
744      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
745      this.CheckOtherStacksAreEmpty();
746    }
747
748    [TestMethod]
749    [TestProperty("Time", "Short")]
750    [TestCategory("ExpressionTest")]
751    [TestCategory("CodeExpressionTest")]
752    public void TestMemberTrue() {
753      var first = PushGPParser.Parse("( A B ( B ) C ");
754      var second = PushGPParser.Parse("( B )");
755
756      this.interpreter.CodeStack.Push(second, first);
757      this.interpreter.Interpret(new CodeMemberExpression());
758
759      Assert.AreEqual(true, this.interpreter.BooleanStack.Top);
760      this.TestStackCounts(booleanStack: 1);
761    }
762
763    [TestMethod]
764    [TestProperty("Time", "Short")]
765    [TestCategory("ExpressionTest")]
766    [TestCategory("CodeExpressionTest")]
767    public void TestMemberFalse() {
768      var first = PushGPParser.Parse("( A B C ");
769      var second = PushGPParser.Parse("( B )");
770
771      this.interpreter.CodeStack.Push(second, first);
772      this.interpreter.Interpret(new CodeMemberExpression());
773
774      Assert.AreEqual(false, this.interpreter.BooleanStack.Top);
775      this.TestStackCounts(booleanStack: 1);
776    }
777
778    [TestMethod]
779    [TestProperty("Time", "Short")]
780    [TestCategory("ExpressionTest")]
781    [TestCategory("CodeExpressionTest")]
782    public void TestMemberIfNoList() {
783      var first = PushGPParser.Parse("B");
784      var second = PushGPParser.Parse("B");
785
786      this.interpreter.CodeStack.Push(second, first);
787      this.interpreter.Interpret(new CodeMemberExpression());
788
789      Assert.AreEqual(true, this.interpreter.BooleanStack.Top);
790      this.TestStackCounts(booleanStack: 1);
791    }
792
793    [TestMethod]
794    [TestProperty("Time", "Short")]
795    [TestCategory("ExpressionTest")]
796    [TestCategory("CodeExpressionTest")]
797    public void TestNoop() {
798      this.interpreter.Interpret(new CodeNoopExpression());
799
800      this.CheckOtherStacksAreEmpty();
801    }
802
803    [TestMethod]
804    [TestProperty("Time", "Short")]
805    [TestCategory("ExpressionTest")]
806    [TestCategory("CodeExpressionTest")]
807    public void TestNthIfNoList() {
808      var prog = PushGPParser.Parse("A");
809      var result = PushGPParser.Parse("A");
810
811      this.interpreter.IntegerStack.Push(3);
812      this.interpreter.CodeStack.Push(prog);
813      this.interpreter.Interpret(new CodeNthExpression());
814
815      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
816      this.CheckOtherStacksAreEmpty();
817    }
818
819    [TestMethod]
820    [TestProperty("Time", "Short")]
821    [TestCategory("ExpressionTest")]
822    [TestCategory("CodeExpressionTest")]
823    public void TestNthIfEmpty() {
824      var prog = PushGPParser.Parse("( )");
825      var result = PushGPParser.Parse("( )");
826
827      this.interpreter.IntegerStack.Push(3);
828      this.interpreter.CodeStack.Push(prog);
829      this.interpreter.Interpret(new CodeNthExpression());
830
831      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
832      this.CheckOtherStacksAreEmpty();
833    }
834
835    [TestMethod]
836    [TestProperty("Time", "Short")]
837    [TestCategory("ExpressionTest")]
838    [TestCategory("CodeExpressionTest")]
839    public void TestNth() {
840      var prog = PushGPParser.Parse("( A B C D E )");
841      var result = PushGPParser.Parse("D");
842
843      this.interpreter.IntegerStack.Push(3);
844      this.interpreter.CodeStack.Push(prog);
845      this.interpreter.Interpret(new CodeNthExpression());
846
847      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
848      this.CheckOtherStacksAreEmpty();
849    }
850
851    [TestMethod]
852    [TestProperty("Time", "Short")]
853    [TestCategory("ExpressionTest")]
854    [TestCategory("CodeExpressionTest")]
855    public void TestNthWithTooBigN() {
856      var prog = PushGPParser.Parse("( A B C D E )");
857      var result = PushGPParser.Parse("D");
858
859      this.interpreter.IntegerStack.Push(13);
860      this.interpreter.CodeStack.Push(prog);
861      this.interpreter.Interpret(new CodeNthExpression());
862
863      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
864      this.CheckOtherStacksAreEmpty();
865    }
866
867    [TestMethod]
868    [TestProperty("Time", "Short")]
869    [TestCategory("ExpressionTest")]
870    [TestCategory("CodeExpressionTest")]
871    public void TestNthWithNegativeN() {
872      var prog = PushGPParser.Parse("( A B C D E )");
873      var result = PushGPParser.Parse("D");
874
875      this.interpreter.IntegerStack.Push(-3);
876      this.interpreter.CodeStack.Push(prog);
877      this.interpreter.Interpret(new CodeNthExpression());
878
879      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
880      this.CheckOtherStacksAreEmpty();
881    }
882
883    [TestMethod]
884    [TestProperty("Time", "Short")]
885    [TestCategory("ExpressionTest")]
886    [TestCategory("CodeExpressionTest")]
887    public void TestNthCdr() {
888      var prog = PushGPParser.Parse("( A B C D E )");
889      var result = PushGPParser.Parse("D");
890
891      this.interpreter.IntegerStack.Push(2);
892      this.interpreter.CodeStack.Push(prog);
893      this.interpreter.Interpret(new CodeNthCdrExpression());
894
895      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
896      this.CheckOtherStacksAreEmpty();
897    }
898
899    [TestMethod]
900    [TestProperty("Time", "Short")]
901    [TestCategory("ExpressionTest")]
902    [TestCategory("CodeExpressionTest")]
903    public void TestNthCdrWithTooBigN() {
904      var prog = PushGPParser.Parse("( A B C D E )");
905      var result = PushGPParser.Parse("D");
906
907      this.interpreter.IntegerStack.Push(10);
908      this.interpreter.CodeStack.Push(prog);
909      this.interpreter.Interpret(new CodeNthCdrExpression());
910
911      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
912      this.CheckOtherStacksAreEmpty();
913    }
914
915    [TestMethod]
916    [TestProperty("Time", "Short")]
917    [TestCategory("ExpressionTest")]
918    [TestCategory("CodeExpressionTest")]
919    public void TestNthCdrWithNegativeN() {
920      var prog = PushGPParser.Parse("( A B C D E )");
921      var result = PushGPParser.Parse("D");
922
923      this.interpreter.IntegerStack.Push(-2);
924      this.interpreter.CodeStack.Push(prog);
925      this.interpreter.Interpret(new CodeNthCdrExpression());
926
927      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
928      this.CheckOtherStacksAreEmpty();
929    }
930
931    [TestMethod]
932    [TestProperty("Time", "Short")]
933    [TestCategory("ExpressionTest")]
934    [TestCategory("CodeExpressionTest")]
935    public void TestNthCdrIfEmpty() {
936      var prog = PushGPParser.Parse("( )");
937      var result = PushGPParser.Parse("( )");
938
939      this.interpreter.IntegerStack.Push(3);
940      this.interpreter.CodeStack.Push(prog);
941      this.interpreter.Interpret(new CodeNthCdrExpression());
942
943      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
944      this.CheckOtherStacksAreEmpty();
945    }
946
947    [TestMethod]
948    [TestProperty("Time", "Short")]
949    [TestCategory("ExpressionTest")]
950    [TestCategory("CodeExpressionTest")]
951    public void TestNthCdrIfNoList() {
952      var prog = PushGPParser.Parse("A");
953      var result = PushGPParser.Parse("A");
954
955      this.interpreter.IntegerStack.Push(3);
956      this.interpreter.CodeStack.Push(prog);
957      this.interpreter.Interpret(new CodeNthCdrExpression());
958
959      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
960      this.CheckOtherStacksAreEmpty();
961    }
962
963    [TestMethod]
964    [TestProperty("Time", "Short")]
965    [TestCategory("ExpressionTest")]
966    [TestCategory("CodeExpressionTest")]
967    public void TestNullTrue() {
968      var prog = PushGPParser.Parse("( )");
969
970      this.interpreter.CodeStack.Push(prog);
971      this.interpreter.Interpret(new CodeNullExpression());
972
973      Assert.AreEqual(true, this.interpreter.BooleanStack.Top);
974      this.TestStackCounts(booleanStack: 1);
975    }
976
977    [TestMethod]
978    [TestProperty("Time", "Short")]
979    [TestCategory("ExpressionTest")]
980    [TestCategory("CodeExpressionTest")]
981    public void TestNullFalse() {
982      var prog = PushGPParser.Parse("A");
983
984      this.interpreter.CodeStack.Push(prog);
985      this.interpreter.Interpret(new CodeNullExpression());
986
987      Assert.AreEqual(false, this.interpreter.BooleanStack.Top);
988      this.TestStackCounts(booleanStack: 1);
989    }
990
991    [TestMethod]
992    [TestProperty("Time", "Short")]
993    [TestCategory("ExpressionTest")]
994    [TestCategory("CodeExpressionTest")]
995    public void TestPosition() {
996      var first = PushGPParser.Parse("( A B C D )");
997      var second = PushGPParser.Parse("C");
998
999      this.interpreter.CodeStack.Push(second, first);
1000      this.interpreter.Interpret(new CodePositionExpression());
1001
1002      Assert.AreEqual(2, this.interpreter.IntegerStack.Top);
1003      this.TestStackCounts(integerStack: 1);
1004    }
1005
1006    [TestMethod]
1007    [TestProperty("Time", "Short")]
1008    [TestCategory("ExpressionTest")]
1009    [TestCategory("CodeExpressionTest")]
1010    public void TestQuote() {
1011      var result = PushGPParser.Parse("A");
1012
1013      this.interpreter.Interpret("( CODE.QUOTE A )");
1014
1015      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
1016      this.CheckOtherStacksAreEmpty();
1017    }
1018
1019    [TestMethod]
1020    [TestProperty("Time", "Short")]
1021    [TestCategory("ExpressionTest")]
1022    [TestCategory("CodeExpressionTest")]
1023    public void TestSize() {
1024      var prog = PushGPParser.Parse("( A ( B ( C ) ) ( D ) INTEGER.+ )");
1025
1026      this.interpreter.CodeStack.Push(prog);
1027      this.interpreter.Interpret(new CodeSizeExpression());
1028
1029      Assert.AreEqual(4, this.interpreter.IntegerStack.Top);
1030      this.TestStackCounts(integerStack: 1);
1031    }
1032
1033    [TestMethod]
1034    [TestProperty("Time", "Short")]
1035    [TestCategory("ExpressionTest")]
1036    [TestCategory("CodeExpressionTest")]
1037    public void TestSubstitude() {
1038      var first = PushGPParser.Parse("( A B C D A B C D )");
1039      var second = PushGPParser.Parse("E");
1040      var third = PushGPParser.Parse("A");
1041      var result = PushGPParser.Parse("( E B C D E B C D )");
1042
1043      this.interpreter.CodeStack.Push(third, second, first);
1044      this.interpreter.Interpret(new CodeSubstitutionExpression());
1045
1046      Assert.AreEqual(result, this.interpreter.CodeStack.Top);
1047      this.CheckOtherStacksAreEmpty();
1048    }
1049
1050    protected override Expression[] GetValues(int count) {
1051      var values = new Expression[count];
1052
1053      for (var i = 0; i < count; i++) values[i] = new CodeNoopExpression();
1054
1055      return values;
1056    }
1057
1058    protected override Expression[] Get2Different() {
1059      return new Expression[] { new CodeNoopExpression(), new CodeNullExpression() };
1060    }
1061
1062    protected override void CheckOtherStacksAreEmpty() {
1063      this.TestStackCounts(codeStack: null);
1064    }
1065  }
1066}
Note: See TracBrowser for help on using the repository browser.