func (s *S) TestHistogramPercentileBinomialApproximation(c *C) {
	hs := &HistogramSpecification{
		Starts:      EquallySizedBucketsFor(0, 5, 6),
		BucketMaker: TallyingBucketBuilder,
	}

	h := CreateHistogram(hs)

	c.Assert(h, Not(IsNil))

	n := 5
	p := 0.5

	for k := 0; k < 6; k++ {
		limit := 1000000.0 * maths.BinomialPDF(k, n, p)
		for j := 0.0; j < limit; j++ {
			h.Add(float64(k))
		}
	}

	c.Check(h.Percentile(0.0), Equals, 0.0)
	c.Check(h.Percentile(0.03125), Equals, 0.0)
	c.Check(h.Percentile(0.1875), Equals, 1.0)
	c.Check(h.Percentile(0.5), Equals, 2.0)
	c.Check(h.Percentile(0.8125), Equals, 3.0)
	c.Check(h.Percentile(0.96875), Equals, 4.0)
	c.Check(h.Percentile(1.0), Equals, 5.0)
}
func (s *S) TestBucketForPercentileWithBinomialApproximation(c *C) {
	hs := &HistogramSpecification{
		Starts:      EquallySizedBucketsFor(0, 5, 6),
		BucketMaker: TallyingBucketBuilder,
	}

	c.Assert(hs, Not(IsNil))

	h := CreateHistogram(hs)

	c.Assert(h, Not(IsNil))

	n := 5
	p := 0.5

	for k := 0; k < 6; k++ {
		limit := 1000000.0 * maths.BinomialPDF(k, n, p)
		for j := 0.0; j < limit; j++ {
			h.Add(float64(k))
		}
	}

	var bucket *Bucket = nil
	var subindex int = 0

	bucket, subindex = h.bucketForPercentile(0.0)
	c.Assert(*bucket, Not(IsNil))
	c.Check(subindex, Equals, 0)
	c.Check((*bucket).Observations(), Equals, 31250)

	bucket, subindex = h.bucketForPercentile(0.03125)
	c.Assert(*bucket, Not(IsNil))
	c.Check(subindex, Equals, 31249)
	c.Check((*bucket).Observations(), Equals, 31250)

	bucket, subindex = h.bucketForPercentile(0.1875)
	c.Assert(*bucket, Not(IsNil))
	c.Check(subindex, Equals, 156249)
	c.Check((*bucket).Observations(), Equals, 156250)

	bucket, subindex = h.bucketForPercentile(0.50)
	c.Assert(*bucket, Not(IsNil))
	c.Check(subindex, Equals, 312499)
	c.Check((*bucket).Observations(), Equals, 312500)

	bucket, subindex = h.bucketForPercentile(0.8125)
	c.Assert(*bucket, Not(IsNil))
	c.Check(subindex, Equals, 312499)
	c.Check((*bucket).Observations(), Equals, 312500)

	bucket, subindex = h.bucketForPercentile(0.96875)
	c.Assert(*bucket, Not(IsNil))
	c.Check(subindex, Equals, 156249)
	c.Check((*bucket).Observations(), Equals, 156250)

	bucket, subindex = h.bucketForPercentile(1.0)
	c.Assert(*bucket, Not(IsNil))
	c.Check(subindex, Equals, 31249)
	c.Check((*bucket).Observations(), Equals, 31250)
}
func (s *S) TestHistogramMarshallable(c *C) {
	hs := &HistogramSpecification{
		Starts:                EquallySizedBucketsFor(0, 5, 6),
		BucketMaker:           TallyingBucketBuilder,
		ReportablePercentiles: []float64{0.03125, 0.1875, 0.5, 0.8125, 0.96875, 1.0},
	}

	h := CreateHistogram(hs)

	c.Assert(h, Not(IsNil))

	n := 5
	p := 0.5

	for k := 0; k < 6; k++ {
		limit := 1000000.0 * maths.BinomialPDF(k, n, p)
		for j := 0.0; j < limit; j++ {
			h.Add(float64(k))
		}
	}

	m := h.Marshallable()

	c.Assert(m, Not(IsNil))
	c.Check(m, HasLen, 2)
	c.Check(m["type"], Equals, "histogram")

	var v map[string]interface{} = m["value"].(map[string]interface{})

	c.Assert(v, Not(IsNil))

	c.Check(v, HasLen, 6)
	c.Check(v["0.031250"], Equals, "0.000000")
	c.Check(v["0.187500"], Equals, "1.000000")
	c.Check(v["0.500000"], Equals, "2.000000")
	c.Check(v["0.812500"], Equals, "3.000000")
	c.Check(v["0.968750"], Equals, "4.000000")
	c.Check(v["1.000000"], Equals, "5.000000")
}