Skip to content

Simplify canonical data types #430

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

Merged
merged 8 commits into from
Sep 9, 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
52 changes: 26 additions & 26 deletions exercises/book-store/BookStoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,91 +7,91 @@ public class BookStoreTest
[Fact]
public void Only_a_single_book()
{
var input = new[] { 1 };
Assert.Equal(8, BookStore.Total(input));
var basket = new[] { 1 };
Assert.Equal(8, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Two_of_the_same_book()
{
var input = new[] { 2, 2 };
Assert.Equal(16, BookStore.Total(input));
var basket = new[] { 2, 2 };
Assert.Equal(16, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Empty_basket()
{
var input = new int[0];
Assert.Equal(0, BookStore.Total(input));
var basket = new int[0];
Assert.Equal(0, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Two_different_books()
{
var input = new[] { 1, 2 };
Assert.Equal(15.2, BookStore.Total(input));
var basket = new[] { 1, 2 };
Assert.Equal(15.2, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Three_different_books()
{
var input = new[] { 1, 2, 3 };
Assert.Equal(21.6, BookStore.Total(input));
var basket = new[] { 1, 2, 3 };
Assert.Equal(21.6, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Four_different_books()
{
var input = new[] { 1, 2, 3, 4 };
Assert.Equal(25.6, BookStore.Total(input));
var basket = new[] { 1, 2, 3, 4 };
Assert.Equal(25.6, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Five_different_books()
{
var input = new[] { 1, 2, 3, 4, 5 };
Assert.Equal(30, BookStore.Total(input));
var basket = new[] { 1, 2, 3, 4, 5 };
Assert.Equal(30, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Two_groups_of_four_is_cheaper_than_group_of_five_plus_group_of_three()
{
var input = new[] { 1, 1, 2, 2, 3, 3, 4, 5 };
Assert.Equal(51.2, BookStore.Total(input));
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 5 };
Assert.Equal(51.2, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Group_of_four_plus_group_of_two_is_cheaper_than_two_groups_of_three()
{
var input = new[] { 1, 1, 2, 2, 3, 4 };
Assert.Equal(40.8, BookStore.Total(input));
var basket = new[] { 1, 1, 2, 2, 3, 4 };
Assert.Equal(40.8, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Two_each_of_first_4_books_and_1_copy_each_of_rest()
{
var input = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5 };
Assert.Equal(55.6, BookStore.Total(input));
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5 };
Assert.Equal(55.6, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Two_copies_of_each_book()
{
var input = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
Assert.Equal(60, BookStore.Total(input));
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
Assert.Equal(60, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Three_copies_of_first_book_and_2_each_of_remaining()
{
var input = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1 };
Assert.Equal(68, BookStore.Total(input));
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1 };
Assert.Equal(68, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Three_each_of_first_2_books_and_2_each_of_remaining_books()
{
var input = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2 };
Assert.Equal(75.2, BookStore.Total(input));
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2 };
Assert.Equal(75.2, BookStore.Total(basket));
}
}
76 changes: 38 additions & 38 deletions exercises/complex-numbers/ComplexNumbersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,116 +52,116 @@ public void Imaginary_unit()
{
var sut = new ComplexNumber(0, 1);
var expected = new ComplexNumber(-1, 0);
Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(0, 1)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(0, 1)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(0, 1)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(0, 1)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Add_purely_real_numbers()
{
var sut = new ComplexNumber(1, 0);
var expected = new ComplexNumber(3, 0);
Assert.Equal(expected.Real(), sut.Add(new ComplexNumber(2, 0)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Add(new ComplexNumber(2, 0)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Add(new ComplexNumber(2, 0)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Add(new ComplexNumber(2, 0)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Add_purely_imaginary_numbers()
{
var sut = new ComplexNumber(0, 1);
var expected = new ComplexNumber(0, 3);
Assert.Equal(expected.Real(), sut.Add(new ComplexNumber(0, 2)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Add(new ComplexNumber(0, 2)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Add(new ComplexNumber(0, 2)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Add(new ComplexNumber(0, 2)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Add_numbers_with_real_and_imaginary_part()
{
var sut = new ComplexNumber(1, 2);
var expected = new ComplexNumber(4, 6);
Assert.Equal(expected.Real(), sut.Add(new ComplexNumber(3, 4)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Add(new ComplexNumber(3, 4)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Add(new ComplexNumber(3, 4)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Add(new ComplexNumber(3, 4)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Subtract_purely_real_numbers()
{
var sut = new ComplexNumber(1, 0);
var expected = new ComplexNumber(-1, 0);
Assert.Equal(expected.Real(), sut.Sub(new ComplexNumber(2, 0)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Sub(new ComplexNumber(2, 0)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Sub(new ComplexNumber(2, 0)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Sub(new ComplexNumber(2, 0)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Subtract_purely_imaginary_numbers()
{
var sut = new ComplexNumber(0, 1);
var expected = new ComplexNumber(0, -1);
Assert.Equal(expected.Real(), sut.Sub(new ComplexNumber(0, 2)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Sub(new ComplexNumber(0, 2)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Sub(new ComplexNumber(0, 2)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Sub(new ComplexNumber(0, 2)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Subtract_numbers_with_real_and_imaginary_part()
{
var sut = new ComplexNumber(1, 2);
var expected = new ComplexNumber(-2, -2);
Assert.Equal(expected.Real(), sut.Sub(new ComplexNumber(3, 4)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Sub(new ComplexNumber(3, 4)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Sub(new ComplexNumber(3, 4)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Sub(new ComplexNumber(3, 4)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Multiply_purely_real_numbers()
{
var sut = new ComplexNumber(1, 0);
var expected = new ComplexNumber(2, 0);
Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(2, 0)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(2, 0)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(2, 0)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(2, 0)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Multiply_purely_imaginary_numbers()
{
var sut = new ComplexNumber(0, 1);
var expected = new ComplexNumber(-2, 0);
Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(0, 2)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(0, 2)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(0, 2)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(0, 2)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Multiply_numbers_with_real_and_imaginary_part()
{
var sut = new ComplexNumber(1, 2);
var expected = new ComplexNumber(-5, 10);
Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(3, 4)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(3, 4)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(3, 4)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(3, 4)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Divide_purely_real_numbers()
{
var sut = new ComplexNumber(1, 0);
var expected = new ComplexNumber(0.5, 0);
Assert.Equal(expected.Real(), sut.Div(new ComplexNumber(2, 0)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Div(new ComplexNumber(2, 0)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Div(new ComplexNumber(2, 0)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Div(new ComplexNumber(2, 0)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Divide_purely_imaginary_numbers()
{
var sut = new ComplexNumber(0, 1);
var expected = new ComplexNumber(0.5, 0);
Assert.Equal(expected.Real(), sut.Div(new ComplexNumber(0, 2)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Div(new ComplexNumber(0, 2)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Div(new ComplexNumber(0, 2)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Div(new ComplexNumber(0, 2)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Divide_numbers_with_real_and_imaginary_part()
{
var sut = new ComplexNumber(1, 2);
var expected = new ComplexNumber(0.44, 0.08);
Assert.Equal(expected.Real(), sut.Div(new ComplexNumber(3, 4)).Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Div(new ComplexNumber(3, 4)).Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Div(new ComplexNumber(3, 4)).Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Div(new ComplexNumber(3, 4)).Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
Expand Down Expand Up @@ -204,52 +204,52 @@ public void Conjugate_a_purely_real_number()
{
var sut = new ComplexNumber(5, 0);
var expected = new ComplexNumber(5, 0);
Assert.Equal(expected.Real(), sut.Conjugate().Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Conjugate().Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Conjugate().Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Conjugate().Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Conjugate_a_purely_imaginary_number()
{
var sut = new ComplexNumber(0, 5);
var expected = new ComplexNumber(0, -5);
Assert.Equal(expected.Real(), sut.Conjugate().Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Conjugate().Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Conjugate().Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Conjugate().Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Conjugate_a_number_with_real_and_imaginary_part()
{
var sut = new ComplexNumber(1, 1);
var expected = new ComplexNumber(1, -1);
Assert.Equal(expected.Real(), sut.Conjugate().Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Conjugate().Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Conjugate().Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Conjugate().Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Eulers_identity_formula()
{
var sut = new ComplexNumber(0, Math.PI);
var expected = new ComplexNumber(-1, 0);
Assert.Equal(expected.Real(), sut.Exp().Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Exp().Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Exponential_of_0()
{
var sut = new ComplexNumber(0, 0);
var expected = new ComplexNumber(1, 0);
Assert.Equal(expected.Real(), sut.Exp().Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Exp().Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), precision: 15);
}

[Fact(Skip = "Remove to run test")]
public void Exponential_of_a_purely_real_number()
{
var sut = new ComplexNumber(1, 0);
var expected = new ComplexNumber(Math.E, 0);
Assert.Equal(expected.Real(), sut.Exp().Real(), 15);
Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), 15);
Assert.Equal(expected.Real(), sut.Exp().Real(), precision: 15);
Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), precision: 15);
}
}
2 changes: 0 additions & 2 deletions generators/Exercises/AllYourBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ protected override void UpdateCanonicalData(CanonicalData canonicalData)
{
foreach (var canonicalDataCase in canonicalData.Cases)
{
canonicalDataCase.Input["input_digits"] = canonicalDataCase.Input["input_digits"].ConvertToEnumerable<int>();

canonicalDataCase.ExceptionThrown = canonicalDataCase.Expected is null ? typeof(ArgumentException) : null;
canonicalDataCase.UseVariablesForInput = true;
canonicalDataCase.UseVariableForExpected = true;
Expand Down
28 changes: 5 additions & 23 deletions generators/Exercises/Allergies.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Generators.Input;
using Generators.Input;
using Generators.Output;
using Newtonsoft.Json.Linq;

namespace Generators.Exercises
{
Expand All @@ -13,19 +10,11 @@ protected override void UpdateCanonicalData(CanonicalData canonicalData)
foreach (var canonicalDataCase in canonicalData.Cases)
{
if (canonicalDataCase.Property == "allergicTo")
{
canonicalDataCase.Property = "IsAllergicTo";
}
else if (canonicalDataCase.Property == "list")
{
canonicalDataCase.Expected = canonicalDataCase.Expected.ConvertToEnumerable<string>();
canonicalDataCase.UseVariableForExpected = true;
}

canonicalDataCase.ConstructorInput = new Dictionary<string, object>
{
["score"] = canonicalDataCase.Properties["score"]
};
canonicalDataCase.SetConstructorInputParameters("score");
}
}

Expand All @@ -41,17 +30,10 @@ private static string RenderIsAllergicToAssert(TestMethodBody testMethodBody)
{
const string template =
@"{%- for allergy in Allergies -%}
Assert.{% if allergy.Result %}True{% else %}False{% endif %}(sut.IsAllergicTo(""{{ allergy.Substance }}""));
Assert.{% if allergy.result %}True{% else %}False{% endif %}(sut.IsAllergicTo(""{{ allergy.substance }}""));
{%- endfor -%}";

var templateParameters = new
{
Allergies = ((JArray) testMethodBody.CanonicalDataCase.Expected)
.Children<JObject>()
.Select(x => new {Result = x["result"].Value<bool>(), Substance = x["substance"].Value<string>()})
.ToArray()
};


var templateParameters = new { Allergies = testMethodBody.CanonicalDataCase.Expected };
return TemplateRenderer.RenderInline(template, templateParameters);
}
}
Expand Down
Loading