Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:53:28

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
0008 #ifndef CATCH_SHARDING_HPP_INCLUDED
0009 #define CATCH_SHARDING_HPP_INCLUDED
0010 
0011 #include <cassert>
0012 #include <cmath>
0013 #include <algorithm>
0014 
0015 namespace Catch {
0016 
0017     template<typename Container>
0018     Container createShard(Container const& container, std::size_t const shardCount, std::size_t const shardIndex) {
0019         assert(shardCount > shardIndex);
0020 
0021         if (shardCount == 1) {
0022             return container;
0023         }
0024 
0025         const std::size_t totalTestCount = container.size();
0026 
0027         const std::size_t shardSize = totalTestCount / shardCount;
0028         const std::size_t leftoverTests = totalTestCount % shardCount;
0029 
0030         const std::size_t startIndex = shardIndex * shardSize + (std::min)(shardIndex, leftoverTests);
0031         const std::size_t endIndex = (shardIndex + 1) * shardSize + (std::min)(shardIndex + 1, leftoverTests);
0032 
0033         auto startIterator = std::next(container.begin(), static_cast<std::ptrdiff_t>(startIndex));
0034         auto endIterator = std::next(container.begin(), static_cast<std::ptrdiff_t>(endIndex));
0035 
0036         return Container(startIterator, endIterator);
0037     }
0038 
0039 }
0040 
0041 #endif // CATCH_SHARDING_HPP_INCLUDED