@@ -23,3 +23,58 @@ func TestRandomStringUniqueness(t *testing.T) {
2323 set [str ] = struct {}{}
2424 }
2525}
26+
27+ func testLengthHelper (generator func (int ) string , t * testing.T ) {
28+ expectedLength := 20
29+ s := generator (expectedLength )
30+ if len (s ) != expectedLength {
31+ t .Fatalf ("Length of %s was %d but expected length %d" , s , len (s ), expectedLength )
32+ }
33+ }
34+
35+ func testUniquenessHelper (generator func (int ) string , t * testing.T ) {
36+ repeats := 25
37+ set := make (map [string ]struct {}, repeats )
38+ for i := 0 ; i < repeats ; i = i + 1 {
39+ str := generator (64 )
40+ if len (str ) != 64 {
41+ t .Fatalf ("Id returned is incorrect: %s" , str )
42+ }
43+ if _ , ok := set [str ]; ok {
44+ t .Fatalf ("Random number is repeated" )
45+ }
46+ set [str ] = struct {}{}
47+ }
48+ }
49+
50+ func isASCII (s string ) bool {
51+ for _ , c := range s {
52+ if c > 127 {
53+ return false
54+ }
55+ }
56+ return true
57+ }
58+
59+ func TestGenerateRandomAlphaOnlyStringLength (t * testing.T ) {
60+ testLengthHelper (GenerateRandomAlphaOnlyString , t )
61+ }
62+
63+ func TestGenerateRandomAlphaOnlyStringUniqueness (t * testing.T ) {
64+ testUniquenessHelper (GenerateRandomAlphaOnlyString , t )
65+ }
66+
67+ func TestGenerateRandomAsciiStringLength (t * testing.T ) {
68+ testLengthHelper (GenerateRandomAsciiString , t )
69+ }
70+
71+ func TestGenerateRandomAsciiStringUniqueness (t * testing.T ) {
72+ testUniquenessHelper (GenerateRandomAsciiString , t )
73+ }
74+
75+ func TestGenerateRandomAsciiStringIsAscii (t * testing.T ) {
76+ str := GenerateRandomAsciiString (64 )
77+ if ! isASCII (str ) {
78+ t .Fatalf ("%s contained non-ascii characters" , str )
79+ }
80+ }
0 commit comments