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
|
package funk
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestReduce(t *testing.T) {
testCases := []struct {
Arr interface{}
Func interface{}
Acc interface{}
Result interface{}
}{
{
[]int{1, 2, 3, 4},
func(acc, elem float64) float64 { return acc + elem },
0,
float64(10),
},
{
&[]int16{1, 2, 3, 4},
'+',
5,
int16(15),
},
{
[]float64{1.1, 2.2, 3.3},
'+',
0,
float64(6.6),
},
{
&[]int{1, 2, 3, 5},
func(acc int8, elem int16) int32 { return int32(acc) * int32(elem) },
1,
int32(30),
},
{
[]interface{}{1, 2, 3.3, 4},
'*',
1,
float64(26.4),
},
{
[]string{"1", "2", "3", "4"},
func(acc string, elem string) string { return acc + elem },
"",
"1234",
},
}
for idx, test := range testCases {
t.Run(fmt.Sprintf("test case #%d", idx+1), func(t *testing.T) {
is := assert.New(t)
result := Reduce(test.Arr, test.Func, test.Acc)
if !is.Equal(result, test.Result) {
t.Errorf("%#v doesn't eqal to %#v", result, test.Result)
}
})
}
}
|