namespace HeuristicLab.Tests.Interpreter.Expressions { using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; using HeuristicLab.Problems.ProgramSynthesis.Push.Parser; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class CodeExpressionTests : CommonTests { protected override string TypeName { get { return "CODE"; } } protected override IPushStack Stack { get { return interpreter.CodeStack; } } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestAppendLists() { var first = PushParser.Parse("( A )"); var second = PushParser.Parse("( B )"); var result = PushParser.Parse("( A B )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeAppendExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestAppendListAndLiteral() { var first = PushParser.Parse("( A )"); var second = PushParser.Parse("5"); var result = PushParser.Parse("( A 5 )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeAppendExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestAppendLiterals() { var first = PushParser.Parse("4"); var second = PushParser.Parse("5"); var result = PushParser.Parse("( 4 5 )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeAppendExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDuplicateList() { var list = PushParser.Parse("( A B C )"); var result = PushParser.Parse("( A B C )"); interpreter.CodeStack.Push(list); interpreter.Run(new CodeDuplicateExpression()); Assert.AreEqual(Stack.Count, 2); Assert.AreEqual(Stack[0], list); Assert.AreEqual(Stack[1], result); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestAtomList() { var first = PushParser.Parse("( A )"); interpreter.CodeStack.Push(first); interpreter.Run(new CodeAtomExpression()); Assert.AreEqual(false, interpreter.BooleanStack.Top); TestStackCounts(booleanStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestAtomLiteral() { var first = PushParser.Parse("5"); interpreter.CodeStack.Push(first); interpreter.Run(new CodeAtomExpression()); Assert.AreEqual(true, interpreter.BooleanStack.Top); TestStackCounts(booleanStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestAtomSingleInstruction() { var first = PushParser.Parse("INTEGER.+"); interpreter.CodeStack.Push(first); interpreter.Run(new CodeAtomExpression()); Assert.AreEqual(true, interpreter.BooleanStack.Top); TestStackCounts(booleanStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestCarList() { var first = PushParser.Parse("( A B C )"); var result = PushParser.Parse("A"); interpreter.CodeStack.Push(first); interpreter.Run(new CodeCarExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestCarNoList() { var first = PushParser.Parse("5"); var result = PushParser.Parse("5"); interpreter.CodeStack.Push(first); interpreter.Run(new CodeCarExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestCdrList() { var first = PushParser.Parse("( A B C )"); var result = PushParser.Parse("( B C )"); interpreter.CodeStack.Push(first); interpreter.Run(new CodeCdrExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestCdrNoList() { var first = PushParser.Parse("5"); var result = PushParser.Parse("( )"); interpreter.CodeStack.Push(first); interpreter.Run(new CodeCdrExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestCons() { var first = PushParser.Parse("( B C )"); var second = PushParser.Parse("A"); var result = PushParser.Parse("( A B C )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeConsExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestContainsTrue() { var first = PushParser.Parse("A"); var second = PushParser.Parse("( A B C )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeContainsExpression()); Assert.AreEqual(true, interpreter.BooleanStack.Top); TestStackCounts(booleanStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestContainsFalse() { var first = PushParser.Parse("D"); var second = PushParser.Parse("( A B C )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeContainsExpression()); Assert.AreEqual(false, interpreter.BooleanStack.Top); TestStackCounts(booleanStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestContainer() { var first = PushParser.Parse("( A )"); var second = PushParser.Parse("( B ( C ( A ) ) ( D ( A ) ) )"); var result = PushParser.Parse("( C ( A ) )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeContainerExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDefinition() { var code = PushParser.Parse("( A )"); interpreter.CustomExpressions.Add("Test", code); interpreter.NameStack.Push("Test"); interpreter.Run(new CodeDefinitionExpression()); Assert.AreEqual(code, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDefinitionWithoutMatchingName() { var code = PushParser.Parse("( A )"); var program = new CodeDefinitionExpression(); interpreter.CustomExpressions.Add("Test1", code); interpreter.NameStack.Push("Test2"); interpreter.Run(program); TestStackCounts(nameStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDiscrepancy() { var first = PushParser.Parse("( A B )"); var second = PushParser.Parse("( B C )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeDiscrepancyExpression()); Assert.AreEqual(2, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDiscrepancyAllEqual() { var first = PushParser.Parse("( A B )"); var second = PushParser.Parse("( B A )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeDiscrepancyExpression()); Assert.AreEqual(0, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDiscrepancyComplex() { var first = PushParser.Parse("( A A B C D A )"); var second = PushParser.Parse("( A A B D E )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeDiscrepancyExpression()); Assert.AreEqual(3, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDiscrepancWithLists() { var first = PushParser.Parse("( ( A ) A ( B ) C D A )"); var second = PushParser.Parse("( A ( A ) B D E )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeDiscrepancyExpression()); Assert.AreEqual(5, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDo() { var prog = PushParser.Parse("( 10 )"); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeDoExpression()); Assert.AreEqual(10, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDoX() { var prog = PushParser.Parse("( 10 )"); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeDoXExpression()); Assert.AreEqual(10, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDoCount() { var prog = PushParser.Parse("INTEGER.+"); interpreter.IntegerStack.Push(3); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeDoCountExpression()); Assert.AreEqual(6, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDoCountWithNegativeCounter() { var prog = PushParser.Parse("INTEGER.+"); interpreter.IntegerStack.Push(-1); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeDoCountExpression()); Assert.AreEqual(-1, interpreter.IntegerStack.Top); TestStackCounts(codeStack: 1, integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDoRange() { var prog = PushParser.Parse("INTEGER.+"); interpreter.IntegerStack.Push(3, 5); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeDoRangeExpression()); Assert.AreEqual(12, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDoRangeWithEqualIndecators() { var prog = PushParser.Parse("INTEGER.+"); interpreter.IntegerStack.Push(3, 3); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeDoRangeExpression()); Assert.AreEqual(3, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDoRangeWithNegativeIndecators() { var prog = PushParser.Parse("INTEGER.+"); interpreter.IntegerStack.Push(-3, -5); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeDoRangeExpression()); Assert.AreEqual(-12, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDoTimes() { var prog = PushParser.Parse("( INTEGER.DUP INTEGER.+ )"); interpreter.IntegerStack.Push(2, 4); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeDoTimesExpression()); Assert.AreEqual(32, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestDoTimesWithNegativeCounter() { var prog = PushParser.Parse("( INTEGER.DUP INTEGER.+ )"); interpreter.IntegerStack.Push(2, -3); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeDoTimesExpression()); Assert.AreEqual(-3, interpreter.IntegerStack.Top); TestStackCounts(codeStack: 1, integerStack: 2); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNestedDoRange() { interpreter.Run( "( 0 2 CODE.QUOTE ( 1 INTEGER.+ 0 3 CODE.QUOTE ( 1 INTEGER.+ INTEGER.* ) CODE.DO*RANGE INTEGER.+ ) CODE.DO*RANGE )"); Assert.AreEqual(144, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNestedDoCount() { interpreter.Run( "( 2 CODE.QUOTE ( 1 INTEGER.+ 3 CODE.QUOTE ( 1 INTEGER.+ INTEGER.* ) CODE.DO*COUNT INTEGER.+ ) CODE.DO*COUNT )"); Assert.AreEqual(144, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNestedDoTimes() { interpreter.Run( "( 4 CODE.QUOTE ( 2 4 CODE.QUOTE ( 2 INTEGER.* ) CODE.DO*TIMES INTEGER.+ ) CODE.DO*TIMES )"); Assert.AreEqual(128, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestExtract() { var prog = PushParser.Parse("( A ( B C ( D ) E ) F G H I J ( K ) L )"); var result = PushParser.Parse("( D )"); interpreter.IntegerStack.Push(5); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeExtractExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestExtractInteger() { var prog = PushParser.Parse("( A ( 5 C ( 4.2 ) INTEGER.+ ) FALSE )"); var result = PushParser.Parse("5"); interpreter.IntegerStack.Push(3); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeExtractExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestExtractFloat() { var prog = PushParser.Parse("( A ( 5 C ( 4.2 ) INTEGER.+ ) FALSE )"); var result = PushParser.Parse("4.2"); interpreter.IntegerStack.Push(6); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeExtractExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestExtractBoolean() { var prog = PushParser.Parse("( A ( 5 C ( 4.2 ) INTEGER.+ ) FALSE )"); var result = PushParser.Parse("FALSE"); interpreter.IntegerStack.Push(8); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeExtractExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestExtractInstruction() { var prog = PushParser.Parse("( A ( 5 C ( 4.2 ) INTEGER.+ ) FALSE )"); var result = PushParser.Parse("INTEGER.+"); interpreter.IntegerStack.Push(7); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeExtractExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestFromBoolean() { var result = PushParser.Parse("FALSE"); interpreter.BooleanStack.Push(false); interpreter.Run(new CodeFromBooleanExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestFromFloat() { var result = PushParser.Parse("4.1"); interpreter.FloatStack.Push(4.1); interpreter.Run(new CodeFromFloatExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestFromInteger() { var result = PushParser.Parse("4"); interpreter.IntegerStack.Push(4); interpreter.Run(new CodeFromIntegerExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestFromName() { var result = PushParser.Parse("A"); interpreter.NameStack.Push("A"); interpreter.Run(new CodeFromNameExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestIfTrue() { var first = PushParser.Parse("FALSCH"); var second = PushParser.Parse("WAHR"); interpreter.BooleanStack.Push(true); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeIfExpression()); Assert.AreEqual("WAHR", interpreter.NameStack.Top); TestStackCounts(nameStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestIfFalse() { var first = PushParser.Parse("FALSCH"); var second = PushParser.Parse("WAHR"); interpreter.BooleanStack.Push(false); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeIfExpression()); Assert.AreEqual("FALSCH", interpreter.NameStack.Top); TestStackCounts(nameStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestInsert() { var first = PushParser.Parse("( A ( B ) C D )"); var second = PushParser.Parse("( E )"); var result = PushParser.Parse("( A ( B ) ( E ) D )"); interpreter.IntegerStack.Push(2); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeInsertExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestInsertWithNegativeN() { var first = PushParser.Parse("( A ( B ) C D )"); var second = PushParser.Parse("( E )"); var result = PushParser.Parse("( A ( B ) ( E ) D )"); interpreter.IntegerStack.Push(-2); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeInsertExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestInsertWithTooBigN() { var first = PushParser.Parse("( A ( B ) C D )"); var second = PushParser.Parse("( E )"); var result = PushParser.Parse("( A ( B ) ( E ) D )"); interpreter.IntegerStack.Push(10); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeInsertExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestList() { var first = PushParser.Parse("A"); var second = PushParser.Parse("( B )"); var result = PushParser.Parse("( A ( B ) )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeListExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestMemberTrue() { var first = PushParser.Parse("( A B ( B ) C "); var second = PushParser.Parse("( B )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeMemberExpression()); Assert.AreEqual(true, interpreter.BooleanStack.Top); TestStackCounts(booleanStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestMemberFalse() { var first = PushParser.Parse("( A B C "); var second = PushParser.Parse("( B )"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeMemberExpression()); Assert.AreEqual(false, interpreter.BooleanStack.Top); TestStackCounts(booleanStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestMemberIfNoList() { var first = PushParser.Parse("B"); var second = PushParser.Parse("B"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodeMemberExpression()); Assert.AreEqual(true, interpreter.BooleanStack.Top); TestStackCounts(booleanStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNoop() { interpreter.Run(new CodeNoopExpression()); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNthIfNoList() { var prog = PushParser.Parse("A"); var result = PushParser.Parse("A"); interpreter.IntegerStack.Push(3); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeNthExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNthIfEmpty() { var prog = PushParser.Parse("( )"); var result = PushParser.Parse("( )"); interpreter.IntegerStack.Push(3); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeNthExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNth() { var prog = PushParser.Parse("( A B C D E )"); var result = PushParser.Parse("D"); interpreter.IntegerStack.Push(3); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeNthExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNthWithTooBigN() { var prog = PushParser.Parse("( A B C D E )"); var result = PushParser.Parse("D"); interpreter.IntegerStack.Push(13); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeNthExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNthWithNegativeN() { var prog = PushParser.Parse("( A B C D E )"); var result = PushParser.Parse("D"); interpreter.IntegerStack.Push(-3); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeNthExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNthCdr() { var prog = PushParser.Parse("( A B C D E )"); var result = PushParser.Parse("D"); interpreter.IntegerStack.Push(2); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeNthCdrExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNthCdrWithTooBigN() { var prog = PushParser.Parse("( A B C D E )"); var result = PushParser.Parse("D"); interpreter.IntegerStack.Push(10); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeNthCdrExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNthCdrWithNegativeN() { var prog = PushParser.Parse("( A B C D E )"); var result = PushParser.Parse("D"); interpreter.IntegerStack.Push(-2); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeNthCdrExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNthCdrIfEmpty() { var prog = PushParser.Parse("( )"); var result = PushParser.Parse("( )"); interpreter.IntegerStack.Push(3); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeNthCdrExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNthCdrIfNoList() { var prog = PushParser.Parse("A"); var result = PushParser.Parse("A"); interpreter.IntegerStack.Push(3); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeNthCdrExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNullTrue() { var prog = PushParser.Parse("( )"); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeNullExpression()); Assert.AreEqual(true, interpreter.BooleanStack.Top); TestStackCounts(booleanStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestNullFalse() { var prog = PushParser.Parse("A"); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeNullExpression()); Assert.AreEqual(false, interpreter.BooleanStack.Top); TestStackCounts(booleanStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestPosition() { var first = PushParser.Parse("( A B C D )"); var second = PushParser.Parse("C"); interpreter.CodeStack.Push(second, first); interpreter.Run(new CodePositionExpression()); Assert.AreEqual(2, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestQuote() { var result = PushParser.Parse("A"); interpreter.Run("( CODE.QUOTE A )"); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestSize() { var prog = PushParser.Parse("( A ( B ( C ) ) ( D ) INTEGER.+ )"); interpreter.CodeStack.Push(prog); interpreter.Run(new CodeSizeExpression()); Assert.AreEqual(4, interpreter.IntegerStack.Top); TestStackCounts(integerStack: 1); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("CodeExpressionTest")] public void TestSubstitude() { var first = PushParser.Parse("( A B C D A B C D )"); var second = PushParser.Parse("E"); var third = PushParser.Parse("A"); var result = PushParser.Parse("( E B C D E B C D )"); interpreter.CodeStack.Push(third, second, first); interpreter.Run(new CodeSubstitutionExpression()); Assert.AreEqual(result, interpreter.CodeStack.Top); CheckOtherStacksAreEmpty(); } protected override Expression[] GetValues(int count) { var values = new Expression[count]; for (var i = 0; i < count; i++) values[i] = new CodeNoopExpression(); return values; } protected override Expression[] Get2Different() { return new Expression[] { new CodeNoopExpression(), new CodeNullExpression() }; } protected override void CheckOtherStacksAreEmpty() { TestStackCounts(codeStack: null); } } }