src/GenFx.Components.Tests/SimpleGeneticAlgorithmTest.cs
/// <summary>
/// Tests that the <see cref="SimpleGeneticAlgorithm.CreateNextGenerationAsync"/> method works correctly.
/// </summary>
Tests that the  method works correctly.
Tests that the method works correctly .
[Fact]
        public async Task SimpleGeneticAlgorithm_CreateNextGeneration_Async()
        {
            SimpleGeneticAlgorithm algorithm = new SimpleGeneticAlgorithm
            {
                FitnessEvaluator = new MockFitnessEvaluator(),
                GeneticEntitySeed = new MockEntity(),
                PopulationSeed = new SimplePopulation(),
                ElitismStrategy = new MockElitismStrategy
                {
                    ElitistRatio = .1
                },
                SelectionOperator = new MockSelectionOperator
                {
                    SelectionBasedOnFitnessType = FitnessType.Scaled
                },
                CrossoverOperator = new MockCrossoverOperator
                {
                    CrossoverRate = 1
                },
                MutationOperator = new MockMutationOperator
                {
                    MutationRate = 1
                }
            };
            algorithm.ElitismStrategy = new MockElitismStrategy { ElitistRatio = .1 };
            algorithm.ElitismStrategy.Initialize(algorithm);
            algorithm.SelectionOperator = new MockSelectionOperator { SelectionBasedOnFitnessType = FitnessType.Scaled };
            algorithm.SelectionOperator.Initialize(algorithm);
            algorithm.CrossoverOperator = new MockCrossoverOperator { CrossoverRate = 1 };
            algorithm.CrossoverOperator.Initialize(algorithm);
            algorithm.MutationOperator = new MockMutationOperator { MutationRate = 1 };
            algorithm.MutationOperator.Initialize(algorithm);
            PrivateObject accessor = new PrivateObject(algorithm);
            SimplePopulation population = GetPopulation(algorithm, 10);
            List<GeneticEntity> originalEntities = new List<GeneticEntity>(population.Entities);
            int prevPopCount = population.Entities.Count;
            await (Task)accessor.Invoke("CreateNextGenerationAsync", population);
            int actualElitistEntitiesCount = population.Entities.Count(e => originalEntities.Contains(e));
            Assert.Equal(1, actualElitistEntitiesCount);
            Assert.Equal(1, ((MockElitismStrategy)algorithm.ElitismStrategy).GetElitistGeneticEntitiesCallCount);
            Assert.Equal(1, ((MockSelectionOperator)algorithm.SelectionOperator).DoSelectCallCount);
            Assert.Equal(4, ((MockCrossoverOperator)algorithm.CrossoverOperator).DoCrossoverCallCount);
            Assert.Equal(9, ((MockMutationOperator)algorithm.MutationOperator).DoMutateCallCount);
            Assert.Equal(prevPopCount, population.Entities.Count);
        }
[ Fact ] public async Task SimpleGeneticAlgorithm_CreateNextGeneration_Async ( ) { SimpleGeneticAlgorithm algorithm = new SimpleGeneticAlgorithm { FitnessEvaluator = new MockFitnessEvaluator ( ) , GeneticEntitySeed = new MockEntity ( ) , PopulationSeed = new SimplePopulation ( ) , ElitismStrategy = new MockElitismStrategy { ElitistRatio = .1 } , SelectionOperator = new MockSelectionOperator { SelectionBasedOnFitnessType = FitnessType . Scaled } , CrossoverOperator = new MockCrossoverOperator { CrossoverRate = 1 } , MutationOperator = new MockMutationOperator { MutationRate = 1 } } ; algorithm . ElitismStrategy = new MockElitismStrategy { ElitistRatio = .1 } ; algorithm . ElitismStrategy . Initialize ( algorithm ) ; algorithm . SelectionOperator = new MockSelectionOperator { SelectionBasedOnFitnessType = FitnessType . Scaled } ; algorithm . SelectionOperator . Initialize ( algorithm ) ; algorithm . CrossoverOperator = new MockCrossoverOperator { CrossoverRate = 1 } ; algorithm . CrossoverOperator . Initialize ( algorithm ) ; algorithm . MutationOperator = new MockMutationOperator { MutationRate = 1 } ; algorithm . MutationOperator . Initialize ( algorithm ) ; PrivateObject accessor = new PrivateObject ( algorithm ) ; SimplePopulation population = GetPopulation ( algorithm , 10 ) ; List < GeneticEntity > originalEntities = new List < GeneticEntity > ( population . Entities ) ; int prevPopCount = population . Entities . Count ; await ( Task ) accessor . Invoke ( " " , population ) ; int actualElitistEntitiesCount = population . Entities . Count ( e => originalEntities . Contains ( e ) ) ; Assert . Equal ( 1 , actualElitistEntitiesCount ) ; Assert . Equal ( 1 , ( ( MockElitismStrategy ) algorithm . ElitismStrategy ) . GetElitistGeneticEntitiesCallCount ) ; Assert . Equal ( 1 , ( ( MockSelectionOperator ) algorithm . SelectionOperator ) . DoSelectCallCount ) ; Assert . Equal ( 4 , ( ( MockCrossoverOperator ) algorithm . CrossoverOperator ) . DoCrossoverCallCount ) ; Assert . Equal ( 9 , ( ( MockMutationOperator ) algorithm . MutationOperator ) . DoMutateCallCount ) ; Assert . Equal ( prevPopCount , population . Entities . Count ) ; }
Tests that the  method works correctly.
Tests that the method works correctly .
// Find the number of entities in the new population that were in the original population