Skip to content

Commit 5f58998

Browse files
binary-search-tree: add generator
1 parent 355cb81 commit 5f58998

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{{ func assertions(node, path)
2+
checks = [$"Assert.Equal({string.to_int node.data}, tree.{path}Value);"]
3+
4+
if node.left
5+
checks = array.add_range checks (assertions node.left (path + "Left."))
6+
end
7+
8+
if node.right
9+
checks = array.add_range checks (assertions node.right (path + "Right."))
10+
end
11+
12+
ret checks
13+
end }}
14+
15+
using System.Linq;
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.input.treeData | array.size == 1 }}
25+
var tree = new {{ testedClass }}({{ test.input.treeData[0] | @string.to_int }});
26+
{{- else -}}
27+
var tree = new {{ testedClass }}(new[] { {{ test.input.treeData | array.each @string.to_int | array.join "," }} });
28+
{{- end -}}
29+
{{- if test.property == "data" }}
30+
{{ test.expected | assertions "" | array.join "\n" }}
31+
{{- else }}
32+
int[] expected = {{ test.expected | array.each @string.to_int }};
33+
Assert.Equal(expected, tree.AsEnumerable());
34+
{{ end -}}
35+
}
36+
{{ end -}}
37+
}

exercises/practice/binary-search-tree/BinarySearchTreeTests.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ public void Data_is_retained()
1111
}
1212

1313
[Fact(Skip = "Remove this Skip property to run this test")]
14-
public void Smaller_number_at_left_node()
14+
public void Insert_data_at_proper_node_smaller_number_at_left_node()
1515
{
1616
var tree = new BinarySearchTree(new[] { 4, 2 });
1717
Assert.Equal(4, tree.Value);
1818
Assert.Equal(2, tree.Left.Value);
1919
}
2020

2121
[Fact(Skip = "Remove this Skip property to run this test")]
22-
public void Same_number_at_left_node()
22+
public void Insert_data_at_proper_node_same_number_at_left_node()
2323
{
2424
var tree = new BinarySearchTree(new[] { 4, 4 });
2525
Assert.Equal(4, tree.Value);
2626
Assert.Equal(4, tree.Left.Value);
2727
}
2828

2929
[Fact(Skip = "Remove this Skip property to run this test")]
30-
public void Greater_number_at_right_node()
30+
public void Insert_data_at_proper_node_greater_number_at_right_node()
3131
{
3232
var tree = new BinarySearchTree(new[] { 4, 5 });
3333
Assert.Equal(4, tree.Value);
@@ -48,37 +48,42 @@ public void Can_create_complex_tree()
4848
}
4949

5050
[Fact(Skip = "Remove this Skip property to run this test")]
51-
public void Can_sort_single_number()
51+
public void Can_sort_data_can_sort_single_number()
5252
{
5353
var tree = new BinarySearchTree(2);
54-
Assert.Equal(new[] { 2 }, tree.AsEnumerable());
54+
int[] expected = [2];
55+
Assert.Equal(expected, tree.AsEnumerable());
5556
}
5657

5758
[Fact(Skip = "Remove this Skip property to run this test")]
58-
public void Can_sort_if_second_number_is_smaller_than_first()
59+
public void Can_sort_data_can_sort_if_second_number_is_smaller_than_first()
5960
{
6061
var tree = new BinarySearchTree(new[] { 2, 1 });
61-
Assert.Equal(new[] { 1, 2 }, tree.AsEnumerable());
62+
int[] expected = [1, 2];
63+
Assert.Equal(expected, tree.AsEnumerable());
6264
}
6365

6466
[Fact(Skip = "Remove this Skip property to run this test")]
65-
public void Can_sort_if_second_number_is_same_as_first()
67+
public void Can_sort_data_can_sort_if_second_number_is_same_as_first()
6668
{
6769
var tree = new BinarySearchTree(new[] { 2, 2 });
68-
Assert.Equal(new[] { 2, 2 }, tree.AsEnumerable());
70+
int[] expected = [2, 2];
71+
Assert.Equal(expected, tree.AsEnumerable());
6972
}
7073

7174
[Fact(Skip = "Remove this Skip property to run this test")]
72-
public void Can_sort_if_second_number_is_greater_than_first()
75+
public void Can_sort_data_can_sort_if_second_number_is_greater_than_first()
7376
{
7477
var tree = new BinarySearchTree(new[] { 2, 3 });
75-
Assert.Equal(new[] { 2, 3 }, tree.AsEnumerable());
78+
int[] expected = [2, 3];
79+
Assert.Equal(expected, tree.AsEnumerable());
7680
}
7781

7882
[Fact(Skip = "Remove this Skip property to run this test")]
79-
public void Can_sort_complex_tree()
83+
public void Can_sort_data_can_sort_complex_tree()
8084
{
8185
var tree = new BinarySearchTree(new[] { 2, 1, 3, 6, 7, 5 });
82-
Assert.Equal(new[] { 1, 2, 3, 5, 6, 7 }, tree.AsEnumerable());
86+
int[] expected = [1, 2, 3, 5, 6, 7];
87+
Assert.Equal(expected, tree.AsEnumerable());
8388
}
8489
}

0 commit comments

Comments
 (0)