package stats import ( "fmt" "math" "reflect" "testing" ) var ( datasets [][]float64 = [][]float64{ // set 0 { 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, }, // set 1 { 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}, // Population kurtosis "Kurtosis": {0.14431032604193028, -0.8979764294956452}, "Variance": {65.0314538961039, 6.59910463333333}, // Population skewness "Skewness": {-0.6233147473136348, -0.2757248239748126}, "NumValues": {22, 21}, "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) } }) } } }