Skip to content

Commit 5605560

Browse files
robkeimErikSchierboom
authored andcommitted
Add test generator for grains exercise (#392)
* Add test generator for grains exercise * Integrate feedback
1 parent ddc5c5b commit 5605560

4 files changed

Lines changed: 77 additions & 18 deletions

File tree

exercises/grains/Example.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
public static class Grains
1+
using System;
2+
3+
public static class Grains
24
{
35
public static ulong Square(int n)
46
{
7+
if (n <= 0 || n > 64)
8+
{
9+
throw new ArgumentOutOfRangeException(nameof(n));
10+
}
11+
512
return n == 1
613
? 1
714
: 2 * Square(n - 1);

exercises/grains/GrainsTest.cs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,73 @@
1-
using Xunit;
1+
// This file was auto-generated based on version 1.0.0 of the canonical data.
2+
3+
using Xunit;
4+
using System;
25

36
public class GrainsTest
47
{
58
[Fact]
6-
public void Test_square_1()
9+
public void Returns_the_total_number_of_grains_on_the_board()
10+
{
11+
Assert.Equal(18446744073709551615UL, Grains.Total());
12+
}
13+
14+
[Fact(Skip = "Remove to run test")]
15+
public void Number_1()
16+
{
17+
Assert.Equal(1UL, Grains.Square(1));
18+
}
19+
20+
[Fact(Skip = "Remove to run test")]
21+
public void Number_2()
22+
{
23+
Assert.Equal(2UL, Grains.Square(2));
24+
}
25+
26+
[Fact(Skip = "Remove to run test")]
27+
public void Number_3()
728
{
8-
Assert.Equal(1ul, Grains.Square(1));
29+
Assert.Equal(4UL, Grains.Square(3));
930
}
1031

1132
[Fact(Skip = "Remove to run test")]
12-
public void Test_square_2()
33+
public void Number_4()
1334
{
14-
Assert.Equal(2ul, Grains.Square(2));
35+
Assert.Equal(8UL, Grains.Square(4));
1536
}
1637

1738
[Fact(Skip = "Remove to run test")]
18-
public void Test_square_3()
39+
public void Number_16()
1940
{
20-
Assert.Equal(4ul, Grains.Square(3));
41+
Assert.Equal(32768UL, Grains.Square(16));
2142
}
2243

2344
[Fact(Skip = "Remove to run test")]
24-
public void Test_square_4()
45+
public void Number_32()
2546
{
26-
Assert.Equal(8ul, Grains.Square(4));
47+
Assert.Equal(2147483648UL, Grains.Square(32));
2748
}
2849

2950
[Fact(Skip = "Remove to run test")]
30-
public void Test_square_16()
51+
public void Number_64()
3152
{
32-
Assert.Equal(32768ul, Grains.Square(16));
53+
Assert.Equal(9223372036854775808UL, Grains.Square(64));
3354
}
3455

3556
[Fact(Skip = "Remove to run test")]
36-
public void Test_square_32()
57+
public void Square_0_raises_an_exception()
3758
{
38-
Assert.Equal(2147483648ul, Grains.Square(32));
59+
Assert.Throws<ArgumentOutOfRangeException>(() => Grains.Square(0));
3960
}
4061

4162
[Fact(Skip = "Remove to run test")]
42-
public void Test_square_64()
63+
public void Negative_square_raises_an_exception()
4364
{
44-
Assert.Equal(9223372036854775808ul, Grains.Square(64));
65+
Assert.Throws<ArgumentOutOfRangeException>(() => Grains.Square(-1));
4566
}
4667

4768
[Fact(Skip = "Remove to run test")]
48-
public void Test_total_grains()
69+
public void Square_greater_than_64_raises_an_exception()
4970
{
50-
Assert.Equal(18446744073709551615ul, Grains.Total());
71+
Assert.Throws<ArgumentOutOfRangeException>(() => Grains.Square(65));
5172
}
5273
}

generators/Exercises/Grains.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using Generators.Input;
3+
4+
namespace Generators.Exercises
5+
{
6+
public class Grains : Exercise
7+
{
8+
protected override void UpdateCanonicalData(CanonicalData canonicalData)
9+
{
10+
foreach (var canonicalDataCase in canonicalData.Cases)
11+
{
12+
if (ShouldThrowException(canonicalDataCase.Expected))
13+
{
14+
canonicalDataCase.ExceptionThrown = typeof(ArgumentOutOfRangeException);
15+
}
16+
else
17+
{
18+
canonicalDataCase.Expected = ulong.Parse(canonicalDataCase.Expected.ToString());
19+
}
20+
}
21+
}
22+
23+
private static bool ShouldThrowException(object value)
24+
{
25+
return int.TryParse(value.ToString(), out int result)
26+
&& result == -1;
27+
}
28+
}
29+
}

generators/Output/ValueFormatter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public static object Format(object val)
3232
return dbl.ToString(CultureInfo.InvariantCulture);
3333
case float flt:
3434
return flt.ToString(CultureInfo.InvariantCulture);
35+
case ulong ulng:
36+
return $"{ulng}UL";
3537
case char c:
3638
return $"'{c}'";
3739
default:

0 commit comments

Comments
 (0)