using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Sunweaver.Commands.Abstracts; using Sunweaver.VM; using Sunweaver.Commands; namespace Sunweaver.UnitTesting.Commands { public class loopTests : UnitTestSharp.TestFixture { BasePair bp = new loop(); VirtualMachine VM; Codule codule; public override void TestSetup() { VM = new VirtualMachine(); codule = new Codule(); codule.BasePairs.Add(bp); } public void Basic() { VM.Stack.Push(10); VM.Stack.Push(77); VM.CallingChromosome = new Chromosome(new[] { codule }); VM.CallingChromosome.Codules.Add(77, new Codule(new BasePair[] { new Constant(10) })); codule.Execute(VM); var nums = new[] { 10, 1, 10, 2, 10, 3, 10, 4, 10, 5, 10, 6, 10, 7, 10, 8, 10, 9, 10, 10, }; CheckEqual(nums, VM.Stack); } public void NegativeCodule() { VM.Stack.Push(10); VM.Stack.Push(-77); VM.CallingChromosome = new Chromosome(new[] { codule }); VM.CallingChromosome.Codules.Add(77, new Codule(new BasePair[] { new Constant(10) })); codule.Execute(VM); var nums = new[] { 10, 1, 10, 2, 10, 3, 10, 4, 10, 5, 10, 6, 10, 7, 10, 8, 10, 9, 10, 10, }; CheckEqual(nums, VM.Stack); } public void NegativeLoopCount() { VM.Stack.Push(-10); VM.Stack.Push(-77); VM.CallingChromosome = new Chromosome(new[] { codule }); VM.CallingChromosome.Codules.Add(77, new Codule(new BasePair[] { new Constant(10) })); codule.Execute(VM); var nums = new[] { 10, -1, 10, -2, 10, -3, 10, -4, 10, -5, 10, -6, 10, -7, 10, -8, 10, -9, 10, -10, }; CheckEqual(nums, VM.Stack); } public void HighCoduleIndexModulus() { VM.Stack.Push(10); VM.Stack.Push(10077); VM.CallingChromosome = new Chromosome(new[] { codule }); VM.CallingChromosome.Codules.Add(77, new Codule(new BasePair[] { new Constant(10) })); codule.Execute(VM); var nums = new[] { 10, 1, 10, 2, 10, 3, 10, 4, 10, 5, 10, 6, 10, 7, 10, 8, 10, 9, 10, 10, }; CheckEqual(nums, VM.Stack); } public void NegativeHighCoduleIndexModulus() { VM.Stack.Push(10); VM.Stack.Push(-10077); VM.CallingChromosome = new Chromosome(new[] { codule }); VM.CallingChromosome.Codules.Add(77, new Codule(new BasePair[] { new Constant(10) })); codule.Execute(VM); var nums = new[] { 10, 1, 10, 2, 10, 3, 10, 4, 10, 5, 10, 6, 10, 7, 10, 8, 10, 9, 10, 10, }; CheckEqual(nums, VM.Stack); } public void FalseOnBoolStack() { VM.Stack.Push(10); VM.Stack.Push(-10077); VM.BoolStack.Push(false); VM.CallingChromosome = new Chromosome(new[] { codule }); VM.CallingChromosome.Codules.Add(77, new Codule(new BasePair[] { new Constant(10) })); codule.Execute(VM); var nums = new[] { 10, 1, 10, 2, 10, 3, 10, 4, 10, 5, 10, 6, 10, 7, 10, 8, 10, 9, 10, 10, }; CheckEqual(nums, VM.Stack); } public void NoRecursion() { VM.Stack.Push(10); VM.Stack.Push(77); VM.CallingChromosome = new Chromosome(new[] { codule }); VM.CallingChromosome.Codules.Add(77, new Codule(new BasePair[] { new Label("", 10), new Label("", 77), new loop(), })); codule.Execute(VM); var nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, }; CheckEqual(nums, VM.Stack); } public void AbortsOnNoArguemnts() { VM.CallingChromosome = new Chromosome(new[] { codule }); VM.CallingChromosome.Codules.Add(77, new Codule(new BasePair[] { new Label("", 10), })); codule.Execute(VM); CheckEqual(0, VM.Stack.Count()); } public void AbortsOnOneArguemnts() { VM.Stack.Push(100); VM.CallingChromosome = new Chromosome(new[] { codule }); VM.CallingChromosome.Codules.Add(77, new Codule(new BasePair[] { new Label("", 10), })); codule.Execute(VM); CheckEqual(1, VM.Stack.Count()); CheckEqual(100, VM.Stack.Pop()); } public void ZeroCountLoop() { VM.Stack.Push(0); VM.Stack.Push(77); VM.CallingChromosome = new Chromosome(new[] { codule }); VM.CallingChromosome.Codules.Add(77, new Codule(new BasePair[] { new Label("", 10), })); codule.Execute(VM); CheckEqual(0, VM.Stack.Count()); } public void NoRepeatsInCallstack() { VM.Stack.Push(3); VM.Stack.Push(77); VM.CallingChromosome = new Chromosome(new[] { codule }); VM.CallingChromosome.Codules.Add(77, new Codule(new BasePair[] { new Constant(33), new loop(), })); VM.CallingChromosome.Codules.Add(33, new Codule(new BasePair[] { new Constant(3), new Constant(77), new loop(), })); var expected = new int[] { 1, 1, 2, 1, 2, 3, }; codule.Execute(VM); CheckEqual(expected, VM.Stack); } public void NameTest() { CheckEqual("loop", bp.ToString()); } } }