src/GenFx.Components.Tests/UniformSelectionOperatorTest.cs
/// <summary>
/// Tests that the Select method works correctly.
/// </summary>
Tests that the Select method works correctly.
Tests that the Select method works correctly .
[Fact]
        public void UniformSelectionOperator_Select()
        {
            MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                FitnessEvaluator = new MockFitnessEvaluator(),
                GeneticEntitySeed = new MockEntity(),
                PopulationSeed = new SimplePopulation(),
                SelectionOperator = new UniformSelectionOperator
                {
                    SelectionBasedOnFitnessType = FitnessType.Scaled
                }
            };
            UniformSelectionOperator op = new UniformSelectionOperator();
            op.Initialize(algorithm);
            SimplePopulation population = new SimplePopulation();
            population.Initialize(algorithm);
            for (int i = 0; i < 4; i++)
            {
                MockEntity entity = new MockEntity();
                entity.Initialize(algorithm);
                population.Entities.Add(entity);
            }
            TestRandomUtil randomUtil = new TestRandomUtil();
            RandomNumberService.Instance = randomUtil;
            randomUtil.Value = 3;
            IList<GeneticEntity> selectedEntities = op.SelectEntities(1, population);
            Assert.Same(population.Entities[randomUtil.Value], selectedEntities[0]);
            randomUtil.Value = 2;
            selectedEntities = op.SelectEntities(1, population);
            Assert.Same(population.Entities[randomUtil.Value], selectedEntities[0]);
            randomUtil.Value = 1;
            selectedEntities = op.SelectEntities(1, population);
            Assert.Same(population.Entities[randomUtil.Value], selectedEntities[0]);
            randomUtil.Value = 0;
            selectedEntities = op.SelectEntities(1, population);
            Assert.Same(population.Entities[randomUtil.Value], selectedEntities[0]);
        }
[ Fact ] public void UniformSelectionOperator_Select ( ) { MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm { FitnessEvaluator = new MockFitnessEvaluator ( ) , GeneticEntitySeed = new MockEntity ( ) , PopulationSeed = new SimplePopulation ( ) , SelectionOperator = new UniformSelectionOperator { SelectionBasedOnFitnessType = FitnessType . Scaled } } ; UniformSelectionOperator op = new UniformSelectionOperator ( ) ; op . Initialize ( algorithm ) ; SimplePopulation population = new SimplePopulation ( ) ; population . Initialize ( algorithm ) ; for ( int i = 0 ; i < 4 ; i ++ ) { MockEntity entity = new MockEntity ( ) ; entity . Initialize ( algorithm ) ; population . Entities . Add ( entity ) ; } TestRandomUtil randomUtil = new TestRandomUtil ( ) ; RandomNumberService . Instance = randomUtil ; randomUtil . Value = 3 ; IList < GeneticEntity > selectedEntities = op . SelectEntities ( 1 , population ) ; Assert . Same ( population . Entities [ randomUtil . Value ] , selectedEntities [ 0 ] ) ; randomUtil . Value = 2 ; selectedEntities = op . SelectEntities ( 1 , population ) ; Assert . Same ( population . Entities [ randomUtil . Value ] , selectedEntities [ 0 ] ) ; randomUtil . Value = 1 ; selectedEntities = op . SelectEntities ( 1 , population ) ; Assert . Same ( population . Entities [ randomUtil . Value ] , selectedEntities [ 0 ] ) ; randomUtil . Value = 0 ; selectedEntities = op . SelectEntities ( 1 , population ) ; Assert . Same ( population . Entities [ randomUtil . Value ] , selectedEntities [ 0 ] ) ; }
Tests that the Select method works correctly.
Tests that the Select method works correctly .