-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrand_test.go
More file actions
363 lines (320 loc) · 8.08 KB
/
rand_test.go
File metadata and controls
363 lines (320 loc) · 8.08 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xrand/blob/main/LICENSE.
package xrand_test
import (
"fmt"
"regexp"
"testing"
"time"
"github.com/actforgood/xrand"
)
func TestIntn(t *testing.T) {
t.Parallel()
// arrange
var (
result int
subject = xrand.Intn
tests = []int{1, 11, 23, 104, 256, 999, 15678}
)
for _, testN := range tests {
t.Run(fmt.Sprintf("[0,%d)", testN), func(t *testing.T) {
for range 1000 {
// act
result = subject(testN)
// assert
assertTrue(t, result >= 0)
assertTrue(t, result < testN)
}
})
}
}
func TestIntnBetween(t *testing.T) {
t.Parallel()
// arrange
var (
result int
subject = xrand.IntnBetween
tests = map[int]int{0: 1, 1: 11, 5: 23, 49: 104, 128: 256, 45: 999, 1000: 15678}
)
for a, b := range tests {
t.Run(fmt.Sprintf("[%d,%d)", a, b), func(t *testing.T) {
for range 1000 {
// act
result = subject(a, b)
// assert
assertTrue(t, result >= a)
assertTrue(t, result < b)
}
})
}
}
func TestFloat64(t *testing.T) {
t.Parallel()
// arrange
var (
result float64
subject = xrand.Float64
)
for range 1000 {
// act
result = subject()
// assert
assertTrue(t, result >= 0.0)
assertTrue(t, result < 1.0)
}
}
func TestJitter(t *testing.T) {
t.Parallel()
t.Run("with default max factor", testJitterWithDefaultMaxFactor)
t.Run("with custom max factor", testJitterWithCustomMaxFactor)
}
func testJitterWithDefaultMaxFactor(t *testing.T) {
t.Parallel()
// arrange
var (
result time.Duration
subject = xrand.Jitter
tests = [...]struct {
name string
inputDuration time.Duration
expectedMin time.Duration
expectedMax time.Duration
}{
{
name: "2s",
inputDuration: 2 * time.Second,
expectedMin: time.Duration(1.6 * float64(time.Second)), // jitter = [-0.4s, 0.4s)
expectedMax: time.Duration(2.4 * float64(time.Second)),
},
{
name: "5m",
inputDuration: 5 * time.Minute,
expectedMin: time.Duration(4 * float64(time.Minute)), // jitter = [-1.0m, 1.0m)
expectedMax: time.Duration(6 * float64(time.Minute)),
},
{
name: "100ms",
inputDuration: 100 * time.Millisecond,
expectedMin: time.Duration(80 * float64(time.Millisecond)), // jitter = [-20ms, 20ms)
expectedMax: time.Duration(120 * float64(time.Millisecond)),
},
}
)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
wasDifferent := false
for range 1000 {
// act
result = subject(test.inputDuration)
// assert
assertTrue(t, result >= test.expectedMin)
assertTrue(t, result < test.expectedMax)
if result != test.inputDuration {
wasDifferent = true
}
}
assertTrue(t, wasDifferent)
})
}
}
func testJitterWithCustomMaxFactor(t *testing.T) {
t.Parallel()
// arrange
var (
result time.Duration
subject = xrand.Jitter
tests = [...]struct {
name string
inputDuration time.Duration
inputMaxFactor float64
expectedMin time.Duration
expectedMax time.Duration
}{
{
name: "2s",
inputDuration: 2 * time.Second,
inputMaxFactor: 1.0,
expectedMin: 1, // jitter = [-2s, 2s)
expectedMax: time.Duration(4 * float64(time.Second)),
},
{
name: "5m",
inputDuration: 5 * time.Minute,
inputMaxFactor: 1.0,
expectedMin: 1, // jitter = [-5m, 5m)
expectedMax: time.Duration(10 * float64(time.Minute)),
},
{
name: "100ms",
inputDuration: 100 * time.Millisecond,
inputMaxFactor: 1.0,
expectedMin: 1, // jitter = [-100ms, 100ms)
expectedMax: time.Duration(200 * float64(time.Millisecond)),
},
{
name: "2s",
inputDuration: 2 * time.Second,
inputMaxFactor: 0.6,
expectedMin: time.Duration(0.8 * float64(time.Second)), // jitter = [-1.2s, 1.2s)
expectedMax: time.Duration(3.2 * float64(time.Second)),
},
{
name: "5m",
inputDuration: 5 * time.Minute,
inputMaxFactor: 0.6,
expectedMin: time.Duration(2 * float64(time.Minute)), // jitter = [-3m, 3m)
expectedMax: time.Duration(8 * float64(time.Minute)),
},
{
name: "100ms",
inputDuration: 100 * time.Millisecond,
inputMaxFactor: 0.6,
expectedMin: time.Duration(40 * float64(time.Millisecond)), // jitter = [-60ms, 60ms)
expectedMax: time.Duration(160 * float64(time.Millisecond)),
},
{
name: "positive",
inputDuration: 1 * time.Nanosecond,
inputMaxFactor: 100.0,
expectedMin: 1, // jitter = [-100ns, 100ns)
expectedMax: time.Duration(101 * float64(time.Nanosecond)),
},
}
)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
wasDifferent := false
for range 1000 {
// act
result = subject(test.inputDuration, test.inputMaxFactor)
// assert
assertTrue(t, result >= test.expectedMin)
assertTrue(t, result < test.expectedMax)
if result != test.inputDuration {
wasDifferent = true
}
}
assertTrue(t, wasDifferent)
})
}
}
func TestString(t *testing.T) {
t.Parallel()
// arrange
var (
result string
subject = xrand.String
tests = [...]struct {
name string
inputLength int
inputAlphabet string
expectedReg *regexp.Regexp
}{
{
name: "len = 10, alphabet = xrand.AlphanumAlphabet",
inputLength: 10,
inputAlphabet: xrand.AlphanumAlphabet,
expectedReg: regexp.MustCompile(`^[a-z0-9]{10}$`),
},
{
name: "len = 32, alphabet = xrand.AlphanumAlphabet",
inputLength: 32,
inputAlphabet: xrand.AlphanumAlphabet,
expectedReg: regexp.MustCompile(`^[a-z0-9]{32}$`),
},
{
name: "len = 150, alphabet = xrand.ALPHANUMAlphabet",
inputLength: 150,
inputAlphabet: xrand.ALPHAnumAlphabet,
expectedReg: regexp.MustCompile(`^[A-Z0-9]{150}$`),
},
{
name: "len = 43, alphabet = xrand.DigitsAlphabet",
inputLength: 43,
inputAlphabet: xrand.DigitsAlphabet,
expectedReg: regexp.MustCompile(`^[0-9]{43}$`),
},
{
name: "len = 109, alphabet = abc-",
inputLength: 109,
inputAlphabet: "abc-",
expectedReg: regexp.MustCompile(`^[a-c\-]{109}$`),
},
{
name: "len = 0",
inputLength: 0,
inputAlphabet: "abc",
expectedReg: regexp.MustCompile(`^$`),
},
{
name: "empty alphabet - default alphabet",
inputLength: 2,
inputAlphabet: "",
expectedReg: regexp.MustCompile(`^[a-z0-9]{2}$`),
},
}
)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
for range 1000 {
// act
result = subject(test.inputLength, test.inputAlphabet)
// assert
assertTrue(t, test.expectedReg.Match([]byte(result)))
}
})
}
}
func BenchmarkString(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for range b.N {
_ = xrand.String(16)
}
}
func BenchmarkJitter(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for range b.N {
_ = xrand.Jitter(5 * time.Minute)
}
}
// assertTrue checks if value passed is true.
// Returns successful assertion status.
func assertTrue(t *testing.T, actual bool) bool {
t.Helper()
if !actual {
t.Error("should be true")
return false
}
return true
}
func ExampleIntn() {
// generate a random int in [0, 1000)
randInt := xrand.Intn(1000)
fmt.Println(randInt)
}
func ExampleIntnBetween() {
// generate a random int in [100, 200)
randInt := xrand.IntnBetween(100, 200)
fmt.Println(randInt)
}
func ExampleFloat64() {
// generate a random float in [0.0, 1.0)
randFloat := xrand.Float64()
fmt.Println(randFloat)
}
func ExampleJitter() {
// slightly alter +/- a time.Duration
cacheTTL := 10 * time.Minute
factor := 0.1
jitteredCacheTTL := xrand.Jitter(cacheTTL, factor)
fmt.Println(jitteredCacheTTL)
}
func ExampleString() {
// generate a random string of length 16, containing [a-z0-9] letters.
randString := xrand.String(16, xrand.AlphanumAlphabet)
fmt.Println(randString)
}