-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_unsafe_test.go
141 lines (120 loc) · 2.78 KB
/
random_unsafe_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
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
package random // import "fluux.io/random"
import (
"math/rand"
"strconv"
"strings"
"testing"
"time"
)
const testsRandomNumber = 100000
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
// TestRandomSize checks RandomUnsafe.Size returns valid values.
func TestRandomSize(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
r := NewRandomUnsafe()
for i := 0; i < testsRandomNumber; i++ {
n := r.Size()
if n == nil {
continue
}
if n.Value < 1 {
t.Errorf("%v < 1)", n.Value)
}
}
}
// TestRandomString checks that length of string returned by RandString
// is falling within the specified bounds.
func TestRandomString(t *testing.T) {
r := NewRandomUnsafe()
for i := 0; i < testsRandomNumber; i++ {
min := int(r.Int(100))
max := min + int(r.Int(100))
s := r.String(min, max)
l := len(s)
if l < min || l > max {
t.Errorf("wrong length: %q, (%d, %d))", s, min, max)
}
}
}
func TestRandomFixedLen(t *testing.T) {
r := NewRandomUnsafe()
for i := 0; i < testsRandomNumber; i++ {
l := int(r.Int(100))
s := r.FixedLenString(l)
if len(s) != l {
t.Errorf("wrong length: %q, (%d))", l, len(s))
}
}
}
// TestRandomBool checks that TestRandBool is balanced.
func TestRandomBool(tt *testing.T) {
r := NewRandomUnsafe()
var t, f int
for i := 0; i < testsRandomNumber; i++ {
b := r.Bool()
if b {
t++
} else {
f++
}
}
min := testsRandomNumber/2 - (testsRandomNumber * 10 / 100)
max := testsRandomNumber/2 + (testsRandomNumber * 10 / 100)
if t < min || t > max || f < min || f > max {
tt.Errorf("RandBool is not balanced, (%d true, %d false))", t, f)
}
}
//=============================================================================
// Benchmarks
func BenchmarkRandString(b *testing.B) {
min := 15
max := 25
for i := 0; i < b.N; i++ {
randString(min, max)
}
}
func BenchmarkRandomString(b *testing.B) {
r := NewRandomUnsafe()
for i := 0; i < b.N; i++ {
r.String(15, 25)
}
}
func BenchmarkRandId(b *testing.B) {
for i := 0; i < b.N; i++ {
randId("test")
}
}
func BenchmarkRandomId(b *testing.B) {
r := NewRandomUnsafe()
for i := 0; i < b.N; i++ {
r.RandomId("test")
}
}
func BenchmarkRandInt(b *testing.B) {
for i := 0; i < b.N; i++ {
rand.Intn(1000)
}
}
func BenchmarkRandomInt(b *testing.B) {
r := NewRandomUnsafe()
for i := 0; i < b.N; i++ {
r.Int(1000)
}
}
//=============================================================================
// Benchmark helpers
func randString(min, max int) string {
n := rand.Intn(max-min) + min
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func randId(prefix string) string {
timestamp := strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
id := []string{"test", randString(10, 20), timestamp}
return strings.Join(id, "_")
}