Generators and shrinking¶
Each checked type a property quantifies over has a
Generator, built from the type's structure by
generator_for: it enumerates a finite type, draws a
value of a given size from an infinite one, and shrinks a failing value.
generators
¶
Values a property quantifies over: enumeration, generation, shrinking.
A property's variable ranges over its type. generator_for gives each
checked QIEC type a Generator, by the type's structure alone, so every
declared data family has one without code written for it:
BoolandUnit, a product of finite types, a tensor of a finite element type at a literal shape, and a closed family without parameters that no constructor reaches again (no recursion) are finite: the generator enumerates every value, and the property is checked exhaustively;Int,Real,String, and a recursive family are generated: the generator draws a value of a given size from a seeded random source (Claessen and Hughes 2000), and shrinks a failing value to smaller ones.
A recursive family is generated by size: a constructor whose fields reach the family again is chosen only while size remains, and each such field gets a share of it, so generation terminates. A value shrinks to its subterms of its own type first, then field by field.
A family whose module declares a computation F__arbitrary(size : Int) :
F generates its values by running it (the shape a derived Arbitrary
instance has); structural shrinking still applies.
GenerationError
¶
Bases: ValueError
A type has no generator, with the declared property that rules it out.
Generator
¶
Bases: Protocol
How a property's variable ranges over its type.
finite
¶
The number of values, when the type is finite.
| RETURNS | DESCRIPTION |
|---|---|
int | None
|
The count, or |
enumerate
¶
enumerate() -> Iterator[TestValue]
Every value of a finite type, smallest first.
| RETURNS | DESCRIPTION |
|---|---|
Iterator[TestValue]
|
The values. |
sample
¶
sample(source: Random, size: int) -> TestValue
Draw one value of at most a given size.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The seeded random source.
TYPE:
|
size
|
The size bound.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TestValue
|
The value. |
Source code in src/quivers/testing/generators.py
BoolGenerator
¶
The two truth values; true shrinks to false.
finite
¶
enumerate
¶
enumerate() -> Iterator[TestValue]
false then true.
| RETURNS | DESCRIPTION |
|---|---|
Iterator[TestValue]
|
The values. |
sample
¶
sample(source: Random, size: int) -> TestValue
A fair coin.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The random source.
TYPE:
|
size
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TestValue
|
A truth value. |
Source code in src/quivers/testing/generators.py
UnitGenerator
¶
The one unit value.
finite
¶
sample
¶
sample(source: Random, size: int) -> TestValue
The unit value.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
Unused.
TYPE:
|
size
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TestValue
|
The empty tuple. |
Source code in src/quivers/testing/generators.py
IntGenerator
¶
Integers between -size and size, shrinking toward zero.
finite
¶
enumerate
¶
enumerate() -> Iterator[TestValue]
No enumeration; the type is infinite.
| RETURNS | DESCRIPTION |
|---|---|
Iterator[TestValue]
|
Never returns. |
| RAISES | DESCRIPTION |
|---|---|
GenerationError
|
Always. |
Source code in src/quivers/testing/generators.py
sample
¶
sample(source: Random, size: int) -> TestValue
A uniform integer in [-size, size].
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The random source.
TYPE:
|
size
|
The bound.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TestValue
|
The integer. |
Source code in src/quivers/testing/generators.py
shrink
¶
0, the absolute value, then halves toward zero.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
An integer.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Iterator[TestValue]
|
The candidates, each closer to zero. |
Source code in src/quivers/testing/generators.py
RealGenerator
¶
Rationals p/q with |p| <= size**2 and 1 <= q <= size.
A real a property draws is an exact rational, so an exact run checks the property at exactly that point; a floating run takes the nearest float. It shrinks toward zero, to integers, then to simpler fractions.
finite
¶
enumerate
¶
enumerate() -> Iterator[TestValue]
No enumeration; the type is infinite.
| RETURNS | DESCRIPTION |
|---|---|
Iterator[TestValue]
|
Never returns. |
| RAISES | DESCRIPTION |
|---|---|
GenerationError
|
Always. |
Source code in src/quivers/testing/generators.py
sample
¶
sample(source: Random, size: int) -> TestValue
A rational of the given size.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The random source.
TYPE:
|
size
|
The bound.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TestValue
|
The rational. |
Source code in src/quivers/testing/generators.py
shrink
¶
Simpler rationals: smaller denominators, then smaller numerators.
Every candidate p'/q' of p/q is smaller in the well-founded
order of (q, |p|, p < 0): 0, the absolute value of a
negative, the integer part and the nearest fractions of smaller
denominator, then numerators moved toward zero over the same
denominator.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A rational.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Iterator[TestValue]
|
The candidates, each smaller. |
Source code in src/quivers/testing/generators.py
StringGenerator
¶
Lowercase strings of length at most size, shrinking by deletion.
finite
¶
enumerate
¶
enumerate() -> Iterator[TestValue]
No enumeration; the type is infinite.
| RETURNS | DESCRIPTION |
|---|---|
Iterator[TestValue]
|
Never returns. |
| RAISES | DESCRIPTION |
|---|---|
GenerationError
|
Always. |
Source code in src/quivers/testing/generators.py
sample
¶
sample(source: Random, size: int) -> TestValue
A string of letters.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The random source.
TYPE:
|
size
|
The length bound.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TestValue
|
The string. |
Source code in src/quivers/testing/generators.py
shrink
¶
The empty string, halves, then each one-character deletion.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A string.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Iterator[TestValue]
|
The candidates, each shorter. |
Source code in src/quivers/testing/generators.py
ProductGenerator
¶
ProductGenerator(components: Sequence[Generator])
Tuples of component values; also a tensor at a literal shape.
| PARAMETER | DESCRIPTION |
|---|---|
components
|
One generator per component.
TYPE:
|
Source code in src/quivers/testing/generators.py
finite
¶
The product of the components' counts, when all are finite.
| RETURNS | DESCRIPTION |
|---|---|
int | None
|
The count. |
Source code in src/quivers/testing/generators.py
enumerate
¶
enumerate() -> Iterator[TestValue]
Every tuple, in lexicographic order of the components'.
| RETURNS | DESCRIPTION |
|---|---|
Iterator[TestValue]
|
The tuples. |
Source code in src/quivers/testing/generators.py
sample
¶
sample(source: Random, size: int) -> TestValue
One value per component.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The random source.
TYPE:
|
size
|
The size bound of each component.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TestValue
|
The tuple. |
Source code in src/quivers/testing/generators.py
shrink
¶
Shrink one component at a time.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A tuple.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Iterator[TestValue]
|
The candidates. |
Source code in src/quivers/testing/generators.py
FamilyGenerator
¶
FamilyGenerator(family: FamilyDecl, type_: TypeApplication, evaluator: TermEvaluator, arbitrary: NamedComputation | None = None, building: tuple[TypeExpr, ...] = ())
Values of a closed data family, by its constructors' fields.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family.
TYPE:
|
type_
|
The instance of it the values have.
TYPE:
|
evaluator
|
The module and the run settings, which turn field values into the run's numbers.
TYPE:
|
arbitrary
|
The module's
TYPE:
|
building
|
The family instances whose generators enclose this one, so a family reached again through another is refused rather than recursed into without bound.
TYPE:
|
Source code in src/quivers/testing/generators.py
finite
¶
The number of values, when no constructor recurses.
| RETURNS | DESCRIPTION |
|---|---|
int | None
|
The sum over constructors of the product of their fields'
counts, or |
Source code in src/quivers/testing/generators.py
enumerate
¶
enumerate() -> Iterator[TestValue]
Every value, constructor by constructor.
| RETURNS | DESCRIPTION |
|---|---|
Iterator[TestValue]
|
The values. |
Source code in src/quivers/testing/generators.py
sample
¶
sample(source: Random, size: int) -> TestValue
Draw a value of at most size constructors deep.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The random source.
TYPE:
|
size
|
The size bound.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TestValue
|
The value. |
| RAISES | DESCRIPTION |
|---|---|
GenerationError
|
If every constructor recurses, so no value is finite. |
Source code in src/quivers/testing/generators.py
shrink
¶
Subterms of the same type, then each field shrunk.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A data value of the family.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Iterator[TestValue]
|
The candidates. |
Source code in src/quivers/testing/generators.py
ListedGenerator
¶
ListedGenerator(values: Sequence[TestValue])
A variable ranging over listed values, in the listed order.
| PARAMETER | DESCRIPTION |
|---|---|
values
|
The values.
TYPE:
|
Source code in src/quivers/testing/generators.py
finite
¶
sample
¶
sample(source: Random, size: int) -> TestValue
A listed value, uniformly.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The random source.
TYPE:
|
size
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TestValue
|
The value. |
Source code in src/quivers/testing/generators.py
generator_for
¶
generator_for(type_: TypeExpr, evaluator: TermEvaluator, building: tuple[TypeExpr, ...] = ()) -> Generator
The generator of a closed type's values.
| PARAMETER | DESCRIPTION |
|---|---|
type_
|
The type.
TYPE:
|
evaluator
|
The module the type's families are declared in, and the run's settings.
TYPE:
|
building
|
The family instances whose generators enclose this one.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Generator
|
The generator. |
| RAISES | DESCRIPTION |
|---|---|
GenerationError
|
If the type has none: a function type, an opaque or indexed family, a family reached again through another family (mutual recursion), or a tensor of unknown shape. |
Source code in src/quivers/testing/generators.py
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 | |