Skip to content

Commit 5b65c9f

Browse files
committed
Fix random runner seed initialization
1 parent 313bf25 commit 5b65c9f

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

subpackages/runner/source/unit_threaded/runner/options.d

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct Options {
2525

2626
bool single;
2727
bool help;
28+
immutable seedSet = args.hasSeedArg;
2829

2930
auto helpInfo =
3031
getopt(args,
@@ -50,6 +51,8 @@ struct Options {
5051
if(random) {
5152
if(!single) writeln("-r implies -s, running in a single thread\n");
5253
single = true;
54+
if(!seedSet)
55+
seed = randomSeed;
5356
}
5457

5558
version(unitUnthreaded)
@@ -64,3 +67,24 @@ struct Options {
6467
exit = help || list;
6568
}
6669
}
70+
71+
private bool hasSeedArg(scope const string[] args) @safe pure {
72+
import std.algorithm: startsWith;
73+
74+
foreach(arg; args)
75+
if(arg == "--seed" || arg.startsWith("--seed="))
76+
return true;
77+
78+
return false;
79+
}
80+
81+
private uint randomSeed() @safe {
82+
import std.random: unpredictableSeed;
83+
84+
uint seed;
85+
do
86+
seed = unpredictableSeed;
87+
while(seed == 0);
88+
89+
return seed;
90+
}

tests/unit_threaded/ut/runner_options.d

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,21 @@ unittest {
3232
assert(options.numThreads == 1);
3333
assert(options.random);
3434
}
35+
36+
@("-r sets a random seed")
37+
unittest {
38+
const options = Options(["ut", "-r"]);
39+
assert(options.seed != 0);
40+
}
41+
42+
@("-r respects an explicit seed")
43+
unittest {
44+
const options = Options(["ut", "-r", "--seed", "123"]);
45+
assert(options.seed == 123);
46+
}
47+
48+
@("-r respects an explicit zero seed")
49+
unittest {
50+
const options = Options(["ut", "-r", "--seed", "0"]);
51+
assert(options.seed == 0);
52+
}

0 commit comments

Comments
 (0)