- Timestamp:
- 12/20/16 22:57:11 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests
- Files:
-
- 3 added
- 1 deleted
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Benchmark/Example.cs
r14392 r14513 1 namespace HeuristicLab.Tests.Benchmark 2 { 3 public class Example<Tin, Tout> 4 { 5 public Tin Input { get; set; } 6 public Tout Output { get; set; } 7 } 1 namespace HeuristicLab.Tests.Benchmark { 2 public class Example<Tin, Tout> { 3 public Tin Input { get; set; } 4 5 public Tout Output { get; set; } 6 } 8 7 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Benchmark/ExampleConverter.cs
r14398 r14513 1 using System.Linq; 1 namespace HeuristicLab.Tests.Benchmark 2 { 3 using System.Linq; 2 4 3 namespace HeuristicLab.Tests.Benchmark 4 { 5 public static class ExampleConverter 5 public static class ExampleConverter 6 { 7 private const char ArrayValueSeparator = ' '; 8 9 private static readonly char[] arraySymbolTrim = { '[', ']' }; 10 11 public static long Integer(string str) 6 12 { 7 private const char ArrayValueSeparator = ' '; 13 return long.Parse(str); 14 } 8 15 9 private static readonly char[] arraySymbolTrim = new[] { '[', ']' }; 16 public static long[] Integers(string str) 17 { 18 var values = 19 str.Trim(arraySymbolTrim).Split(ArrayValueSeparator).Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); 10 20 21 var result = new long[values.Count]; 11 22 12 public static long Integer(string str) 13 { 14 return long.Parse(str); 15 } 23 for (var i = 0; i < result.Length; i++) result[i] = Integer(values[i]); 16 24 17 public static long[] Integers(string str) 18 { 19 var values = str 20 .Trim(arraySymbolTrim) 21 .Split(ArrayValueSeparator) 22 .Where(s => !string.IsNullOrWhiteSpace(s)) 23 .ToList(); 24 25 var result = new long[values.Count]; 26 27 for (var i = 0; i < result.Length; i++) 28 { 29 result[i] = Integer(values[i]); 30 } 31 32 return result; 33 } 25 return result; 34 26 } 27 } 35 28 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Benchmark/Problem/CountOdds.cs
r14398 r14513 1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Threading.Tasks; 6 using HeuristicLab.Algorithms.PushGP.Expressions; 7 using HeuristicLab.Algorithms.PushGP.Interpreter; 8 using Microsoft.VisualStudio.TestTools.UnitTesting; 1 namespace HeuristicLab.Tests.Benchmark.Problem { 2 using System; 3 using System.Collections.Generic; 4 using System.Diagnostics; 5 using System.Linq; 6 using System.Threading.Tasks; 9 7 10 namespace HeuristicLab.Tests.Benchmark.Problem 11 { 12 [TestClass] 13 public class CountOdds : Problem<long[], long> 14 { 15 public const string ProblemFileName = "examples-count-odds.csv"; 8 using HeuristicLab.Algorithms.PushGP.Data.Random; 9 using HeuristicLab.Algorithms.PushGP.Expressions; 10 using HeuristicLab.Algorithms.PushGP.Generators; 11 using HeuristicLab.Algorithms.PushGP.Interpreter; 16 12 17 public CountOdds() 18 { 19 Examples = GetExamples(ProblemFileName, ExampleConverter.Integers, ExampleConverter.Integer); 13 using Microsoft.VisualStudio.TestTools.UnitTesting; 14 15 [TestClass] 16 public class CountOdds : Problem<long[], long> { 17 public CountOdds() { 18 this.Examples = this.GetExamples( 19 "examples-count-odds.csv", 20 ExampleConverter.Integers, 21 ExampleConverter.Integer); 22 } 23 24 [TestMethod] 25 [TestProperty("Time", "Medium")] 26 [TestCategory("ProblemTest")] 27 public void RandomWalk() { 28 var maxProgramSizeLimit = 100; 29 var iterations = 4000; 30 var best = double.MaxValue; 31 var globalExecCounter = 0; 32 var lockObj = new object(); 33 var lockCount = new object(); 34 RandomFactory.Seed = 1337; 35 36 Expression bestProgram = null; 37 var config = new Configuration { EvalPushLimit = 500 }; 38 var pool = new PushGpInterpreterPool(config); 39 40 Parallel.For(0, iterations, i => { 41 42 var execCounter = 0; 43 var program = CodeGenerator.RandomProgram(maxProgramSizeLimit); 44 var results = new List<long>(); 45 46 using (var interpreter = pool.GetInstance()) { 47 foreach (var example in this.Examples) { 48 if (example.Input.Length > 0) interpreter.IntegerStack.Push(example.Input); 49 50 interpreter.Interpret(program); 51 52 if (!interpreter.IntegerStack.IsEmpty) { 53 var diff = example.Output - interpreter.IntegerStack.Top; 54 var value = diff == long.MinValue ? 10000 : Math.Min(Math.Abs(diff), 10000); 55 56 results.Add(value); 57 } 58 59 execCounter += interpreter.ExecCounter; 60 interpreter.Clear(); 61 } 20 62 } 21 63 22 [TestMethod] 23 [TestProperty("Time", "Medium")] 24 [TestCategory("ProblemTest")] 25 public void RandomWalk() 26 { 27 var maxProgramSizeLimit = 1000; 28 var iterations = 20000; 29 var best = double.MaxValue; 30 Expression bestProgram = null; 31 var lockObj = new object(); 32 var globalExecCounter = 0; 64 lock (lockCount) { 65 globalExecCounter += execCounter; 66 } 33 67 68 if (results.Count == 0) return; 34 69 35 Parallel.For(0, iterations, async i => 36 { 37 var interpreter = new PushGPInterpreter(new Configuration 38 { 39 EvalPushLimit = 10000 40 }); 70 var avg = results.Average(); 41 71 42 var execCounter = 0; 43 var program = interpreter.CodeGenerator.RandomProgram(maxProgramSizeLimit); 44 var results = new List<long>(); 72 if (avg >= best) return; 45 73 46 foreach (var example in Examples) 47 { 48 if (example.Input.Length > 0) 49 { 50 interpreter.IntegerStack.Push(example.Input); 51 } 74 lock (lockObj) { 75 if (avg < best) { 76 best = avg; 77 bestProgram = program; 78 } 79 } 80 }); 52 81 53 await interpreter.InterpretAsync(program); 54 55 if (!interpreter.IntegerStack.IsEmpty) 56 { 57 var value = Math.Abs(example.Output - interpreter.IntegerStack.Top); 58 59 results.Add(Math.Min(value, 1000)); 60 } 61 62 execCounter += interpreter.ExecCounter; 63 interpreter.Clear(); 64 } 65 66 if (!results.Any()) 67 return; 68 69 var avg = results.Average(); 70 71 lock (lockObj) 72 { 73 if (avg < best) 74 { 75 best = avg; 76 bestProgram = program; 77 globalExecCounter += execCounter; 78 } 79 } 80 }); 81 82 Debug.WriteLine("Best: {0}", best); 83 Debug.WriteLine("Program: {0}", bestProgram); 84 Debug.WriteLine("ExecCounter: {0}", globalExecCounter); 85 } 82 Debug.WriteLine("Best: {0}", best); 83 Debug.WriteLine("Program: {0}", bestProgram); 84 Debug.WriteLine("ExecCounter: {0}", globalExecCounter); 86 85 } 86 } 87 87 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Benchmark/Problem/Problem.cs
r14392 r14513 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.IO.Compression; 5 using System.Linq; 6 using System.Reflection; 7 using System.Text.RegularExpressions; 1 namespace HeuristicLab.Tests.Benchmark.Problem 2 { 3 using System; 4 using System.Collections.Generic; 5 using System.IO; 6 using System.IO.Compression; 7 using System.Linq; 8 using System.Reflection; 9 using System.Text.RegularExpressions; 8 10 9 namespace HeuristicLab.Tests.Benchmark.Problem 10 { 11 public class Problem<Tin, Tout> 11 public class Problem<Tin, Tout> 12 { 13 public const char ExampleSeparator = ','; 14 15 public const string ArchiveFileName = "BenchmarkExamples.zip"; 16 17 private static readonly string instanceArchiveName = GetResourceName(ArchiveFileName); 18 19 protected List<Example<Tin, Tout>> Examples { get; set; } 20 21 protected List<Example<Tin, Tout>> GetExamples( 22 string problemName, 23 Converter<string, Tin> inputConverter, 24 Converter<string, Tout> outputConverter) 12 25 { 13 public const char ExampleSeparator = ','; 14 public const string ArchiveFileName = "BenchmarkExamples.zip"; 26 using (var file = this.GetType().Assembly.GetManifestResourceStream(instanceArchiveName)) 27 { 28 using (var archive = new ZipArchive(file, ZipArchiveMode.Read)) 29 { 30 var entry = archive.Entries.SingleOrDefault(x => x.Name == problemName); 15 31 16 private static string instanceArchiveName = GetResourceName(ArchiveFileName); 32 using (var reader = new StreamReader(entry.Open())) 33 { 34 var examples = new List<Example<Tin, Tout>>(); 17 35 18 protected List<Example<Tin, Tout>> Examples { get; set; } 36 // skip training headline 37 if (reader.Peek() >= 0) reader.ReadLine(); 19 38 20 protected List<Example<Tin, Tout>> GetExamples(string problemName, Converter<string, Tin> inputConverter, Converter<string, Tout> outputConverter) 21 { 22 using (var file = GetType().Assembly.GetManifestResourceStream(instanceArchiveName)) 23 using (var archive = new ZipArchive(file, ZipArchiveMode.Read)) 39 while (reader.Peek() >= 0) 24 40 { 25 var entry = archive.Entries.SingleOrDefault(x => x.Name == problemName);41 var line = reader.ReadLine(); 26 42 27 using (var reader = new StreamReader(entry.Open())) 28 { 29 var examples = new List<Example<Tin, Tout>>(); 43 // return 44 if (line.StartsWith("test")) break; 30 45 31 // skip training headline 32 if (reader.Peek() >= 0) 33 { 34 reader.ReadLine(); 35 } 46 var values = line.Split(ExampleSeparator); 36 47 37 while (reader.Peek() >= 0)38 {39 var line = reader.ReadLine();48 examples.Add( 49 new Example<Tin, Tout> { Input = inputConverter(values[0]), Output = outputConverter(values[1]) }); 50 } 40 51 41 // return42 if (line.StartsWith("test"))43 {44 break;45 52 return examples; 53 } 54 } 55 } 56 } 46 57 47 var values = line.Split(ExampleSeparator); 48 49 examples.Add(new Example<Tin, Tout> 50 { 51 Input = inputConverter(values[0]), 52 Output = outputConverter(values[1]) 53 }); 54 } 55 56 return examples; 57 } 58 } 59 } 60 61 private static string GetResourceName(string fileName) 62 { 63 return Assembly 64 .GetExecutingAssembly() 65 .GetManifestResourceNames() 66 .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success) 67 .SingleOrDefault(); 68 } 58 private static string GetResourceName(string fileName) 59 { 60 return 61 Assembly.GetExecutingAssembly() 62 .GetManifestResourceNames() 63 .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success) 64 .SingleOrDefault(); 69 65 } 66 } 70 67 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/HeuristicLab.Tests.csproj
r14392 r14513 27 27 <ErrorReport>prompt</ErrorReport> 28 28 <WarningLevel>4</WarningLevel> 29 <LangVersion>5</LangVersion> 29 30 </PropertyGroup> 30 31 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> … … 74 75 <Compile Include="Interpreter\Expressions\CodeExpressionTests.cs" /> 75 76 <Compile Include="Interpreter\Expressions\IntegerExpressionTests.cs" /> 76 <Compile Include="Interpreter\InterpreterTest.cs" /> 77 <Compile Include="Interpreter\Expressions\StandardTests.cs" /> 78 <Compile Include="Interpreter\ExpressionTest.cs" /> 77 79 <Compile Include="Properties\AssemblyInfo.cs" /> 78 80 </ItemGroup> … … 86 88 <EmbeddedResource Include="Benchmark\Data\BenchmarkExamples.zip" /> 87 89 </ItemGroup> 88 <ItemGroup /> 90 <ItemGroup> 91 <Folder Include="Interpreter\Generator\" /> 92 </ItemGroup> 89 93 <Choose> 90 94 <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'"> -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/HeuristicLab.Tests.csproj.user
r14392 r14513 2 2 <Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 3 3 <PropertyGroup> 4 <ProjectView> ProjectFiles</ProjectView>4 <ProjectView>ShowAllFiles</ProjectView> 5 5 </PropertyGroup> 6 6 </Project> -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Interpreter/Expressions/BooleanExpressionTests.cs
r14398 r14513 1 using HeuristicLab.Algorithms.PushGP.Expressions; 2 using HeuristicLab.Algorithms.PushGP.Stack; 3 using Microsoft.VisualStudio.TestTools.UnitTesting; 1 namespace HeuristicLab.Tests.Interpreter.Expressions 2 { 3 using HeuristicLab.Algorithms.PushGP.Expressions; 4 using HeuristicLab.Algorithms.PushGP.Stack; 4 5 5 namespace HeuristicLab.Tests.Interpreter.Expressions 6 { 7 [TestClass] 8 public class BooleanExpressionTests : CommonTests<bool> 6 using Microsoft.VisualStudio.TestTools.UnitTesting; 7 8 [TestClass] 9 public class BooleanExpressionTests : CommonTests<bool> 10 { 11 protected override string TypeName 9 12 { 10 protected override string TypeName { get { return "BOOLEAN"; } } 11 protected override IStack<bool> Stack { get { return interpreter.BooleanStack; } } 13 get 14 { 15 return "BOOLEAN"; 16 } 17 } 12 18 13 [TestMethod] 14 [TestProperty("Time", "Short")] 15 [TestCategory("ExpressionTest")] 16 [TestCategory("BooleanExpressionTest")] 17 public void TestAnd() 18 { 19 interpreter.BooleanStack.Push(true, false); 20 interpreter.Interpret(new BooleanAndExpression()); 19 protected override IStack<bool> Stack 20 { 21 get 22 { 23 return this.interpreter.BooleanStack; 24 } 25 } 21 26 22 Assert.AreEqual(false, interpreter.BooleanStack.Top); 27 [TestMethod] 28 [TestProperty("Time", "Short")] 29 [TestCategory("ExpressionTest")] 30 [TestCategory("BooleanExpressionTest")] 31 public void TestAnd() 32 { 33 this.interpreter.BooleanStack.Push(true, false); 34 this.interpreter.Interpret(new BooleanAndExpression()); 23 35 24 TestStackCounts(booleanStack: 1); 25 } 36 Assert.AreEqual(false, this.interpreter.BooleanStack.Top); 26 37 27 [TestMethod] 28 [TestProperty("Time", "Short")] 29 [TestCategory("ExpressionTest")] 30 [TestCategory("BooleanExpressionTest")] 31 public void TestAndWithInsufficientArguments() 32 { 33 TestWithInsufficientArguments("AND", 1); 34 } 38 this.TestStackCounts(booleanStack: 1); 39 } 35 40 36 37 38 39 40 public void TestOr()41 42 interpreter.BooleanStack.Push(true, false);43 interpreter.Interpret(new BooleanOrExpression());41 [TestMethod] 42 [TestProperty("Time", "Short")] 43 [TestCategory("ExpressionTest")] 44 [TestCategory("BooleanExpressionTest")] 45 public void TestAndWithInsufficientArguments() 46 { 47 this.TestWithInsufficientArguments("AND", 1); 48 } 44 49 45 Assert.AreEqual(true, interpreter.BooleanStack.Top); 50 [TestMethod] 51 [TestProperty("Time", "Short")] 52 [TestCategory("ExpressionTest")] 53 [TestCategory("BooleanExpressionTest")] 54 public void TestOr() 55 { 56 this.interpreter.BooleanStack.Push(true, false); 57 this.interpreter.Interpret(new BooleanOrExpression()); 46 58 47 TestStackCounts(booleanStack: 1); 48 } 59 Assert.AreEqual(true, this.interpreter.BooleanStack.Top); 49 60 50 [TestMethod] 51 [TestProperty("Time", "Short")] 52 [TestCategory("ExpressionTest")] 53 [TestCategory("BooleanExpressionTest")] 54 public void TestOrWithInsufficientArguments() 55 { 56 TestWithInsufficientArguments("OR", 1); 57 } 61 this.TestStackCounts(booleanStack: 1); 62 } 58 63 59 60 61 62 63 public void TestNot()64 65 interpreter.BooleanStack.Push(true);66 interpreter.Interpret(new BooleanNotExpression());64 [TestMethod] 65 [TestProperty("Time", "Short")] 66 [TestCategory("ExpressionTest")] 67 [TestCategory("BooleanExpressionTest")] 68 public void TestOrWithInsufficientArguments() 69 { 70 this.TestWithInsufficientArguments("OR", 1); 71 } 67 72 68 Assert.AreEqual(false, interpreter.BooleanStack.Top); 73 [TestMethod] 74 [TestProperty("Time", "Short")] 75 [TestCategory("ExpressionTest")] 76 [TestCategory("BooleanExpressionTest")] 77 public void TestNot() 78 { 79 this.interpreter.BooleanStack.Push(true); 80 this.interpreter.Interpret(new BooleanNotExpression()); 69 81 70 TestStackCounts(booleanStack: 1); 71 } 82 Assert.AreEqual(false, this.interpreter.BooleanStack.Top); 72 83 73 [TestMethod] 74 [TestProperty("Time", "Short")] 75 [TestCategory("ExpressionTest")] 76 [TestCategory("BooleanExpressionTest")] 77 public void TestNotWithInsufficientArguments() 78 { 79 TestWithInsufficientArguments("NOT"); 80 } 84 this.TestStackCounts(booleanStack: 1); 85 } 81 86 82 83 84 85 86 public void TestFromFloat()87 88 interpreter.FloatStack.Push(2.0);89 interpreter.Interpret(new BooleanFromFloatExpression());87 [TestMethod] 88 [TestProperty("Time", "Short")] 89 [TestCategory("ExpressionTest")] 90 [TestCategory("BooleanExpressionTest")] 91 public void TestNotWithInsufficientArguments() 92 { 93 this.TestWithInsufficientArguments("NOT"); 94 } 90 95 91 Assert.AreEqual(true, interpreter.BooleanStack.Top); 96 [TestMethod] 97 [TestProperty("Time", "Short")] 98 [TestCategory("ExpressionTest")] 99 [TestCategory("BooleanExpressionTest")] 100 public void TestFromFloat() 101 { 102 this.interpreter.FloatStack.Push(2.0); 103 this.interpreter.Interpret(new BooleanFromFloatExpression()); 92 104 93 TestStackCounts(booleanStack: 1); 94 } 105 Assert.AreEqual(true, this.interpreter.BooleanStack.Top); 95 106 96 [TestMethod] 97 [TestProperty("Time", "Short")] 98 [TestCategory("ExpressionTest")] 99 [TestCategory("BooleanExpressionTest")] 100 public void TestFromFloatWithInsufficientArguments() 101 { 102 TestWithInsufficientArguments("FROMFLOAT"); 103 } 107 this.TestStackCounts(booleanStack: 1); 108 } 104 109 105 106 107 108 109 public void TestFromInteger()110 111 interpreter.IntegerStack.Push(2);112 interpreter.Interpret(new BooleanFromIntegerExpression());110 [TestMethod] 111 [TestProperty("Time", "Short")] 112 [TestCategory("ExpressionTest")] 113 [TestCategory("BooleanExpressionTest")] 114 public void TestFromFloatWithInsufficientArguments() 115 { 116 this.TestWithInsufficientArguments("FROMFLOAT"); 117 } 113 118 114 Assert.AreEqual(true, interpreter.BooleanStack.Top); 119 [TestMethod] 120 [TestProperty("Time", "Short")] 121 [TestCategory("ExpressionTest")] 122 [TestCategory("BooleanExpressionTest")] 123 public void TestFromInteger() 124 { 125 this.interpreter.IntegerStack.Push(2); 126 this.interpreter.Interpret(new BooleanFromIntegerExpression()); 115 127 116 TestStackCounts(booleanStack: 1); 117 } 128 Assert.AreEqual(true, this.interpreter.BooleanStack.Top); 118 129 119 [TestMethod] 120 [TestProperty("Time", "Short")] 121 [TestCategory("ExpressionTest")] 122 [TestCategory("BooleanExpressionTest")] 123 public void TestFromIntegerWithInsufficientArguments() 124 { 125 TestWithInsufficientArguments("FROMINTEGER"); 126 } 130 this.TestStackCounts(booleanStack: 1); 131 } 127 132 128 protected override bool[] GetValues(int count) 129 { 130 var values = new bool[count]; 133 [TestMethod] 134 [TestProperty("Time", "Short")] 135 [TestCategory("ExpressionTest")] 136 [TestCategory("BooleanExpressionTest")] 137 public void TestFromIntegerWithInsufficientArguments() 138 { 139 this.TestWithInsufficientArguments("FROMINTEGER"); 140 } 131 141 132 for (var i = 0; i < count; i++) 133 { 134 values[i] = i % 2 == 0; 135 } 142 protected override bool[] GetValues(int count) 143 { 144 var values = new bool[count]; 136 145 137 return values; 138 } 146 for (var i = 0; i < count; i++) values[i] = i % 2 == 0; 139 147 140 protected override void CheckOtherStacksAreEmpty() 141 { 142 TestStackCounts(booleanStack: null); 143 } 148 return values; 144 149 } 150 151 protected override void CheckOtherStacksAreEmpty() 152 { 153 this.TestStackCounts(booleanStack: null); 154 } 155 } 145 156 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Interpreter/Expressions/CodeExpressionTests.cs
r14398 r14513 1 using HeuristicLab.Algorithms.PushGP; 2 using HeuristicLab.Algorithms.PushGP.Expressions; 3 using HeuristicLab.Algorithms.PushGP.Generators; 4 using HeuristicLab.Algorithms.PushGP.Stack; 5 using Microsoft.VisualStudio.TestTools.UnitTesting; 6 7 namespace HeuristicLab.Tests.Interpreter.Expressions 8 { 9 [TestClass] 10 public class CodeExpressionTests : CommonTests<Expression> 1 namespace 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 11 12 { 12 protected override string TypeName { get { return "CODE"; } } 13 protected override IStack<Expression> Stack { get { return interpreter.CodeStack; } } 14 15 private CodeGenerator cg; 16 public CodeExpressionTests() 17 { 18 cg = new CodeGenerator(interpreter); 19 } 20 21 [TestMethod] 22 [TestProperty("Time", "Short")] 23 [TestCategory("ExpressionTest")] 24 [TestCategory("CodeExpressionTest")] 25 public void TestAppendLists() 26 { 27 var first = Parser.Parse("( A )"); 28 var second = Parser.Parse("( B )"); 29 var result = Parser.Parse("( A B )"); 30 31 interpreter.CodeStack.Push(second, first); 32 interpreter.Interpret(new CodeAppendExpression()); 33 34 Assert.AreEqual(result, interpreter.CodeStack.Top); 35 CheckOtherStacksAreEmpty(); 36 } 37 38 [TestMethod] 39 [TestProperty("Time", "Short")] 40 [TestCategory("ExpressionTest")] 41 [TestCategory("CodeExpressionTest")] 42 public void TestAppendListAndLiteral() 43 { 44 var first = Parser.Parse("( A )"); 45 var second = Parser.Parse("5"); 46 var result = Parser.Parse("( A 5 )"); 47 48 interpreter.CodeStack.Push(second, first); 49 interpreter.Interpret(new CodeAppendExpression()); 50 51 Assert.AreEqual(result, interpreter.CodeStack.Top); 52 CheckOtherStacksAreEmpty(); 53 } 54 55 [TestMethod] 56 [TestProperty("Time", "Short")] 57 [TestCategory("ExpressionTest")] 58 [TestCategory("CodeExpressionTest")] 59 public void TestAppendLiterals() 60 { 61 var first = Parser.Parse("4"); 62 var second = Parser.Parse("5"); 63 var result = Parser.Parse("( 4 5 )"); 64 65 interpreter.CodeStack.Push(second, first); 66 interpreter.Interpret(new CodeAppendExpression()); 67 68 Assert.AreEqual(result, interpreter.CodeStack.Top); 69 CheckOtherStacksAreEmpty(); 70 } 71 72 [TestMethod] 73 [TestProperty("Time", "Short")] 74 [TestCategory("ExpressionTest")] 75 [TestCategory("CodeExpressionTest")] 76 public void TestAtomList() 77 { 78 var first = Parser.Parse("( A )"); 79 80 interpreter.CodeStack.Push(first); 81 interpreter.Interpret(new CodeAtomExpression()); 82 83 Assert.AreEqual(false, interpreter.BooleanStack.Top); 84 TestStackCounts(booleanStack: 1); 85 } 86 87 [TestMethod] 88 [TestProperty("Time", "Short")] 89 [TestCategory("ExpressionTest")] 90 [TestCategory("CodeExpressionTest")] 91 public void TestAtomLiteral() 92 { 93 var first = Parser.Parse("5"); 94 95 interpreter.CodeStack.Push(first); 96 interpreter.Interpret(new CodeAtomExpression()); 97 98 Assert.AreEqual(true, interpreter.BooleanStack.Top); 99 TestStackCounts(booleanStack: 1); 100 } 101 102 [TestMethod] 103 [TestProperty("Time", "Short")] 104 [TestCategory("ExpressionTest")] 105 [TestCategory("CodeExpressionTest")] 106 public void TestAtomSingleInstruction() 107 { 108 var first = Parser.Parse("INTEGER.+"); 109 110 interpreter.CodeStack.Push(first); 111 interpreter.Interpret(new CodeAtomExpression()); 112 113 Assert.AreEqual(true, interpreter.BooleanStack.Top); 114 TestStackCounts(booleanStack: 1); 115 } 116 117 [TestMethod] 118 [TestProperty("Time", "Short")] 119 [TestCategory("ExpressionTest")] 120 [TestCategory("CodeExpressionTest")] 121 public void TestCarList() 122 { 123 var first = Parser.Parse("( A B C )"); 124 var result = Parser.Parse("A"); 125 126 interpreter.CodeStack.Push(first); 127 interpreter.Interpret(new CodeCarExpression()); 128 129 Assert.AreEqual(result, interpreter.CodeStack.Top); 130 CheckOtherStacksAreEmpty(); 131 } 132 133 [TestMethod] 134 [TestProperty("Time", "Short")] 135 [TestCategory("ExpressionTest")] 136 [TestCategory("CodeExpressionTest")] 137 public void TestCarNoList() 138 { 139 var first = Parser.Parse("5"); 140 var result = Parser.Parse("5"); 141 142 interpreter.CodeStack.Push(first); 143 interpreter.Interpret(new CodeCarExpression()); 144 145 Assert.AreEqual(result, interpreter.CodeStack.Top); 146 CheckOtherStacksAreEmpty(); 147 } 148 149 [TestMethod] 150 [TestProperty("Time", "Short")] 151 [TestCategory("ExpressionTest")] 152 [TestCategory("CodeExpressionTest")] 153 public void TestCdrList() 154 { 155 var first = Parser.Parse("( A B C )"); 156 var result = Parser.Parse("( B C )"); 157 158 interpreter.CodeStack.Push(first); 159 interpreter.Interpret(new CodeCdrExpression()); 160 161 Assert.AreEqual(result, interpreter.CodeStack.Top); 162 CheckOtherStacksAreEmpty(); 163 } 164 165 [TestMethod] 166 [TestProperty("Time", "Short")] 167 [TestCategory("ExpressionTest")] 168 [TestCategory("CodeExpressionTest")] 169 public void TestCdrNoList() 170 { 171 var first = Parser.Parse("5"); 172 var result = Parser.Parse("( )"); 173 174 interpreter.CodeStack.Push(first); 175 interpreter.Interpret(new CodeCdrExpression()); 176 177 Assert.AreEqual(result, interpreter.CodeStack.Top); 178 CheckOtherStacksAreEmpty(); 179 } 180 181 [TestMethod] 182 [TestProperty("Time", "Short")] 183 [TestCategory("ExpressionTest")] 184 [TestCategory("CodeExpressionTest")] 185 public void TestCons() 186 { 187 var first = Parser.Parse("( B C )"); 188 var second = Parser.Parse("A"); 189 var result = Parser.Parse("( A B C )"); 190 191 interpreter.CodeStack.Push(second, first); 192 interpreter.Interpret(new CodeConsExpression()); 193 194 Assert.AreEqual(result, interpreter.CodeStack.Top); 195 CheckOtherStacksAreEmpty(); 196 } 197 198 [TestMethod] 199 [TestProperty("Time", "Short")] 200 [TestCategory("ExpressionTest")] 201 [TestCategory("CodeExpressionTest")] 202 public void TestContainsTrue() 203 { 204 var first = Parser.Parse("A"); 205 var second = Parser.Parse("( A B C )"); 206 207 interpreter.CodeStack.Push(second, first); 208 interpreter.Interpret(new CodeContainsExpression()); 209 210 Assert.AreEqual(true, interpreter.BooleanStack.Top); 211 TestStackCounts(booleanStack: 1); 212 } 213 214 [TestMethod] 215 [TestProperty("Time", "Short")] 216 [TestCategory("ExpressionTest")] 217 [TestCategory("CodeExpressionTest")] 218 public void TestContainsFalse() 219 { 220 var first = Parser.Parse("D"); 221 var second = Parser.Parse("( A B C )"); 222 223 interpreter.CodeStack.Push(second, first); 224 interpreter.Interpret(new CodeContainsExpression()); 225 226 Assert.AreEqual(false, interpreter.BooleanStack.Top); 227 TestStackCounts(booleanStack: 1); 228 } 229 230 [TestMethod] 231 [TestProperty("Time", "Short")] 232 [TestCategory("ExpressionTest")] 233 [TestCategory("CodeExpressionTest")] 234 public void TestContainer() 235 { 236 var first = Parser.Parse("( A )"); 237 var second = Parser.Parse("( B ( C ( A ) ) ( D ( A ) ) )"); 238 var result = Parser.Parse("( C ( A ) )"); 239 240 interpreter.CodeStack.Push(second, first); 241 interpreter.Interpret(new CodeContainerExpression()); 242 243 Assert.AreEqual(result, interpreter.CodeStack.Top); 244 CheckOtherStacksAreEmpty(); 245 } 246 247 [TestMethod] 248 [TestProperty("Time", "Short")] 249 [TestCategory("ExpressionTest")] 250 [TestCategory("CodeExpressionTest")] 251 public void TestDefinition() 252 { 253 var code = Parser.Parse("( A )"); 254 255 interpreter.CustomExpressions.Add("Test", code); 256 interpreter.NameStack.Push("Test"); 257 258 interpreter.Interpret(new CodeDefinitionExpression()); 259 260 Assert.AreEqual(code, interpreter.CodeStack.Top); 261 CheckOtherStacksAreEmpty(); 262 } 263 264 [TestMethod] 265 [TestProperty("Time", "Short")] 266 [TestCategory("ExpressionTest")] 267 [TestCategory("CodeExpressionTest")] 268 public void TestDefinitionWithoutMatchingName() 269 { 270 var code = Parser.Parse("( A )"); 271 var program = new CodeDefinitionExpression(); 272 273 interpreter.CustomExpressions.Add("Test1", code); 274 interpreter.NameStack.Push("Test2"); 275 interpreter.Interpret(program); 276 277 TestStackCounts(nameStack: 1); 278 } 279 280 [TestMethod] 281 [TestProperty("Time", "Short")] 282 [TestCategory("ExpressionTest")] 283 [TestCategory("CodeExpressionTest")] 284 public void TestDiscrepancy() 285 { 286 var first = Parser.Parse("( A B )"); 287 var second = Parser.Parse("( B C )"); 288 289 interpreter.CodeStack.Push(second, first); 290 interpreter.Interpret(new CodeDiscrepancyExpression()); 291 292 Assert.AreEqual(2, interpreter.IntegerStack.Top); 293 TestStackCounts(integerStack: 1); 294 } 295 296 [TestMethod] 297 [TestProperty("Time", "Short")] 298 [TestCategory("ExpressionTest")] 299 [TestCategory("CodeExpressionTest")] 300 public void TestDiscrepancyAllEqual() 301 { 302 var first = Parser.Parse("( A B )"); 303 var second = Parser.Parse("( B A )"); 304 305 interpreter.CodeStack.Push(second, first); 306 interpreter.Interpret(new CodeDiscrepancyExpression()); 307 308 Assert.AreEqual(0, interpreter.IntegerStack.Top); 309 TestStackCounts(integerStack: 1); 310 } 311 312 [TestMethod] 313 [TestProperty("Time", "Short")] 314 [TestCategory("ExpressionTest")] 315 [TestCategory("CodeExpressionTest")] 316 public void TestDiscrepancyComplex() 317 { 318 var first = Parser.Parse("( A A B C D A )"); 319 var second = Parser.Parse("( A A B D E )"); 320 321 interpreter.CodeStack.Push(second, first); 322 interpreter.Interpret(new CodeDiscrepancyExpression()); 323 324 Assert.AreEqual(3, interpreter.IntegerStack.Top); 325 TestStackCounts(integerStack: 1); 326 } 327 328 [TestMethod] 329 [TestProperty("Time", "Short")] 330 [TestCategory("ExpressionTest")] 331 [TestCategory("CodeExpressionTest")] 332 public void TestDiscrepancWithLists() 333 { 334 var first = Parser.Parse("( ( A ) A ( B ) C D A )"); 335 var second = Parser.Parse("( A ( A ) B D E )"); 336 337 interpreter.CodeStack.Push(second, first); 338 interpreter.Interpret(new CodeDiscrepancyExpression()); 339 340 Assert.AreEqual(5, interpreter.IntegerStack.Top); 341 TestStackCounts(integerStack: 1); 342 } 343 344 [TestMethod] 345 [TestProperty("Time", "Short")] 346 [TestCategory("ExpressionTest")] 347 [TestCategory("CodeExpressionTest")] 348 public void TestDo() 349 { 350 var prog = Parser.Parse("( 10 )"); 351 352 interpreter.CodeStack.Push(prog); 353 interpreter.Interpret(new CodeDoExpression()); 354 355 Assert.AreEqual(10, interpreter.IntegerStack.Top); 356 TestStackCounts(integerStack: 1); 357 } 358 359 [TestMethod] 360 [TestProperty("Time", "Short")] 361 [TestCategory("ExpressionTest")] 362 [TestCategory("CodeExpressionTest")] 363 public void TestDoX() 364 { 365 var prog = Parser.Parse("( 10 )"); 366 367 interpreter.CodeStack.Push(prog); 368 interpreter.Interpret(new CodeDoXExpression()); 369 370 Assert.AreEqual(10, interpreter.IntegerStack.Top); 371 TestStackCounts(integerStack: 1); 372 } 373 374 [TestMethod] 375 [TestProperty("Time", "Short")] 376 [TestCategory("ExpressionTest")] 377 [TestCategory("CodeExpressionTest")] 378 public void TestDoCount() 379 { 380 var prog = Parser.Parse("INTEGER.+"); 381 382 interpreter.IntegerStack.Push(3); 383 interpreter.CodeStack.Push(prog); 384 interpreter.Interpret(new CodeDoCountExpression()); 385 386 Assert.AreEqual(6, interpreter.IntegerStack.Top); 387 TestStackCounts(integerStack: 1); 388 } 389 390 [TestMethod] 391 [TestProperty("Time", "Short")] 392 [TestCategory("ExpressionTest")] 393 [TestCategory("CodeExpressionTest")] 394 public void TestDoCountWithNegativeCounter() 395 { 396 var prog = Parser.Parse("INTEGER.+"); 397 398 interpreter.IntegerStack.Push(-1); 399 interpreter.CodeStack.Push(prog); 400 interpreter.Interpret(new CodeDoCountExpression()); 401 402 Assert.AreEqual(-1, interpreter.IntegerStack.Top); 403 TestStackCounts(codeStack: 1, integerStack: 1); 404 } 405 406 [TestMethod] 407 [TestProperty("Time", "Short")] 408 [TestCategory("ExpressionTest")] 409 [TestCategory("CodeExpressionTest")] 410 public void TestDoRange() 411 { 412 var prog = Parser.Parse("INTEGER.+"); 413 414 interpreter.IntegerStack.Push(3, 5); 415 interpreter.CodeStack.Push(prog); 416 interpreter.Interpret(new CodeDoRangeExpression()); 417 418 Assert.AreEqual(12, interpreter.IntegerStack.Top); 419 TestStackCounts(integerStack: 1); 420 } 421 422 [TestMethod] 423 [TestProperty("Time", "Short")] 424 [TestCategory("ExpressionTest")] 425 [TestCategory("CodeExpressionTest")] 426 public void TestDoRangeWithEqualIndecators() 427 { 428 var prog = Parser.Parse("INTEGER.+"); 429 430 interpreter.IntegerStack.Push(3, 3); 431 interpreter.CodeStack.Push(prog); 432 interpreter.Interpret(new CodeDoRangeExpression()); 433 434 Assert.AreEqual(3, interpreter.IntegerStack.Top); 435 TestStackCounts(codeStack: 1, integerStack: 2); 436 } 437 438 [TestMethod] 439 [TestProperty("Time", "Short")] 440 [TestCategory("ExpressionTest")] 441 [TestCategory("CodeExpressionTest")] 442 public void TestDoRangeWithNegativeIndecators() 443 { 444 var prog = Parser.Parse("INTEGER.+"); 445 446 interpreter.IntegerStack.Push(-3, -5); 447 interpreter.CodeStack.Push(prog); 448 interpreter.Interpret(new CodeDoRangeExpression()); 449 450 Assert.AreEqual(-12, interpreter.IntegerStack.Top); 451 TestStackCounts(integerStack: 1); 452 } 453 454 [TestMethod] 455 [TestProperty("Time", "Short")] 456 [TestCategory("ExpressionTest")] 457 [TestCategory("CodeExpressionTest")] 458 public void TestDoTimes() 459 { 460 var prog = Parser.Parse("( INTEGER.DUP INTEGER.+ )"); 461 462 interpreter.IntegerStack.Push(2, 3); 463 interpreter.CodeStack.Push(prog); 464 interpreter.Interpret(new CodeDoTimesExpression()); 465 466 Assert.AreEqual(32, interpreter.IntegerStack.Top); 467 TestStackCounts(integerStack: 1); 468 } 469 470 [TestMethod] 471 [TestProperty("Time", "Short")] 472 [TestCategory("ExpressionTest")] 473 [TestCategory("CodeExpressionTest")] 474 public void TestDoTimesWithNegativeCounter() 475 { 476 var prog = Parser.Parse("( INTEGER.DUP INTEGER.+ )"); 477 478 interpreter.IntegerStack.Push(2, -3); 479 interpreter.CodeStack.Push(prog); 480 interpreter.Interpret(new CodeDoTimesExpression()); 481 482 Assert.AreEqual(-3, interpreter.IntegerStack.Top); 483 TestStackCounts(codeStack: 1, integerStack: 2); 484 } 485 486 [TestMethod] 487 [TestProperty("Time", "Short")] 488 [TestCategory("ExpressionTest")] 489 [TestCategory("CodeExpressionTest")] 490 public void TestNestedDoRange() 491 { 492 interpreter.Interpret("( 0 2 CODE.QUOTE ( 1 INTEGER.+ 0 3 CODE.QUOTE ( 1 INTEGER.+ INTEGER.* ) CODE.DO*RANGE INTEGER.+ ) CODE.DO*RANGE )"); 493 494 Assert.AreEqual(144, interpreter.IntegerStack.Top); 495 TestStackCounts(integerStack: 1); 496 } 497 498 [TestMethod] 499 [TestProperty("Time", "Short")] 500 [TestCategory("ExpressionTest")] 501 [TestCategory("CodeExpressionTest")] 502 public void TestNestedDoCount() 503 { 504 interpreter.Interpret("( 2 CODE.QUOTE ( 1 INTEGER.+ 3 CODE.QUOTE ( 1 INTEGER.+ INTEGER.* ) CODE.DO*COUNT INTEGER.+ ) CODE.DO*COUNT )"); 505 506 Assert.AreEqual(144, interpreter.IntegerStack.Top); 507 TestStackCounts(integerStack: 1); 508 } 509 510 [TestMethod] 511 [TestProperty("Time", "Short")] 512 [TestCategory("ExpressionTest")] 513 [TestCategory("CodeExpressionTest")] 514 public void TestNestedDoTimes() 515 { 516 interpreter.Interpret("( 3 CODE.QUOTE ( 2 3 CODE.QUOTE ( 2 INTEGER.* ) CODE.DO*TIMES INTEGER.+ ) CODE.DO*TIMES )"); 517 518 Assert.AreEqual(128, interpreter.IntegerStack.Top); 519 TestStackCounts(integerStack: 1); 520 } 521 522 [TestMethod] 523 [TestProperty("Time", "Short")] 524 [TestCategory("ExpressionTest")] 525 [TestCategory("CodeExpressionTest")] 526 public void TestExtract() 527 { 528 var prog = Parser.Parse("( A ( B C ( D ) E ) F )"); 529 var result = Parser.Parse("( D )"); 530 531 interpreter.IntegerStack.Push(5); 532 interpreter.CodeStack.Push(prog); 533 interpreter.Interpret(new CodeExtractExpression()); 534 535 Assert.AreEqual(result, interpreter.CodeStack.Top); 536 CheckOtherStacksAreEmpty(); 537 } 538 539 [TestMethod] 540 [TestProperty("Time", "Short")] 541 [TestCategory("ExpressionTest")] 542 [TestCategory("CodeExpressionTest")] 543 public void TestExtractInteger() 544 { 545 var prog = Parser.Parse("( A ( 5 C ( 4.2 ) INTEGER.+ ) FALSE )"); 546 var result = Parser.Parse("5"); 547 548 interpreter.IntegerStack.Push(3); 549 interpreter.CodeStack.Push(prog); 550 interpreter.Interpret(new CodeExtractExpression()); 551 552 Assert.AreEqual(result, interpreter.CodeStack.Top); 553 CheckOtherStacksAreEmpty(); 554 } 555 556 [TestMethod] 557 [TestProperty("Time", "Short")] 558 [TestCategory("ExpressionTest")] 559 [TestCategory("CodeExpressionTest")] 560 public void TestExtractFloat() 561 { 562 var prog = Parser.Parse("( A ( 5 C ( 4.2 ) INTEGER.+ ) FALSE )"); 563 var result = Parser.Parse("4.2"); 564 565 interpreter.IntegerStack.Push(6); 566 interpreter.CodeStack.Push(prog); 567 interpreter.Interpret(new CodeExtractExpression()); 568 569 Assert.AreEqual(result, interpreter.CodeStack.Top); 570 CheckOtherStacksAreEmpty(); 571 } 572 573 [TestMethod] 574 [TestProperty("Time", "Short")] 575 [TestCategory("ExpressionTest")] 576 [TestCategory("CodeExpressionTest")] 577 public void TestExtractBoolean() 578 { 579 var prog = Parser.Parse("( A ( 5 C ( 4.2 ) INTEGER.+ ) FALSE )"); 580 var result = Parser.Parse("FALSE"); 581 582 interpreter.IntegerStack.Push(8); 583 interpreter.CodeStack.Push(prog); 584 interpreter.Interpret(new CodeExtractExpression()); 585 586 Assert.AreEqual(result, interpreter.CodeStack.Top); 587 CheckOtherStacksAreEmpty(); 588 } 589 590 [TestMethod] 591 [TestProperty("Time", "Short")] 592 [TestCategory("ExpressionTest")] 593 [TestCategory("CodeExpressionTest")] 594 public void TestExtractInstruction() 595 { 596 var prog = Parser.Parse("( A ( 5 C ( 4.2 ) INTEGER.+ ) FALSE )"); 597 var result = Parser.Parse("INTEGER.+"); 598 599 interpreter.IntegerStack.Push(7); 600 interpreter.CodeStack.Push(prog); 601 interpreter.Interpret(new CodeExtractExpression()); 602 603 Assert.AreEqual(result, interpreter.CodeStack.Top); 604 CheckOtherStacksAreEmpty(); 605 } 606 607 [TestMethod] 608 [TestProperty("Time", "Short")] 609 [TestCategory("ExpressionTest")] 610 [TestCategory("CodeExpressionTest")] 611 public void TestFromBoolean() 612 { 613 var result = Parser.Parse("FALSE"); 614 interpreter.BooleanStack.Push(false); 615 interpreter.Interpret(new CodeFromBooleanExpression()); 616 617 Assert.AreEqual(result, interpreter.CodeStack.Top); 618 CheckOtherStacksAreEmpty(); 619 } 620 621 [TestMethod] 622 [TestProperty("Time", "Short")] 623 [TestCategory("ExpressionTest")] 624 [TestCategory("CodeExpressionTest")] 625 public void TestFromFloat() 626 { 627 var result = Parser.Parse("4.1"); 628 interpreter.FloatStack.Push(4.1); 629 interpreter.Interpret(new CodeFromFloatExpression()); 630 631 Assert.AreEqual(result, interpreter.CodeStack.Top); 632 CheckOtherStacksAreEmpty(); 633 } 634 635 [TestMethod] 636 [TestProperty("Time", "Short")] 637 [TestCategory("ExpressionTest")] 638 [TestCategory("CodeExpressionTest")] 639 public void TestFromInteger() 640 { 641 var result = Parser.Parse("4"); 642 interpreter.IntegerStack.Push(4); 643 interpreter.Interpret(new CodeFromIntegerExpression()); 644 645 Assert.AreEqual(result, interpreter.CodeStack.Top); 646 CheckOtherStacksAreEmpty(); 647 } 648 649 [TestMethod] 650 [TestProperty("Time", "Short")] 651 [TestCategory("ExpressionTest")] 652 [TestCategory("CodeExpressionTest")] 653 public void TestFromName() 654 { 655 var result = Parser.Parse("A"); 656 interpreter.NameStack.Push("A"); 657 interpreter.Interpret(new CodeFromNameExpression()); 658 659 Assert.AreEqual(result, interpreter.CodeStack.Top); 660 CheckOtherStacksAreEmpty(); 661 } 662 663 [TestMethod] 664 [TestProperty("Time", "Short")] 665 [TestCategory("ExpressionTest")] 666 [TestCategory("CodeExpressionTest")] 667 public void TestIfTrue() 668 { 669 var first = Parser.Parse("FALSCH"); 670 var second = Parser.Parse("WAHR"); 671 672 interpreter.BooleanStack.Push(true); 673 interpreter.CodeStack.Push(second, first); 674 interpreter.Interpret(new CodeIfExpression()); 675 676 Assert.AreEqual("WAHR", interpreter.NameStack.Top); 677 TestStackCounts(nameStack: 1); 678 } 679 680 [TestMethod] 681 [TestProperty("Time", "Short")] 682 [TestCategory("ExpressionTest")] 683 [TestCategory("CodeExpressionTest")] 684 public void TestIfFalse() 685 { 686 var first = Parser.Parse("FALSCH"); 687 var second = Parser.Parse("WAHR"); 688 689 interpreter.BooleanStack.Push(false); 690 interpreter.CodeStack.Push(second, first); 691 interpreter.Interpret(new CodeIfExpression()); 692 693 Assert.AreEqual("FALSCH", interpreter.NameStack.Top); 694 TestStackCounts(nameStack: 1); 695 } 696 697 [TestMethod] 698 [TestProperty("Time", "Short")] 699 [TestCategory("ExpressionTest")] 700 [TestCategory("CodeExpressionTest")] 701 public void TestInsert() 702 { 703 var first = Parser.Parse("( A ( B ) C D )"); 704 var second = Parser.Parse("( E )"); 705 var result = Parser.Parse("( A ( B ) ( E ) D )"); 706 707 interpreter.IntegerStack.Push(2); 708 interpreter.CodeStack.Push(second, first); 709 interpreter.Interpret(new CodeInsertExpression()); 710 711 Assert.AreEqual(result, interpreter.CodeStack.Top); 712 CheckOtherStacksAreEmpty(); 713 } 714 715 [TestMethod] 716 [TestProperty("Time", "Short")] 717 [TestCategory("ExpressionTest")] 718 [TestCategory("CodeExpressionTest")] 719 public void TestInsertWithNegativeN() 720 { 721 var first = Parser.Parse("( A ( B ) C D )"); 722 var second = Parser.Parse("( E )"); 723 var result = Parser.Parse("( A ( B ) ( E ) D )"); 724 725 interpreter.IntegerStack.Push(-2); 726 interpreter.CodeStack.Push(second, first); 727 interpreter.Interpret(new CodeInsertExpression()); 728 729 Assert.AreEqual(result, interpreter.CodeStack.Top); 730 CheckOtherStacksAreEmpty(); 731 } 732 733 [TestMethod] 734 [TestProperty("Time", "Short")] 735 [TestCategory("ExpressionTest")] 736 [TestCategory("CodeExpressionTest")] 737 public void TestInsertWithTooBigN() 738 { 739 var first = Parser.Parse("( A ( B ) C D )"); 740 var second = Parser.Parse("( E )"); 741 var result = Parser.Parse("( A ( B ) ( E ) D )"); 742 743 interpreter.IntegerStack.Push(10); 744 interpreter.CodeStack.Push(second, first); 745 interpreter.Interpret(new CodeInsertExpression()); 746 747 Assert.AreEqual(result, interpreter.CodeStack.Top); 748 CheckOtherStacksAreEmpty(); 749 } 750 751 [TestMethod] 752 [TestProperty("Time", "Short")] 753 [TestCategory("ExpressionTest")] 754 [TestCategory("CodeExpressionTest")] 755 public void TestList() 756 { 757 var first = Parser.Parse("A"); 758 var second = Parser.Parse("( B )"); 759 var result = Parser.Parse("( A ( B ) )"); 760 761 interpreter.CodeStack.Push(second, first); 762 interpreter.Interpret(new CodeListExpression()); 763 764 Assert.AreEqual(result, interpreter.CodeStack.Top); 765 CheckOtherStacksAreEmpty(); 766 } 767 768 [TestMethod] 769 [TestProperty("Time", "Short")] 770 [TestCategory("ExpressionTest")] 771 [TestCategory("CodeExpressionTest")] 772 public void TestMemberTrue() 773 { 774 var first = Parser.Parse("( A B ( B ) C "); 775 var second = Parser.Parse("( B )"); 776 777 interpreter.CodeStack.Push(second, first); 778 interpreter.Interpret(new CodeMemberExpression()); 779 780 Assert.AreEqual(true, interpreter.BooleanStack.Top); 781 TestStackCounts(booleanStack: 1); 782 } 783 784 [TestMethod] 785 [TestProperty("Time", "Short")] 786 [TestCategory("ExpressionTest")] 787 [TestCategory("CodeExpressionTest")] 788 public void TestMemberFalse() 789 { 790 var first = Parser.Parse("( A B C "); 791 var second = Parser.Parse("( B )"); 792 793 interpreter.CodeStack.Push(second, first); 794 interpreter.Interpret(new CodeMemberExpression()); 795 796 Assert.AreEqual(false, interpreter.BooleanStack.Top); 797 TestStackCounts(booleanStack: 1); 798 } 799 800 [TestMethod] 801 [TestProperty("Time", "Short")] 802 [TestCategory("ExpressionTest")] 803 [TestCategory("CodeExpressionTest")] 804 public void TestMemberIfNoList() 805 { 806 var first = Parser.Parse("B"); 807 var second = Parser.Parse("B"); 808 809 interpreter.CodeStack.Push(second, first); 810 interpreter.Interpret(new CodeMemberExpression()); 811 812 Assert.AreEqual(true, interpreter.BooleanStack.Top); 813 TestStackCounts(booleanStack: 1); 814 } 815 816 [TestMethod] 817 [TestProperty("Time", "Short")] 818 [TestCategory("ExpressionTest")] 819 [TestCategory("CodeExpressionTest")] 820 public void TestNoop() 821 { 822 interpreter.Interpret(new CodeNoopExpression()); 823 824 CheckOtherStacksAreEmpty(); 825 } 826 827 [TestMethod] 828 [TestProperty("Time", "Short")] 829 [TestCategory("ExpressionTest")] 830 [TestCategory("CodeExpressionTest")] 831 public void TestNthIfNoList() 832 { 833 var prog = Parser.Parse("A"); 834 var result = Parser.Parse("A"); 835 836 interpreter.IntegerStack.Push(3); 837 interpreter.CodeStack.Push(prog); 838 interpreter.Interpret(new CodeNthExpression()); 839 840 Assert.AreEqual(result, interpreter.CodeStack.Top); 841 CheckOtherStacksAreEmpty(); 842 } 843 844 [TestMethod] 845 [TestProperty("Time", "Short")] 846 [TestCategory("ExpressionTest")] 847 [TestCategory("CodeExpressionTest")] 848 public void TestNthIfEmpty() 849 { 850 var prog = Parser.Parse("( )"); 851 var result = Parser.Parse("( )"); 852 853 interpreter.IntegerStack.Push(3); 854 interpreter.CodeStack.Push(prog); 855 interpreter.Interpret(new CodeNthExpression()); 856 857 Assert.AreEqual(result, interpreter.CodeStack.Top); 858 CheckOtherStacksAreEmpty(); 859 } 860 861 [TestMethod] 862 [TestProperty("Time", "Short")] 863 [TestCategory("ExpressionTest")] 864 [TestCategory("CodeExpressionTest")] 865 public void TestNth() 866 { 867 var prog = Parser.Parse("( A B C D E )"); 868 var result = Parser.Parse("D"); 869 870 interpreter.IntegerStack.Push(3); 871 interpreter.CodeStack.Push(prog); 872 interpreter.Interpret(new CodeNthExpression()); 873 874 Assert.AreEqual(result, interpreter.CodeStack.Top); 875 CheckOtherStacksAreEmpty(); 876 } 877 878 [TestMethod] 879 [TestProperty("Time", "Short")] 880 [TestCategory("ExpressionTest")] 881 [TestCategory("CodeExpressionTest")] 882 public void TestNthWithTooBigN() 883 { 884 var prog = Parser.Parse("( A B C D E )"); 885 var result = Parser.Parse("D"); 886 887 interpreter.IntegerStack.Push(13); 888 interpreter.CodeStack.Push(prog); 889 interpreter.Interpret(new CodeNthExpression()); 890 891 Assert.AreEqual(result, interpreter.CodeStack.Top); 892 CheckOtherStacksAreEmpty(); 893 } 894 895 [TestMethod] 896 [TestProperty("Time", "Short")] 897 [TestCategory("ExpressionTest")] 898 [TestCategory("CodeExpressionTest")] 899 public void TestNthWithNegativeN() 900 { 901 var prog = Parser.Parse("( A B C D E )"); 902 var result = Parser.Parse("D"); 903 904 interpreter.IntegerStack.Push(-3); 905 interpreter.CodeStack.Push(prog); 906 interpreter.Interpret(new CodeNthExpression()); 907 908 Assert.AreEqual(result, interpreter.CodeStack.Top); 909 CheckOtherStacksAreEmpty(); 910 } 911 912 [TestMethod] 913 [TestProperty("Time", "Short")] 914 [TestCategory("ExpressionTest")] 915 [TestCategory("CodeExpressionTest")] 916 public void TestNthCdr() 917 { 918 var prog = Parser.Parse("( A B C D E )"); 919 var result = Parser.Parse("D"); 920 921 interpreter.IntegerStack.Push(2); 922 interpreter.CodeStack.Push(prog); 923 interpreter.Interpret(new CodeNthCdrExpression()); 924 925 Assert.AreEqual(result, interpreter.CodeStack.Top); 926 CheckOtherStacksAreEmpty(); 927 } 928 929 [TestMethod] 930 [TestProperty("Time", "Short")] 931 [TestCategory("ExpressionTest")] 932 [TestCategory("CodeExpressionTest")] 933 public void TestNthCdrWithTooBigN() 934 { 935 var prog = Parser.Parse("( A B C D E )"); 936 var result = Parser.Parse("D"); 937 938 interpreter.IntegerStack.Push(10); 939 interpreter.CodeStack.Push(prog); 940 interpreter.Interpret(new CodeNthCdrExpression()); 941 942 Assert.AreEqual(result, interpreter.CodeStack.Top); 943 CheckOtherStacksAreEmpty(); 944 } 945 946 [TestMethod] 947 [TestProperty("Time", "Short")] 948 [TestCategory("ExpressionTest")] 949 [TestCategory("CodeExpressionTest")] 950 public void TestNthCdrWithNegativeN() 951 { 952 var prog = Parser.Parse("( A B C D E )"); 953 var result = Parser.Parse("D"); 954 955 interpreter.IntegerStack.Push(-2); 956 interpreter.CodeStack.Push(prog); 957 interpreter.Interpret(new CodeNthCdrExpression()); 958 959 Assert.AreEqual(result, interpreter.CodeStack.Top); 960 CheckOtherStacksAreEmpty(); 961 } 962 963 [TestMethod] 964 [TestProperty("Time", "Short")] 965 [TestCategory("ExpressionTest")] 966 [TestCategory("CodeExpressionTest")] 967 public void TestNthCdrIfEmpty() 968 { 969 var prog = Parser.Parse("( )"); 970 var result = Parser.Parse("( )"); 971 972 interpreter.IntegerStack.Push(3); 973 interpreter.CodeStack.Push(prog); 974 interpreter.Interpret(new CodeNthCdrExpression()); 975 976 Assert.AreEqual(result, interpreter.CodeStack.Top); 977 CheckOtherStacksAreEmpty(); 978 } 979 980 [TestMethod] 981 [TestProperty("Time", "Short")] 982 [TestCategory("ExpressionTest")] 983 [TestCategory("CodeExpressionTest")] 984 public void TestNthCdrIfNoList() 985 { 986 var prog = Parser.Parse("A"); 987 var result = Parser.Parse("A"); 988 989 interpreter.IntegerStack.Push(3); 990 interpreter.CodeStack.Push(prog); 991 interpreter.Interpret(new CodeNthCdrExpression()); 992 993 Assert.AreEqual(result, interpreter.CodeStack.Top); 994 CheckOtherStacksAreEmpty(); 995 } 996 997 [TestMethod] 998 [TestProperty("Time", "Short")] 999 [TestCategory("ExpressionTest")] 1000 [TestCategory("CodeExpressionTest")] 1001 public void TestNullTrue() 1002 { 1003 var prog = Parser.Parse("( )"); 1004 1005 interpreter.CodeStack.Push(prog); 1006 interpreter.Interpret(new CodeNullExpression()); 1007 1008 Assert.AreEqual(true, interpreter.BooleanStack.Top); 1009 TestStackCounts(booleanStack: 1); 1010 } 1011 1012 [TestMethod] 1013 [TestProperty("Time", "Short")] 1014 [TestCategory("ExpressionTest")] 1015 [TestCategory("CodeExpressionTest")] 1016 public void TestNullFalse() 1017 { 1018 var prog = Parser.Parse("A"); 1019 1020 interpreter.CodeStack.Push(prog); 1021 interpreter.Interpret(new CodeNullExpression()); 1022 1023 Assert.AreEqual(false, interpreter.BooleanStack.Top); 1024 TestStackCounts(booleanStack: 1); 1025 } 1026 1027 [TestMethod] 1028 [TestProperty("Time", "Short")] 1029 [TestCategory("ExpressionTest")] 1030 [TestCategory("CodeExpressionTest")] 1031 public void TestPosition() 1032 { 1033 var first = Parser.Parse("( A B C D )"); 1034 var second = Parser.Parse("C"); 1035 1036 interpreter.CodeStack.Push(second, first); 1037 interpreter.Interpret(new CodePositionExpression()); 1038 1039 Assert.AreEqual(2, interpreter.IntegerStack.Top); 1040 TestStackCounts(integerStack: 1); 1041 } 1042 1043 [TestMethod] 1044 [TestProperty("Time", "Short")] 1045 [TestCategory("ExpressionTest")] 1046 [TestCategory("CodeExpressionTest")] 1047 public void TestQuote() 1048 { 1049 var result = Parser.Parse("A"); 1050 1051 interpreter.Interpret("( CODE.QUOTE A )"); 1052 1053 Assert.AreEqual(result, interpreter.CodeStack.Top); 1054 CheckOtherStacksAreEmpty(); 1055 } 1056 1057 [TestMethod] 1058 [TestProperty("Time", "Short")] 1059 [TestCategory("ExpressionTest")] 1060 [TestCategory("CodeExpressionTest")] 1061 public void TestSize() 1062 { 1063 var prog = Parser.Parse("( A ( B ( C ) ) ( D ) INTEGER.+ )"); 1064 1065 interpreter.CodeStack.Push(prog); 1066 interpreter.Interpret(new CodeSizeExpression()); 1067 1068 Assert.AreEqual(4, interpreter.IntegerStack.Top); 1069 TestStackCounts(integerStack: 1); 1070 } 1071 1072 [TestMethod] 1073 [TestProperty("Time", "Short")] 1074 [TestCategory("ExpressionTest")] 1075 [TestCategory("CodeExpressionTest")] 1076 public void TestSubstitude() 1077 { 1078 var first = Parser.Parse("( A B C D A B C D )"); 1079 var second = Parser.Parse("E"); 1080 var third = Parser.Parse("A"); 1081 var result = Parser.Parse("( E B C D E B C D )"); 1082 1083 interpreter.CodeStack.Push(third, second, first); 1084 interpreter.Interpret(new CodeSubstitutionExpression()); 1085 1086 Assert.AreEqual(result, interpreter.CodeStack.Top); 1087 CheckOtherStacksAreEmpty(); 1088 } 1089 1090 protected override Expression[] GetValues(int count) 1091 { 1092 var values = new Expression[count]; 1093 1094 for (var i = 0; i < count; i++) 1095 { 1096 values[i] = new CodeNoopExpression(); 1097 } 1098 1099 return values; 1100 } 1101 1102 protected override Expression[] Get2Different() 1103 { 1104 return new Expression[] 1105 { 1106 new CodeNoopExpression(), 1107 new CodeNullExpression() 1108 }; 1109 } 1110 1111 protected override void CheckOtherStacksAreEmpty() 1112 { 1113 TestStackCounts(codeStack: null); 1114 } 1115 } 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 } 1116 1066 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Interpreter/Expressions/CommonTests.cs
r14398 r14513 1 using HeuristicLab.Algorithms.PushGP; 2 using HeuristicLab.Algorithms.PushGP.Expressions; 3 using HeuristicLab.Algorithms.PushGP.Stack; 4 using Microsoft.VisualStudio.TestTools.UnitTesting; 5 6 namespace HeuristicLab.Tests.Interpreter.Expressions 7 { 8 9 public abstract class CommonTests<T> : ExpressionTest 10 { 11 protected const string FullInstructionNameFormat = "{0}.{1}"; 12 protected abstract string TypeName { get; } 13 protected abstract IStack<T> Stack { get; } 14 15 protected abstract T[] GetValues(int count); 16 17 protected virtual T[] Get2Different() 18 { 19 return GetValues(2); 20 } 21 22 protected virtual void Test(Expression expression) 23 { 24 interpreter.Interpret(expression); 25 } 26 27 protected abstract void CheckOtherStacksAreEmpty(); 28 29 [TestMethod] 30 [TestProperty("Time", "Short")] 31 [TestCategory("ExpressionTest")] 32 [TestCategory("CommonExpressionTest")] 33 public virtual void TestEqualsTrue() 34 { 35 var values = GetValues(1); 36 var merged = new[] { values[0], values[0] }; 37 38 Stack.Push(merged); 39 40 var isBooleanStack = interpreter.BooleanStack.Count == 2; 41 42 Test(ExpressionTable.GetStatelessExpression(TypeName + ".=")); 43 44 if (isBooleanStack) 45 { 46 Assert.AreEqual(1, Stack.Count); 47 } 48 else 49 { 50 Assert.AreEqual(0, Stack.Count); 51 } 52 53 Assert.IsTrue(interpreter.BooleanStack.Top); 54 } 55 56 [TestMethod] 57 [TestProperty("Time", "Short")] 58 [TestCategory("ExpressionTest")] 59 [TestCategory("CommonExpressionTest")] 60 public virtual void TestEqualsFalse() 61 { 62 var values = Get2Different(); 63 Stack.Push(values); 64 var isBooleanStack = interpreter.BooleanStack.Count == 2; 65 66 Test(ExpressionTable.GetStatelessExpression(TypeName + ".=")); 67 68 if (isBooleanStack) 69 { 70 Assert.AreEqual(1, Stack.Count); 71 } 72 else 73 { 74 Assert.AreEqual(0, Stack.Count); 75 } 76 77 Assert.IsFalse(interpreter.BooleanStack.Top); 78 } 79 80 [TestMethod] 81 [TestProperty("Time", "Short")] 82 [TestCategory("ExpressionTest")] 83 [TestCategory("CommonExpressionTest")] 84 public virtual void TestEqualsWithInsufficientArguments() 85 { 86 TestWithInsufficientArguments("=", 1); 87 } 88 89 [TestMethod] 90 [TestProperty("Time", "Short")] 91 [TestCategory("ExpressionTest")] 92 [TestCategory("CommonExpressionTest")] 93 public virtual void TestPop() 94 { 95 var values = GetValues(2); 96 Stack.Push(values); 97 98 Test(ExpressionTable.GetStatelessExpression(TypeName + ".POP")); 99 100 CheckOtherStacksAreEmpty(); 101 } 102 103 [TestMethod] 104 [TestProperty("Time", "Short")] 105 [TestCategory("ExpressionTest")] 106 [TestCategory("CommonExpressionTest")] 107 public virtual void TestSwap() 108 { 109 var values = GetValues(3); 110 Stack.Push(values); 111 112 Test(ExpressionTable.GetStatelessExpression(TypeName + ".SWAP")); 113 114 Assert.AreEqual(values[0], Stack[0]); 115 Assert.AreEqual(values[2], Stack[1]); 116 Assert.AreEqual(values[1], Stack[2]); 117 118 CheckOtherStacksAreEmpty(); 119 } 120 121 [TestMethod] 122 [TestProperty("Time", "Short")] 123 [TestCategory("ExpressionTest")] 124 [TestCategory("CommonExpressionTest")] 125 public virtual void TestSwapWithInsufficientArguments() 126 { 127 TestWithInsufficientArguments("SWAP", 1); 128 } 129 130 131 [TestMethod] 132 [TestProperty("Time", "Short")] 133 [TestCategory("ExpressionTest")] 134 [TestCategory("CommonExpressionTest")] 135 public virtual void TestRotate() 136 { 137 var values = GetValues(4); 138 Stack.Push(values); 139 140 Test(ExpressionTable.GetStatelessExpression(TypeName + ".ROT")); 141 142 Assert.AreEqual(values[0], Stack[0]); 143 Assert.AreEqual(values[3], Stack[1]); 144 Assert.AreEqual(values[1], Stack[2]); 145 Assert.AreEqual(values[2], Stack[3]); 146 147 CheckOtherStacksAreEmpty(); 148 } 149 150 [TestMethod] 151 [TestProperty("Time", "Short")] 152 [TestCategory("ExpressionTest")] 153 [TestCategory("CommonExpressionTest")] 154 public virtual void TestRotateWithInsufficientArguments() 155 { 156 TestWithInsufficientArguments("ROT", 2); 157 } 158 159 [TestMethod] 160 [TestProperty("Time", "Short")] 161 [TestCategory("ExpressionTest")] 162 [TestCategory("CommonExpressionTest")] 163 public virtual void TestShove() 164 { 165 var values = GetValues(3); 166 Stack.Push(values); 167 168 interpreter.IntegerStack.Push(1); 169 Test(ExpressionTable.GetStatelessExpression(TypeName + ".SHOVE")); 170 171 Assert.AreEqual(values[0], Stack[0]); 172 Assert.AreEqual(values[2], Stack[1]); 173 Assert.AreEqual(values[1], Stack[2]); 174 175 CheckOtherStacksAreEmpty(); 176 } 177 178 [TestMethod] 179 [TestProperty("Time", "Short")] 180 [TestCategory("ExpressionTest")] 181 [TestCategory("CommonExpressionTest")] 182 public virtual void TestShoveWithInsufficientArguments() 183 { 184 TestWithInsufficientArguments("SHOVE", 1); 185 } 186 187 [TestMethod] 188 [TestProperty("Time", "Short")] 189 [TestCategory("ExpressionTest")] 190 [TestCategory("CommonExpressionTest")] 191 public virtual void TestShoveWithNegativeIndex() 192 { 193 TestWithNegativeIndex("SHOVE"); 194 } 195 196 [TestMethod] 197 [TestProperty("Time", "Short")] 198 [TestCategory("ExpressionTest")] 199 [TestCategory("CommonExpressionTest")] 200 public virtual void TestYank() 201 { 202 var values = GetValues(3); 203 Stack.Push(values); 204 205 interpreter.IntegerStack.Push(1); 206 Test(ExpressionTable.GetStatelessExpression(TypeName + ".YANK")); 207 208 Assert.AreEqual(values[0], Stack[0]); 209 Assert.AreEqual(values[2], Stack[1]); 210 Assert.AreEqual(values[1], Stack[2]); 211 212 CheckOtherStacksAreEmpty(); 213 } 214 215 [TestMethod] 216 [TestProperty("Time", "Short")] 217 [TestCategory("ExpressionTest")] 218 [TestCategory("CommonExpressionTest")] 219 public virtual void TestYankWithInsufficientArguments() 220 { 221 TestWithInsufficientArguments("YANK", 1); 222 } 223 224 [TestMethod] 225 [TestProperty("Time", "Short")] 226 [TestCategory("ExpressionTest")] 227 [TestCategory("CommonExpressionTest")] 228 public virtual void TestYankWithNegativeIndex() 229 { 230 TestWithNegativeIndex("YANK"); 231 } 232 233 [TestMethod] 234 [TestProperty("Time", "Short")] 235 [TestCategory("ExpressionTest")] 236 [TestCategory("CommonExpressionTest")] 237 public virtual void TestYankDuplicate() 238 { 239 var values = GetValues(3); 240 Stack.Push(values); 241 242 interpreter.IntegerStack.Push(1); 243 Test(ExpressionTable.GetStatelessExpression(TypeName + ".YANKDUP")); 244 245 Assert.AreEqual(values[0], Stack[0]); 246 Assert.AreEqual(values[1], Stack[1]); 247 Assert.AreEqual(values[2], Stack[2]); 248 Assert.AreEqual(values[1], Stack[3]); 249 250 CheckOtherStacksAreEmpty(); 251 } 252 253 [TestMethod] 254 [TestProperty("Time", "Short")] 255 [TestCategory("ExpressionTest")] 256 [TestCategory("CommonExpressionTest")] 257 public virtual void TestYankDuplicateWithInsufficientArguments() 258 { 259 TestWithInsufficientArguments("YANKDUP", 1); 260 } 261 262 [TestMethod] 263 [TestProperty("Time", "Short")] 264 [TestCategory("ExpressionTest")] 265 [TestCategory("CommonExpressionTest")] 266 public virtual void TestYankDuplicateWithNegativeIndex() 267 { 268 TestWithNegativeIndex("YANKDUP"); 269 } 270 271 [TestMethod] 272 [TestProperty("Time", "Short")] 273 [TestCategory("ExpressionTest")] 274 [TestCategory("CommonExpressionTest")] 275 public virtual void TestStackdpeth() 276 { 277 var values = GetValues(3); 278 Stack.Push(values); 279 280 Test(ExpressionTable.GetStatelessExpression(TypeName + ".STACKDEPTH")); 281 282 // if stack is integer stack 283 if (Stack.Count != values.Length) 284 { 285 Assert.AreEqual(4, Stack.Count); 286 CheckOtherStacksAreEmpty(); 287 } 288 else 289 { 290 Assert.AreEqual(3, Stack.Count); 291 Assert.AreEqual(values.Length, interpreter.IntegerStack.Top); 292 } 293 } 294 295 protected void TestWithInsufficientArguments(string instructionName, int argCount = 0) 296 { 297 for (var i = 0; i < argCount + 1; i++) 298 { 299 var values = GetValues(i); 300 Stack.Push(values); 301 302 var fullInstructionname = string.Format(FullInstructionNameFormat, TypeName, instructionName); 303 Test(ExpressionTable.GetStatelessExpression(fullInstructionname)); 304 305 for (var j = 0; j < i; j++) 306 { 307 Assert.AreEqual(values[j], Stack[j]); 308 } 309 310 CheckOtherStacksAreEmpty(); 311 interpreter.Clear(); 312 } 313 } 314 315 protected void TestWithNegativeIndex(string instructionName) 316 { 317 var values = GetValues(1); 318 Stack.Push(values); 319 320 interpreter.IntegerStack.Push(-1); 321 322 var fullInstructionname = string.Format(FullInstructionNameFormat, TypeName, instructionName); 323 Test(ExpressionTable.GetStatelessExpression(fullInstructionname)); 324 325 Assert.AreEqual(values[0], Stack[0]); 326 } 327 } 1 namespace HeuristicLab.Tests.Interpreter.Expressions { 2 using HeuristicLab.Algorithms.PushGP.Expressions; 3 using HeuristicLab.Algorithms.PushGP.Stack; 4 5 using Microsoft.VisualStudio.TestTools.UnitTesting; 6 7 public abstract class CommonTests<T> : ExpressionTest { 8 protected const string FullInstructionNameFormat = "{0}.{1}"; 9 10 protected abstract string TypeName { get; } 11 12 protected abstract IStack<T> Stack { get; } 13 14 protected abstract T[] GetValues(int count); 15 16 protected virtual T[] Get2Different() { 17 return this.GetValues(2); 18 } 19 20 protected virtual void Test(Expression expression) { 21 this.interpreter.Interpret(expression); 22 } 23 24 protected abstract void CheckOtherStacksAreEmpty(); 25 26 [TestMethod] 27 [TestProperty("Time", "Short")] 28 [TestCategory("ExpressionTest")] 29 [TestCategory("CommonExpressionTest")] 30 public virtual void TestEqualsTrue() { 31 var values = this.GetValues(1); 32 var merged = new[] { values[0], values[0] }; 33 34 this.Stack.Push(merged); 35 36 var isBooleanStack = this.interpreter.BooleanStack.Count == 2; 37 38 this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".=")); 39 40 if (isBooleanStack) Assert.AreEqual(1, this.Stack.Count); 41 else Assert.AreEqual(0, this.Stack.Count); 42 43 Assert.IsTrue(this.interpreter.BooleanStack.Top); 44 } 45 46 [TestMethod] 47 [TestProperty("Time", "Short")] 48 [TestCategory("ExpressionTest")] 49 [TestCategory("CommonExpressionTest")] 50 public virtual void TestEqualsFalse() { 51 var values = this.Get2Different(); 52 this.Stack.Push(values); 53 var isBooleanStack = this.interpreter.BooleanStack.Count == 2; 54 55 this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".=")); 56 57 if (isBooleanStack) Assert.AreEqual(1, this.Stack.Count); 58 else Assert.AreEqual(0, this.Stack.Count); 59 60 Assert.IsFalse(this.interpreter.BooleanStack.Top); 61 } 62 63 [TestMethod] 64 [TestProperty("Time", "Short")] 65 [TestCategory("ExpressionTest")] 66 [TestCategory("CommonExpressionTest")] 67 public virtual void TestEqualsWithInsufficientArguments() { 68 this.TestWithInsufficientArguments("=", 1); 69 } 70 71 [TestMethod] 72 [TestProperty("Time", "Short")] 73 [TestCategory("ExpressionTest")] 74 [TestCategory("CommonExpressionTest")] 75 public virtual void TestDuplicate() { 76 var values = this.GetValues(1); 77 this.Stack.Push(values); 78 79 this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".DUP")); 80 81 Assert.AreEqual(this.Stack.Count, 2); 82 Assert.AreEqual(this.Stack[0], values[0]); 83 Assert.AreEqual(this.Stack[1], values[0]); 84 this.CheckOtherStacksAreEmpty(); 85 } 86 87 [TestMethod] 88 [TestProperty("Time", "Short")] 89 [TestCategory("ExpressionTest")] 90 [TestCategory("CommonExpressionTest")] 91 public virtual void TestPop() { 92 var values = this.GetValues(2); 93 this.Stack.Push(values); 94 95 this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".POP")); 96 97 this.CheckOtherStacksAreEmpty(); 98 } 99 100 [TestMethod] 101 [TestProperty("Time", "Short")] 102 [TestCategory("ExpressionTest")] 103 [TestCategory("CommonExpressionTest")] 104 public virtual void TestSwap() { 105 var values = this.GetValues(3); 106 this.Stack.Push(values); 107 108 this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".SWAP")); 109 110 Assert.AreEqual(values[0], this.Stack[0]); 111 Assert.AreEqual(values[2], this.Stack[1]); 112 Assert.AreEqual(values[1], this.Stack[2]); 113 114 this.CheckOtherStacksAreEmpty(); 115 } 116 117 [TestMethod] 118 [TestProperty("Time", "Short")] 119 [TestCategory("ExpressionTest")] 120 [TestCategory("CommonExpressionTest")] 121 public virtual void TestSwapWithInsufficientArguments() { 122 this.TestWithInsufficientArguments("SWAP", 1); 123 } 124 125 [TestMethod] 126 [TestProperty("Time", "Short")] 127 [TestCategory("ExpressionTest")] 128 [TestCategory("CommonExpressionTest")] 129 public virtual void TestRotate() { 130 var values = this.GetValues(4); 131 this.Stack.Push(values); 132 133 this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".ROT")); 134 135 Assert.AreEqual(values[0], this.Stack[0]); 136 Assert.AreEqual(values[3], this.Stack[1]); 137 Assert.AreEqual(values[1], this.Stack[2]); 138 Assert.AreEqual(values[2], this.Stack[3]); 139 140 this.CheckOtherStacksAreEmpty(); 141 } 142 143 [TestMethod] 144 [TestProperty("Time", "Short")] 145 [TestCategory("ExpressionTest")] 146 [TestCategory("CommonExpressionTest")] 147 public virtual void TestRotateWithInsufficientArguments() { 148 this.TestWithInsufficientArguments("ROT", 2); 149 } 150 151 [TestMethod] 152 [TestProperty("Time", "Short")] 153 [TestCategory("ExpressionTest")] 154 [TestCategory("CommonExpressionTest")] 155 public virtual void TestShove() { 156 var values = this.GetValues(3); 157 this.Stack.Push(values); 158 159 this.interpreter.IntegerStack.Push(1); 160 this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".SHOVE")); 161 162 Assert.AreEqual(values[0], this.Stack[0]); 163 Assert.AreEqual(values[2], this.Stack[1]); 164 Assert.AreEqual(values[1], this.Stack[2]); 165 166 this.CheckOtherStacksAreEmpty(); 167 } 168 169 [TestMethod] 170 [TestProperty("Time", "Short")] 171 [TestCategory("ExpressionTest")] 172 [TestCategory("CommonExpressionTest")] 173 public virtual void TestShoveWithInsufficientArguments() { 174 this.TestWithInsufficientArguments("SHOVE", 1); 175 } 176 177 [TestMethod] 178 [TestProperty("Time", "Short")] 179 [TestCategory("ExpressionTest")] 180 [TestCategory("CommonExpressionTest")] 181 public virtual void TestShoveWithNegativeIndex() { 182 this.TestWithNegativeIndex("SHOVE"); 183 } 184 185 [TestMethod] 186 [TestProperty("Time", "Short")] 187 [TestCategory("ExpressionTest")] 188 [TestCategory("CommonExpressionTest")] 189 public virtual void TestYank() { 190 var values = this.GetValues(3); 191 this.Stack.Push(values); 192 193 this.interpreter.IntegerStack.Push(1); 194 this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".YANK")); 195 196 Assert.AreEqual(values[0], this.Stack[0]); 197 Assert.AreEqual(values[2], this.Stack[1]); 198 Assert.AreEqual(values[1], this.Stack[2]); 199 200 this.CheckOtherStacksAreEmpty(); 201 } 202 203 [TestMethod] 204 [TestProperty("Time", "Short")] 205 [TestCategory("ExpressionTest")] 206 [TestCategory("CommonExpressionTest")] 207 public virtual void TestYankWithInsufficientArguments() { 208 this.TestWithInsufficientArguments("YANK", 1); 209 } 210 211 [TestMethod] 212 [TestProperty("Time", "Short")] 213 [TestCategory("ExpressionTest")] 214 [TestCategory("CommonExpressionTest")] 215 public virtual void TestYankWithNegativeIndex() { 216 this.TestWithNegativeIndex("YANK"); 217 } 218 219 [TestMethod] 220 [TestProperty("Time", "Short")] 221 [TestCategory("ExpressionTest")] 222 [TestCategory("CommonExpressionTest")] 223 public virtual void TestYankDuplicate() { 224 var values = this.GetValues(3); 225 this.Stack.Push(values); 226 227 this.interpreter.IntegerStack.Push(1); 228 this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".YANKDUP")); 229 230 Assert.AreEqual(values[0], this.Stack[0]); 231 Assert.AreEqual(values[1], this.Stack[1]); 232 Assert.AreEqual(values[2], this.Stack[2]); 233 Assert.AreEqual(values[1], this.Stack[3]); 234 235 this.CheckOtherStacksAreEmpty(); 236 } 237 238 [TestMethod] 239 [TestProperty("Time", "Short")] 240 [TestCategory("ExpressionTest")] 241 [TestCategory("CommonExpressionTest")] 242 public virtual void TestYankDuplicateWithInsufficientArguments() { 243 this.TestWithInsufficientArguments("YANKDUP", 1); 244 } 245 246 [TestMethod] 247 [TestProperty("Time", "Short")] 248 [TestCategory("ExpressionTest")] 249 [TestCategory("CommonExpressionTest")] 250 public virtual void TestYankDuplicateWithNegativeIndex() { 251 this.TestWithNegativeIndex("YANKDUP"); 252 } 253 254 [TestMethod] 255 [TestProperty("Time", "Short")] 256 [TestCategory("ExpressionTest")] 257 [TestCategory("CommonExpressionTest")] 258 public virtual void TestStackdpeth() { 259 var values = this.GetValues(3); 260 this.Stack.Push(values); 261 262 this.Test(ExpressionTable.GetStatelessExpression(this.TypeName + ".STACKDEPTH")); 263 264 // if stack is integer stack 265 if (this.Stack.Count != values.Length) { 266 Assert.AreEqual(4, this.Stack.Count); 267 this.CheckOtherStacksAreEmpty(); 268 } else { 269 Assert.AreEqual(3, this.Stack.Count); 270 Assert.AreEqual(values.Length, this.interpreter.IntegerStack.Top); 271 } 272 } 273 274 protected void TestWithInsufficientArguments(string instructionName, int argCount = 0) { 275 for (var i = 0; i < argCount + 1; i++) { 276 var values = this.GetValues(i); 277 this.Stack.Push(values); 278 279 var fullInstructionname = string.Format(FullInstructionNameFormat, this.TypeName, instructionName); 280 this.Test(ExpressionTable.GetStatelessExpression(fullInstructionname)); 281 282 for (var j = 0; j < i; j++) Assert.AreEqual(values[j], this.Stack[j]); 283 284 this.CheckOtherStacksAreEmpty(); 285 this.interpreter.Clear(); 286 } 287 } 288 289 protected void TestWithNegativeIndex(string instructionName) { 290 var values = this.GetValues(1); 291 this.Stack.Push(values); 292 293 this.interpreter.IntegerStack.Push(-1); 294 295 var fullInstructionname = string.Format(FullInstructionNameFormat, this.TypeName, instructionName); 296 this.Test(ExpressionTable.GetStatelessExpression(fullInstructionname)); 297 298 Assert.AreEqual(values[0], this.Stack[0]); 299 } 300 } 328 301 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Interpreter/Expressions/ExampleTests.cs
r14398 r14513 1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 using Microsoft.VisualStudio.TestTools.UnitTesting; 3 4 namespace HeuristicLab.Tests.Interpreter.Expressions 5 { 6 [TestClass] 7 public class MixedExpressionTests : ExpressionTest 8 { 9 const double Delta = 0.00000001; 10 11 [TestMethod] 12 [TestProperty("Time", "Short")] 13 [TestCategory("ExampleTest")] 14 public void Example1() 15 { 16 interpreter.Interpret("( 2 3 INTEGER.* 4.1 5.2 FLOAT.+ TRUE FALSE BOOLEAN.OR )"); 17 18 Assert.AreEqual(6, interpreter.IntegerStack.Top); 19 Assert.AreEqual(9.3, interpreter.FloatStack.Top); 20 Assert.AreEqual(interpreter.BooleanStack.Top, true); 21 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 22 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 23 Assert.IsTrue(interpreter.NameStack.IsEmpty); 24 } 25 26 [TestMethod] 27 [TestProperty("Time", "Short")] 28 [TestCategory("ExampleTest")] 29 public void Example2() 30 { 31 interpreter.Interpret("( 5 1.23 INTEGER.+ ( 4 ) INTEGER.- 5.67 FLOAT.* )"); 32 33 Assert.AreEqual(1, interpreter.IntegerStack.Top); 34 Assert.AreEqual(6.9741, interpreter.FloatStack.Top); 35 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 36 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 37 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 38 Assert.IsTrue(interpreter.NameStack.IsEmpty); 39 } 40 41 [TestMethod] 42 [TestProperty("Time", "Short")] 43 [TestCategory("ExampleTest")] 44 public void Example3() 45 { 46 interpreter.Interpret("( 5 INTEGER.DUP INTEGER.+ )"); 47 48 Assert.AreEqual(10, interpreter.IntegerStack.Top); 49 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 50 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 51 Assert.IsTrue(interpreter.NameStack.IsEmpty); 52 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 53 Assert.IsTrue(interpreter.FloatStack.IsEmpty); 54 } 55 56 [TestMethod] 57 [TestProperty("Time", "Short")] 58 [TestCategory("ExampleTest")] 59 public void Example4() 60 { 61 interpreter.Interpret("( 5 CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DO )"); 62 63 Assert.AreEqual(10, interpreter.IntegerStack.Top); 64 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 65 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 66 Assert.IsTrue(interpreter.NameStack.IsEmpty); 67 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 68 Assert.IsTrue(interpreter.FloatStack.IsEmpty); 69 } 70 71 [TestMethod] 72 [TestProperty("Time", "Short")] 73 [TestCategory("ExampleTest")] 74 public void Example5() 75 { 76 interpreter.Interpret("( DOUBLE CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DEFINE 5 DOUBLE )"); 77 78 Assert.AreEqual(10, interpreter.IntegerStack.Top); 79 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 80 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 81 Assert.IsTrue(interpreter.NameStack.IsEmpty); 82 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 83 Assert.IsTrue(interpreter.FloatStack.IsEmpty); 84 } 85 86 [TestMethod] 87 [TestProperty("Time", "Short")] 88 [TestCategory("ExampleTest")] 89 public void Example6() 90 { 91 interpreter.Interpret("( CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) DOUBLE CODE.DEFINE 5 DOUBLE )"); 92 93 Assert.AreEqual(10, interpreter.IntegerStack.Top); 94 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 95 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 96 Assert.IsTrue(interpreter.NameStack.IsEmpty); 97 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 98 Assert.IsTrue(interpreter.FloatStack.IsEmpty); 99 } 100 101 [TestMethod] 102 [TestProperty("Time", "Short")] 103 [TestCategory("ExampleTest")] 104 public void Example7() 105 { 106 interpreter.Interpret("( DOUBLE EXEC.DEFINE ( INTEGER.DUP INTEGER.+ ) 5 DOUBLE )"); 107 108 Assert.AreEqual(10, interpreter.IntegerStack.Top); 109 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 110 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 111 Assert.IsTrue(interpreter.NameStack.IsEmpty); 112 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 113 Assert.IsTrue(interpreter.FloatStack.IsEmpty); 114 } 115 116 [TestMethod] 117 [TestProperty("Time", "Short")] 118 [TestCategory("ExampleTest")] 119 public void Example8() 120 { 121 interpreter.Configuration.TopLevelPushCode = true; 122 123 interpreter.IntegerStack.Push(5); 124 interpreter.Interpret(@"( CODE.QUOTE ( INTEGER.POP 1 ) 1 namespace HeuristicLab.Tests.Interpreter.Expressions { 2 using HeuristicLab.Algorithms.PushGP.Parser; 3 4 using Microsoft.VisualStudio.TestTools.UnitTesting; 5 6 [TestClass] 7 public class MixedExpressionTests : ExpressionTest { 8 private const double Delta = 0.00000001; 9 10 [TestMethod] 11 [TestProperty("Time", "Short")] 12 [TestCategory("ExampleTest")] 13 public void Example1() { 14 this.interpreter.Interpret("( 2 3 INTEGER.* 4.1 5.2 FLOAT.+ TRUE FALSE BOOLEAN.OR )"); 15 16 Assert.AreEqual(6, this.interpreter.IntegerStack.Top); 17 Assert.AreEqual(9.3, this.interpreter.FloatStack.Top); 18 Assert.IsTrue(this.interpreter.BooleanStack.Top); 19 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 20 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 21 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 22 } 23 24 [TestMethod] 25 [TestProperty("Time", "Short")] 26 [TestCategory("ExampleTest")] 27 public void Example2() { 28 this.interpreter.Interpret("( 5 1.23 INTEGER.+ ( 4 ) INTEGER.- 5.67 FLOAT.* )"); 29 30 Assert.AreEqual(1, this.interpreter.IntegerStack.Top); 31 Assert.AreEqual(6.9741, this.interpreter.FloatStack.Top); 32 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 33 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 34 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 35 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 36 } 37 38 [TestMethod] 39 [TestProperty("Time", "Short")] 40 [TestCategory("ExampleTest")] 41 public void Example3() { 42 this.interpreter.Interpret("( 5 INTEGER.DUP INTEGER.+ )"); 43 44 Assert.AreEqual(10, this.interpreter.IntegerStack.Top); 45 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 46 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 47 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 48 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 49 Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); 50 } 51 52 [TestMethod] 53 [TestProperty("Time", "Short")] 54 [TestCategory("ExampleTest")] 55 public void Example4() { 56 this.interpreter.Interpret("( 5 CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DO )"); 57 58 Assert.AreEqual(10, this.interpreter.IntegerStack.Top); 59 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 60 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 61 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 62 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 63 Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); 64 } 65 66 [TestMethod] 67 [TestProperty("Time", "Short")] 68 [TestCategory("ExampleTest")] 69 public void Example5() { 70 this.interpreter.Interpret("( DOUBLE CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DEFINE 5 DOUBLE )"); 71 72 Assert.AreEqual(10, this.interpreter.IntegerStack.Top); 73 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 74 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 75 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 76 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 77 Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); 78 } 79 80 [TestMethod] 81 [TestProperty("Time", "Short")] 82 [TestCategory("ExampleTest")] 83 public void Example6() { 84 this.interpreter.Interpret("( CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) DOUBLE CODE.DEFINE 5 DOUBLE )"); 85 86 Assert.AreEqual(10, this.interpreter.IntegerStack.Top); 87 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 88 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 89 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 90 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 91 Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); 92 } 93 94 [TestMethod] 95 [TestProperty("Time", "Short")] 96 [TestCategory("ExampleTest")] 97 public void Example7() { 98 this.interpreter.Interpret("( DOUBLE EXEC.DEFINE ( INTEGER.DUP INTEGER.+ ) 5 DOUBLE )"); 99 100 Assert.AreEqual(10, this.interpreter.IntegerStack.Top); 101 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 102 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 103 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 104 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 105 Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); 106 } 107 108 [TestMethod] 109 [TestProperty("Time", "Short")] 110 [TestCategory("ExampleTest")] 111 public void Example8() { 112 this.interpreter.Configuration.TopLevelPushCode = true; 113 114 this.interpreter.IntegerStack.Push(5); 115 this.interpreter.Interpret(@"( CODE.QUOTE ( INTEGER.POP 1 ) 125 116 CODE.QUOTE ( CODE.DUP INTEGER.DUP 1 INTEGER.- CODE.DO INTEGER.* ) 126 117 INTEGER.DUP 2 INTEGER.< CODE.IF )"); 127 118 128 Assert.AreEqual(120, interpreter.IntegerStack.Top); 129 Assert.IsTrue(interpreter.CodeStack.Count == 1); 130 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 131 Assert.IsTrue(interpreter.NameStack.IsEmpty); 132 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 133 Assert.IsTrue(interpreter.FloatStack.IsEmpty); 134 } 135 136 [TestMethod] 137 [TestProperty("Time", "Short")] 138 [TestCategory("ExampleTest")] 139 public void Example9() 140 { 141 interpreter.IntegerStack.Push(5); 142 interpreter.Interpret("( 1 INTEGER.MAX 1 EXEC.DO*RANGE INTEGER.* )"); 143 144 Assert.AreEqual(120, interpreter.IntegerStack.Top); 145 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 146 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 147 Assert.IsTrue(interpreter.NameStack.IsEmpty); 148 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 149 Assert.IsTrue(interpreter.FloatStack.IsEmpty); 150 } 151 152 [TestMethod] 153 [TestProperty("Time", "Short")] 154 [TestCategory("ExampleTest")] 155 public void Example10() 156 { 157 interpreter.IntegerStack.Push(5); 158 interpreter.Interpret("( 1 INTEGER.MAX 1 EXEC.DO*RANGE ( 2 INTEGER.* ) )"); 159 160 Assert.AreEqual(2, interpreter.IntegerStack.Top); 161 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 162 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 163 Assert.IsTrue(interpreter.NameStack.IsEmpty); 164 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 165 Assert.IsTrue(interpreter.FloatStack.IsEmpty); 166 } 167 168 [TestMethod] 169 [TestProperty("Time", "Short")] 170 [TestCategory("ExampleTest")] 171 public void Example11() 172 { 173 var program = PushGPInterpreter.Encode("( INTEGER.= CODE.QUOTE FLOAT.* CODE.QUOTE FLOAT./ CODE.IF )"); 174 175 interpreter.IntegerStack.Push(1, 1); 176 interpreter.FloatStack.Push(2.1, 0.7); 177 178 interpreter.Interpret(program); 179 180 Assert.IsTrue(interpreter.IntegerStack.IsEmpty); 181 Assert.AreEqual(1.47, 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 187 interpreter.Clear(); 188 interpreter.IntegerStack.Push(1, 2); 189 interpreter.FloatStack.Push(2.1, 0.7); 190 interpreter.Interpret(program); 191 192 Assert.IsTrue(interpreter.IntegerStack.IsEmpty); 193 Assert.AreEqual(3d, interpreter.FloatStack.Top, Delta); 194 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 195 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 196 Assert.IsTrue(interpreter.NameStack.IsEmpty); 197 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 198 } 199 200 [TestMethod] 201 [TestProperty("Time", "Short")] 202 [TestCategory("ExampleTest")] 203 public void Example12() 204 { 205 var program = PushGPInterpreter.Encode("( INTEGER.= EXEC.IF FLOAT.* FLOAT./ )"); 206 207 interpreter.IntegerStack.Push(1, 1); 208 interpreter.FloatStack.Push(2.1, 0.7); 209 210 interpreter.Interpret(program); 211 212 Assert.IsTrue(interpreter.IntegerStack.IsEmpty); 213 Assert.AreEqual(1.47, interpreter.FloatStack.Top, Delta); 214 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 215 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 216 Assert.IsTrue(interpreter.NameStack.IsEmpty); 217 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 218 219 interpreter.Clear(); 220 interpreter.IntegerStack.Push(1, 2); 221 interpreter.FloatStack.Push(2.1, 0.7); 222 interpreter.Interpret(program); 223 224 Assert.IsTrue(interpreter.IntegerStack.IsEmpty); 225 Assert.AreEqual(3d, interpreter.FloatStack.Top, Delta); 226 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 227 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 228 Assert.IsTrue(interpreter.NameStack.IsEmpty); 229 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 230 } 231 232 [TestMethod] 233 [TestProperty("Time", "Short")] 234 [TestCategory("ExampleTest")] 235 public void Example13() 236 { 237 interpreter.IntegerStack.Push(5); 238 interpreter.Interpret("( EXEC.Y ( ( FALSE 2 INTEGER.* ) EXEC.IF ( ) EXEC.POP ) )"); 239 240 Assert.AreEqual(10, interpreter.IntegerStack.Top); 241 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 242 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 243 Assert.IsTrue(interpreter.NameStack.IsEmpty); 244 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 245 Assert.IsTrue(interpreter.FloatStack.IsEmpty); 246 } 247 248 [TestMethod] 249 [TestProperty("Time", "Short")] 250 [TestCategory("ExampleTest")] 251 public void Example14() 252 { 253 interpreter.IntegerStack.Push(5); 254 interpreter.Interpret("( EXEC.Y ( ( 2 INTEGER.* INTEGER.DUP 1000 INTEGER.< ) EXEC.IF ( ) EXEC.POP ) )"); 255 256 Assert.AreEqual(1280, interpreter.IntegerStack.Top); 257 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 258 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 259 Assert.IsTrue(interpreter.NameStack.IsEmpty); 260 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 261 Assert.IsTrue(interpreter.FloatStack.IsEmpty); 262 } 263 264 [TestMethod] 265 [TestProperty("Time", "Short")] 266 [TestCategory("ExampleTest")] 267 public void Example15() 268 { 269 interpreter.IntegerStack.Push(10); 270 interpreter.FloatStack.Push(2); 271 interpreter.Interpret(@"( ARG FLOAT.DEFINE 119 Assert.AreEqual(120, this.interpreter.IntegerStack.Top); 120 Assert.IsTrue(this.interpreter.CodeStack.Count == 1); 121 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 122 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 123 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 124 Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); 125 } 126 127 [TestMethod] 128 [TestProperty("Time", "Short")] 129 [TestCategory("ExampleTest")] 130 public void Example9() { 131 this.interpreter.IntegerStack.Push(5); 132 this.interpreter.Interpret("( 1 INTEGER.MAX 1 EXEC.DO*RANGE INTEGER.* )"); 133 134 Assert.AreEqual(120, this.interpreter.IntegerStack.Top); 135 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 136 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 137 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 138 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 139 Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); 140 } 141 142 [TestMethod] 143 [TestProperty("Time", "Short")] 144 [TestCategory("ExampleTest")] 145 public void Example10() { 146 this.interpreter.IntegerStack.Push(5); 147 this.interpreter.Interpret("( 1 INTEGER.MAX 1 EXEC.DO*RANGE ( 2 INTEGER.* ) )"); 148 149 Assert.AreEqual(2, this.interpreter.IntegerStack.Top); 150 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 151 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 152 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 153 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 154 Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); 155 } 156 157 [TestMethod] 158 [TestProperty("Time", "Short")] 159 [TestCategory("ExampleTest")] 160 public void Example11() { 161 var program = PushGPParser.Parse("( INTEGER.= CODE.QUOTE FLOAT.* CODE.QUOTE FLOAT./ CODE.IF )"); 162 163 this.interpreter.IntegerStack.Push(1, 1); 164 this.interpreter.FloatStack.Push(2.1, 0.7); 165 166 this.interpreter.Interpret(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.Interpret(program); 179 180 Assert.IsTrue(this.interpreter.IntegerStack.IsEmpty); 181 Assert.AreEqual(3d, this.interpreter.FloatStack.Top, Delta); 182 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 183 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 184 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 185 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 186 } 187 188 [TestMethod] 189 [TestProperty("Time", "Short")] 190 [TestCategory("ExampleTest")] 191 public void Example12() { 192 var program = PushGPParser.Parse("( INTEGER.= EXEC.IF FLOAT.* FLOAT./ )"); 193 194 this.interpreter.IntegerStack.Push(1, 1); 195 this.interpreter.FloatStack.Push(2.1, 0.7); 196 197 this.interpreter.Interpret(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.Interpret(program); 210 211 Assert.IsTrue(this.interpreter.IntegerStack.IsEmpty); 212 Assert.AreEqual(3d, this.interpreter.FloatStack.Top, Delta); 213 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 214 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 215 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 216 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 217 } 218 219 [TestMethod] 220 [TestProperty("Time", "Short")] 221 [TestCategory("ExampleTest")] 222 public void Example13() { 223 this.interpreter.IntegerStack.Push(5); 224 this.interpreter.Interpret("( EXEC.Y ( ( FALSE 2 INTEGER.* ) EXEC.IF ( ) EXEC.POP ) )"); 225 226 Assert.AreEqual(10, this.interpreter.IntegerStack.Top); 227 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 228 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 229 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 230 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 231 Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); 232 } 233 234 [TestMethod] 235 [TestProperty("Time", "Short")] 236 [TestCategory("ExampleTest")] 237 public void Example14() { 238 this.interpreter.IntegerStack.Push(5); 239 this.interpreter.Interpret("( EXEC.Y ( ( 2 INTEGER.* INTEGER.DUP 1000 INTEGER.< ) EXEC.IF ( ) EXEC.POP ) )"); 240 241 Assert.AreEqual(1280, this.interpreter.IntegerStack.Top); 242 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 243 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 244 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 245 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 246 Assert.IsTrue(this.interpreter.FloatStack.IsEmpty); 247 } 248 249 [TestMethod] 250 [TestProperty("Time", "Short")] 251 [TestCategory("ExampleTest")] 252 public void Example15() { 253 this.interpreter.IntegerStack.Push(10); 254 this.interpreter.FloatStack.Push(2); 255 this.interpreter.Interpret(@"( ARG FLOAT.DEFINE 272 256 EXEC.Y ( 273 257 ARG FLOAT.* … … 278 262 )"); 279 263 280 Assert.AreEqual(1024, interpreter.FloatStack.Top, Delta); 281 Assert.AreEqual(1, interpreter.IntegerStack.Top); 282 Assert.IsTrue(interpreter.CodeStack.IsEmpty); 283 Assert.IsTrue(interpreter.ExecStack.IsEmpty); 284 Assert.IsTrue(interpreter.NameStack.IsEmpty); 285 Assert.IsTrue(interpreter.BooleanStack.IsEmpty); 286 } 287 } 264 Assert.AreEqual(1024, this.interpreter.FloatStack.Top, Delta); 265 Assert.AreEqual(1, this.interpreter.IntegerStack.Top); 266 Assert.IsTrue(this.interpreter.CodeStack.IsEmpty); 267 Assert.IsTrue(this.interpreter.ExecStack.IsEmpty); 268 Assert.IsTrue(this.interpreter.NameStack.IsEmpty); 269 Assert.IsTrue(this.interpreter.BooleanStack.IsEmpty); 270 } 271 272 [TestMethod] 273 [TestProperty("Time", "Short")] 274 [TestCategory("ExampleTest")] 275 public void Example16() { 276 var list = PushGPParser.Parse("( A B C )"); 277 278 this.interpreter.CodeStack.Push(list); 279 this.interpreter.Interpret("( CODE.DUP 0 CODE.INSERT )"); 280 281 var id = this.interpreter.CodeStack.Top.GetHashCode(); 282 283 if (id == default(int)) Assert.Fail(); 284 } 285 } 288 286 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Interpreter/Expressions/ExecExpressionTests.cs
r14398 r14513 1 using HeuristicLab.Algorithms.PushGP;2 using HeuristicLab.Algorithms.PushGP.Expressions;3 using HeuristicLab.Algorithms.PushGP.Stack;4 using Microsoft.VisualStudio.TestTools.UnitTesting;1 namespace HeuristicLab.Tests.Interpreter.Expressions { 2 using HeuristicLab.Algorithms.PushGP.Expressions; 3 using HeuristicLab.Algorithms.PushGP.Parser; 4 using HeuristicLab.Algorithms.PushGP.Stack; 5 5 6 namespace HeuristicLab.Tests.Interpreter.Expressions 7 { 8 [TestClass] 9 public class ExecExpressionTests : CommonTests<Expression> 6 using Microsoft.VisualStudio.TestTools.UnitTesting; 7 8 [TestClass] 9 public class ExecExpressionTests : CommonTests<Expression> { 10 protected override string TypeName 10 11 { 11 protected override string TypeName { get { return "EXEC"; } } 12 get 13 { 14 return "EXEC"; 15 } 16 } 12 17 13 protected override IStack<Expression> Stack { get { return interpreter.ExecStack; } } 18 protected override IStack<Expression> Stack 19 { 20 get 21 { 22 return this.interpreter.ExecStack; 23 } 24 } 14 25 15 protected override void Test(Expression expression) 16 { 17 interpreter.InterpretAsync(expression, true).Wait(); 18 } 26 protected override void Test(Expression expression) { 27 this.interpreter.InterpretAsync(expression, true).Wait(); 28 } 19 29 20 [TestMethod] 21 [TestProperty("Time", "Short")] 22 [TestCategory("ExpressionTest")] 23 [TestCategory("ExecExpressionTest")] 24 public void TestIfTrue() 25 { 26 interpreter.BooleanStack.Push(true); 27 interpreter.Interpret("( EXEC.IF WAHR FALSCH )"); 30 [TestMethod] 31 [TestProperty("Time", "Short")] 32 [TestCategory("ExpressionTest")] 33 [TestCategory("ExecExpressionTest")] 34 public void TestIfTrue() { 35 this.interpreter.BooleanStack.Push(true); 36 this.interpreter.Interpret("( EXEC.IF WAHR FALSCH )"); 28 37 29 Assert.AreEqual("WAHR",interpreter.NameStack.Top);30 31 38 Assert.AreEqual("WAHR", this.interpreter.NameStack.Top); 39 this.TestStackCounts(nameStack: 1); 40 } 32 41 33 [TestMethod] 34 [TestProperty("Time", "Short")] 35 [TestCategory("ExpressionTest")] 36 [TestCategory("ExecExpressionTest")] 37 public void TestIfFalse() 38 { 39 interpreter.BooleanStack.Push(false); 40 interpreter.Interpret("( EXEC.IF WAHR FALSCH )"); 42 [TestMethod] 43 [TestProperty("Time", "Short")] 44 [TestCategory("ExpressionTest")] 45 [TestCategory("ExecExpressionTest")] 46 public void TestIfFalse() { 47 this.interpreter.BooleanStack.Push(false); 48 this.interpreter.Interpret("( EXEC.IF WAHR FALSCH )"); 41 49 42 Assert.AreEqual("FALSCH",interpreter.NameStack.Top);43 44 50 Assert.AreEqual("FALSCH", this.interpreter.NameStack.Top); 51 this.TestStackCounts(nameStack: 1); 52 } 45 53 46 [TestMethod] 47 [TestProperty("Time", "Short")] 48 [TestCategory("ExpressionTest")] 49 [TestCategory("ExecExpressionTest")] 50 public void TestK() 51 { 52 var first = Parser.Parse("A"); 53 var second = Parser.Parse("B"); 54 var third = Parser.Parse("C"); 54 [TestMethod] 55 [TestProperty("Time", "Short")] 56 [TestCategory("ExpressionTest")] 57 [TestCategory("ExecExpressionTest")] 58 public void TestK() { 59 var first = PushGPParser.Parse("A"); 60 var second = PushGPParser.Parse("B"); 61 var third = PushGPParser.Parse("C"); 55 62 56 57 63 this.interpreter.ExecStack.Push(third, second, first); 64 this.Test(new ExecKExpression()); 58 65 59 Assert.AreEqual(first,interpreter.ExecStack.Top);60 Assert.AreEqual(third,interpreter.ExecStack.Bottom);61 TestStackCounts(execStack:2);62 66 Assert.AreEqual(first, this.interpreter.ExecStack.Top); 67 Assert.AreEqual(third, this.interpreter.ExecStack.Bottom); 68 this.TestStackCounts(2); 69 } 63 70 64 [TestMethod] 65 [TestProperty("Time", "Short")] 66 [TestCategory("ExpressionTest")] 67 [TestCategory("ExecExpressionTest")] 68 public void TestS() 69 { 70 var first = Parser.Parse("A"); 71 var second = Parser.Parse("B"); 72 var third = Parser.Parse("C"); 73 var result = Parser.Parse("( B C )"); 71 [TestMethod] 72 [TestProperty("Time", "Short")] 73 [TestCategory("ExpressionTest")] 74 [TestCategory("ExecExpressionTest")] 75 public void TestS() { 76 var first = PushGPParser.Parse("A"); 77 var second = PushGPParser.Parse("B"); 78 var third = PushGPParser.Parse("C"); 79 var result = PushGPParser.Parse("( B C )"); 74 80 75 76 81 this.interpreter.ExecStack.Push(third, second, first); 82 this.Test(new ExecSExpression()); 77 83 78 Assert.AreEqual(result,interpreter.ExecStack.ReverseElementAt(2));79 Assert.AreEqual(third,interpreter.ExecStack.ReverseElementAt(1));80 Assert.AreEqual(first,interpreter.ExecStack.Top);81 TestStackCounts(execStack:3);82 84 Assert.AreEqual(result, this.interpreter.ExecStack.ReverseElementAt(2)); 85 Assert.AreEqual(third, this.interpreter.ExecStack.ReverseElementAt(1)); 86 Assert.AreEqual(first, this.interpreter.ExecStack.Top); 87 this.TestStackCounts(3); 88 } 83 89 84 [TestMethod] 85 [TestProperty("Time", "Short")] 86 [TestCategory("ExpressionTest")] 87 [TestCategory("ExecExpressionTest")] 88 public void TestY() 89 { 90 var first = Parser.Parse("A"); 91 var result = Parser.Parse("( EXEC.Y A )"); 90 [TestMethod] 91 [TestProperty("Time", "Short")] 92 [TestCategory("ExpressionTest")] 93 [TestCategory("ExecExpressionTest")] 94 public void TestY() { 95 var first = PushGPParser.Parse("A"); 96 var result = PushGPParser.Parse("( EXEC.Y A )"); 92 97 93 94 98 this.interpreter.ExecStack.Push(first); 99 this.Test(new ExecYExpression()); 95 100 96 Assert.AreEqual(first,interpreter.ExecStack.Top);97 Assert.AreEqual(result,interpreter.ExecStack.ReverseElementAt(1));98 TestStackCounts(execStack:2);99 101 Assert.AreEqual(first, this.interpreter.ExecStack.Top); 102 Assert.AreEqual(result, this.interpreter.ExecStack.ReverseElementAt(1)); 103 this.TestStackCounts(2); 104 } 100 105 101 102 103 104 105 public void TestNestedDoRange()106 {107 interpreter.Interpret("( 0 2 EXEC.DO*RANGE ( 1 INTEGER.+ 0 3 EXEC.DO*RANGE ( 1 INTEGER.+ INTEGER.* ) INTEGER.+ )");106 [TestMethod] 107 [TestProperty("Time", "Short")] 108 [TestCategory("ExpressionTest")] 109 [TestCategory("ExecExpressionTest")] 110 public void TestNestedDoRange() { 111 this.interpreter.Interpret( 112 "( 0 2 EXEC.DO*RANGE ( 1 INTEGER.+ 0 3 EXEC.DO*RANGE ( 1 INTEGER.+ INTEGER.* ) INTEGER.+ )"); 108 113 109 Assert.AreEqual(144,interpreter.IntegerStack.Top);110 111 114 Assert.AreEqual(144, this.interpreter.IntegerStack.Top); 115 this.TestStackCounts(integerStack: 1); 116 } 112 117 113 114 115 116 117 public void TestNestedDoCount()118 {119 interpreter.Interpret("( 2 EXEC.DO*COUNT ( 1 INTEGER.+ 3 EXEC.DO*COUNT ( 1 INTEGER.+ INTEGER.* ) INTEGER.+ )");118 [TestMethod] 119 [TestProperty("Time", "Short")] 120 [TestCategory("ExpressionTest")] 121 [TestCategory("ExecExpressionTest")] 122 public void TestNestedDoCount() { 123 this.interpreter.Interpret( 124 "( 2 EXEC.DO*COUNT ( 1 INTEGER.+ 3 EXEC.DO*COUNT ( 1 INTEGER.+ INTEGER.* ) INTEGER.+ )"); 120 125 121 Assert.AreEqual(144,interpreter.IntegerStack.Top);122 123 126 Assert.AreEqual(144, this.interpreter.IntegerStack.Top); 127 this.TestStackCounts(integerStack: 1); 128 } 124 129 125 [TestMethod] 126 [TestProperty("Time", "Short")] 127 [TestCategory("ExpressionTest")] 128 [TestCategory("ExecExpressionTest")] 129 public void TestNestedDoTimes() 130 { 131 interpreter.Interpret("( 3 EXEC.DO*TIMES ( 2 3 EXEC.DO*TIMES ( 2 INTEGER.* ) INTEGER.+ )"); 130 [TestMethod] 131 [TestProperty("Time", "Short")] 132 [TestCategory("ExpressionTest")] 133 [TestCategory("ExecExpressionTest")] 134 public void TestNestedDoTimes() { 135 this.interpreter.Interpret("( 3 EXEC.DO*TIMES ( 2 3 EXEC.DO*TIMES ( 2 INTEGER.* ) INTEGER.+ )"); 132 136 133 Assert.AreEqual(128,interpreter.IntegerStack.Top);134 135 137 Assert.AreEqual(128, this.interpreter.IntegerStack.Top); 138 this.TestStackCounts(integerStack: 1); 139 } 136 140 137 protected override Expression[] GetValues(int count) 138 { 139 var values = new Expression[count]; 141 protected override Expression[] GetValues(int count) { 142 var values = new Expression[count]; 140 143 141 for (var i = 0; i < count; i++) 142 { 143 values[i] = new CodeNoopExpression(); 144 } 144 for (var i = 0; i < count; i++) values[i] = new CodeNoopExpression(); 145 145 146 147 146 return values; 147 } 148 148 149 protected override Expression[] Get2Different() 150 { 151 return new Expression[] 152 { 153 new CodeNoopExpression(), 154 new CodeNullExpression() 155 }; 156 } 149 protected override Expression[] Get2Different() { 150 return new Expression[] { new CodeNoopExpression(), new CodeNullExpression() }; 151 } 157 152 158 protected override void CheckOtherStacksAreEmpty() 159 { 160 TestStackCounts(execStack: null); 161 } 153 protected override void CheckOtherStacksAreEmpty() { 154 this.TestStackCounts(null); 162 155 } 156 } 163 157 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Interpreter/Expressions/FloatExpressionTests.cs
r14398 r14513 1 using System; 2 using HeuristicLab.Algorithms.PushGP.Expressions; 3 using HeuristicLab.Algorithms.PushGP.Stack; 4 using Microsoft.VisualStudio.TestTools.UnitTesting; 5 6 namespace HeuristicLab.Tests.Interpreter.Expressions 1 namespace HeuristicLab.Tests.Interpreter.Expressions 7 2 { 8 [TestClass] 9 public class FloatExpressionTests : CommonTests<double> 10 { 11 private const double delta = 0.00001; 12 protected override string TypeName { get { return "FLOAT"; } } 13 protected override IStack<double> Stack { get { return interpreter.FloatStack; } } 14 15 [TestMethod] 16 [TestProperty("Time", "Short")] 17 [TestCategory("ExpressionTest")] 18 [TestCategory("FloatExpressionTest")] 19 public void TestAdd() 20 { 21 interpreter.FloatStack.Push(5.3, 5.3); 22 interpreter.Interpret(new FloatAddExpression()); 23 24 Assert.AreEqual(10.6, interpreter.FloatStack.Top, delta); 25 26 TestStackCounts(floatStack: 1); 27 } 28 29 [TestMethod] 30 [TestProperty("Time", "Short")] 31 [TestCategory("ExpressionTest")] 32 [TestCategory("FloatExpressionTest")] 33 public void TestAddWithInsufficientArguments() 34 { 35 TestWithInsufficientArguments("+", 1); 36 } 37 38 [TestMethod] 39 [TestProperty("Time", "Short")] 40 [TestCategory("ExpressionTest")] 41 [TestCategory("FloatExpressionTest")] 42 public void TestSubtract() 43 { 44 interpreter.FloatStack.Push(10.2, 5.3); 45 interpreter.Interpret(new FloatSubtractExpression()); 46 47 Assert.AreEqual(4.9, interpreter.FloatStack.Top, delta); 48 49 TestStackCounts(floatStack: 1); 50 } 51 52 [TestMethod] 53 [TestProperty("Time", "Short")] 54 [TestCategory("ExpressionTest")] 55 [TestCategory("FloatExpressionTest")] 56 public void TestSubractWithInsufficientArguments() 57 { 58 TestWithInsufficientArguments("-", 1); 59 } 60 61 [TestMethod] 62 [TestProperty("Time", "Short")] 63 [TestCategory("ExpressionTest")] 64 [TestCategory("FloatExpressionTest")] 65 public void TestMultiply() 66 { 67 interpreter.FloatStack.Push(9.9, 3.3); 68 interpreter.Interpret(new FloatMultiplyExpression()); 69 70 Assert.AreEqual(32.67, interpreter.FloatStack.Top, delta); 71 72 TestStackCounts(floatStack: 1); 73 } 74 75 [TestMethod] 76 [TestProperty("Time", "Short")] 77 [TestCategory("ExpressionTest")] 78 [TestCategory("FloatExpressionTest")] 79 public void TestMultiplyWithInsufficientArguments() 80 { 81 TestWithInsufficientArguments("*", 1); 82 } 83 84 [TestMethod] 85 [TestProperty("Time", "Short")] 86 [TestCategory("ExpressionTest")] 87 [TestCategory("FloatExpressionTest")] 88 public void TestDivide() 89 { 90 interpreter.FloatStack.Push(9.9, 3.3); 91 interpreter.Interpret(new FloatDivideExpression()); 92 93 Assert.AreEqual(3.0, interpreter.FloatStack.Top, delta); 94 95 TestStackCounts(floatStack: 1); 96 } 97 98 [TestMethod] 99 [TestProperty("Time", "Short")] 100 [TestCategory("ExpressionTest")] 101 [TestCategory("FloatExpressionTest")] 102 public void TestDivideWithInsufficientArguments() 103 { 104 TestWithInsufficientArguments("/", 1); 105 } 106 107 [TestMethod] 108 [TestProperty("Time", "Short")] 109 [TestCategory("ExpressionTest")] 110 [TestCategory("FloatExpressionTest")] 111 public void TestModulo() 112 { 113 interpreter.FloatStack.Push(11.6, 5.3); 114 interpreter.Interpret(new FloatModuloExpression()); 115 116 Assert.AreEqual(1, interpreter.FloatStack.Top, delta); 117 118 TestStackCounts(floatStack: 1); 119 } 120 121 [TestMethod] 122 [TestProperty("Time", "Short")] 123 [TestCategory("ExpressionTest")] 124 [TestCategory("FloatExpressionTest")] 125 public void TestModuloWithInsufficientArguments() 126 { 127 TestWithInsufficientArguments("%", 1); 128 } 129 130 [TestMethod] 131 [TestProperty("Time", "Short")] 132 [TestCategory("ExpressionTest")] 133 [TestCategory("FloatExpressionTest")] 134 public void TestMin() 135 { 136 interpreter.FloatStack.Push(10.2, 5.3); 137 interpreter.Interpret(new FloatMinExpression()); 138 139 Assert.AreEqual(5.3, interpreter.FloatStack.Top, delta); 140 141 TestStackCounts(floatStack: 1); 142 } 143 144 [TestMethod] 145 [TestProperty("Time", "Short")] 146 [TestCategory("ExpressionTest")] 147 [TestCategory("FloatExpressionTest")] 148 public void TestMinWithInsufficientArguments() 149 { 150 TestWithInsufficientArguments("MIN", 1); 151 } 152 153 [TestMethod] 154 [TestProperty("Time", "Short")] 155 [TestCategory("ExpressionTest")] 156 [TestCategory("FloatExpressionTest")] 157 public void TestMax() 158 { 159 interpreter.FloatStack.Push(10.3, 5.3); 160 interpreter.Interpret(new FloatMaxExpression()); 161 162 Assert.AreEqual(10.3, interpreter.FloatStack.Top, delta); 163 164 TestStackCounts(floatStack: 1); 165 } 166 167 [TestMethod] 168 [TestProperty("Time", "Short")] 169 [TestCategory("ExpressionTest")] 170 [TestCategory("FloatExpressionTest")] 171 public void TestMaxWithInsufficientArguments() 172 { 173 TestWithInsufficientArguments("MAX", 1); 174 } 175 176 [TestMethod] 177 [TestProperty("Time", "Short")] 178 [TestCategory("ExpressionTest")] 179 [TestCategory("FloatExpressionTest")] 180 public void TestSmallerThan() 181 { 182 interpreter.FloatStack.Push(10.2, 5.3); 183 interpreter.Interpret(new FloatSmallerThanExpression()); 184 185 Assert.AreEqual(false, interpreter.BooleanStack.Top); 186 187 TestStackCounts(booleanStack: 1); 188 } 189 190 [TestMethod] 191 [TestProperty("Time", "Short")] 192 [TestCategory("ExpressionTest")] 193 [TestCategory("FloatExpressionTest")] 194 public void TestSmallerThanWithInsufficientArguments() 195 { 196 TestWithInsufficientArguments("<", 1); 197 } 198 199 [TestMethod] 200 [TestProperty("Time", "Short")] 201 [TestCategory("ExpressionTest")] 202 [TestCategory("FloatExpressionTest")] 203 public void TestGreaterThan() 204 { 205 interpreter.FloatStack.Push(10.2, 5.3); 206 interpreter.Interpret(new FloatGreaterThanExpression()); 207 208 Assert.AreEqual(true, interpreter.BooleanStack.Top); 209 210 TestStackCounts(booleanStack: 1); 211 } 212 213 [TestMethod] 214 [TestProperty("Time", "Short")] 215 [TestCategory("ExpressionTest")] 216 [TestCategory("FloatExpressionTest")] 217 public void TestGreaterThanWithInsufficientArguments() 218 { 219 TestWithInsufficientArguments(">", 1); 220 } 221 222 [TestMethod] 223 [TestProperty("Time", "Short")] 224 [TestCategory("ExpressionTest")] 225 [TestCategory("FloatExpressionTest")] 226 public void TestFromBooleanTrue() 227 { 228 interpreter.BooleanStack.Push(true); 229 interpreter.Interpret(new FloatFromBooleanExpression()); 230 231 Assert.AreEqual(1d, interpreter.FloatStack.Top, delta); 232 233 TestStackCounts(floatStack: 1); 234 } 235 236 [TestMethod] 237 [TestProperty("Time", "Short")] 238 [TestCategory("ExpressionTest")] 239 [TestCategory("FloatExpressionTest")] 240 public void TestFromBooleanFalse() 241 { 242 interpreter.BooleanStack.Push(false); 243 interpreter.Interpret(new FloatFromBooleanExpression()); 244 245 Assert.AreEqual(0d, interpreter.FloatStack.Top, delta); 246 247 TestStackCounts(floatStack: 1); 248 } 249 250 [TestMethod] 251 [TestProperty("Time", "Short")] 252 [TestCategory("ExpressionTest")] 253 [TestCategory("FloatExpressionTest")] 254 public void TestFromBooleanWithInsufficientArguments() 255 { 256 TestWithInsufficientArguments("FROMBOOLEAN"); 257 } 258 259 [TestMethod] 260 [TestProperty("Time", "Short")] 261 [TestCategory("ExpressionTest")] 262 [TestCategory("FloatExpressionTest")] 263 public void TestFromInteger() 264 { 265 interpreter.IntegerStack.Push(5); 266 interpreter.Interpret(new FloatFromIntegerExpression()); 267 268 Assert.AreEqual(5d, interpreter.FloatStack.Top, delta); 269 270 TestStackCounts(floatStack: 1); 271 } 272 273 [TestMethod] 274 [TestProperty("Time", "Short")] 275 [TestCategory("ExpressionTest")] 276 [TestCategory("FloatExpressionTest")] 277 public void TestFromIntegerWithInsufficientArguments() 278 { 279 TestWithInsufficientArguments("FROMINTEGER"); 280 } 281 282 [TestMethod] 283 [TestProperty("Time", "Short")] 284 [TestCategory("ExpressionTest")] 285 [TestCategory("FloatExpressionTest")] 286 public void TestSine() 287 { 288 interpreter.FloatStack.Push(Math.PI / 2); 289 interpreter.Interpret(new FloatSineExpression()); 290 291 Assert.AreEqual(1, interpreter.FloatStack.Top, delta); 292 293 TestStackCounts(floatStack: 1); 294 } 295 296 [TestMethod] 297 [TestProperty("Time", "Short")] 298 [TestCategory("ExpressionTest")] 299 [TestCategory("FloatExpressionTest")] 300 public void TestSineWithInsufficientArguments() 301 { 302 TestWithInsufficientArguments("SIN"); 303 } 304 305 [TestMethod] 306 [TestProperty("Time", "Short")] 307 [TestCategory("ExpressionTest")] 308 [TestCategory("FloatExpressionTest")] 309 public void TestCosine() 310 { 311 interpreter.FloatStack.Push(Math.PI); 312 interpreter.Interpret(new FloatCosineExpression()); 313 314 Assert.AreEqual(-1, interpreter.FloatStack.Top, delta); 315 316 TestStackCounts(floatStack: 1); 317 } 318 319 [TestMethod] 320 [TestProperty("Time", "Short")] 321 [TestCategory("ExpressionTest")] 322 [TestCategory("FloatExpressionTest")] 323 public void TestCosineWithInsufficientArguments() 324 { 325 TestWithInsufficientArguments("COS"); 326 } 327 328 protected override double[] GetValues(int count) 329 { 330 var values = new double[count]; 331 332 for (var i = 0; i < count; i++) 333 { 334 values[i] = i * 0.9; 335 } 336 337 return values; 338 } 339 340 protected override void CheckOtherStacksAreEmpty() 341 { 342 TestStackCounts(floatStack: null); 343 } 344 } 3 using System; 4 5 using HeuristicLab.Algorithms.PushGP.Expressions; 6 using HeuristicLab.Algorithms.PushGP.Stack; 7 8 using Microsoft.VisualStudio.TestTools.UnitTesting; 9 10 [TestClass] 11 public class FloatExpressionTests : CommonTests<double> 12 { 13 private const double delta = 0.00001; 14 15 protected override string TypeName 16 { 17 get 18 { 19 return "FLOAT"; 20 } 21 } 22 23 protected override IStack<double> Stack 24 { 25 get 26 { 27 return this.interpreter.FloatStack; 28 } 29 } 30 31 [TestMethod] 32 [TestProperty("Time", "Short")] 33 [TestCategory("ExpressionTest")] 34 [TestCategory("FloatExpressionTest")] 35 public void TestAdd() 36 { 37 this.interpreter.FloatStack.Push(5.3, 5.3); 38 this.interpreter.Interpret(new FloatAddExpression()); 39 40 Assert.AreEqual(10.6, this.interpreter.FloatStack.Top, delta); 41 42 this.TestStackCounts(floatStack: 1); 43 } 44 45 [TestMethod] 46 [TestProperty("Time", "Short")] 47 [TestCategory("ExpressionTest")] 48 [TestCategory("FloatExpressionTest")] 49 public void TestAddWithInsufficientArguments() 50 { 51 this.TestWithInsufficientArguments("+", 1); 52 } 53 54 [TestMethod] 55 [TestProperty("Time", "Short")] 56 [TestCategory("ExpressionTest")] 57 [TestCategory("FloatExpressionTest")] 58 public void TestSubtract() 59 { 60 this.interpreter.FloatStack.Push(10.2, 5.3); 61 this.interpreter.Interpret(new FloatSubtractExpression()); 62 63 Assert.AreEqual(4.9, this.interpreter.FloatStack.Top, delta); 64 65 this.TestStackCounts(floatStack: 1); 66 } 67 68 [TestMethod] 69 [TestProperty("Time", "Short")] 70 [TestCategory("ExpressionTest")] 71 [TestCategory("FloatExpressionTest")] 72 public void TestSubractWithInsufficientArguments() 73 { 74 this.TestWithInsufficientArguments("-", 1); 75 } 76 77 [TestMethod] 78 [TestProperty("Time", "Short")] 79 [TestCategory("ExpressionTest")] 80 [TestCategory("FloatExpressionTest")] 81 public void TestMultiply() 82 { 83 this.interpreter.FloatStack.Push(9.9, 3.3); 84 this.interpreter.Interpret(new FloatMultiplyExpression()); 85 86 Assert.AreEqual(32.67, this.interpreter.FloatStack.Top, delta); 87 88 this.TestStackCounts(floatStack: 1); 89 } 90 91 [TestMethod] 92 [TestProperty("Time", "Short")] 93 [TestCategory("ExpressionTest")] 94 [TestCategory("FloatExpressionTest")] 95 public void TestMultiplyWithInsufficientArguments() 96 { 97 this.TestWithInsufficientArguments("*", 1); 98 } 99 100 [TestMethod] 101 [TestProperty("Time", "Short")] 102 [TestCategory("ExpressionTest")] 103 [TestCategory("FloatExpressionTest")] 104 public void TestDivide() 105 { 106 this.interpreter.FloatStack.Push(9.9, 3.3); 107 this.interpreter.Interpret(new FloatDivideExpression()); 108 109 Assert.AreEqual(3.0, this.interpreter.FloatStack.Top, delta); 110 111 this.TestStackCounts(floatStack: 1); 112 } 113 114 [TestMethod] 115 [TestProperty("Time", "Short")] 116 [TestCategory("ExpressionTest")] 117 [TestCategory("FloatExpressionTest")] 118 public void TestDivideWithInsufficientArguments() 119 { 120 this.TestWithInsufficientArguments("/", 1); 121 } 122 123 [TestMethod] 124 [TestProperty("Time", "Short")] 125 [TestCategory("ExpressionTest")] 126 [TestCategory("FloatExpressionTest")] 127 public void TestModulo() 128 { 129 this.interpreter.FloatStack.Push(11.6, 5.3); 130 this.interpreter.Interpret(new FloatModuloExpression()); 131 132 Assert.AreEqual(1, this.interpreter.FloatStack.Top, delta); 133 134 this.TestStackCounts(floatStack: 1); 135 } 136 137 [TestMethod] 138 [TestProperty("Time", "Short")] 139 [TestCategory("ExpressionTest")] 140 [TestCategory("FloatExpressionTest")] 141 public void TestModuloWithInsufficientArguments() 142 { 143 this.TestWithInsufficientArguments("%", 1); 144 } 145 146 [TestMethod] 147 [TestProperty("Time", "Short")] 148 [TestCategory("ExpressionTest")] 149 [TestCategory("FloatExpressionTest")] 150 public void TestMin() 151 { 152 this.interpreter.FloatStack.Push(10.2, 5.3); 153 this.interpreter.Interpret(new FloatMinExpression()); 154 155 Assert.AreEqual(5.3, this.interpreter.FloatStack.Top, delta); 156 157 this.TestStackCounts(floatStack: 1); 158 } 159 160 [TestMethod] 161 [TestProperty("Time", "Short")] 162 [TestCategory("ExpressionTest")] 163 [TestCategory("FloatExpressionTest")] 164 public void TestMinWithInsufficientArguments() 165 { 166 this.TestWithInsufficientArguments("MIN", 1); 167 } 168 169 [TestMethod] 170 [TestProperty("Time", "Short")] 171 [TestCategory("ExpressionTest")] 172 [TestCategory("FloatExpressionTest")] 173 public void TestMax() 174 { 175 this.interpreter.FloatStack.Push(10.3, 5.3); 176 this.interpreter.Interpret(new FloatMaxExpression()); 177 178 Assert.AreEqual(10.3, this.interpreter.FloatStack.Top, delta); 179 180 this.TestStackCounts(floatStack: 1); 181 } 182 183 [TestMethod] 184 [TestProperty("Time", "Short")] 185 [TestCategory("ExpressionTest")] 186 [TestCategory("FloatExpressionTest")] 187 public void TestMaxWithInsufficientArguments() 188 { 189 this.TestWithInsufficientArguments("MAX", 1); 190 } 191 192 [TestMethod] 193 [TestProperty("Time", "Short")] 194 [TestCategory("ExpressionTest")] 195 [TestCategory("FloatExpressionTest")] 196 public void TestSmallerThan() 197 { 198 this.interpreter.FloatStack.Push(10.2, 5.3); 199 this.interpreter.Interpret(new FloatSmallerThanExpression()); 200 201 Assert.AreEqual(false, this.interpreter.BooleanStack.Top); 202 203 this.TestStackCounts(booleanStack: 1); 204 } 205 206 [TestMethod] 207 [TestProperty("Time", "Short")] 208 [TestCategory("ExpressionTest")] 209 [TestCategory("FloatExpressionTest")] 210 public void TestSmallerThanWithInsufficientArguments() 211 { 212 this.TestWithInsufficientArguments("<", 1); 213 } 214 215 [TestMethod] 216 [TestProperty("Time", "Short")] 217 [TestCategory("ExpressionTest")] 218 [TestCategory("FloatExpressionTest")] 219 public void TestGreaterThan() 220 { 221 this.interpreter.FloatStack.Push(10.2, 5.3); 222 this.interpreter.Interpret(new FloatGreaterThanExpression()); 223 224 Assert.AreEqual(true, this.interpreter.BooleanStack.Top); 225 226 this.TestStackCounts(booleanStack: 1); 227 } 228 229 [TestMethod] 230 [TestProperty("Time", "Short")] 231 [TestCategory("ExpressionTest")] 232 [TestCategory("FloatExpressionTest")] 233 public void TestGreaterThanWithInsufficientArguments() 234 { 235 this.TestWithInsufficientArguments(">", 1); 236 } 237 238 [TestMethod] 239 [TestProperty("Time", "Short")] 240 [TestCategory("ExpressionTest")] 241 [TestCategory("FloatExpressionTest")] 242 public void TestFromBooleanTrue() 243 { 244 this.interpreter.BooleanStack.Push(true); 245 this.interpreter.Interpret(new FloatFromBooleanExpression()); 246 247 Assert.AreEqual(1d, this.interpreter.FloatStack.Top, delta); 248 249 this.TestStackCounts(floatStack: 1); 250 } 251 252 [TestMethod] 253 [TestProperty("Time", "Short")] 254 [TestCategory("ExpressionTest")] 255 [TestCategory("FloatExpressionTest")] 256 public void TestFromBooleanFalse() 257 { 258 this.interpreter.BooleanStack.Push(false); 259 this.interpreter.Interpret(new FloatFromBooleanExpression()); 260 261 Assert.AreEqual(0d, this.interpreter.FloatStack.Top, delta); 262 263 this.TestStackCounts(floatStack: 1); 264 } 265 266 [TestMethod] 267 [TestProperty("Time", "Short")] 268 [TestCategory("ExpressionTest")] 269 [TestCategory("FloatExpressionTest")] 270 public void TestFromBooleanWithInsufficientArguments() 271 { 272 this.TestWithInsufficientArguments("FROMBOOLEAN"); 273 } 274 275 [TestMethod] 276 [TestProperty("Time", "Short")] 277 [TestCategory("ExpressionTest")] 278 [TestCategory("FloatExpressionTest")] 279 public void TestFromInteger() 280 { 281 this.interpreter.IntegerStack.Push(5); 282 this.interpreter.Interpret(new FloatFromIntegerExpression()); 283 284 Assert.AreEqual(5d, this.interpreter.FloatStack.Top, delta); 285 286 this.TestStackCounts(floatStack: 1); 287 } 288 289 [TestMethod] 290 [TestProperty("Time", "Short")] 291 [TestCategory("ExpressionTest")] 292 [TestCategory("FloatExpressionTest")] 293 public void TestFromIntegerWithInsufficientArguments() 294 { 295 this.TestWithInsufficientArguments("FROMINTEGER"); 296 } 297 298 [TestMethod] 299 [TestProperty("Time", "Short")] 300 [TestCategory("ExpressionTest")] 301 [TestCategory("FloatExpressionTest")] 302 public void TestSine() 303 { 304 this.interpreter.FloatStack.Push(Math.PI / 2); 305 this.interpreter.Interpret(new FloatSineExpression()); 306 307 Assert.AreEqual(1, this.interpreter.FloatStack.Top, delta); 308 309 this.TestStackCounts(floatStack: 1); 310 } 311 312 [TestMethod] 313 [TestProperty("Time", "Short")] 314 [TestCategory("ExpressionTest")] 315 [TestCategory("FloatExpressionTest")] 316 public void TestSineWithInsufficientArguments() 317 { 318 this.TestWithInsufficientArguments("SIN"); 319 } 320 321 [TestMethod] 322 [TestProperty("Time", "Short")] 323 [TestCategory("ExpressionTest")] 324 [TestCategory("FloatExpressionTest")] 325 public void TestCosine() 326 { 327 this.interpreter.FloatStack.Push(Math.PI); 328 this.interpreter.Interpret(new FloatCosineExpression()); 329 330 Assert.AreEqual(-1, this.interpreter.FloatStack.Top, delta); 331 332 this.TestStackCounts(floatStack: 1); 333 } 334 335 [TestMethod] 336 [TestProperty("Time", "Short")] 337 [TestCategory("ExpressionTest")] 338 [TestCategory("FloatExpressionTest")] 339 public void TestCosineWithInsufficientArguments() 340 { 341 this.TestWithInsufficientArguments("COS"); 342 } 343 344 protected override double[] GetValues(int count) 345 { 346 var values = new double[count]; 347 348 for (var i = 0; i < count; i++) values[i] = i * 0.9; 349 350 return values; 351 } 352 353 protected override void CheckOtherStacksAreEmpty() 354 { 355 this.TestStackCounts(floatStack: null); 356 } 357 } 345 358 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Interpreter/Expressions/IntegerExpressionTests.cs
r14398 r14513 1 using HeuristicLab.Algorithms.PushGP.Expressions; 2 using HeuristicLab.Algorithms.PushGP.Stack; 3 using Microsoft.VisualStudio.TestTools.UnitTesting; 4 5 namespace HeuristicLab.Tests.Interpreter.Expressions 1 namespace HeuristicLab.Tests.Interpreter.Expressions 6 2 { 7 [TestClass] 8 public class IntegerExpressionTests : CommonTests<long> 9 { 10 protected override string TypeName { get { return "INTEGER"; } } 11 protected override IStack<long> Stack { get { return interpreter.IntegerStack; } } 12 13 [TestMethod] 14 [TestProperty("Time", "Short")] 15 [TestCategory("ExpressionTest")] 16 [TestCategory("IntegerExpressionTest")] 17 public void TestAdd() 18 { 19 interpreter.IntegerStack.Push(5, 5); 20 interpreter.Interpret(new IntegerAddExpression()); 21 22 Assert.AreEqual(10, interpreter.IntegerStack.Top); 23 24 TestStackCounts(integerStack: 1); 25 } 26 27 [TestMethod] 28 [TestProperty("Time", "Short")] 29 [TestCategory("ExpressionTest")] 30 [TestCategory("IntegerExpressionTest")] 31 public void TestAddWithInsufficientArguments() 32 { 33 TestWithInsufficientArguments("+", 1); 34 } 35 36 [TestMethod] 37 [TestProperty("Time", "Short")] 38 [TestCategory("ExpressionTest")] 39 [TestCategory("IntegerExpressionTest")] 40 public void TestSubtract() 41 { 42 interpreter.IntegerStack.Push(10, 5); 43 interpreter.Interpret(new IntegerSubtractExpression()); 44 45 Assert.AreEqual(5, interpreter.IntegerStack.Top); 46 47 TestStackCounts(integerStack: 1); 48 } 49 50 [TestMethod] 51 [TestProperty("Time", "Short")] 52 [TestCategory("ExpressionTest")] 53 [TestCategory("IntegerExpressionTest")] 54 public void TestSubtractWithInsufficientArguments() 55 { 56 TestWithInsufficientArguments("-", 1); 57 } 58 59 [TestMethod] 60 [TestProperty("Time", "Short")] 61 [TestCategory("ExpressionTest")] 62 [TestCategory("IntegerExpressionTest")] 63 public void TestMultiply() 64 { 65 interpreter.IntegerStack.Push(10, 5); 66 interpreter.Interpret(new IntegerMultiplyExpression()); 67 68 Assert.AreEqual(50, interpreter.IntegerStack.Top); 69 70 TestStackCounts(integerStack: 1); 71 } 72 73 [TestMethod] 74 [TestProperty("Time", "Short")] 75 [TestCategory("ExpressionTest")] 76 [TestCategory("IntegerExpressionTest")] 77 public void TestMultiplyWithInsufficientArguments() 78 { 79 TestWithInsufficientArguments("*", 1); 80 } 81 82 [TestMethod] 83 [TestProperty("Time", "Short")] 84 [TestCategory("ExpressionTest")] 85 [TestCategory("IntegerExpressionTest")] 86 public void TestDivide() 87 { 88 interpreter.IntegerStack.Push(10, 5); 89 interpreter.Interpret(new IntegerDivideExpression()); 90 91 Assert.AreEqual(2, interpreter.IntegerStack.Top); 92 93 TestStackCounts(integerStack: 1); 94 } 95 96 [TestMethod] 97 [TestProperty("Time", "Short")] 98 [TestCategory("ExpressionTest")] 99 [TestCategory("IntegerExpressionTest")] 100 public void TestDivideWithInsufficientArguments() 101 { 102 TestWithInsufficientArguments("/", 1); 103 } 104 105 [TestMethod] 106 [TestProperty("Time", "Short")] 107 [TestCategory("ExpressionTest")] 108 [TestCategory("IntegerExpressionTest")] 109 public void TestModulo() 110 { 111 interpreter.IntegerStack.Push(10, 5); 112 interpreter.Interpret(new IntegerModuloExpression()); 113 114 Assert.AreEqual(0, interpreter.IntegerStack.Top); 115 116 TestStackCounts(integerStack: 1); 117 } 118 119 [TestMethod] 120 [TestProperty("Time", "Short")] 121 [TestCategory("ExpressionTest")] 122 [TestCategory("IntegerExpressionTest")] 123 public void TestModuloWithInsufficientArguments() 124 { 125 TestWithInsufficientArguments("%", 1); 126 } 127 128 [TestMethod] 129 [TestProperty("Time", "Short")] 130 [TestCategory("ExpressionTest")] 131 [TestCategory("IntegerExpressionTest")] 132 public void TestMin() 133 { 134 interpreter.IntegerStack.Push(10, 5); 135 interpreter.Interpret(new IntegerMinExpression()); 136 137 Assert.AreEqual(5, interpreter.IntegerStack.Top); 138 139 TestStackCounts(integerStack: 1); 140 } 141 142 [TestMethod] 143 [TestProperty("Time", "Short")] 144 [TestCategory("ExpressionTest")] 145 [TestCategory("IntegerExpressionTest")] 146 public void TestMinWithInsufficientArguments() 147 { 148 TestWithInsufficientArguments("MIN", 1); 149 } 150 151 [TestMethod] 152 [TestProperty("Time", "Short")] 153 [TestCategory("ExpressionTest")] 154 [TestCategory("IntegerExpressionTest")] 155 public void TestMax() 156 { 157 interpreter.IntegerStack.Push(10, 5); 158 interpreter.Interpret(new IntegerMaxExpression()); 159 160 Assert.AreEqual(10, interpreter.IntegerStack.Top); 161 162 TestStackCounts(integerStack: 1); 163 } 164 165 [TestMethod] 166 [TestProperty("Time", "Short")] 167 [TestCategory("ExpressionTest")] 168 [TestCategory("IntegerExpressionTest")] 169 public void TestMaxWithInsufficientArguments() 170 { 171 TestWithInsufficientArguments("MAX", 1); 172 } 173 174 [TestMethod] 175 [TestProperty("Time", "Short")] 176 [TestCategory("ExpressionTest")] 177 [TestCategory("IntegerExpressionTest")] 178 public void TestSmallerThan() 179 { 180 interpreter.IntegerStack.Push(10, 5); 181 interpreter.Interpret(new IntegerSmallerThanExpression()); 182 183 Assert.AreEqual(false, interpreter.BooleanStack.Top); 184 185 TestStackCounts(booleanStack: 1); 186 } 187 188 [TestMethod] 189 [TestProperty("Time", "Short")] 190 [TestCategory("ExpressionTest")] 191 [TestCategory("IntegerExpressionTest")] 192 public void TestSmallerThanWithInsufficientArguments() 193 { 194 TestWithInsufficientArguments("<", 1); 195 } 196 197 [TestMethod] 198 [TestProperty("Time", "Short")] 199 [TestCategory("ExpressionTest")] 200 [TestCategory("IntegerExpressionTest")] 201 public void TestGreaterThan() 202 { 203 interpreter.IntegerStack.Push(10, 5); 204 interpreter.Interpret(new IntegerGreaterThanExpression()); 205 206 Assert.AreEqual(true, interpreter.BooleanStack.Top); 207 208 TestStackCounts(booleanStack: 1); 209 } 210 211 [TestMethod] 212 [TestProperty("Time", "Short")] 213 [TestCategory("ExpressionTest")] 214 [TestCategory("IntegerExpressionTest")] 215 public void TestGreaterThanWithInsufficientArguments() 216 { 217 TestWithInsufficientArguments(">", 1); 218 } 219 220 [TestMethod] 221 [TestProperty("Time", "Short")] 222 [TestCategory("ExpressionTest")] 223 [TestCategory("IntegerExpressionTest")] 224 public void TestFromBooleanTrue() 225 { 226 interpreter.BooleanStack.Push(true); 227 interpreter.Interpret(new IntegerFromBooleanExpression()); 228 229 Assert.AreEqual(1, interpreter.IntegerStack.Top); 230 231 TestStackCounts(integerStack: 1); 232 } 233 234 [TestMethod] 235 [TestProperty("Time", "Short")] 236 [TestCategory("ExpressionTest")] 237 [TestCategory("IntegerExpressionTest")] 238 public void TestFromBooleanWithInsufficientArguments() 239 { 240 TestWithInsufficientArguments("FROMBOOLEAN"); 241 } 242 243 [TestMethod] 244 [TestProperty("Time", "Short")] 245 [TestCategory("ExpressionTest")] 246 [TestCategory("IntegerExpressionTest")] 247 public void TestFromBooleanFalse() 248 { 249 interpreter.BooleanStack.Push(false); 250 interpreter.Interpret(new IntegerFromBooleanExpression()); 251 252 Assert.AreEqual(0, interpreter.IntegerStack.Top); 253 254 TestStackCounts(integerStack: 1); 255 } 256 257 [TestMethod] 258 [TestProperty("Time", "Short")] 259 [TestCategory("ExpressionTest")] 260 [TestCategory("IntegerExpressionTest")] 261 public void TestFromFloat() 262 { 263 interpreter.FloatStack.Push(1.5); 264 interpreter.Interpret(new IntegerFromFloatExpression()); 265 266 Assert.AreEqual(1, interpreter.IntegerStack.Top); 267 268 TestStackCounts(integerStack: 1); 269 } 270 271 [TestMethod] 272 [TestProperty("Time", "Short")] 273 [TestCategory("ExpressionTest")] 274 [TestCategory("IntegerExpressionTest")] 275 public void TestFromFloatWithInsufficientArguments() 276 { 277 TestWithInsufficientArguments("FROMFLOAT"); 278 } 279 280 protected override long[] GetValues(int count) 281 { 282 var values = new long[count]; 283 284 for (long i = 0; i < count; i++) 285 { 286 values[i] = i; 287 } 288 289 return values; 290 } 291 292 protected override void CheckOtherStacksAreEmpty() 293 { 294 TestStackCounts(integerStack: null); 295 } 296 } 3 using HeuristicLab.Algorithms.PushGP.Expressions; 4 using HeuristicLab.Algorithms.PushGP.Stack; 5 6 using Microsoft.VisualStudio.TestTools.UnitTesting; 7 8 [TestClass] 9 public class IntegerExpressionTests : CommonTests<long> 10 { 11 protected override string TypeName 12 { 13 get 14 { 15 return "INTEGER"; 16 } 17 } 18 19 protected override IStack<long> Stack 20 { 21 get 22 { 23 return this.interpreter.IntegerStack; 24 } 25 } 26 27 [TestMethod] 28 [TestProperty("Time", "Short")] 29 [TestCategory("ExpressionTest")] 30 [TestCategory("IntegerExpressionTest")] 31 public void TestAdd() 32 { 33 this.interpreter.IntegerStack.Push(5, 5); 34 this.interpreter.Interpret(new IntegerAddExpression()); 35 36 Assert.AreEqual(10, this.interpreter.IntegerStack.Top); 37 38 this.TestStackCounts(integerStack: 1); 39 } 40 41 [TestMethod] 42 [TestProperty("Time", "Short")] 43 [TestCategory("ExpressionTest")] 44 [TestCategory("IntegerExpressionTest")] 45 public void TestAddWithInsufficientArguments() 46 { 47 this.TestWithInsufficientArguments("+", 1); 48 } 49 50 [TestMethod] 51 [TestProperty("Time", "Short")] 52 [TestCategory("ExpressionTest")] 53 [TestCategory("IntegerExpressionTest")] 54 public void TestSubtract() 55 { 56 this.interpreter.IntegerStack.Push(10, 5); 57 this.interpreter.Interpret(new IntegerSubtractExpression()); 58 59 Assert.AreEqual(5, this.interpreter.IntegerStack.Top); 60 61 this.TestStackCounts(integerStack: 1); 62 } 63 64 [TestMethod] 65 [TestProperty("Time", "Short")] 66 [TestCategory("ExpressionTest")] 67 [TestCategory("IntegerExpressionTest")] 68 public void TestSubtractWithInsufficientArguments() 69 { 70 this.TestWithInsufficientArguments("-", 1); 71 } 72 73 [TestMethod] 74 [TestProperty("Time", "Short")] 75 [TestCategory("ExpressionTest")] 76 [TestCategory("IntegerExpressionTest")] 77 public void TestMultiply() 78 { 79 this.interpreter.IntegerStack.Push(10, 5); 80 this.interpreter.Interpret(new IntegerMultiplyExpression()); 81 82 Assert.AreEqual(50, this.interpreter.IntegerStack.Top); 83 84 this.TestStackCounts(integerStack: 1); 85 } 86 87 [TestMethod] 88 [TestProperty("Time", "Short")] 89 [TestCategory("ExpressionTest")] 90 [TestCategory("IntegerExpressionTest")] 91 public void TestMultiplyWithInsufficientArguments() 92 { 93 this.TestWithInsufficientArguments("*", 1); 94 } 95 96 [TestMethod] 97 [TestProperty("Time", "Short")] 98 [TestCategory("ExpressionTest")] 99 [TestCategory("IntegerExpressionTest")] 100 public void TestDivide() 101 { 102 this.interpreter.IntegerStack.Push(10, 5); 103 this.interpreter.Interpret(new IntegerDivideExpression()); 104 105 Assert.AreEqual(2, this.interpreter.IntegerStack.Top); 106 107 this.TestStackCounts(integerStack: 1); 108 } 109 110 [TestMethod] 111 [TestProperty("Time", "Short")] 112 [TestCategory("ExpressionTest")] 113 [TestCategory("IntegerExpressionTest")] 114 public void TestDivideWithInsufficientArguments() 115 { 116 this.TestWithInsufficientArguments("/", 1); 117 } 118 119 [TestMethod] 120 [TestProperty("Time", "Short")] 121 [TestCategory("ExpressionTest")] 122 [TestCategory("IntegerExpressionTest")] 123 public void TestModulo() 124 { 125 this.interpreter.IntegerStack.Push(10, 5); 126 this.interpreter.Interpret(new IntegerModuloExpression()); 127 128 Assert.AreEqual(0, this.interpreter.IntegerStack.Top); 129 130 this.TestStackCounts(integerStack: 1); 131 } 132 133 [TestMethod] 134 [TestProperty("Time", "Short")] 135 [TestCategory("ExpressionTest")] 136 [TestCategory("IntegerExpressionTest")] 137 public void TestModuloWithInsufficientArguments() 138 { 139 this.TestWithInsufficientArguments("%", 1); 140 } 141 142 [TestMethod] 143 [TestProperty("Time", "Short")] 144 [TestCategory("ExpressionTest")] 145 [TestCategory("IntegerExpressionTest")] 146 public void TestMin() 147 { 148 this.interpreter.IntegerStack.Push(10, 5); 149 this.interpreter.Interpret(new IntegerMinExpression()); 150 151 Assert.AreEqual(5, this.interpreter.IntegerStack.Top); 152 153 this.TestStackCounts(integerStack: 1); 154 } 155 156 [TestMethod] 157 [TestProperty("Time", "Short")] 158 [TestCategory("ExpressionTest")] 159 [TestCategory("IntegerExpressionTest")] 160 public void TestMinWithInsufficientArguments() 161 { 162 this.TestWithInsufficientArguments("MIN", 1); 163 } 164 165 [TestMethod] 166 [TestProperty("Time", "Short")] 167 [TestCategory("ExpressionTest")] 168 [TestCategory("IntegerExpressionTest")] 169 public void TestMax() 170 { 171 this.interpreter.IntegerStack.Push(10, 5); 172 this.interpreter.Interpret(new IntegerMaxExpression()); 173 174 Assert.AreEqual(10, this.interpreter.IntegerStack.Top); 175 176 this.TestStackCounts(integerStack: 1); 177 } 178 179 [TestMethod] 180 [TestProperty("Time", "Short")] 181 [TestCategory("ExpressionTest")] 182 [TestCategory("IntegerExpressionTest")] 183 public void TestMaxWithInsufficientArguments() 184 { 185 this.TestWithInsufficientArguments("MAX", 1); 186 } 187 188 [TestMethod] 189 [TestProperty("Time", "Short")] 190 [TestCategory("ExpressionTest")] 191 [TestCategory("IntegerExpressionTest")] 192 public void TestSmallerThan() 193 { 194 this.interpreter.IntegerStack.Push(10, 5); 195 this.interpreter.Interpret(new IntegerSmallerThanExpression()); 196 197 Assert.AreEqual(false, this.interpreter.BooleanStack.Top); 198 199 this.TestStackCounts(booleanStack: 1); 200 } 201 202 [TestMethod] 203 [TestProperty("Time", "Short")] 204 [TestCategory("ExpressionTest")] 205 [TestCategory("IntegerExpressionTest")] 206 public void TestSmallerThanWithInsufficientArguments() 207 { 208 this.TestWithInsufficientArguments("<", 1); 209 } 210 211 [TestMethod] 212 [TestProperty("Time", "Short")] 213 [TestCategory("ExpressionTest")] 214 [TestCategory("IntegerExpressionTest")] 215 public void TestGreaterThan() 216 { 217 this.interpreter.IntegerStack.Push(10, 5); 218 this.interpreter.Interpret(new IntegerGreaterThanExpression()); 219 220 Assert.AreEqual(true, this.interpreter.BooleanStack.Top); 221 222 this.TestStackCounts(booleanStack: 1); 223 } 224 225 [TestMethod] 226 [TestProperty("Time", "Short")] 227 [TestCategory("ExpressionTest")] 228 [TestCategory("IntegerExpressionTest")] 229 public void TestGreaterThanWithInsufficientArguments() 230 { 231 this.TestWithInsufficientArguments(">", 1); 232 } 233 234 [TestMethod] 235 [TestProperty("Time", "Short")] 236 [TestCategory("ExpressionTest")] 237 [TestCategory("IntegerExpressionTest")] 238 public void TestFromBooleanTrue() 239 { 240 this.interpreter.BooleanStack.Push(true); 241 this.interpreter.Interpret(new IntegerFromBooleanExpression()); 242 243 Assert.AreEqual(1, this.interpreter.IntegerStack.Top); 244 245 this.TestStackCounts(integerStack: 1); 246 } 247 248 [TestMethod] 249 [TestProperty("Time", "Short")] 250 [TestCategory("ExpressionTest")] 251 [TestCategory("IntegerExpressionTest")] 252 public void TestFromBooleanWithInsufficientArguments() 253 { 254 this.TestWithInsufficientArguments("FROMBOOLEAN"); 255 } 256 257 [TestMethod] 258 [TestProperty("Time", "Short")] 259 [TestCategory("ExpressionTest")] 260 [TestCategory("IntegerExpressionTest")] 261 public void TestFromBooleanFalse() 262 { 263 this.interpreter.BooleanStack.Push(false); 264 this.interpreter.Interpret(new IntegerFromBooleanExpression()); 265 266 Assert.AreEqual(0, this.interpreter.IntegerStack.Top); 267 268 this.TestStackCounts(integerStack: 1); 269 } 270 271 [TestMethod] 272 [TestProperty("Time", "Short")] 273 [TestCategory("ExpressionTest")] 274 [TestCategory("IntegerExpressionTest")] 275 public void TestFromFloat() 276 { 277 this.interpreter.FloatStack.Push(1.5); 278 this.interpreter.Interpret(new IntegerFromFloatExpression()); 279 280 Assert.AreEqual(1, this.interpreter.IntegerStack.Top); 281 282 this.TestStackCounts(integerStack: 1); 283 } 284 285 [TestMethod] 286 [TestProperty("Time", "Short")] 287 [TestCategory("ExpressionTest")] 288 [TestCategory("IntegerExpressionTest")] 289 public void TestFromFloatWithInsufficientArguments() 290 { 291 this.TestWithInsufficientArguments("FROMFLOAT"); 292 } 293 294 protected override long[] GetValues(int count) 295 { 296 var values = new long[count]; 297 298 for (long i = 0; i < count; i++) values[i] = i; 299 300 return values; 301 } 302 303 protected override void CheckOtherStacksAreEmpty() 304 { 305 this.TestStackCounts(integerStack: null); 306 } 307 } 297 308 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Interpreter/Expressions/NameExpressionTests.cs
r14398 r14513 1 using HeuristicLab.Algorithms.PushGP.Expressions; 2 using HeuristicLab.Algorithms.PushGP.Generators; 3 using HeuristicLab.Algorithms.PushGP.Stack; 4 using Microsoft.VisualStudio.TestTools.UnitTesting; 1 namespace HeuristicLab.Tests.Interpreter.Expressions 2 { 3 using HeuristicLab.Algorithms.PushGP.Expressions; 4 using HeuristicLab.Algorithms.PushGP.Generators; 5 using HeuristicLab.Algorithms.PushGP.Stack; 5 6 6 namespace HeuristicLab.Tests.Interpreter.Expressions 7 { 8 [TestClass] 9 public class NameExpressionTests : CommonTests<string> 7 using Microsoft.VisualStudio.TestTools.UnitTesting; 8 9 [TestClass] 10 public class NameExpressionTests : CommonTests<string> 11 { 12 protected override string TypeName 10 13 { 14 get 15 { 16 return "NAME"; 17 } 18 } 11 19 12 protected override string TypeName { get { return "NAME"; } } 13 protected override IStack<string> Stack { get { return interpreter.NameStack; } } 20 protected override IStack<string> Stack 21 { 22 get 23 { 24 return this.interpreter.NameStack; 25 } 26 } 14 27 15 16 17 18 19 20 21 28 [TestMethod] 29 [TestProperty("Time", "Short")] 30 [TestCategory("ExpressionTest")] 31 [TestCategory("NameExpressionTest")] 32 public void TestRand() 33 { 34 this.interpreter.Interpret(new NameRandExpression()); 22 35 23 Assert.IsTrue(interpreter.NameStack.Count == 1);36 Assert.IsTrue(this.interpreter.NameStack.Count == 1); 24 37 25 26 38 this.CheckOtherStacksAreEmpty(); 39 } 27 40 28 29 30 31 32 33 34 35 36 41 [TestMethod] 42 [TestProperty("Time", "Short")] 43 [TestCategory("ExpressionTest")] 44 [TestCategory("NameExpressionTest")] 45 public void TestRandomBound() 46 { 47 this.interpreter.CustomExpressions.Add("c1", new CodeNoopExpression()); 48 this.interpreter.CustomExpressions.Add("c2", new CodeNoopExpression()); 49 this.interpreter.CustomExpressions.Add("c3", new CodeNoopExpression()); 37 50 38 51 this.interpreter.Interpret(new NameRandBoundNameExpression()); 39 52 40 Assert.IsTrue(interpreter.CustomExpressions.Keys.Contains(interpreter.NameStack.Top));53 Assert.IsTrue(this.interpreter.CustomExpressions.Keys.Contains(this.interpreter.NameStack.Top)); 41 54 42 43 55 this.CheckOtherStacksAreEmpty(); 56 } 44 57 45 46 47 48 49 50 51 52 58 [TestMethod] 59 [TestProperty("Time", "Short")] 60 [TestCategory("ExpressionTest")] 61 [TestCategory("NameExpressionTest")] 62 public void TestRandomBoundWithInsufficientArguments() 63 { 64 this.TestWithInsufficientArguments("RANDBOUNDNAME"); 65 } 53 66 54 55 56 57 58 59 60 67 [TestMethod] 68 [TestProperty("Time", "Short")] 69 [TestCategory("ExpressionTest")] 70 [TestCategory("NameExpressionTest")] 71 public void TestQuote() 72 { 73 this.interpreter.Interpret(new NameQuoteExpression()); 61 74 62 Assert.IsTrue(interpreter.IsNameQuoteFlagSet);75 Assert.IsTrue(this.interpreter.IsNameQuoteFlagSet); 63 76 64 65 77 this.TestStackCounts(); 78 } 66 79 67 68 69 70 80 protected override string[] GetValues(int count) 81 { 82 var ng = new NameGenerator(); 83 var values = new string[count]; 71 84 72 for (var i = 0; i < count; i++) 73 { 74 values[i] = ng.GetNextString(); 75 } 85 for (var i = 0; i < count; i++) values[i] = ng.GetNextString(); 76 86 77 78 87 return values; 88 } 79 89 80 protected override void CheckOtherStacksAreEmpty() 81 { 82 TestStackCounts(nameStack: null); 83 } 90 protected override void CheckOtherStacksAreEmpty() 91 { 92 this.TestStackCounts(nameStack: null); 84 93 } 94 } 85 95 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Properties/AssemblyInfo.cs
r14323 r14513 1 1 using System.Reflection; 2 using System.Runtime.CompilerServices;3 2 using System.Runtime.InteropServices; 4 3 … … 6 5 // set of attributes. Change these attribute values to modify the information 7 6 // associated with an assembly. 7 8 8 [assembly: AssemblyTitle("HeuristicLab.Tests")] 9 9 [assembly: AssemblyDescription("")] … … 18 18 // to COM components. If you need to access a type in this assembly from 19 19 // COM, set the ComVisible attribute to true on that type. 20 20 21 [assembly: ComVisible(false)] 21 22 22 23 // The following GUID is for the ID of the typelib if this project is exposed to COM 24 23 25 [assembly: Guid("117b7d68-ed7d-4350-996b-9ca15259e107")] 24 26 … … 33 35 // by using the '*' as shown below: 34 36 // [assembly: AssemblyVersion("1.0.*")] 37 35 38 [assembly: AssemblyVersion("1.0.0.0")] 36 39 [assembly: AssemblyFileVersion("1.0.0.0")]
Note: See TracChangeset
for help on using the changeset viewer.