Skip to content

Commit 4fc0a21

Browse files
Add more generators (#2365)
* bob: add generator * collatz-conjecture: add generator * Be specific * grains: add generator [no important files changed] * hello-world: add generator * trinary: add generator * pig-latin: add generator * protein-translation: add generator * reverse-string: add generator * resistor-color: add generator * largest-series-product: add generator * luhn: add generator * phone-number: add generator * raindrops: add generator * scrabble-score: add generator * matching-brackets: add generator * markdown: add generator * run-length-encoding: add generator * say: add generator * resistor-color-duo: add generator * resistor-color-trio: add generator * roman-numerals: add generator * binary: add generator * secret-handshake: add generator * change: add generator * rail-fence-cipher: add generator * nth-prime: add generator * binary-search: add generator * rna-transcription: add generator * matrix: add generator * binary: fix * resistor-color-trio: fix * matrix: fix
1 parent 1720a5a commit 4fc0a21

File tree

52 files changed

+705
-280
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+705
-280
lines changed

bin/generate-tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $ErrorActionPreference = "Stop"
2424
$PSNativeCommandUseErrorActionPreference = $true
2525

2626
if ($Exercise) {
27-
dotnet run --project generators --exercise $Exercise
27+
dotnet run --project generators generate --exercise $Exercise
2828
} else {
29-
dotnet run --project generators
29+
dotnet run --project generators generate
3030
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using Xunit;
3+
4+
public class {{testClass}}
5+
{
6+
{{for test in tests}}
7+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
8+
public void {{test.testMethod}}()
9+
{
10+
Assert.Equal({{if test.expected.error}}-1{{else}}{{test.expected}}{{end}}, {{testedClass}}.{{test.testedMethod}}({{test.input.array}}, {{test.input.value}}));
11+
}
12+
{{end}}
13+
}

exercises/practice/binary-search/BinarySearchTests.cs

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,88 +6,66 @@ public class BinarySearchTests
66
[Fact]
77
public void Finds_a_value_in_an_array_with_one_element()
88
{
9-
var array = new[] { 6 };
10-
var value = 6;
11-
Assert.Equal(0, BinarySearch.Find(array, value));
9+
Assert.Equal(0, BinarySearch.Find([6], 6));
1210
}
1311

1412
[Fact(Skip = "Remove this Skip property to run this test")]
1513
public void Finds_a_value_in_the_middle_of_an_array()
1614
{
17-
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
18-
var value = 6;
19-
Assert.Equal(3, BinarySearch.Find(array, value));
15+
Assert.Equal(3, BinarySearch.Find([1, 3, 4, 6, 8, 9, 11], 6));
2016
}
2117

2218
[Fact(Skip = "Remove this Skip property to run this test")]
2319
public void Finds_a_value_at_the_beginning_of_an_array()
2420
{
25-
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
26-
var value = 1;
27-
Assert.Equal(0, BinarySearch.Find(array, value));
21+
Assert.Equal(0, BinarySearch.Find([1, 3, 4, 6, 8, 9, 11], 1));
2822
}
2923

3024
[Fact(Skip = "Remove this Skip property to run this test")]
3125
public void Finds_a_value_at_the_end_of_an_array()
3226
{
33-
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
34-
var value = 11;
35-
Assert.Equal(6, BinarySearch.Find(array, value));
27+
Assert.Equal(6, BinarySearch.Find([1, 3, 4, 6, 8, 9, 11], 11));
3628
}
3729

3830
[Fact(Skip = "Remove this Skip property to run this test")]
3931
public void Finds_a_value_in_an_array_of_odd_length()
4032
{
41-
var array = new[] { 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634 };
42-
var value = 144;
43-
Assert.Equal(9, BinarySearch.Find(array, value));
33+
Assert.Equal(9, BinarySearch.Find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144));
4434
}
4535

4636
[Fact(Skip = "Remove this Skip property to run this test")]
4737
public void Finds_a_value_in_an_array_of_even_length()
4838
{
49-
var array = new[] { 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 };
50-
var value = 21;
51-
Assert.Equal(5, BinarySearch.Find(array, value));
39+
Assert.Equal(5, BinarySearch.Find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21));
5240
}
5341

