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 stats
import (
"fmt"
"math"
"reflect"
"testing"
)
var (
datasets [][]float64 = [][]float64{
{
22.90, 28.55, 36.33, 33.71, 26.29, 34.64, 30.14, 34.31,
25.69, 30.09, 28.69, 11.61, 10.68, 24.70, 22.83, 38.08,
24.22, 41.08, 29.70, 26.01, 38.47, 40.25,
},
{
15.054, 19.106, 16.926, 16.243, 20.345, 18.615, 15.050,
18.716, 13.691, 18.296, 16.816, 21.415, 20.756, 13.696,
22.679, 16.277, 18.738, 20.271, 20.967, 19.157, 20.338,
},
}
expectedMethodResults map[string][]float64 = map[string][]float64{
"Mean": {29.044091, 18.2453333333333},
"StandardDeviation": {8.06420820019572, 2.56887224932135},
"Kurtosis": {0.515004967048032, -0.804414119818956},
"Variance": {65.0314538961039, 6.59910463333333},
"Skewness": {-0.669882132708844, -0.297404073286609},
"NumValues": {22, 21},
}
expectedFieldValues map[string][]float64 = map[string][]float64{
"Min": {10.68, 13.691},
"Max": {41.08, 22.679},
}
)
func TestStats(t *testing.T) {
for i, set := range datasets {
s := &Stats{}
for _, n := range set {
s.Push(n)
}
for method, values := range expectedMethodResults {
t.Run(fmt.Sprintf("%s for set %d", method, i), func(t *testing.T) {
m := reflect.ValueOf(s).MethodByName(method).Call([]reflect.Value{})
// Round to three decimal places
got := math.Round(m[0].Float()*1000) / 1000
expected := math.Round(values[i]*1000) / 1000
if got != expected {
t.Errorf("Method %s failure: \n"+
"\tOn set: %d\n"+
"\tExpected: %f\n"+
"\tGot: %f\n",
method, i, expected, got)
}
})
}
for field, values := range expectedFieldValues {
t.Run(fmt.Sprintf("%s for set %d", field, i), func(t *testing.T) {
m := reflect.ValueOf(s).FieldByName(field)
fmt.Println(m)
// Round to three decimal places
//got := math.Round(m.Float()*1000) / 1000
got := 0.00000
expected := math.Round(values[i]*1000) / 1000
if got != expected {
t.Errorf("Field %s failure: \n"+
"\tOn set: %d\n"+
"\tExpected: %f\n"+
"\tGot: %f\n",
field, i, expected, got)
}
})
}
}
}
|