Skip to content

Commit b9d75e9

Browse files
ErikSchierboomrobkeim
authored andcommitted
Improved long data type support (#1311)
* Convert JTokens that are in nested objects * Add support for rendering long values
1 parent 00dde79 commit b9d75e9

File tree

8 files changed

+38
-34
lines changed

8 files changed

+38
-34
lines changed

exercises/prime-factors/PrimeFactorsTest.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,42 @@ public class PrimeFactorsTest
77
[Fact]
88
public void No_factors()
99
{
10-
Assert.Empty(PrimeFactors.Factors(1));
10+
Assert.Empty(PrimeFactors.Factors(1L));
1111
}
1212

1313
[Fact(Skip = "Remove to run test")]
1414
public void Prime_number()
1515
{
16-
Assert.Equal(new[] { 2 }, PrimeFactors.Factors(2));
16+
Assert.Equal(new[] { 2 }, PrimeFactors.Factors(2L));
1717
}
1818

1919
[Fact(Skip = "Remove to run test")]
2020
public void Square_of_a_prime()
2121
{
22-
Assert.Equal(new[] { 3, 3 }, PrimeFactors.Factors(9));
22+
Assert.Equal(new[] { 3, 3 }, PrimeFactors.Factors(9L));
2323
}
2424

2525
[Fact(Skip = "Remove to run test")]
2626
public void Cube_of_a_prime()
2727
{
28-
Assert.Equal(new[] { 2, 2, 2 }, PrimeFactors.Factors(8));
28+
Assert.Equal(new[] { 2, 2, 2 }, PrimeFactors.Factors(8L));
2929
}
3030

3131
[Fact(Skip = "Remove to run test")]
3232
public void Product_of_primes_and_non_primes()
3333
{
34-
Assert.Equal(new[] { 2, 2, 3 }, PrimeFactors.Factors(12));
34+
Assert.Equal(new[] { 2, 2, 3 }, PrimeFactors.Factors(12L));
3535
}
3636

3737
[Fact(Skip = "Remove to run test")]
3838
public void Product_of_primes()
3939
{
40-
Assert.Equal(new[] { 5, 17, 23, 461 }, PrimeFactors.Factors(901255));
40+
Assert.Equal(new[] { 5, 17, 23, 461 }, PrimeFactors.Factors(901255L));
4141
}
4242

4343
[Fact(Skip = "Remove to run test")]
4444
public void Factors_include_a_large_prime()
4545
{
46-
Assert.Equal(new[] { 11, 9539, 894119 }, PrimeFactors.Factors(93819012551));
46+
Assert.Equal(new[] { 11, 9539, 894119 }, PrimeFactors.Factors(93819012551L));
4747
}
4848
}

exercises/say/SayTest.cs

+15-15
Original file line numberDiff line numberDiff line change
@@ -8,90 +8,90 @@ public class SayTest
88
[Fact]
99
public void Zero()
1010
{
11-
Assert.Equal("zero", Say.InEnglish(0));
11+
Assert.Equal("zero", Say.InEnglish(0L));
1212
}
1313

1414
[Fact(Skip = "Remove to run test")]
1515
public void One()
1616
{
17-
Assert.Equal("one", Say.InEnglish(1));
17+
Assert.Equal("one", Say.InEnglish(1L));
1818
}
1919

2020
[Fact(Skip = "Remove to run test")]
2121
public void Fourteen()
2222
{
23-
Assert.Equal("fourteen", Say.InEnglish(14));
23+
Assert.Equal("fourteen", Say.InEnglish(14L));
2424
}
2525

2626
[Fact(Skip = "Remove to run test")]
2727
public void Twenty()
2828
{
29-
Assert.Equal("twenty", Say.InEnglish(20));
29+
Assert.Equal("twenty", Say.InEnglish(20L));
3030
}
3131

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

3838
[Fact(Skip = "Remove to run test")]
3939
public void One_hundred()
4040
{
41-
Assert.Equal("one hundred", Say.InEnglish(100));
41+
Assert.Equal("one hundred", Say.InEnglish(100L));
4242
}
4343

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

