예제 #1
0
        public void GetInt_Range_RandomInRangeResult()
        {
            var target = new FastRandomRandomization();

            FlowAssert.IsAtLeastOneAttemptOk(100, () => {
                Assert.AreEqual(0, target.GetInt(0, 2));
            });

            FlowAssert.IsAtLeastOneAttemptOk(100, () => {
                Assert.AreEqual(1, target.GetInt(0, 2));
            });

            for (int i = 0; i < 100; i++)
            {
                Assert.AreNotEqual(2, target.GetInt(0, 2));
            }
        }
예제 #2
0
        public void GetDouble_Range_RandomInRangeResult()
        {
            var target = new FastRandomRandomization();

            FlowAssert.IsAtLeastOneAttemptOk(100, () => {
                Assert.IsTrue(target.GetDouble(0, 2.2) < 1);
            });

            FlowAssert.IsAtLeastOneAttemptOk(1000, () => {
                Assert.IsTrue(target.GetDouble(0, 2.2) > 2.1);
            });

            for (int i = 0; i < 100; i++)
            {
                Assert.AreNotEqual(2.3, target.GetDouble(0, 2.2));
            }
        }
        public void GetUniqueInts_Length_ArrayWithUniqueInts()
        {
            var target = new FastRandomRandomization();
            var actual = target.GetUniqueInts(10, 0, 10);

            Assert.AreEqual(10, actual.Length);
            Assert.AreEqual(10, actual.Distinct().Count());

            for (int i = 0; i < 10; i++)
            {
                Assert.IsTrue(actual[i] >= 0 && actual[i] < 10);
            }

            actual = target.GetUniqueInts(10, 10, 20);
            Assert.AreEqual(10, actual.Length);
            Assert.AreEqual(10, actual.Distinct().Count());

            for (int i = 0; i < 10; i++)
            {
                Assert.IsTrue(actual[i] >= 10 && actual[i] < 20);
            }

            actual = target.GetUniqueInts(2, 0, 20);
            Assert.AreEqual(2, actual.Length);
            Assert.AreEqual(2, actual.Distinct().Count());

            for (int i = 0; i < 2; i++)
            {
                Assert.IsTrue(actual[i] >= 0 && actual[i] < 20);
            }

            FlowAssert.IsAtLeastOneAttemptOk(100, () =>
            {
                actual = target.GetUniqueInts(2, 0, 20);
                Assert.AreEqual(2, actual.Length);
                Assert.AreEqual(2, actual.Distinct().Count());

                Assert.IsTrue(actual[0] >= 2);
            });
        }
예제 #4
0
        public void SelectChromosomes_Generation_ChromosomesSelected()
        {
            var target = new RouletteWheelSelection();
            var c1     = Substitute.ForPartsOf <ChromosomeBase>(2);

            c1.Fitness = 0.1;

            var c2 = Substitute.ForPartsOf <ChromosomeBase>(2);

            c2.Fitness = 0.5;

            var c3 = Substitute.ForPartsOf <ChromosomeBase>(2);

            c3.Fitness = 0;

            var c4 = Substitute.ForPartsOf <ChromosomeBase>(2);

            c4.Fitness = 0.7;

            var generation = new Generation(1, new List <IChromosome>()
            {
                c1, c2, c3, c4
            });

            // Just one selected chromosome is c1.
            FlowAssert.IsAtLeastOneAttemptOk(100, () =>
            {
                var actual = target.SelectChromosomes(2, generation);
                Assert.AreEqual(2, actual.Count);
                Assert.AreEqual(1, actual.Count(c => c.Fitness == 0.1));
            });

            // All selected chromosome is c1.
            FlowAssert.IsAtLeastOneAttemptOk(1000, () =>
            {
                var actual = target.SelectChromosomes(2, generation);
                Assert.AreEqual(2, actual.Count);
                Assert.IsTrue(actual.All(c => c.Fitness == 0.1));
            });

            // Just one selected chromosome is c2.
            FlowAssert.IsAtLeastOneAttemptOk(100, () =>
            {
                var actual = target.SelectChromosomes(2, generation);
                Assert.AreEqual(2, actual.Count);
                Assert.AreEqual(1, actual.Count(c => c.Fitness == 0.5));
            });

            // All selected chromosome is c2.
            FlowAssert.IsAtLeastOneAttemptOk(1000, () =>
            {
                var actual = target.SelectChromosomes(2, generation);
                Assert.AreEqual(2, actual.Count);
                Assert.IsTrue(actual.All(c => c.Fitness == 0.5));
            });

            // None selected chromosome is c3.
            FlowAssert.IsAtLeastOneAttemptOk(100, () =>
            {
                var actual = target.SelectChromosomes(2, generation);
                Assert.AreEqual(2, actual.Count);
                Assert.AreEqual(0, actual.Count(c => c.Fitness == 0.0));
            });

            // Just one selected chromosome is c4.
            FlowAssert.IsAtLeastOneAttemptOk(100, () =>
            {
                var actual = target.SelectChromosomes(2, generation);
                Assert.AreEqual(2, actual.Count);
                Assert.AreEqual(1, actual.Count(c => c.Fitness == 0.7));
            });

            // All selected chromosome is c4.
            FlowAssert.IsAtLeastOneAttemptOk(1000, () =>
            {
                var actual = target.SelectChromosomes(2, generation);
                Assert.AreEqual(2, actual.Count);
                Assert.IsTrue(actual.All(c => c.Fitness == 0.7));
            });
        }