5442
[Fact(Skip = "Remove this Skip property to run this test")]
5543
public void Identifies_that_a_value_is_not_included_in_the_array()
5644
{
57-
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
58-
var value = 7;
59-
Assert.Equal(-1, BinarySearch.Find(array, value));
45+
Assert.Equal(-1, BinarySearch.Find([1, 3, 4, 6, 8, 9, 11], 7));
6046
}
6147

6248
[Fact(Skip = "Remove this Skip property to run this test")]
63-
public void A_value_smaller_than_the_arrays_smallest_value_is_not_found()
49+
public void A_value_smaller_than_the_array_s_smallest_value_is_not_found()
6450
{
65-
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
66-
var value = 0;
67-
Assert.Equal(-1, BinarySearch.Find(array, value));
51+
Assert.Equal(-1, BinarySearch.Find([1, 3, 4, 6, 8, 9, 11], 0));
6852
}
6953

7054
[Fact(Skip = "Remove this Skip property to run this test")]
71-
public void A_value_larger_than_the_arrays_largest_value_is_not_found()
55+
public void A_value_larger_than_the_array_s_largest_value_is_not_found()
7256
{
73-
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
74-
var value = 13;
75-
Assert.Equal(-1, BinarySearch.Find(array, value));
57+
Assert.Equal(-1, BinarySearch.Find([1, 3, 4, 6, 8, 9, 11], 13));
7658
}
7759

7860
[Fact(Skip = "Remove this Skip property to run this test")]
7961
public void Nothing_is_found_in_an_empty_array()
8062
{
81-
var array = Array.Empty<int>();
82-
var value = 1;
83-
Assert.Equal(-1, BinarySearch.Find(array, value));
63+
Assert.Equal(-1, BinarySearch.Find([], 1));
8464
}
8565

8666
[Fact(Skip = "Remove this Skip property to run this test")]
8767
public void Nothing_is_found_when_the_left_and_right_bounds_cross()
8868
{
89-
var array = new[] { 1, 2 };
90-
var value = 0;
91-
Assert.Equal(-1, BinarySearch.Find(array, value));
69+
Assert.Equal(-1, BinarySearch.Find([1, 2], 0));
9270
}
9371
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Xunit;
2+
3+
public class {{testClass}}
4+
{
5+
{{for test in tests}}
6+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
7+
public void {{test.testMethod}}()
8+
{
9+
Assert.Equal({{if test.expected}}{{test.expected}}{{else}}0{{end}}, {{testedClass}}.To{{test.testedMethod}}({{test.input.binary | string.literal}}));
10+
}
11+
{{end}}
12+
}

exercises/practice/binary/BinaryTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Xunit;
22

3-
public class BinaryTest
3+
public class BinaryTests
44
{
55
[Fact]
66
public void Binary_0_is_decimal_0()
@@ -57,7 +57,7 @@ public void Binary_ignores_leading_zeros()
5757
}
5858

5959
[Fact(Skip = "Remove this Skip property to run this test")]
60-
public void Invalid_binary_2_converts_to_decimal_0()
60+
public void Two_is_not_a_valid_binary_digit()
6161
{
6262
Assert.Equal(0, Binary.ToDecimal("2"));
6363
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Xunit;
2+
3+
public class {{testClass}}
4+
{
5+
{{for test in tests}}
6+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
7+
public void {{test.testMethod}}()
8+
{
9+
Assert.Equal({{test.expected | string.literal}}, {{testedClass}}.{{test.testedMethod}}({{test.input.heyBob | string.literal}}));
10+
}
11+
{{end}}
12+
}

exercises/practice/bob/BobTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ public void Alternate_silence()
122122
Assert.Equal("Fine. Be that way!", Bob.Response("\t\t\t\t\t\t\t\t\t\t"));
123123
}
124124

