-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_test.go
More file actions
201 lines (178 loc) · 5.2 KB
/
random_test.go
File metadata and controls
201 lines (178 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package stringx
import (
"strings"
"sync"
"testing"
)
func TestNewRandomFactory(t *testing.T) {
t.Run("default options", func(t *testing.T) {
factory := NewRandomFactory()
if factory == nil {
t.Fatal("expected non-nil factory")
}
})
t.Run("custom alphabet", func(t *testing.T) {
customAlphabet := "abc123"
factory := NewRandomFactory(RandomAlphabet(customAlphabet))
if factory == nil {
t.Fatal("expected non-nil factory")
}
// Generate a string and verify it only contains characters from the custom alphabet
str := factory.MakeRandomString(100)
for _, c := range str {
if !strings.ContainsRune(customAlphabet, c) {
t.Errorf("generated string contains character %c not in alphabet %s", c, customAlphabet)
}
}
})
}
func TestRandomFactory_MakeRandomString(t *testing.T) {
factory := NewRandomFactory()
t.Run("generates string of correct length", func(t *testing.T) {
lengths := []int{1, 5, 10, 50, 100}
for _, length := range lengths {
str := factory.MakeRandomString(length)
if len(str) != length {
t.Errorf("expected length %d, got %d", length, len(str))
}
}
})
t.Run("generates different strings", func(t *testing.T) {
str1 := factory.MakeRandomString(20)
str2 := factory.MakeRandomString(20)
// While theoretically possible to get the same string, it's extremely unlikely
if str1 == str2 {
t.Logf("warning: generated two identical strings (very unlikely): %s", str1)
}
})
t.Run("only contains alphabet characters", func(t *testing.T) {
str := factory.MakeRandomString(1000)
for _, c := range str {
if !strings.ContainsRune(DefaultRandomAlphabet, c) {
t.Errorf("generated string contains character %c not in default alphabet", c)
}
}
})
t.Run("empty string on zero length", func(t *testing.T) {
if factory.MakeRandomString(0) != "" {
t.Error("expected empty string for zero length")
}
})
t.Run("panics on negative length", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expected panic for negative length")
}
}()
factory.MakeRandomString(-1)
})
}
func TestRandom(t *testing.T) {
t.Run("generates string of correct length", func(t *testing.T) {
lengths := []int{1, 5, 10, 50, 100}
for _, length := range lengths {
str := Random(length)
if len(str) != length {
t.Errorf("expected length %d, got %d", length, len(str))
}
}
})
}
func TestRandomFactory_Concurrent(t *testing.T) {
factory := NewRandomFactory()
var wg sync.WaitGroup
numGoroutines := 100
strLength := 50
results := make(chan string, numGoroutines)
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
str := factory.MakeRandomString(strLength)
results <- str
}()
}
wg.Wait()
close(results)
// Verify all strings have correct length
for str := range results {
if len(str) != strLength {
t.Errorf("expected length %d, got %d", strLength, len(str))
}
}
}
func TestRandomAlphabet(t *testing.T) {
t.Run("numeric only", func(t *testing.T) {
factory := NewRandomFactory(RandomAlphabet("0123456789"))
str := factory.MakeRandomString(100)
for _, c := range str {
if c < '0' || c > '9' {
t.Errorf("expected only digits, got %c", c)
}
}
})
t.Run("lowercase only", func(t *testing.T) {
factory := NewRandomFactory(RandomAlphabet("abcdefghijklmnopqrstuvwxyz"))
str := factory.MakeRandomString(100)
for _, c := range str {
if c < 'a' || c > 'z' {
t.Errorf("expected only lowercase letters, got %c", c)
}
}
})
t.Run("single character", func(t *testing.T) {
factory := NewRandomFactory(RandomAlphabet("x"))
str := factory.MakeRandomString(10)
if str != "xxxxxxxxxx" {
t.Errorf("expected 'xxxxxxxxxx', got %s", str)
}
})
t.Run("unicode alphabet", func(t *testing.T) {
unicodeAlphabet := "你好世界"
factory := NewRandomFactory(RandomAlphabet(unicodeAlphabet))
str := factory.MakeRandomString(100)
// Verify string length is correct (in runes, not bytes)
if Len(str) != 100 {
t.Errorf("expected rune length 100, got %d", Len(str))
}
// Verify all characters are from the alphabet
for _, c := range str {
if !strings.ContainsRune(unicodeAlphabet, c) {
t.Errorf("generated string contains character %c not in alphabet %s", c, unicodeAlphabet)
}
}
})
t.Run("mixed ascii and unicode alphabet", func(t *testing.T) {
mixedAlphabet := "abc你好"
factory := NewRandomFactory(RandomAlphabet(mixedAlphabet))
str := factory.MakeRandomString(100)
// Verify string length is correct (in runes, not bytes)
if Len(str) != 100 {
t.Errorf("expected rune length 100, got %d", Len(str))
}
// Verify all characters are from the alphabet
for _, c := range str {
if !strings.ContainsRune(mixedAlphabet, c) {
t.Errorf("generated string contains character %c not in alphabet %s", c, mixedAlphabet)
}
}
})
}
func BenchmarkRandomFactory_MakeRandomString(b *testing.B) {
factory := NewRandomFactory()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
factory.MakeRandomString(32)
}
}
func BenchmarkRandomFactory_MakeRandomString_Concurrent(b *testing.B) {
factory := NewRandomFactory()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
factory.MakeRandomString(32)
}
})
}