From 21f8a0983b7790c240c11a008aabce069bcde897 Mon Sep 17 00:00:00 2001 From: Jonathan J Lawlor Date: Thu, 30 Oct 2014 21:41:18 -0400 Subject: [PATCH] add tests for HarmonicMean Added tests for harmonic mean, specifically when the weights are nil or the possible panic. --- stat_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/stat_test.go b/stat_test.go index aa2d0da7..8fbb4c29 100644 --- a/stat_test.go +++ b/stat_test.go @@ -306,6 +306,27 @@ func ExampleHarmonicMean() { // The arithmetic mean is 10.16667, but the harmonic mean is 6.8354. } +func TestHarmonicMean(t *testing.T) { + for i, test := range []struct { + x []float64 + wts []float64 + ans float64 + }{ + { + x: []float64{.5, .125}, + ans: .2, + }, + } { + c := HarmonicMean(test.x, test.wts) + if math.Abs(c-test.ans) > 1e-14 { + t.Errorf("Harmonic mean mismatch case %d: Expected %v, Found %v", i, test.ans, c) + } + } + if !Panics(func() { HarmonicMean(make([]float64, 3), make([]float64, 2)) }) { + t.Errorf("HarmonicMean did not panic with x, wts length mismatch") + } +} + func TestHistogram(t *testing.T) { for i, test := range []struct { x []float64