-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerators.go
More file actions
403 lines (368 loc) · 9.54 KB
/
generators.go
File metadata and controls
403 lines (368 loc) · 9.54 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package suss
import (
"encoding/binary"
"math"
"math/rand"
)
type SliceGen struct {
Avg int
Min int
Max int
f func()
}
// Generator generates
// data to be used in a test.
// Fill should draw bytes from the Data interface
// and change its own value based off those bytes.
//
// Generators can be passed to the Runner.Draw function
// to be supplied with a Data to draw bytes from.
// This is the main way to get varying data that might
// cause tests to fail.
type Generator interface {
Fill(d Data)
}
// Data is an interface passed to Fill methods on types implementing
// the Generator interface. It is the main method for getting random
// data from the Suspicion runner.
type Data interface {
// Draw is the main way to get random data
// for a Suspicion test. It takes a number of bytes
// to draw and a Sample function. The Sample function
// should return a valid byte sequence for the value.
//
// Since return values from Sample is used as examples
// during execution, it is adventageous to return
// interesting values. Floating point Not-a-Number
// and Infinity values is a good example of such a
// value.
//
// During test execution, Draw might return the value
// from Sample or a random one. Callers must handle
// any random byte sequence, either by reinterpreting it
// or calling Invalid.
Draw(n int, smp Sample) []byte
// StartExample and EndExample are used to specify boundaries for
// draws. The shrinking algorithm uses these boundaries to make
// decisions about how to shrink.
//
// Most users will not need to
// explicitly call these functions, since Draw inserts StartExample
// and EndExample calls around calls to Fill.
//
// Calls to these functions can be nested.
StartExample()
EndExample()
}
// Slice returns a generator for a slice value.
// It will repeatedly call the given function
// during fill. It is the functions responsibility
// to build the slice wanted.
//
// An example of the proper use of
// Slice:
//
// var f []float64
// s := suss.Slice(func() {
// f = append(f, runner.Float64())
// })
// runner.Draw(s)
func Slice(f func()) *SliceGen {
return &SliceGen{
Avg: 50,
Min: 0,
Max: int(^uint(0) >> 1),
f: f,
}
}
func (s *SliceGen) Fill(d Data) {
// The intuitive way to turn an infinite bytestream into a
// slice would be to grab a value at the beginning
// and then generate that number of elements
// However, this gets in the way of shrinking
//
// Instead, for each element, grab a byte
// asking us if we want more elements.
// That way, deleting a span in the byte
// stream turns into the element not being
// added.
l := uint64(0)
stopvalue := 1 - (1.0 / (1 + float64(s.Avg)))
if s.Min < 0 {
panic("invalid min slice length")
}
min := uint64(s.Min)
max := uint64(s.Max)
for l < max {
d.StartExample()
more := biasBool(d, stopvalue)
if !more && l >= min {
d.EndExample()
return
}
l++
sliceCall(s.f, d)
}
}
// Since slice functions can panic, make sure that we
// always call d.EndExample
func sliceCall(f func(), d Data) {
defer d.EndExample()
f()
}
// BoolGen implements a generator for boolean values.
type BoolGen bool
func (b *BoolGen) Fill(d Data) {
byt := d.Draw(1, Uniform)
*b = byt[0]&1 == 1
}
// Bool is a convenience function that returns
// a boolean value from the Runner.
func (r *Runner) Bool() bool {
var bgen BoolGen
r.Draw(&bgen)
return bool(bgen)
}
// Float64 is a convenience function that returns
// a float64 value from the Runner.
func (r *Runner) Float64() float64 {
var f Float64Gen
r.Draw(&f)
return float64(f)
}
// Float64Gen implements a generator for float64 values.
type Float64Gen float64
var nastyFloats = []float64{
0.0, 0.5, 1.0 / 3, 10e6, 10e-6, 1.175494351e-38, 2.2250738585072014e-308,
1.7976931348623157e+308, 3.402823466e+38, 9007199254740992, 1 - 10e-6,
2 + 10e-6, 1.192092896e-07, 2.2204460492503131e-016,
}
func init() {
n := []float64{math.NaN(), math.Inf(0)}
// add 5 NaN and Inf here, since they are more likely to
// cause failures.
for i := 0; i < 5; i++ {
nastyFloats = append(nastyFloats, n...)
}
for _, f := range nastyFloats {
nastyFloats = append(nastyFloats, -f)
}
}
func (f *Float64Gen) Fill(d Data) {
fbits := d.Draw(10, func(r *rand.Rand, n int) []byte {
if n != 10 {
panic("bad float size")
}
flavor := r.Intn(10)
var f float64
switch {
case flavor <= 4:
f = nastyFloats[r.Intn(len(nastyFloats))]
case flavor == 5:
var b [10]byte
r.Read(b[:])
return b[:]
case flavor == 6:
f = r.Float64() * float64((r.Intn(2)*2)-1)
case flavor == 7:
f = r.NormFloat64()
case flavor == 8:
u := r.Int63()
if r.Intn(2) == 1 {
u = -u
}
f = float64(u)
default:
u := r.Int63()
if r.Intn(2) == 1 {
u = -u
}
f = float64(u)
n := r.NormFloat64()
n += f
}
b := encodefloat64(f)
return b[:]
})
fl, invalid := decodefloat64(fbits)
if invalid {
Invalid()
}
*f = Float64Gen(fl)
}
// encodefloat64 attempts to encode a floating point number
// so that its lexicographical ordering follows human intuition
//
// Design goals were:
// - Integers are simpler than fractionals
// - positive numbers are simpler than negative ones
// - exponents of smaller magnitude are simpler, regardless of sign
// - 0 is the simplest number, 1 is the second most simple number
func encodefloat64(f float64) [10]byte {
var b [10]byte
bits := math.Float64bits(f)
// encode the sign bit as a single byte
b[0] = byte((bits & (1 << 63)) >> 63)
// for the mantissa, we want simpler fractions
// This means we get numbers that require fewer
// digits to print it
// Encoding as a little endian number
// makes shrinking go towards a number with
// fewer significant digits
mant := bits & (^uint64(0) >> (64 - 52))
binary.LittleEndian.PutUint64(b[1:], mant)
// if the exponent is 0, that means this value
// is a zero. don't unbias the exponent in this case
// TODO: handle subnormals so that they're more complex
sexp := int16((bits >> 52) & 0x7ff)
var exp uint16
if sexp == 0 {
if mant != 0 {
// subnormal number, use the extra range we get from
// int16 to signal this
exp = sint16tolex16(-1024)
}
} else if sexp == 0x7ff {
// infinity and NaN, they're more complex that negative
// exponent and subnormals
exp = sint16tolex16(-1025)
} else {
// regular exponent
// unbias
sexp -= 1023
// if exponent is positive, bias it +1
// so that an exponent of 1 becomes 0
// This keeps the invariant that 0 is
// simpler than 1
if sexp >= 0 {
exp = uint16(sexp) + 1
} else {
// for negative exponents
// use signed regular integer
// This makes -1 simpler than -2
// when interpreted as a byte stream
// the sign keeps the invariant that
// integers are simpler than fractionals
exp = sint16tolex16(sexp)
}
}
binary.BigEndian.PutUint16(b[8:], exp)
return b
}
func sint16tolex16(s int16) uint16 {
s *= -1
exp := uint16(s)
exp ^= (1 << 15)
return exp
}
func decodefloat64(b []byte) (float64, bool) {
fbits := uint64(0)
sign := b[0]
if sign != 0 && sign != 1 {
return 0, true
}
fbits = uint64(sign) << 63
// mantissa only take 7 bytes in our binary packing
// but binary only lets us read in chunks of 8
// copy the mantissa value into an empty array
// and then decode to make sure that we don't
var mb [8]byte
copy(mb[:], b[1:8])
mant := binary.LittleEndian.Uint64(mb[:])
exp := binary.BigEndian.Uint16(b[8:])
if exp&(1<<15) != 0 {
// this is a signed exponent
// clear the sign bit
sexp := int16(exp & (^uint16(0) >> 1))
if sexp == 1024 {
exp = 0
} else if sexp == 1025 {
exp = 0x7ff
} else {
// this is a regular negative exponent
// make into negative number
sexp *= -1
// bias
sexp += 1023
exp = uint16(sexp)
}
} else if exp != 0 {
// positive exponent
exp -= 1
exp += 1023
} else if mant != 0 {
mant = 0
}
fbits ^= mant & (^uint64(0) >> (64 - 52))
fbits ^= uint64(exp) << 52
return math.Float64frombits(fbits), false
}
// Uint64Gen implements a generator for uint64 values.
type Uint64Gen uint64
func (u *Uint64Gen) Fill(d Data) {
b := d.Draw(8, Uniform)
*u = Uint64Gen(binary.BigEndian.Uint64(b))
}
// Uint64 is a convenience function that returns
// a uint64 value from the Runner.
func (r *Runner) Uint64() uint64 {
var u Uint64Gen
r.Draw(&u)
return uint64(u)
}
// Int16Gen implements a generator for int16 values.
type Int16Gen int16
func (i *Int16Gen) Fill(d Data) {
f := d.Draw(2, Uniform)
*i = Int16Gen(binary.BigEndian.Uint16(f))
}
// Int16 is a convenience function that returns
// a int16 value from the Runner.
func (r *Runner) Int16() int16 {
var i Int16Gen
r.Draw(&i)
return int16(i)
}
// ByteGen implements a generator for byte values.
type ByteGen byte
func (b *ByteGen) Fill(d Data) {
*b = ByteGen(d.Draw(1, Uniform)[0])
}
// Byte is a convenience function that returns
// a byte value from the Runner.
func (r *Runner) Byte() byte {
var b ByteGen
r.Draw(&b)
return byte(b)
}
// Int63nGen generates a int64 between 0 and N, following
// the pattern of the math/rand function. After Fill
// the value can be read from Value
type Int63nGen struct {
Value int64
N int64
}
func (i *Int63nGen) Fill(d Data) {
bits := d.Draw(8, func(r *rand.Rand, n int) []byte {
var b [8]byte
val := r.Int63n(i.N)
binary.BigEndian.PutUint64(b[:], uint64(val))
return b[:]
})
val := int64(binary.BigEndian.Uint64(bits))
if val >= i.N || val < 0 {
Invalid()
}
i.Value = val
}
func biasBool(d Data, f float64) bool {
bits := d.Draw(1, func(r *rand.Rand, n int) []byte {
roll := r.Float64()
b := byte(0)
if roll < f {
b = 1
}
return []byte{b}
})
return bits[0] != 0
}