Add base Percentile interface and tests

This commit is contained in:
Justin Li
2014-07-22 02:41:39 -04:00
parent 8d8c1fba62
commit 2f4d0b0f9a
2 changed files with 75 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
package stats
import (
"testing"
"math/rand"
)
func TestPercentiles(t *testing.T) {
testInRange(t, 1, 0.5)
testInRange(t, 1, 0.9)
testInRange(t, 1, 0.95)
testInRange(t, 10000, 0.5)
testInRange(t, 10000, 0.9)
testInRange(t, 10000, 0.95)
}
func testInRange(t *testing.T, max, percentile float64) {
p := NewPercentile(percentile, 10)
for i := 0; i < 1000; i++ {
p.AddSample(rand.Float64() * max)
}
got := p.Value()
expected := percentile * max
if got < expected * (1 - 0.02) || got > expected * (1 + 0.02) {
t.Errorf("Percentile out of range\n actual: %f\nexpected: %f", got, expected)
}
}