-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
random_test.go
56 lines (47 loc) · 1.11 KB
/
random_test.go
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
package utils
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestRandomStringWithLength(t *testing.T) {
for i := 0; i < 10; i++ {
n, err := SecRandInt(1000)
if err != nil {
require.NoError(t, err)
}
ret := RandomStringWithLength(n)
require.Len(t, ret, n)
ret, err = SecRandomStringWithLength(n)
require.NoError(t, err)
require.Len(t, ret, n)
}
}
func TestRandomChoice(t *testing.T) {
var arr []int64
for i := 0; i < 10000; i++ {
arr = append(arr, time.Now().UnixNano())
}
randor := NewRand()
for i := 0; i < 1000; i++ {
n := randor.Intn(10000)
got := RandomChoice(arr, n)
require.Len(t, got, n, "n: %d, got: %d", n, len(got))
}
}
// cpu: Intel(R) Xeon(R) Gold 5320 CPU @ 2.20GHz
// BenchmarkRandomChoice/run-16 8062 130228 ns/op 7472 B/op 10 allocs/op
func BenchmarkRandomChoice(b *testing.B) {
var arr []int64
for i := 0; i < 10000; i++ {
arr = append(arr, time.Now().UnixNano())
}
b.Run("run", func(b *testing.B) {
for i := 0; i < b.N; i++ {
got := RandomChoice(arr, 100)
if len(got) != 100 {
b.FailNow()
}
}
})
}