Skip to content

Commit d42f9fa

Browse files
list-ops: add generator
1 parent 0228a37 commit d42f9fa

File tree

2 files changed

+142
-51
lines changed

2 files changed

+142
-51
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{{ func list_type
2+
case ($["depth"] ?? 1)
3+
when 1
4+
ret "List<int>"
5+
when 2
6+
ret "List<List<int>>"
7+
when 3
8+
ret "List<List<List<int>>>"
9+
when 4
10+
ret "List<List<List<List<int>>>>"
11+
end
12+
end }}
13+
14+
using System;
15+
using System.Collections.Generic;
16+
using Xunit;
17+
18+
public class {{ testClass }}
19+
{
20+
{{- for test in tests }}
21+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
22+
public void {{ test.testMethod }}()
23+
{
24+
{{- if test.property == "append" }}
25+
List<int> list1 = {{ test.input.list1 }};
26+
List<int> list2 = {{ test.input.list2 }};
27+
{{- if test.expected.empty? }}
28+
Assert.Empty({{ testedClass }}.{{ test.testedMethod }}(list1, list2));
29+
{{- else }}
30+
List<int> expected = {{ test.expected }};
31+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}(list1, list2));
32+
{{ end -}}
33+
{{- else if test.property == "concat" }}
34+
{{- if test.description == "list of nested lists" -}}
35+
List<List<List<int>>> lists = {{ test.input.lists }};
36+
{{- else -}}
37+
List<List<int>> lists = {{ test.input.lists }};
38+
{{- end -}}
39+
{{- if test.expected.empty? }}
40+
Assert.Empty({{ testedClass }}.{{ test.testedMethod }}(lists));
41+
{{- else }}
42+
{{- if test.description == "list of nested lists" }}
43+
List<List<int>> expected = {{ test.expected }};
44+
{{ else }}
45+
List<int> expected = {{ test.expected }};
46+
{{ end -}}
47+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}(lists));
48+
{{ end -}}
49+
{{- else if test.property == "filter" }}
50+
List<int> list = {{ test.input.list }};
51+
Func<int, bool> function = {{ test.input.function | string.replace "->" "=>" | string.replace "modulo" "%" }};
52+
{{- if test.expected.empty? }}
53+
Assert.Empty({{ testedClass }}.{{ test.testedMethod }}(list, function));
54+
{{- else }}
55+
List<int> expected = {{ test.expected }};
56+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}(list, function));
57+
{{ end -}}
58+
{{- else if test.property == "map" }}
59+
List<int> list = {{ test.input.list }};
60+
Func<int, int> function = {{ test.input.function | string.replace "->" "=>" | string.replace "modulo" "%" }};
61+
{{- if test.expected.empty? }}
62+
Assert.Empty({{ testedClass }}.{{ test.testedMethod }}(list, function));
63+
{{- else }}
64+
List<int> expected = {{ test.expected }};
65+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}(list, function));
66+
{{ end -}}
67+
{{- else if test.property == "length" }}
68+
List<int> list = {{ test.input.list }};
69+
Assert.Equal({{ test.expected }}, {{ testedClass }}.{{ test.testedMethod }}(list));
70+
{{- else if test.property == "foldl" || test.property == "foldr" }}
71+
List<int> list = {{ test.input.list }};
72+
int initial = {{ test.input.initial }};
73+
Func<int, int, int> function = {{ test.input.function | string.replace "->" "=>" | string.replace "modulo" "%" }};
74+
Assert.Equal({{ test.expected }}, {{ testedClass }}.{{ test.testedMethod }}(list, initial, function));
75+
{{- else if test.property == "reverse" }}
76+
{{- if test.expected.empty? }}
77+
List<int> list = {{ test.input.list }};
78+
Assert.Empty({{ testedClass }}.{{ test.testedMethod }}(list));
79+
{{- else if test.description == "list of lists is not flattened" }}
80+
List<List<int>> list = {{ test.input.list }};
81+
List<List<int>> expected = {{ test.expected }};
82+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}(list));
83+
{{ else }}
84+
List<int> list = {{ test.input.list }};
85+
List<int> expected = {{ test.expected }};
86+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}(list));
87+
{{ end -}}
88+
{{ end -}}
89+
}
90+
{{ end -}}
91+
}

