-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution_test.go
62 lines (54 loc) · 1.19 KB
/
Solution_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
package Solution
import (
"fmt"
"reflect"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// Solution func Info
type SolutionFuncType func(*TreeNode) bool
var SolutionFuncList = []SolutionFuncType{
isValidBST_1,
isValidBST_2,
}
// Test case info struct
type Case struct {
name string
input *TreeNode
expect bool
}
// Test case
var cases = []Case{
{name: "TestCase 1", input: &TreeNode{Val: 2, Left: &TreeNode{Val: 1}, Right: &TreeNode{Val: 3}}, expect: true},
{
name: "TestCase 2",
input: &TreeNode{
Val: 3,
Left: &TreeNode{
Val: 1,
Left: &TreeNode{Val: 0},
Right: &TreeNode{Val: 2},
},
Right: &TreeNode{
Val: 5,
Left: &TreeNode{Val: 4},
Right: &TreeNode{Val: 6}},
},
expect: true},
}
// TestSolution Run test case for all solutions
func TestSolution(t *testing.T) {
ast := assert.New(t)
for _, f := range SolutionFuncList {
funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), ".")[1]
for _, c := range cases {
t.Run(fmt.Sprintf("%s %s", funcName, c.name), func(t *testing.T) {
got := f(c.input)
ast.Equal(c.expect, got,
"func: %v case: %v ", funcName, c.name)
})
}
}
}