Пример #1
0
        /// <summary>
        /// Entry point into the program
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        static void Main(string[] args)
        {
            const int secondsInAMinute        = 60;
            const int millisecondsInASecond   = 1000;
            const int oneMinuteInMilliseconds = secondsInAMinute * millisecondsInASecond;

            var primeNumber = new PrimeNumbers();
            var timeTracker = new TimeTracker(oneMinuteInMilliseconds);
            var highestNumberOfPrimesToGenerate = 1000000000;

            timeTracker.Begin();

            foreach (var prime in primeNumber.Generate(highestNumberOfPrimesToGenerate))
            {
                if (!timeTracker.IsTimeExpired())
                {
                    OnPrimeNumberFound(prime, timeTracker);
                }
                else
                {
                    break;
                }
            }

            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
        }
Пример #2
0
        public void ShouldReturnPrimesUnder30()
        {
            // Arrange
            var primeNumbers = new PrimeNumbers();
            var primesReturned = false;
            var primesGenerated = new List<int>();

            // Primes taken from https://oeis.org/A000040
            var primesUnder30 = new List<int>
                {
                    2,
                    3,
                    5,
                    7,
                    11,
                    13,
                    17,
                    19,
                    23,
                    29
                };

            // Act
            foreach (var prime in primeNumbers.Generate(30))
            {
                Assert.IsTrue(primesUnder30.Contains(prime), $"Unexpected prime: {prime}");
                primesGenerated.Add(prime);
                primesReturned = true;
            }

            // Assert
            Assert.IsTrue(primesReturned, "Expected the on prime action to be called.");
            Assert.IsFalse(primesUnder30.Except(primesGenerated).Any(), "Expected all primes under 30 to be generated.");
        }
Пример #3
0
        private void TestNoPrimesFound(Func<PrimeNumbers, IEnumerable<int>> primeNumbersActionToTest)
        {
            // Arrange
            var primeNumbers = new PrimeNumbers();
            var primeFound = false;

            // Act
            foreach (var prime in primeNumbersActionToTest(primeNumbers))
            {
                primeFound = true;
                break;
            }

            // Assert
            Assert.IsFalse(primeFound, "Expected no primes to be found");
        }