Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nth-prime test generator #254

Merged
merged 1 commit into from
Mar 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion exercises/nth-prime/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

public static class NthPrime
{
public static int Nth(int nth)
public static int Prime(int nth)
{
if (nth < 1)
throw new ArgumentOutOfRangeException();

return Primes().Skip(nth - 1).First();
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/nth-prime/NthPrime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public static class NthPrime
{
public static int Nth(int nth)
public static int Prime(int nth)
{
throw new NotImplementedException("You need to implement this function.");
}
Expand Down
44 changes: 29 additions & 15 deletions exercises/nth-prime/NthPrimeTest.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
using Xunit;
using Xunit;
using System;

public class NthPrimeTest
{
[Theory]
[InlineData(1, 2)]
[InlineData(2, 3)]
[InlineData(3, 5)]
[InlineData(4, 7)]
[InlineData(5, 11)]
[InlineData(6, 13)]
[InlineData(7, 17)]
[InlineData(8, 19)]
[InlineData(1000, 7919)]
[InlineData(10000, 104729)]
[InlineData(10001, 104743)]
public void Nth_prime_calculated(int nth, int expected)
[Fact]
public void First_prime()
{
Assert.Equal(expected, NthPrime.Nth(nth));
Assert.Equal(2, NthPrime.Prime(1));
}

[Fact(Skip = "Remove to run test")]
public void Second_prime()
{
Assert.Equal(3, NthPrime.Prime(2));
}

[Fact(Skip = "Remove to run test")]
public void Sixth_prime()
{
Assert.Equal(13, NthPrime.Prime(6));
}

[Fact(Skip = "Remove to run test")]
public void Big_prime()
{
Assert.Equal(104743, NthPrime.Prime(10001));
}

[Fact(Skip = "Remove to run test")]
public void There_is_no_zeroth_prime()
{
Assert.Throws<ArgumentOutOfRangeException>(() => NthPrime.Prime(0));
}
}
25 changes: 25 additions & 0 deletions generators/Exercises/NthPrimeExercise.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using Generators.Data;
using Generators.Methods;
using Humanizer;

namespace Generators.Exercises
{
public class NthPrimeExercise : EqualityExercise
{
public NthPrimeExercise() : base("nth-prime")
{
}

protected override TestMethod CreateTestMethod(TestMethodData testMethodData)
{
if (testMethodData.CanonicalDataCase.Expected is bool b)
{
testMethodData.Options.ExceptionType = typeof(ArgumentOutOfRangeException);
return CreateExceptionTestMethod(testMethodData);
}

return CreateEqualityTestMethod(testMethodData);
}
}
}