aboutsummaryrefslogtreecommitdiff
path: root/stats_test.go
blob: 3340b77db787ba4302e38d94c0aad75dabe06b3f (plain) (blame)
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
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,
		},
	}
	expectedResults 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},
	}
)

func TestStats(t *testing.T) {
	for i, set := range datasets {
		s := &Stats{}
		for _, n := range set {
			s.Push(n)
		}
		// rs := reflect.ValueOf(&s)
		for method, values := range expectedResults {
			t.Run(fmt.Sprintf("%s for set %d", method, i), func(t *testing.T) {
				// m := rs.MethodByName(method).Call([]reflect.Value{})
				// got := reflect.ValueOf(&s).MethodByName(method).Call([]reflect.Value{})
				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)
				}
			})
		}
	}

}