Sharding.tests.cpp 1.4 KB

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