make OnlineStat marshal nicely to json

This commit is contained in:
sm
2016-07-31 22:36:22 -07:00
parent c782f99f05
commit 3e9c1a9df6
2 changed files with 38 additions and 0 deletions
+18
View File
@@ -1,6 +1,8 @@
package ostat
import (
"bytes"
"encoding/json"
"fmt"
"math"
"testing"
@@ -279,3 +281,19 @@ func TestString(t *testing.T) {
t.Errorf("failed to generate correct string")
}
}
func TestJson(t *testing.T) {
ps := NewPopulationStat()
for _, v := range []float64{1, 1, 2, 2} {
ps.Push(v)
}
expected := "{\"n\":4,\"min\":1,\"max\":2,\"mean\":1.5,\"variance\":0.25,\"std_dev\":0.5}\n"
buf := &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(ps); err != nil {
t.Fatalf("encoding problem:", err)
}
if got, want := expected, buf.String(); got != want {
t.Fatalf("wrong json encoding; got %q, want %q", got, want)
}
}