-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
63 lines (53 loc) · 1.14 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"testing"
"golang.org/x/tour/tree"
)
func TestWalk(t *testing.T) {
k := 3
max := 10 // max random value generated by tree.New()
tr := tree.New(k)
ch := make(chan int, max)
Walk(tr, ch)
if cap(ch) != max {
t.Errorf("channel's capacity shoud be %d but is %d\n", max, cap(ch))
t.FailNow()
}
for i := 0; i < max; i++ {
e := (i + 1) * k
if v := <-ch; v != e {
t.Errorf("channel's value %d is the expected value %d\n", v, e)
t.FailNow()
}
}
}
func TestSame(t *testing.T) {
t1 := tree.New(1)
t2 := tree.New(1)
t3 := &tree.Tree{ // one node more
Left: t2,
Value: 12,
Right: nil,
}
tt := []struct {
name string
trees []*tree.Tree
result bool
}{
{"t1 equals t2", []*tree.Tree{t1, t2}, true},
{"t1 equals t1", []*tree.Tree{t1, t1}, true},
{"t1 is not equal t3", []*tree.Tree{t1, t3}, false},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
status := "equivalent"
if !tc.result {
status = "different"
}
if Same(tc.trees[0], tc.trees[1]) != tc.result {
t.Errorf("%v\nand\n%v\nshould be %v\n", tc.trees[0], tc.trees[1], status)
t.FailNow()
}
})
}
}