diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2019-03-13 22:11:38 -0400 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2019-03-13 22:11:38 -0400 |
| commit | 9ebe8a048bb57e7b85fdabb7cbbe7957ae33af65 (patch) | |
| tree | 518f9e99ea41a236e2decf1853b132925775ec62 | |
| parent | a2245a9a1653dc25e899ea8b090d2c6be48b00a3 (diff) | |
| download | stats-9ebe8a048bb57e7b85fdabb7cbbe7957ae33af65.tar.gz stats-9ebe8a048bb57e7b85fdabb7cbbe7957ae33af65.tar.xz | |
Most tests work.
| -rw-r--r-- | stats_test.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/stats_test.go b/stats_test.go new file mode 100644 index 0000000..3340b77 --- /dev/null +++ b/stats_test.go @@ -0,0 +1,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) + } + }) + } + } + +} |
