-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomock_test.go
320 lines (289 loc) · 6.53 KB
/
randomock_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
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
package randomock
import (
"math/rand"
"testing"
)
var methods = []string{"ExpFloat64", "Float32", "Float64", "Int", "Int31", "Int31n", "Int63", "Int63n", "Intn", "NormFloat64", "Uint32", "Uint64"}
type callpair struct {
key string
val float64
}
var testCases = []struct {
calls []callpair
}{
{
[]callpair{
{"a", 1.5},
{"b", 5.5},
{"c", -35.5},
},
},
}
func setSeed() {
rand.Seed(1)
}
func setCalls(calls []callpair) *RandoMock {
r := NewRandoMock()
for _, cp := range calls {
r.Add(cp.key, cp.val)
}
return r
}
func TestRandoMock_Add(t *testing.T) {
t.Parallel()
for _, tc := range testCases {
r := setCalls(tc.calls)
for _, call := range tc.calls {
results, ok := r.ret[call.key]
if !ok {
t.Fatalf("cannot find key %s", call.key)
}
got := results.Get()
expected := call.val
if got != expected {
t.Fatalf("inserted call value of %f does not equal expected %f", got, expected)
}
}
}
}
func callMethod(r Randomizer, method string, key string, args ...interface{}) interface{} {
var result interface{}
switch method {
case "ExpFloat64":
result = r.ExpFloat64(key)
case "Float32":
result = r.Float32(key)
case "Float64":
result = r.Float64(key)
case "Int":
result = r.Int(key)
case "Int31":
result = r.Int31(key)
case "Int31n":
result = r.Int31n(key, int32(args[0].(int)))
case "Int63":
result = r.Int63(key)
case "Int63n":
result = r.Int63n(key, int64(args[0].(int)))
case "NormFloat64":
result = r.NormFloat64(key)
case "Intn":
result = r.Intn(key, args[0].(int))
case "Uint32":
result = r.Uint32(key)
case "Uint64":
result = r.Uint64(key)
}
return result
}
func callRealMethod(method string, args ...interface{}) interface{} {
var result interface{}
switch method {
case "ExpFloat64":
result = rand.ExpFloat64()
case "Float32":
result = rand.Float32()
case "Float64":
result = rand.Float64()
case "Int":
result = rand.Int()
case "Int31":
result = rand.Int31()
case "Int31n":
result = rand.Int31n(int32(args[0].(int)))
case "Int63":
result = rand.Int63()
case "Int63n":
result = rand.Int63n(int64(args[0].(int)))
case "NormFloat64":
result = rand.NormFloat64()
case "Intn":
result = rand.Intn(args[0].(int))
case "Uint32":
result = rand.Uint32()
case "Uint64":
result = rand.Uint64()
}
return result
}
func convertResult(method string, r float64) interface{} {
var result interface{}
switch method {
case "ExpFloat64", "Float64", "NormFloat64":
result = float64(r)
case "Float32":
result = float32(r)
case "Int", "Intn":
result = int(r)
case "Int31", "Int31n":
result = int32(r)
case "Int63", "Int63n":
result = int64(r)
case "Uint32":
result = uint32(r)
case "Uint64":
result = uint64(r)
}
return result
}
func TestRandoMockReturns(t *testing.T) {
t.Parallel()
for _, tc := range testCases {
for _, meth := range methods {
r := setCalls(tc.calls)
for _, call := range tc.calls {
t.Run(meth,
func(t *testing.T) {
var expected = convertResult(meth, call.val)
v := callMethod(r, meth, call.key, 123)
if v != expected {
t.Fatalf("RandoMock call value of %v does not equal expected %v", v, expected)
}
})
}
}
}
}
func TestRandomReturns(t *testing.T) {
t.Parallel()
for _, tc := range testCases {
r := &Random{}
for _, call := range tc.calls {
for _, meth := range methods {
t.Run(meth,
func(t *testing.T) {
// Note we need to reset the seed after each call to a randomizing function
// in order to get the same result.
setSeed()
var expected = callRealMethod(meth, 123)
setSeed()
v := callMethod(r, meth, call.key, 123)
if v != expected {
t.Fatalf("Random call value of %d does not equal expected %d", v, expected)
}
})
}
}
}
}
func TestHelpers(t *testing.T) {
t.Parallel()
t.Run("RoundTo", func(t *testing.T) {
testCases := []struct {
v float64
prec int
expected float64
}{
{0.123, 2, 0.12},
{-245.128, 2, -245.13},
{5.1, 3, 5.1},
{9.99, 1, 10.0},
{0.0000123, 6, 0.000012},
{0.001, 2, 0},
{0.05, 1, 0.1},
{0.6, 0, 1},
{0.008, 2, 0.01},
{0.02, 1, 0},
}
for _, tc := range testCases {
r := RoundTo(tc.v, tc.prec)
if r != tc.expected {
t.Fatalf("expected %f, got %f", tc.expected, r)
}
}
})
t.Run("RandBetweenFloat64", func(t *testing.T) {
attempts := 10
testCases := []struct {
a, b float64
}{
{0, 5},
{-100, 100},
{7, 7},
{-10, -10},
}
for _, tc := range testCases {
for attempt := 0; attempt < attempts; attempt++ {
r := RandBetweenFloat64(tc.a, tc.b)
if r < tc.a || r > tc.b {
t.Fatalf("expected value to be between %f and %f", tc.a, tc.b)
}
}
}
})
}
func TestPolicies(t *testing.T) {
allExpected := []float64{0.5, 1, 2.5, 3.5, 4}
testCases := []struct {
name string
policy Policy
expectedFunc func(callNum int) (float64, error)
}{
{
name: "WrapAroundPolicy",
policy: WrapAroundPolicy,
expectedFunc: func(count int) (float64, error) {
return allExpected[count%len(allExpected)], nil
},
},
{
name: "ErrorOutPolicy",
policy: ErrorOutPolicy,
expectedFunc: func(count int) (float64, error) {
if count >= len(allExpected) {
return 0, OutOfBoundsError
}
return allExpected[count], nil
},
},
{
name: "RepeatLastPolicy",
policy: RepeatLastPolicy,
expectedFunc: func(count int) (float64, error) {
if count >= len(allExpected) {
count = len(allExpected) - 1
}
return allExpected[count], nil
},
},
}
tries := 50
for _, tc := range testCases {
r := NewRandoMock().Add("k", allExpected...)
r.SetPolicy("k", tc.policy)
t.Run(tc.name, func(t *testing.T) {
for ii := 0; ii < tries; ii++ {
expected, errExpected := tc.expectedFunc(ii)
defer func(t *testing.T) {
err := recover()
if errExpected != err {
t.Fatal(errExpected)
}
}(t)
got := r.Float64("k")
if got != expected {
t.Fatalf("expected %f, got %f", expected, got)
}
}
})
}
t.Run("DefaultPolicy", func(t *testing.T) {
before := defaultPolicy
policy := RepeatLastPolicy
SetDefaultPolicy(policy)
now := defaultPolicy
if now != RepeatLastPolicy || now == before {
t.Fatalf("failed to change default policy")
}
r := NewRandoMock().Add("k", allExpected...)
if r.Policy("k") != policy {
t.Fatalf("failed to change default policy of results struct")
}
})
}
func TestErrors(t *testing.T) {
msg := OutOfBoundsError.Error()
if msg == "" {
t.Fatalf("no error message")
}
}