add testcase

This commit is contained in:
YDZ
2020-08-14 07:30:15 +08:00
parent 6cb397f002
commit d737763a75

View File

@ -5,30 +5,61 @@ import (
"testing" "testing"
) )
func TestTwoSum(t *testing.T) { type question1 struct {
tests := [][]int{ para1
[]int{3, 2, 4}, ans1
[]int{3, 2, 4},
[]int{0, 8, 7, 3, 3, 4, 2},
[]int{0, 1},
} }
targets := []int{
6, // para 是参数
5, // one 代表第一个参数
11, type para1 struct {
1, nums []int
target int
} }
results := [][]int{
[]int{1, 2}, // ans 是答案
[]int{0, 1}, // one 代表第一个答案
[]int{1, 3}, type ans1 struct {
[]int{0, 1}, one []int
} }
func Test_Problem1(t *testing.T) {
qs := []question1{
question1{
para1{[]int{3, 2, 4}, 6},
ans1{[]int{1, 2}},
},
question1{
para1{[]int{3, 2, 4}, 5},
ans1{[]int{0, 1}},
},
question1{
para1{[]int{0, 8, 7, 3, 3, 4, 2}, 11},
ans1{[]int{1, 3}},
},
question1{
para1{[]int{0, 1}, 1},
ans1{[]int{0, 1}},
},
question1{
para1{[]int{0, 3}, 5},
ans1{[]int{}},
},
// 如需多个测试,可以复制上方元素。
}
fmt.Printf("------------------------Leetcode Problem 1------------------------\n") fmt.Printf("------------------------Leetcode Problem 1------------------------\n")
for i := 0; i < len(targets); i++ {
fmt.Printf("nums = %v target = %v result = %v\n", tests[i], targets[i], twoSum(tests[i], targets[i])) for _, q := range qs {
if ret := twoSum(tests[i], targets[i]); ret[0] != results[i][0] && ret[1] != results[i][1] { _, p := q.ans1, q.para1
t.Fatalf("case %d fails: %v\n", i, ret) fmt.Printf("【input】:%v 【output】:%v\n", p, twoSum(p.nums, p.target))
}
} }
fmt.Printf("\n\n\n")
} }