exercises/practice/list-ops/ListOpsTests.cs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,183 +7,183 @@ public class ListOpsTests
77
[Fact]
88
public void Append_entries_to_a_list_and_return_the_new_list_empty_lists()
99
{
10-
var list1 = new List<int>();
11-
var list2 = new List<int>();
10+
List<int> list1 = [];
11+
List<int> list2 = [];
1212
Assert.Empty(ListOps.Append(list1, list2));
1313
}
1414

1515
[Fact(Skip = "Remove this Skip property to run this test")]
1616
public void Append_entries_to_a_list_and_return_the_new_list_list_to_empty_list()
1717
{
18-
var list1 = new List<int>();
19-
var list2 = new List<int> { 1, 2, 3, 4 };
20-
var expected = new List<int> { 1, 2, 3, 4 };
18+
List<int> list1 = [];
19+
List<int> list2 = [1, 2, 3, 4];
20+
List<int> expected = [1, 2, 3, 4];
2121
Assert.Equal(expected, ListOps.Append(list1, list2));
2222
}
2323

2424
[Fact(Skip = "Remove this Skip property to run this test")]
2525
public void Append_entries_to_a_list_and_return_the_new_list_empty_list_to_list()
2626
{
27-
var list1 = new List<int> { 1, 2, 3, 4 };
28-
var list2 = new List<int>();
29-
var expected = new List<int> { 1, 2, 3, 4 };
27+
List<int> list1 = [1, 2, 3, 4];
28+
List<int> list2 = [];
29+
List<int> expected = [1, 2, 3, 4];
3030
Assert.Equal(expected, ListOps.Append(list1, list2));
3131
}
3232

3333
[Fact(Skip = "Remove this Skip property to run this test")]
3434
public void Append_entries_to_a_list_and_return_the_new_list_non_empty_lists()
3535
{
36-
var list1 = new List<int> { 1, 2 };
37-
var list2 = new List<int> { 2, 3, 4, 5 };
38-
var expected = new List<int> { 1, 2, 2, 3, 4, 5 };
36+
List<int> list1 = [1, 2];
37+
List<int> list2 = [2, 3, 4, 5];
38+
List<int> expected = [1, 2, 2, 3, 4, 5];
3939
Assert.Equal(expected, ListOps.Append(list1, list2));
4040
}
4141

4242
[Fact(Skip = "Remove this Skip property to run this test")]
4343
public void Concatenate_a_list_of_lists_empty_list()
4444
{
45-
var lists = new List<List<int>>();
45+
List<List<int>> lists = [];
4646
Assert.Empty(ListOps.Concat(lists));
4747
}
4848

4949
[Fact(Skip = "Remove this Skip property to run this test")]
5050
public void Concatenate_a_list_of_lists_list_of_lists()
5151
{
52-
var lists = new List<List<int>> { new List<int> { 1, 2 }, new List<int> { 3 }, new List<int>(), new List<int> { 4, 5, 6 } };
53-
var expected = new List<int> { 1, 2, 3, 4, 5, 6 };
52+
List<List<int>> lists = [[1, 2], [3], [], [4, 5, 6]];
53+
List<int> expected = [1, 2, 3, 4, 5, 6];
5454
Assert.Equal(expected, ListOps.Concat(lists));
5555
}
5656

5757
[Fact(Skip = "Remove this Skip property to run this test")]
5858
public void Concatenate_a_list_of_lists_list_of_nested_lists()
5959
{
60-
var lists = new List<List<List<int>>> { new List<List<int>> { new List<int> { 1 }, new List<int> { 2 } }, new List<List<int>> { new List<int> { 3 } }, new List<List<int>> { new List<int>() }, new List<List<int>> { new List<int> { 4, 5, 6 } } };
61-
var expected = new List<List<int>> { new List<int> { 1 }, new List<int> { 2 }, new List<int> { 3 }, new List<int>(), new List<int> { 4, 5, 6 } };
60+
List<List<List<int>>> lists = [[[1], [2]], [[3]], [[]], [[4, 5, 6]]];
61+
List<List<int>> expected = [[1], [2], [3], [], [4, 5, 6]];
6262
Assert.Equal(expected, ListOps.Concat(lists));
6363
}
6464

