Merge pull request #8 from StableLib/zerolength

Fix hanging when requested zero-length string.
This commit is contained in:
Dmitry Chestnykh 2015-01-21 19:32:26 +01:00
commit 45d4bb4e8f
2 changed files with 12 additions and 0 deletions

View File

@ -53,6 +53,9 @@ func NewLen(length int) string {
// NewLenChars returns a new random string of the provided length, consisting // NewLenChars returns a new random string of the provided length, consisting
// of the provided byte slice of allowed characters (maximum 256). // of the provided byte slice of allowed characters (maximum 256).
func NewLenChars(length int, chars []byte) string { func NewLenChars(length int, chars []byte) string {
if length == 0 {
return ""
}
b := make([]byte, length) b := make([]byte, length)
r := make([]byte, length+(length/4)) // storage for random bytes. r := make([]byte, length+(length/4)) // storage for random bytes.
clen := len(chars) clen := len(chars)

View File

@ -46,6 +46,15 @@ func TestNew(t *testing.T) {
} }
} }
func TestNewLen(t *testing.T) {
for i := 0; i < 100; i++ {
u := NewLen(i)
if len(u) != i {
t.Fatalf("request length %d, got %d", i, len(u))
}
}
}
func TestNewLenChars(t *testing.T) { func TestNewLenChars(t *testing.T) {
length := 10 length := 10
chars := []byte("01234567") chars := []byte("01234567")