-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_test.go
More file actions
99 lines (96 loc) · 1.54 KB
/
sum_test.go
File metadata and controls
99 lines (96 loc) · 1.54 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package utils
import "testing"
func TestSumInt(t *testing.T) {
type args[T Number] struct {
list []T
}
type testCase[T Number] struct {
name string
args args[T]
want T
}
tests := []testCase[int]{
{
name: "empty",
args: struct {
list []int
}{list: make([]int, 0)},
want: 0,
},
{
name: "1",
args: struct {
list []int
}{list: []int{1}},
want: 1,
},
{
name: "2",
args: struct {
list []int
}{list: []int{1, 2}},
want: 3,
},
{
name: "3",
args: struct {
list []int
}{list: []int{1, 2, 3}},
want: 6,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Sum(tt.args.list); got != tt.want {
t.Errorf("Sum() = %v, want %v", got, tt.want)
}
})
}
}
func TestSumFloat64(t *testing.T) {
type args[T Number] struct {
list []T
}
type testCase[T Number] struct {
name string
args args[T]
want T
}
tests := []testCase[float64]{
{
name: "empty",
args: struct {
list []float64
}{list: make([]float64, 0)},
want: 0,
},
{
name: "1",
args: struct {
list []float64
}{list: []float64{1.0}},
want: 1.0,
},
{
name: "2",
args: struct {
list []float64
}{list: []float64{1.0, 2.0}},
want: 3.0,
},
{
name: "3",
args: struct {
list []float64
}{list: []float64{1.0, 2.0, 3.5}},
want: 6.5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Sum(tt.args.list); got != tt.want {
t.Errorf("Sum() = %v, want %v", got, tt.want)
}
})
}
}