forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGen.java
More file actions
650 lines (603 loc) · 22.8 KB
/
Gen.java
File metadata and controls
650 lines (603 loc) · 22.8 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
package fj.test;
import static fj.Bottom.error;
import fj.Effect;
import fj.F;
import fj.Function;
import static fj.Function.flip;
import static fj.Function.curry;
import fj.P2;
import static fj.P2.__1;
import fj.Unit;
import fj.F2;
import static fj.data.Array.array;
import fj.data.List;
import static fj.data.List.nil;
import static fj.data.List.replicate;
import fj.data.Option;
import static fj.Monoid.intAdditionMonoid;
import static fj.Ord.intOrd;
import static java.lang.Math.max;
import static java.lang.Math.min;
/**
* <p> A generator for values of the type of the given type parameter (<code>A</code>). Generation
* of a value accepts a general 'size' argument (integer), a {@link Rand random generator} and
* returns an {@link Option optional value} of the type parameter. Several generators are provided,
* allowing various forms of composition of generators. </p> <p> A user typically creates an {@link
* Arbitrary arbitrary} to return a generator using the 'combinator methods' below. For example,
* suppose a <code>class Person</code>:
<pre>
class Person {
final int age;
final String name;
final boolean male;
Person(final int age, final String name, final boolean male) {
this.age = age;
this.name = name;
this.male = male;
}
}
</pre>
* </p> <p> In a case like this one, a user may create a generator over <code>Person</code> by
* invoking the {@link #bind(F)} methods — in this case, {@link #bind(Gen , Gen , F)} the one
* that takes two generator arguments}, since the class has one more than two fields (the bind
* method is invoked on a generator adding the extra one to the count as they are composed). The
* class fields are of types for which there exist generators (on {@link Arbitrary} so those can be
* used to compose a generator for <code>Person</code>: </p>
<pre>
static Arbitrary<Person> personArbitrary() {
final Gen<Person> personGenerator = arbInteger.gen.bind(arbString().gen, arbBoolean().gen,
// compose the generators
{int age => {String name => {boolean male => new Person(age, name, male)}}};
return arbitrary(personGenerator);
}
</pre>
* <p/>
* The example above uses Java 7 closure syntax. Here is the same example using objects instead:
<pre>
static Arbitrary<Person> personArbitrary() {
final Gen<Person> personGenerator = arbInteger.gen.bind(arbString.gen, arbBoolean.gen,
// compose the generators
new F<Integer, F<String, F<Boolean, Person>>>() {
public F<String, F<Boolean, Person>> f(final Integer age) {
return new F<String, F<Boolean, Person>>() {
public F<Boolean, Person> f(final String name) {
return new F<Boolean, Person>() {
public Person f(final Boolean male) {
return new Person(age, name, male);
}
};
}
};
}
});
return arbitrary(personGenerator);
}
</pre>
*
* @version %build.number%
*/
public final class Gen<A> {
private final F<Integer, F<Rand, A>> f;
private Gen(final F<Integer, F<Rand, A>> f) {
this.f = f;
}
/**
* Applies the given size and random generator to produce a value.
*
* @param i The size to use to produce the value.
* @param r The random generator to use to produce the value..
* @return A generated value.
*/
public A gen(final int i, final Rand r) {
return f.f(i).f(r);
}
/**
* Maps the given function across this generator.
*
* @param f The function to map across this generator.
* @return A new generator after applying the mapping function.
*/
public <B> Gen<B> map(final F<A, B> f) {
return new Gen<B>(new F<Integer, F<Rand, B>>() {
public F<Rand, B> f(final Integer i) {
return new F<Rand, B>() {
public B f(final Rand r) {
return f.f(gen(i, r));
}
};
}
});
}
/**
* Returns a generator that produces values that meet the given predicate.
*
* @param f The predicate to meet for the values produced by the generator.
* @return A generator that produces values that meet the given predicate.
*/
public Gen<A> filter(final F<A, Boolean> f) {
return gen(curry(new F2<Integer, Rand, A>() {
public A f(final Integer i, final Rand r) {
A a;
do {
a = gen(i, r);
} while(!f.f(a));
return a;
}
}));
}
/**
* Executes a side-effect for each generated result using the given arguments.
*
* @param i The size to generate the result to apply the side-effect to.
* @param r The random generator to generate the result to apply the side-effect to.
* @param f The side-effect to execute on the generated value.
* @return The unit value.
*/
public Unit foreach(final Integer i, final Rand r, final F<A, Unit> f) {
return f.f(this.f.f(i).f(r));
}
/**
* Executes a side-effect for each generated result using the given arguments.
*
* @param i The size to generate the result to apply the side-effect to.
* @param r The random generator to generate the result to apply the side-effect to.
* @param f The side-effect to execute on the generated value.
*/
public void foreach(final Integer i, final Rand r, final Effect<A> f) {
f.e(this.f.f(i).f(r));
}
/**
* Binds the given function across this generator to produce a new generator.
*
* @param f The function to bind across this generator.
* @return A new generator after binding the given function.
*/
public <B> Gen<B> bind(final F<A, Gen<B>> f) {
return new Gen<B>(new F<Integer, F<Rand, B>>() {
public F<Rand, B> f(final Integer i) {
return new F<Rand, B>() {
public B f(final Rand r) {
return f.f(gen(i, r)).f.f(i).f(r);
}
};
}
});
}
/**
* Binds the given function across this generator and the given generator to produce a new
* generator.
*
* @param gb The second generator to bind the given function across.
* @param f The function to bind across this generator and the given generator.
* @return A new generator after binding the given function.
*/
public <B, C> Gen<C> bind(final Gen<B> gb, final F<A, F<B, C>> f) {
return gb.apply(map(f));
}
/**
* Binds the given function across this generator and the given generators to produce a new
* generator.
*
* @param gb The second generator to bind the given function across.
* @param gc The third generator to bind the given function across.
* @param f The function to bind across this generator and the given generators.
* @return A new generator after binding the given function.
*/
public <B, C, D> Gen<D> bind(final Gen<B> gb, final Gen<C> gc, final F<A, F<B, F<C, D>>> f) {
return gc.apply(bind(gb, f));
}
/**
* Binds the given function across this generator and the given generators to produce a new
* generator.
*
* @param gb The second generator to bind the given function across.
* @param gc The third generator to bind the given function across.
* @param gd The fourth generator to bind the given function across.
* @param f The function to bind across this generator and the given generators.
* @return A new generator after binding the given function.
*/
public <B, C, D, E> Gen<E> bind(final Gen<B> gb, final Gen<C> gc, final Gen<D> gd, final F<A, F<B, F<C, F<D, E>>>> f) {
return gd.apply(bind(gb, gc, f));
}
/**
* Binds the given function across this generator and the given generators to produce a new
* generator.
*
* @param gb The second generator to bind the given function across.
* @param gc The third generator to bind the given function across.
* @param gd The fourth generator to bind the given function across.
* @param ge The fifth generator to bind the given function across.
* @param f The function to bind across this generator and the given generators.
* @return A new generator after binding the given function.
*/
public <B, C, D, E, F$> Gen<F$> bind(final Gen<B> gb, final Gen<C> gc, final Gen<D> gd, final Gen<E> ge, final F<A, F<B, F<C, F<D, F<E, F$>>>>> f) {
return ge.apply(bind(gb, gc, gd, f));
}
/**
* Binds the given function across this generator and the given generators to produce a new
* generator.
*
* @param gb The second generator to bind the given function across.
* @param gc The third generator to bind the given function across.
* @param gd The fourth generator to bind the given function across.
* @param ge The fifth generator to bind the given function across.
* @param gf The sixth generator to bind the given function across.
* @param f The function to bind across this generator and the given generators.
* @return A new generator after binding the given function.
*/
public <B, C, D, E, F$, G> Gen<G> bind(final Gen<B> gb, final Gen<C> gc, final Gen<D> gd, final Gen<E> ge, final Gen<F$> gf, final F<A, F<B, F<C, F<D, F<E, F<F$, G>>>>>> f) {
return gf.apply(bind(gb, gc, gd, ge, f));
}
/**
* Binds the given function across this generator and the given generators to produce a new
* generator.
*
* @param gb The second generator to bind the given function across.
* @param gc The third generator to bind the given function across.
* @param gd The fourth generator to bind the given function across.
* @param ge The fifth generator to bind the given function across.
* @param gf The sixth generator to bind the given function across.
* @param gg The seventh generator to bind the given function across.
* @param f The function to bind across this generator and the given generators.
* @return A new generator after binding the given function.
*/
public <B, C, D, E, F$, G, H> Gen<H> bind(final Gen<B> gb, final Gen<C> gc, final Gen<D> gd, final Gen<E> ge, final Gen<F$> gf, final Gen<G> gg, final F<A, F<B, F<C, F<D, F<E, F<F$, F<G, H>>>>>>> f) {
return gg.apply(bind(gb, gc, gd, ge, gf, f));
}
/**
* Binds the given function across this generator and the given generators to produce a new
* generator.
*
* @param gb The second generator to bind the given function across.
* @param gc The third generator to bind the given function across.
* @param gd The fourth generator to bind the given function across.
* @param ge The fifth generator to bind the given function across.
* @param gf The sixth generator to bind the given function across.
* @param gg The seventh generator to bind the given function across.
* @param gh The eighth generator to bind the given function across.
* @param f The function to bind across this generator and the given generators.
* @return A new generator after binding the given function.
*/
public <B, C, D, E, F$, G, H, I> Gen<I> bind(final Gen<B> gb, final Gen<C> gc, final Gen<D> gd, final Gen<E> ge, final Gen<F$> gf, final Gen<G> gg, final Gen<H> gh, final F<A, F<B, F<C, F<D, F<E, F<F$, F<G, F<H, I>>>>>>>> f) {
return gh.apply(bind(gb, gc, gd, ge, gf, gg, f));
}
/**
* Function application within this generator to produce a new generator.
*
* @param gf The generator over the function to apply to this generator.
* @return A new generator after function application.
*/
public <B> Gen<B> apply(final Gen<F<A, B>> gf) {
return gf.bind(new F<F<A, B>, Gen<B>>() {
public Gen<B> f(final F<A, B> f) {
return map(new F<A, B>() {
public B f(final A a) {
return f.f(a);
}
});
}
});
}
/**
* Resizes this generator with the given size.
*
* @param s The new size of the generator.
* @return A new generator that uses the given size.
*/
public Gen<A> resize(final int s) {
return new Gen<A>(new F<Integer, F<Rand, A>>() {
public F<Rand, A> f(final Integer i) {
return new F<Rand, A>() {
public A f(final Rand r) {
return f.f(s).f(r);
}
};
}
});
}
/**
* Returns a generator that uses the given function.
*
* @param f The function to use for this generator.
* @return A new generator that uses the given function.
*/
public static <A> Gen<A> gen(final F<Integer, F<Rand, A>> f) {
return new Gen<A>(f);
}
/**
* Sequence the given generators through a {@link #bind(F)} operation.
*
* @param gs The generators to sequence.
* @return A generator of lists after sequencing the given generators.
*/
public static <A> Gen<List<A>> sequence(final List<Gen<A>> gs) {
return gs.foldRight(new F<Gen<A>, F<Gen<List<A>>, Gen<List<A>>>>() {
public F<Gen<List<A>>, Gen<List<A>>> f(final Gen<A> ga) {
return new F<Gen<List<A>>, Gen<List<A>>>() {
public Gen<List<A>> f(final Gen<List<A>> gas) {
return ga.bind(gas, List.<A>cons());
}
};
}
}, value(List.<A>nil()));
}
/**
* Sequences the given generator the given number of times through a {@link #bind(F)} operation.
*
* @param n The number of times to sequence the given generator.
* @param g The generator sequence.
* @return A generator of lists after sequencing the given generator.
*/
public static <A> Gen<List<A>> sequenceN(final int n, final Gen<A> g) {
return sequence(replicate(n, g));
}
/**
* Constructs a generator that can access its construction arguments — size and random
* generator.
*
* @param f The function that constructs the generator with its arguments.
* @return A new generator.
*/
public static <A> Gen<A> parameterised(final F<Integer, F<Rand, Gen<A>>> f) {
return new Gen<A>(curry(new F2<Integer, Rand, A>() {
public A f(final Integer i, final Rand r) {
return f.f(i).f(r).gen(i, r);
}
}));
}
/**
* Constructs a generator that can access its size construction arguments.
*
* @param f The function that constructs the generator with its size argument.
* @return A new generator.
*/
public static <A> Gen<A> sized(final F<Integer, Gen<A>> f) {
return parameterised(flip(Function.<Rand, F<Integer, Gen<A>>>constant(f)));
}
/**
* Returns a generator that always produces the given value.
*
* @param a The value to always produce.
* @return A generator that always produces the given value.
*/
public static <A> Gen<A> value(final A a) {
return new Gen<A>(new F<Integer, F<Rand, A>>() {
public F<Rand, A> f(final Integer i) {
return new F<Rand, A>() {
public A f(final Rand r) {
return a;
}
};
}
});
}
/**
* Returns a generator that produces values between the given range (inclusive).
*
* @param from The value for the generator to produce values from.
* @param to The value for the generator to produce values from.
* @return A generator that produces values between the given range (inclusive).
*/
public static Gen<Integer> choose(final int from, final int to) {
final int f = min(from, to);
final int t = max(from, to);
return parameterised(curry(new F2<Integer, Rand, Gen<Integer>>() {
public Gen<Integer> f(final Integer i, final Rand r) {
return value(r.choose(f, t));
}
}));
}
/**
* Returns a generator that produces values between the given range (inclusive).
*
* @param from The value for the generator to produce values from.
* @param to The value for the generator to produce values from.
* @return A generator that produces v
*/
public static Gen<Double> choose(final double from, final double to) {
final double f = min(from, to);
final double t = max(from, to);
return parameterised(new F<Integer, F<Rand, Gen<Double>>>() {
public F<Rand, Gen<Double>> f(final Integer i) {
return new F<Rand, Gen<Double>>() {
public Gen<Double> f(final Rand r) {
return value(r.choose(f, t));
}
};
}
});
}
/**
* Returns a generator that never returns a value.
*
* @return A generator that never returns a value.
*/
public static <A> Gen<A> fail() {
return new Gen<A>(new F<Integer, F<Rand, A>>() {
public F<Rand, A> f(final Integer i) {
return new F<Rand, A>() {
public A f(final Rand r) {
throw error("Failing generator");
}
};
}
});
}
/**
* Joins the generator of generators through a {@link #bind(F)} operation.
*
* @param g The generator of generators to join.
* @return A new generator after joining the given generator.
*/
public static <A> Gen<A> join(final Gen<Gen<A>> g) {
return g.bind(Function.<Gen<A>>identity());
}
/**
* Returns a generator that uses values from the given frequency and generator pairs. The returned
* generator will produce values from the generator in a pair with a higher frequency than a lower
* frequency generator.
*
* @param gs The pairs of frequency and generator from which to return values in the returned
* generator.
* @return A new generator that uses the given pairs of frequency and generator.
*/
public static <A> Gen<A> frequency(final List<P2<Integer, Gen<A>>> gs) {
final class Pick {
<A> Gen<A> pick(final int n, final List<P2<Integer, Gen<A>>> gs) {
if(gs.isEmpty())
return fail();
else {
final int k = gs.head()._1();
return n <= k ? gs.head()._2() : pick(n - k, gs.tail());
}
}
}
final F<P2<Integer, Gen<A>>, Integer> f = __1();
return choose(1, intAdditionMonoid.sumLeft(gs.map(f))).bind(new F<Integer, Gen<A>>() {
public Gen<A> f(final Integer i) {
return new Pick().pick(i, gs);
}
});
}
/**
* Returns a generator that produces values from the given frequency and value pairs. The returned
* generator will produce the value with a higher frequency than a lower one.
*
* @param as The pairs of frequency and value from which to produce values.
* @return A new generator that uses the given pairs of frequency and value.
*/
public static <A> Gen<A> elemFrequency(final List<P2<Integer, A>> as) {
return frequency(as.map(new F<P2<Integer, A>, P2<Integer, Gen<A>>>() {
public P2<Integer, Gen<A>> f(final P2<Integer, A> p) {
return p.map2(new F<A, Gen<A>>() {
public Gen<A> f(final A a) {
return value(a);
}
});
}
}));
}
/**
* Returns a generator that produces values from the given arguments.
*
* @param as The values that the returned generator may produce.
* @return A generator that produces values from the given arguments.
*/
public static <A> Gen<A> elements(final A... as) {
return array(as).isEmpty() ? Gen.<A>fail() : choose(0, as.length - 1).map(new F<Integer, A>() {
public A f(final Integer i) {
return as[i];
}
});
}
/**
* Returns a generator that produces values from one of the given generators on subsequent
* requests.
*
* @param gs The list of generators to produce a value from.
* @return A generator that produces values from one of the given generators on subsequent
* requests.
*/
public static <A> Gen<A> oneOf(final List<Gen<A>> gs) {
return gs.isEmpty() ? Gen.<A>fail() : choose(0, gs.length() - 1).bind(new F<Integer, Gen<A>>() {
public Gen<A> f(final Integer i) {
return gs.index(i);
}
});
}
/**
* Returns a generator of lists whose values come from the given generator.
*
* @param g The generator to produce values from for the returned generator.
* @param x An adjuster of size to apply to the given generator when producing values.
* @return A generator of lists whose values come from the given generator.
*/
public static <A> Gen<List<A>> listOf(final Gen<A> g, final int x) {
return sized(new F<Integer, Gen<List<A>>>() {
public Gen<List<A>> f(final Integer size) {
return choose(x, size).bind(new F<Integer, Gen<List<A>>>() {
public Gen<List<A>> f(final Integer n) {
return sequenceN(n, g);
}
});
}
});
}
/**
* Returns a generator of lists whose values come from the given generator.
*
* @param g The generator to produce values from for the returned generator.
* @return A generator of lists whose values come from the given generator.
*/
public static <A> Gen<List<A>> listOf(final Gen<A> g) {
return listOf(g, 0);
}
/**
* Returns a generator of lists whose values come from the given generator.
*
* @param g The generator to produce values from for the returned generator.
* @return A generator of lists whose values come from the given generator.
*/
public static <A> Gen<List<A>> listOf1(final Gen<A> g) {
return listOf(g, 1);
}
/**
* Returns a generator of lists that picks the given number of elements from the given list. If
* the given number is less than zero or greater than the length of the given list, then the
* returned generator will never produce a value.
*
* @param n The number of elements to pick from the given list.
* @param as The list from which to pick elements.
* @return A generator of lists that picks the given number of elements from the given list.
*/
public static <A> Gen<List<A>> pick(final int n, final List<A> as) {
return n < 0 || n > as.length() ? Gen.<List<A>>fail() : sequenceN(n, choose(0, as.length() - 1)).map(new F<List<Integer>, List<A>>() {
public List<A> f(final List<Integer> is) {
List<A> r = nil();
List<Integer> iis = is.sort(intOrd);
List<P2<A, Integer>> aas = as.zipIndex();
//noinspection ForLoopWithMissingComponent
for(; iis.isNotEmpty() && aas.isNotEmpty(); aas = aas.tail())
if(iis.head().equals(aas.head()._2()))
iis = iis.tail();
else
r = r.snoc(aas.head()._1());
return r;
}
});
}
/**
* Returns a generator of lists that produces some of the values of the given list.
*
* @param as The list from which to pick values.
* @return A generator of lists that produces some of the values of the given list.
*/
public static <A> Gen<List<A>> someOf(final List<A> as) {
return choose(0, as.length()).bind(new F<Integer, Gen<List<A>>>() {
public Gen<List<A>> f(final Integer i) {
return pick(i, as);
}
});
}
/**
* Promotes the given function to a generator for functions.
*
* @param f The function to promote to a generator of functions.
* @return A generator for functions.
*/
public static <A, B> Gen<F<A, B>> promote(final F<A, Gen<B>> f) {
return new Gen<F<A, B>>(new F<Integer, F<Rand, F<A, B>>>() {
public F<Rand, F<A, B>> f(final Integer i) {
return new F<Rand, F<A, B>>() {
public F<A, B> f(final Rand r) {
return new F<A, B>() {
public B f(final A a) {
return f.f(a).f.f(i).f(r);
}
};
}
};
}
});
}
}