Skip to content

Commit

Permalink
etest: Run tests in a randomized order
Browse files Browse the repository at this point in the history
We don't want dependencies between tests.
  • Loading branch information
robinlinden committed Sep 26, 2024
1 parent b7bfba9 commit e9f9048
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
15 changes: 15 additions & 0 deletions etest/etest2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iostream>
#include <iterator>
#include <optional>
#include <random>
#include <source_location>
#include <sstream>
#include <string_view>
Expand Down Expand Up @@ -86,6 +87,20 @@ int Suite::run(RunOptions const &opts) {
return 1;
}

unsigned const seed = [&] {
if (opts.rng_seed) {
return *opts.rng_seed;
}

return std::random_device{}();
}();
std::mt19937 rng{seed};

// Shuffle tests to avoid dependencies between them.
std::ranges::shuffle(tests_to_run, rng);

std::cout << "Running " << tests_to_run.size() << " tests with the seed " << seed << ".\n";

auto const longest_name = std::ranges::max_element(
tests_to_run, [](auto const &a, auto const &b) { return a.size() < b.size(); }, &Test::name);

Expand Down
1 change: 1 addition & 0 deletions etest/etest2.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ concept Printable = requires(std::ostream &os, T t) {

struct RunOptions {
bool run_disabled_tests{false};
std::optional<unsigned> rng_seed;
};

class IActions {
Expand Down
27 changes: 27 additions & 0 deletions etest/shuffled_tests_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-FileCopyrightText: 2024 Robin Lindén <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause

#include "etest/etest2.h"

#include <iostream>
#include <random>
#include <tuple>

int main() {
unsigned seed = std::random_device{}();
etest::Suite s;

int last_run_test{};
s.add_test("1", [&](auto &) { last_run_test = 1; });
s.add_test("2", [&](auto &) { last_run_test = 2; });

std::ignore = s.run({.rng_seed = seed});
auto after_first_run = last_run_test;

std::ignore = s.run({.rng_seed = seed});
if (last_run_test != after_first_run) {
std::cerr << "Tests didn't run in the same order with the same seed\n";
return 1;
}
}

0 comments on commit e9f9048

Please sign in to comment.