Beispiel #1
0
        public void AddCreature_GetBestCreatures(int size, int seed)
        {
            //arrange
            NewBestCretureFoundEventDelegate <FakeCreature> newBestFound = (_, __) => { };
            RatedCreature <FakeCreature>          foreFather             = new RatedCreature <FakeCreature>(new FakeCreature(-1), -1);
            DisposedCreaturesStore <FakeCreature> disposedCreatures      = new DisposedCreaturesStore <FakeCreature>();

            DefaultSelector <FakeCreature> selector
                = new DefaultSelector <FakeCreature>(newBestFound, foreFather, size, disposedCreatures);
            RandomEnum rndE = new RandomEnum(seed);
            List <RatedCreature <FakeCreature> > insertedCreatures = new List <RatedCreature <FakeCreature> >();

            int limit = size * 10;

            insertedCreatures.Add(foreFather);

            foreach (var num in rndE)
            {
                if (--limit < 0)
                {
                    break;
                }

                insertedCreatures.Add(new RatedCreature <FakeCreature>(new FakeCreature(num), num));
            }

            //act
            foreach (var creature in insertedCreatures)
            {
                selector.AddCreature(creature);
            }

            //Assert
            insertedCreatures.Sort((x, y) => y.CompareTo(x));
            var expected = insertedCreatures.Take(size).ToList();
            var actual   =
                (from cr in selector.GetSurvivingCreatures()
                 orderby cr.FitnessValue descending
                 select cr).ToList();

            Assert.AreEqual(expected.Count, actual.Count);

            for (int i = 0; i < actual.Count; i++)
            {
                Assert.AreEqual(expected[i].FitnessValue, actual[i].FitnessValue);
            }
        }