| 1 | |
| 2 | // Copyright Catch2 Authors |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE.txt or copy at |
| 5 | // https://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | |
| 9 | #include <catch2/catch_test_macros.hpp> |
| 10 | #include <catch2/generators/catch_generators_all.hpp> |
| 11 | |
| 12 | #include <catch2/internal/catch_sharding.hpp> |
| 13 | |
| 14 | #include <unordered_map> |
| 15 | #include <vector> |
| 16 | |
| 17 | TEST_CASE("Sharding Function", "[approvals]") { |
| 18 | std::vector<int> testContainer = { 0, 1, 2, 3, 4, 5, 6 }; |
| 19 | std::unordered_map<int, std::vector<std::size_t>> expectedShardSizes = { |
| 20 | {1, {7}}, |
| 21 | {2, {4, 3}}, |
| 22 | {3, {3, 2, 2}}, |
| 23 | {4, {2, 2, 2, 1}}, |
| 24 | {5, {2, 2, 1, 1, 1}}, |
| 25 | {6, {2, 1, 1, 1, 1, 1}}, |
| 26 | {7, {1, 1, 1, 1, 1, 1, 1}}, |
| 27 | }; |
| 28 | |
| 29 | auto shardCount = GENERATE(range(1, 7)); |
| 30 | auto shardIndex = GENERATE_COPY(filter([=](int i) { return i < shardCount; }, range(0, 6))); |
| 31 | |
| 32 | std::vector<int> result = Catch::createShard(testContainer, shardCount, shardIndex); |
| 33 | |
| 34 | auto& sizes = expectedShardSizes[shardCount]; |
| 35 | REQUIRE(result.size() == sizes[shardIndex]); |
| 36 | |
| 37 | std::size_t startIndex = 0; |
| 38 | for(int i = 0; i < shardIndex; i++) { |
| 39 | startIndex += sizes[i]; |
| 40 | } |
| 41 | |
| 42 | for(std::size_t i = 0; i < sizes[shardIndex]; i++) { |
| 43 | CHECK(result[i] == testContainer[i + startIndex]); |
| 44 | } |
| 45 | } |