-
Notifications
You must be signed in to change notification settings - Fork 25
/
combinators.go
286 lines (232 loc) · 6.06 KB
/
combinators.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
// Copyright 2019 Gregory Petrosyan <gregory.petrosyan@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid
import (
"fmt"
"math"
"strings"
)
const tryLabel = "try"
// Custom creates a generator which produces results of calling fn. In fn, values should be generated
// by calling other generators; it is invalid to return a value from fn without using any other generator.
// Custom is a primary way of creating user-defined generators.
func Custom[V any](fn func(*T) V) *Generator[V] {
return newGenerator[V](&customGen[V]{
fn: fn,
})
}
type customGen[V any] struct {
fn func(*T) V
}
func (g *customGen[V]) String() string {
var v V
return fmt.Sprintf("Custom(%T)", v)
}
func (g *customGen[V]) value(t *T) V {
return find(g.maybeValue, t, small)
}
func (g *customGen[V]) maybeValue(t *T) (V, bool) {
t = newT(t.tb, t.s, flags.debug, nil)
defer func() {
if r := recover(); r != nil {
if _, ok := r.(invalidData); !ok {
panic(r)
}
}
}()
return g.fn(t), true
}
// Deferred creates a generator which defers calling fn until attempting to produce a value. This allows
// to define recursive generators.
func Deferred[V any](fn func() *Generator[V]) *Generator[V] {
return newGenerator[V](&deferredGen[V]{
fn: fn,
})
}
type deferredGen[V any] struct {
g *Generator[V]
fn func() *Generator[V]
}
func (g *deferredGen[V]) String() string {
var v V
return fmt.Sprintf("Deferred(%T)", v)
}
func (g *deferredGen[V]) value(t *T) V {
if g.g == nil {
g.g = g.fn()
}
return g.g.value(t)
}
func filter[V any](g *Generator[V], fn func(V) bool) *Generator[V] {
return newGenerator[V](&filteredGen[V]{
g: g,
fn: fn,
})
}
type filteredGen[V any] struct {
g *Generator[V]
fn func(V) bool
}
func (g *filteredGen[V]) String() string {
return fmt.Sprintf("%v.Filter(...)", g.g)
}
func (g *filteredGen[V]) value(t *T) V {
return find(g.maybeValue, t, small)
}
func (g *filteredGen[V]) maybeValue(t *T) (V, bool) {
v := g.g.value(t)
if g.fn(v) {
return v, true
} else {
var zero V
return zero, false
}
}
func find[V any](gen func(*T) (V, bool), t *T, tries int) V {
for n := 0; n < tries; n++ {
i := t.s.beginGroup(tryLabel, false)
v, ok := gen(t)
t.s.endGroup(i, !ok)
if ok {
return v
}
}
panic(invalidData(fmt.Sprintf("failed to find suitable value in %d tries", tries)))
}
// Map creates a generator producing fn(u) for each u produced by g.
func Map[U any, V any](g *Generator[U], fn func(U) V) *Generator[V] {
return newGenerator[V](&mappedGen[U, V]{
g: g,
fn: fn,
})
}
type mappedGen[U any, V any] struct {
g *Generator[U]
fn func(U) V
}
func (g *mappedGen[U, V]) String() string {
return fmt.Sprintf("Map(%v, %T)", g.g, g.fn)
}
func (g *mappedGen[U, V]) value(t *T) V {
return g.fn(g.g.value(t))
}
// Just creates a generator which always produces the given value.
// Just(val) is a shorthand for [SampledFrom]([]V{val}).
func Just[V any](val V) *Generator[V] {
return SampledFrom([]V{val})
}
// SampledFrom creates a generator which produces values from the given slice.
// SampledFrom panics if slice is empty.
func SampledFrom[S ~[]E, E any](slice S) *Generator[E] {
assertf(len(slice) > 0, "slice should not be empty")
return newGenerator[E](&sampledGen[E]{
slice: slice,
})
}
type sampledGen[E any] struct {
slice []E
}
func (g *sampledGen[E]) String() string {
if len(g.slice) == 1 {
return fmt.Sprintf("Just(%v)", g.slice[0])
} else {
return fmt.Sprintf("SampledFrom(%v %T)", len(g.slice), g.slice[0])
}
}
func (g *sampledGen[E]) value(t *T) E {
i := genIndex(t.s, len(g.slice), true)
return g.slice[i]
}
// Permutation creates a generator which produces permutations of the given slice.
func Permutation[S ~[]E, E any](slice S) *Generator[S] {
return newGenerator[S](&permGen[S, E]{
slice: slice,
})
}
type permGen[S ~[]E, E any] struct {
slice S
}
func (g *permGen[S, E]) String() string {
var zero E
return fmt.Sprintf("Permutation(%v %T)", len(g.slice), zero)
}
func (g *permGen[S, E]) value(t *T) S {
s := append(S(nil), g.slice...)
n := len(s)
m := n - 1
if m < 0 {
m = 0
}
// shrink-friendly variant of Fisher–Yates shuffle: shrinks to lower number of smaller distance swaps
repeat := newRepeat(0, m, math.MaxInt, "permute")
for i := 0; repeat.more(t.s); i++ {
j, _, _ := genUintRange(t.s, uint64(i), uint64(n-1), false)
s[i], s[j] = s[j], s[i]
}
return s
}
// OneOf creates a generator which produces each value by selecting one of gens and producing a value from it.
// OneOf panics if gens is empty.
func OneOf[V any](gens ...*Generator[V]) *Generator[V] {
assertf(len(gens) > 0, "at least one generator should be specified")
return newGenerator[V](&oneOfGen[V]{
gens: gens,
})
}
type oneOfGen[V any] struct {
gens []*Generator[V]
}
func (g *oneOfGen[V]) String() string {
strs := make([]string, len(g.gens))
for i, g := range g.gens {
strs[i] = g.String()
}
return fmt.Sprintf("OneOf(%v)", strings.Join(strs, ", "))
}
func (g *oneOfGen[V]) value(t *T) V {
i := genIndex(t.s, len(g.gens), true)
return g.gens[i].value(t)
}
// Ptr creates a *E generator. If allowNil is true, Ptr can return nil pointers.
func Ptr[E any](elem *Generator[E], allowNil bool) *Generator[*E] {
return newGenerator[*E](&ptrGen[E]{
elem: elem,
allowNil: allowNil,
})
}
type ptrGen[E any] struct {
elem *Generator[E]
allowNil bool
}
func (g *ptrGen[E]) String() string {
return fmt.Sprintf("Ptr(%v, allowNil=%v)", g.elem, g.allowNil)
}
func (g *ptrGen[E]) value(t *T) *E {
pNonNil := float64(1)
if g.allowNil {
pNonNil = 0.5
}
if flipBiasedCoin(t.s, pNonNil) {
e := g.elem.value(t)
return &e
} else {
return nil
}
}
func asAny[V any](g *Generator[V]) *Generator[any] {
return newGenerator[any](&asAnyGen[V]{
gen: g,
})
}
type asAnyGen[V any] struct {
gen *Generator[V]
}
func (g *asAnyGen[V]) String() string {
return fmt.Sprintf("%v.AsAny()", g.gen)
}
func (g *asAnyGen[V]) value(t *T) any {
return g.gen.value(t)
}