6565
[Fact(Skip = "Remove this Skip property to run this test")]
6666
public void Filter_list_returning_only_values_that_satisfy_the_filter_function_empty_list()
6767
{
68-
var list = new List<int>();
69-
var function = new Func<int, bool>((x) => x % 2 == 1);
68+
List<int> list = [];
69+
Func<int, bool> function = (x) => x % 2 == 1;
7070
Assert.Empty(ListOps.Filter(list, function));
7171
}
7272

7373
[Fact(Skip = "Remove this Skip property to run this test")]
7474
public void Filter_list_returning_only_values_that_satisfy_the_filter_function_non_empty_list()
7575
{
76-
var list = new List<int> { 1, 2, 3, 5 };
77-
var function = new Func<int, bool>((x) => x % 2 == 1);
78-
var expected = new List<int> { 1, 3, 5 };
76+
List<int> list = [1, 2, 3, 5];
77+
Func<int, bool> function = (x) => x % 2 == 1;
78+
List<int> expected = [1, 3, 5];
7979
Assert.Equal(expected, ListOps.Filter(list, function));
8080
}
8181

8282
[Fact(Skip = "Remove this Skip property to run this test")]
8383
public void Returns_the_length_of_a_list_empty_list()
8484
{
85-
var list = new List<int>();
85+
List<int> list = [];
8686
Assert.Equal(0, ListOps.Length(list));
8787
}
8888

8989
[Fact(Skip = "Remove this Skip property to run this test")]
9090
public void Returns_the_length_of_a_list_non_empty_list()
9191
{
92-
var list = new List<int> { 1, 2, 3, 4 };
92+
List<int> list = [1, 2, 3, 4];
9393
Assert.Equal(4, ListOps.Length(list));
9494
}
9595

9696
[Fact(Skip = "Remove this Skip property to run this test")]
9797
public void Return_a_list_of_elements_whose_values_equal_the_list_value_transformed_by_the_mapping_function_empty_list()
9898
{
99-
var list = new List<int>();
100-
var function = new Func<int, int>((x) => x + 1);
99+
List<int> list = [];
100+
Func<int, int> function = (x) => x + 1;
101101
Assert.Empty(ListOps.Map(list, function));
102102
}
103103

104104
[Fact(Skip = "Remove this Skip property to run this test")]
105105
public void Return_a_list_of_elements_whose_values_equal_the_list_value_transformed_by_the_mapping_function_non_empty_list()
106106
{
107-
var list = new List<int> { 1, 3, 5, 7 };
108-
var function = new Func<int, int>((x) => x + 1);
109-
var expected = new List<int> { 2, 4, 6, 8 };
107+
List<int> list = [1, 3, 5, 7];
108+
Func<int, int> function = (x) => x + 1;
109+
List<int> expected = [2, 4, 6, 8];
110110
Assert.Equal(expected, ListOps.Map(list, function));
111111
}
112112

113113
[Fact(Skip = "Remove this Skip property to run this test")]
114114
public void Folds_reduces_the_given_list_from_the_left_with_a_function_direction_dependent_function_applied_to_non_empty_list()
115115
{
116-
var list = new List<int> { 2, 5 };
117-
var initial = 5;
118-
var function = new Func<int, int, int>((x, y) => x / y);
116+
List<int> list = [2, 5];
117+
int initial = 5;
118+
Func<int, int, int> function = (x, y) => x / y;
119119
Assert.Equal(0, ListOps.Foldl(list, initial, function));
120120
}
121121

122122
[Fact(Skip = "Remove this Skip property to run this test")]
123123
public void Folds_reduces_the_given_list_from_the_left_with_a_function_empty_list()
124124
{
125-
var list = new List<int>();
126-
var initial = 2;
127-
var function = new Func<int, int, int>((acc, el) => el * acc);
125+
List<int> list = [];
126+
int initial = 2;
127+
Func<int, int, int> function = (acc, el) => el * acc;
128128
Assert.Equal(2, ListOps.Foldl(list, initial, function));
129129
}
130130

