From 8902c56451e9b58ff940bbe5fec35d5f9c04584a Mon Sep 17 00:00:00 2001 From: Dmitry Chestnykh Date: Fri, 12 Feb 2016 17:43:26 +0100 Subject: [PATCH] Add test for modulo bias Successfully detects bias fixed by PR #9. --- uniuri_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/uniuri_test.go b/uniuri_test.go index c9c3ffb..0e26efe 100644 --- a/uniuri_test.go +++ b/uniuri_test.go @@ -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) + } + } +}