Skip to content

Improved long data type support #1311

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 2 commits into from
Aug 19, 2019
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
14 changes: 7 additions & 7 deletions exercises/prime-factors/PrimeFactorsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@ public class PrimeFactorsTest
[Fact]
public void No_factors()
{
Assert.Empty(PrimeFactors.Factors(1));
Assert.Empty(PrimeFactors.Factors(1L));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now made it clear that each input value is actually a long value. Previously, an implicit conversion was taking place, but this is more clear I feel.

}

[Fact(Skip = "Remove to run test")]
public void Prime_number()
{
Assert.Equal(new[] { 2 }, PrimeFactors.Factors(2));
Assert.Equal(new[] { 2 }, PrimeFactors.Factors(2L));
}

[Fact(Skip = "Remove to run test")]
public void Square_of_a_prime()
{
Assert.Equal(new[] { 3, 3 }, PrimeFactors.Factors(9));
Assert.Equal(new[] { 3, 3 }, PrimeFactors.Factors(9L));
}

[Fact(Skip = "Remove to run test")]
public void Cube_of_a_prime()
{
Assert.Equal(new[] { 2, 2, 2 }, PrimeFactors.Factors(8));
Assert.Equal(new[] { 2, 2, 2 }, PrimeFactors.Factors(8L));
}

[Fact(Skip = "Remove to run test")]
public void Product_of_primes_and_non_primes()
{
Assert.Equal(new[] { 2, 2, 3 }, PrimeFactors.Factors(12));
Assert.Equal(new[] { 2, 2, 3 }, PrimeFactors.Factors(12L));
}

[Fact(Skip = "Remove to run test")]
public void Product_of_primes()
{
Assert.Equal(new[] { 5, 17, 23, 461 }, PrimeFactors.Factors(901255));
Assert.Equal(new[] { 5, 17, 23, 461 }, PrimeFactors.Factors(901255L));
}

[Fact(Skip = "Remove to run test")]
public void Factors_include_a_large_prime()
{
Assert.Equal(new[] { 11, 9539, 894119 }, PrimeFactors.Factors(93819012551));
Assert.Equal(new[] { 11, 9539, 894119 }, PrimeFactors.Factors(93819012551L));
}
}
30 changes: 15 additions & 15 deletions exercises/say/SayTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,90 +8,90 @@ public class SayTest
[Fact]
public void Zero()
{
Assert.Equal("zero", Say.InEnglish(0));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now made it clear that each input value is actually a long value. Previously, an implicit conversion was taking place, but this is more clear I feel.

Assert.Equal("zero", Say.InEnglish(0L));
}

[Fact(Skip = "Remove to run test")]
public void One()
{
Assert.Equal("one", Say.InEnglish(1));
Assert.Equal("one", Say.InEnglish(1L));
}

[Fact(Skip = "Remove to run test")]
public void Fourteen()
{
Assert.Equal("fourteen", Say.InEnglish(14));
Assert.Equal("fourteen", Say.InEnglish(14L));
}

[Fact(Skip = "Remove to run test")]
public void Twenty()
{
Assert.Equal("twenty", Say.InEnglish(20));
Assert.Equal("twenty", Say.InEnglish(20L));
}

[Fact(Skip = "Remove to run test")]
public void Twenty_two()
{
Assert.Equal("twenty-two", Say.InEnglish(22));
Assert.Equal("twenty-two", Say.InEnglish(22L));
}

[Fact(Skip = "Remove to run test")]
public void One_hundred()
{
Assert.Equal("one hundred", Say.InEnglish(100));
Assert.Equal("one hundred", Say.InEnglish(100L));
}

[Fact(Skip = "Remove to run test")]
public void One_hundred_twenty_three()
{
Assert.Equal("one hundred twenty-three", Say.InEnglish(123));
Assert.Equal("one hundred twenty-three", Say.InEnglish(123L));
}

[Fact(Skip = "Remove to run test")]
public void One_thousand()
{
Assert.Equal("one thousand", Say.InEnglish(1000));
Assert.Equal("one thousand", Say.InEnglish(1000L));
}

[Fact(Skip = "Remove to run test")]
public void One_thousand_two_hundred_thirty_four()
{
Assert.Equal("one thousand two hundred thirty-four", Say.InEnglish(1234));
Assert.Equal("one thousand two hundred thirty-four", Say.InEnglish(1234L));
}

[Fact(Skip = "Remove to run test")]
public void One_million()
{
Assert.Equal("one million", Say.InEnglish(1000000));
Assert.Equal("one million", Say.InEnglish(1000000L));
}

