-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathharmonic_mean_test.go
77 lines (71 loc) · 1.47 KB
/
harmonic_mean_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
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
package statistics
import (
"fmt"
"testing"
)
func ExampleHarmonicMean() {
mean := HarmonicMean(1, 1000)
fmt.Printf("The mean of [1 1000] is %0.3f.\n", mean)
// Output:
// The mean of [1 1000] is 1.998.
}
func TestHarmonicMean(t *testing.T) {
cases := []testArrayCase{
{
name: "no numbers",
input: []uint{},
expected: "NaN",
},
{
name: "negatives",
input: []int{-10, -20},
expected: "-13.333333333333332",
},
{
name: "two numbers",
input: []uint{5, 15},
expected: "7.5",
},
{
name: "zero",
input: []int{-5, 5},
expected: "NaN",
},
{
name: "floats",
input: []float32{2.5, 4.5},
expected: "3.2142857142857144",
},
{
name: "large numbers",
input: []uint64{1, 4294967295, 18446744073709551615},
expected: "2.999999999301508",
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
var actual float64
switch c.input.(type) {
case []uint:
actual = HarmonicMean(c.input.([]uint)...)
case []uint64:
actual = HarmonicMean(c.input.([]uint64)...)
case []int:
actual = HarmonicMean(c.input.([]int)...)
case []float32:
actual = HarmonicMean(c.input.([]float32)...)
}
if fmt.Sprintf("%v", actual) != c.expected {
t.Logf("expected %v, got %v", c.expected, actual)
t.FailNow()
}
})
}
}
func BenchmarkHarmonicMean(b *testing.B) {
for i := 0; i < b.N; i++ {
HarmonicMean(i, i-5, i+3, i-8)
}
}