File: parallel_runner.cpp

package info (click to toggle)
boost-ext-ut 2.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 620 kB
  • sloc: cpp: 6,773; makefile: 13; sh: 10
file content (65 lines) | stat: -rw-r--r-- 1,606 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// Copyright (c) 2019-2020 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <algorithm>
#include <boost/ut.hpp>
#include <iostream>
#if __has_include(<execution>)
#include <execution>
#endif

namespace ut = boost::ut;

namespace cfg {
class parallel_runner : public ut::runner<> {
 public:
  using ut::runner<>::on;

  template <class... Ts>
  auto on(ut::events::test<Ts...> test) {
    std::clog << test.name << '\n';
    ut::runner<>::on(test);
  }

  [[nodiscard]] auto run() -> bool {
#if defined(__cpp_lib_parallel_algorithm)
    std::for_each(std::execution::par, std::cbegin(suites_), std::cend(suites_),
                  [&](const auto& suite) { suite.first(); });
#else
    std::for_each(std::cbegin(suites_), std::cend(suites_),
                  [&](const auto& suite) { suite.first(); });
#endif

    suites_.clear();

    return fails_ > 0;
  }
};
}  // namespace cfg

template <class... Ts>
inline auto ut::cfg<ut::override, Ts...> = cfg::parallel_runner{};

ut::suite parallel_1 = [] {
  using namespace ut;

  // sequential
  "test.1.1"_test = [] { expect(1_i == 1); };
  "test.1.2"_test = [] { expect(2_i == 2); };
  "test.1.3"_test = [] { expect(3_i == 3); };
};

ut::suite parallel_2 = [] {
  using namespace ut;

  // sequential
  "test.2.1"_test = [] { expect(1_i == 1); };
  "test.2.2"_test = [] { expect(2_i == 2); };
  "test.2.3"_test = [] { expect(3_i == 3); };
};

int main() { return ut::cfg<ut::override>.run(); }