5050
[Fact(Skip = "Remove to run test")]
5151
public void One_thousand()
5252
{
53-
Assert.Equal("one thousand", Say.InEnglish(1000));
53+
Assert.Equal("one thousand", Say.InEnglish(1000L));
5454
}
5555

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

6262
[Fact(Skip = "Remove to run test")]
6363
public void One_million()
6464
{
65-
Assert.Equal("one million", Say.InEnglish(1000000));
65+
Assert.Equal("one million", Say.InEnglish(1000000L));
6666
}
6767

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

7474
[Fact(Skip = "Remove to run test")]
7575
public void One_billion()
7676
{
77-
Assert.Equal("one billion", Say.InEnglish(1000000000));
77+
Assert.Equal("one billion", Say.InEnglish(1000000000L));
7878
}
7979

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

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

9292
[Fact(Skip = "Remove to run test")]
9393
public void Numbers_above_999_999_999_999_are_out_of_range()
9494
{
95-
Assert.Throws<ArgumentOutOfRangeException>(() => Say.InEnglish(1000000000000));
95+
Assert.Throws<ArgumentOutOfRangeException>(() => Say.InEnglish(1000000000000L));
9696
}
9797
}

generators/Exercises/Generators/DiffieHellman.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ private static dynamic ConvertKeyExchangeInput(dynamic input, TestMethod testMet
9494
{
9595
switch (input)
9696
{
97-
case long l:
98-
return new BigInteger(l);
97+
case int i:
98+
return new BigInteger(i);
9999
case string str:
100100
return new UnescapedValue($"{testMethod.TestedClass}.{char.ToUpper(str[0]) + str.Substring(1)}");
101101
default:
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
namespace Exercism.CSharp.Exercises.Generators
1+
using Exercism.CSharp.Output;
2+
3+
namespace Exercism.CSharp.Exercises.Generators
24
{
35
public class PrimeFactors : GeneratorExercise
46
{
7+
protected override void UpdateTestMethod(TestMethod testMethod) =>
8+
testMethod.Input["value"] = (long)testMethod.Input["value"];
59
}
610
}

generators/Exercises/Generators/Say.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ public class Say : GeneratorExercise
88
protected override void UpdateTestMethod(TestMethod testMethod)
99
{
1010
testMethod.TestedMethod = "InEnglish";
11+
testMethod.Input["number"] = (long)testMethod.Input["number"];
1112

12-
if (!(testMethod.Expected is string))
13+
if (testMethod.ExpectedIsError)
1314
testMethod.ExceptionThrown = typeof(ArgumentOutOfRangeException);
1415
}
1516
}

generators/Input/JTokenHelper.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,12 @@ public static dynamic ConvertJToken(JToken jToken)
4545

4646
private static dynamic ConvertJObject(JObject jObject)
4747
{
48-
var properties = jObject.ToObject<IDictionary<string, dynamic>>();
48+
var properties = new Dictionary<string, dynamic>(jObject.Count, StringComparer.OrdinalIgnoreCase);
4949

50-
for (var i = 0; i < properties.Count; i++)
51-
{
52-
var key = properties.Keys.ElementAt(i);
53-
var value = properties[key];
54-
properties[key] = value is JToken jToken ? ConvertJToken(jToken) : value;
55-
}
50+
foreach (var (key, value) in jObject)
51+
properties[key] = ConvertJToken(value);
5652

57-
return new Dictionary<string, dynamic>(properties, StringComparer.OrdinalIgnoreCase);
53+
return properties;
5854
}
5955

6056
private static dynamic ConvertJArray(JArray jArray)

generators/Output/Rendering/Render.cs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public string Object(object val)
2222
case uint ui: return Uint(ui);
2323
case float flt: return Float(flt);
2424
case ulong ulng: return Ulong(ulng);
25+
case long l: return Long(l);
2526
case char c: return Char(c);
2627
case DateTime dateTime: return DateTime(dateTime);
2728
case Regex regex: return Regex(regex);

generators/Output/Rendering/RenderNumber.cs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public partial class Render
1212
public string Float(float flt) => flt.ToString(CultureInfo.InvariantCulture);
1313

1414
public string Int(int i) => i.ToString(CultureInfo.InvariantCulture);
15+
16+
public string Long(long lng) => $"{lng}L";
1517

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

0 commit comments

Comments
 (0)