131131
[Fact(Skip = "Remove this Skip property to run this test")]
132132
public void Folds_reduces_the_given_list_from_the_left_with_a_function_direction_independent_function_applied_to_non_empty_list()
133133
{
134-
var list = new List<int> { 1, 2, 3, 4 };
135-
var initial = 5;
136-
var function = new Func<int, int, int>((acc, el) => el + acc);
134+
List<int> list = [1, 2, 3, 4];
135+
int initial = 5;
136+
Func<int, int, int> function = (acc, el) => el + acc;
137137
Assert.Equal(15, ListOps.Foldl(list, initial, function));
138138
}
139139

140140
[Fact(Skip = "Remove this Skip property to run this test")]
141141
public void Folds_reduces_the_given_list_from_the_right_with_a_function_direction_dependent_function_applied_to_non_empty_list()
142142
{
143-
var list = new List<int> { 2, 5 };
144-
var initial = 5;
145-
var function = new Func<int, int, int>((x, y) => x / y);
143+
List<int> list = [2, 5];
144+
int initial = 5;
145+
Func<int, int, int> function = (x, y) => x / y;
146146
Assert.Equal(2, ListOps.Foldr(list, initial, function));
147147
}
148148

149149
[Fact(Skip = "Remove this Skip property to run this test")]
150150
public void Folds_reduces_the_given_list_from_the_right_with_a_function_empty_list()
151151
{
152-
var list = new List<int>();
153-
var initial = 2;
154-
var function = new Func<int, int, int>((acc, el) => el * acc);
152+
List<int> list = [];
153+
int initial = 2;
154+
Func<int, int, int> function = (acc, el) => el * acc;
155155
Assert.Equal(2, ListOps.Foldr(list, initial, function));
156156
}
157157

158158
[Fact(Skip = "Remove this Skip property to run this test")]
159159
public void Folds_reduces_the_given_list_from_the_right_with_a_function_direction_independent_function_applied_to_non_empty_list()
160160
{
161-
var list = new List<int> { 1, 2, 3, 4 };
162-
var initial = 5;
163-
var function = new Func<int, int, int>((acc, el) => el + acc);
161+
List<int> list = [1, 2, 3, 4];
162+
int initial = 5;
163+
Func<int, int, int> function = (acc, el) => el + acc;
164164
Assert.Equal(15, ListOps.Foldr(list, initial, function));
165165
}
166166

167167
[Fact(Skip = "Remove this Skip property to run this test")]
168168
public void Reverse_the_elements_of_the_list_empty_list()
169169
{
170-
var list = new List<int>();
170+
List<int> list = [];
171171
Assert.Empty(ListOps.Reverse(list));
172172
}
173173

174174
[Fact(Skip = "Remove this Skip property to run this test")]
175175
public void Reverse_the_elements_of_the_list_non_empty_list()
176176
{
177-
var list = new List<int> { 1, 3, 5, 7 };
178-
var expected = new List<int> { 7, 5, 3, 1 };
177+
List<int> list = [1, 3, 5, 7];
178+
List<int> expected = [7, 5, 3, 1];
179179
Assert.Equal(expected, ListOps.Reverse(list));
180180
}
181181

182182
[Fact(Skip = "Remove this Skip property to run this test")]
183183
public void Reverse_the_elements_of_the_list_list_of_lists_is_not_flattened()
184184
{
185-
var list = new List<List<int>> { new List<int> { 1, 2 }, new List<int> { 3 }, new List<int>(), new List<int> { 4, 5, 6 } };
186-
var expected = new List<List<int>> { new List<int> { 4, 5, 6 }, new List<int>(), new List<int> { 3 }, new List<int> { 1, 2 } };
185+
List<List<int>> list = [[1, 2], [3], [], [4, 5, 6]];
186+
List<List<int>> expected = [[4, 5, 6], [], [3], [1, 2]];
187187
Assert.Equal(expected, ListOps.Reverse(list));
188188
}
189189
}

0 commit comments

Comments
 (0)