2018-02-05 16:05:59 -05:00
|
|
|
package testutil // import "github.com/docker/docker/internal/testutil"
|
2017-10-27 20:28:19 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2017-10-27 20:28:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testLengthHelper(generator func(int) string, t *testing.T) {
|
|
|
|
expectedLength := 20
|
|
|
|
s := generator(expectedLength)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(expectedLength, len(s)))
|
2017-10-27 20:28:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func testUniquenessHelper(generator func(int) string, t *testing.T) {
|
|
|
|
repeats := 25
|
|
|
|
set := make(map[string]struct{}, repeats)
|
|
|
|
for i := 0; i < repeats; i = i + 1 {
|
|
|
|
str := generator(64)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(64, len(str)))
|
2017-10-27 20:28:19 -04:00
|
|
|
_, ok := set[str]
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, !ok, "Random number is repeated")
|
2017-10-27 20:28:19 -04:00
|
|
|
set[str] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateRandomAlphaOnlyStringLength(t *testing.T) {
|
|
|
|
testLengthHelper(GenerateRandomAlphaOnlyString, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateRandomAlphaOnlyStringUniqueness(t *testing.T) {
|
|
|
|
testUniquenessHelper(GenerateRandomAlphaOnlyString, t)
|
|
|
|
}
|