125-
[Fact(Skip = "Remove this Skip property to run this test")]
126-
public void Multiple_line_question()
127-
{
128-
Assert.Equal("Whatever.", Bob.Response("\nDoes this cryogenic chamber make me look fat?\nNo."));
129-
}
130-
131125
[Fact(Skip = "Remove this Skip property to run this test")]
132126
public void Starting_with_whitespace()
133127
{
@@ -151,4 +145,10 @@ public void Non_question_ending_with_whitespace()
151145
{
152146
Assert.Equal("Whatever.", Bob.Response("This is a statement ending with whitespace "));
153147
}
148+
149+
[Fact(Skip = "Remove this Skip property to run this test")]
150+
public void Multiple_line_question()
151+
{
152+
Assert.Equal("Sure.", Bob.Response("\nDoes this cryogenic chamber make\n me look fat?"));
153+
}
154154
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Xunit;
3+
4+
public class {{testClass}}
5+
{
6+
{{for test in tests}}
7+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
8+
public void {{test.testMethod}}()
9+
{
10+
{{if test.expected.error}}
11+
int[] coins = {{test.input.coins}};
12+
Assert.Throws<ArgumentException>(() => {{testedClass}}.{{test.testedMethod}}(coins, {{test.input.target}}));
13+
{{else}}
14+
int[] coins = {{test.input.coins}};
15+
int[] expected = {{test.expected}};
16+
Assert.Equal(expected, {{testedClass}}.{{test.testedMethod}}(coins, {{test.input.target}}));
17+
{{end}}
18+
}
19+
{{end}}
20+
}

exercises/practice/change/ChangeTests.cs

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,104 +6,101 @@ public class ChangeTests
66
[Fact]
77
public void Change_for_1_cent()
88
{
9-
var coins = new[] { 1, 5, 10, 25 };
10-
var target = 1;
11-
var expected = new[] { 1 };
12-
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
9+
int[] coins = [1, 5, 10, 25];
10+
int[] expected = [1];
11+
Assert.Equal(expected, Change.FindFewestCoins(coins, 1));
1312
}
1413

1514
[Fact(Skip = "Remove this Skip property to run this test")]
1615
public void Single_coin_change()
1716
{
18-
var coins = new[] { 1, 5, 10, 25, 100 };
19-
var target = 25;
20-
var expected = new[] { 25 };
21-
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
17+
int[] coins = [1, 5, 10, 25, 100];
18+
int[] expected = [25];
19+
Assert.Equal(expected, Change.FindFewestCoins(coins, 25));
2220
}
2321

2422
[Fact(Skip = "Remove this Skip property to run this test")]
2523
public void Multiple_coin_change()
2624
{
27-
var coins = new[] { 1, 5, 10, 25, 100 };
28-
var target = 15;
29-
var expected = new[] { 5, 10 };
30-
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
25+
int[] coins = [1, 5, 10, 25, 100];
26+
int[] expected = [5, 10];
27+
Assert.Equal(expected, Change.FindFewestCoins(coins, 15));
3128
}
3229

3330
[Fact(Skip = "Remove this Skip property to run this test")]
3431
public void Change_with_lilliputian_coins()
3532
{
36-
var coins = new[] { 1, 4, 15, 20, 50 };
37-
var target = 23;
38-
var expected = new[] { 4, 4, 15 };
39-
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
33+
int[] coins = [1, 4, 15, 20, 50];
34+
int[] expected = [4, 4, 15];
35+
Assert.Equal(expected, Change.FindFewestCoins(coins, 23));
4036
}
4137

4238
[Fact(Skip = "Remove this Skip property to run this test")]
4339
public void Change_with_lower_elbonia_coins()
4440
{
45-
var coins = new[] { 1, 5, 10, 21, 25 };
46-
var target = 63;
47-
var expected = new[] { 21, 21, 21 };
48-
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
41+
int[] coins = [1, 5, 10, 21, 25];
42+
int[] expected = [21, 21, 21];
43+
Assert.Equal(expected, Change.FindFewestCoins(coins, 63));
4944
}
5045

5146
[Fact(Skip = "Remove this Skip property to run this test")]
5247
public void Large_target_values()
5348
{
54-
var coins = new[] { 1, 2, 5, 10, 20, 50, 100 };
55-
var target = 999;
56-
var expected = new[] { 2, 2, 5, 20, 20, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100 };
57-
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
49+
int[] coins = [1, 2, 5, 10, 20, 50, 100];
50+
int[] expected = [2, 2, 5, 20, 20, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100];
51+
Assert.Equal(expected, Change.FindFewestCoins(coins, 999));
5852
}
5953

6054
[Fact(Skip = "Remove this Skip property to run this test")]
6155
public void Possible_change_without_unit_coins_available()
6256
{
63-
var coins = new[] { 2, 5, 10, 20, 50 };
64-
var target = 21;
65-
var expected = new[] { 2, 2, 2, 5, 10 };
66-
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
57+
int[] coins = [2, 5, 10, 20, 50];
58+
int[] expected = [2, 2, 2, 5, 10];
59+
Assert.Equal(expected, Change.FindFewestCoins(coins, 21));
6760
}
6861

6962
[Fact(Skip = "Remove this Skip property to run this test")]
7063
public void Another_possible_change_without_unit_coins_available()
7164
{
72-
var coins = new[] { 4, 5 };
73-
var target = 27;
74-
var expected = new[] { 4, 4, 4, 5, 5, 5 };
75-
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
65+
int[] coins = [4, 5];
66+
int[] expected = [4, 4, 4, 5, 5, 5];
67+
Assert.Equal(expected, Change.FindFewestCoins(coins, 27));
68+
}
69+
70+
[Fact(Skip = "Remove this Skip property to run this test")]
71+
public void A_greedy_approach_is_not_optimal()
72+
{
73+
int[] coins = [1, 10, 11];
74+
int[] expected = [10, 10];
75+
Assert.Equal(expected, Change.FindFewestCoins(coins, 20));
7676
}
7777

7878
[Fact(Skip = "Remove this Skip property to run this test")]
7979
public void No_coins_make_0_change()
8080
{
81-
var coins = new[] { 1, 5, 10, 21, 25 };
82-
var target = 0;
83-
Assert.Empty(Change.FindFewestCoins(coins, target));
81+
int[] coins = [1, 5, 10, 21, 25];
82+
int[] expected = [];
83+
Assert.Equal(expected, Change.FindFewestCoins(coins, 0));
8484
}
8585

8686
[Fact(Skip = "Remove this Skip property to run this test")]
8787
public void Error_testing_for_change_smaller_than_the_smallest_of_coins()
8888
{
89-
var coins = new[] { 5, 10 };
90-
var target = 3;
91-
Assert.Throws<ArgumentException>(() => Change.FindFewestCoins(coins, target));
89+
int[] coins = [5, 10];
90+
Assert.Throws<ArgumentException>(() => Change.FindFewestCoins(coins, 3));
9291
}
9392

9493
[Fact(Skip = "Remove this Skip property to run this test")]
9594
public void Error_if_no_combination_can_add_up_to_target()
9695
{
97-
var coins = new[] { 5, 10 };
98-
var target = 94;
99-
Assert.Throws<ArgumentException>(() => Change.FindFewestCoins(coins, target));
96+
int[] coins = [5, 10];
97+
Assert.Throws<ArgumentException>(() => Change.FindFewestCoins(coins, 94));
10098
}
10199

102100
[Fact(Skip = "Remove this Skip property to run this test")]
103101
public void Cannot_find_negative_change_values()
104102
{
105-
var coins = new[] { 1, 2, 5 };
106-
var target = -5;
107-
Assert.Throws<ArgumentException>(() => Change.FindFewestCoins(coins, target));
103+
int[] coins = [1, 2, 5];
104+
Assert.Throws<ArgumentException>(() => Change.FindFewestCoins(coins, -5));
108105
}
109106
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using Xunit;
3+
4+
public class {{testClass}}
5+
{
6+
{{for test in tests}}
7+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
8+
public void {{test.testMethod}}()
9+
{
10+
{{if test.expected.error}}
11+
Assert.Throws<ArgumentOutOfRangeException>(() => {{testedClass}}.{{test.testedMethod}}({{test.input.number}}));
12+
{{else}}
13+
Assert.Equal({{test.expected}}, {{testedClass}}.{{test.testedMethod}}({{test.input.number}}));
14+
{{end}}
15+
}
16+
{{end}}
17+
}

0 commit comments

Comments
 (0)