[Fact(Skip = "Remove to run test")]
public void One_million_two_thousand_three_hundred_forty_five()
{
Assert.Equal("one million two thousand three hundred forty-five", Say.InEnglish(1002345));
Assert.Equal("one million two thousand three hundred forty-five", Say.InEnglish(1002345L));
}

[Fact(Skip = "Remove to run test")]
public void One_billion()
{
Assert.Equal("one billion", Say.InEnglish(1000000000));
Assert.Equal("one billion", Say.InEnglish(1000000000L));
}

[Fact(Skip = "Remove to run test")]
public void A_big_number()
{
Assert.Equal("nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three", Say.InEnglish(987654321123));
Assert.Equal("nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three", Say.InEnglish(987654321123L));
}

[Fact(Skip = "Remove to run test")]
public void Numbers_below_zero_are_out_of_range()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Say.InEnglish(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => Say.InEnglish(-1L));
}

[Fact(Skip = "Remove to run test")]
public void Numbers_above_999_999_999_999_are_out_of_range()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Say.InEnglish(1000000000000));
Assert.Throws<ArgumentOutOfRangeException>(() => Say.InEnglish(1000000000000L));
}
}
4 changes: 2 additions & 2 deletions generators/Exercises/Generators/DiffieHellman.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ private static dynamic ConvertKeyExchangeInput(dynamic input, TestMethod testMet
{
switch (input)
{
case long l:
return new BigInteger(l);
case int i:
return new BigInteger(i);
case string str:
return new UnescapedValue($"{testMethod.TestedClass}.{char.ToUpper(str[0]) + str.Substring(1)}");
default:
Expand Down
6 changes: 5 additions & 1 deletion generators/Exercises/Generators/PrimeFactors.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
namespace Exercism.CSharp.Exercises.Generators
using Exercism.CSharp.Output;

namespace Exercism.CSharp.Exercises.Generators
{
public class PrimeFactors : GeneratorExercise
{
protected override void UpdateTestMethod(TestMethod testMethod) =>
testMethod.Input["value"] = (long)testMethod.Input["value"];
}
}
3 changes: 2 additions & 1 deletion generators/Exercises/Generators/Say.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ public class Say : GeneratorExercise
protected override void UpdateTestMethod(TestMethod testMethod)
{
testMethod.TestedMethod = "InEnglish";
testMethod.Input["number"] = (long)testMethod.Input["number"];

if (!(testMethod.Expected is string))
if (testMethod.ExpectedIsError)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't resist a tiny cleanup :)

testMethod.ExceptionThrown = typeof(ArgumentOutOfRangeException);
}
}
Expand Down
12 changes: 4 additions & 8 deletions generators/Input/JTokenHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,12 @@ public static dynamic ConvertJToken(JToken jToken)

private static dynamic ConvertJObject(JObject jObject)
{
var properties = jObject.ToObject<IDictionary<string, dynamic>>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line was the main issue, as the conversion here would result in the dictionary not containing JToken values, which meant that the ConvertJToken functionality (which already had code to deal with long/int value conversions) was not called.

Interestingly, the new code is way easier!

var properties = new Dictionary<string, dynamic>(jObject.Count, StringComparer.OrdinalIgnoreCase);

for (var i = 0; i < properties.Count; i++)
{
var key = properties.Keys.ElementAt(i);
var value = properties[key];
properties[key] = value is JToken jToken ? ConvertJToken(jToken) : value;
}
foreach (var (key, value) in jObject)
properties[key] = ConvertJToken(value);

return new Dictionary<string, dynamic>(properties, StringComparer.OrdinalIgnoreCase);
return properties;
}

private static dynamic ConvertJArray(JArray jArray)
Expand Down
1 change: 1 addition & 0 deletions generators/Output/Rendering/Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public string Object(object val)
case uint ui: return Uint(ui);
case float flt: return Float(flt);
case ulong ulng: return Ulong(ulng);
case long l: return Long(l);
case char c: return Char(c);
case DateTime dateTime: return DateTime(dateTime);
case Regex regex: return Regex(regex);
Expand Down
2 changes: 2 additions & 0 deletions generators/Output/Rendering/RenderNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public partial class Render
public string Float(float flt) => flt.ToString(CultureInfo.InvariantCulture);

public string Int(int i) => i.ToString(CultureInfo.InvariantCulture);

public string Long(long lng) => $"{lng}L";

public string Ulong(ulong ulng) => $"{ulng}UL";

Expand Down