1 module unit_threaded.randomized.random;
2 
3 
4 /** This type will generate a $(D Gen!T) for all passed $(D T...).
5     Every call to $(D genValues) will call $(D gen) of all $(D Gen) structs
6     present in $(D values). The member $(D values) can be passed to every
7     function accepting $(D T...).
8 */
9 struct RndValueGen(T...)
10 {
11     import std.meta : staticMap;
12     import std.random: Random;
13 
14     /* $(D Values) is a collection of $(D Gen) types created through
15        $(D ParameterToGen) of passed $(T ...).
16     */
17     static if(is(typeof(T[0]) == string[])) {
18         alias generators = T[1 .. $];
19         string[] parameterNames = T[0];
20     } else {
21         alias generators = T;
22         string[T.length] parameterNames;
23     }
24 
25     alias Values = staticMap!(ParameterToGen, generators);
26 
27     /// Ditto
28     Values values;
29 
30     /* The constructor accepting the required random number generator.
31        Params:
32        rnd = The required random number generator.
33     */
34     this(Random* rnd) @safe
35     {
36         this.rnd = rnd;
37     }
38 
39     this(ref Random rnd) {
40         this.rnd = &rnd;
41     }
42 
43     /* The random number generator used to generate new value for all
44        $(D values).
45     */
46     Random* rnd;
47 
48     /** A call to this member function will call $(D gen) on all items in
49         $(D values) passing $(D the provided) random number generator
50     */
51     void genValues()
52     {
53         assert(rnd !is null);
54         foreach (ref it; this.values)
55         {
56             it.gen(*this.rnd);
57         }
58     }
59 
60     void toString(scope void delegate(const(char)[]) sink)
61     {
62         import std.format : formattedWrite;
63 
64         foreach (idx, ref it; values)
65         {
66             formattedWrite(sink, "'%s' = %s ", parameterNames[idx], it);
67         }
68     }
69 }
70 
71 
72 /** A template that turns a $(D T) into a $(D Gen!T) unless $(D T) is
73     already a $(D Gen) or no $(D Gen) for given $(D T) is available.
74 */
75 template ParameterToGen(T)
76 {
77     import unit_threaded.randomized.gen: isGen, Gen;
78     static if (isGen!T)
79         alias ParameterToGen = T;
80     else static if (is(T : GenASCIIString!(S), S...))
81         alias ParameterToGen = T;
82     else {
83         static assert(__traits(compiles, Gen!T),
84                       "ParameterToGen does not handle " ~ T.stringof);
85         alias ParameterToGen = Gen!T;
86     }
87 }