forked from catchorg/Catch2
/
catch_sharding.hpp
42 lines (28 loc) · 1.36 KB
/
catch_sharding.hpp
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
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
#ifndef CATCH_SHARDING_HPP_INCLUDED
#define CATCH_SHARDING_HPP_INCLUDED
#include <catch2/catch_session.hpp>
#include <cmath>
#include <algorithm>
namespace Catch {
template<typename Container>
Container createShard(Container const& container, std::size_t const shardCount, std::size_t const shardIndex) {
assert(shardCount > shardIndex);
if (shardCount == 1) {
return container;
}
const std::size_t totalTestCount = container.size();
const std::size_t shardSize = totalTestCount / shardCount;
const std::size_t leftoverTests = totalTestCount % shardCount;
const std::size_t startIndex = shardIndex * shardSize + (std::min)(shardIndex, leftoverTests);
const std::size_t endIndex = (shardIndex + 1) * shardSize + (std::min)(shardIndex + 1, leftoverTests);
auto startIterator = std::next(container.begin(), static_cast<std::ptrdiff_t>(startIndex));
auto endIterator = std::next(container.begin(), static_cast<std::ptrdiff_t>(endIndex));
return Container(startIterator, endIterator);
}
}
#endif // CATCH_SHARDING_HPP_INCLUDED