-
Notifications
You must be signed in to change notification settings - Fork 0
/
avg_test.go
50 lines (42 loc) · 1005 Bytes
/
avg_test.go
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
package functools
import "testing"
func TestAvg(t *testing.T) {
cases := []struct {
in interface{}
want float64
}{
{[]int{1, 2, 3}, 2.0},
{[]float32{2.3, 2.3, 2.3, 2.3}, 2.299999952316284},
{[]float64{2.3, 2.3, 2.3, 2.3}, 2.3},
{[]int8{1, 2, 3, 4, 5}, 3.0},
}
for _, c := range cases {
got := Avg(c.in)
if got != c.want {
t.Errorf("Avg(%v) == %.4f want %.4f", c.in, got, c.want)
}
}
}
func TestAvgSafe(t *testing.T) {
cases := []struct {
in interface{}
want float64
e bool
}{
{[]int{1, 2, 3}, 2.0, false},
{[]float32{2.3, 2.3, 2.3, 2.3}, 2.299999952316284, false},
{[]float64{2.3, 2.3, 2.3, 2.3}, 2.3, false},
{[]int8{1, 2, 3, 4, 5}, 3.0, false},
{[]int{}, 0.0, true},
{[]string{"a", "b", "c"}, 0.0, true},
}
for _, c := range cases {
got, err := AvgSafe(c.in)
if err != nil && !c.e {
t.Errorf("AvgSafe(%v) raised %v", c.in, err)
}
if err == nil && got != c.want {
t.Errorf("AvgSafe(%v) == %.4f want %.4f", c.in, got, c.want)
}
}
}