public void CustomWorks(double percentage, int count, int?expected = null) { // what were testing here is // make sure the truncation only truncates the set amount // make sure the remaining and the truncated amounts are expected var organismArray = new OrganismStruct[count]; Array.Fill(organismArray, new OrganismStruct(0, 1.2f)); // make sure it had the right elements to begin with Assert.Equal(count, organismArray.Length); Span <OrganismStruct> organisms = organismArray; // truncate Span <OrganismStruct> result = Span <OrganismStruct> .Empty; Span <OrganismStruct> remaining = Span <OrganismStruct> .Empty; lock (ReproductionHandler) { ReproductionHandler.TrucationMethod = OrganismTruncationMethod.Custom; ReproductionHandler.CustomOrganismTruncationPercentage = percentage; result = ReproductionHandler.TruncateOrganisms(ref organisms, out remaining); } expected ??= (int)(percentage * count); Assert.Equal(expected, result.Length); Assert.Equal(count - expected, remaining.Length); }
public void TruncateDefaultWorks(OrganismTruncationMethod method, int count, int expected) { // what were testing here is // make sure the truncation only truncates the set amount // make sure the remaining and the truncated amounts are expected var organismArray = new OrganismStruct[count]; Array.Fill(organismArray, new OrganismStruct(0, 1.2f)); // make sure it had the right elements to begin with Assert.Equal(count, organismArray.Length); Span <OrganismStruct> organisms = organismArray; // truncate Span <OrganismStruct> result = Span <OrganismStruct> .Empty; Span <OrganismStruct> remaining = Span <OrganismStruct> .Empty; lock (ReproductionHandler) { ReproductionHandler.TrucationMethod = method; result = ReproductionHandler.TruncateOrganisms(ref organisms, out remaining); } Assert.Equal(expected, result.Length); Assert.Equal(count - expected, remaining.Length); }
public void RandomWorks() { // what were testing here is // make sure the truncation only truncates the set amount // make sure the remaining and the truncated amounts are expected var organismArray = new OrganismStruct[100]; Array.Fill(organismArray, new OrganismStruct(0, 1.2f)); // make sure it had the right elements to begin with Assert.Equal(100, organismArray.Length); Span <OrganismStruct> organisms = organismArray; // truncate Span <OrganismStruct> result = Span <OrganismStruct> .Empty; Span <OrganismStruct> remaining = Span <OrganismStruct> .Empty; lock (ReproductionHandler) { ReproductionHandler.TrucationMethod = OrganismTruncationMethod.Random; result = ReproductionHandler.TruncateOrganisms(ref organisms, out remaining); } // just make sure the amount that is returned is proportional Assert.Equal(100 - result.Length, remaining.Length); }
public void BoolWorks() { // what were testing here is // make sure the truncation only truncates the set amount // make sure the remaining and the truncated amounts are expected var organismArray = new OrganismStruct[100]; Array.Fill(organismArray, new OrganismStruct(0, 1.2f)); // make sure it had the right elements to begin with Assert.Equal(100, organismArray.Length); Span <OrganismStruct> organisms = organismArray; // truncate Span <OrganismStruct> result = Span <OrganismStruct> .Empty; Span <OrganismStruct> remaining = Span <OrganismStruct> .Empty; int x = 0; lock (ReproductionHandler) { ReproductionHandler.TrucationMethod = OrganismTruncationMethod.Bool; ReproductionHandler.CustomOrganismTruncater = (ref OrganismStruct organism) => { return(x++ < 50); }; result = ReproductionHandler.TruncateOrganisms(ref organisms, out remaining); } // since our custom truncator just keesp the ones before 50 and throws away the ones after 50 we should get two 50's Assert.Equal(result.Length, remaining.Length); Assert.Equal(50, result.Length); }
public void GenerationWorks(OrganismReproductionMethod method, int count, params int[] inputsAndExpected) { var organismArray = new OrganismStruct[inputsAndExpected.Length]; for (int i = 0; i < inputsAndExpected.Length; i++) { organismArray[i] = new OrganismStruct(inputsAndExpected[i], 1.0d); } Span <OrganismStruct> organisms = organismArray; Span <OrganismStruct> parents = organisms.Slice(0, count); var results = Span <(OrganismStruct Left, OrganismStruct Right)> .Empty; lock (ReproductionHandler) { ReproductionHandler.ReproductionMethod = method; results = ReproductionHandler.GenerateBreedingPairs(ref parents); } // create the expected Span <OrganismStruct> expectedPacked = organisms[count..];