File indexing completed on 2025-01-18 09:54:06
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef CATCH_SHARDING_HPP_INCLUDED
0009 #define CATCH_SHARDING_HPP_INCLUDED
0010
0011 #include <catch2/catch_session.hpp>
0012
0013 #include <cmath>
0014 #include <algorithm>
0015
0016 namespace Catch {
0017
0018 template<typename Container>
0019 Container createShard(Container const& container, std::size_t const shardCount, std::size_t const shardIndex) {
0020 assert(shardCount > shardIndex);
0021
0022 if (shardCount == 1) {
0023 return container;
0024 }
0025
0026 const std::size_t totalTestCount = container.size();
0027
0028 const std::size_t shardSize = totalTestCount / shardCount;
0029 const std::size_t leftoverTests = totalTestCount % shardCount;
0030
0031 const std::size_t startIndex = shardIndex * shardSize + (std::min)(shardIndex, leftoverTests);
0032 const std::size_t endIndex = (shardIndex + 1) * shardSize + (std::min)(shardIndex + 1, leftoverTests);
0033
0034 auto startIterator = std::next(container.begin(), static_cast<std::ptrdiff_t>(startIndex));
0035 auto endIterator = std::next(container.begin(), static_cast<std::ptrdiff_t>(endIndex));
0036
0037 return Container(startIterator, endIterator);
0038 }
0039
0040 }
0041
0042 #endif