-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_test.go
360 lines (297 loc) · 9.27 KB
/
gen_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
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
package gen
import (
"math/rand"
"reflect"
"testing"
"testing/quick"
)
func TestOnly(t *testing.T) {
expected := 2
g := Only(expected)
for _, value := range GenerateN(g, 100) {
if value != expected {
t.Fatalf("only returned unexpected value. expected: %d, actual: %d", expected, value)
}
}
}
func TestOneOf(t *testing.T) {
possibilities := []string{"Margot", "John", "Jack", "Peter", "Bob", "Anne", "Marry"}
g := OneOf(possibilities...)
possibilitiesLookup := make(map[string]struct{})
for _, p := range possibilities {
possibilitiesLookup[p] = struct{}{}
}
for _, value := range GenerateN(g, 100) {
if _, exists := possibilitiesLookup[value]; !exists {
t.Fatalf("OneOf returned %s which was not in the possibilities", value)
}
}
}
func isBetween(num, actualMin, actualMax int) bool {
return num >= actualMin && num <= actualMax
}
func TestBetween(t *testing.T) {
min := 100000 // this is intentional
max := -790832 // this is intentional
g := Between(min, max)
actualMin := max
actualMax := min
for _, value := range GenerateN(g, 100) {
if !isBetween(value, actualMin, actualMax) {
t.Fatalf("%d is not actually between %d and %d", value, actualMin, actualMax)
}
}
}
func TestBetweenWithOnePossibility(t *testing.T) {
amount := 10
g := Between(amount, amount)
for _, value := range GenerateN(g, 100) {
if value != amount {
t.Fatalf("Between with the same arguments did not act as Only")
}
}
}
func TestOneOfWithOnePossibility(t *testing.T) {
type Human struct{ Name string }
onlyPossibility := Human{"John"}
choices := OneOf(onlyPossibility)
for _, value := range GenerateN(choices, 100) {
if value != onlyPossibility {
t.Fatal("OneOf did not act as Only when only one possibility exists")
}
}
}
type quickOnly[T any] struct {
value T
}
func (qo *quickOnly[T]) Generate(rand *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(qo.value)
}
func BenchmarkOnly(b *testing.B) {
value := 42
o := Only(value)
var qo quick.Generator = &quickOnly[int]{value}
b.Run("gen-only", func(b *testing.B) {
for i := 0; i < b.N; i++ {
o.Generate()
}
})
b.Run("quick-only", func(b *testing.B) {
for i := 0; i < b.N; i++ {
qo.Generate(random, 50).Int()
}
})
}
type quickBetween[T Numeric] struct {
start, end int
}
// This is a much-simpler implementation of Between, so it makes sense if it can compete with gen's Between
func (qb *quickBetween[T]) Generate(rand *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(
rand.Intn(int(qb.end-qb.start)) + qb.start,
)
}
func BenchmarkBetween(b *testing.B) {
start := 1
end := 100000000
g := Between(start, end)
var qb quick.Generator = &quickBetween[int]{start, end}
b.Run("gen-between", func(b *testing.B) {
for i := 0; i < b.N; i++ {
g.Generate()
}
})
b.Run("quick-between", func(b *testing.B) {
for i := 0; i < b.N; i++ {
qb.Generate(random, 50).Int()
}
})
}
type quickOneOf[T any] struct {
slice []T
length int
}
func (qo *quickOneOf[T]) Generate(rand *rand.Rand, size int) reflect.Value {
index := rand.Intn(qo.length)
return reflect.ValueOf(qo.slice[index])
}
func BenchmarkOneOf(b *testing.B) {
slice := make([]int, 1000)
for i := 0; i < 1000; i++ {
slice[i] = i
}
g := OneOf(slice)
var qo quick.Generator = &quickOneOf[int]{slice, len(slice)}
b.Run("gen-one-of", func(b *testing.B) {
for i := 0; i < b.N; i++ {
g.Generate()
}
})
b.Run("quick-one-of", func(b *testing.B) {
for i := 0; i < b.N; i++ {
qo.Generate(random, 50).Int()
}
})
}
type TestPerson struct {
Name string
Surname string
Age int
}
func BenchmarkComposition(b *testing.B) {
nameChoices := []string{"John", "Bob", "Alice", "Anne", "Sherlock", "Jack", "Ross", "Brian"}
surnameChoices := []string{"Cole", "Dylan", "Chains", "Marry", "Holmes", "Black", "Geller", "UNKNOWN"}
age := 20
g := FlatMap(OneOf(nameChoices...), func(name string) Gen[TestPerson] {
return FlatMap(OneOf(surnameChoices...), func(surname string) Gen[TestPerson] {
return Map(Only(age), func(age int) TestPerson {
return TestPerson{name, surname, age}
})
})
})
inferedG, _ := Infer[TestPerson](Wrap(OneOf(nameChoices)), Wrap(Only(age)))
b.Run("gen-composition-functional", func(b *testing.B) {
for i := 0; i < b.N; i++ {
g.Generate()
}
})
b.Run("gen-infered-composition", func(b *testing.B) {
for i := 0; i < b.N; i++ {
inferedG.Generate()
}
})
}
type personGen struct {
nameGen, surnameGen Gen[string]
ageGen Gen[int]
}
func (pg *personGen) Generate() TestPerson {
return TestPerson{pg.nameGen.Generate(), pg.surnameGen.Generate(), pg.ageGen.Generate()}
}
type quickPersonGen struct {
nameGen, surnameGen, ageGen quick.Generator
}
func (qpg quickPersonGen) Generate(rand *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(
TestPerson{qpg.nameGen.Generate(random, 50).String(), qpg.surnameGen.Generate(random, 50).String(), int(qpg.ageGen.Generate(random, 50).Int())},
)
}
func BenchmarkComposition2(b *testing.B) {
names := []string{"John", "Jack", "Beth", "Anne", "Freddie", "Ross", "Rachel", "Tom"}
surnames := []string{"Smith", "Hart", "Lauren", "Marry", "Simpson", "Geller", "Hanks", "Mercury"}
age := 42
g := &personGen{OneOf(names...), OneOf(surnames...), Only(age)}
qg := &quickPersonGen{&quickOneOf[string]{names, len(names)}, &quickOneOf[string]{surnames, len(surnames)}, &quickOnly[int]{age}}
b.Run("gen-composition", func(b *testing.B) {
for i := 0; i < b.N; i++ {
g.Generate()
}
})
b.Run("quick-composition", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = qg.Generate(random, 50).Interface().(TestPerson)
}
})
}
type Programmer struct {
Name string
Surname string
GithubUrl string
FavLang string
Origin string
Age int
Experiance int
}
type programmerGen struct {
nameGen, surnameGen, gitGen, langGen, originGen Gen[string]
ageGen, experienceGen Gen[int]
}
func (pg *programmerGen) Generate() Programmer {
return Programmer{
pg.nameGen.Generate(),
pg.surnameGen.Generate(),
pg.gitGen.Generate(),
pg.langGen.Generate(),
pg.originGen.Generate(),
pg.ageGen.Generate(),
pg.experienceGen.Generate(),
}
}
type quickProgrammerGen struct {
nameGen, surnameGen, gitGen, langGen, originGen, ageGen, experienceGen quick.Generator
}
func (qpg quickProgrammerGen) Generate(rand *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(
Programmer{
qpg.nameGen.Generate(random, 50).String(),
qpg.surnameGen.Generate(random, 50).String(),
qpg.gitGen.Generate(random, 50).String(),
qpg.langGen.Generate(random, 50).String(),
qpg.originGen.Generate(random, 50).String(),
int(qpg.ageGen.Generate(random, 50).Int()),
int(qpg.experienceGen.Generate(random, 50).Int()),
},
)
}
func BenchmarkCompositionMultipleFields(b *testing.B) {
nameGen := OneOf("John", "Jack", "Beth", "Anne", "Freddie", "Ross", "Rachel", "Tom")
surnameGen := OneOf("Smith", "Hart", "Lauren", "Marry", "Simpson", "Geller", "Hanks", "Mercury")
gitGen := Only("https://github.com/AminMal")
langGen := OneOf("Scala", "Rust", "Go", "Java", "Python")
originGen := OneOf("USA", "Iran", "Spain", "France")
ageGen := Between(16, 79)
experienceGen := Between(1, 8)
var directGen Gen[Programmer] = &programmerGen{nameGen, surnameGen, gitGen, langGen, originGen, ageGen, experienceGen}
qg := quickProgrammerGen{
nameGen: &quickOneOf[string]{[]string{"John", "Jack", "Beth", "Anne", "Freddie", "Ross", "Rachel", "Tom"}, 8},
surnameGen: &quickOneOf[string]{[]string{"Smith", "Hart", "Lauren", "Marry", "Simpson", "Geller", "Hanks", "Mercury"}, 8},
gitGen: &quickOnly[string]{"https://github.com/AminMal"},
langGen: &quickOneOf[string]{[]string{"Scala", "Rust", "Go", "Java", "Python"}, 5},
originGen: &quickOneOf[string]{[]string{"USA", "Iran", "Spain", "France"}, 4},
ageGen: &quickBetween[int]{16, 79},
experienceGen: &quickBetween[int]{1, 8},
}
var functional1 Gen[Programmer] = FlatMap(nameGen, func(name string) Gen[Programmer] {
return FlatMap(surnameGen, func(surname string) Gen[Programmer] {
return FlatMap(gitGen, func(git string) Gen[Programmer] {
return FlatMap(langGen, func(lang string) Gen[Programmer] {
return FlatMap(originGen, func(origin string) Gen[Programmer] {
return FlatMap(ageGen, func(age int) Gen[Programmer] {
return Map(experienceGen, func(experience int) Programmer {
return Programmer{
name, surname, git, lang, origin, age, experience,
}
})
})
})
})
})
})
})
var functional2 Gen[Programmer] = Map7(
nameGen, surnameGen, gitGen, langGen, originGen, ageGen, experienceGen,
func(name, surname, git, lang, origin string, age, experience int) Programmer {
return Programmer{name, surname, git, lang, origin, age, experience}
},
)
b.Run("gen-composition-7-fields", func(b *testing.B) {
for i := 0; i < b.N; i++ {
directGen.Generate()
}
})
b.Run("gen-composition-map/flatmap-7-fields", func(b *testing.B) {
for i := 0; i < b.N; i++ {
functional1.Generate()
}
})
b.Run("gen-composition-mapN-7-fields", func(b *testing.B) {
for i := 0; i < b.N; i++ {
functional2.Generate()
}
})
b.Run("quick-composition-7-fields", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = qg.Generate(random, 50).Interface().(Programmer)
}
})
}