|
1 | 1 | package problem001
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | "github.com/stretchr/testify/assert"
|
7 | 8 | )
|
8 | 9 |
|
| 10 | +type TestCase struct { |
| 11 | + nums []int |
| 12 | + target int |
| 13 | + out []int |
| 14 | +} |
| 15 | + |
9 | 16 | func TestTwoSum(t *testing.T) {
|
10 | 17 | a := assert.New(t)
|
11 | 18 |
|
12 |
| - a.ElementsMatch([]int{0, 1}, twoSum([]int{2, 7, 11, 15}, 9)) |
13 |
| - a.ElementsMatch([]int{1, 2}, twoSum([]int{3, 2, 4}, 6)) |
14 |
| - a.ElementsMatch([]int{0, 1}, twoSum([]int{3, 3}, 6)) |
15 |
| - a.ElementsMatch([]int{1, 2}, twoSum([]int{1, 2, 3, 4}, 5)) |
16 |
| - a.ElementsMatch([]int{2, 4}, twoSum([]int{1, 1, 2, 3, 4}, 6)) |
17 |
| - a.ElementsMatch([]int{1, 2}, twoSum([]int{-1, -2, -3, -4}, -5)) |
18 |
| - a.ElementsMatch([]int{0, 1}, twoSum([]int{0, 0, 0}, 0)) |
19 |
| - a.ElementsMatch([]int{}, twoSum([]int{1, 2, 3, 4}, 10)) |
| 19 | + testCases := []TestCase{ |
| 20 | + {nums: []int{2, 7, 11, 15}, target: 9, out: []int{0, 1}}, |
| 21 | + {nums: []int{3, 2, 4}, target: 6, out: []int{1, 2}}, |
| 22 | + {nums: []int{3, 3}, target: 6, out: []int{0, 1}}, |
| 23 | + {nums: []int{1, 2, 3, 4}, target: 5, out: []int{1, 2}}, |
| 24 | + {nums: []int{1, 1, 2, 3, 4}, target: 6, out: []int{2, 4}}, |
| 25 | + {nums: []int{-1, -2, -3, -4}, target: -5, out: []int{1, 2}}, |
| 26 | + {nums: []int{0, 0, 0}, target: 0, out: []int{0, 1}}, |
| 27 | + {nums: []int{1, 2, 3, 4}, target: 10, out: []int{}}, |
| 28 | + } |
| 29 | + |
| 30 | + for key, tc := range testCases { |
| 31 | + a.ElementsMatch(tc.out, twoSum(tc.nums, tc.target), fmt.Sprintf("TestTwoSum number # %d", key+1)) |
| 32 | + } |
20 | 33 | }
|
21 | 34 |
|
22 | 35 | func BenchmarkTwoSum(b *testing.B) {
|
|
0 commit comments