[#14] Added tests for sampler
Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
parent
56ef32f21e
commit
eeeaa8a4fd
1 changed files with 40 additions and 0 deletions
40
pkg/pool/sampler_test.go
Normal file
40
pkg/pool/sampler_test.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package pool
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSamplerStability(t *testing.T) {
|
||||
const COUNT = 100000
|
||||
|
||||
cases := []struct {
|
||||
probabilities []float64
|
||||
expected []int
|
||||
}{
|
||||
{
|
||||
probabilities: []float64{1, 0},
|
||||
expected: []int{COUNT, 0},
|
||||
},
|
||||
{
|
||||
probabilities: []float64{0.1, 0.2, 0.7},
|
||||
expected: []int{10138, 19813, 70049},
|
||||
},
|
||||
{
|
||||
probabilities: []float64{0.2, 0.2, 0.4, 0.1, 0.1, 0},
|
||||
expected: []int{19824, 20169, 39900, 10243, 9864, 0},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
sampler := NewSampler(tc.probabilities, rand.NewSource(0))
|
||||
res := make([]int, len(tc.probabilities))
|
||||
for i := 0; i < COUNT; i++ {
|
||||
res[sampler.Next()]++
|
||||
}
|
||||
|
||||
require.Equal(t, tc.expected, res, "probabilities: %v", tc.probabilities)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue