This commit is contained in:
xing 2022-11-06 12:12:57 +08:00
parent e453dd996e
commit f648fd6765
2 changed files with 32 additions and 0 deletions

View File

@ -309,6 +309,14 @@ func Max[T IntNumber | ~float64 | ~float32](a ...T) T {
return max return max
} }
func Sum[T IntNumber | ~float64 | ~float32](a ...T) T {
s := T(0)
for _, t := range a {
s += t
}
return s
}
func SliceChunk[T any](arr []T, size int) [][]T { func SliceChunk[T any](arr []T, size int) [][]T {
var r [][]T var r [][]T
i := 0 i := 0

View File

@ -744,3 +744,27 @@ func TestSliceChunk(t *testing.T) {
}) })
} }
} }
func TestSum(t *testing.T) {
type args struct {
a []int
}
tests := []struct {
name string
args args
want int
}{
{
name: "t1",
args: args{a: RangeSlice(1, 10, 1)},
want: 55,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Sum(tt.args.a...); got != tt.want {
t.Errorf("Sum() = %v, want %v", got, tt.want)
}
})
}
}