diff --git a/simple/simple_test.go b/simple/simple_test.go new file mode 100644 index 0000000..4ea9592 --- /dev/null +++ b/simple/simple_test.go @@ -0,0 +1,41 @@ +// Copyright 2019, LightStep Inc. + +package simple_test + +import ( + "math/rand" + "testing" + + "github.com/lightstep/varopt/simple" + "github.com/stretchr/testify/require" +) + +type iRec int + +func TestSimple(t *testing.T) { + const ( + popSize = 1e6 + sampleProb = 0.1 + sampleSize int = popSize * sampleProb + epsilon = 0.01 + ) + + rnd := rand.New(rand.NewSource(17167)) + + ss := simple.New(sampleSize, rnd) + + psum := 0. + for i := 0; i < popSize; i++ { + ss.Add(iRec(i)) + psum += float64(i) + } + + require.Equal(t, ss.Size(), sampleSize) + + ssum := 0.0 + for i := 0; i < sampleSize; i++ { + ssum += float64(ss.Get(i).(iRec)) + } + + require.InEpsilon(t, ssum/float64(ss.Size()), psum/popSize, epsilon) +} diff --git a/varopt.go b/varopt.go index 1856614..5ab1ee5 100644 --- a/varopt.go +++ b/varopt.go @@ -171,7 +171,7 @@ func (s *Varopt) TotalCount() int { } // Tau returns the current large-weight threshold. Weights larger -// than Tau() carry their exact weight int he sample. See the VarOpt +// than Tau() carry their exact weight in the sample. See the VarOpt // paper for details. func (s *Varopt) Tau() float64 { return s.tau