This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExerciseOne.cs
63 lines (56 loc) · 1.72 KB
/
ExerciseOne.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NUnit.Framework;
namespace TDDSolo
{
public class ExerciseOne
{
public class PrimeNumbers
{
private static IEnumerable<int> GeneratePrimes(int numPrimes)
{
if (numPrimes == 0)
{
return Enumerable.Empty<int>();
}
var primes = new List<int> { 2 };
if (numPrimes == 1)
{
return primes;
}
int number = primes.Last() + 1;
while (primes.Count < numPrimes)
{
if (primes.All(i => number % i != 0))
{
primes.Add(number);
}
++number;
}
return primes;
}
[TestCase(0, new int[] { })]
[TestCase(1, new[] { 2 })]
[TestCase(2, new[] { 2, 3 })]
public void GenerateListOfPrimeNumbers(int primeCount, IEnumerable<int> output)
{
var primes = GeneratePrimes(primeCount);
Assert.That(primes, Is.EqualTo(output));
}
[Test]
public void ListSizeIsCorrect()
{
var primes = GeneratePrimes(100);
Assert.That(primes, Has.Count.EqualTo(100));
}
[Test]
public void GenerateLargePrimeNumbers()
{
var prime = GeneratePrimes(100).ElementAt(100 - 1);
Assert.That(prime, Is.EqualTo(541));
}
}
}
}