Module eqc_gen

This module implements QuickCheck generators.

Version: 1.0.1

Description

This module implements QuickCheck generators. QuickCheck generators are used to generate random test data for QuickCheck properties. A generator specifies three things at the same time: QuickCheck permits constants to be used as generators for their own value, and also permits tuples, records, and lists containing generators to be used as generators for values of the same form. For example,
 {int(),bool()} 
is a generator that generates random pairs of integers and booleans.

Many of the functions in this module are usually used via macros, defined in eqc.hrl. These macros are listed here.

?LET(Pat,G1,G2)

Generates a value from G1, binds it to Pat, then generates a value from G2 (which may refer to the variables bound in Pat).

The result is shrunk by first shrinking the value generated by G1 while the test still fails, then shrinking the value generated by G2. It is thus better to write ?LET({X,Y},{G1,G2},G3) than ?LET(X,G1,?LET(Y,G2,G3)) (provided G2 does not depend on X), since in the first case shrinking can shrink G1 a bit, shrink G2, then shrink G1 some more, while in the second case G1 cannot be shrunk further once shrinking G2 has begun.

?SIZED(Size,G)

Binds the variable Size to the current size parameter for generation. G may use Size in any way to control the size of generated data. However, as Size increases, the set of possible values that G can generate should also increase. Size is always a natural number, and increases during QuickCheck testing from a small value up to about 40. See also resize/2 and pick/2.

?SUCHTHAT(X,G,P)

Generates values X from G such that the condition P is true. Should only be used if the probability that P holds is reasonably high for values generated by G--otherwise generation may be slow, and the distribution of generated values may be skewed. For example,
?SUCHTHAT(Xs,list(int()),lists:sort(Xs)==Xs)
generates predominantly very short lists, since the probability that a random longer list will just happen to be sorted is very low. If no value is found within 100 attempts, then ?SUCHTHAT exits.

?LETSHRINK(Pat,G1,G2)

This behaves in the same way as ?LET(Pat,G1,G2), except that G1 must generate a list of values, and each one of these values is added as a possible shrinking of the result. This is intended for use in generating tree-like structures. For example,
 ?LETSHRINK([L,R],[tree(),tree()],{branch,L,R})
generates a tree node {branch,L,R}, which can shrink to either L or R.

?LAZY(G)

A generator equivalent to its argument, but which is always cheap to construct. To be used, for example, in recursive generators to avoid building a huge generator, only a small part of which will be used.

Data Types

box()

abstract datatype: box(A)

Boxes are not supported in this version of QuickCheck.

gen()

abstract datatype: gen(A)

A QuickCheck generator for values of type A. QuickCheck generators are first-class values, and can be used repeatedly to generate many different values.

proplist()

proplist() = list({atom(), term()})

A property list associating values with names. See the standard module proplists.

Function Index

binary/0Generates a binary of random size.
binary/1Generates a binary of a given size in bytes.
bitstring/0Generates a list of bits in a bitstring.
bitstring/1Generates a bitstring of a given size in bits.
bool/0Generates a random boolean.
char/0Generates a random character.
choose/2Generates a number in the range M to N.
default/2Adds a default value to a generator, to be chosen half the time.
elements/1Generates an element of the list argument.
eval/1Evaluates terms of the form {call,Module,Function,Args} anywhere in its argument, replacing them by the result of the corresponding function call.
eval/2Like eval/1, but also replaces symbolic variables, that is, terms of the form {var,V}, by their corresponding values in the property list.
frequency/1Makes a weighted choice between the generators in its argument, such that the probability of choosing each generator is proportional to the weight paired with it.
function0/1Generates a function of no arguments with result generated by G.
function1/1Generates a function of one argument with result generated by G.
int/0Generates a small integer (with absolute value bounded by the generation size).
is_generator/1Returns true if the argument is a QuickCheck generator.
largeint/0Generates an integer from a large range.
list/1Generates a list of elements generated by its argument.
nat/0Generates a small natural number (bounded by the generation size).
non_empty/1Make sure that the generated value is not empty.
noshrink/1Generates the same values as G, but these values are never shrunk.
oneof/1Generates a value using a randomly chosen element of the list of generators.
orderedlist/1Generates an ordered list of elements generated by G.
real/0Generates a real number.
resize/2Binds the generation size parameter to Size within G.
return/1Constructs a generator that always generates the value X.
sample/1Prints 11 values randomly generated by G, for sizes ranging from 10 to 20.
sampleshrink/1Prints a value generated by G, followed by one way of shrinking it.
shuffle/1Shuffles a list and shrinks to the unshuffled list.
vector/2Generates a list of the given length, with elements generated by G.

Function Details

binary/0

binary() -> gen(binary())

Generates a binary of random size. The binary shrinks both in size as well as in content. If you consider the binary as a representation of a number, then each shrinking step will result in a smaller-or-equal number.

binary/1

binary(NrBytes::int()) -> gen(binary())

