make OnlineStat marshal nicely to json
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user