1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/pkg/common/randomid_test.go
Srini Brahmaroutu 53ece336dc moving random.go from utils
Closes #10962
Signed-off-by: Srini Brahmaroutu <srbrahma@us.ibm.com>
2015-02-26 18:31:18 +00:00

59 lines
1.2 KiB
Go

package common
import (
"testing"
)
func TestShortenId(t *testing.T) {
id := GenerateRandomID()
truncID := TruncateID(id)
if len(truncID) != 12 {
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
}
}
func TestShortenIdEmpty(t *testing.T) {
id := ""
truncID := TruncateID(id)
if len(truncID) > len(id) {
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
}
}
func TestShortenIdInvalid(t *testing.T) {
id := "1234"
truncID := TruncateID(id)
if len(truncID) != len(id) {
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
}
}
func TestGenerateRandomID(t *testing.T) {
id := GenerateRandomID()
if len(id) != 64 {
t.Fatalf("Id returned is incorrect: %s", id)
}
}
func TestRandomString(t *testing.T) {
id := RandomString()
if len(id) != 64 {
t.Fatalf("Id returned is incorrect: %s", id)
}
}
func TestRandomStringUniqueness(t *testing.T) {
repeats := 25
set := make(map[string]struct{}, repeats)
for i := 0; i < repeats; i = i + 1 {
id := RandomString()
if len(id) != 64 {
t.Fatalf("Id returned is incorrect: %s", id)
}
if _, ok := set[id]; ok {
t.Fatalf("Random number is repeated")
}
set[id] = struct{}{}
}
}