Skip to content

Commit 74a13ef

Browse files
iscgarphkahler
authored andcommitted
tests: actually randomise the order of tests
`std::random_shuffle` can use `std::rand` (and does so on Windows), and without intialising with `srand()` the order will always be the same, so we don't get the expected randomisation. Switch to using `std::shuffle` with a proper random number generator in order to fix this and get random order execution.
1 parent 2fd3989 commit 74a13ef

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

test/harness.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
//
44
// Copyright 2016 whitequark
55
//-----------------------------------------------------------------------------
6+
#include <cstdio>
7+
#include <random>
68
#include <regex>
79
#include <cairo.h>
810

@@ -337,19 +339,29 @@ int Test::Case::Register(Test::Case testCase) {
337339
int main(int argc, char **argv) {
338340
std::vector<std::string> args = Platform::InitCli(argc, argv);
339341

340-
std::regex filter(".*");
342+
std::string filterPattern = ".*";
343+
unsigned int seed = std::random_device{}();
341344
if(args.size() == 1) {
342345
} else if(args.size() == 2) {
343-
filter = args[1];
346+
filterPattern = args[1];
347+
} else if(args.size() == 3) {
348+
filterPattern = args[1];
349+
seed = std::stoul(args[2]);
344350
} else {
345-
fprintf(stderr, "Usage: %s [test filter regex]\n", args[0].c_str());
351+
fprintf(stderr, "Usage: %s [test filter regex] [shuffle seed]\n", args[0].c_str());
346352
return 1;
347353
}
348354

355+
fprintf(stderr, "info: using test filter `%s' and seed %u\n", filterPattern.c_str(), seed);
356+
349357
Platform::fontFiles.push_back(HostRoot().Join("Gentium-R.ttf"));
350358

359+
std::mt19937 g(seed);
360+
351361
// Wreck order dependencies between tests!
352-
std::random_shuffle(testCasesPtr->begin(), testCasesPtr->end());
362+
std::shuffle(testCasesPtr->begin(), testCasesPtr->end(), g);
363+
364+
std::regex filter(filterPattern);
353365

354366
auto testStartTime = std::chrono::steady_clock::now();
355367
size_t ranTally = 0, skippedTally = 0, checkTally = 0, failTally = 0;

0 commit comments

Comments
 (0)