Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

etest: Run tests in a randomized order #1055

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}