-
-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add perfect-numbers generator (#253)
Restructured generators Support exception-based tests
- Loading branch information
1 parent
a7d9aa8
commit 6092824
Showing
34 changed files
with
306 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,33 @@ | ||
public enum NumberType | ||
using System; | ||
|
||
public enum Classification | ||
{ | ||
Perfect, | ||
Abundant, | ||
Deficient | ||
} | ||
|
||
public class PerfectNumbers | ||
public static class PerfectNumbers | ||
{ | ||
public static NumberType Classify(int number) | ||
public static Classification Classify(int number) | ||
{ | ||
if (number < 1) | ||
throw new ArgumentOutOfRangeException(); | ||
|
||
int sumOfFactors = 0; | ||
|
||
for (int i = 1; i < number; i++) | ||
{ | ||
if (number % i == 0) | ||
{ | ||
sumOfFactors += i; | ||
} | ||
} | ||
|
||
if (sumOfFactors < number) | ||
{ | ||
return NumberType.Deficient; | ||
} | ||
else if (sumOfFactors == number) | ||
{ | ||
return NumberType.Perfect; | ||
} | ||
else | ||
{ | ||
return NumberType.Abundant; | ||
} | ||
return Classification.Deficient; | ||
|
||
if (sumOfFactors == number) | ||
return Classification.Perfect; | ||
|
||
return Classification.Abundant; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,83 @@ | ||
using Xunit; | ||
using Xunit; | ||
using System; | ||
|
||
public class PerfectNumbersTest | ||
{ | ||
[Theory] | ||
[InlineData(3)] | ||
[InlineData(7)] | ||
[InlineData(13)] | ||
public void Can_classify_deficient_numbers(int number) | ||
{ | ||
Assert.Equal(NumberType.Deficient, PerfectNumbers.Classify(number)); | ||
} | ||
|
||
[Theory(Skip = "Remove to run test")] | ||
[InlineData(6)] | ||
[InlineData(28)] | ||
[InlineData(496)] | ||
public void Can_classify_perfect_numbers(int number) | ||
{ | ||
Assert.Equal(NumberType.Perfect, PerfectNumbers.Classify(number)); | ||
} | ||
|
||
[Theory(Skip = "Remove to run test")] | ||
[InlineData(12)] | ||
[InlineData(18)] | ||
[InlineData(20)] | ||
public void Can_classify_abundant_numbers(int number) | ||
{ | ||
Assert.Equal(NumberType.Abundant, PerfectNumbers.Classify(number)); | ||
[Fact] | ||
public void Smallest_perfect_number_is_classified_correctly() | ||
{ | ||
Assert.Equal(Classification.Perfect, PerfectNumbers.Classify(6)); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Medium_perfect_number_is_classified_correctly() | ||
{ | ||
Assert.Equal(Classification.Perfect, PerfectNumbers.Classify(28)); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Large_perfect_number_is_classified_correctly() | ||
{ | ||
Assert.Equal(Classification.Perfect, PerfectNumbers.Classify(33550336)); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Smallest_abundant_number_is_classified_correctly() | ||
{ | ||
Assert.Equal(Classification.Abundant, PerfectNumbers.Classify(12)); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Medium_abundant_number_is_classified_correctly() | ||
{ | ||
Assert.Equal(Classification.Abundant, PerfectNumbers.Classify(30)); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Large_abundant_number_is_classified_correctly() | ||
{ | ||
Assert.Equal(Classification.Abundant, PerfectNumbers.Classify(33550335)); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Smallest_prime_deficient_number_is_classified_correctly() | ||
{ | ||
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(2)); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Smallest_non_prime_deficient_number_is_classified_correctly() | ||
{ | ||
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(4)); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Medium_deficient_number_is_classified_correctly() | ||
{ | ||
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(32)); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Large_deficient_number_is_classified_correctly() | ||
{ | ||
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(33550337)); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Edge_case_no_factors_other_than_itself_is_classified_correctly() | ||
{ | ||
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(1)); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Zero_is_rejected_not_a_natural_number_() | ||
{ | ||
Assert.Throws<ArgumentOutOfRangeException>(() => PerfectNumbers.Classify(0)); | ||
} | ||
|
||
[Fact(Skip = "Remove to run test")] | ||
public void Negative_integer_is_rejected_not_a_natural_number_() | ||
{ | ||
Assert.Throws<ArgumentOutOfRangeException>(() => PerfectNumbers.Classify(-1)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
generators/TestClassNameTransformer.cs → ...ators/Classes/TestClassNameTransformer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
generators/TestClassRenderer.cs → generators/Classes/TestClassRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
generators/TestedClassNameTransformer.cs → ...ors/Classes/TestedClassNameTransformer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
generators/CanonicalDataCase.cs → generators/Data/CanonicalDataCase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...rators/CanonicalDataCasesJsonConverter.cs → ...s/Data/CanonicalDataCasesJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
generators/ExerciseCollection.cs → generators/Data/ExerciseCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
namespace Generators.Exercises | ||
using Generators.Data; | ||
using Generators.Methods; | ||
|
||
namespace Generators.Exercises | ||
{ | ||
public class AcronymExercise : Exercise | ||
public class AcronymExercise : EqualityExercise | ||
{ | ||
public AcronymExercise() : base("acronym") | ||
{ | ||
} | ||
|
||
protected override TestMethod CreateTestMethod(TestMethodData testMethodData) | ||
protected override TestMethodData CreateTestMethodData(CanonicalData canonicalData, CanonicalDataCase canonicalDataCase, int index) | ||
{ | ||
testMethodData.InputProperty = "phrase"; | ||
return CreateEqualityTestMethod(testMethodData); | ||
var testMethodData = base.CreateTestMethodData(canonicalData, canonicalDataCase, index); | ||
testMethodData.Options.InputProperty = "phrase"; | ||
|
||
return testMethodData; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Generators.Methods; | ||
|
||
namespace Generators.Exercises | ||
{ | ||
public abstract class BooleanExercise : Exercise | ||
{ | ||
protected BooleanExercise(string name) : base(name) | ||
{ | ||
} | ||
|
||
protected override TestMethod CreateTestMethod(TestMethodData testMethodData) | ||
=> CreateBooleanTestMethod(testMethodData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Generators.Methods; | ||
|
||
namespace Generators.Exercises | ||
{ | ||
public abstract class EqualityExercise : Exercise | ||
{ | ||
protected EqualityExercise(string name) : base(name) | ||
{ | ||
} | ||
|
||
protected override TestMethod CreateTestMethod(TestMethodData testMethodData) | ||
=> CreateEqualityTestMethod(testMethodData); | ||
} | ||
} |
Oops, something went wrong.