Ejemplo n.º 1
0
        public void GenerateIntsTest()
        {
            // Arrange
            int seed = 1337;

            int rounds = 100;

            IntegerGenerator ig1 = new IntegerGenerator();
            IntegerGenerator ig2 = new IntegerGenerator();
            IntegerGenerator ig3 = new IntegerGenerator();
            IntegerGenerator ig4 = new IntegerGenerator();
            IntegerGenerator ig5 = new IntegerGenerator();

            var range2 = (min : 2, max : 10);

            List <object> gene1Objects = new List <object>(capacity: rounds);
            List <object> gene2Objects = new List <object>(capacity: rounds);
            List <object> gene3Objects = new List <object>(capacity: rounds);
            List <object> gene4Objects = new List <object>(capacity: rounds);
            List <object> gene5Objects = new List <object>(capacity: rounds);

            List <bool> gene1Success = new List <bool>(capacity: rounds);
            List <bool> gene2Success = new List <bool>(capacity: rounds);
            List <bool> gene3Success = new List <bool>(capacity: rounds);
            List <bool> gene4Success = new List <bool>(capacity: rounds);
            List <bool> gene5Success = new List <bool>(capacity: rounds);

            // Act
            var shouldBeValidInitResult1 = ig1.Init(null, seed);
            var shouldBeValidInitResult2 = ig2.Init($"{range2.min}..{range2.max}", seed);
            var shouldBeValidInitResult3 = ig3.Init("-100,-66", seed);
            var shouldBeValidInitResult4 = ig4.Init($"{int.MinValue}..{int.MaxValue}", seed);
            var shouldBeValidInitResult5 = ig5.Init("0,1", seed);

            for (int i = 0; i < rounds; i++)
            {
                var genResult1 = ig1.Generate();
                var genResult2 = ig2.Generate();
                var genResult3 = ig3.Generate();
                var genResult4 = ig4.Generate();
                var genResult5 = ig5.Generate();

                gene1Objects.Add(genResult1.result);
                gene2Objects.Add(genResult2.result);
                gene3Objects.Add(genResult3.result);
                gene4Objects.Add(genResult4.result);
                gene5Objects.Add(genResult5.result);

                gene1Success.Add(genResult1.success);
                gene2Success.Add(genResult2.success);
                gene3Success.Add(genResult3.success);
                gene4Success.Add(genResult4.success);
                gene5Success.Add(genResult5.success);

                ig1.NextStep();
                ig2.NextStep();
                ig3.NextStep();
                ig4.NextStep();
                ig5.NextStep();
            }

            // Assert
            Assert.AreEqual(rounds, gene1Objects.Count);
            Assert.AreEqual(rounds, gene2Objects.Count);
            Assert.AreEqual(rounds, gene3Objects.Count);
            Assert.AreEqual(rounds, gene4Objects.Count);
            Assert.AreEqual(rounds, gene5Objects.Count);

            Assert.AreEqual(rounds, gene1Success.Count);
            Assert.AreEqual(rounds, gene2Success.Count);
            Assert.AreEqual(rounds, gene3Success.Count);
            Assert.AreEqual(rounds, gene4Success.Count);
            Assert.AreEqual(rounds, gene5Success.Count);

            CollectionAssert.AllItemsAreInstancesOfType(gene1Objects, typeof(int), "There should be only ints generated");
            CollectionAssert.AllItemsAreInstancesOfType(gene2Objects, typeof(int), "There should be only ints generated");
            CollectionAssert.AllItemsAreInstancesOfType(gene3Objects, typeof(int), "There should be only ints generated");
            CollectionAssert.AllItemsAreInstancesOfType(gene4Objects, typeof(int), "There should be only ints generated");
            CollectionAssert.AllItemsAreInstancesOfType(gene5Objects, typeof(int), "There should be only ints generated");

            var range1 = ig1.GetRange();

            foreach (object val in gene1Objects)
            {
                Assert.GreaterOrEqual((int)val, (int)range1.minRangeInclusive);
                Assert.Less((int)val, (int)range1.maxRangeExclusive);
            }

            Assert.AreEqual(range2.min, (int)ig2.GetRange().minRangeInclusive, "Range min should have been parsed correctly");
            Assert.AreEqual(range2.max, (int)ig2.GetRange().maxRangeExclusive, "Range max should have been parsed correctly");

            foreach (object val in gene2Objects)
            {
                Assert.GreaterOrEqual((int)val, range2.min);
                Assert.Less((int)val, range2.max);
            }
        }
