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}, // para 是参数
} // one 代表第一个参数
targets := []int{ type para1 struct {
6, nums []int
5, target int
11, }
1,
} // ans 是答案
results := [][]int{ // one 代表第一个答案
[]int{1, 2}, type ans1 struct {
[]int{0, 1}, one []int
[]int{1, 3}, }
[]int{0, 1},
} func Test_Problem1(t *testing.T) {
fmt.Printf("------------------------Leetcode Problem 1------------------------\n")
for i := 0; i < len(targets); i++ { qs := []question1{
fmt.Printf("nums = %v target = %v result = %v\n", tests[i], targets[i], twoSum(tests[i], targets[i]))
if ret := twoSum(tests[i], targets[i]); ret[0] != results[i][0] && ret[1] != results[i][1] { question1{
t.Fatalf("case %d fails: %v\n", i, ret) 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")
for _, q := range qs {
_, p := q.ans1, q.para1
fmt.Printf("【input】:%v 【output】:%v\n", p, twoSum(p.nums, p.target))
}
fmt.Printf("\n\n\n")
} }