public void ThenTheSecondBlockIsMutatedAndTheFirstBlockRemainsTheSameForEachRule() { var actualRandom = new System.Random(); var randomMock = new Mock <System.Random>(); _calls = 0; randomMock.Setup(x => x.NextDouble()) .Returns(() => { ++_calls; return(_calls == 14 || _calls == 29 ? -1 : 0); // Forces an always successful mutation on second run }); randomMock.Setup(x => x.Next(It.IsAny <int>(), It.IsAny <int>())) .Returns((int lowerBound, int upperBound) => actualRandom.Next(lowerBound, upperBound)); PlantMutation mutation = new PlantMutation(randomMock.Object, 0); RuleSet ruleSet = new RuleSet(new Dictionary <string, List <LSystemRule> > { { "F", new List <LSystemRule> { new LSystemRule { Probability = 1, Rule = "+F[+F+F][+F+F+F]" } } }, { "A", new List <LSystemRule> { new LSystemRule { Probability = 1, Rule = "+A[+A+A][+A+A+A]" } } } }); Debug.Log("Original F Rule: " + ruleSet.Rules["F"][0].Rule); Debug.Log("Original A Rule: " + ruleSet.Rules["A"][0].Rule); Color color = Color.black; RuleSet mutatedRuleSet = mutation.Mutate(ruleSet, ref color); string fRule = mutatedRuleSet.Rules["F"][0].Rule; string aRule = mutatedRuleSet.Rules["A"][0].Rule; Debug.Log("Entire F Rule: " + fRule); Debug.Log("Entire A Rule: " + aRule); Debug.Log("Mutated F Block: " + fRule.Substring(8, fRule.Length - 8)); Debug.Log("Mutated A Block: " + aRule.Substring(8, aRule.Length - 8)); Assert.That(fRule.Substring(0, 2), Is.EqualTo("+F")); Assert.That(fRule.Substring(2, 6), Is.EqualTo("[+F+F]")); Assert.That(fRule.Substring(8, fRule.Length - 8).Length, Is.AtLeast(3)); Assert.That(fRule.Substring(8, fRule.Length - 8), Is.Not.EqualTo("[+F+F+F]")); Assert.That(aRule.Substring(0, 2), Is.EqualTo("+A")); Assert.That(aRule.Substring(2, 6), Is.EqualTo("[+A+A]")); Assert.That(aRule.Substring(8, aRule.Length - 8).Length, Is.AtLeast(3)); Assert.That(aRule.Substring(8, aRule.Length - 8), Is.Not.EqualTo("[+A+A+A]")); }
public void ThenTheCharactersBetweenTheBracketsAreReturned() { var randomMock = new Mock <System.Random>(); randomMock.Setup(x => x.NextDouble()) .Returns(0); PlantMutation mutation = new PlantMutation(randomMock.Object, 0); RuleSet ruleSet = new RuleSet(new Dictionary <string, List <LSystemRule> > { { "F", new List <LSystemRule> { new LSystemRule { Probability = 1, Rule = "+F[+F+F]FFFFF[+F+F+F]" } } } }); Debug.Log("Original Rule: " + ruleSet.Rules["F"][0].Rule); Color color = Color.black; RuleSet mutatedRuleSet = mutation.Mutate(ruleSet, ref color); string fRule = mutatedRuleSet.Rules["F"][0].Rule; Debug.Log("After Mutation Rule: " + fRule); Assert.That(fRule, Is.EqualTo("+F[+F+F]FFFFF[+F+F+F]")); }
public void ThenNoMutationOccurs() { PlantMutation mutation = new PlantMutation(new System.Random(), 0); RuleSet ruleSet = new RuleSet(new Dictionary <string, List <LSystemRule> > { { "F", new List <LSystemRule> { new LSystemRule { Probability = 1, Rule = "+F" } } } }); Debug.Log("Original Rule: " + ruleSet.Rules["F"][0].Rule); Color color = Color.black; RuleSet mutatedRuleSet = mutation.Mutate(ruleSet, ref color); string fRule = mutatedRuleSet.Rules["F"][0].Rule; Debug.Log("After Mutation Rule: " + fRule); Assert.That(fRule, Is.EqualTo("+F")); }
public void ThenOneExtraBracketAppearsInTheRule() { var actualRandom = new System.Random(); var randomMock = new Mock <System.Random>(); randomMock.Setup(x => x.NextDouble()) .Returns(() => { ++_nextDoubleCalls; return(_nextDoubleCalls == 8 ? -1 : 0); }); randomMock.Setup(x => x.Next(It.IsAny <int>(), It.IsAny <int>())) .Returns((int lowerBound, int upperBound) => { ++_nextCalls; if (_nextCalls == 1) { return(0); } return(actualRandom.Next(lowerBound, upperBound)); }); PlantMutation mutation = new PlantMutation(randomMock.Object, 0); RuleSet ruleSet = new RuleSet(new Dictionary <string, List <LSystemRule> > { { "F", new List <LSystemRule> { new LSystemRule { Probability = 1, Rule = "+F[+F+F]" } } } }); Debug.Log("Original Rule: " + ruleSet.Rules["F"][0].Rule); Color color = Color.black; RuleSet mutatedRuleSet = mutation.Mutate(ruleSet, ref color); string fRule = mutatedRuleSet.Rules["F"][0].Rule; Debug.Log("After Mutation Rule: " + fRule); Assert.That(fRule.Count(x => x == '['), Is.EqualTo(2)); Assert.That(fRule.Count(x => x == ']'), Is.EqualTo(2)); }
public void ThenTheRuleDoesNotChange() { var randomMock = new Mock <System.Random>(); randomMock.Setup(x => x.NextDouble()) .Returns(0); PlantMutation mutation = new PlantMutation(randomMock.Object, 0); RuleSet ruleSet = new RuleSet(new Dictionary <string, List <LSystemRule> > { { "F", new List <LSystemRule> { new LSystemRule { Probability = 1, Rule = "+F[+F+F]" } } }, { "A", new List <LSystemRule> { new LSystemRule { Probability = 1, Rule = "+A[+A+A]" } } } }); Debug.Log("Original F Rule: " + ruleSet.Rules["F"][0].Rule); Debug.Log("Original A Rule: " + ruleSet.Rules["A"][0].Rule); Color color = Color.black; RuleSet mutatedRuleSet = mutation.Mutate(ruleSet, ref color); string fRule = mutatedRuleSet.Rules["F"][0].Rule; string aRule = mutatedRuleSet.Rules["A"][0].Rule; Debug.Log("After Mutation F Rule: " + fRule); Debug.Log("After Mutation A Rule: " + aRule); Assert.That(fRule, Is.EqualTo("+F[+F+F]")); Assert.That(aRule, Is.EqualTo("+A[+A+A]")); }
public void ThenTheFirstBlockIsMutated() { var actualRandom = new System.Random(); var randomMock = new Mock <System.Random>(); randomMock.Setup(x => x.NextDouble()) .Returns(() => { ++_calls; return(_calls == 7 ? -1 : 0); }); // Forces an always successful mutation on first run randomMock.Setup(x => x.Next(It.IsAny <int>(), It.IsAny <int>())) .Returns((int lowerBound, int upperBound) => actualRandom.Next(lowerBound, upperBound)); PlantMutation mutation = new PlantMutation(randomMock.Object, 0); RuleSet ruleSet = new RuleSet(new Dictionary <string, List <LSystemRule> > { { "F", new List <LSystemRule> { new LSystemRule { Probability = 1, Rule = "+F[+F+F]" } } } }); Debug.Log("Original Rule: " + ruleSet.Rules["F"][0].Rule); Color color = Color.black; RuleSet mutatedRuleSet = mutation.Mutate(ruleSet, ref color); string fRule = mutatedRuleSet.Rules["F"][0].Rule; Debug.Log("Entire Rule: " + fRule); Debug.Log("Mutated Block: " + fRule.Substring(2, fRule.Length - 2)); Assert.That(fRule.Substring(0, 2), Is.EqualTo("+F")); Assert.That(fRule.Substring(2, fRule.Length - 2).Length, Is.AtLeast(3)); Assert.That(fRule.Substring(2, fRule.Length - 2), Is.Not.EqualTo("[+F+F]")); }