using System; using System.Collections.Generic; using System.Text; using UnitTestSharp; using Sunweaver.Commands; using Sunweaver.Commands.Abstracts; using Sunweaver.VM; namespace Sunweaver.UnitTesting { public class ChromosomeTests : TestFixture { public class ToStringTests : TestFixture { public void EmbeddedCodule() { var original = "{ 2 3 add } call"; var chromo = Chromosome.Parse(original); var actual = chromo.ToString(); var expected = "{ 2 3 add } call"; CheckEqual(expected, actual); } public void CoduleDefAtTop() { var original = "{five 2 3 add } five call"; var chromo = Chromosome.Parse(original); var actual = chromo.ToString(); var expected = "{five 2 3 add } five call"; CheckEqual(expected, actual); } public void CoduleExplicit() { var original = "@{ 2 3 add }"; var chromo = Chromosome.Parse(original); var actual = chromo.ToString(); var expected = "@{ 2 3 add }"; CheckEqual(expected, actual); } public void DoubleEmbedded() { var original = "@{ @{ 2 } @{ 3 } add }"; var chromo = Chromosome.Parse(original); var actual = chromo.ToString(); var expected = "@{ @{ 2 } @{ 3 } add }"; CheckEqual(expected, actual); } } public class ExamplePrograms : TestFixture { public void CyclicMacros() { string program = @" macro one two add macro two three add macro three one add 7 7 7 7 7 one"; List messages; var chromo = new Chromosome(); Sunweaver.Parsing.Parser.ParseDNA(program, ref chromo, out messages); CheckEqual(3, messages.Count); CheckEqualRegex("one->two->three", messages[0].message); CheckEqualRegex("two->three->one", messages[1].message); CheckEqualRegex("three->one->two", messages[2].message); Sunweaver.VM.Memory memory = new Memory(); var VM = chromo.Execute(memory); CheckEqual(2, VM.Stack.Count); CheckEqual(28, VM.Stack.Pop()); } public void Example1() { string program = @" 'This doesn't do much, but it shows some of the tricks you can do, and how you 'might organize your code const A 10 'A will be 10 const B 20 'B will be 20 const C 30 'C will be 30 'define a custom command pyth, which returns the other value of the pythagorean 'triplet {pyth dup mul swap dup mul add sqrt } 'Make it look like pyth is an in-built command macro pyth @pyth ' To make rough looking if/else branches macro else not 'Store the hypotenuses in memory locations 10, 20, and 30 3 4 pyth .A 6 8 pyth .B 8 15 pyth .C 'If the value in 10 is less than the value in 20 *A *B < { 'Store how much larger A is than B in A *A *B sub .A } branch { 'Store the sum of A and B in A *A *B add .A } else branch"; List messages; var chromo = new Chromosome(); Sunweaver.Parsing.Parser.ParseDNA(program, ref chromo, out messages); CheckEqual(new Sunweaver.Parsing.CompilationMessage[] { }, messages); Sunweaver.VM.Memory memory = new Memory(); var VM = chromo.Execute(memory); CheckEqual(0, VM.Stack.Count); CheckEqual(1, VM.BoolStack.Count); CheckEqual(-5, memory[10]); } } public class ForkTests : TestFixture { public void Basic() { var original = "@{ @{ 2 } @{ 3 } add }"; var chromoWorking = Chromosome.Parse(original); var chromoPristine = Chromosome.Parse(original); var fork = chromoWorking.Fork(); fork.Codules.Clear(); fork.Codules.Add(0, new Codule()); CheckEqual(chromoPristine, chromoWorking); CheckNotEqual(chromoWorking, fork); } } } }