public void TestPrimeGeneration() { Stopwatch sw = Stopwatch.StartNew(); PrimeCalculation primeCalculation = new PrimeCalculation(TEST_PRIME_COUNT); sw.Stop(); TestContext.WriteLine(message: "Generated " + TEST_PRIME_COUNT + " primes in " + sw.ElapsedMilliseconds + "ms"); // List our primes so we can check them for false negatives HashSet <ulong> foundPrimes = new HashSet <ulong>(); // Check for false positives for (int i = 0; i < TEST_PRIME_COUNT; ++i) { ulong possiblePrime = primeCalculation.GetPrime(i); foundPrimes.Add(possiblePrime); Assert.IsTrue(IsPrime(possiblePrime), "Non-prime value found: " + possiblePrime); } // Check for false negatives ulong upperPrimeBound = PrimeCalculation.UpperPrimeBound(TEST_PRIME_COUNT); int totalPrimesFound = 0; for (ulong i = 0; i < upperPrimeBound && totalPrimesFound < TEST_PRIME_COUNT; ++i) { if (IsPrime(i)) { ++totalPrimesFound; Assert.IsTrue(foundPrimes.Contains(i), "Calculated primes are missing number: " + i); } } }
public void FindPrimeNumbersWrongParams_ExpectArgumentOutOfRangeException() { Action actionArg1 = () => _ = PrimeCalculation.EratosthenesSieve(0, 1, null).ToArray(); Action actionArg2 = () => _ = PrimeCalculation.EratosthenesSieve(2, 0, null).ToArray(); actionArg1.Should().ThrowExactly <ArgumentOutOfRangeException>(); actionArg2.Should().ThrowExactly <ArgumentOutOfRangeException>(); }
/// <summary> /// Calculate prime numbers. Send message with result to ui handlers. Return result to server. /// </summary> /// <param name="requestParam"></param> /// <returns></returns> protected override async Task <PrimeRet> ResponseHandler(PrimeParam requestParam) { if (requestParam == null) { throw new NullReferenceException(nameof(requestParam)); } var result = new PrimeRet() { Primes = await Task.Run(() => PrimeCalculation.EratosthenesSieve(requestParam.BeginNumber, requestParam.Count, requestParam.Primes) .ToArray()).ConfigureAwait(false) }; await _eventAggregator.PublishOnCurrentThreadAsync(result); return(result); }
public void FindPrimeNumbers_ShouldReturnArray(int beginNo, int count, int[] primes, int[] expected) { var result = PrimeCalculation.EratosthenesSieve(beginNo, count, primes).ToArray(); result.Should().BeEquivalentTo(expected); }
public void Next_Prime_Should_Be_11() { PrimeNumber newPrimeNumber = PrimeCalculation.NextPrime(10); Assert.Equal(11, newPrimeNumber.Number); }
public void Next_Prime_Should_Be_5623() { PrimeNumber newPrimeNumber = PrimeCalculation.NextPrime(5592); Assert.Equal(5623, newPrimeNumber.Number); }
public void Next_Prime_Should_Be_359() { PrimeNumber newPrimeNumber = PrimeCalculation.NextPrime(354); Assert.Equal(359, newPrimeNumber.Number); }