Ejemplo n.º 2
0
        public void GenerateLongsTest()
        {
            // Arrange
            int seed = 1337;

            int rounds = 100;

            Type longType = typeof(long);

            IntegerGenerator ig1 = new IntegerGenerator();
            IntegerGenerator ig2 = new IntegerGenerator();
            IntegerGenerator ig3 = new IntegerGenerator();

            long intMin = int.MinValue;
            long intMax = int.MaxValue;
            var  range1 = (min : intMin - 1, max : 10);
            var  range2 = (min : -10, max : intMax + 1);
            var  range3 = (min : long.MinValue, max : long.MaxValue);

            List <object> gene1Objects = new List <object>(capacity: rounds);
            List <object> gene2Objects = new List <object>(capacity: rounds);
            List <object> gene3Objects = new List <object>(capacity: rounds);

            List <bool> gene1Success = new List <bool>(capacity: rounds);
            List <bool> gene2Success = new List <bool>(capacity: rounds);
            List <bool> gene3Success = new List <bool>(capacity: rounds);

            // Act
            var shouldBeValidInitResult1 = ig1.Init($"{range1.min}..{range1.max}", seed);
            var shouldBeValidInitResult2 = ig2.Init($"{range2.min}..{range2.max}", seed);
            var shouldBeValidInitResult3 = ig3.Init($"{range3.min}..{range3.max}", seed);

            for (int i = 0; i < rounds; i++)
            {
                var genResult1 = ig1.Generate(null, longType);
                var genResult2 = ig2.Generate(null, longType);
                var genResult3 = ig3.Generate(null, longType);

                gene1Objects.Add(genResult1.result);
                gene2Objects.Add(genResult2.result);
                gene3Objects.Add(genResult3.result);

                gene1Success.Add(genResult1.success);
                gene2Success.Add(genResult2.success);
                gene3Success.Add(genResult3.success);

                ig1.NextStep();
                ig2.NextStep();
                ig3.NextStep();
            }

            // Assert
            Assert.AreEqual(rounds, gene1Objects.Count);
            Assert.AreEqual(rounds, gene2Objects.Count);
            Assert.AreEqual(rounds, gene3Objects.Count);

            Assert.AreEqual(rounds, gene1Success.Count);
            Assert.AreEqual(rounds, gene2Success.Count);
            Assert.AreEqual(rounds, gene3Success.Count);

            CollectionAssert.AllItemsAreInstancesOfType(gene1Objects, longType, "There should be only longs generated");
            CollectionAssert.AllItemsAreInstancesOfType(gene2Objects, longType, "There should be only longs generated");
            CollectionAssert.AllItemsAreInstancesOfType(gene3Objects, longType, "There should be only longs generated");

            var rangeTemp = ig1.GetRange();

            foreach (object val in gene1Objects)
            {
                Assert.GreaterOrEqual((long)val, (long)rangeTemp.minRangeInclusive);
                Assert.Less((long)val, (long)rangeTemp.maxRangeExclusive);
            }

            Assert.AreEqual(range2.min, (long)ig2.GetRange().minRangeInclusive, "Range min should have been parsed correctly");
            Assert.AreEqual(range2.max, (long)ig2.GetRange().maxRangeExclusive, "Range max should have been parsed correctly");

            foreach (object val in gene2Objects)
            {
                Assert.GreaterOrEqual((long)val, range2.min);
                Assert.Less((long)val, range2.max);
            }
        }