package alcobar
Module AlcobarSource
Types
'a gen knows how to generate 'a for use in Alcobar tests.
Multiple generators are passed to functions using a listlike syntax, for example map [int; int] (fun a b -> a + b).
pretty-printers for items generated by Alcobar; useful for the user in translating test failures into bugfixes.
Generators
Simple Generators
int generates an integer ranging from min_int to max_int, inclusive. If you need integers from a smaller domain, consider using range.
uint16 generates an unsigned 16-bit integer, ranging from 0 to 65535 inclusive.
int16 generates a signed 16-bit integer, ranging from -32768 to 32767 inclusive.
bytes generates a string of arbitrary length (including zero-length strings).
bytes_fixed length generates a string of the specified length.
range ?min n is a generator for integers between min (inclusive) and min + n (exclusive). Default min value is 0. range ?min n will raise Invalid_argument for n <= 0.
Functions on Generators
map gens map_fn provides a means for creating generators using other generators' output. For example, one might generate a Char.t from a uint8:
open Alcobar
let char_gen : Char.t gen = map [ uint8 ] Char.chrunlazy gen forces the generator gen. It is useful when defining generators for recursive data types:
open Alcobar
type a = A of int | Self of a
let rec a_gen =
lazy
(choose
[
map [ int ] (fun i -> A i); map [ unlazy a_gen ] (fun s -> Self s);
])
let (lazy a_gen) = a_genfix fn applies the function fn. It is useful when defining generators for recursive data types:
open Alcobar
type a = A of int | Self of a
let rec a_gen =
fix (fun a_gen ->
choose [ map [ int ] (fun i -> A i); map [ a_gen ] (fun s -> Self s) ])option gen generates either None or Some x, where x is the item generated by gen.
pair gena gen generates (a, b) where a is generated by gena and b by genb.
result gena genb generates either Ok va or Error vb, where va, vb are generated by gena, genb respectively.
list gen makes a generator for lists using gen. Lists may be empty; for non-empty lists, use list1.
list1 gen makes non-empty list generators. For potentially empty lists, use list.
array gen makes a generator for arrays using gen. Arrays may be empty; for non-empty arrays, use array1.
array1 gen makes non-empty array generators. For potentially empty arrays, use array.
concat_gen_list sep l concatenates a list of string gen l inserting the separator sep between each.
with_printer printer gen generates the same values as gen. If gen is used to create a failing test case and the test was reached by calling check_eq without a printer argument, printer will be used to print the failing test case.
dynamic_bind gen f is a monadic bind, it allows to express the generation of a value whose generator itself depends on a previously generated value. This is in contrast with map gen f, where no further generation happens in f after gen has generated an element.
An typical example where this sort of dependencies is required is a serialization library exporting combinators letting you build values of the form 'a serializer. You may want to test this library by first generating a pair of a serializer and generator 'a serializer * 'a gen for arbitrary 'a, and then generating values of type 'a depending on the (generated) generator to test the serializer. There is such an example in the examples/serializer/ directory of the Alcobar implementation.
Because the structure of a generator built with dynamic_bind is opaque/dynamic (it depends on generated values), the Alcobar library cannot analyze its statically (without generating anything) -- the generator is opaque to the library, hidden in a function. In particular, many optimizations or or fuzzing techniques based on generator analysis are impossible. As a client of the library, you should avoid dynamic_bind whenever it is not strictly required to express a given generator, so that you can take advantage of these features (present or future ones). Use the least powerful/complex combinators that suffice for your needs.
Printing
Format a message using the supplied formatter.
Print an optional value using the supplied element printer.
Testing
An opaque test case.
test_case ~outcomes name generators test_fn creates a test case: test_fn is run with random data produced by generators. Any failure is reported annotated with name.
outcomes (default []) names the outcomes test_fn reports with reach, such as the Ok and Error branches of a decoder. A property run counts, for each outcome, the passing samples that reached it, prints the counts when the process exits, and fails the test when no passing sample reached an outcome: a roundtrip property whose decoder refuses every generated input then fails instead of passing on every sample.
reach outcome records that the running sample reached outcome. A sample counts once towards each outcome it reaches, however many times it calls reach with it.
reach fails the sample when outcome is not among the outcomes its test case declares, so a misspelt outcome is reported where it is reached. Outside a sample it does nothing.
verdict name check is a case that judges a whole campaign rather than a sample, such as a coverage floor over the outcomes the properties counted. An Alcotest run runs check once, after every property of the run, as the test SUITE: name of a last group, verdict; a fail or check in it fails that test, and a guard that rejects skips it.
The AFL runner and --gen-corpus never offer it an input: under AFL a persistent process runs many inputs, so a check over counts kept between samples would read whatever the earlier inputs left, or nothing in a fresh process, and report the input as a crash.
run name suites runs suites. Each suite is a pair of a suite name and its test cases. Mirrors Alcotest.run: CLI flags (including Alcotest's own) are parsed from Sys.argv.
Three modes are detected automatically:
--gen-corpus DIR: generate seed corpus files inDIRand exit. Each file holds the bytes AFL mode reads to choose its test, then the exact bytes its generators consumed during a passing run.- If the last argument is an existing file, run in AFL mode.
- Otherwise, run as an Alcotest test suite.
Only the properties, never a verdict, are seeded and run under AFL.
Aborting Tests
guard b aborts a test if b is false. The test will not be recorded or reported as a failure.
bad_test () aborts a test. The test will not be recorded or reported as a failure.
nonetheless o aborts a test if o is None. The test will not be recorded or reported as a failure.
Failing
fail message generates a test failure and prints message.
failf format generates a test failure and prints the message specified by the format string format and the following arguments. It is set up so that %a calls for an 'a printer and an 'a value.
Asserting Properties
check b generates a test failure if b is false. No useful information will be printed in this case.
val check_eq :
?pp:'a printer ->
?cmp:('a -> 'a -> int) ->
?eq:('a -> 'a -> bool) ->
'a ->
'a ->
unitcheck_eq pp cmp eq x y evaluates whether x and y are equal, and if they are not, raises a failure and prints an error message. Equality is evaluated as follows:
- use a provided
eq - if no
eqis provided, use a providedcmp - if neither
eqnorcmpis provided, use Stdlib.compare
If pp is provided, use this to print x and y if they are not equal. If pp is not provided, a best-effort printer will be generated from the printers for primitive generators and any printers registered with with_printer and used.