Skip to content

Commit

Permalink
Add perfect-numbers generator (#253)
Browse files Browse the repository at this point in the history
Restructured generators
Support exception-based tests
  • Loading branch information
ErikSchierboom authored Mar 20, 2017
1 parent a7d9aa8 commit 6092824
Show file tree
Hide file tree
Showing 34 changed files with 306 additions and 117 deletions.
30 changes: 14 additions & 16 deletions exercises/perfect-numbers/Example.cs
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;
}
}
4 changes: 2 additions & 2 deletions exercises/perfect-numbers/PerfectNumbers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

public enum NumberType
public enum Classification
{
Perfect,
Abundant,
Expand All @@ -9,7 +9,7 @@ public enum NumberType

public static class PerfectNumbers
{
public static NumberType Classify(int number)
public static Classification Classify(int number)
{
throw new NotImplementedException();
}
Expand Down
104 changes: 78 additions & 26 deletions exercises/perfect-numbers/PerfectNumbersTest.cs
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));
}
}
3 changes: 2 additions & 1 deletion generators/TestClass.cs → generators/Classes/TestClass.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using Generators.Methods;

namespace Generators
namespace Generators.Classes
{
public class TestClass
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Humanizer;

namespace Generators
namespace Generators.Classes
{
public class TestClassNameTransformer : IStringTransformer
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using Generators.Methods;

namespace Generators
namespace Generators.Classes
{
public static class TestClassRenderer
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Humanizer;

namespace Generators
namespace Generators.Classes
{
public class TestedClassNameTransformer : IStringTransformer
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;

namespace Generators
namespace Generators.Data
{
public class CanonicalData
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;

namespace Generators
namespace Generators.Data
{
[JsonConverter(typeof(CanonicalDataCaseJsonConverter))]
public class CanonicalDataCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Generators
namespace Generators.Data
{
public class CanonicalDataCaseJsonConverter : JsonConverter
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;

namespace Generators
namespace Generators.Data
{
public class CanonicalDataCasesJsonConverter : JsonConverter
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Newtonsoft.Json;
using System;
using System;
using System.ComponentModel.DataAnnotations;
using System.Net.Http;
using Newtonsoft.Json;

namespace Generators
namespace Generators.Data
{
public static class CanonicalDataParser
{
private static readonly HttpClient httpClient = new HttpClient();
private static readonly HttpClient HttpClient = new HttpClient();

public static CanonicalData Parse(string exercise)
{
Expand All @@ -20,7 +20,7 @@ public static CanonicalData Parse(string exercise)
}

private static string DownloadCanonicalDataJson(string exercise)
=> httpClient.GetStringAsync(GetCanonicalDataUrl(exercise)).GetAwaiter().GetResult();
=> HttpClient.GetStringAsync(GetCanonicalDataUrl(exercise)).GetAwaiter().GetResult();

private static Uri GetCanonicalDataUrl(string exercise)
=> new Uri($"https://raw.githubusercontent.com/exercism/x-common/master/exercises/{exercise}/canonical-data.json");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Generators.Exercises;
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Generators.Exercises;

namespace Generators
namespace Generators.Data
{
public class ExerciseCollection : IEnumerable<Exercise>
{
Expand Down
8 changes: 0 additions & 8 deletions generators/EqualityTestMethodGenerator.cs

This file was deleted.

15 changes: 10 additions & 5 deletions generators/Exercises/AcronymExercise.cs
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;
}
}
}
14 changes: 14 additions & 0 deletions generators/Exercises/BooleanExercise.cs
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);
}
}
14 changes: 14 additions & 0 deletions generators/Exercises/EqualityExercise.cs
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);
}
}
Loading

0 comments on commit 6092824

Please sign in to comment.