dcheck.arbitrary

Module defines routines for generating testing sets.

To define Arbitrary template for particular type T (or types) you should follow compile-time interface: <ul> <li>Define generate function that takes nothing and returns range of T. This function is used to generate random sets of testing data. Size of required sample isn't passed thus use lazy ranges to generate possible infinite set of data.</li> <li>Define shrink function that takes value of T and returns range of truncated variations. This function is used to reduce failing case data to minimum possible set. You can return empty array if you like to get large bunch of random data.</li> <li>Define specialCases function that takes nothing and returns range of T. This function is used to test some special values for particular type like NaN or null pointer. You can return empty array if no testing on special cases is required.</li> </ul>

Usually useful practice is put static assert with CheckArbitrary template into your implementation to actually get confidence that you've done it properly.

Members

Manifest constants

ArrayGenSizeMax
enum ArrayGenSizeMax;

Maximum size of generated arrays (and strings)

ArrayGenSizeMin
enum ArrayGenSizeMin;

Minimum size of generated arrays (and strings)

Mixin templates

DefaultShrink
mixintemplate DefaultShrink(T)

You can use this to define default shrink implementation.

DefaultShrinkAndSpecialCases
mixintemplate DefaultShrinkAndSpecialCases(T)

You can use this to define only generate function.

DefaultSpecialCases
mixintemplate DefaultSpecialCases(T)

You can use this to define default special cases implementation.

Templates

Arbitrary
template Arbitrary(T)

Arbitrary for ubyte, byte, ushort, short, uing, int, ulong, long.

Arbitrary
template Arbitrary(T)

Arbitrary for float, double

Arbitrary
template Arbitrary(T)

Arbitrary for bool

Arbitrary
template Arbitrary(T)

Arbitrary template for char, dchar, wchar

Arbitrary
template Arbitrary(T)

Arbitrary template for strings

Arbitrary
template Arbitrary(T)

Arbitrary template for arrays

CheckArbitrary
template CheckArbitrary(T)

Check the T type has properly defined Arbitrary template. Prints useful user-friendly messages at compile time.

HasArbitrary
template HasArbitrary(T)

Checks if T has Arbitrary template with generate, shrink and specialCases functions.

Examples

template Arbitrary(T)
    if(isIntegral!T)
{
    static assert(CheckArbitrary!T);

    auto generate()
    {
        return (() => Maybe!T(uniform!"[]"(T.min, T.max))).generator;
    }

    auto shrink(T val)
    {
        class Shrinker
        {
            T saved;

            this(T firstVal)
            {
                saved = firstVal;
            }

            Maybe!T shrink()
            {
                if(saved == 0) return Maybe!T.nothing;

                if(saved > 0) saved--;
                if(saved < 0) saved++;

                return Maybe!T(saved);
            }
        }

        return (&(new Shrinker(val)).shrink).generator;
    }

    T[] specialCases()
    {
        return [T.min, 0, T.max];
    }
}

Meta

License

Subject to the terms of the MIT license, as written in the included LICENSE file.

Authors

NCrashed <ncrashed@gmail.com>