Skip to content

FrequencyArbitrary ​

Rasuvaeff\PropertyTesting\Arbitrary\FrequencyArbitrary

Class — Package: property-testing-core — Source — Version: v1.0.0-1-g88fdee2

Implements: Swarmable, ArbitraryInterface

Type parameters:

  • TValue of mixed = mixed

Weighted choice among several arbitraries: each [weight, arbitrary] pair is picked with probability proportional to its (positive integer) weight, then the chosen arbitrary produces the value.

The chosen branch's shrink tree is returned as-is, so shrinking stays within the branch that actually generated the value.

Constructor ​

php
__construct(
    iterable $pairs,
)
ParameterTypeDefaultDescription
$pairsiterablerequiredEach item must be [int $weight, ArbitraryInterface $arbitrary] with $weight >= 1.

Methods ​

generate() ​

php
generate(Random $random): Shrinkable

Produce one random value from this arbitrary's space, together with its shrink tree. Candidates must be ordered most aggressive first (typically toward a zero/empty/identity element) and every branch of the tree must be finite, so shrinking terminates.

Documentation inherited from ArbitraryInterface.

variantCount() ​

php
variantCount(): int

How many variants this generator chooses among. Must be positive: a choice generator with nothing to choose from is not a choice, and Arbitrary\SwarmArbitrary rejects a source that reports otherwise rather than drawing from an empty alphabet. Deliberately typed int and not int<1, max> — the bound is a promise implementations make, and a swarm still checks it at runtime because it cannot analyse the implementations it will be handed.

Documentation inherited from Swarmable.

withVariants() ​

php
withVariants(list<int> $indices): self

The kept pairs carry their weights unchanged, so a swarm changes which branches exist and not how the surviving ones compete: the total weight shrinks with them, and a branch that was twice as likely as its neighbour still is.

  • $indices — Variant positions to keep, each in [0, variantCount() - 1].

Throws:

  • InvalidArgumentException — When an index falls outside the weighted branches.