diff --git a/tests/src/unit-json_view.cpp b/tests/src/unit-json_view.cpp index 8eb7bdcd0..95dcc63a7 100644 --- a/tests/src/unit-json_view.cpp +++ b/tests/src/unit-json_view.cpp @@ -1156,31 +1156,38 @@ TEST_CASE("json_view dump") // through the conversion; both as dump() writes them { std::mt19937_64 tokens(1170); // NOLINT(cert-msc32-c,cert-msc51-cpp,bugprone-random-generator-seed) + // a number below n; the remainder is a std::uint64_t, which is + // std::size_t on some platforms and wider on others + const auto draw = [&tokens](std::size_t n) + { + const std::uint64_t r = tokens() % n; + return static_cast(r); + }; std::string many_tokens = "["; for (int i = 0; i < 20000; ++i) { - const auto length = static_cast(1 + (tokens() % 17)); - std::string digits(1, static_cast('1' + (tokens() % 9))); + const std::size_t length = 1 + draw(17); + std::string digits(1, static_cast('1' + draw(9))); for (std::size_t k = 1; k < length; ++k) { - digits += static_cast('0' + (tokens() % 10)); + digits += static_cast('0' + draw(10)); } - digits += std::string(tokens() % 4, '0'); // trailing zeros - std::string token = tokens() % 3 == 0 ? "-" : ""; - const auto point = static_cast(tokens() % (digits.size() + 1)); + digits += std::string(draw(4), '0'); // trailing zeros + std::string token = draw(3) == 0 ? "-" : ""; + const std::size_t point = draw(digits.size() + 1); if (point == 0) { - token += "0." + std::string(tokens() % 5, '0') + digits; + token += "0." + std::string(draw(5), '0') + digits; } else { token += digits.substr(0, point) + (point < digits.size() ? "." + digits.substr(point) : ""); } // an exponent that keeps the value between about 1e-320 and 1e300 - const int exponent = static_cast(tokens() % 600) - 300 - static_cast(point); - if (tokens() % 4 != 0) + const int exponent = static_cast(draw(600)) - 300 - static_cast(point); + if (draw(4) != 0) { - token += (tokens() % 2 == 0 ? "e" : "E") + std::string(exponent >= 0 && tokens() % 2 == 0 ? "+" : "") + std::to_string(exponent); + token += (draw(2) == 0 ? "e" : "E") + std::string(exponent >= 0 && draw(2) == 0 ? "+" : "") + std::to_string(exponent); } else if (point == digits.size()) {