Esempio n. 1
0
        public void _08_NextBalloonFromArray_ShouldReturnRandomItemFromBalloonArray()
        {
            AssertNextBalloonFromArrayMethodIsDefinedCorrectly();

            int numberOfBalloons = 10000;

            Balloon[] allBalloons = new Balloon[numberOfBalloons];
            for (int i = 0; i < numberOfBalloons; i++)
            {
                allBalloons[i] = new Balloon(Color.Red, 10);
            }

            var pickedBalloons         = new HashSet <Balloon>();
            int numberOfPicks          = 100;
            int numberOfDuplicatePicks = 0;

            for (int i = 0; i < numberOfPicks; i++)
            {
                Balloon balloon = InvokeNextBalloonFromArray(allBalloons);
                Assert.That(balloon, Is.Not.Null, "The returned balloon cannot be null");
                Assert.That(allBalloons.Any(b => b == balloon), Is.True, "The returned balloon must be a balloon from the array.");
                if (pickedBalloons.Contains(balloon))
                {
                    numberOfDuplicatePicks++;
                }
                else
                {
                    pickedBalloons.Add(balloon);
                }
            }

            if (numberOfDuplicatePicks > numberOfPicks * 0.2) //20% duplicate picks on 10000 balloons is nearly impossible
            {
                Assert.Fail(
                    "The picking of a balloon seems not to be random. " +
                    $"{numberOfDuplicatePicks * 100.0 / numberOfPicks}% of {numberOfPicks} balloon picks " +
                    $"in a balloon array of size {numberOfBalloons} pick an already picked balloon");
            }
        }