-
Notifications
You must be signed in to change notification settings - Fork 40
/
Fuzz.elm
1862 lines (1465 loc) · 59 KB
/
Fuzz.elm
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Fuzz exposing
( Fuzzer, examples, labelExamples
, int, intRange, uniformInt, intAtLeast, intAtMost
, float, niceFloat, percentage, floatRange, floatAtLeast, floatAtMost
, char, asciiChar
, string, stringOfLength, stringOfLengthBetween, asciiString, asciiStringOfLength, asciiStringOfLengthBetween
, pair, triple
, list, listOfLength, listOfLengthBetween, shuffledList
, array, maybe, result
, bool, unit, order, weightedBool
, oneOf, oneOfValues, frequency, frequencyValues
, constant, invalid, filter, filterMap
, map, map2, map3, map4, map5, map6, map7, map8, andMap
, andThen, lazy, sequence, traverse
, fromGenerator
)
{-| This is a library of _fuzzers_ you can use to supply values to your fuzz
tests. You can typically pick out which ones you need according to their types.
A `Fuzzer a` knows how to create values of type `a`. It can create them randomly,
so that your test's expectations are run against many values. Fuzzers will often
generate edge cases likely to find bugs. If the fuzzer can make your test fail,
the test runner also knows how to "simplify" that failing input into more minimal
examples, some of which might also cause the tests to fail. In this way, fuzzers
can usually find the simplest input that reproduces a bug.
## Fuzzers
@docs Fuzzer, examples, labelExamples
## Number fuzzers
@docs int, intRange, uniformInt, intAtLeast, intAtMost
@docs float, niceFloat, percentage, floatRange, floatAtLeast, floatAtMost
## String-related fuzzers
@docs char, asciiChar
@docs string, stringOfLength, stringOfLengthBetween, asciiString, asciiStringOfLength, asciiStringOfLengthBetween
## Collection fuzzers
@docs pair, triple
@docs list, listOfLength, listOfLengthBetween, shuffledList
@docs array, maybe, result
## Other fuzzers
@docs bool, unit, order, weightedBool
## Choosing fuzzers
@docs oneOf, oneOfValues, frequency, frequencyValues
## Working with Fuzzers
@docs constant, invalid, filter, filterMap
@docs map, map2, map3, map4, map5, map6, map7, map8, andMap
@docs andThen, lazy, sequence, traverse
## Misc helpers
@docs fromGenerator
-}
import Array exposing (Array)
import Bitwise
import Char
import Dict exposing (Dict)
import Fuzz.Float
import Fuzz.Internal exposing (Fuzzer(..))
import GenResult exposing (GenResult(..))
import MicroDictExtra as Dict
import MicroListExtra as List
import PRNG exposing (PRNG(..))
import Random
import RandomRun
{-| The representation of fuzzers is opaque. Conceptually, a `Fuzzer a` consists
of a way to randomly generate values of type `a` in a way allowing the test runner
to simplify those values.
-}
type alias Fuzzer a =
Fuzz.Internal.Fuzzer a
{-| A fuzzer for the unit value. Unit is a type with only one value, commonly
used as a placeholder.
-}
unit : Fuzzer ()
unit =
constant ()
{-| A fuzzer for boolean values. It's useful when building up fuzzers of complex
types that contain a boolean somewhere.
We recommend against writing tests fuzzing over booleans. Write a unit test for
the true and false cases explicitly.
Simplifies in order `False < True`.
-}
bool : Fuzzer Bool
bool =
oneOfValues [ False, True ]
{-| A fuzzer for order values.
Simplifies in order `LT < EQ < GT`.
-}
order : Fuzzer Order
order =
oneOfValues [ LT, EQ, GT ]
intBucketingThreshold : Int
intBucketingThreshold =
255
intPreferences : List { weight : Int, bits : Int }
intPreferences =
[ { weight = 4, bits = 4 } -- 0..15
, { weight = 8, bits = 8 } -- 0..255
, { weight = 2, bits = 16 } -- 0..65535
, { weight = 1, bits = 32 } -- 0..4294967295
]
{-| A fuzzer for int values. It will never produce `NaN`, `Infinity`, or
`-Infinity`.
This fuzzer will generate values in the range `Random.minInt .. Random.maxInt`.
- Simplifies towards 0
- Prefers positive values over negative ones
- Prefers smaller values over larger ones
-}
int : Fuzzer Int
int =
intPreferences
|> List.map (\{ weight, bits } -> ( weight, intBits bits ))
|> intFrequency
|> map
(\n ->
let
isNegative =
Bitwise.and 1 n == 1
withoutFirstBit =
Bitwise.shiftRightBy 1 n
in
if isNegative then
negate withoutFirstBit
else
withoutFirstBit
)
{-| An unsigned integer taken uniformly from the range 0..(2^n)-1.
-}
intBits : Int -> Fuzzer Int
intBits bitsCount =
uniformInt ((2 ^ bitsCount) - 1)
{-| A fuzzer that will generate values in range n..2^32-1.
-}
intAtLeast : Int -> Fuzzer Int
intAtLeast n =
intRange n (2 ^ 32 - 1)
{-| A fuzzer that will generate values in range -(2^32-1)..n.
-}
intAtMost : Int -> Fuzzer Int
intAtMost n =
intRange -(2 ^ 32 - 1) n
{-| A fuzzer for int values between a given minimum and maximum value,
inclusive. Shrunk values will also be within the range.
-}
intRange : Int -> Int -> Fuzzer Int
intRange lo hi =
if hi < lo then
intRange hi lo
else if lo == hi then
constant lo
else
let
int_ : Int -> Fuzzer Int
int_ upperLimit =
{- Two variants:
1. If the range of numbers is low enough, we skip the
bucketing and just do uniform choice in the whole range.
Meaning `intRange INT_BELOW_THRESHOLD` will only draw
one integer.
2. If the range is above some threshold, we turn on the
bucketing and prefer smaller values.
Meaning `intRange INT_ABOVE_THRESHOLD` will draw
two integers: one for the bucket and one for the actual
integer inside.
-}
if upperLimit <= intBucketingThreshold then
{- TODO PERF: is
intBits bitsCount |> map (modBy (upperLimit + 1))
faster than
uniformInt upperLimit
?
-}
uniformInt upperLimit
else
let
range : Int
range =
upperLimit + 1
maxBits : Int
maxBits =
range
-- find how many bits the number takes
|> toFloat
|> logBase 2
|> ceiling
-- then find the next power of 2 (which is what our intPreferences are)
|> toFloat
|> logBase 2
|> ceiling
|> (\n -> 2 ^ n)
in
intPreferences
|> List.filter (\{ bits } -> bits <= maxBits)
|> (\list_ ->
{- failsafe for values taking less than 4 bits
(lowest bitcount in intPreferences)
-}
if List.isEmpty list_ then
List.take 1 intPreferences
else
list_
)
|> List.map (\{ weight, bits } -> ( weight, intBits bits ))
|> intFrequency
|> map (modBy range)
in
if lo >= 0 then
-- both non-negative
int_ (hi - lo)
{- intRange 2 5: uniformInt 3: 0,1,2,3
=> (+) 2 => 2,3,4,5
simplifying towards zero, not Inf
-}
|> map (\n -> n + lo)
else if hi <= 0 then
-- both negative
int_ (hi - lo)
{- intRange -5 -2: uniformInt 3: 0,1,2,3
=> negate => -0,-1,-2,-3
=> (+) -2 => -2,-3,-4,-5
simplifying towards zero, not -Inf
-}
|> map (\n -> negate n + hi)
else
{- somewhere in the middle, divide it into negative and positive ranges,
both of which will simplify towards zero. We prefer positive values.
-}
oneOf
[ intRange 0 hi -- the conditions above guarantee hi >= 1
, intRange lo -1 -- the conditions above guarantee lo <= -1
]
{-| A fuzzer for float values.
Will prefer integer values, nice fractions and positive numbers over the rest.
Will occasionally try infinities and NaN. If you don't want to generate these,
use [`Fuzz.niceFloat`](#niceFloat).
-}
float : Fuzzer Float
float =
intFrequency
[ {- Just to shrink nicely. The wellShrinkingFloat below needs 3 items
in the RandomRun so sometimes it's not an option anymore.
-}
( 1, constant 0 )
, ( 5, wellShrinkingFloat )
, ( 1, constant (1 / 0) )
, ( 1, constant (-1 / 0) )
, ( 1, constant (0 / 0) )
]
{-| A fuzzer for float values.
Will prefer integer values, nice fractions and positive numbers over the rest.
Will never try infinities or NaN.
-}
niceFloat : Fuzzer Float
niceFloat =
wellShrinkingFloat
{-| This float fuzzer will prefer non-fractional floats and (if it must) nice
fractions.
-}
wellShrinkingFloat : Fuzzer Float
wellShrinkingFloat =
map3
(\hi lo shouldNegate ->
let
f : Float
f =
Fuzz.Float.wellShrinkingFloat ( hi, lo )
in
if shouldNegate then
negate f
else
f
)
int32
int32
bool
|> filter (\n -> not (isInfinite n || isNaN n))
{-| Fuzzer generating floats in range `n..Infinity`.
The positive part of the range will shrink nicely, the negative part will shrink uniformly.
The fuzzer will occasionally try the minimum, 0 (if in range) and Infinity.
-}
floatAtLeast : Float -> Fuzzer Float
floatAtLeast n =
if n <= 0 then
intFrequency
[ ( 4, floatRange n 0 )
, ( 4, wellShrinkingFloat |> map abs )
, ( 2, constant n )
, ( 2, constant (1 / 0) )
, ( 1, constant 0 )
]
else
intFrequency
[ ( 8, wellShrinkingFloat |> map (\x -> n + abs x) )
, ( 2, constant n )
, ( 2, constant (1 / 0) )
]
{-| Fuzzer generating floats in range `-Infinity..n`.
The negative part of the range will shrink nicely, the positive part will shrink uniformly.
The fuzzer will occasionally try the maximum, 0 (if in range) and -Infinity.
-}
floatAtMost : Float -> Fuzzer Float
floatAtMost n =
if n >= 0 then
intFrequency
[ ( 4, floatRange 0 n )
, ( 4, wellShrinkingFloat |> map (negate << abs) )
, ( 2, constant n )
, ( 2, constant (-1 / 0) )
, ( 1, constant 0 )
]
else
intFrequency
[ ( 8, wellShrinkingFloat |> map (\x -> n - abs x) )
, ( 2, constant n )
, ( 2, constant (-1 / 0) )
]
{-| A fuzzer for float values within between a given minimum and maximum (inclusive).
Shrunken values will also be within the range.
-}
floatRange : Float -> Float -> Fuzzer Float
floatRange lo hi =
if hi < lo then
floatRange hi lo
else if lo == hi then
constant lo
else if lo >= 0 then
-- both non-negative
intFrequency
[ ( 1, constant lo )
, ( 1, constant hi )
, ( 4, scaledFloat lo hi )
]
else if hi <= 0 then
-- both negative
intFrequency
[ ( 1, constant lo )
, ( 1, constant hi )
, ( 4
, scaledFloat (negate hi) (negate lo)
-- simplify towards zero
|> map negate
)
]
else
{- somewhere in the middle, divide it into negative and positive ranges,
both of which will simplify towards zero. We prefer positive values.
-}
intFrequency
[ ( 1, constant 0 )
, ( 2, constant lo )
, ( 2, constant hi )
, ( 4, scaledFloat 0 (negate lo) |> map negate )
, ( 4, scaledFloat 0 hi )
]
{-| This float fuzzer won't shrink nicely (to integers or nice fractions). For
that, use `wellShrinkingFloat`.
-}
scaledFloat : Float -> Float -> Fuzzer Float
scaledFloat lo hi =
if lo == hi then
constant lo
else if lo > hi then
scaledFloat hi lo
else
percentage
|> map (\f -> f * (hi - lo) + lo)
{-| A fuzzer for percentage values. Generates random floats between `0.0`
inclusive and `1.0` exclusive, in an uniform fashion.
Will occasionally try the boundaries.
Doesn't shrink to nice values like [`Fuzz.float`](#float) does; shrinks towards
zero.
-}
percentage : Fuzzer Float
percentage =
{- We can't use Random.Generators here as all fuzzed values must be
representable as one or more ints. We generally use a pair of 32bit ints
to represent a 64bit float.
Here we know the top 12 bits of the high int wouldn't be used for the
mantissa calculations so we don't bother generating those.
-}
intFrequency
[ ( 1, constant 0 )
, ( 1, constant Fuzz.Float.maxFractionalFloat ) -- just barely below 1
, ( 4
, pair (uniformInt 0x000FFFFF) int32
|> map Fuzz.Float.fractionalFloat
)
]
int32 : Fuzzer Int
int32 =
uniformInt 0xFFFFFFFF
{-| A fuzzer for simple ASCII char values (range 32..126).
Skips control characters and the extended character set.
For more serious char fuzzing look at [`Fuzz.char`](#char) which generates the
whole Unicode range.
-}
asciiChar : Fuzzer Char
asciiChar =
-- TODO: what about preferring nasty chars like \, /, $, @ (interpolation, SQL injections, ...)?
intRange 32 126
|> map Char.fromCode
{-| A fuzzer for arbitrary Unicode char values.
Avoids surrogate pairs or their components (`0xD800..0xDFFF`).
Will prefer ASCII characters, whitespace, and some examples known to cause
trouble, like combining diacritics marks and emojis.
-}
char : Fuzzer Char
char =
let
whitespaceChar : Fuzzer Char
whitespaceChar =
oneOfValues
[ ' '
, '\t'
, '\n'
]
combiningDiacriticalMarkChar : Fuzzer Char
combiningDiacriticalMarkChar =
-- going a roundabout way about this to keep Vim from being confused about unclosed strings
oneOfValues
[ Char.fromCode 0x0302 -- combining circumflex accent
, Char.fromCode 0x0303 -- combining tilde
, Char.fromCode 0x0308 -- combining diaeresis
]
emojiChar : Fuzzer Char
emojiChar =
oneOfValues
[ '🌈'
, '❤'
, '🔥'
]
{- Note: This can produce garbage values as Unicode doesn't use all valid values.
0xD800..0xDFFF are surrogate code units and would break tests like
`(str |> reverse |> reverse) == str` because of being converted to
0xFFFD REPLACEMENT CHARACTER.
-}
arbitraryUnicodeChar : Fuzzer Char
arbitraryUnicodeChar =
intRange 0 0x0010FFFF
|> filter (\n -> not (n >= 0xD800 && n <= 0xDFFF))
|> map Char.fromCode
in
intFrequency
[ ( 5, asciiChar )
, ( 2, whitespaceChar )
, ( 1, combiningDiacriticalMarkChar )
, ( 1, emojiChar )
, ( 1, arbitraryUnicodeChar )
]
{-| Generates random unicode strings of up to 10 characters.
-}
string : Fuzzer String
string =
stringOfLengthBetween 0 10
{-| Generates random unicode strings of a given length.
Note that some unicode characters have `String.length` of 2. This fuzzer will
make sure the `String.length` of the returned string is equal to the wanted
length, even if it will mean there are less characters. If you instead want it
to give N characters even if their `String.length` will be above N, you can use
Fuzz.listOfLength n Fuzz.char
|> Fuzz.map String.fromList
-}
stringOfLength : Int -> Fuzzer String
stringOfLength n =
stringOfLengthBetween n n
{-| Generates random unicode strings of length between the given limits.
Note that some unicode characters have `String.length` of 2. This fuzzer will
make sure the `String.length` of the returned string is equal to the wanted
length, even if it will mean there are less characters. If you instead want it
to give between MIN and MAX characters even if their `String.length` will be
above MAX, you can use
Fuzz.listOfLengthBetween min max Fuzz.char
|> Fuzz.map String.fromList
-}
stringOfLengthBetween : Int -> Int -> Fuzzer String
stringOfLengthBetween min max =
if min > max then
stringOfLengthBetween max min
else if max <= 0 then
constant ""
else
listOfLengthBetween min max char
|> map String.fromList
|> filter
(\str ->
let
length =
String.length str
in
length >= min && length <= max
)
{-| Generates random ASCII strings of up to 10 characters.
-}
asciiString : Fuzzer String
asciiString =
asciiStringOfLengthBetween 0 10
{-| Generates random ASCII strings of a given length.
-}
asciiStringOfLength : Int -> Fuzzer String
asciiStringOfLength n =
asciiStringOfLengthBetween n n
{-| Generates random ASCII strings of length between the given limits.
-}
asciiStringOfLengthBetween : Int -> Int -> Fuzzer String
asciiStringOfLengthBetween min max =
listOfLengthBetween min max asciiChar
|> map String.fromList
{-| Given a fuzzer of a type, create a fuzzer of a maybe for that type.
-}
maybe : Fuzzer a -> Fuzzer (Maybe a)
maybe fuzzer =
intFrequency
[ ( 1, constant Nothing )
, ( 3, map Just fuzzer )
]
{-| Given fuzzers for an error type and a success type, create a fuzzer for
a result.
-}
result : Fuzzer error -> Fuzzer value -> Fuzzer (Result error value)
result fuzzerError fuzzerValue =
intFrequency
[ ( 1, map Err fuzzerError )
, ( 3, map Ok fuzzerValue )
]
{-| Given a fuzzer of a type, create a fuzzer of a list of that type.
Generates random lists of varying length, up to 32 elements.
-}
list : Fuzzer a -> Fuzzer (List a)
list fuzzer =
listOfLengthBetween 0 32 fuzzer
{-| Given a fuzzer of a type, create a fuzzer of a list of that type.
Generates random lists of exactly the specified length.
-}
listOfLength : Int -> Fuzzer a -> Fuzzer (List a)
listOfLength n fuzzer =
listOfLengthBetween n n fuzzer
{-| Given a fuzzer of a type, create a fuzzer of a list of that type.
Generates random lists of length between the two given integers.
-}
listOfLengthBetween : Int -> Int -> Fuzzer a -> Fuzzer (List a)
listOfLengthBetween lo hi itemFuzzer =
if lo > hi then
-- the name allows for it, even if it's a little weird
listOfLengthBetween hi lo itemFuzzer
else if hi <= 0 then
constant []
else
let
average : Float
average =
toFloat lo + toFloat hi / 2
continueProbability : Float
continueProbability =
{- Taken from Python Hypothesis library (ListStrategy).
It should supposedly be a geometric distribution, although I
don't see the connection from the below formula. ~janiczek
-}
1 - 1 / (1 + average)
addItem : Int -> List a -> Fuzzer (List a)
addItem length acc =
itemFuzzer
|> andThen
(\item ->
go (length + 1) (item :: acc)
)
end : List a -> Fuzzer (List a)
end acc =
constant (List.reverse acc)
go : Int -> List a -> Fuzzer (List a)
go length acc =
if length < lo then
forcedChoice 1
|> andThen (\_ -> addItem length acc)
else if length == hi then
forcedChoice 0
|> andThen (\_ -> end acc)
else
weightedBool continueProbability
|> andThen
(\oneMorePlease ->
if oneMorePlease then
addItem length acc
else
end acc
)
in
go 0 []
{-| Given a fuzzer of a type, create a fuzzer of an array of that type.
Generates random arrays of varying length, favoring shorter arrays.
-}
array : Fuzzer a -> Fuzzer (Array a)
array fuzzer =
map Array.fromList (list fuzzer)
{-| Create a fuzzer of pairs from two fuzzers.
-}
pair : Fuzzer a -> Fuzzer b -> Fuzzer ( a, b )
pair fuzzerA fuzzerB =
map2 (\a b -> ( a, b )) fuzzerA fuzzerB
{-| Create a fuzzer of triples from three fuzzers.
-}
triple : Fuzzer a -> Fuzzer b -> Fuzzer c -> Fuzzer ( a, b, c )
triple fuzzerA fuzzerB fuzzerC =
map3 (\a b c -> ( a, b, c )) fuzzerA fuzzerB fuzzerC
{-| Create a fuzzer that only and always returns the value provided, and performs
no simplifying. This is hardly random, and so this function is best used as a
helper when creating more complicated fuzzers.
-}
constant : a -> Fuzzer a
constant x =
Fuzzer <|
\prng ->
Generated
{ value = x
, prng = prng
}
{-| Map a function over a fuzzer.
-}
map : (a -> b) -> Fuzzer a -> Fuzzer b
map fn (Fuzzer fuzzer) =
Fuzzer <|
\prng ->
case fuzzer prng of
Generated g ->
Generated
{ value = fn g.value
, prng = g.prng
}
Rejected r ->
Rejected r
{-| Map over two fuzzers.
-}
map2 : (a -> b -> c) -> Fuzzer a -> Fuzzer b -> Fuzzer c
map2 fn (Fuzzer fuzzerA) (Fuzzer fuzzerB) =
Fuzzer <|
\prng ->
case fuzzerA prng of
Generated a ->
case fuzzerB a.prng of
Generated b ->
Generated
{ value = fn a.value b.value
, prng = b.prng
}
Rejected r ->
Rejected r
Rejected r ->
Rejected r
{-| Map over three fuzzers.
-}
map3 : (a -> b -> c -> d) -> Fuzzer a -> Fuzzer b -> Fuzzer c -> Fuzzer d
map3 fn (Fuzzer fuzzerA) (Fuzzer fuzzerB) (Fuzzer fuzzerC) =
Fuzzer <|
\prng ->
case fuzzerA prng of
Generated a ->
case fuzzerB a.prng of
Generated b ->
case fuzzerC b.prng of
Generated c ->
Generated
{ value = fn a.value b.value c.value
, prng = c.prng
}
Rejected r ->
Rejected r
Rejected r ->
Rejected r
Rejected r ->
Rejected r
{-| Map over four fuzzers.
-}
map4 : (a -> b -> c -> d -> e) -> Fuzzer a -> Fuzzer b -> Fuzzer c -> Fuzzer d -> Fuzzer e
map4 fn (Fuzzer fuzzerA) (Fuzzer fuzzerB) (Fuzzer fuzzerC) (Fuzzer fuzzerD) =
Fuzzer <|
\prng ->
case fuzzerA prng of
Generated a ->
case fuzzerB a.prng of
Generated b ->
case fuzzerC b.prng of
Generated c ->
case fuzzerD c.prng of
Generated d ->
Generated
{ value = fn a.value b.value c.value d.value
, prng = d.prng
}
Rejected r ->
Rejected r
Rejected r ->
Rejected r
Rejected r ->
Rejected r
Rejected r ->
Rejected r
{-| Map over five fuzzers.
-}
map5 : (a -> b -> c -> d -> e -> f) -> Fuzzer a -> Fuzzer b -> Fuzzer c -> Fuzzer d -> Fuzzer e -> Fuzzer f
map5 fn (Fuzzer fuzzerA) (Fuzzer fuzzerB) (Fuzzer fuzzerC) (Fuzzer fuzzerD) (Fuzzer fuzzerE) =
Fuzzer <|
\prng ->
case fuzzerA prng of
Generated a ->
case fuzzerB a.prng of
Generated b ->
case fuzzerC b.prng of
Generated c ->
case fuzzerD c.prng of
Generated d ->
case fuzzerE d.prng of
Generated e ->
Generated
{ value = fn a.value b.value c.value d.value e.value
, prng = e.prng
}
Rejected r ->
Rejected r
Rejected r ->
Rejected r
Rejected r ->
Rejected r
Rejected r ->
Rejected r
Rejected r ->
Rejected r
{-| Map over six fuzzers.
-}
map6 : (a -> b -> c -> d -> e -> f -> g) -> Fuzzer a -> Fuzzer b -> Fuzzer c -> Fuzzer d -> Fuzzer e -> Fuzzer f -> Fuzzer g
map6 fn (Fuzzer fuzzerA) (Fuzzer fuzzerB) (Fuzzer fuzzerC) (Fuzzer fuzzerD) (Fuzzer fuzzerE) (Fuzzer fuzzerF) =
Fuzzer <|
\prng ->
case fuzzerA prng of
Generated a ->
case fuzzerB a.prng of
Generated b ->
case fuzzerC b.prng of
Generated c ->
case fuzzerD c.prng of
Generated d ->
case fuzzerE d.prng of
Generated e ->
case fuzzerF e.prng of
Generated f ->
Generated
{ value = fn a.value b.value c.value d.value e.value f.value
, prng = f.prng
}
Rejected r ->
Rejected r
Rejected r ->
Rejected r
Rejected r ->
Rejected r
Rejected r ->
Rejected r
Rejected r ->
Rejected r
Rejected r ->
Rejected r
{-| Map over seven fuzzers.
-}
map7 : (a -> b -> c -> d -> e -> f -> g -> h) -> Fuzzer a -> Fuzzer b -> Fuzzer c -> Fuzzer d -> Fuzzer e -> Fuzzer f -> Fuzzer g -> Fuzzer h
map7 fn (Fuzzer fuzzerA) (Fuzzer fuzzerB) (Fuzzer fuzzerC) (Fuzzer fuzzerD) (Fuzzer fuzzerE) (Fuzzer fuzzerF) (Fuzzer fuzzerG) =
Fuzzer <|
\prng ->
case fuzzerA prng of
Generated a ->
case fuzzerB a.prng of
Generated b ->
case fuzzerC b.prng of
Generated c ->
case fuzzerD c.prng of
Generated d ->
case fuzzerE d.prng of
Generated e ->
case fuzzerF e.prng of
Generated f ->
case fuzzerG f.prng of
Generated g ->
Generated
{ value = fn a.value b.value c.value d.value e.value f.value g.value
, prng = g.prng
}
Rejected r ->
Rejected r
Rejected r ->
Rejected r
Rejected r ->
Rejected r
Rejected r ->