private bool MutateThisRepetition(int i, RepetitionsToMutate o) { switch (o) { case RepetitionsToMutate.All: return(true); case RepetitionsToMutate.None: return(false); case RepetitionsToMutate.Even: return(i % 2 == 0); case RepetitionsToMutate.Odd: return(i % 2 == 1); default: throw new NotSupportedException(); } }
public void CanRepeatedlyEnsureMemoryUpToDate(bool makeChildUpToDate, bool makeParentUpToDate, RepetitionsToMutate mutateParent, RepetitionsToMutate mutateChild) { Example e = GetTypicalExample(); e.MyChild1 = new ExampleChildInherited() { MyInt = 25 }; Example c1 = null, c2 = null; // early clones -- make sure unaffected int repetitions = 8; Random r = new Random(); long randLong = 0; short randShort = 0; for (int i = 0; i < repetitions; i++) { //if (doNotAutomaticallyReturnToPool && e.LazinatorMemoryStorage.IsEmpty == false) // e.LazinatorMemoryStorage.LazinatorShouldNotReturnToPool(); if (i == 0) { e.MyChild1.MyLong = 0; e.MyChild1.MyShort = 0; ((ExampleChildInherited)e.MyChild1).MyInt = 0; } if (i == 5) { c1 = e.CloneLazinatorTyped(IncludeChildrenMode.IncludeAllChildren, CloneBufferOptions.IndependentBuffers); c2 = e.CloneNoBuffer(); } if (MutateThisRepetition(i, mutateParent)) { e.MyBool = !e.MyBool; } if (MutateThisRepetition(i, mutateChild)) { if (i > 2) { e.MyChild1.MyLong.Should().Be(randLong); e.MyChild1.MyShort.Should().Be(randShort); ((ExampleChildInherited)e.MyChild1).MyInt.Should().Be(randShort); } unchecked { randLong = r.Next(0, 2) == 0 ? r.Next(1, 100) : r.Next(); randShort = (short)(r.Next(0, 2) == 0 ? r.Next(1, 100) : r.Next()); } e.MyChild1.MyLong = randLong; e.MyChild1.MyShort = randShort; ((ExampleChildInherited)e.MyChild1).MyInt = randShort; } if (makeChildUpToDate) { //if (doNotAutomaticallyReturnToPool && e.MyChild1.LazinatorMemoryStorage.IsEmpty == false) // e.MyChild1.LazinatorMemoryStorage.LazinatorShouldNotReturnToPool(); e.MyChild1.SerializeLazinator(); } if (makeParentUpToDate) { e.SerializeLazinator(); } } foreach (Example c in new Example[] { c1, c2 }) { c.MyChild2.MyLong = -3; // make sure early clone still works } }