Generates a binary of a given size in bytes. When shrinking, the size is unchanged, but content shrinks like binary/0.

bitstring/0

bitstring() -> gen(bitstring())

Generates a list of bits in a bitstring. For Erlang release R12B and later. The bitstring shrinks both in size as well as in content. If you consider the bitstring as a representation of a number, then each shrinking step will result in a smaller-or-equal number.

bitstring/1

bitstring(NrBits::int()) -> gen(bitstring())

Generates a bitstring of a given size in bits. For Erlang release R12B and later. When shrinking, the size is unchanged, but content shrinks like bitstring/0.

bool/0

bool() -> gen(bool())

Generates a random boolean. Shrinks to false.

char/0

char() -> gen(char())

Generates a random character. Shrinks to a, b or c.

choose/2

choose(M, N::integer()) -> gen(integer())

Generates a number in the range M to N. The result shrinks towards smaller absolute values.

default/2

default(Default::A, G::gen(A)) -> gen(A)

Adds a default value to a generator, to be chosen half the time. Any other value shrinks to the default.

elements/1

elements(Xs::list(A)) -> gen(A)

Generates an element of the list argument. Shrinking chooses an earlier element.

eval/1

eval(Term::term()) -> term()

Evaluates terms of the form {call,Module,Function,Args} anywhere in its argument, replacing them by the result of the corresponding function call. This is useful when, for example, test data is of an abstract datatype, and we want to know how it was generated, rather than its representation--it is much clearer to see that a test failed for sets:new() (that is {call,sets,new,[]}), for example, than for its representation. We write ?FORALL(X,TGen,...eval(X)...), where TGen generates terms containing calls, so that test cases are displayed in this form, but the actual test data is the result of evaluating the calls.

eval/2

eval(Env::proplist(), T::term()) -> term()

Like eval/1, but also replaces symbolic variables, that is, terms of the form {var,V}, by their corresponding values in the property list. This should be a list of pairs of atoms and values. For example, eval([{x,3}],{var,x}) evaluates to 3.

frequency/1

frequency(FGs::list({integer(), gen(A)})) -> gen(A)

Makes a weighted choice between the generators in its argument, such that the probability of choosing each generator is proportional to the weight paired with it. The weights should be non-negative integers and sum to a positive value. A generator with a weight of zero will not be chosen.

function0/1

function0(G::gen(A)) -> gen(() -> A)

Generates a function of no arguments with result generated by G.

function1/1

function1(G::gen(A)) -> gen((term()) -> A)

Generates a function of one argument with result generated by G. The generated function is pure--will always return the same result for the same argument-- and the result depends randomly on the argument.

int/0

int() -> gen(integer())

Generates a small integer (with absolute value bounded by the generation size).

is_generator/1

is_generator(X::any()) -> bool()

Returns true if the argument is a QuickCheck generator.

largeint/0

largeint() -> any()

Generates an integer from a large range.

list/1

list(G::gen(A)) -> gen(list(A))

Generates a list of elements generated by its argument. Shrinking drops elements from the list. The length of the list varies up to one third of the generation size parameter.

nat/0

nat() -> gen(integer())

Generates a small natural number (bounded by the generation size).

non_empty/1

non_empty(G::gen(A)) -> gen(A)

Make sure that the generated value is not empty. For example when creating a list of integers, but the list should always contain at least one element non_empty(list(int())).

noshrink/1

noshrink(G::gen(A)) -> gen(A)

Generates the same values as G, but these values are never shrunk.

oneof/1

oneof(Gs::list(gen(A))) -> gen(A)

Generates a value using a randomly chosen element of the list of generators.

orderedlist/1

orderedlist(G::gen(A)) -> gen(list(A))

Generates an ordered list of elements generated by G.

real/0

real() -> gen(float())

Generates a real number.

resize/2

resize(Size::integer(), G::gen(A)) -> gen(A)

Binds the generation size parameter to Size within G. Size should never be negative.

return/1

return(X::A) -> gen(A)

Constructs a generator that always generates the value X. Most values can also be used as generators for themselves, making return unnecessary, but return(X) may be more efficient than using X as a generator, since when return(X) is used then QuickCheck does not traverse X searching for values to be intepreted specially.

sample/1

sample(G::gen(A)) -> ok

Prints 11 values randomly generated by G, for sizes ranging from 10 to 20.

sampleshrink/1

sampleshrink(G::gen(A)) -> ok

Prints a value generated by G, followed by one way of shrinking it. Each following line displays a list of values that the first value on the previous line can be shrunk to in one step. Thus the output traces the leftmost path through the shrinking tree.

shuffle/1

shuffle(List::list(A)) -> gen(list(A))

Shuffles a list and shrinks to the unshuffled list.

vector/2

vector(K::integer(), G::gen(A)) -> gen(list(A))

Generates a list of the given length, with elements generated by G.


Generated by EDoc, Jun 13 2010, 13:15:30.