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

1 template Arbitrary(T)
2     if(isIntegral!T)
3 {
4     static assert(CheckArbitrary!T);
5 
6     auto generate()
7     {
8         return (() => Maybe!T(uniform!"[]"(T.min, T.max))).generator;
9     }
10 
11     auto shrink(T val)
12     {
13         class Shrinker
14         {
15             T saved;
16 
17             this(T firstVal)
18             {
19                 saved = firstVal;
20             }
21 
22             Maybe!T shrink()
23             {
24                 if(saved == 0) return Maybe!T.nothing;
25 
26                 if(saved > 0) saved--;
27                 if(saved < 0) saved++;
28 
29                 return Maybe!T(saved);
30             }
31         }
32 
33         return (&(new Shrinker(val)).shrink).generator;
34     }
35 
36     T[] specialCases()
37     {
38         return [T.min, 0, T.max];
39     }
40 }

Meta

License

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

Authors

NCrashed <ncrashed@gmail.com>