1 module unit_threaded.ut.randomized.gen;
2 
3 import unit_threaded.randomized.gen;
4 
5 @safe pure unittest {
6     import unit_threaded.asserts: assertEqual;
7     import std.random: Random;
8 
9     auto rnd = Random(1337);
10     Gen!int gen;
11     assertEqual(gen.gen(rnd), 0);
12     assertEqual(gen.gen(rnd), 1);
13     assertEqual(gen.gen(rnd), int.min);
14     assertEqual(gen.gen(rnd), int.max);
15     assertEqual(gen.gen(rnd), 1125387415); //1st non front-loaded value
16 }
17 
18 @safe unittest {
19     // not pure because of floating point flags
20     import unit_threaded.asserts: assertEqual;
21     import std.math: approxEqual;
22     import std.conv: to;
23     import std.random: Random;
24 
25     auto rnd = Random(1337);
26     Gen!float gen;
27     assertEqual(gen.gen(rnd), 0);
28     assertEqual(gen.gen(rnd), float.epsilon);
29     assertEqual(gen.gen(rnd), float.min_normal);
30     assert(approxEqual(gen.gen(rnd), 6.022E23), gen.value.to!string);
31     assert(approxEqual(gen.gen(rnd), 1.57791E23), gen.value.to!string);
32 }
33 
34 
35 @safe unittest {
36     // not pure because of floating point flags
37     import unit_threaded.asserts: assertEqual;
38     import std.math: approxEqual;
39     import std.conv: to;
40     import std.random: Random;
41 
42     auto rnd = Random(1337);
43     Gen!(float, 0, 5) gen;
44     assertEqual(gen.gen(rnd), 0);
45     assertEqual(gen.gen(rnd), float.epsilon);
46     assertEqual(gen.gen(rnd), float.min_normal);
47     assertEqual(gen.gen(rnd), 5);
48     assert(approxEqual(gen.gen(rnd), 1.31012), gen.value.to!string);
49 }
50 
51 @safe unittest {
52     import std.meta : AliasSeq, aliasSeqOf;
53     import std.range : iota;
54     import std.array : empty;
55     import std.random: Random;
56     import unit_threaded.asserts;
57 
58     foreach (index, T; AliasSeq!(string, wstring, dstring)) {
59         auto r = Random(1337);
60         Gen!T a;
61         T expected = "";
62         assertEqual(a.gen(r), expected);
63         expected = "a";
64         assertEqual(a.gen(r), expected);
65         expected = "é";
66         assertEqual(a.gen(r), expected);
67         assert(a.gen(r).length > 1);
68     }
69 }
70 
71 // FIXME: fails in this mode
72 version(unitThreadedLight) {}
73 else {
74     @safe unittest {
75         import unit_threaded.asserts;
76         import std.random: Random;
77 
78         auto rnd = Random(1337);
79         GenASCIIString!() gen;
80         assertEqual(gen.gen(rnd), "");
81         assertEqual(gen.gen(rnd), "a");
82         version(Windows)
83             assertEqual(gen.gen(rnd), "yt4>%PnZwJ*Nv3L5:9I#N_ZK");
84         else
85             assertEqual(gen.gen(rnd), "i<pDqp7-LV;W`d)w/}VXi}TR=8CO|m");
86     }
87 }
88 
89 @("Gen!int[] generates random arrays of int")
90 @safe unittest {
91     import unit_threaded.asserts: assertEqual;
92     import std.random: Random;
93 
94     auto rnd = Random(1337);
95     auto gen = Gen!(int[], 1, 10)();
96 
97     // first the front-loaded values
98     assertEqual(gen.gen(rnd), []);
99     version(Windows)
100         assertEqual(gen.gen(rnd), [0, 1]);
101     else
102         assertEqual(gen.gen(rnd), [0, 1, -2147483648, 2147483647, 681542492, 913057000, 1194544295, -1962453543, 1972751015]);
103 }
104 
105 @("Gen!ubyte[] generates random arrays of ubyte")
106 @safe unittest {
107     import unit_threaded.asserts: assertEqual;
108     import std.random: Random;
109 
110     auto rnd = Random(1337);
111     auto gen = Gen!(ubyte[], 1, 10)();
112     assertEqual(gen.gen(rnd), []);
113 }
114 
115 
116 @("Gen!double[] generates random arrays of double")
117 @safe unittest {
118     import unit_threaded.asserts: assertEqual;
119     import std.random: Random;
120 
121     auto rnd = Random(1337);
122     auto gen = Gen!(double[], 1, 10)();
123 
124     // first the front-loaded values
125     assertEqual(gen.gen(rnd), []);
126     // then the pseudo-random ones
127     version(Windows)
128         assertEqual(gen.gen(rnd).length, 2);
129     else
130         assertEqual(gen.gen(rnd).length, 9);
131 }
132 
133 @("Gen!string[] generates random arrays of string")
134 @safe unittest {
135     import unit_threaded.asserts: assertEqual;
136     import std.random: Random;
137 
138     auto rnd = Random(1337);
139     auto gen = Gen!(string[])();
140 
141     assertEqual(gen.gen(rnd), []);
142     auto strings = gen.gen(rnd);
143     assert(strings.length > 1);
144     assertEqual(strings[1], "a");
145 }
146 
147 @("Gen!string[][] generates random arrays of string")
148 @safe unittest {
149     import unit_threaded.asserts: assertEqual;
150     import std.random: Random;
151 
152     auto rnd = Random(1337);
153     auto gen = Gen!(string[][])();
154 
155     assertEqual(gen.gen(rnd), []);
156     // takes too long
157     // auto strings = gen.gen(rnd);
158     // assert(strings.length > 1);
159 }
160 
161 @("Gen!bool generates random booleans")
162 @safe unittest {
163     import unit_threaded.asserts: assertEqual;
164     import std.random: Random;
165 
166     auto rnd = Random(1337);
167     auto gen = Gen!bool();
168 
169     assertEqual(gen.gen(rnd), true);
170     assertEqual(gen.gen(rnd), true);
171     assertEqual(gen.gen(rnd), false);
172     assertEqual(gen.gen(rnd), false);
173 }
174 
175 @("Gen char, wchar, dchar")
176 @safe unittest {
177     import unit_threaded.asserts: assertEqual;
178     import std.random: Random;
179 
180     {
181         auto rnd = Random(1337);
182         Gen!char gen;
183         assertEqual(cast(int)gen.gen(rnd), 151);
184     }
185     {
186         auto rnd = Random(1337);
187         Gen!wchar gen;
188         assertEqual(cast(int)gen.gen(rnd), 3223);
189     }
190     {
191         auto rnd = Random(1337);
192         Gen!dchar gen;
193         assertEqual(cast(int)gen.gen(rnd), 3223);
194     }
195 }
196 
197 @("struct")
198 @safe unittest {
199     import unit_threaded.asserts: assertEqual;
200     import std.random: Random;
201 
202     struct Foo {
203         int i;
204         string s;
205     }
206 
207     auto rnd = Random(1337);
208     Gen!Foo gen;
209     assertEqual(gen.gen(rnd), Foo(0, ""));
210     assertEqual(gen.gen(rnd), Foo(1, "a"));
211     assertEqual(gen.gen(rnd), Foo(int.min, "é"));
212 }
213 
214 @("class")
215 @safe unittest {
216     import unit_threaded.asserts: assertEqual;
217     import std.random: Random;
218 
219     static class Foo {
220         this() {}
221         this(int i, string s) { this.i = i; this.s = s; }
222         override string toString() @safe const pure nothrow {
223             import std.conv;
224             return text(`Foo(`, i, `, "`, s, `")`);
225         }
226         override bool opEquals(Object _rhs) @safe const pure nothrow {
227             auto rhs = cast(Foo)_rhs;
228             return i == rhs.i && s == rhs.s;
229         }
230         int i;
231         string s;
232     }
233 
234     auto rnd = Random(1337);
235     Gen!Foo gen;
236     assertEqual(gen.gen(rnd), new Foo(0, ""));
237     assertEqual(gen.gen(rnd), new Foo(1, "a"));
238     assertEqual(gen.gen(rnd), new Foo(int.min, "é"));
239 }