Function quickCheck
Feed testee with arbitrary data to check that it behaves correctly.
Example
comparing sort algorithms
// https://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort#D
static T[] bubbleSort(T)(T[] data) pure nothrow
{
import std .algorithm : swap;
foreach_reverse (n; 0 .. data .length)
{
bool swapped;
foreach (i; 0 .. n)
if (data[i] > data[i + 1]) {
swap(data[i], data[i + 1]);
swapped = true;
}
if (!swapped)
break;
}
return data;
}
/// random data is injected into testee arguments
static bool testee(ubyte[] data)
{
import std .algorithm : equal, sort;
return bubbleSort(data .dup) .equal(data .sort());
}
quickCheck!testee;
Example
testee can reject random arguments to enforce additional constraints
static QCheckResult testee(string haystack, string needle)
{
import std .algorithm : canFind, boyerMooreFinder;
if (needle .length < haystack .length)
return QCheckResult .discard;
auto bmFinder = boyerMooreFinder(needle);
immutable found = !!bmFinder .beFound(haystack) .length;
return found == haystack .canFind(needle) ? QCheckResult .pass : QCheckResult .fail;
}
randomSeed = 42;
quickCheck!testee;