github.com/jfrazelle/docker@v1.1.2-0.20210712172922-bf78e25fe508/testutil/stringutils_test.go (about) 1 package testutil // import "github.com/docker/docker/testutil" 2 3 import ( 4 "testing" 5 6 "gotest.tools/v3/assert" 7 is "gotest.tools/v3/assert/cmp" 8 ) 9 10 func testLengthHelper(generator func(int) string, t *testing.T) { 11 expectedLength := 20 12 s := generator(expectedLength) 13 assert.Check(t, is.Equal(expectedLength, len(s))) 14 } 15 16 func testUniquenessHelper(generator func(int) string, t *testing.T) { 17 repeats := 25 18 set := make(map[string]struct{}, repeats) 19 for i := 0; i < repeats; i = i + 1 { 20 str := generator(64) 21 assert.Check(t, is.Equal(64, len(str))) 22 _, ok := set[str] 23 assert.Check(t, !ok, "Random number is repeated") 24 set[str] = struct{}{} 25 } 26 } 27 28 func TestGenerateRandomAlphaOnlyStringLength(t *testing.T) { 29 testLengthHelper(GenerateRandomAlphaOnlyString, t) 30 } 31 32 func TestGenerateRandomAlphaOnlyStringUniqueness(t *testing.T) { 33 testUniquenessHelper(GenerateRandomAlphaOnlyString, t) 34 }