Add test for modulo bias

Successfully detects bias fixed by PR #9.
This commit is contained in:
Dmitry Chestnykh 2016-02-12 17:43:26 +01:00
parent 53057c437f
commit 8902c56451

View File

@ -83,3 +83,20 @@ func TestNewLenCharsMaxLength(t *testing.T) {
chars := make([]byte, 257)
NewLenChars(32, chars)
}
func TestBias(t *testing.T) {
chars := []byte("abcdefghijklmnopqrstuvwxyz")
slen := 100000
s := NewLenChars(slen, chars)
counts := make(map[rune]int)
for _, b := range s {
counts[b]++
}
avg := float64(slen) / float64(len(chars))
for k, n := range counts {
diff := float64(n) / avg
if diff < 0.95 || diff > 1.05 {
t.Errorf("Bias on '%c': expected average %f, got %d", k, avg, n)
}
}
}