Merge branch 'morhekil-master'

This commit is contained in:
Dmitry Chestnykh 2014-08-09 15:58:23 +02:00
commit 7dd3037eec
2 changed files with 26 additions and 8 deletions

View File

@ -56,7 +56,7 @@ func NewLenChars(length int, chars []byte) string {
panic("error reading from random source: " + err.Error()) panic("error reading from random source: " + err.Error())
} }
for _, c := range r { for _, c := range r {
if c >= maxrb { if c > maxrb {
// Skip this number to avoid modulo bias. // Skip this number to avoid modulo bias.
continue continue
} }

View File

@ -2,13 +2,7 @@ package uniuri
import "testing" import "testing"
func TestNew(t *testing.T) { func validateChars(t *testing.T, u string, chars []byte) {
u := New()
// Check length
if len(u) != StdLen {
t.Fatalf("wrong length: expected %d, got %d", StdLen, len(u))
}
// Check that only allowed characters are present
for _, c := range u { for _, c := range u {
var present bool var present bool
for _, a := range StdChars { for _, a := range StdChars {
@ -20,6 +14,17 @@ func TestNew(t *testing.T) {
t.Fatalf("chars not allowed in %q", u) t.Fatalf("chars not allowed in %q", u)
} }
} }
}
func TestNew(t *testing.T) {
u := New()
// Check length
if len(u) != StdLen {
t.Fatalf("wrong length: expected %d, got %d", StdLen, len(u))
}
// Check that only allowed characters are present
validateChars(t, u, StdChars)
// Generate 1000 uniuris and check that they are unique // Generate 1000 uniuris and check that they are unique
uris := make([]string, 1000) uris := make([]string, 1000)
for i, _ := range uris { for i, _ := range uris {
@ -33,3 +38,16 @@ func TestNew(t *testing.T) {
} }
} }
} }
func TestNewLenChars(t *testing.T) {
length := 10
chars := []byte("01234567")
u := NewLenChars(length, chars)
// Check length
if len(u) != length {
t.Fatalf("wrong length: expected %d, got %d", StdLen, len(u))
}
// Check that only allowed characters are present
validateChars(t, u, chars)
}