1 package funk
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestReduce(t *testing.T) {
11 testCases := []struct {
12 Arr interface{}
13 Func interface{}
14 Acc interface{}
15 Result interface{}
16 }{
17 {
18 []int{1, 2, 3, 4},
19 func(acc, elem float64) float64 { return acc + elem },
20 0,
21 float64(10),
22 },
23 {
24 &[]int16{1, 2, 3, 4},
25 '+',
26 5,
27 int16(15),
28 },
29 {
30 []float64{1.1, 2.2, 3.3},
31 '+',
32 0,
33 float64(6.6),
34 },
35 {
36 &[]int{1, 2, 3, 5},
37 func(acc int8, elem int16) int32 { return int32(acc) * int32(elem) },
38 1,
39 int32(30),
40 },
41 {
42 []interface{}{1, 2, 3.3, 4},
43 '*',
44 1,
45 float64(26.4),
46 },
47 {
48 []string{"1", "2", "3", "4"},
49 func(acc string, elem string) string { return acc + elem },
50 "",
51 "1234",
52 },
53 }
54
55 for idx, test := range testCases {
56 t.Run(fmt.Sprintf("test case #%d", idx+1), func(t *testing.T) {
57 is := assert.New(t)
58 result := Reduce(test.Arr, test.Func, test.Acc)
59 if !is.Equal(result, test.Result) {
60 t.Errorf("%#v doesn't eqal to %#v", result, test.Result)
61 }
62 })
63 }
64 }
65
View as plain text