Esempio n. 1
0
        public void TestPrimesFoundReturnsNumbersFoundToBePrime()
        {
            // arrange
            const int knownPrime = 7;

            // act
            var serviceUnderTest = new PrimeNumberService();

            serviceUnderTest.IsPrimeNumber(knownPrime);

            // assert
            Assert.IsTrue(serviceUnderTest.PrimesFound.Contains(knownPrime));
        }
Esempio n. 2
0
        public void TestIsPrimeMethodNumberMethodShouldReturnTrueForPrimeNumber()
        {
            // arrange
            const int  knownPrime = 7;
            const bool expected   = true;

            // act
            var serviceUnderTest = new PrimeNumberService();
            var actual           = serviceUnderTest.IsPrimeNumber(knownPrime);

            // assert
            Assert.AreEqual(expected, actual, "The prime number should return true.");
        }
Esempio n. 3
0
        public void TestIsPrimeNumberMethodShouldReturnFalseForNonPrimeNumber()
        {
            // arrange
            const int  knownNonPrime = 8;
            const bool expected      = false;

            // act
            var serviceUnderTest = new PrimeNumberService();
            var actual           = serviceUnderTest.IsPrimeNumber(knownNonPrime);

            // assert
            Assert.AreEqual(expected, actual, "The non prime number should return false.");
        }
Esempio n. 4
0
        public void TestResetEmptiesPrimesFoundList()
        {
            // arrange
            const int knownPrime = 7;

            // act
            var serviceUnderTest = new PrimeNumberService();

            serviceUnderTest.IsPrimeNumber(knownPrime);
            serviceUnderTest.Reset();

            // assert
            Assert.IsFalse(serviceUnderTest.PrimesFound.Any());
        }
        private void ExecuteProcessNumberSieveCommand()
        {
            // reset display
            LoadNumbers();

            // disable process button
            DispatchService.Invoke(() => IsNotBusy = false);

            // perform sieve in background thread
            DispatchService.Background(() =>
            {
                const int interval = 100;
                var max            = _numbers.Count + 1;

                for (var i = 2; i <= max; i++)
                {
                    // delay
                    System.Threading.Thread.Sleep(interval);

                    // adjust for collection starting at 2
                    var index  = i - 2;
                    var number = i;

                    // See if i is prime.
                    if (!PrimeNumberService.IsPrimeNumber(number))
                    {
                        continue;
                    }

                    // mark as prime
                    _numbers[index].IsPrime      = true;
                    _numbers[index].DisplayColor = ColorService.PrimeColor;

                    // Knock out multiples of number
                    var backgroundColor = ColorService.GetNextColor();
                    for (var j = number * number; j <= max; j += number)
                    {
                        _numbers[j - 2].IsPrime            = false;
                        _numbers[j - 2].Model.DisplayColor = backgroundColor;

                        // delay
                        System.Threading.Thread.Sleep(interval);
                    }
                }

                // re-enable button
                DispatchService.Invoke(() => IsNotBusy = true);
            });
        }