Shrinkable
October 30, 2018 ยท View on GitHub
This section is incomplete.
Shrinkable<T> is a fundamental template class in RapidCheck. An instance of Shrinkable<T> represents some value of type T and provides a way to access all the ways in which this value can be shrunk. It has value semantics which means that it can be copied and passed around just like any other value. It has two member functions:
T value() constreturns the represented value of thisShrinkable.Seq<Shrinkable<T>> shrinks() constreturns aSeq(see the documentation forSeqfor more information) of all the possible ways of shrinking the value from the smallest to the largest.
There are two important things to note here:
value()is a member function, not a member variable. This means that the way that theShrinkableobtains the valueTthat it returns is abstracted away. If this wasn't the case, we would have to impose the following restrictions onT:Twould need to have a copy constructor. Otherwise, we would only be able to retrieveTonce. This means we couldn't generate, for example,std::unique_ptrs to objects. Non-copyable objects are very common in mainstream C++ code so this would be undesirable.T's copy constructor would have to produce copies that shared no state with the original value. Without this guarantee, we would not be able to repeatedly provide semantically equivalent objects since the consumer ofTmight modify the copy which could also modify the original. The obvious example of a type which certainly doesn't provide the non-shared-state guarantee isstd::shared_ptrwhose entire purpose is having a copy constructor that yields copies with shared state.
shrinks()returns aSeqofShrinkable<T>and not justT. This means that aShrinkable<T>is a value ofTcombined with a tree of possible ways of shrinking it, not just a list. This allows RapidCheck to recursively search this for the smallest value value satisfying some condition (usually failing the property).