-
Notifications
You must be signed in to change notification settings - Fork 1
/
randutil_test.go
222 lines (208 loc) · 5.42 KB
/
randutil_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
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
// Copyright (c) 2012 Jason McVetta. This is Free Software, released under the
// terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
package utils
import (
"fmt"
// "github.com/stretchr/testify/assert"
"log"
"math"
"math/rand"
"testing"
)
const (
maxIntRange = 999999
)
var (
stringChoices []string
intChoices []int
)
// Test that AlphaStringRange produces a string within specified min/max length
// parameters. The actual randonimity of the string is not tested.
func TestAlphaStringRange(t *testing.T) {
min := rand.Intn(100)
max := min + 1 + rand.Intn(100)
s, err := AlphaStringRange(min, max)
if err != nil {
t.Error(err)
}
switch true {
case len(s) < min:
t.Error("Random string is too short")
case len(s) > max:
t.Error("Random string is too short")
}
return
}
// Test that IntRange produces an integer between min and max
func TestIntRange(t *testing.T) {
min := 567
max := 890
i, err := IntRange(min, max)
if err != nil {
t.Error(err)
}
if i > max || i < min {
t.Error("IntRange returned an out-of-range integer")
}
// Check that we get an error when min > max
i, err = IntRange(max, min)
if err != MinMaxError {
msg := fmt.Sprintf("Expected error when min > max, but got:%v", err)
t.Error(msg)
}
}
// Test that the strings we produce are actually random. This is done by
// comparing two 50,000 character generated random strings and checking that
// they differ. It is quite unlikely, but not strictly impossible, that two
// truly random strings will be identical.
func TestRandonimity(t *testing.T) {
l := 50000
s1, err := AlphaString(l)
if err != nil {
t.Error(err)
}
s2, err := AlphaString(l)
if err != nil {
t.Error(err)
}
if s1 == s2 {
msg := "Generated two identical 'random' strings - this is probably an error"
t.Error(msg)
}
}
// TestChoice tests that over the course of 1,000,000 calls on the same 100
// possible choices, the Choice() function returns every possible choice at
// least once. Note, there is a VERY small chance this test could fail by
// random chance even when the code is working correctly.
func TestChoice(t *testing.T) {
// Create a map associating each possible choice with a bool.
chosen := make(map[int]bool)
for _, v := range intChoices {
chosen[v] = false
}
// Run Choice() a million times, and record which of the possible choices it returns.
for i := 0; i < 1000000; i++ {
c, err := ChoiceInt(intChoices)
if err != nil {
t.Error(err)
}
chosen[c] = true
}
// Fail if any of the choices was not chosen even once.
for _, v := range chosen {
if v == false {
msg := "In 1,000,000 passes Choice() failed to return all 100 possible choices. Something is probably wrong."
t.Error(msg)
}
}
}
// TestWeightedChoice assembles a list of choices, weighted 0-9, and tests that
// over the course of 1,000,000 calls to WeightedChoice() each choice is
// returned more often than choices with a lower weight.
func TestWeightedChoice(t *testing.T) {
// Make weighted choices
var choices []Choice
chosenCount := make(map[Choice]int)
for i := 0; i < 10; i++ {
c := Choice{
Weight: i,
Item: i,
}
choices = append(choices, c)
chosenCount[c] = 0
}
// Run WeightedChoice() a million times, and record how often it returns each
// of the possible choices.
for i := 0; i < 1000000; i++ {
c, err := WeightedChoice(choices)
if err != nil {
t.Error(err)
}
chosenCount[c] += 1
}
// Test that higher weighted choices were chosen more often than their lower
// weighted peers.
for i, c := range choices[0 : len(choices)-1] {
next := choices[i+1]
expr := chosenCount[c] < chosenCount[next]
t.Log("expr -> ", expr)
// assert.True(t, expr)
}
}
// BenchmarkChoiceInt runs a benchmark on the ChoiceInt function.
func BenchmarkChoiceInt(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := ChoiceInt(intChoices)
if err != nil {
b.Error(err)
}
}
}
// BenchmarkChoiceString runs a benchmark on the ChoiceString function.
func BenchmarkChoiceString(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := ChoiceString(stringChoices)
if err != nil {
b.Error(err)
}
}
}
// BenchmarkIntRange runs a benchmark on the IntRange function.
func BenchmarkIntRange(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := IntRange(0, math.MaxInt32)
if err != nil {
b.Error(err)
}
}
}
// BenchmarkIntRange runs a benchmark on the WeightedChoice function.
func BenchmarkWeightedChoice(b *testing.B) {
// Create some random choices and weights before we start
b.StopTimer()
choices := []Choice{}
for i := 0; i < 100; i++ {
s, err := AlphaString(64)
if err != nil {
b.Error(err)
}
w, err := IntRange(1, 10)
if err != nil {
b.Error(err)
}
c := Choice{
Item: s,
Weight: w,
}
choices = append(choices, c)
}
// Run the benchmark
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err := WeightedChoice(choices)
if err != nil {
b.Error(err)
}
}
}
// init populates two arrays of random choices, intChoices and stringChoices,
// which will be used by various test and benchmark functions.
func init() {
log.SetFlags(log.Ltime | log.Lshortfile)
// Random integers
for i := 0; i < 100; i++ {
randint, err := IntRange(0, maxIntRange)
if err != nil {
log.Panicln(err)
}
intChoices = append(intChoices, randint)
}
// Random strings
for i := 0; i < 100; i++ {
randstr, err := AlphaString(32)
if err != nil {
log.Panicln(err)
}
stringChoices = append(stringChoices, randstr)
}
}