Description: tests: use in-range grid indices in distribute_sparse_vector
 The "recv_sizes" test cases drew the grid indices for
 all_to_all_recv_sizes() from the inclusive range [0, nodes * N], while a grid
 split over "nodes" nodes only has the valid indices [0, nodes * N). An index
 equal to nodes * N belongs to a node that does not exist, which makes the
 library's "assert (group == nodes)" fire.  In addition the bound of the
 distribution was computed in 32 bit arithmetic, so "nodes * N" overflowed for
 the larger images (e.g. 2 * 32768^2) and the std::uniform_int_distribution
 was ill-formed; libstdc++ 15 aborts on that in non-optimised builds with
 "Assertion '_M_a <= _M_b' failed".
 .
 Bound the distributions by the largest valid index, computed in 64 bit
 arithmetic, so that the tests only exercise valid grids.  Clamping the bound
 to the range of t_int keeps the expected-to-throw combinations working.
Author: tangxiaofeng <tangxiaofeng@kylinos.cn>
Origin: vendor
Forwarded: no
Last-Update: 2026-10-20
--- purify-5.0.1.orig/cpp/tests/distribute_sparse_vector.cc
+++ purify-5.0.1/cpp/tests/distribute_sparse_vector.cc
@@ -135,7 +135,17 @@ TEST_CASE("recv_sizes") {
       std::random_device rnd_device;
       // Specify the engine and distribution.
       std::mt19937_64 mersenne_engine(rnd_device());  // Generates random integers
-      std::uniform_int_distribution<t_int> dist(0, nodes * N);
+      // The grid indices are in [0, nodes * N), so the distribution has to be
+      // bounded by the largest valid index. Compute that bound in 64 bit
+      // arithmetic, since `nodes * N` overflows a 32 bit int for the larger
+      // images; when it does not fit into t_int the values are only used for
+      // the rejection test below, so clamping to the t_int range is enough.
+      const std::int64_t indices_range =
+          static_cast<std::int64_t>(nodes) * static_cast<std::int64_t>(N);
+      const t_int max_index = indices_range - 1 > std::numeric_limits<t_int>::max()
+                                  ? std::numeric_limits<t_int>::max()
+                                  : static_cast<t_int>(indices_range - 1);
+      std::uniform_int_distribution<t_int> dist(0, max_index);
       auto gen = [&dist, &mersenne_engine]() { return dist(mersenne_engine); };
       std::vector<t_int> local_indices(10);
       std::generate(local_indices.begin(), local_indices.end(), gen);
@@ -164,8 +174,9 @@ TEST_CASE("recv_sizes 64 bit") {
       std::random_device rnd_device;
       // Specify the engine and distribution.
       std::mt19937_64 mersenne_engine(rnd_device());  // Generates random integers
+      // The grid indices are in [0, nodes * N), as in the "recv_sizes" case.
       std::uniform_int_distribution<std::int64_t> dist(
-          0, static_cast<std::int64_t>(nodes) * static_cast<std::int64_t>(N));
+          0, static_cast<std::int64_t>(nodes) * static_cast<std::int64_t>(N) - 1);
       auto gen = [&dist, &mersenne_engine]() -> std::int64_t { return dist(mersenne_engine); };
       std::vector<std::int64_t> local_indices(10);
       std::generate(local_indices.begin(), local_indices.end(), gen);
