Skip to content

Commit 93867e5

Browse files
committed
Re-organize test cases
1 parent a05c568 commit 93867e5

File tree

1 file changed

+105
-25
lines changed

1 file changed

+105
-25
lines changed

binarytree_test.go

Lines changed: 105 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"gopkg.in/dnaeon/go-binarytree.v1"
1010
)
1111

12-
func TestBinaryTree(t *testing.T) {
13-
// Construct the following simple binary tree
12+
func TestHeightAndSize(t *testing.T) {
13+
// Our test tree
1414
//
1515
// __1
1616
// / \
@@ -39,54 +39,134 @@ func TestBinaryTree(t *testing.T) {
3939
if five.Height() != 0 {
4040
t.Fatal("expected height from node (5) should be 0")
4141
}
42+
}
43+
44+
func TestIsLeaf(t *testing.T) {
45+
// Our test tree
46+
//
47+
// __1
48+
// / \
49+
// 2 3
50+
// / \
51+
// 4 5
52+
//
53+
root := binarytree.NewNode(1)
54+
two := root.InsertLeft(2)
55+
three := root.InsertRight(3)
56+
four := two.InsertLeft(4)
57+
five := two.InsertRight(5)
4258

4359
if root.IsLeaf() {
4460
t.Fatal("root node should not be a leaf")
4561
}
4662

63+
if two.IsLeaf() {
64+
t.Fatal("node (2) should not be a leaf")
65+
}
66+
67+
if !three.IsLeaf() {
68+
t.Fatal("node (3) should be a leaf")
69+
}
70+
71+
if !four.IsLeaf() {
72+
t.Fatal("node (4) should not be a leaf")
73+
}
74+
4775
if !five.IsLeaf() {
4876
t.Fatal("node (5) should be a leaf")
4977
}
78+
}
5079

51-
collectorFunc := func(values *[]int) binarytree.WalkFunc[int] {
52-
walkFunc := func(node *binarytree.Node[int]) error {
53-
*values = append(*values, node.Value)
54-
return nil
55-
}
80+
func TestWalkInOrder(t *testing.T) {
81+
// Our test tree
82+
//
83+
// __1
84+
// / \
85+
// 2 3
86+
// / \
87+
// 4 5
88+
//
89+
root := binarytree.NewNode(1)
90+
two := root.InsertLeft(2)
91+
root.InsertRight(3)
92+
two.InsertLeft(4)
93+
two.InsertRight(5)
94+
95+
result := make([]int, 0)
96+
wantResult := []int{4, 2, 5, 1, 3}
97+
walkFunc := func(node *binarytree.Node[int]) error {
98+
result = append(result, node.Value)
99+
return nil
100+
}
56101

57-
return walkFunc
102+
if err := root.WalkInOrder(walkFunc); err != nil {
103+
t.Fatal(err)
58104
}
59105

60-
inOrderValues := make([]int, 0)
61-
preOrderValues := make([]int, 0)
62-
postOrderValues := make([]int, 0)
106+
if !reflect.DeepEqual(result, wantResult) {
107+
t.Fatalf("want in-order values %v, got %v", wantResult, result)
108+
}
109+
}
63110

64-
wantInOrderValues := []int{4, 2, 5, 1, 3}
65-
wantPreOrderValues := []int{1, 2, 4, 5, 3}
66-
wantPostOrderValues := []int{4, 5, 2, 3, 1}
111+
func TestWalkPreOrder(t *testing.T) {
112+
// Our test tree
113+
//
114+
// __1
115+
// / \
116+
// 2 3
117+
// / \
118+
// 4 5
119+
//
120+
root := binarytree.NewNode(1)
121+
two := root.InsertLeft(2)
122+
root.InsertRight(3)
123+
two.InsertLeft(4)
124+
two.InsertRight(5)
67125

68-
if err := root.WalkInOrder(collectorFunc(&inOrderValues)); err != nil {
69-
t.Fatal(err)
126+
result := make([]int, 0)
127+
wantResult := []int{1, 2, 4, 5, 3}
128+
walkFunc := func(node *binarytree.Node[int]) error {
129+
result = append(result, node.Value)
130+
return nil
70131
}
71132

72-
if err := root.WalkPreOrder(collectorFunc(&preOrderValues)); err != nil {
133+
if err := root.WalkPreOrder(walkFunc); err != nil {
73134
t.Fatal(err)
74135
}
75136

76-
if err := root.WalkPostOrder(collectorFunc(&postOrderValues)); err != nil {
77-
t.Fatal(err)
137+
if !reflect.DeepEqual(wantResult, result) {
138+
t.Fatalf("want pre-order values %v, got %v", wantResult, result)
78139
}
140+
}
79141

80-
if !reflect.DeepEqual(inOrderValues, wantInOrderValues) {
81-
t.Fatalf("want in-order values %v, got %v", wantInOrderValues, inOrderValues)
142+
func TestWalkPostOrder(t *testing.T) {
143+
// Our test tree
144+
//
145+
// __1
146+
// / \
147+
// 2 3
148+
// / \
149+
// 4 5
150+
//
151+
root := binarytree.NewNode(1)
152+
two := root.InsertLeft(2)
153+
root.InsertRight(3)
154+
two.InsertLeft(4)
155+
two.InsertRight(5)
156+
157+
result := make([]int, 0)
158+
wantResult := []int{4, 5, 2, 3, 1}
159+
walkFunc := func(node *binarytree.Node[int]) error {
160+
result = append(result, node.Value)
161+
return nil
82162
}
83163

84-
if !reflect.DeepEqual(preOrderValues, wantPreOrderValues) {
85-
t.Fatalf("want pre-order values %v, got %v", wantPreOrderValues, preOrderValues)
164+
if err := root.WalkPostOrder(walkFunc); err != nil {
165+
t.Fatal(err)
86166
}
87167

88-
if !reflect.DeepEqual(postOrderValues, wantPostOrderValues) {
89-
t.Fatalf("want post-order values %v, got %v", wantPostOrderValues, postOrderValues)
168+
if !reflect.DeepEqual(wantResult, result) {
169+
t.Fatalf("want post-order values %v, got %v", wantResult, result)
90170
}
91171
}
92172

0 commit comments

Comments
 (0)