1 module unit_threaded.ut.randomized.random; 2 3 import unit_threaded.randomized.random; 4 5 unittest 6 { 7 import unit_threaded.randomized.gen: Gen; 8 import std.random: Random; 9 10 auto rnd = Random(1337); 11 auto generator = (&rnd).RndValueGen!(["i", "f"], 12 Gen!(int, 0, 10), 13 Gen!(float, 0.0, 10.0)); 14 generator.genValues(); 15 16 static fun(int i, float f) 17 { 18 import std.conv: to; 19 assert(i >= 0 && i <= 10, i.to!string); 20 assert(f >= 0.0 && f <= 10.0, f.to!string); 21 } 22 23 fun(generator.values); 24 } 25 26 @("RndValueGen can be used without parameter names") 27 unittest 28 { 29 import unit_threaded.randomized.gen: Gen; 30 import std.random: Random; 31 32 auto rnd = Random(1337); 33 auto generator = rnd.RndValueGen!(Gen!(int, 0, 10), 34 Gen!(float, 0.0, 10.0)); 35 generator.genValues(); 36 37 static fun(int i, float f) 38 { 39 import std.conv: to; 40 assert(i >= 0 && i <= 10, i.to!string); 41 assert(f >= 0.0 && f <= 10.0, f.to!string); 42 } 43 44 fun(generator.values); 45 } 46 47 48 unittest 49 { 50 import unit_threaded.randomized.gen: Gen; 51 import std.random: Random; 52 53 static fun(int i, float f) 54 { 55 assert(i >= 0 && i <= 10); 56 assert(f >= 0.0 && i <= 10.0); 57 } 58 59 auto rnd = Random(1337); 60 auto generator = (&rnd).RndValueGen!(["i", "f"], 61 Gen!(int, 0, 10), 62 Gen!(float, 0.0, 10.0)); 63 64 generator.genValues(); 65 foreach (i; 0 .. 1000) 66 { 67 fun(generator.values); 68 } 69 } 70 71 @("RndValueGen with int[]") 72 unittest { 73 import unit_threaded.randomized.gen: Gen; 74 import std.random: Random; 75 76 void fun(int[] i) { } 77 auto rnd = Random(1337); 78 auto gen = rnd.RndValueGen!(Gen!(int[])); 79 gen.genValues; 80 fun(gen.values); 81 } 82 83 unittest 84 { 85 alias GenInt = ParameterToGen!int; 86 87 static fun(int i) 88 { 89 assert(i == 1337); 90 } 91 92 GenInt a; 93 a.value = 1337; 94 fun(a); 95 } 96 97 @("RndValueGen with user defined struct") 98 unittest { 99 import unit_threaded.randomized.gen: Gen; 100 import std.random: Random; 101 102 struct Foo { 103 int i; 104 short s; 105 } 106 107 auto rnd = Random(1337); 108 auto gen = rnd.RndValueGen!(Gen!Foo); 109 110 foreach(_; 0 .. 5) // get rid of front-loaded uninteresting values 111 gen.genValues; 112 113 void fun(Foo foo) { 114 import std.conv: text; 115 assert(foo == Foo(1125387415, -8003), text(foo)); 116 } 117 118 fun(gen.values); 119 } 120 121 122 unittest 123 { 124 import unit_threaded.randomized.gen: isGen; 125 import std.random: Random; 126 import std.meta : AliasSeq, staticMap; 127 128 struct Foo { 129 int i; 130 double d; 131 } 132 133 foreach (T; AliasSeq!(byte, ubyte, ushort, short, uint, int, ulong, long, 134 float, double, real, 135 string, wstring, dstring, Foo)) 136 { 137 alias TP = staticMap!(ParameterToGen, T); 138 static assert(isGen!TP); 139 } 140 }