started the stats module

This commit is contained in:
didier.amyot committed 2014-05-13 20:59:51 -04:00
1 parent 4ff72d3080
commit f6809a20e6
2 files changed
+76

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
package main
import (
"fmt"
)
var _ = fmt.Print
const initialLength int = 1024
const verySmall float64 = -100000
const veryBig float64 = 100000
type StatAggregator struct {
sum float64
sumOfTheSquares float64
n int
histogram []float64
min float64
max float64
}
type Stats struct{}
func NewStatAggregator() *StatAggregator {
return &StatAggregator{0, 0, 0, make([]float64, initialLength), veryBig, verySmall}
}
func (this *StatAggregator) AddStats(dataChannel chan float64, output chan *Stats) {
for datum := range dataChannel {
this.sum += datum
this.sumOfTheSquares += datum * datum
this.n += 1
if datum < this.min {
this.min = datum
}
if datum > this.max {
this.max = datum
}
}
output <- nil
}
+35
View File
@@ -0,0 +1,35 @@
package main
import (
"testing"
)
func TestNewStats(t *testing.T) {
s := NewStatAggregator()
if s.n != 0 ||
s.sum != 0 ||
s.sumOfTheSquares != 0 ||
len(s.histogram) != initialLength ||
s.min != veryBig ||
s.max != verySmall {
t.Fail()
}
}
func TestAddStatsBasic(t *testing.T) {
numbers := []float64{-1, 1, 20, 3}
c := make(chan float64)
o := make(chan *Stats)
s := NewStatAggregator()
go s.AddStats(c, o)
for _, n := range numbers {
c <- n
}
close(c)
<-o
if s.n != 4. || s.min != -1. || s.max != 20. || s.sum != 23. || s.sumOfTheSquares != 411. {
t.Error(">>>", s.max, s.min)
}
}